266 lines
30 KiB
HTML
266 lines
30 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>اتصال XAiMcpServer به XAiApi | راهنمای گامبهگام</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: 980px;
|
||
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;
|
||
}
|
||
|
||
.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; }
|
||
|
||
.code-block {
|
||
direction: ltr;
|
||
text-align: left;
|
||
background: #1a202c;
|
||
color: #e2e8f0;
|
||
padding: 16px;
|
||
border-radius: 8px;
|
||
overflow-x: auto;
|
||
font-family: Consolas, Monaco, 'Courier New', monospace;
|
||
font-size: 12px;
|
||
line-height: 1.6;
|
||
margin: 15px 0;
|
||
white-space: pre;
|
||
border: 1px solid #2d3748;
|
||
}
|
||
|
||
.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; }
|
||
.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>شماره مستند: MCP-1405-001</div> <div>نسخه: ۱.۰</div> <div>تاریخ تهیه: آبان ۱۴۰۵</div> <div>طبقهبندی: فنی</div> </div> </div> <div class="document-title"> <span class="label">MCP INTEGRATION GUIDE</span> <h1>اتصال XAiMcpServer به XAiApi</h1> <h2>راهنمای گامبهگام اتصال سرور MCP ساده به API هوش مصنوعی</h2> </div> </header> <div class="document-body"> <div class="intro-text"> <strong>هدف این مستند:</strong><br> XAiMcpServer یک MCP Server ساده و مستقل است که قبلاً بهصورت مستقیم تست شده و ابزار نمونه <code>Echo</code> را ارائه میدهد. در این مستند، روشهای اتصال این سرور به XAiApi بهصورت گامبهگام، همراه با پیشنیازها، نکات بحرانی، روشهای ثبت، راستیآزمایی و عیبیابی شرح داده میشود. </div> <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>XAiApi یک کلاینت MCP داخلی با نام <code>XAiMcpServerProvider</code> دارد. این کلاینت سرورهای MCP فعال را از جدول <code>AiMcpServers</code> خوانده، با <code>HttpClientTransport</code> به Endpoint آنها متصل میشود، ابزارها را با <code>ListToolsAsync</code> دریافت میکند و در اختیار لایه هوش مصنوعی قرار میدهد.</p> <p>XAiMcpServer نیز یک MCP Server ساده ASP.NET Core است که endpoint اصلی آن <code>POST /mcp</code> و endpoint SSE آن <code>GET /mcp/sse</code> میباشد. ابزار <code>Echo</code> در کلاس <code>XEchoTools</code> قرار دارد و با attributeهای <code>XAiMcpToolType</code> و <code>XAiMcpTool</code> شناسایی میشود.</p> <div class="success-box"> <strong>نتیجه کلیدی:</strong> اتصال پایه از طریق ثبت مشخصات XAiMcpServer در XAiApi و سپس فعال بودن آن در جدول <code>AiMcpServers</code> انجام میشود. اما برای اینکه مدل زبانی واقعاً از ابزارها استفاده کند، باید <code>IXAiMcpServerProvider</code> را در سرویس هوش مصنوعی تزریق کرده و ابزارهای دریافتی را به <code>ChatOptions.Tools</code> اختصاص دهید. </div> </div> </div> <div class="section"> <div class="section-header"> <div class="section-number">۲</div> <div class="section-title"> <h3>وضعیت فعلی XAiMcpServer</h3> <div class="subtitle">Simple MCP Server — Directly Tested</div> </div> </div> <div class="section-content"> <p>XAiMcpServer یک سرور MCP ساده است که مستقیماً تست شده و حداقل یک ابزار نمونه دارد:</p> <table class="styled-table"> <thead> <tr> <th>مؤلفه</th> <th>توضیح</th> </tr> </thead> <tbody> <tr> <td>Controller</td> <td><code>McpController</code> با route <code>mcp</code></td> </tr> <tr> <td>Endpoint اصلی</td> <td><code>POST /mcp</code> برای JSON-RPC</td> </tr> <tr> <td>Endpoint SSE</td> <td><code>GET /mcp/sse</code></td> </tr> <tr> <td>Handler</td> <td><code>XAiMcpHandler</code> با پشتیبانی از <code>initialize</code>، <code>tools/list</code>، <code>tools/call</code> و <code>ping</code></td> </tr> <tr> <td>Tool Scanner</td> <td><code>XAiMcpToolScanner</code> اسمبلیهای <code>xAi*</code> را اسکن میکند</td> </tr> <tr class="highlight-row"> <td>ابزار نمونه</td> <td><code>XEchoTools.Echo</code> با نام <code>Echo</code></td> </tr> </tbody> </table> <p class="no-indent"><strong>تست مستقیم سرور با curl:</strong></p> <pre class="code-block">curl -k -X POST https://localhost:5001/mcp \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}'</pre> <pre class="code-block">curl -k -X POST https://localhost:5001/mcp \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}'</pre> <pre class="code-block">curl -k -X POST https://localhost:5001/mcp \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"Echo","arguments":{"message":"test"}}}'</pre> <div class="info-box"> <strong>نکته:</strong> اگر پورت یا scheme متفاوت است، آدرس را با پورت واقعی XAiMcpServer جایگزین کنید. برای محیط توسعه میتوان از HTTP استفاده کرد تا مشکل گواهی self-signed پیش نیاید. </div> </div> </div> <div class="section"> <div class="section-header"> <div class="section-number">۳</div> <div class="section-title"> <h3>معماری اتصال</h3> <div class="subtitle">How XAiApi Connects to XAiMcpServer</div> </div> </div> <div class="section-content"> <p>جریان اتصال در XAiApi به این صورت است:</p> <table class="styled-table"> <thead> <tr> <th>مرحله</th> <th>مؤلفه</th> <th>عملیات</th> </tr> </thead> <tbody> <tr> <td>۱</td> <td><code>XAiMcpServerProvider.GetAvailableToolsAsync</code></td> <td>سرورهای فعال را از <code>AiMcpServers</code> میخواند</td> </tr> <tr> <td>۲</td> <td><code>ConnectAndValidateAsync</code></td> <td>برای هر سرور کلاینت MCP میسازد، Ping و ListTools انجام میدهد</td> </tr> <tr> <td>۳</td> <td><code>CreateClientAsync</code></td> <td>با <code>HttpClientTransport</code> و <code>TransportMode.AutoDetect</code> به Endpoint وصل میشود</td> </tr> <tr> <td>۴</td> <td><code>XAiService / XAIServiceBase</code></td> <td>در صورت تزریق Provider، ابزارها را به <code>ChatOptions.Tools</code> میدهد</td> </tr> <tr class="highlight-row"> <td>۵</td> <td>مدل زبانی</td> <td>با <code>UseFunctionInvocation</code> ابزار را فراخوانی میکند</td> </tr> </tbody> </table> <div class="warning-box"> <strong>نکته بسیار مهم:</strong> در کد فعلی <code>XAIServiceBase</code> مقدار <code>Options = new ChatOptions()</code> است و هیچجا <code>IXAiMcpServerProvider.GetAvailableToolsAsync</code> فراخوانی نمیشود. بنابراین اتصال در سطح Provider برقرار میشود، اما تا زمانی که ابزارها به <code>Options.Tools</code> اضافه نشوند، مدل زبانی از آنها استفاده نمیکند. </div> </div> </div> <div class="section"> <div class="section-header"> <div class="section-number">۴</div> <div class="section-title"> <h3>پیشنیازها و نکات بحرانی</h3> <div class="subtitle">Prerequisites & Critical Notes</div> </div> </div> <div class="section-content"> <table class="styled-table"> <thead> <tr> <th>پیشنیاز</th> <th>توضیح</th> </tr> </thead> <tbody> <tr> <td>دسترسی شبکهای</td> <td>XAiApi باید بتواند به آدرس XAiMcpServer وصل شود (پورت، فایروال، DNS)</td> </tr> <tr> <td>گواهی SSL معتبر</td> <td>در صورت استفاده از HTTPS، گواهی باید معتبر باشد؛ Provider پیشفرض handler سفارشی برای نادیدهگرفتن خطای SSL ندارد</td> </tr> <tr> <td>جدول <code>AiMcpServers</code></td> <td>باید در دیتابیس XAiApi وجود داشته باشد. در migration اولیه ارائهشده این جدول دیده نمیشود؛ در صورت نیاز migration جدید اضافه کنید</td> </tr> <tr> <td>ثبت <code>IXAiMcpServerProvider</code></td> <td>در DI فعلی XAiApi ثبت نشده است؛ برای استفاده در سرویس AI باید ثبت شود</td> </tr> <tr> <td>توکن ادمین</td> <td>برای ثبت از طریق API مدیریتی، توکن با Policy <code>EnabledAdmin</code> لازم است</td> </tr> <tr> <td>Scope</td> <td>اگر کاربر ادمین نباشد، Provider بهصورت خودکار Scope را <code>Owner</code> میکند</td> </tr> </tbody> </table> <div class="warning-box"> <strong>هشدار migration:</strong> در فایل migration اولیه XAiApi جدول <code>AiMcpServers</code> ساخته نشده است. اگر دیتابیس شما بر اساس همان migration است، قبل از ثبت سرور باید migration جدید برای entity <code>XAiMcpServer</code> اضافه و اعمال شود. </div> </div> </div> <div class="section"> <div class="section-header"> <div class="section-number">۵</div> <div class="section-title"> <h3>روش ۱: ثبت از طریق API مدیریتی XAiApi</h3> <div class="subtitle">Recommended — Register via Admin API</div> </div> </div> <div class="section-content"> <p>این روش توصیهشده است porque از مسیر رسمی <code>McpServerController</code> استفاده میکند.</p> <p class="no-indent"><strong>گام ۱:</strong> XAiMcpServer را اجرا و مستقیماً تست کنید (بخش ۲). مطمئن شوید <code>tools/list</code> ابزار <code>Echo</code> را برمیگرداند.</p> <p class="no-indent"><strong>گام ۲:</strong> توکن ادمین XAiApi را دریافت کنید. این توکن باید Policy <code>EnabledAdmin</code> را داشته باشد.</p> <p class="no-indent"><strong>گام ۳:</strong> سرور را با درخواست <code>POST</code> ثبت کنید. مسیر دقیق را از Swagger بردارید؛ معمولاً مسیری مانند <code>/api/v1/McpServer</code> است.</p> <pre class="code-block">curl -k -X POST https://localhost:7001/api/v1/McpServer \ -H "Authorization: Bearer <ADMIN_TOKEN>" \ -H "Content-Type: application/json" \ -d '{ "ownerId": "<ADMIN_USER_ID>", "name": "xAiMcpServer", "description": "Simple Echo MCP Server - directly tested", "endpoint": "https://localhost:5001/mcp", "enabled": true, "scope": 2, "headers": "{}", "timeoutSeconds": 30, "createdOn": "2026-11-05T00:00:00Z" }'</pre> <table class="styled-table"> <thead> <tr> <th>فیلد</th> <th>مقدار پیشنهادی</th> <th>توضیح</th> </tr> </thead> <tbody> <tr> <td><code>name</code></td> <td><code>xAiMcpServer</code></td> <td>نام نمایشی سرور</td> </tr> <tr> <td><code>endpoint</code></td> <td><code>https://host:port/mcp</code></td> <td>برای Streamable HTTP. اگر اتصال برقرار نشد، <code>/mcp/sse</code> را امتحان کنید</td> </tr> <tr> <td><code>enabled</code></td> <td><code>true</code></td> <td>فقط سرورهای فعال توسط Provider خوانده میشوند</td> </tr> <tr> <td><code>scope</code></td> <td><code>2</code> یا <code>1</code></td> <td><code>Owner=2</code> برای استفاده مالک، <code>All=1</code> برای استفاده همه (ادمین)</td> </tr> <tr> <td><code>headers</code></td> <td><code>"{}"</code></td> <td>اگر سرور احراز هویت دارد، JSON شامل هدرها مثل <code>Authorization</code></td> </tr> <tr> <td><code>timeoutSeconds</code></td> <td><code>30</code> یا بیشتر</td> <td>حداقل مقدار مجاز در ثابتها ۳۰ ثانیه است</td> </tr> </tbody> </table> <p class="no-indent"><strong>گام ۴:</strong> XAiApi را ریاستارت کنید یا در صورت وجود، متد <code>RefreshAsync</code> را فراخوانی کنید. Provider نتایج را cache میکند و سرور جدید تا refresh یا restart شناسایی نمیشود.</p> <p class="no-indent"><strong>گام ۵:</strong> در لاگ XAiApi عبارت زیر را جستجو کنید:</p> <pre class="code-block">MCP 'xAiMcpServer': {Count} Tools Detected ...</pre> <div class="success-box"> <strong>نتیجه:</strong> اگر این لاگ را دیدید، اتصال HTTP برقرار شده و ابزارها دریافت شدهاند. ابزار <code>Echo</code> باید در لیست باشد. </div> </div> </div> <div class="section"> <div class="section-header"> <div class="section-number">۶</div> <div class="section-title"> <h3>روش ۲: ثبت مستقیم در دیتابیس یا Seeder</h3> <div class="subtitle">Direct DB Insert / EF Seeder</div> </div> </div> <div class="section-content"> <p>اگر نمیخواهید از API مدیریتی استفاده کنید، میتوانید یک رکورد در جدول <code>AiMcpServers</code> درج کنید. این روش برای محیط توسعه یا Seed اولیه مناسب است.</p> <table class="styled-table"> <thead> <tr> <th>ستون</th> <th>مقدار نمونه</th> </tr> </thead> <tbody> <tr><td><code>Id</code></td><td>یک GUID جدید</td></tr> <tr><td><code>Deleted</code></td><td><code>0</code></td></tr> <tr><td><code>OwnerId</code></td><td>شناسه کاربر مالک</td></tr> <tr><td><code>Scope</code></td><td><code>2</code> (Owner) یا <code>1</code> (All)</td></tr> <tr><td><code>Name</code></td><td><code>xAiMcpServer</code></td></tr> <tr><td><code>Description</code></td><td>Simple Echo MCP Server</td></tr> <tr><td><code>Enabled</code></td><td><code>1</code></td></tr> <tr><td><code>Endpoint</code></td><td><code>https://localhost:5001/mcp</code></td></tr> <tr><td><code>Headers</code></td><td><code>{}</code></td></tr> <tr><td><code>TimeoutSeconds</code></td><td><code>30</code></td></tr> <tr><td><code>CreatedOn</code></td><td>زمان UTC</td></tr> <tr><td><code>UpdatedAt</code></td><td>زمان UTC</td></tr> </tbody> </table> <p>سپس XAiApi را ریاستارت کنید تا Provider سرور را بارگذاری کند.</p> <div class="info-box"> <strong>پیشنهاد:</strong> برای محیط عملیاتی، Seed مستقیم را فقط برای داده اولیه استفاده کنید و مدیریت تغییرات را از طریق API مدیریتی انجام دهید. </div> </div> </div> <div class="section"> <div class="section-header"> <div class="section-number">۷</div> <div class="section-title"> <h3>روش ۳: ثبت برنامهنویسی با IXAiMcpServerProvider</h3> <div class="subtitle">Programmatic Registration</div> </div> </div> <div class="section-content"> <p>اگر میخواهید در Startup یا یک سرویس مدیریتی، سرور را ثبت کنید، از متد <code>AddMcpServer</code> در <code>IXAiMcpServerProvider</code> استفاده کنید.</p> <p class="no-indent"><strong>گام ۱:</strong> Provider را در DI ثبت کنید:</p> <pre class="code-block">services.AddScoped<IXAiMcpServerProvider, XAiMcpServerProvider>();</pre> <p class="no-indent"><strong>گام ۲:</strong> در یک سرویس دارای دسترسی ادمین، DTO زیر را بسازید و متد <code>AddMcpServer</code> را فراخوانی کنید:</p> <pre class="code-block">var item = new XAiMcpServerDto { Name = "xAiMcpServer", Description = "Simple Echo MCP Server", Endpoint = "https://localhost:5001/mcp", Enabled = true, Scope = XAiMcpServerUsageScope.Owner, Headers = "{}", TimeoutSeconds = 30 }; var result = await mcpProvider.AddMcpServer( item: item, userInfo: adminUserInfo, cancellationToken: cancellationToken );</pre> <div class="success-box"> <strong>مزیت این روش:</strong> اعتبارسنجی، ذخیره در دیتابیس، Enrichment و اتصال/دریافت ابزارها بهصورت یکجا انجام میشود. </div> </div> </div> <div class="section"> <div class="section-header"> <div class="section-number">۸</div> <div class="section-title"> <h3>اتصال ابزارها به مدل زبانی</h3> <div class="subtitle">Inject Tools into ChatOptions</div> </div> </div> <div class="section-content"> <p>این مرحله برای «استفاده واقعی مدل از ابزار» ضروری است. در کد فعلی <code>XAIServiceBase</code> گزینههای Chat خالی است و ابزارها به مدل داده نمیشوند.</p> <p class="no-indent"><strong>گام ۱:</strong> <code>IXAiMcpServerProvider</code> را به <code>XAiService</code> یا <code>XAIServiceBase</code> تزریق کنید.</p> <p class="no-indent"><strong>گام ۲:</strong> در متدهای <code>AskTextAsync</code>، <code>AskAsync</code>، <code>AskAsEnumerable</code> و مشابه آن، قبل از فراخوانی مدل، ابزارها را دریافت و به <code>Options.Tools</code> اختصاص دهید:</p> <pre class="code-block">var availableTools = await mcpProvider.GetAvailableToolsAsync( userInfo: userInfo, cancellationToken: cancellationToken ); Options.Tools = availableTools .Cast<AITool>() .ToList();</pre> <p class="no-indent"><strong>گام ۳:</strong> مطمئن شوید کلاینت مدل با <code>UseFunctionInvocation</code> ساخته میشود. در <code>XModelsExtensions.GetClient</code> این کار انجام شده است.</p> <div class="warning-box"> <strong>نکته:</strong> ابزارها بر اساس <code>userInfo</code> فیلتر میشوند. اگر <code>userInfo</code> نال باشد، <code>GetAvailableToolsAsync</code> لیست خالی برمیگرداند. بنابراین حتماً کاربر احراز هویتشده را پاس دهید. </div> <div class="info-box"> <strong>اگر خطای کامپایل گرفتید:</strong> نوع <code>McpClientTool</code> ممکن است مستقیماً <code>AITool</code> نباشد. در آن صورت از تبدیل مناسب SDK یا <code>Select</code> استفاده کنید. در نسخههای جدید ModelContextProtocol، ابزارها معمولاً بهصورت <code>AIFunction</code> قابل استفاده هستند. </div> </div> </div> <div class="section"> <div class="section-header"> <div class="section-number">۹</div> <div class="section-title"> <h3>راستیآزمایی و تست نهایی</h3> <div class="subtitle">Verification Checklist</div> </div> </div> <div class="section-content"> <table class="styled-table"> <thead> <tr> <th>#</th> <th>تست</th> <th>نتیجه مورد انتظار</th> </tr> </thead> <tbody> <tr> <td>۱</td> <td>POST /mcp با متد initialize</td> <td>پاسخ JSON-RPC شامل <code>protocolVersion</code> و <code>serverInfo</code></td> </tr> <tr> <td>۲</td> <td>POST /mcp با متد tools/list</td> <td>لیست ابزارها شامل <code>Echo</code></td> </tr> <tr> <td>۳</td> <td>POST /mcp با متد tools/call برای Echo</td> <td>پاسخ متنی مانند <code>hello test</code></td> </tr> <tr> <td>۴</td> <td>ثبت سرور در XAiApi</td> <td>رکورد در <code>AiMcpServers</code> با <code>Enabled=true</code></td> </tr> <tr> <td>۵</td> <td>ریاستارت XAiApi</td> <td>لاگ <code>MCP 'xAiMcpServer': N Tools Detected</code></td> </tr> <tr class="highlight-row"> <td>۶</td> <td>پرسش از مدل با درخواست استفاده از Echo</td> <td>مدل ابزار را فراخوانی و پاسخ مناسب تولید میکند</td> </tr> </tbody> </table> </div> </div> <div class="section"> <div class="section-header"> <div class="section-number">۱۰</div> <div class="section-title"> <h3>عیبیابی خطاهای رایج</h3> <div class="subtitle">Troubleshooting</div> </div> </div> <div class="section-content"> <table class="styled-table"> <thead> <tr> <th>علامت</th> <th>علت احتمالی</th> <th>راهحل</th> </tr> </thead> <tbody> <tr> <td>اتصال برقرار نمیشود</td> <td>آدرس اشتباه، پورت بسته یا فایروال</td> <td>از همان ماشین XAiApi با curl تست کنید؛ HTTP را امتحان کنید</td> </tr> <tr> <td>خطای SSL</td> <td>گواهی self-signed</td> <td>گواهی معتبر نصب کنید یا در توسعه از HTTP استفاده کنید</td> </tr> <tr> <td>404 روی /mcp</td> <td>مسیر یا پورت اشتباه</td> <td>مسیر دقیق را از Swagger یا لاگ XAiMcpServer بردارید</td> </tr> <tr> <td>هیچ ابزاری تشخیص داده نمیشود</td> <td>اسمبلی ابزارها اسکن نشده یا attributeها وجود ندارند</td> <td>وجود <code>[XAiMcpToolType]</code> و <code>[XAiMcpTool]</code> را بررسی کنید</td> </tr> <tr> <td>سرور جدید دیده نمیشود</td> <td>Provider cache شده است</td> <td>XAiApi را ریاستارت کنید یا <code>RefreshAsync</code> را صدا بزنید</td> </tr> <tr> <td>خطای جدول وجود ندارد</td> <td>migration <code>AiMcpServers</code> اعمال نشده</td> <td>migration جدید اضافه و <code>database update</code> کنید</td> </tr> <tr> <td>مدل از ابزار استفاده نمیکند</td> <td><code>Options.Tools</code> پر نشده</td> <td>بخش ۸ را پیادهسازی کنید</td> </tr> <tr> <td>401 / 403</td> <td>توکن ادمین یا Policy نامعتبر</td> <td>توکن با Policy <code>EnabledAdmin</code> دریافت کنید</td> </tr> </tbody> </table> </div> </div> <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"> <p>XAiMcpServer یک MCP Server ساده و تستشده است. برای اتصال آن به XAiApi، مسیر پیشنهادی زیر است:</p> <table class="styled-table"> <thead> <tr> <th>اولویت</th> <th>اقدام</th> </tr> </thead> <tbody> <tr> <td>۱</td> <td>تست مستقیم XAiMcpServer با <code>initialize</code>، <code>tools/list</code> و <code>tools/call</code></td> </tr> <tr> <td>۲</td> <td>اطمینان از وجود جدول <code>AiMcpServers</code> و ثبت <code>IXAiMcpServerProvider</code> در DI</td> </tr> <tr> <td>۳</td> <td>ثبت سرور از طریق API مدیریتی XAiApi با Endpoint <code>/mcp</code></td> </tr> <tr> <td>۴</td> <td>ریاستارت XAiApi و بررسی لاگ دریافت ابزارها</td> </tr> <tr class="highlight-row"> <td>۵</td> <td>تزریق ابزارها به <code>ChatOptions.Tools</code> برای استفاده واقعی مدل</td> </tr> </tbody> </table> <div class="conclusion-box"> <h3>🎯 پیشنهاد نهایی</h3> <p>برای شروع، روش ۱ (ثبت از طریق API مدیریتی) را انتخاب کنید و پس از دیدن لاگ اتصال، بخش ۸ (تزریق ابزارها به مدل) را تکمیل کنید.</p> <div class="highlight"> <strong>💎 نتیجه:</strong><br> اتصال پایه با ثبت Endpoint و فعالسازی سرور انجام میشود؛ استفاده واقعی از ابزارها نیازمند تزریق <code>IXAiMcpServerProvider</code> و پرکردن <code>ChatOptions.Tools</code> است. </div> </div> <div class="info-box"> <strong>گام بعدی:</strong> پس از اتصال موفق، ابزار <code>Echo</code> را از طریق یک درخواست Ask آزمایش کنید و سپس ابزارهای واقعیتر را با همان الگوی <code>[XAiMcpToolType]</code> و <code>[XAiMcpTool]</code> به XAiMcpServer اضافه کنید. </div> </div> </div> <div class="page-info"> این مستند فنی صرفاً برای تیم توسعه XAiApi و XAiMcpServer تهیه شده است. </div> </div> <footer class="document-footer"> <div class="footer-brand">راهنمای اتصال XAiMcpServer به XAiApi</div> <div class="footer-divider"></div> <div>نسخه ۱.۰ | آبان ۱۴۰۵ | طبقهبندی: فنی</div> </footer></div></body> </html>
|