1124 lines
41 KiB
HTML
1124 lines
41 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>راهنمای ساخت تولز در MCP Server و اتصال به 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: 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; }
|
||
|
||
.subsection {
|
||
margin-top: 30px;
|
||
margin-bottom: 20px;
|
||
}
|
||
|
||
.subsection h4 {
|
||
font-size: 16px;
|
||
font-weight: 700;
|
||
color: var(--primary);
|
||
margin-bottom: 12px;
|
||
padding-right: 12px;
|
||
border-right: 3px solid var(--accent);
|
||
padding-bottom: 6px;
|
||
}
|
||
|
||
.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: 15px;
|
||
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;
|
||
}
|
||
|
||
.comparison-card ul li:last-child { border-bottom: none; }
|
||
|
||
/* Code Block */
|
||
.code-block {
|
||
background: #1a202c;
|
||
color: #e2e8f0;
|
||
padding: 18px 22px;
|
||
border-radius: 8px;
|
||
direction: ltr;
|
||
text-align: left;
|
||
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
|
||
font-size: 12.5px;
|
||
line-height: 1.65;
|
||
overflow-x: auto;
|
||
margin: 15px 0;
|
||
border-right: 4px solid var(--accent);
|
||
white-space: pre;
|
||
}
|
||
|
||
.code-block .c { color: #68d391; } /* comment */
|
||
.code-block .k { color: #63b3ed; } /* keyword */
|
||
.code-block .s { color: #f6ad55; } /* string */
|
||
.code-block .a { color: #d6bcfa; } /* attribute */
|
||
.code-block .t { color: #fbb6ce; } /* type */
|
||
|
||
.code-label {
|
||
display: inline-block;
|
||
background: var(--primary);
|
||
color: white;
|
||
padding: 4px 12px;
|
||
border-radius: 6px 6px 0 0;
|
||
font-size: 11px;
|
||
font-weight: 600;
|
||
direction: ltr;
|
||
font-family: 'Consolas', monospace;
|
||
margin-bottom: -8px;
|
||
margin-top: 15px;
|
||
}
|
||
|
||
.number-list {
|
||
padding-right: 25px;
|
||
margin: 15px 0;
|
||
}
|
||
|
||
.number-list li {
|
||
margin-bottom: 10px;
|
||
padding-right: 5px;
|
||
}
|
||
|
||
.number-list li strong {
|
||
color: var(--primary);
|
||
}
|
||
|
||
.flow-diagram {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
gap: 10px;
|
||
margin: 25px 0;
|
||
flex-wrap: wrap;
|
||
}
|
||
|
||
.flow-step {
|
||
background: linear-gradient(135deg, var(--primary), var(--primary-light));
|
||
color: white;
|
||
padding: 15px 20px;
|
||
border-radius: 8px;
|
||
font-size: 13px;
|
||
font-weight: 600;
|
||
text-align: center;
|
||
min-width: 120px;
|
||
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
|
||
}
|
||
|
||
.flow-arrow {
|
||
font-size: 20px;
|
||
color: var(--accent);
|
||
font-weight: 800;
|
||
}
|
||
|
||
.file-path {
|
||
display: inline-block;
|
||
background: #edf2f7;
|
||
border: 1px solid #cbd5e0;
|
||
padding: 2px 8px;
|
||
border-radius: 4px;
|
||
font-family: 'Consolas', monospace;
|
||
font-size: 11.5px;
|
||
direction: ltr;
|
||
color: var(--primary);
|
||
}
|
||
|
||
.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; }
|
||
.header-top { flex-direction: column; gap: 15px; }
|
||
.doc-meta { text-align: center; }
|
||
.flow-diagram { flex-direction: column; }
|
||
}
|
||
</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-1405-002</div>
|
||
<div>نسخه: ۱.۰</div>
|
||
<div>تاریخ تهیه: آبان ۱۴۰۵</div>
|
||
<div>طبقهبندی: فنی</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="document-title">
|
||
<span class="label">TECHNICAL GUIDE</span>
|
||
<h1>راهنمای ساخت تولز در MCP Server و اتصال به xAiApi</h1>
|
||
<h2>مبتنی بر ساختار پروژه xSaherelmWorkspace</h2>
|
||
</div>
|
||
</header>
|
||
|
||
<div class="document-body">
|
||
|
||
<div class="intro-text">
|
||
<strong>هدف مستند:</strong><br>
|
||
این مستند بهعنوان یک راهنمای فنی عملی، نحوهی تعریف و ایجاد یک تولز (Tool) در MCP Server پروژه و سپس اتصال این سرور به سرویس xAiApi را بر اساس ساختار کد موجود تشریح مینماید. تمامی نمونهکدها و مسیرهای فایل بر اساس پیادهسازی فعلی پروژه استخراج شدهاند.
|
||
</div>
|
||
|
||
<!-- ===================== SECTION 1 ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۱</div>
|
||
<div class="section-title">
|
||
<h3>ایجاد یک تولز (Tool) در MCP Server</h3>
|
||
<div class="subtitle">Creating a Tool in xAiMcpServer</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
|
||
<p>پروژه <span class="file-path">xAiMcpServer</span> از کتابخانهی <span class="file-path">ModelContextProtocol.Server</span> استفاده میکند و بر پایهی سرور <span class="file-path">Grapevine</span> پیادهسازی شده است. برای تعریف تولز، دو رویکرد موجود است که در ادامه هر دو تشریح میشود:</p>
|
||
|
||
<div class="comparison-grid">
|
||
<div class="comparison-card recommended">
|
||
<span class="card-badge">پیشنهاد ما ✓</span>
|
||
<h4>روش Attribute-Based</h4>
|
||
<ul>
|
||
<li>استفاده از <code>[McpServerToolType]</code></li>
|
||
<li>استفاده از <code>[McpServerTool]</code></li>
|
||
<li>کشف خودکار از طریق Assembly</li>
|
||
<li>سازگار با استاندارد MCP SDK</li>
|
||
<li>سادهتر و کمکدتر</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="comparison-card">
|
||
<h4>روش Custom Handler</h4>
|
||
<ul>
|
||
<li>پیادهسازی <code>IXAiMcpHandler</code></li>
|
||
<li>استفاده از <code>XAiMcpToolDescriptor</code></li>
|
||
<li>مدیریت دستی JSON-RPC</li>
|
||
<li>انعطاف بیشتر در کنترل</li>
|
||
<li>مناسب برای Register پویا</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۱-۱. روش اول: Attribute-Based (روش توصیهشده)</h4>
|
||
<p class="no-indent">در این روش کافی است یک کلاس Static تعریف کرده و متدهای آن را با اتریبیوتهای مربوطه تزئین کنید. نمونهی موجود در پروژه:</p>
|
||
|
||
<div class="code-label">Tools/XEchoTools.cs</div>
|
||
<div class="code-block"><span class="c">// File: xAiMcpServer/Tools/XEchoTools.cs</span>
|
||
<span class="k">using</span> System.ComponentModel;
|
||
<span class="k">using</span> ModelContextProtocol.Server;
|
||
|
||
<span class="k">namespace</span> xAiMcpServer.Tools
|
||
{
|
||
[<span class="a">McpServerToolType</span>]
|
||
<span class="k">public static class</span> XEchoTools
|
||
{
|
||
[<span class="a">McpServerTool</span>,
|
||
<span class="a">Description</span>(<span class="s">"Echoes the message back to the client."</span>)]
|
||
<span class="k">public static string</span> Echo(string message)
|
||
{
|
||
<span class="c">//</span>
|
||
<span class="k">var</span> result = <span class="s">$"hello {message}"</span>;
|
||
<span class="k">return</span> result;
|
||
}
|
||
}
|
||
}</div>
|
||
|
||
<p class="no-indent">برای افزودن تولز جدید، کافی است یک متد جدید با اتریبیوت <code>[McpServerTool]</code> و توضیح مناسب <code>[Description(...)]</code> به همین کلاس (یا کلاس جدید با <code>[McpServerToolType]</code>) اضافه کنید:</p>
|
||
|
||
<div class="code-label">نمونه تولز جدید</div>
|
||
<div class="code-block"><span class="k">using</span> System.ComponentModel;
|
||
<span class="k">using</span> ModelContextProtocol.Server;
|
||
|
||
<span class="k">namespace</span> xAiMcpServer.Tools
|
||
{
|
||
[<span class="a">McpServerToolType</span>]
|
||
<span class="k">public static class</span> XEchoTools
|
||
{
|
||
[<span class="a">McpServerTool</span>,
|
||
<span class="a">Description</span>(<span class="s">"Echoes the message back to the client."</span>)]
|
||
<span class="k">public static string</span> Echo(string message)
|
||
=> <span class="s">$"hello {message}"</span>;
|
||
|
||
<span class="c">// --- تولز جدید: جمع دو عدد ---</span>
|
||
[<span class="a">McpServerTool</span>,
|
||
<span class="a">Description</span>(<span class="s">"Adds two numbers and returns the sum."</span>)]
|
||
<span class="k">public static int</span> Add(
|
||
[<span class="a">Description</span>(<span class="s">"First number"</span>)] int a,
|
||
[<span class="a">Description</span>(<span class="s">"Second number"</span>)] int b)
|
||
=> a + b;
|
||
}
|
||
}</div>
|
||
|
||
<div class="success-box">
|
||
<strong>✓ کشف خودکار:</strong> در <span class="file-path">Startup.cs</span> از طریق <code>WithToolsFromAssembly()</code> تمام کلاسهای دارای <code>[McpServerToolType]</code> در اسمبلی جاری بهصورت خودکار شناسایی و ثبت میشوند.
|
||
</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۱-۲. روش دوم: Custom Handler با XAiMcpToolDescriptor</h4>
|
||
<p class="no-indent">در این روش از مکانیزم داخلی <code>XAiMcpHandler</code> استفاده میشود. تولزها در یک دیکشنری به نام <code>tools</code> از نوع <code>Dictionary<string, XAiMcpToolDescriptor></code> نگهداری میشوند.</p>
|
||
|
||
<div class="code-label">xAiService/Services/XAiMcpHandler.cs</div>
|
||
<div class="code-block"><span class="k">private void</span> RegisterTools()
|
||
{
|
||
<span class="c">//</span>
|
||
tools[<span class="s">"search_knowledge"</span>] = <span class="k">new</span> XAiMcpToolDescriptor
|
||
{
|
||
Name = <span class="s">"search_knowledge"</span>,
|
||
Description = <span class="s">"Search in knowledge base"</span>,
|
||
InputSchema = <span class="k">new</span> JObject
|
||
{
|
||
[<span class="s">"type"</span>] = <span class="s">"object"</span>,
|
||
[<span class="s">"properties"</span>] = <span class="k">new</span> JObject
|
||
{
|
||
[<span class="s">"query"</span>] = <span class="k">new</span> JObject { [<span class="s">"type"</span>] = <span class="s">"string"</span> }
|
||
},
|
||
[<span class="s">"required"</span>] = <span class="k">new</span> JArray(<span class="s">"query"</span>)
|
||
},
|
||
Execute = <span class="k">async</span> (args, ct) =>
|
||
{
|
||
<span class="k">var</span> query = args?[<span class="s">"query"</span>]?.ToString() ?? <span class="s">""</span>;
|
||
<span class="k">return</span> <span class="s">$"Results for: {query}"</span>;
|
||
}
|
||
};
|
||
}</div>
|
||
|
||
<p class="no-indent">ساختار <code>XAiMcpToolDescriptor</code> در <span class="file-path">xAiModels/Models/XAiMcpToolDescriptor.cs</span> تعریف شده است:</p>
|
||
|
||
<div class="code-label">Models/XAiMcpToolDescriptor.cs</div>
|
||
<div class="code-block"><span class="k">public class</span> XAiMcpToolDescriptor
|
||
{
|
||
<span class="k">public string</span> Name { <span class="k">get</span>; <span class="k">set</span>; }
|
||
<span class="k">public string</span> Description { <span class="k">get</span>; <span class="k">set</span>; }
|
||
<span class="k">public</span> JObject InputSchema { <span class="k">get</span>; <span class="k">set</span>; }
|
||
<span class="k">public</span> Func<JObject, CancellationToken, Task<string>> Execute { <span class="k">get</span>; <span class="k">set</span>; }
|
||
}</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۱-۳. ثبت و پیکربندی در DI و Startup</h4>
|
||
<p class="no-indent">در فایل <span class="file-path">Startup.cs</span> پروژه <span class="file-path">xAiMcpServer</span>، دو ثبت اصلی مورد نیاز است:</p>
|
||
|
||
<div class="code-label">Startup.cs - ConfigureServices</div>
|
||
<div class="code-block"><span class="c">//</span>
|
||
<span class="c">// Register MCP Handler (for Custom Handler approach)</span>
|
||
services.AddXAiMcpHandler();
|
||
|
||
<span class="c">// ... سایر سرویسها ...</span>
|
||
|
||
<span class="c">//</span>
|
||
<span class="c">// Register MCP Server with Http Transport and Assembly Tool Discovery</span>
|
||
services
|
||
.AddMcpServer()
|
||
.WithHttpTransport()
|
||
.WithToolsFromAssembly();</div>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 نکته:</strong> تزریق <code>WithToolsFromAssembly()</code> باعث میشود تمام کلاسهای دارای <code>[McpServerToolType]</code> در اسمبلی جاری اسکن شده و متدهای دارای <code>[McpServerTool]</code> بهعنوان تولز ثبت شوند.
|
||
</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۱-۴. کنترلر MCP</h4>
|
||
<p class="no-indent">کنترلر <span class="file-path">McpController</span> وظیفهی پردازش درخواستهای JSON-RPC در مسیر <code>/mcp</code> را بر عهده دارد:</p>
|
||
|
||
<div class="code-label">Controllers/McpController.cs</div>
|
||
<div class="code-block"><span class="k">public class</span> McpController : XAiMcpControllerBase
|
||
{
|
||
<span class="k">public</span> McpController(
|
||
IXAiMcpHandler mcpHandler,
|
||
ILogger<McpController> logger,
|
||
XAppConfiguration appConfiguration,
|
||
IXIdentityProvider identityProvider,
|
||
XValidationProvider validationProvider
|
||
) : base(mcpHandler, appConfiguration, identityProvider, logger, validationProvider)
|
||
{ }
|
||
}</div>
|
||
|
||
<p class="no-indent">مسیرهای در دسترس:</p>
|
||
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>متد</th>
|
||
<th>مسیر</th>
|
||
<th>توضیح</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td><strong>POST</strong></td>
|
||
<td><code>/mcp</code></td>
|
||
<td>پردازش JSON-RPC (initialize, tools/list, tools/call, ping)</td>
|
||
</tr>
|
||
<tr class="highlight-row">
|
||
<td><strong>GET</strong></td>
|
||
<td><code>/mcp/sse</code></td>
|
||
<td>Endpoint مربوط به Server-Sent Events (SSE)</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۱-۵. نکات مهم در تعریف تولز</h4>
|
||
<ul class="number-list">
|
||
<li><strong>نام منحصربهفرد:</strong> نام متد/تولز در سطح سرور باید یکتا باشد.</li>
|
||
<li><strong>توضیح شفاف:</strong> فیلد <code>Description</code> مستقیماً به LLM ارسال میشود؛ لذا توضیح دقیق پارامترها ضروری است.</li>
|
||
<li><strong>Schema ورودی:</strong> در روش Descriptor، فیلد <code>InputSchema</code> باید یک JSON Schema معتبر باشد.</li>
|
||
<li><strong>Thread-Safety:</strong> متدهای تولز ممکن است همزمان فراخوانی شوند؛ از State سراسری پرهیز کنید.</li>
|
||
<li><strong>Exception Handling:</strong> خطاها را بهصورت پیام متنی برگردانید تا بهدرستی به کلاینت منتقل شوند.</li>
|
||
</ul>
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===================== SECTION 2 ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۲</div>
|
||
<div class="section-title">
|
||
<h3>اتصال MCP Server به xAiApi</h3>
|
||
<div class="subtitle">Connecting xAiMcpServer to xAiApi</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
|
||
<p>پس از آمادهسازی تولزها، نوبت به ثبت MCP Server در سرویس <span class="file-path">xAiApi</span> و برقراری اتصال میرسد. جریان کلی بهصورت زیر است:</p>
|
||
|
||
<div class="flow-diagram">
|
||
<div class="flow-step">ثبت سرور<br>در دیتابیس</div>
|
||
<span class="flow-arrow">←</span>
|
||
<div class="flow-step">اتصال<br>XAiMcpServerProvider</div>
|
||
<span class="flow-arrow">←</span>
|
||
<div class="flow-step">دریافت لیست<br>تولزها</div>
|
||
<span class="flow-arrow">←</span>
|
||
<div class="flow-step">افزودن به<br>ChatOptions</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۲-۱. ساختار مدل XAiMcpServer</h4>
|
||
<p class="no-indent">موجودیت MCP Server در <span class="file-path">xAiModels/Models/Entities/XAiMcpServer.cs</span> تعریف شده است:</p>
|
||
|
||
<div class="code-label">Models/Entities/XAiMcpServer.cs</div>
|
||
<div class="code-block"><span class="k">public class</span> XAiMcpServer : XBaseGuidIDEntity
|
||
{
|
||
[Required] <span class="k">public string</span> OwnerId { <span class="k">get</span>; <span class="k">set</span>; }
|
||
<span class="k">public</span> XAiMcpServerUsageScope Scope { <span class="k">get</span>; <span class="k">set</span>; } = XAiMcpServerUsageScope.Owner;
|
||
|
||
[Required] <span class="k">public string</span> Name { <span class="k">get</span>; <span class="k">set</span>; }
|
||
<span class="k">public string</span> Description { <span class="k">get</span>; <span class="k">set</span>; }
|
||
<span class="k">public bool</span> Enabled { <span class="k">get</span>; <span class="k">set</span>; } = <span class="k">true</span>;
|
||
<span class="k">public string</span> EnvironmentVariables { <span class="k">get</span>; <span class="k">set</span>; }
|
||
<span class="k">public string</span> Endpoint { <span class="k">get</span>; <span class="k">set</span>; }
|
||
<span class="k">public string</span> Headers { <span class="k">get</span>; <span class="k">set</span>; }
|
||
<span class="k">public int</span> TimeoutSeconds { <span class="k">get</span>; <span class="k">set</span>; } = 30;
|
||
}</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۲-۲. ثبت سرور از طریق API</h4>
|
||
<p class="no-indent">کنترلر <span class="file-path">McpServerController</span> در xAiApi عملیات CRUD کامل را ارائه میدهد. برای ثبت یک MCP Server کافی است درخواست زیر را ارسال کنید:</p>
|
||
|
||
<div class="code-label">POST /api/v1/mcpServer</div>
|
||
<div class="code-block">{
|
||
<span class="s">"name"</span>: <span class="s">"xAiMcpServer"</span>,
|
||
<span class="s">"description"</span>: <span class="s">"AI MCP Server for xAiApi"</span>,
|
||
<span class="s">"endpoint"</span>: <span class="s">"https://localhost:7001/mcp"</span>,
|
||
<span class="s">"enabled"</span>: <span class="k">true</span>,
|
||
<span class="s">"scope"</span>: <span class="s">"Owner"</span>,
|
||
<span class="s">"timeoutSeconds"</span>: <span class="k">30</span>,
|
||
<span class="s">"headers"</span>: <span class="s">"{\"Authorization\":\"Bearer YOUR_TOKEN\"}"</span>
|
||
}</div>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 نکته درباره فیلد Endpoint:</strong> مقدار باید مسیر دقیق <code>/mcp</code> سرور را مشخص کند؛ مثلاً <code>https://localhost:7001/mcp</code> که در <span class="file-path">XAiMcpControllerBase</span> با <code>[Route("mcp")]</code> تعریف شده است.
|
||
</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۲-۳. مکانیزم اتصال (XAiMcpServerProvider)</h4>
|
||
<p class="no-indent">کلاس <span class="file-path">XAiMcpServerProvider</span> وظیفهی برقراری اتصال و مدیریت طولعمر <code>McpClient</code> را بر عهده دارد. فرآیند اتصال به شرح زیر است:</p>
|
||
|
||
<ol class="number-list">
|
||
<li><strong>دریافت لیست سرورهای فعال:</strong> از جدول <code>AiMcpServers</code>، سرورهایی که <code>Enabled == true</code> هستند خوانده میشوند.</li>
|
||
<li><strong>ساخت Client Transport:</strong> با استفاده از <code>HttpClientTransport</code> و تنظیم Endpoint و Headers.</li>
|
||
<li><strong>Ping و دریافت تولزها:</strong> با <code>PingAsync</code> و <code>ListToolsAsync</code>.</li>
|
||
<li><strong>ذخیره در کش:</strong> نتایج در دیکشنریهای <code>clients</code>، <code>tools</code> و <code>mcpServers</code> ذخیره میشوند.</li>
|
||
</ol>
|
||
|
||
<div class="code-label">Providers/XAiMcpServerProvider.cs - CreateClientAsync</div>
|
||
<div class="code-block"><span class="k">private static async</span> Task<McpClient> CreateClientAsync(
|
||
XAiMcpServerDto s,
|
||
CancellationToken cancellationToken)
|
||
{
|
||
<span class="c">//</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 = ParseJsonDict(s.Headers),
|
||
TransportMode = HttpTransportMode.AutoDetect
|
||
});
|
||
|
||
<span class="c">//</span>
|
||
<span class="k">return await</span> McpClient
|
||
.CreateAsync(
|
||
transport,
|
||
cancellationToken: cancellationToken
|
||
);
|
||
}</div>
|
||
|
||
<div class="code-label">Providers/XAiMcpServerProvider.cs - ConnectAndValidateAsync</div>
|
||
<div class="code-block"><span class="k">private async</span> Task ConnectAndValidateAsync(
|
||
XAiMcpServerDto descriptor,
|
||
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, cts.Token);
|
||
<span class="k">await</span> client.PingAsync(cancellationToken: cts.Token);
|
||
<span class="k">var</span> serverTools = <span class="k">await</span> client
|
||
.ListToolsAsync(cancellationToken: cts.Token);
|
||
|
||
<span class="c">//</span>
|
||
clients[descriptor.Id] = client;
|
||
tools[descriptor.Id] = serverTools;
|
||
mcpServers[descriptor.Id] = descriptor;
|
||
}
|
||
<span class="k">catch</span> (Exception ex)
|
||
{
|
||
logger.LogWarning(ex,
|
||
<span class="s">"MCP '{Name}': Connection Established Failed, Ignored ..."</span>,
|
||
descriptor.Name);
|
||
}
|
||
}</div>
|
||
|
||
<div class="success-box">
|
||
<strong>✓ نکته:</strong> در صورت عدم موفقیت اتصال، خطا فقط Log شده و سایر سرورها تحت تأثیر قرار نمیگیرند (Fail-Safe).
|
||
</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۲-۴. دریافت تولزهای در دسترس</h4>
|
||
<p class="no-indent">متد <code>GetAvailableToolsAsync</code> بر اساس Scope کاربر، لیست تولزهای مجاز را برمیگرداند:</p>
|
||
|
||
<div class="code-label">Providers/XAiMcpServerProvider.cs - GetAvailableToolsAsync</div>
|
||
<div class="code-block"><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.IsAdmin())
|
||
{
|
||
forceOwner = <span class="k">true</span>;
|
||
}
|
||
|
||
<span class="c">//</span>
|
||
<span class="k">var</span> selectedServers = mcpServers
|
||
.Where(s =>
|
||
s.Value.OwnerId == userInfo.UserId ||
|
||
s.Value.Scope == XAiMcpServerUsageScope.All
|
||
)
|
||
.Select(s => s.Key);
|
||
|
||
<span class="c">//</span>
|
||
<span class="k">var</span> result = tools
|
||
.Where(c => selectedServers.Contains(c.Key))
|
||
.SelectMany(c => c.Value);
|
||
|
||
<span class="k">return</span> [.. result];
|
||
}</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۲-۵. استفاده از تولزها در XAiService</h4>
|
||
<p class="no-indent">در سرویس <span class="file-path">XAIServiceBase</span>، ویژگی <code>Options</code> از نوع <code>ChatOptions</code> وجود دارد که میتوان تولزهای MCP را به آن افزود:</p>
|
||
|
||
<div class="code-label">Providers/XAIServiceBase.cs (الگوی استفاده)</div>
|
||
<div class="code-block"><span class="c">// داخل یکی از متدهای XAIServiceBase</span>
|
||
<span class="k">var</span> tools = <span class="k">await</span> mcpServerProvider.GetAvailableToolsAsync(
|
||
userInfo: userInfo,
|
||
cancellationToken: cancellationToken
|
||
);
|
||
<span class="k">if</span> (tools.HasChild())
|
||
{
|
||
Options.Tools = [.. tools];
|
||
}
|
||
|
||
<span class="c">//</span>
|
||
<span class="k">using var</span> client = Descriptor.GetClient();
|
||
<span class="k">var</span> response = <span class="k">await</span> client.GetResponseAsync(
|
||
options: Options,
|
||
messages: history,
|
||
cancellationToken: cancellationToken
|
||
);</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۲-۶. مدیریت چرخهی حیات سرور</h4>
|
||
<p class="no-indent">سرویس <span class="file-path">XAiMcpServerProvider</span> متدهایی برای مدیریت پویا ارائه میدهد:</p>
|
||
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>متد</th>
|
||
<th>وظیفه</th>
|
||
<th>تأثیر بر کش</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td><code>AddMcpServer</code></td>
|
||
<td>افزودن سرور جدید</td>
|
||
<td>اتصال و افزودن به کش</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>UpdateMcpServer</code></td>
|
||
<td>بروزرسانی تنظیمات</td>
|
||
<td>حذف و اتصال مجدد</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>RemoveMcpServer</code></td>
|
||
<td>حذف نرم سرور</td>
|
||
<td>حذف از کش</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>EnableMcpServer</code></td>
|
||
<td>فعالسازی</td>
|
||
<td>اتصال و افزودن</td>
|
||
</tr>
|
||
<tr class="highlight-row">
|
||
<td><code>RefreshAsync</code></td>
|
||
<td>بازسازی کامل کش</td>
|
||
<td>Dispose همه و اتصال مجدد</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۲-۷. کنترل دسترسی (Scope)</h4>
|
||
<p class="no-indent">هر سرور MCP دارای یک فیلد <code>Scope</code> است که تعیین میکند چه کاربرانی میتوانند از تولزهای آن استفاده کنند:</p>
|
||
|
||
<div class="comparison-grid">
|
||
<div class="comparison-card">
|
||
<h4>Scope = Owner</h4>
|
||
<ul>
|
||
<li>فقط صاحب سرور به تولزها دسترسی دارد</li>
|
||
<li>پیشفرض برای کاربران عادی</li>
|
||
<li>مناسب اطلاعات حساس</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="comparison-card recommended">
|
||
<span class="card-badge">اشتراکی ✓</span>
|
||
<h4>Scope = All</h4>
|
||
<ul>
|
||
<li>همه کاربران به تولزها دسترسی دارند</li>
|
||
<li>تنها ادمینها میتوانند تنظیم کنند</li>
|
||
<li>مناسب تولزهای عمومی</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="subsection">
|
||
<h4>۲-۸. چکلیست اتصال موفق</h4>
|
||
<ul class="number-list">
|
||
<li>سرور MCP در حال اجرا روی Endpoint مشخص باشد (مثلاً <code>https://localhost:7001/mcp</code>).</li>
|
||
<li>گواهی SSL در محیط توسعه پذیرفته شده باشد (در <span class="file-path">Startup.cs</span> تنظیم شده است).</li>
|
||
<li>فیلد <code>Endpoint</code> دقیقاً به مسیر <code>/mcp</code> اشاره کند.</li>
|
||
<li>در صورت نیاز به احراز هویت، <code>Headers</code> به فرمت JSON معتبر تنظیم شود.</li>
|
||
<li>مقدار <code>TimeoutSeconds</code> حداقل ۳۰ ثانیه باشد.</li>
|
||
<li>سرور در وضعیت <code>Enabled = true</code> قرار داشته باشد.</li>
|
||
<li>Scope بهدرستی تنظیم شده باشد (Owner برای خصوصی، All برای اشتراکی).</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="warning-box">
|
||
<strong>⚠ هشدار امنیتی:</strong> هرگز توکنهای حساس را در فیلد <code>Headers</code> بهصورت plain-text ذخیره نکنید. از <code>EnvironmentVariables</code> یا سیستم مدیریت Secrets استفاده نمایید.
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===================== CONCLUSION ===================== -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۳</div>
|
||
<div class="section-title">
|
||
<h3>جمعبندی و نقشهی راه</h3>
|
||
<div class="subtitle">Summary & Roadmap</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
|
||
<p>فرآیند کلی از تعریف تولز تا استفاده در xAiApi شامل مراحل زیر است:</p>
|
||
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>مرحله</th>
|
||
<th>مسئول</th>
|
||
<th>فایل/ماژول</th>
|
||
<th>خروجی</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>۱. تعریف تولز</td>
|
||
<td>توسعهدهنده</td>
|
||
<td><span class="file-path">xAiMcpServer/Tools/</span></td>
|
||
<td>متد با <code>[McpServerTool]</code></td>
|
||
</tr>
|
||
<tr>
|
||
<td>۲. ثبت در DI</td>
|
||
<td>توسعهدهنده</td>
|
||
<td><span class="file-path">Startup.cs</span></td>
|
||
<td>ثبت خودکار تولز</td>
|
||
</tr>
|
||
<tr>
|
||
<td>۳. اجرای سرور</td>
|
||
<td>DevOps</td>
|
||
<td><span class="file-path">Program.cs</span></td>
|
||
<td>سرور روی Port فعال</td>
|
||
</tr>
|
||
<tr>
|
||
<td>۴. ثبت سرور در xAiApi</td>
|
||
<td>کاربر/ادمین</td>
|
||
<td><span class="file-path">McpServerController</span></td>
|
||
<td>رکورد در جدول</td>
|
||
</tr>
|
||
<tr>
|
||
<td>۵. اتصال خودکار</td>
|
||
<td>XAiMcpServerProvider</td>
|
||
<td><span class="file-path">Providers/</span></td>
|
||
<td>کش تولزها</td>
|
||
</tr>
|
||
<tr class="highlight-row">
|
||
<td><strong>۶. استفاده در Chat</strong></td>
|
||
<td><strong>XAIServiceBase</strong></td>
|
||
<td><strong><span class="file-path">Providers/XAIServiceBase.cs</span></strong></td>
|
||
<td><strong>افزودن به ChatOptions</strong></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<div class="conclusion-box">
|
||
<h3>🎯 نتیجه نهایی</h3>
|
||
<p>با پیروی از ساختار موجود، افزودن یک تولز جدید در سمت MCP Server تنها نیازمند یک متد با اتریبیوت <code>[McpServerTool]</code> است و اتصال آن به xAiApi بهصورت خودکار از طریق <code>XAiMcpServerProvider</code> مدیریت میشود.</p>
|
||
<div class="highlight">
|
||
<strong>💎 نکته کلیدی:</strong><br>
|
||
معماری موجود بهگونهای طراحی شده که توسعهی تولزهای جدید بدون نیاز به تغییر در xAiApi انجام شود؛ کافی است سرور MCP مجدداً اجرا و در صورت نیاز از طریق <code>RefreshAsync</code> بازخوانی شود.
|
||
</div>
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<div class="page-info">
|
||
این مستند فنی برای استفادهی تیم توسعهی xSaherelmWorkspace تهیه شده است. آخرین بروزرسانی بر اساس ساختار کد فعلی پروژه انجام شده است.
|
||
</div>
|
||
|
||
</div>
|
||
|
||
<footer class="document-footer">
|
||
<div class="footer-brand">تیم توسعه: xSaherelmWorkspace</div>
|
||
<div class="footer-divider"></div>
|
||
<div>تاریخ تهیه: آبان ۱۴۰۵ | نسخه: ۱.۰ | طبقهبندی: فنی</div>
|
||
</footer>
|
||
|
||
</div>
|
||
|
||
</body>
|
||
</html> |