Files
xSaherelmWorkspace/Documents/Docs/Developer/Interview MCP Client Extends.html
2026-11-06 21:00:06 +03:30

1054 lines
39 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>پیشنهاد پیاده‌سازی McpClient سفارشی | رفع مشکل انتقال هدر و احراز هویت</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--danger: #e53e3e;
--warning: #dd6b20;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 950px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.document-header::before {
content: '';
position: absolute;
top: 0; right: 0;
width: 200px; height: 200px;
background: radial-gradient(circle, rgba(201,169,97,0.15) 0%, transparent 70%);
border-radius: 50%;
transform: translate(50%, -50%);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
position: relative;
z-index: 1;
}
.company-badge {
display: flex;
align-items: center;
gap: 12px;
}
.company-logo {
width: 60px; height: 60px;
background: var(--accent);
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.doc-meta {
text-align: left;
font-size: 12px;
opacity: 0.9;
}
.doc-meta div { margin-bottom: 3px; }
.document-title {
text-align: center;
position: relative;
z-index: 1;
padding: 20px 0;
}
.document-title .label {
display: inline-block;
background: rgba(201,169,97,0.2);
color: var(--accent);
padding: 4px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-bottom: 15px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-title h1 {
font-size: 28px;
font-weight: 800;
margin-bottom: 10px;
letter-spacing: -0.5px;
}
.document-title h2 {
font-size: 16px;
font-weight: 400;
opacity: 0.9;
}
.document-body {
padding: 40px;
}
.intro-text {
background: linear-gradient(to left, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin-bottom: 35px;
font-size: 14px;
color: var(--text-muted);
}
.section {
margin-bottom: 40px;
page-break-inside: avoid;
}
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 20px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
}
.section-title { flex: 1; }
.section-title h3 {
font-size: 19px;
font-weight: 700;
color: var(--primary);
margin-bottom: 2px;
}
.section-title .subtitle {
font-size: 12px;
color: var(--text-muted);
}
.section-content {
padding-right: 10px;
}
.section-content p {
margin-bottom: 12px;
text-align: justify;
text-indent: 20px;
}
.section-content p.no-indent { text-indent: 0; }
.highlight-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #f6e05e;
border-right: 4px solid var(--accent);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.highlight-box strong {
color: var(--accent-dark);
}
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.danger-box {
background: linear-gradient(to left, #fff5f5, #fed7d7);
border: 1px solid #fc8181;
border-right: 4px solid var(--danger);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.warning-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #fbd38d;
border-right: 4px solid var(--warning);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.styled-table thead {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
}
.styled-table th {
padding: 14px 16px;
text-align: right;
font-weight: 600;
font-size: 13px;
}
.styled-table td {
padding: 13px 16px;
border-bottom: 1px solid var(--border);
font-size: 13px;
}
.styled-table tbody tr:last-child td { border-bottom: none; }
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
.styled-table .highlight-row {
background: #fffaf0 !important;
font-weight: 600;
}
.comparison-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 25px 0;
}
.comparison-card {
background: #f8fafc;
border: 2px solid var(--border);
border-radius: 10px;
padding: 20px;
position: relative;
}
.comparison-card.recommended {
border-color: var(--success);
background: linear-gradient(to left, #f0fff4, #e6fffa);
}
.comparison-card .card-badge {
position: absolute;
top: -10px;
right: 20px;
background: var(--success);
color: white;
padding: 3px 12px;
border-radius: 15px;
font-size: 11px;
font-weight: 600;
}
.comparison-card h4 {
font-size: 16px;
font-weight: 700;
color: var(--primary);
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 2px solid var(--border);
}
.comparison-card.recommended h4 {
border-bottom-color: var(--success);
}
.comparison-card ul {
list-style: none;
padding: 0;
}
.comparison-card ul li {
padding: 8px 0;
border-bottom: 1px solid var(--border);
font-size: 13px;
display: flex;
justify-content: space-between;
}
.comparison-card ul li:last-child { border-bottom: none; }
.comparison-card ul li .value {
font-weight: 600;
color: var(--primary);
}
.code-block {
background: #1a202c;
color: #e2e8f0;
border-radius: 8px;
padding: 20px;
margin: 20px 0;
font-family: 'Consolas', 'Courier New', monospace;
font-size: 12px;
line-height: 1.6;
direction: ltr;
text-align: left;
overflow-x: auto;
border: 1px solid #2d3748;
}
.code-block .comment { color: #68d391; }
.code-block .keyword { color: #f6ad55; }
.code-block .string { color: #fc8181; }
.code-block .type { color: #63b3ed; }
.quote-box {
background: linear-gradient(135deg, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin: 20px 0;
position: relative;
}
.quote-box::before {
content: '"';
position: absolute;
top: -10px;
right: 15px;
font-size: 60px;
color: var(--accent);
opacity: 0.3;
font-family: Georgia, serif;
}
.quote-box p {
font-style: italic;
color: var(--text-muted);
margin-bottom: 10px;
text-indent: 0 !important;
}
.quote-box .source {
font-size: 12px;
color: var(--text-muted);
text-align: left;
font-style: normal;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
margin: 25px 0;
}
.stat-card {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
box-shadow: 0 4px 12px rgba(26,54,93,0.2);
}
.stat-card .stat-value {
font-size: 24px;
font-weight: 800;
margin-bottom: 5px;
color: var(--accent);
line-height: 1.3;
}
.stat-card .stat-label {
font-size: 12px;
opacity: 0.9;
}
.timeline {
position: relative;
padding: 20px 0;
margin: 25px 0;
}
.timeline::before {
content: '';
position: absolute;
right: 50%;
top: 0;
bottom: 0;
width: 3px;
background: var(--border);
transform: translateX(50%);
}
.timeline-item {
display: flex;
margin-bottom: 20px;
position: relative;
}
.timeline-item:nth-child(odd) {
flex-direction: row-reverse;
}
.timeline-content {
width: 45%;
background: #f8fafc;
border: 1px solid var(--border);
border-radius: 8px;
padding: 15px;
position: relative;
}
.timeline-item:nth-child(odd) .timeline-content {
margin-left: 5%;
}
.timeline-item:nth-child(even) .timeline-content {
margin-right: 5%;
}
.timeline-dot {
position: absolute;
right: 50%;
top: 20px;
width: 16px;
height: 16px;
background: var(--accent);
border: 3px solid white;
border-radius: 50%;
transform: translateX(50%);
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
z-index: 1;
}
.timeline-content h5 {
font-size: 14px;
font-weight: 700;
color: var(--primary);
margin-bottom: 8px;
}
.timeline-content p {
font-size: 12px;
color: var(--text-muted);
text-indent: 0 !important;
margin-bottom: 0 !important;
}
.reference-list {
list-style: none;
padding: 0;
margin: 20px 0;
}
.reference-list li {
background: #f8fafc;
border-right: 3px solid var(--primary-light);
padding: 15px 20px;
margin-bottom: 10px;
border-radius: 6px;
font-size: 13px;
}
.reference-list li strong {
color: var(--primary);
display: block;
margin-bottom: 5px;
}
.reference-list li .ref-source {
font-size: 11px;
color: var(--text-muted);
margin-top: 5px;
}
.conclusion-box {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 30px;
border-radius: 12px;
margin: 30px 0;
text-align: center;
box-shadow: 0 8px 20px rgba(26,54,93,0.3);
}
.conclusion-box h3 {
font-size: 22px;
font-weight: 800;
margin-bottom: 15px;
color: var(--accent);
}
.conclusion-box p {
font-size: 14px;
line-height: 1.8;
text-indent: 0 !important;
margin-bottom: 15px;
}
.conclusion-box .highlight {
background: rgba(201,169,97,0.2);
padding: 15px 20px;
border-radius: 8px;
margin-top: 20px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.document-footer .footer-brand {
font-weight: 700;
color: var(--accent);
margin-bottom: 5px;
font-size: 14px;
}
.footer-divider {
width: 60px;
height: 2px;
background: var(--accent);
margin: 10px auto;
}
.page-info {
text-align: center;
padding: 15px;
font-size: 11px;
color: var(--text-muted);
border-top: 1px solid var(--border);
margin-top: 30px;
}
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; border-radius: 0; }
.section { page-break-inside: avoid; }
}
@media (max-width: 768px) {
.document-body { padding: 25px 20px; }
.document-header { padding: 30px 20px; }
.comparison-grid { grid-template-columns: 1fr; }
.stats-grid { grid-template-columns: 1fr; }
.header-top { flex-direction: column; gap: 15px; }
.doc-meta { text-align: center; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="company-badge">
<div class="company-logo"></div>
</div>
<div class="doc-meta">
<div>شماره مستند: ARCH-1405-014</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: آبان ۱۴۰۵</div>
<div>طبقه‌بندی: داخلی فنی</div>
</div>
</div>
<div class="document-title">
<span class="label">ARCHITECTURE DECISION RECORD</span>
<h1>پیاده‌سازی McpClient سفارشی در xAiApi</h1>
<h2>رفع مشکل انتقال هدرها، اعتبارسنجی و احراز هویت در یکپارچه‌سازی MCP</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>خلاصه تصمیم فنی:</strong><br>
در یکپارچه‌سازی فعلی بین <strong>xAiApi</strong> (به‌عنوان MCP Client) و <strong>xAiMcpServer</strong> (به‌عنوان MCP Server)، از کتابخانه‌ی رسمی <code>ModelContextProtocol.Client</code> استفاده می‌شود. مشاهدات عملی نشان می‌دهد که هدرهای سفارشی (Authorization، X-Powered-By، AccessToken، RefreshToken و …) در درخواست‌های JSON-RPC و SSE به درستی به سرور منتقل نمی‌شوند و در نتیجه احراز هویت و اعتبارسنجی سمت سرور ناموفق می‌ماند. این مستند دلایل فنی، راه‌حل پیشنهادی و طرح پیاده‌سازی یک <strong>McpClient سفارشی</strong> را تشریح می‌کند.
</div>
<!-- SECTION 1 -->
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>بیان مسئله و شواهد موجود</h3>
<div class="subtitle">Problem Statement & Current Evidence</div>
</div>
</div>
<div class="section-content">
<p>در پیاده‌سازی فعلی، متد <code>CreateClientAsync</code> در <code>XAiMcpServerProvider</code> تلاش می‌کند هدرهای احراز هویت را از طریق <code>HttpClientTransportOptions.AdditionalHeaders</code> به کلاینت MCP تزریق کند. اما در عمل، سرور MCP درخواست را بدون هدرهای مورد انتظار دریافت می‌کند و پاسخ <code>401 Unauthorized</code> یا خطای «Access Denied» بازمی‌گرداند.</p>
<div class="danger-box">
<strong>⚠️ نشانه‌های مشکل در لاگ‌ها و رفتار سیستم:</strong>
<ul style="padding-right: 20px; margin-top: 10px;">
<li>درخواست <code>initialize</code> به سرور MCP ارسال می‌شود اما سرور هدر <code>Authorization</code> را دریافت نمی‌کند.</li>
<li>پس از فعال‌شدن موفق اتصال اولیه (گاهی)، درخواست‌های <code>tools/list</code> و <code>tools/call</code> بدون هدرهای توکن ارسال می‌شوند.</li>
<li><code>HttpTransportMode.AutoDetect</code> در SDK، ممکن است بین Streamable HTTP و SSE جابه‌جا شود و هدرهای اضافی در همه‌ی حالت‌ها حفظ نشوند.</li>
<li>هدرهای <code>X-Powered-By</code>، <code>X-Expires-At</code> و <code>X-Refresh-Token</code> که سرور برای اعتبارسنجی نیاز دارد، به هیچ‌وجه در Request دیده نمی‌شوند.</li>
<li>در بعضی سناریوها، SDK کلاینت HTTP داخلی خود را می‌سازد و از <code>IHttpClientFactory</code> یا <code>HttpClient</code> سفارشی استفاده نمی‌کند.</li>
</ul>
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">تحلیل ریشه‌ای:</h4>
<table class="styled-table">
<thead>
<tr>
<th>لایه</th>
<th>انتظار ما</th>
<th>واقعیت در SDK</th>
<th>نتیجه</th>
</tr>
</thead>
<tbody>
<tr>
<td>HTTP Transport</td>
<td>استفاده از HttpClient سفارشی با هدرهای کامل</td>
<td>ساخت HttpClient داخلی و نادیده‌گرفتن برخی تنظیمات</td>
<td style="color: var(--danger); font-weight: 600;">هدرها ارسال نمی‌شوند</td>
</tr>
<tr>
<td>SSE / Streamable</td>
<td>حفظ هدرها در هر دو حالت</td>
<td>AutoDetect ممکن است هدرها را در حالت SSE اعمال نکند</td>
<td style="color: var(--danger); font-weight: 600;">خطای احراز هویت</td>
</tr>
<tr>
<td>Session</td>
<td>کنترل کامل بر SessionId و درخواست‌های پسین</td>
<td>SDK مدیریت می‌کند و اجازه تزریق هدر در درخواست‌های بعدی نمی‌دهد</td>
<td style="color: var(--danger); font-weight: 600;">قطع ارتباط بعد از initialize</td>
</tr>
<tr class="highlight-row">
<td><strong>جمع‌بندی</strong></td>
<td>کلاینت کاملاً قابل تنظیم</td>
<td>کلاینت Black-Box با کنترل محدود</td>
<td style="color: var(--danger); font-weight: 700;"><strong>عدم کارکرد در محیط واقعی</strong></td>
</tr>
</tbody>
</table>
<div class="warning-box">
<strong>📌 نکته مهم:</strong> سرور MCP ما (<code>xAiMcpServer</code>) از پالیسی <code>XPolicies.EnabledUser</code> و هدرهای اختصاصی <code>X-Powered-By</code>، <code>X-Expires-At</code>، <code>X-Refresh-Token</code> و <code>X-Access-Token</code> استفاده می‌کند. بدون انتقال دقیق این هدرها، عملاً هیچ ابزاری (Tool) قابل فراخوانی نیست.
</div>
</div>
</div>
<!-- SECTION 2 -->
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>مقایسه SDK رسمی با McpClient سفارشی</h3>
<div class="subtitle">Official SDK vs. Custom McpClient</div>
</div>
</div>
<div class="section-content">
<p>دو مسیر پیش‌رو داریم: ادامه‌ی استفاده از SDK رسمی با دور زدن محدودیت‌ها، یا پیاده‌سازی یک کلاینت سبک اختصاصی که پروتکل JSON-RPC 2.0 را مستقیماً پیاده می‌کند.</p>
<div class="comparison-grid">
<div class="comparison-card">
<h4>SDK رسمی ModelContextProtocol.Client</h4>
<ul>
<li><span>کنترل هدر HTTP</span> <span class="value" style="color: var(--danger);">محدود</span></li>
<li><span>پشتیبانی از هدرهای سفارشی در همه‌ی حالت‌ها</span> <span class="value" style="color: var(--danger);">ناکامل</span></li>
<li><span>کنترل Session و Reconnect</span> <span class="value" style="color: var(--danger);">سیاه‌جعبه</span></li>
<li><span>سازگاری با سرور اختصاصی ما</span> <span class="value" style="color: var(--danger);">ناکامل</span></li>
<li><span>هزینه نگهداری</span> <span class="value" style="color: var(--success);">کم</span></li>
<li><span>شفافیت کد</span> <span class="value" style="color: var(--danger);">پایین</span></li>
</ul>
</div>
<div class="comparison-card recommended">
<span class="card-badge">پیشنهاد ما ✓</span>
<h4>McpClient سفارشی (XAiMcpClient)</h4>
<ul>
<li><span>کنترل هدر HTTP</span> <span class="value" style="color: var(--success);">کامل</span></li>
<li><span>پشتیبانی از هدرهای سفارشی در همه‌ی حالت‌ها</span> <span class="value" style="color: var(--success);">کامل</span></li>
<li><span>کنترل Session و Reconnect</span> <span class="value" style="color: var(--success);">شفاف و دستی</span></li>
<li><span>سازگاری با سرور اختصاصی ما</span> <span class="value" style="color: var(--success);">۱۰۰٪</span></li>
<li><span>هزینه نگهداری</span> <span class="value" style="color: var(--warning);">متوسط</span></li>
<li><span>شفافیت کد</span> <span class="value" style="color: var(--success);">بالا</span></li>
</ul>
</div>
</div>
<div class="info-box">
<strong>💡 تصمیم:</strong> چون سرور MCP ما درون‌سازمانی است و از پروتکل JSON-RPC 2.0 بر روی HTTP/SSE پیروی می‌کند، پیاده‌سازی یک کلاینت اختصاصی که صرفاً سه متد <code>initialize</code>، <code>tools/list</code> و <code>tools/call</code> را پشتیبانی کند، بسیار ساده‌تر و قابل کنترل‌تر از تلاش برای هم‌راستاکردن SDK رسمی است.
</div>
</div>
</div>
<!-- SECTION 3 -->
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>معماری پیشنهادی XAiMcpClient</h3>
<div class="subtitle">Proposed Architecture</div>
</div>
</div>
<div class="section-content">
<p>کلاینت سفارشی در قالب یک کلاس <code>XAiMcpClient</code> پیاده می‌شود که مسئولیت‌های زیر را بر عهده دارد:</p>
<table class="styled-table">
<thead>
<tr>
<th>کامپوننت</th>
<th>مسئولیت</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>XAiMcpClient</strong></td>
<td>مدیریت اتصال، ارسال JSON-RPC، دریافت پاسخ و نگهداری SessionId</td>
</tr>
<tr>
<td><strong>XAiMcpHttpTransport</strong></td>
<td>ارسال درخواست POST به endpoint MCP با هدرهای کامل و مدیریت پاسخ‌های SSE</td>
</tr>
<tr>
<td><strong>XAiMcpSseReader</strong></td>
<td>پارس کردن استریم SSE دریافتی از سرور (رویدادهای <code>event:</code> و <code>data:</code>)</td>
</tr>
<tr>
<td><strong>XAiMcpHeaderBuilder</strong></td>
<td>ساخت مجموعه هدرهای احراز هویت از روی <code>XUserClaimsInfoDto</code></td>
</tr>
<tr>
<td><strong>XAiMcpSessionStore</strong></td>
<td>نگهداری SessionId به‌ازای هر (ServerId, UserId) و مدیریت TTL</td>
</tr>
<tr>
<td><strong>XAiMcpToolProxy</strong></td>
<td>تبدیل <code>XAiMcpToolDescriptor</code> به <code>McpClientTool</code> سازگار با <code>Microsoft.Extensions.AI</code></td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">نمونه‌ی شماتیک هدرها:</h4>
<div class="code-block">
<span class="comment">// XAiMcpHeaderBuilder.Build(userInfo, configuration)</span>
{
<span class="string">"Authorization"</span>: <span class="string">"Bearer {userInfo.AccessToken}"</span>,
<span class="string">"X-Powered-By"</span>: <span class="string">"{configuration.XPoweredValue}"</span>,
<span class="string">"X-Expires-At"</span>: <span class="string">"{userInfo.ExpiresAt}"</span>,
<span class="string">"X-Refresh-Token"</span>: <span class="string">"{userInfo.RefreshToken}"</span>,
<span class="string">"X-Connection-Id"</span>: <span class="string">"{connectionId}"</span>,
<span class="string">"Accept"</span>: <span class="string">"application/json, text/event-stream"</span>,
<span class="string">"Content-Type"</span>: <span class="string">"application/json"</span>,
<span class="string">"Mcp-Session-Id"</span>: <span class="string">"{sessionId}"</span> <span class="comment">// بعد از initialize</span>
}
</div>
<div class="success-box">
<strong>✓ مزیت کلیدی:</strong> با این معماری، هر درخواست JSON-RPC به‌صورت کامل و با هدرهای احراز هویت ساخته می‌شود و دیگر وابسته به رفتار داخلی SDK نیستیم. هم‌چنین امکان لاگ‌گیری دقیق درخواست/پاسخ برای عیب‌یابی فراهم می‌شود.
</div>
</div>
</div>
<!-- SECTION 4 -->
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>طرح پیاده‌سازی گام‌به‌گام</h3>
<div class="subtitle">Implementation Roadmap</div>
</div>
</div>
<div class="section-content">
<div class="timeline">
<div class="timeline-item">
<div class="timeline-dot"></div>
<div class="timeline-content">
<h5>گام ۱: لایه‌ی Transport</h5>
<p>پیاده‌سازی <code>XAiMcpHttpTransport</code> با استفاده از <code>IHttpClientFactory</code> سفارشی و پشتیبانی از هدرهای کامل. این لایه مستقل از MCP است و صرفاً یک ارسال‌کننده‌ی HTTP است.</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-dot"></div>
<div class="timeline-content">
<h5>گام ۲: پیاده‌سازی JSON-RPC</h5>
<p>ساخت کلاس <code>XAiMcpClient</code> با متدهای <code>InitializeAsync</code>، <code>ListToolsAsync</code> و <code>CallToolAsync</code>. هر متد یک payload استاندارد JSON-RPC 2.0 می‌سازد.</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-dot"></div>
<div class="timeline-content">
<h5>گام ۳: مدیریت SSE</h5>
<p>پیاده‌سازی <code>XAiMcpSseReader</code> برای خواندن پاسخ‌های <code>text/event-stream</code> و استخراج <code>data:</code> به‌صورت Async.</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-dot"></div>
<div class="timeline-content">
<h5>گام ۴: مدیریت Session</h5>
<p>نگهداری <code>Mcp-Session-Id</code> برگشتی از <code>initialize</code> و تزریق آن در تمام درخواست‌های بعدی. مدیریت TTL و Reconnect.</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-dot"></div>
<div class="timeline-content">
<h5>گام ۵: آداپتور به Microsoft.Extensions.AI</h5>
<p>پیاده‌سازی <code>XAiMcpToolProxy</code> که خروجی <code>ListToolsAsync</code> را به <code>McpClientTool</code> قابل استفاده در <code>ChatOptions.Tools</code> تبدیل می‌کند.</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-dot"></div>
<div class="timeline-content">
<h5>گام ۶: جایگزینی در XAiMcpServerProvider</h5>
<p>حذف وابستگی به <code>ModelContextProtocol.Client</code> و جایگزینی <code>CreateClientAsync</code> با نمونه‌سازی از <code>XAiMcpClient</code>.</p>
</div>
</div>
</div>
<div class="info-box">
<strong>📦 خروجی مورد انتظار:</strong> یک پکیج داخلی (<code>xAiMcpClient</code>) شامل کلاس‌های <code>XAiMcpClient</code>، <code>XAiMcpHttpTransport</code>، <code>XAiMcpSseReader</code> و <code>XAiMcpSessionStore</code> که در DI به‌صورت Scoped ثبت می‌شود و در <code>XAiMcpServerProvider</code> مورد استفاده قرار می‌گیرد.
</div>
</div>
</div>
<!-- SECTION 5 -->
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>مزایای کلیدی راه‌حل سفارشی</h3>
<div class="subtitle">Key Benefits of the Custom Client</div>
</div>
</div>
<div class="section-content">
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value">۱۰۰٪<br>کنترل هدر</div>
<div class="stat-label">انتقال دقیق هدرهای احراز هویت در هر درخواست</div>
</div>
<div class="stat-card">
<div class="stat-value">شفافیت<br>کامل</div>
<div class="stat-label">کد قابل خواندن و قابل عیب‌یابی</div>
</div>
<div class="stat-card">
<div class="stat-value">حذف<br>وابستگی</div>
<div class="stat-label">عدم وابستگی به SDK خارجی با رفتار غیرقابل پیش‌بینی</div>
</div>
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">مزایای تفصیلی:</h4>
<ul style="padding-right: 20px; margin-top: 10px;">
<li style="margin-bottom: 10px;"><strong>کنترل کامل بر هدرها:</strong> هر درخواست JSON-RPC، هدرهای <code>Authorization</code>، <code>X-Powered-By</code>، <code>X-Expires-At</code> و <code>X-Refresh-Token</code> را دقیقاً مطابق نیاز سرور ارسال می‌کند.</li>
<li style="margin-bottom: 10px;"><strong>پشتیبانی کامل از SSE و Streamable HTTP:</strong> هر دو حالت به‌صورت شفاف پیاده‌سازی می‌شوند و انتخاب حالت توسط یک فلگ در تنظیمات انجام می‌شود.</li>
<li style="margin-bottom: 10px;"><strong>مدیریت Session قابل پیش‌بینی:</strong> SessionId به‌ازای هر (ServerId, UserId) در یک <code>ConcurrentDictionary</code> نگهداری می‌شود و در صورت انقضا، به‌صورت خودکار Reconnect می‌شود.</li>
<li style="margin-bottom: 10px;"><strong>لاگ‌گیری و عیب‌یابی آسان:</strong> هر درخواست و پاسخ با جزئیات کامل (بدون توکن‌های حساس) قابل لاگ‌گیری است.</li>
<li style="margin-bottom: 10px;"><strong>آداپتور سازگار با Microsoft.Extensions.AI:</strong> خروجی ListTools به <code>McpClientTool</code> قابل استفاده در <code>ChatOptions.Tools</code> تبدیل می‌شود و نیازی به تغییر در <code>XAIServiceBase</code> نیست.</li>
<li style="margin-bottom: 10px;"><strong>قابلیت تست‌پذیری بالا:</strong> هر لایه (Transport، SSE Reader، Session Store) به‌صورت مستقل قابل Unit Test است.</li>
</ul>
<div class="success-box">
<strong>✓ نتیجه:</strong> با حذف SDK رسمی از مسیر حیاتی، عملاً مشکل «عدم انتقال هدرها و شکست احراز هویت» به‌طور کامل برطرف می‌شود و یکپارچه‌سازی MCP با xAiApi پایدار و قابل اتکا خواهد بود.
</div>
</div>
</div>
<!-- SECTION 6 -->
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>ریسک‌ها و راهکارهای کاهش آن‌ها</h3>
<div class="subtitle">Risks & Mitigations</div>
</div>
</div>
<div class="section-content">
<table class="styled-table">
<thead>
<tr>
<th>ریسک</th>
<th>احتمال</th>
<th>راهکار کاهش</th>
</tr>
</thead>
<tbody>
<tr>
<td>پیچیدگی پیاده‌سازی SSE Parser</td>
<td>متوسط</td>
<td>استفاده از <code>System.IO.Pipelines</code> و تست با سرور واقعی در محیط توسعه</td>
</tr>
<tr>
<td>همگام نبودن با تغییرات آینده‌ی پروتکل MCP</td>
<td>پایین</td>
<td>نسخه‌بندی کلاینت و پشتیبانی از <code>protocolVersion</code> در <code>initialize</code></td>
</tr>
<tr>
<td>افزایش بار نگهداری</td>
<td>متوسط</td>
<td>محدود کردن به سه متد ضروری و مستندسازی کامل</td>
</tr>
<tr>
<td>ناسازگاری با ابزارهای ثالث MCP</td>
<td>پایین</td>
<td>کلاینت فقط برای سرور داخلی xAiMcpServer طراحی می‌شود؛ SDK رسمی برای سناریوهای عمومی باقی می‌ماند</td>
</tr>
<tr class="highlight-row">
<td><strong>ارزیابی نهایی</strong></td>
<td><strong>قابل مدیریت</strong></td>
<td style="color: var(--success); font-weight: 700;">ریسک کلی: پایین</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- SECTION 7 -->
<div class="section">
<div class="section-header">
<div class="section-number">۷</div>
<div class="section-title">
<h3>جمع‌بندی و پیشنهاد نهایی</h3>
<div class="subtitle">Final Recommendation</div>
</div>
</div>
<div class="section-content">
<div class="conclusion-box">
<h3>🎯 تصمیم معماری</h3>
<p>با توجه به شواهد عملی و تحلیل فنی انجام‌شده، پیاده‌سازی <strong>XAiMcpClient سفارشی</strong> در <code>xAiApi</code> بهترین و مطمئن‌ترین راه‌حل برای رفع مشکل انتقال هدرها، احراز هویت و اعتبارسنجی در یکپارچه‌سازی MCP است.</p>
<p>این رویکرد با کنترل کامل بر HTTP Transport، مدیریت شفاف Session و آداپتور سازگار با <code>Microsoft.Extensions.AI</code>، هم‌زمان سادگی، پایداری و قابلیت نگهداری را فراهم می‌کند.</p>
<div class="highlight">
<strong>💎 ارزش پیشنهادی:</strong><br>
رفع کامل مشکل احراز هویت + کنترل ۱۰۰٪ بر هدرها + شفافیت کد + کاهش وابستگی خارجی + قابلیت تست‌پذیری بالا
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> در صورت تأیید تصمیم، پیاده‌سازی از گام ۱ (Transport Layer) آغاز می‌شود و پس از تکمیل هر گام، تست یکپارچگی با <code>xAiMcpServer</code> انجام می‌گیرد. پیش‌بینی می‌شود کل فرآیند در حدود ۳ تا ۵ روز کاری به پایان برسد.
</div>
</div>
</div>
<!-- REFERENCES -->
<div class="section">
<div class="section-header">
<div class="section-number">۸</div>
<div class="section-title">
<h3>منابع و مستندات مرجع</h3>
<div class="subtitle">References</div>
</div>
</div>
<div class="section-content">
<ul class="reference-list">
<li>
<strong>Model Context Protocol Specification</strong>
"JSON-RPC 2.0, Streamable HTTP Transport, SSE"
<div class="ref-source">منبع: https://modelcontextprotocol.io/specification</div>
</li>
<li>
<strong>Microsoft.Extensions.AI</strong>
"ChatOptions.Tools & McpClientTool Integration"
<div class="ref-source">منبع: https://learn.microsoft.com/dotnet/ai/</div>
</li>
<li>
<strong>ModelContextProtocol C# SDK</strong>
"HttpClientTransport & HttpTransportMode AutoDetect Limitations"
<div class="ref-source">منبع: https://github.com/modelcontextprotocol/csharp-sdk</div>
</li>
<li>
<strong>System.Net.ServerSentEvents</strong>
"SSE Parsing in .NET"
<div class="ref-source">منبع: https://learn.microsoft.com/dotnet/api/system.net.serversentevents</div>
</li>
<li>
<strong>IdentityModel.AspNetCore.OAuth2Introspection</strong>
"Bearer Token Validation & Back-Channel Auth"
<div class="ref-source">منبع: https://docs.duendesoftware.com/</div>
</li>
</ul>
</div>
</div>
<div class="page-info">
این مستند داخلی بوده و صرفاً جهت تصمیم‌گیری معماری در تیم فنی xSaherelmWorkspace تهیه شده است.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">طراحی و توسعه: هادی خزاعی اصل</div>
<div class="footer-divider"></div>
<div>تاریخ تهیه: آبان ۱۴۰۵ &nbsp;|&nbsp; نسخه: ۱.۰ &nbsp;|&nbsp; طبقه‌بندی: داخلی فنی</div>
</footer>
</div>
</body>
</html>