1248 lines
54 KiB
HTML
1248 lines
54 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="fa" dir="rtl">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>انتقال هویت کاربر از xAiApi به MCP Server | راهنمای گام به گام</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: 1000px;
|
||
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: 26px;
|
||
font-weight: 800;
|
||
margin-bottom: 10px;
|
||
letter-spacing: -0.5px;
|
||
}
|
||
|
||
.document-title h2 {
|
||
font-size: 15px;
|
||
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: 45px; 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;
|
||
}
|
||
|
||
.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);
|
||
}
|
||
|
||
.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: 22px;
|
||
font-weight: 800;
|
||
margin-bottom: 5px;
|
||
color: var(--accent);
|
||
line-height: 1.3;
|
||
}
|
||
|
||
.stat-card .stat-label {
|
||
font-size: 12px;
|
||
opacity: 0.9;
|
||
}
|
||
|
||
.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;
|
||
}
|
||
|
||
.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;
|
||
}
|
||
|
||
.code-block {
|
||
direction: ltr;
|
||
text-align: left;
|
||
background: #1a202c;
|
||
color: #e2e8f0;
|
||
padding: 18px 20px;
|
||
border-radius: 8px;
|
||
overflow-x: auto;
|
||
font-family: 'Consolas', 'Monaco', monospace;
|
||
font-size: 12.5px;
|
||
line-height: 1.65;
|
||
margin: 15px 0;
|
||
border-right: 4px solid var(--accent);
|
||
white-space: pre;
|
||
}
|
||
|
||
.code-block code { color: inherit; font-family: inherit; }
|
||
.code-block .c { color: #68d391; }
|
||
.code-block .k { color: #f6ad55; }
|
||
.code-block .s { color: #90cdf4; }
|
||
.code-block .t { color: #fbb6ce; }
|
||
|
||
.file-path {
|
||
display: inline-block;
|
||
background: #edf2f7;
|
||
color: var(--primary);
|
||
padding: 2px 10px;
|
||
border-radius: 4px;
|
||
font-family: 'Consolas', monospace;
|
||
font-size: 12px;
|
||
direction: ltr;
|
||
margin: 0 4px;
|
||
}
|
||
|
||
.step-list {
|
||
counter-reset: step;
|
||
list-style: none;
|
||
padding: 0;
|
||
margin: 20px 0;
|
||
}
|
||
|
||
.step-list > li {
|
||
counter-increment: step;
|
||
position: relative;
|
||
padding: 14px 60px 14px 15px;
|
||
background: #f8fafc;
|
||
border-right: 3px solid var(--accent);
|
||
border-radius: 6px;
|
||
margin-bottom: 10px;
|
||
font-size: 13.5px;
|
||
}
|
||
|
||
.step-list > li::before {
|
||
content: counter(step);
|
||
position: absolute;
|
||
right: 15px;
|
||
top: 50%;
|
||
transform: translateY(-50%);
|
||
width: 30px;
|
||
height: 30px;
|
||
background: var(--primary);
|
||
color: white;
|
||
border-radius: 50%;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
font-weight: 700;
|
||
font-size: 13px;
|
||
}
|
||
|
||
.checklist {
|
||
list-style: none;
|
||
padding: 0;
|
||
margin: 20px 0;
|
||
}
|
||
|
||
.checklist li {
|
||
padding: 10px 15px 10px 45px;
|
||
background: #f8fafc;
|
||
border-radius: 6px;
|
||
margin-bottom: 8px;
|
||
position: relative;
|
||
font-size: 13.5px;
|
||
}
|
||
|
||
.checklist li::before {
|
||
content: '✓';
|
||
position: absolute;
|
||
left: 15px;
|
||
top: 50%;
|
||
transform: translateY(-50%);
|
||
width: 22px;
|
||
height: 22px;
|
||
background: var(--success);
|
||
color: white;
|
||
border-radius: 50%;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
font-weight: 700;
|
||
font-size: 12px;
|
||
}
|
||
|
||
@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>شماره مستند: TECH-MCP-TOKEN-001</div>
|
||
<div>نسخه: ۱.۰</div>
|
||
<div>تاریخ تهیه: آبان ۱۴۰۵</div>
|
||
<div>طبقهبندی: فنی</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="document-title">
|
||
<span class="label">TECHNICAL INTEGRATION GUIDE</span>
|
||
<h1>انتقال هویت کاربر از xAiApi به MCP Server</h1>
|
||
<h2>راهنمای گام به گام تزریق توکن و اعمال احراز هویت در فراخوانی MCP</h2>
|
||
</div>
|
||
</header>
|
||
|
||
<div class="document-body">
|
||
|
||
<div class="intro-text">
|
||
<strong>هدف این مستند:</strong> در حال حاضر، سرویس <span class="file-path">xAiApi</span> هنگام اتصال به MCP Server، هیچ اطلاعاتی از کاربر جاری را ارسال نمیکند. اتصالها فقط با هدرهای ایستای ذخیرهشده در دیتابیس ساخته میشوند و بین تمام کاربران مشترک هستند. این مستند، گامهای دقیق لازم برای تزریق توکن کاربر جاری، تفکیک اتصالها به ازای هر کاربر و مدیریت صحیح چرخه حیات آنها را شرح میدهد تا احراز هویت در سمت MCP Server بهدرستی انجام شود.
|
||
</div>
|
||
|
||
<!-- ===================== SECTION 1 ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۱</div>
|
||
<div class="section-title">
|
||
<h3>خلاصه اجرایی و تصویر کلی مشکل</h3>
|
||
<div class="subtitle">Executive Summary</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>در پیادهسازی فعلی <span class="file-path">XAiMcpServerProvider</span>، هنگام ساخت اتصال به MCP Server فقط از <span class="file-path">AdditionalHeaders</span> ایستا استفاده میشود که از فیلد <span class="file-path">Headers</span> موجود در جدول <span class="file-path">AiMcpServers</span> خوانده میشود. همچنین اتصالها در دیکشنری <span class="file-path">clients</span> فقط با کلید <span class="file-path">serverId</span> کش میشوند.</p>
|
||
|
||
<div class="warning-box">
|
||
<strong>⚠ دو مشکل بنیادین:</strong>
|
||
<ul style="padding-right: 20px; margin-top: 10px;">
|
||
<li>هیچکدام از هدرها شامل توکن کاربر جاری نیستند؛ بنابراین MCP Server هویت هیچ کاربری را نمیبیند.</li>
|
||
<li>اتصالها به ازای <span class="file-path">serverId</span> کش میشوند، نه <span class="file-path">(serverId, userId)</span>. اگر اتصال کاربر A کش شود، کاربر B از همان اتصال استفاده میکند و MCP Server هویت اشتباهی میبیند.</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<p class="no-indent"><strong>راهحل پیشنهادی:</strong> تغییر کلید کش به <span class="file-path">(ServerId, UserId)</span> و تزریق هدر <span class="file-path">Authorization: Bearer {AccessToken}</span> در زمان ساخت اتصال، بههمراه بهروزرسانی متدهای مدیریت چرخه حیات اتصالها.</p>
|
||
|
||
<div class="stats-grid">
|
||
<div class="stat-card">
|
||
<div class="stat-value">Per-User<br>Connection</div>
|
||
<div class="stat-label">هر کاربر اتصال مستقل با توکن خودش</div>
|
||
</div>
|
||
<div class="stat-card">
|
||
<div class="stat-value">Token<br>Injection</div>
|
||
<div class="stat-label">تزریق خودکار Bearer Token در هر اتصال</div>
|
||
</div>
|
||
<div class="stat-card">
|
||
<div class="stat-value">Lifecycle<br>Management</div>
|
||
<div class="stat-label">مدیریت هوشمند انقضا و بازسازی اتصالها</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="highlight-box">
|
||
<strong>نتیجه کلیدی:</strong> پس از پیادهسازی این راهنما، هر Tool در MCP Server میتواند هویت کاربر صحیح را از توکن Bearer دریافتی استخراج کند و بر اساس آن مجوز دسترسی یا رد درخواست را صادر نماید.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===================== SECTION 2 ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۲</div>
|
||
<div class="section-title">
|
||
<h3>گام ۱ — تغییر ساختار کشها به «به ازای هر کاربر»</h3>
|
||
<div class="subtitle">Step 1: Per-User Cache Structure</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>اولین گام، تغییر دیکشنریهای نگهدارنده اتصالها است. به جای کلید <span class="file-path">Guid</span>، از کلید ترکیبی <span class="file-path">(Guid ServerId, string UserId)</span> استفاده میکنیم تا هر کاربر اتصال اختصاصی خودش را با توکن خودش داشته باشد.</p>
|
||
|
||
<p class="no-indent"><strong>فایل هدف:</strong> <span class="file-path">xAiApi\Providers\XAiMcpServerProvider.cs</span></p>
|
||
|
||
<ul class="step-list">
|
||
<li>تغییر نوع دیکشنری <span class="file-path">clients</span> از <span class="file-path">Dictionary<Guid, McpClient></span> به <span class="file-path">ConcurrentDictionary<(Guid ServerId, string UserId), McpClient></span>.</li>
|
||
<li>تغییر نوع دیکشنری <span class="file-path">tools</span> به همان ساختار کلید ترکیبی.</li>
|
||
<li>نگهداشتن <span class="file-path">mcpServers</span> با کلید <span class="file-path">Guid</span> (چون متادیتای سرور بین همه کاربران مشترک است).</li>
|
||
<li>استفاده از <span class="file-path">ConcurrentDictionary</span> به جای <span class="file-path">Dictionary</span> برای جلوگیری از race condition در درخواستهای همزمان.</li>
|
||
</ul>
|
||
|
||
<div class="code-block"><code><span class="k">using</span> System.Collections.Concurrent;
|
||
|
||
<span class="c">// در کلاس XAiMcpServerProvider:</span>
|
||
<span class="c">// پیش از این:</span>
|
||
<span class="c">// private readonly Dictionary<Guid, McpClient> clients = [];</span>
|
||
<span class="c">// private readonly Dictionary<Guid, IList<McpClientTool>> tools = [];</span>
|
||
|
||
<span class="c">// پس از تغییر:</span>
|
||
<span class="k">private readonly</span> ConcurrentDictionary<(<span class="k">Guid</span> ServerId, <span class="k">string</span> UserId), McpClient> clients = <span class="k">new</span>();
|
||
<span class="k">private readonly</span> ConcurrentDictionary<(<span class="k">Guid</span> ServerId, <span class="k">string</span> UserId), IList<McpClientTool>> tools = <span class="k">new</span>();
|
||
|
||
<span class="c">// mcpServers بدون تغییر باقی میماند (متادیتای مشترک):</span>
|
||
<span class="k">private readonly</span> ConcurrentDictionary<<span class="k">Guid</span>, XAiMcpServerDto> mcpServers = <span class="k">new</span>();</code></div>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 چرا ConcurrentDictionary؟</strong> چون <span class="file-path">GetAvailableToolsAsync</span> روی هر درخواست همزمان از کاربران مختلف فراخوانی میشود. در <span class="file-path">Dictionary</span> معمولی، همزمانی خواندن/نوشتن میتواند باعث فساد داده یا Exception شود. <span class="file-path">ConcurrentDictionary</span> عملیات را بهصورت thread-safe مدیریت میکند.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===================== SECTION 3 ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۳</div>
|
||
<div class="section-title">
|
||
<h3>گام ۲ — تزریق توکن در CreateClientAsync</h3>
|
||
<div class="subtitle">Step 2: Inject Token into Client Creation</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>هسته اصلی راهحل، تزریق هدر <span class="file-path">Authorization: Bearer {AccessToken}</span> در زمان ساخت اتصال است. این متد باید پارامتر <span class="file-path">userInfo</span> را دریافت کند و توکن را به هدرهای ایستای سرور اضافه کند.</p>
|
||
|
||
<ul class="step-list">
|
||
<li>تغییر امضای متد <span class="file-path">CreateClientAsync</span> برای دریافت پارامتر <span class="file-path">XUserClaimsInfoDto userInfo</span>.</li>
|
||
<li>خواندن هدرهای ایستای سرور از فیلد <span class="file-path">Headers</span> (همانند قبل).</li>
|
||
<li>اضافه کردن هدر <span class="file-path">Authorization</span> با استفاده از ثابتهای <span class="file-path">XAuthorization.Header</span> و <span class="file-path">XAuthorization.TokenIdentifier</span>.</li>
|
||
<li>پاس دادن دیکشنری هدرها به <span class="file-path">HttpClientTransportOptions.AdditionalHeaders</span>.</li>
|
||
</ul>
|
||
|
||
<div class="code-block"><code><span class="k">private static async</span> Task<McpClient> CreateClientAsync(
|
||
XAiMcpServerDto s,
|
||
XUserClaimsInfoDto userInfo, <span class="c">// ← پارامتر جدید</span>
|
||
CancellationToken cancellationToken
|
||
)
|
||
{
|
||
<span class="c">// ۱. هدرهای ایستای سرور (مثلاً کلید API یا هدرهای ثابت سازمانی)</span>
|
||
<span class="k">var</span> headers = ParseJsonDict(s.Headers) ?? <span class="k">new</span> Dictionary<<span class="k">string</span>, <span class="k">string</span>>();
|
||
|
||
<span class="c">// ۲. تزریق توکن کاربر جاری</span>
|
||
<span class="k">if</span> (userInfo <span class="k">is not null</span> && !userInfo.AccessToken.IsNullOrEmpty())
|
||
{
|
||
headers[XAuthorization.Header] =
|
||
$<span class="s">"{XAuthorization.TokenIdentifier} {userInfo.AccessToken}"</span>;
|
||
}
|
||
|
||
<span class="c">// ۳. ساخت Transport با هدرهای ترکیبی</span>
|
||
IClientTransport transport = <span class="k">new</span> HttpClientTransport(<span class="k">new</span> HttpClientTransportOptions
|
||
{
|
||
Endpoint = <span class="k">new</span> Uri(s.Endpoint!),
|
||
AdditionalHeaders = headers,
|
||
TransportMode = HttpTransportMode.AutoDetect
|
||
});
|
||
|
||
<span class="k">return await</span> McpClient.CreateAsync(transport, cancellationToken: cancellationToken);
|
||
}</code></div>
|
||
|
||
<div class="success-box">
|
||
<strong>✓ نکته کلیدی:</strong> استفاده از ثابتهای <span class="file-path">XAuthorization.Header</span> و <span class="file-path">XAuthorization.TokenIdentifier</span> تضمین میکند که هدر ارسالی با استانداردی که در پروژه شما (و سایر کلاینتها) تعریف شده هماهنگ باشد و MCP Server بتواند آن را از طریق Middleware استاندارد <span class="file-path">UseAuthentication</span> بخواند.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===================== SECTION 4 ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۴</div>
|
||
<div class="section-title">
|
||
<h3>گام ۳ — بهروزرسانی ConnectAndValidateAsync</h3>
|
||
<div class="subtitle">Step 3: Connect and Validate per User</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>این متد مسئول ایجاد اتصال، تست سلامت (Ping) و دریافت لیست Toolها است. باید پارامتر <span class="file-path">userInfo</span> را بگیرد و از کلید ترکیبی برای ذخیره در کش استفاده کند.</p>
|
||
|
||
<div class="code-block"><code><span class="k">private async</span> Task ConnectAndValidateAsync(
|
||
XAiMcpServerDto descriptor,
|
||
XUserClaimsInfoDto userInfo, <span class="c">// ← پارامتر جدید</span>
|
||
CancellationToken cancellationToken
|
||
)
|
||
{
|
||
<span class="k">try</span>
|
||
{
|
||
<span class="k">using var</span> cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||
cts.CancelAfter(TimeSpan.FromSeconds(descriptor.TimeoutSeconds));
|
||
|
||
<span class="c">// ۱. ساخت اتصال با توکن این کاربر</span>
|
||
<span class="k">var</span> client = <span class="k">await</span> CreateClientAsync(descriptor, userInfo, cts.Token);
|
||
|
||
<span class="c">// ۲. تست سلامت</span>
|
||
<span class="k">await</span> client.PingAsync(cancellationToken: cts.Token);
|
||
|
||
<span class="c">// ۳. دریافت لیست Toolها</span>
|
||
<span class="k">var</span> serverTools = <span class="k">await</span> client.ListToolsAsync(cancellationToken: cts.Token);
|
||
|
||
<span class="c">// ۴. ذخیره در کش با کلید ترکیبی</span>
|
||
<span class="k">var</span> key = (descriptor.Id, userInfo.UserId);
|
||
clients[key] = client;
|
||
tools[key] = serverTools;
|
||
mcpServers[descriptor.Id] = descriptor;
|
||
|
||
logger.LogInformation(
|
||
<span class="s">"MCP '{Name}': {Count} Tools Detected for user {UserId} ..."</span>,
|
||
descriptor.Name,
|
||
serverTools.Count,
|
||
userInfo.UserId
|
||
);
|
||
}
|
||
<span class="k">catch</span> (Exception ex)
|
||
{
|
||
logger.LogWarning(ex,
|
||
<span class="s">"MCP '{Name}': Connection Failed for user {UserId}, Ignored ..."</span>,
|
||
descriptor.Name,
|
||
userInfo.UserId
|
||
);
|
||
}
|
||
}</code></div>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 چرا خطا را نادیده میگیریم؟</strong> چون اگر یک MCP Server در دسترس نباشد، نباید کل فرآیند پرسش کاربر متوقف شود. لاگ هشدار ثبت میشود و Toolهای بقیه سرورها همچنان قابل استفاده هستند. این همان رفتار graceful degradation است که در نسخه قبلی نیز رعایت شده بود.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===================== SECTION 5 ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۵</div>
|
||
<div class="section-title">
|
||
<h3>گام ۴ — بازنویسی GetAvailableToolsAsync</h3>
|
||
<div class="subtitle">Step 4: Rewrite GetAvailableToolsAsync</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>این متد باید دو کار اصلی انجام دهد: (۱) متادیتای سرورها را یک بار بارگذاری کند، (۲) اتصال مخصوص کاربر جاری را در صورت نبود ایجاد کند و Toolهای آن کاربر را بازگرداند.</p>
|
||
|
||
<ul class="step-list">
|
||
<li>استخراج متادیتای سرورها (فقط یک بار) در متد کمکی <span class="file-path">EnsureServersLoadedAsync</span>.</li>
|
||
<li>فیلتر کردن سرورهای قابل دسترس این کاربر (بر اساس مالکیت یا Scope = All).</li>
|
||
<li>بررسی وجود اتصال در کش با کلید <span class="file-path">(ServerId, UserId)</span> و ایجاد اتصال در صورت نبود.</li>
|
||
<li>بازگرداندن فقط Toolهای مربوط به همین کاربر از دیکشنری <span class="file-path">tools</span>.</li>
|
||
</ul>
|
||
|
||
<div class="code-block"><code><span class="k">public async</span> Task<IList<McpClientTool>> GetAvailableToolsAsync(
|
||
<span class="k">bool</span> forceOwner = <span class="k">false</span>,
|
||
XUserClaimsInfoDto userInfo = <span class="k">null</span>,
|
||
CancellationToken cancellationToken = <span class="k">default</span>
|
||
)
|
||
{
|
||
<span class="c">// ۱. اعتبارسنجی کاربر</span>
|
||
<span class="k">if</span> (userInfo.IsNullOrDefault() || userInfo.UserId.IsNullOrEmpty())
|
||
{
|
||
logger.LogInformation(<span class="s">"there isn't any McpServer for non authenticated user ..."</span>);
|
||
<span class="k">return</span> [];
|
||
}
|
||
|
||
<span class="k">if</span> (!userInfo.IsAdmin()) forceOwner = <span class="k">true</span>;
|
||
|
||
<span class="c">// ۲. بارگذاری متادیتای سرورها (فقط یک بار)</span>
|
||
<span class="k">await</span> EnsureServersLoadedAsync(cancellationToken);
|
||
|
||
<span class="c">// ۳. فیلتر سرورهای قابل دسترس این کاربر</span>
|
||
<span class="k">var</span> accessibleServers = mcpServers.Values
|
||
.Where(s => s.OwnerId == userInfo.UserId ||
|
||
s.Scope == XAiMcpServerUsageScope.All)
|
||
.ToList();
|
||
|
||
<span class="c">// ۴. ایجاد اتصال اختصاصی این کاربر برای سرورهای متصلنشده</span>
|
||
<span class="k">foreach</span> (<span class="k">var</span> server <span class="k">in</span> accessibleServers)
|
||
{
|
||
<span class="k">var</span> key = (server.Id, userInfo.UserId);
|
||
<span class="k">if</span> (clients.ContainsKey(key)) <span class="k">continue</span>;
|
||
|
||
<span class="k">await</span> initLock.WaitAsync(cancellationToken);
|
||
<span class="k">try</span>
|
||
{
|
||
<span class="c">// double-check pattern پس از گرفتن قفل</span>
|
||
<span class="k">if</span> (!clients.ContainsKey(key))
|
||
{
|
||
<span class="k">await</span> ConnectAndValidateAsync(server, userInfo, cancellationToken);
|
||
}
|
||
}
|
||
<span class="k">finally</span>
|
||
{
|
||
initLock.Release();
|
||
}
|
||
}
|
||
|
||
<span class="c">// ۵. بازگرداندن فقط Toolهای مربوط به همین کاربر</span>
|
||
<span class="k">var</span> result = tools
|
||
.Where(kv => kv.Key.UserId == userInfo.UserId)
|
||
.SelectMany(kv => kv.Value)
|
||
.ToList();
|
||
|
||
<span class="k">return</span> result;
|
||
}
|
||
|
||
<span class="k">private async</span> Task EnsureServersLoadedAsync(CancellationToken cancellationToken)
|
||
{
|
||
<span class="k">if</span> (isInitialized) <span class="k">return</span>;
|
||
|
||
<span class="k">await</span> initLock.WaitAsync(cancellationToken);
|
||
<span class="k">try</span>
|
||
{
|
||
<span class="k">if</span> (isInitialized) <span class="k">return</span>;
|
||
|
||
<span class="k">var</span> servers = <span class="k">await</span> mcpServerServiceProvider.FindManyAsync(
|
||
orderBuilder: <span class="k">null</span>,
|
||
includeBuilder: <span class="k">null</span>,
|
||
ignoreSoftDeleteds: <span class="k">true</span>,
|
||
predicate: x => x.Enabled,
|
||
cancellationToken: cancellationToken
|
||
);
|
||
|
||
<span class="k">if</span> (servers.HasChild())
|
||
{
|
||
<span class="k">foreach</span> (<span class="k">var</span> s <span class="k">in</span> servers)
|
||
{
|
||
<span class="c">// فقط متادیتا ذخیره میشود؛ اتصال per-user در حلقه قبلی ساخته میشود</span>
|
||
mcpServers[s.Id] = s;
|
||
}
|
||
}
|
||
|
||
isInitialized = <span class="k">true</span>;
|
||
}
|
||
<span class="k">finally</span>
|
||
{
|
||
initLock.Release();
|
||
}
|
||
}</code></div>
|
||
|
||
<div class="warning-box">
|
||
<strong>⚠ نکته مهم درباره isInitialized:</strong> پس از این تغییر، <span class="file-path">isInitialized</span> فقط به معنای «متادیتای سرورها بارگذاری شده» است، نه «همه اتصالها برقرارند». اتصالها بهصورت on-demand (در اولین دسترسی هر کاربر) ساخته میشوند.
|
||
</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">Step 5: Update Management Methods</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>متدهای <span class="file-path">AddMcpServer</span>، <span class="file-path">UpdateMcpServer</span>، <span class="file-path">DisableMcpServer</span>، <span class="file-path">EnableMcpServer</span>، <span class="file-path">ToggleMcpServer</span>، <span class="file-path">RemoveMcpServer</span> و <span class="file-path">RefreshAsync</span> باید به شکل مناسبی با ساختار جدید هماهنگ شوند.</p>
|
||
|
||
<h4 style="margin-top: 20px; color: var(--primary); font-size: 15px;">۵.۱ — AddMcpServer و UpdateMcpServer (اتصال کاربر جاری)</h4>
|
||
<p class="no-indent">این متدها فقط اتصال کاربری که عملیات را انجام میدهد میسازند. سایر کاربران هنگام دسترسی بعدی، اتصال خودشان را میسازند.</p>
|
||
|
||
<div class="code-block"><code><span class="c">// در AddMcpServer — بعد از ذخیره موفق:</span>
|
||
<span class="k">await</span> initLock.WaitAsync(cancellationToken);
|
||
<span class="k">try</span>
|
||
{
|
||
mcpServers[result.Id] = result;
|
||
<span class="k">await</span> ConnectAndValidateAsync(result, userInfo, cancellationToken);
|
||
}
|
||
<span class="k">finally</span> { initLock.Release(); }
|
||
|
||
<span class="c">// در UpdateMcpServer — ابتدا اتصالهای قدیمی را پاک کن:</span>
|
||
<span class="k">await</span> initLock.WaitAsync(cancellationToken);
|
||
<span class="k">try</span>
|
||
{
|
||
mcpServers[result.Id] = result;
|
||
|
||
<span class="c">// حذف همه اتصالهای قبلی برای این سرور (هر کاربری که بود)</span>
|
||
<span class="k">var</span> keysToRemove = clients.Keys
|
||
.Where(k => k.ServerId == result.Id)
|
||
.ToList();
|
||
<span class="k">foreach</span> (<span class="k">var</span> key <span class="k">in</span> keysToRemove)
|
||
{
|
||
<span class="k">if</span> (clients.TryRemove(key, <span class="k">out var</span> old))
|
||
{
|
||
<span class="k">try</span> { <span class="k">await</span> old.DisposeAsync(); } <span class="k">catch</span> { }
|
||
}
|
||
tools.TryRemove(key, <span class="k">out</span> _);
|
||
}
|
||
|
||
<span class="c">// اتصال مجدد برای کاربر جاری</span>
|
||
<span class="k">await</span> ConnectAndValidateAsync(result, userInfo, cancellationToken);
|
||
}
|
||
<span class="k">finally</span> { initLock.Release(); }</code></div>
|
||
|
||
<h4 style="margin-top: 20px; color: var(--primary); font-size: 15px;">۵.۲ — DisableMcpServer و RemoveMcpServer (قطع همه کاربران)</h4>
|
||
<p class="no-indent">این متدها باید اتصالهای همه کاربران را برای آن سرور قطع کنند.</p>
|
||
|
||
<div class="code-block"><code><span class="k">await</span> initLock.WaitAsync(cancellationToken);
|
||
<span class="k">try</span>
|
||
{
|
||
mcpServers.TryRemove(result.Id, <span class="k">out</span> _);
|
||
|
||
<span class="c">// قطع همه اتصالهای کاربران مختلف به این سرور</span>
|
||
<span class="k">var</span> keysToRemove = clients.Keys
|
||
.Where(k => k.ServerId == result.Id)
|
||
.ToList();
|
||
<span class="k">foreach</span> (<span class="k">var</span> key <span class="k">in</span> keysToRemove)
|
||
{
|
||
<span class="k">if</span> (clients.TryRemove(key, <span class="k">out var</span> c))
|
||
{
|
||
<span class="k">try</span> { <span class="k">await</span> c.DisposeAsync(); } <span class="k">catch</span> { }
|
||
}
|
||
tools.TryRemove(key, <span class="k">out</span> _);
|
||
}
|
||
}
|
||
<span class="k">finally</span> { initLock.Release(); }</code></div>
|
||
|
||
<h4 style="margin-top: 20px; color: var(--primary); font-size: 15px;">۵.۳ — RefreshAsync و DisposeAsync</h4>
|
||
<p class="no-indent">بدون تغییر منطقی، فقط مطمئن شوید همه اتصالها dispose و پاک میشوند. این متدها از قبل روی <span class="file-path">clients.Values</span> میچرخند و با ساختار جدید نیز بهدرستی کار میکنند.</p>
|
||
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>متد</th>
|
||
<th>رفتار جدید</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>AddMcpServer</td>
|
||
<td>فقط اتصال کاربر جاری ساخته میشود</td>
|
||
</tr>
|
||
<tr>
|
||
<td>UpdateMcpServer</td>
|
||
<td>اتصالهای همه کاربران قطع، اتصال کاربر جاری بازسازی</td>
|
||
</tr>
|
||
<tr>
|
||
<td>DisableMcpServer</td>
|
||
<td>اتصالهای همه کاربران قطع، متادیتا حذف</td>
|
||
</tr>
|
||
<tr>
|
||
<td>EnableMcpServer</td>
|
||
<td>فقط اتصال کاربر جاری ساخته میشود</td>
|
||
</tr>
|
||
<tr>
|
||
<td>ToggleMcpServer</td>
|
||
<td>مشابه Enable/Disable بر اساس وضعیت جدید</td>
|
||
</tr>
|
||
<tr>
|
||
<td>RemoveMcpServer</td>
|
||
<td>اتصالهای همه کاربران قطع، متادیتا حذف</td>
|
||
</tr>
|
||
<tr class="highlight-row">
|
||
<td>RefreshAsync</td>
|
||
<td>همه اتصالها پاک، سپس اتصال کاربر جاری بازسازی</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">Step 6: Token Expiration Management</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>چون اتصالها کش میشوند، اگر توکن کاربر منقضی شود، اتصال کششده با توکن قدیمی به خطای 401 برخورد میکند. دو راهحل ساده وجود دارد:</p>
|
||
|
||
<div class="comparison-grid">
|
||
<div class="comparison-card recommended">
|
||
<span class="card-badge">پیشنهاد ما ✓</span>
|
||
<h4>راهحل الف — TTL برای اتصالها</h4>
|
||
<ul>
|
||
<li><span>پیچیدگی</span> <span class="value">کم</span></li>
|
||
<li><span>پیشبینیپذیری</span> <span class="value">بالا</span></li>
|
||
<li><span>نیاز به تغییر</span> <span class="value">حداقلی</span></li>
|
||
</ul>
|
||
<p style="font-size:12px; margin-top:10px; color:var(--text-muted);">زمان انقضا برای هر اتصال نگه دارید و اگر منقضی شد، اتصال را بازسازی کنید.</p>
|
||
</div>
|
||
|
||
<div class="comparison-card">
|
||
<h4>راهحل ب — بازسازی در صورت 401</h4>
|
||
<ul>
|
||
<li><span>پیچیدگی</span> <span class="value">بالا</span></li>
|
||
<li><span>پیشبینیپذیری</span> <span class="value">متوسط</span></li>
|
||
<li><span>نیاز به تغییر</span> <span class="value">بیشتر</span></li>
|
||
</ul>
|
||
<p style="font-size:12px; margin-top:10px; color:var(--text-muted);">نیاز به Decorator حول AITool دارد و پیچیدگی مدیریت خطا را بالا میبرد.</p>
|
||
</div>
|
||
</div>
|
||
|
||
<p class="no-indent"><strong>پیادهسازی راهحل الف:</strong></p>
|
||
|
||
<div class="code-block"><code><span class="c">// ۱. افزودن دیکشنری برای نگهداری زمان انقضا</span>
|
||
<span class="k">private readonly</span> ConcurrentDictionary<(<span class="k">Guid</span>, <span class="k">string</span>), DateTime> clientExpirations = <span class="k">new</span>();
|
||
|
||
<span class="c">// ۲. در ConnectAndValidateAsync بعد از موفقیت:</span>
|
||
clientExpirations[key] = DateTime.UtcNow.AddMinutes(<span class="s">15</span>);
|
||
|
||
<span class="c">// ۳. در GetAvailableToolsAsync قبل از بررسی ContainsKey:</span>
|
||
<span class="k">if</span> (clients.ContainsKey(key) &&
|
||
clientExpirations.TryGetValue(key, <span class="k">out var</span> exp) &&
|
||
exp < DateTime.UtcNow)
|
||
{
|
||
<span class="c">// اتصال منقضی شده — پاک کن تا مجدداً ساخته شود</span>
|
||
<span class="k">if</span> (clients.TryRemove(key, <span class="k">out var</span> old))
|
||
{
|
||
<span class="k">try</span> { <span class="k">await</span> old.DisposeAsync(); } <span class="k">catch</span> { }
|
||
}
|
||
tools.TryRemove(key, <span class="k">out</span> _);
|
||
clientExpirations.TryRemove(key, <span class="k">out</span> _);
|
||
}</code></div>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 انتخاب TTL مناسب:</strong> مدت TTL را کمی کمتر از عمر توکن (مثلاً اگر توکن ۳۰ دقیقه اعتبار دارد، TTL را ۱۵ دقیقه بگذارید) انتخاب کنید تا از ارسال درخواست با توکن منقضی جلوگیری شود. این مقدار را میتوانید از یک تنظیمات قابل پیکربندی بخوانید.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===================== SECTION 8 ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۸</div>
|
||
<div class="section-title">
|
||
<h3>گام ۷ — بررسی سمت xAiMcpServer</h3>
|
||
<div class="subtitle">Step 7: Server-Side Verification</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>برای اینکه توکن ارسالی از <span class="file-path">xAiApi</span> در سمت سرور بهدرستی پردازش شود، سمت <span class="file-path">xAiMcpServer</span> باید این موارد را داشته باشد (طبق مستند قبلی یکپارچهسازی احراز هویت در Tools):</p>
|
||
|
||
<ul class="checklist">
|
||
<li>روی <span class="file-path">XAiMcpControllerBase</span>، <span class="file-path">[AllowAnonymous]</span> حذف و <span class="file-path">[Authorize(Policy = XPolicies.EnabledUser)]</span> اضافه شده باشد.</li>
|
||
<li>در <span class="file-path">Startup.cs</span>، <span class="file-path">app.UseAuthentication()</span> و <span class="file-path">app.UseAuthorization()</span> بهترتیب صحیح پس از <span class="file-path">UseRouting</span> قرار گرفته باشند.</li>
|
||
<li><span class="file-path">XAiMcpHandler.ProcessAsync</span> پارامتر <span class="file-path">userInfo</span> را دریافت و <span class="file-path">userContext.Set()</span> را فراخوانی کند.</li>
|
||
<li><span class="file-path">XAiMcpUserContext</span> با <span class="file-path">AsyncLocal</span> پیادهسازی شده باشد تا Toolهای static هم به کاربر دسترسی داشته باشند.</li>
|
||
<li>در <span class="file-path">HandleToolCallAsync</span>، بررسی <span class="file-path">RequiresAuthentication</span> و <span class="file-path">RequiredPolicy</span> انجام شود.</li>
|
||
</ul>
|
||
|
||
<div class="success-box">
|
||
<strong>✓ اگر این موارد فعال باشند:</strong> توکن Bearer که از <span class="file-path">xAiApi</span> ارسال میشود، توسط <span class="file-path">UseAuthentication</span> در MCP Server اعتبارسنجی میشود، <span class="file-path">ClaimsPrincipal</span> ساخته میشود و در کنترلر با <span class="file-path">GetUserInfo()</span> به <span class="file-path">XUserClaimsInfoDto</span> تبدیل میشود.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===================== SECTION 9 ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۹</div>
|
||
<div class="section-title">
|
||
<h3>جریان کامل یک درخواست پس از تغییرات</h3>
|
||
<div class="subtitle">Full End-to-End Request Flow</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>جدول زیر جریان کامل یک درخواست از لحظه ورود تا اجرای Tool در MCP Server را نشان میدهد:</p>
|
||
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>مرحله</th>
|
||
<th>لایه</th>
|
||
<th>عملیات</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>۱</td>
|
||
<td>کاربر</td>
|
||
<td>ارسال درخواست <span class="file-path">POST /api/v1/xai/ask</span> با هدر <span class="file-path">Authorization: Bearer {userToken}</span></td>
|
||
</tr>
|
||
<tr>
|
||
<td>۲</td>
|
||
<td>xAiApi</td>
|
||
<td>احراز هویت کاربر، ساخت <span class="file-path">XUserClaimsInfoDto</span></td>
|
||
</tr>
|
||
<tr>
|
||
<td>۳</td>
|
||
<td>XAIServiceBase.AskTextAsync</td>
|
||
<td>فراخوانی <span class="file-path">mcpServerProvider.GetAvailableToolsAsync(userInfo)</span></td>
|
||
</tr>
|
||
<tr>
|
||
<td>۴</td>
|
||
<td>XAiMcpServerProvider</td>
|
||
<td>فیلتر سرورهای دسترسیدار، بررسی وجود اتصال برای <span class="file-path">(ServerId, UserId)</span></td>
|
||
</tr>
|
||
<tr class="highlight-row">
|
||
<td>۵</td>
|
||
<td>XAiMcpServerProvider</td>
|
||
<td>اگر اتصال نبود، <span class="file-path">CreateClientAsync(server, userInfo)</span> با هدر <span class="file-path">Authorization: Bearer {userToken}</span></td>
|
||
</tr>
|
||
<tr>
|
||
<td>۶</td>
|
||
<td>MCP Server</td>
|
||
<td><span class="file-path">UseAuthentication</span> توکن را اعتبارسنجی میکند → <span class="file-path">[Authorize]</span> اجازه میدهد → <span class="file-path">GetUserInfo()</span> ساخته میشود → <span class="file-path">XAiMcpUserContext</span> پر میشود</td>
|
||
</tr>
|
||
<tr>
|
||
<td>۷</td>
|
||
<td>LLM</td>
|
||
<td>Tool را فرا میخواند (از طریق <span class="file-path">McpClientTool</span> متصل به اتصال کاربر)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>۸</td>
|
||
<td>MCP Server</td>
|
||
<td>Tool اجرا میشود و <span class="file-path">XAiMcpUserContext.CurrentUser</span> کاربر صحیح را برمیگرداند</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<div class="info-box">
|
||
<strong>🔒 نکته امنیتی:</strong> چون اتصالها به ازای هر کاربر جداگانه کش میشوند، حتی اگر دو کاربر همزمان به یک MCP Server درخواست بفرستند، هر کدام توکن خودش را در اتصال مخصوص خودش دارند و هیچگاه هویتها با هم تداخل پیدا نمیکنند.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===================== SECTION 10 ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۱۰</div>
|
||
<div class="section-title">
|
||
<h3>چکلیست پیادهسازی و تست</h3>
|
||
<div class="subtitle">Implementation & Test Checklist</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p class="no-indent"><strong>پس از اتمام پیادهسازی، موارد زیر را بررسی کنید:</strong></p>
|
||
|
||
<ul class="checklist">
|
||
<li>دیکشنریهای <span class="file-path">clients</span> و <span class="file-path">tools</span> به کلید <span class="file-path">(ServerId, UserId)</span> تغییر کردند.</li>
|
||
<li>از <span class="file-path">ConcurrentDictionary</span> برای هر دو دیکشنری استفاده میشود.</li>
|
||
<li><span class="file-path">CreateClientAsync</span> پارامتر <span class="file-path">userInfo</span> میگیرد و هدر <span class="file-path">Authorization: Bearer</span> را تزریق میکند.</li>
|
||
<li><span class="file-path">ConnectAndValidateAsync</span> پارامتر <span class="file-path">userInfo</span> میگیرد و کلید ترکیبی را در کش ذخیره میکند.</li>
|
||
<li><span class="file-path">GetAvailableToolsAsync</span> متادیتا را یک بار بارگذاری میکند و اتصال را per-user میسازد.</li>
|
||
<li><span class="file-path">AddMcpServer</span> و <span class="file-path">UpdateMcpServer</span> فقط برای کاربر جاری اتصال میسازند.</li>
|
||
<li><span class="file-path">DisableMcpServer</span> و <span class="file-path">RemoveMcpServer</span> اتصالهای همه کاربران را حذف میکنند.</li>
|
||
<li><span class="file-path">RefreshAsync</span> تمام اتصالها را dispose و پاک میکند.</li>
|
||
<li>TTL برای اتصالها (یا مکانیزم بازسازی) پیاده شده است.</li>
|
||
<li>سمت <span class="file-path">xAiMcpServer</span> دارای <span class="file-path">[Authorize]</span> و <span class="file-path">UseAuthentication</span> است.</li>
|
||
</ul>
|
||
|
||
<p class="no-indent" style="margin-top: 25px;"><strong>سناریوهای تست پیشنهادی:</strong></p>
|
||
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>سناریو</th>
|
||
<th>نتیجه مورد انتظار</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>کاربر A با توکن معتبر درخواست میفرستد</td>
|
||
<td>Toolها با هویت A اجرا میشوند</td>
|
||
</tr>
|
||
<tr>
|
||
<td>کاربر B همان سرور Global را فراخوانی میکند</td>
|
||
<td>Toolها با هویت B اجرا میشوند (نه A)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>درخواست بدون توکن به MCP Server</td>
|
||
<td>رد با 401 از سمت MCP Server</td>
|
||
</tr>
|
||
<tr>
|
||
<td>توکن منقضی پس از TTL</td>
|
||
<td>اتصال بازسازی و درخواست موفق</td>
|
||
</tr>
|
||
<tr>
|
||
<td>دو کاربر همزمان به یک سرور درخواست میفرستند</td>
|
||
<td>هر کدام توکن خودش را میبیند</td>
|
||
</tr>
|
||
<tr class="highlight-row">
|
||
<td>غیرفعالسازی سرور توسط ادمین</td>
|
||
<td>اتصال همه کاربران به آن سرور قطع میشود</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===================== CONCLUSION ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">✓</div>
|
||
<div class="section-title">
|
||
<h3>جمعبندی و گام بعدی</h3>
|
||
<div class="subtitle">Conclusion & Next Steps</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<div class="conclusion-box">
|
||
<h3>🎯 نتیجه نهایی</h3>
|
||
<p>با پیادهسازی هفت گام فوق، سیستم <span class="file-path">xAiApi</span> میتواند هویت کاربر جاری را بهدرستی به MCP Server منتقل کند و احراز هویت در سطح Tool انجام شود.</p>
|
||
<p>هر کاربر اکنون:</p>
|
||
<div style="display:flex; gap:12px; justify-content:center; margin-top:15px; flex-wrap:wrap;">
|
||
<span style="background:rgba(201,169,97,0.2); padding:10px 15px; border-radius:8px; font-size:13px;">✓ اتصال اختصاصی خودش را با توکن خودش دارد</span>
|
||
<span style="background:rgba(201,169,97,0.2); padding:10px 15px; border-radius:8px; font-size:13px;">✓ هویت او در MCP Server قابل استخراج است</span>
|
||
<span style="background:rgba(201,169,97,0.2); padding:10px 15px; border-radius:8px; font-size:13px;">✓ دسترسیها بر اساس نقش او فیلتر میشوند</span>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="highlight-box">
|
||
<strong>📋 گامهای بعدی پیشنهادی:</strong>
|
||
<ol style="padding-right: 20px; margin-top: 10px; line-height: 2;">
|
||
<li>پیادهسازی گامهای ۱ تا ۵ (اجباری).</li>
|
||
<li>افزودن TTL برای اتصالها (گام ۶ — توصیهشده).</li>
|
||
<li>اطمینان از فعال بودن احراز هویت سمت MCP Server (گام ۷).</li>
|
||
<li>نوشتن تستهای یکپارچگی برای سناریوهای جدول بالا.</li>
|
||
<li>پیکربندی مقدار TTL از appsettings برای انعطافپذیری بیشتر.</li>
|
||
<li>افزودن لاگهای <span class="file-path">Information</span> هنگام ساخت/حذف اتصال برای تحلیل امنیتی.</li>
|
||
</ol>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="page-info">
|
||
این مستند فنی محرمانه بوده و صرفاً جهت استفاده تیم توسعه <span class="file-path">xSaherelmWorkspace</span> تهیه شده است.
|
||
</div>
|
||
|
||
</div>
|
||
|
||
<footer class="document-footer">
|
||
<div class="footer-brand">طراحی و توسعه: هادی خزاعی اصل</div>
|
||
<div class="footer-divider"></div>
|
||
<div>تاریخ تهیه: آبان ۱۴۰۵ | نسخه: ۱.۰ | طبقهبندی: فنی</div>
|
||
</footer>
|
||
|
||
</div>
|
||
|
||
</body>
|
||
</html> |