1236 lines
54 KiB
HTML
1236 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>راهنمای گامبهگام ساخت MCP Server در C# .NET Standard 2.0</title>
|
||
<style>
|
||
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500;600&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;
|
||
--code-bg: #1e293b;
|
||
--code-text: #e2e8f0;
|
||
--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: 1050px;
|
||
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: 27px;
|
||
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); font-family: 'JetBrains Mono', monospace; direction: ltr; text-align: right; }
|
||
|
||
.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, .info-box, .success-box, .warning-box, .danger-box {
|
||
padding: 18px 22px;
|
||
border-radius: 8px;
|
||
margin: 20px 0;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.highlight-box {
|
||
background: linear-gradient(to left, #fffaf0, #fef5e7);
|
||
border: 1px solid #f6e05e;
|
||
border-right: 4px solid var(--accent);
|
||
}
|
||
.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);
|
||
}
|
||
|
||
.success-box {
|
||
background: linear-gradient(to left, #f0fff4, #e6fffa);
|
||
border: 1px solid #9ae6b4;
|
||
border-right: 4px solid var(--success);
|
||
}
|
||
|
||
.warning-box {
|
||
background: linear-gradient(to left, #fffaf0, #fef5e7);
|
||
border: 1px solid #fbd38d;
|
||
border-right: 4px solid var(--warning);
|
||
}
|
||
|
||
.danger-box {
|
||
background: linear-gradient(to left, #fff5f5, #fed7d7);
|
||
border: 1px solid #fc8181;
|
||
border-right: 4px solid var(--danger);
|
||
}
|
||
|
||
pre {
|
||
background: var(--code-bg);
|
||
color: var(--code-text);
|
||
padding: 20px 22px;
|
||
border-radius: 8px;
|
||
overflow-x: auto;
|
||
margin: 15px 0;
|
||
font-family: 'JetBrains Mono', 'Courier New', monospace;
|
||
font-size: 12.5px;
|
||
line-height: 1.7;
|
||
direction: ltr;
|
||
text-align: left;
|
||
border: 1px solid #334155;
|
||
position: relative;
|
||
}
|
||
|
||
pre::before {
|
||
content: attr(data-lang);
|
||
position: absolute;
|
||
top: 8px;
|
||
right: 12px;
|
||
font-size: 10px;
|
||
color: #64748b;
|
||
text-transform: uppercase;
|
||
letter-spacing: 1px;
|
||
font-weight: 600;
|
||
}
|
||
|
||
code {
|
||
font-family: 'JetBrains Mono', 'Courier New', monospace;
|
||
background: #f1f5f9;
|
||
color: #1e293b;
|
||
padding: 2px 6px;
|
||
border-radius: 4px;
|
||
font-size: 12.5px;
|
||
direction: ltr;
|
||
display: inline-block;
|
||
}
|
||
|
||
pre code { background: none; color: inherit; padding: 0; display: inline; }
|
||
|
||
.file-path {
|
||
background: #2c5282;
|
||
color: #bee3f8;
|
||
padding: 8px 14px;
|
||
border-radius: 6px 6px 0 0;
|
||
font-family: 'JetBrains Mono', monospace;
|
||
font-size: 11.5px;
|
||
direction: ltr;
|
||
text-align: left;
|
||
margin-top: 15px;
|
||
margin-bottom: -15px;
|
||
border-bottom: 2px solid #1a365d;
|
||
}
|
||
|
||
.file-path::before { content: '📄 '; margin-left: 4px; }
|
||
|
||
.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; }
|
||
|
||
.file-tree {
|
||
background: #f8fafc;
|
||
border: 1px solid var(--border);
|
||
border-radius: 8px;
|
||
padding: 20px 25px;
|
||
margin: 15px 0;
|
||
font-family: 'JetBrains Mono', monospace;
|
||
font-size: 12.5px;
|
||
direction: ltr;
|
||
text-align: left;
|
||
line-height: 1.9;
|
||
color: var(--text-muted);
|
||
}
|
||
.file-tree .folder { color: var(--primary); font-weight: 600; }
|
||
.file-tree .new { color: var(--success); }
|
||
|
||
.step-badge {
|
||
display: inline-block;
|
||
background: var(--accent);
|
||
color: white;
|
||
padding: 3px 12px;
|
||
border-radius: 15px;
|
||
font-size: 12px;
|
||
font-weight: 600;
|
||
margin-left: 8px;
|
||
}
|
||
|
||
.checklist { list-style: none; padding: 0; margin: 15px 0; }
|
||
.checklist li {
|
||
padding: 10px 15px;
|
||
background: #f8fafc;
|
||
border-right: 3px solid var(--primary-light);
|
||
margin-bottom: 8px;
|
||
border-radius: 6px;
|
||
font-size: 13.5px;
|
||
}
|
||
.checklist li::before {
|
||
content: '✓';
|
||
color: var(--success);
|
||
font-weight: 700;
|
||
margin-left: 10px;
|
||
display: inline-block;
|
||
}
|
||
|
||
.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;
|
||
}
|
||
|
||
.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: 20px;
|
||
font-weight: 800;
|
||
margin-bottom: 5px;
|
||
color: var(--accent);
|
||
line-height: 1.3;
|
||
}
|
||
|
||
.stat-card .stat-label { font-size: 12px; opacity: 0.9; }
|
||
|
||
@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; } .stats-grid { grid-template-columns: 1fr; } }
|
||
</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>شماره مستند: GUIDE-MCP-1405-001</div>
|
||
<div>نسخه: ۱.۰</div>
|
||
<div>تاریخ تهیه: آبان ۱۴۰۵</div>
|
||
<div>طبقهبندی: آموزشی-فنی</div>
|
||
</div>
|
||
</div>
|
||
<div class="document-title">
|
||
<span class="label">STEP-BY-STEP TUTORIAL</span>
|
||
<h1>راهنمای گامبهگام ساخت MCP Server در C#</h1>
|
||
<h2>پیادهسازی کامل با .NET Standard 2.0 و کتابخانه رسمی ModelContextProtocol</h2>
|
||
</div>
|
||
</header>
|
||
|
||
<div class="document-body">
|
||
|
||
<div class="intro-text">
|
||
<strong>هدف مستند:</strong><br>
|
||
این مستند، یک راهنمای عملی و گامبهگام برای ساخت یک <strong>MCP Server</strong> (Model Context Protocol Server) با استفاده از زبان C# و کتابخانه رسمی <code>ModelContextProtocol</code> است. تمام مراحل با کدهای کامل و قابل اجرا ارائه شده و پروژه نهایی روی <strong>.NET Standard 2.0</strong> کامپایل میشود تا با طیف گستردهای از فریمورکها (از جمله .NET Framework قدیمیتر) سازگار باشد. در پایان، سرور ما با کلاینتهایی مانند <strong>Claude Desktop</strong> یا <strong>GitHub Copilot</strong> قابل استفاده خواهد بود.
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 0: MCP چیست -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۰</div>
|
||
<div class="section-title">
|
||
<h3>MCP چیست و چرا اهمیت دارد؟</h3>
|
||
<div class="subtitle">What is MCP? / Why it matters</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p><strong>Model Context Protocol (MCP)</strong> یک پروتکل باز و استاندارد است که توسط شرکت Anthropic معرفی شده و هدف آن، ایجاد یک زبان مشترک بین <strong>مدلهای زبانی (LLM)</strong> و <strong>منابع خارجی</strong> (مانند دیتابیس، API، فایلسیستم، ابزارهای داخلی سازمان) است. به کمک MCP، مدلهای زبانی میتوانند بهطور استاندارد و امن، به دادهها و قابلیتهای خارجی دسترسی داشته باشند بدون آنکه نیاز به کدنویسی اختصاصی برای هر مدل باشد.</p>
|
||
|
||
<div class="stats-grid">
|
||
<div class="stat-card">
|
||
<div class="stat-value">استاندارد<br>باز</div>
|
||
<div class="stat-label">پشتیبانی توسط Anthropic، Microsoft، و جامعه</div>
|
||
</div>
|
||
<div class="stat-card">
|
||
<div class="stat-value">سه<br>قابلیت</div>
|
||
<div class="stat-label">Tools، Resources، Prompts</div>
|
||
</div>
|
||
<div class="stat-card">
|
||
<div class="stat-value">قابل<br>حمل</div>
|
||
<div class="stat-label">یک سرور، چند کلاینت</div>
|
||
</div>
|
||
</div>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">سه قابلیت اصلی MCP:</h4>
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>قابلیت</th>
|
||
<th>توضیح</th>
|
||
<th>مثال</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td><strong>Tools</strong></td>
|
||
<td>توابع قابل فراخوانی توسط مدل (مانند Function Calling)</td>
|
||
<td>جستجو در دیتابیس، ارسال ایمیل، محاسبه</td>
|
||
</tr>
|
||
<tr>
|
||
<td><strong>Resources</strong></td>
|
||
<td>دادههای فقطخواندنی که مدل میتواند بخواند</td>
|
||
<td>فایل، رکورد دیتابیس، تنظیمات</td>
|
||
</tr>
|
||
<tr>
|
||
<td><strong>Prompts</strong></td>
|
||
<td>قالبهای آماده برای پرامپتها</td>
|
||
<td>الگوهای پاسخ، شخصیتها، سناریوها</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 نکته:</strong> در این مستند، تمرکز ما بر روی <strong>Tools</strong> خواهد بود، چراکه پرکاربردترین قابلیت MCP است و اجازه میدهد منطق تجاری موجود در پروژههای C# خود را بهراحتی در اختیار مدلهای زبانی قرار دهید.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 1: Prerequisites -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۱</div>
|
||
<div class="section-title">
|
||
<h3>پیشنیازها و آمادهسازی محیط</h3>
|
||
<div class="subtitle">Prerequisites</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>پیش از شروع، مطمئن شوید که موارد زیر روی سیستم شما نصب و آماده هستند:</p>
|
||
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>پیشنیاز</th>
|
||
<th>حداقل نسخه</th>
|
||
<th>توضیح</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td><strong>.NET SDK</strong></td>
|
||
<td>8.0 یا بالاتر</td>
|
||
<td>برای اجرا و Build پروژه (خود کتابخانه روی netstandard2.0 کار میکند)</td>
|
||
</tr>
|
||
<tr>
|
||
<td><strong>IDE یا Editor</strong></td>
|
||
<td>—</td>
|
||
<td>Visual Studio 2022، VS Code، یا Rider</td>
|
||
</tr>
|
||
<tr>
|
||
<td><strong>NuGet</strong></td>
|
||
<td>—</td>
|
||
<td>بهطور خودکار همراه .NET SDK نصب میشود</td>
|
||
</tr>
|
||
<tr>
|
||
<td><strong>کلاینت MCP (اختیاری)</strong></td>
|
||
<td>—</td>
|
||
<td>Claude Desktop، GitHub Copilot، یا هر MCP Client دیگر برای تست</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 25px;">بررسی نصب .NET SDK:</h4>
|
||
<p class="no-indent">در ترمینال یا Command Prompt دستور زیر را اجرا کنید:</p>
|
||
|
||
<pre data-lang="bash"><code>dotnet --version</code></pre>
|
||
|
||
<p class="no-indent">اگر نسخه ۸.۰ یا بالاتر نمایش داده شد، همه چیز آماده است. در غیر این صورت، از سایت رسمی <code>dotnet.microsoft.com/download</code> آخرین نسخه را دانلود و نصب کنید.</p>
|
||
|
||
<div class="warning-box">
|
||
<strong>⚠️ توجه به .NET Standard 2.0:</strong> اگرچه خود پروژه شما بر روی <code>netstandard2.0</code> کامپایل میشود، اما برای اجرای آن و ساخت DLL نیاز به یک SDK مدرن دارید. این SDK هنگام اجرا، پروژه شما را به یک باینری سازگار تبدیل میکند. کتابخانه <code>ModelContextProtocol</code> بهطور رسمی از <code>netstandard2.0</code> پشتیبانی میکند.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 2: Create Project -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۲</div>
|
||
<div class="section-title">
|
||
<h3>ایجاد پروژه جدید</h3>
|
||
<div class="subtitle">Create a New Project</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>در ترمینال، به مسیری که میخواهید پروژه در آن ساخته شود بروید و دستور زیر را اجرا کنید:</p>
|
||
|
||
<pre data-lang="bash"><code>dotnet new console -n SampleMcpServer
|
||
cd SampleMcpServer</code></pre>
|
||
|
||
<p>این دستور یک پروژه کنسول ساده با نام <code>SampleMcpServer</code> ایجاد میکند و شما را به پوشه آن منتقل میکند.</p>
|
||
|
||
<div class="file-tree">
|
||
<span class="folder">SampleMcpServer/</span>
|
||
├── <span class="new">Program.cs</span>
|
||
├── <span class="new">SampleMcpServer.csproj</span>
|
||
└── obj/
|
||
</div>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 چرا Console؟</strong> سرورهای MCP اغلب بهصورت پروسههای Console اجرا میشوند و از طریق <strong>stdin/stdout</strong> با کلاینت (مثلاً Claude Desktop) ارتباط برقرار میکنند. این نوع Transport که <strong>stdio</strong> نامیده میشود، سادهترین و پرکاربردترین روش برای اتصال کلاینت و سرور در محیط دسکتاپ است.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 3: NuGet Packages -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۳</div>
|
||
<div class="section-title">
|
||
<h3>افزودن پکیجهای NuGet</h3>
|
||
<div class="subtitle">Add NuGet Packages</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>دو پکیج اصلی نیاز دارید:</p>
|
||
|
||
<pre data-lang="bash"><code>dotnet add package ModelContextProtocol
|
||
dotnet add package Microsoft.Extensions.Hosting</code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">توضیح پکیجها:</h4>
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>پکیج</th>
|
||
<th>نقش</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td><code>ModelContextProtocol</code></td>
|
||
<td>هسته اصلی SDK — شامل سرور، کلاینت، Transportها (stdio/http)، و Attributeهای `[McpServerTool]`</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>Microsoft.Extensions.Hosting</code></td>
|
||
<td>زیرساخت Host و Dependency Injection — برای راهاندازی سرویسها با الگوی مدرن .NET</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 تأیید نسخه:</strong> کتابخانه <code>ModelContextProtocol</code> بهطور فعال توسط Microsoft و Anthropic توسعه داده میشود و نسخه آن مرتباً بهروزرسانی میشود. برای دریافت آخرین نسخه از <code>nuget.org</code> استفاده کنید.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 4: csproj Configuration -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۴</div>
|
||
<div class="section-title">
|
||
<h3>پیکربندی فایل پروژه (.csproj)</h3>
|
||
<div class="subtitle">Configure Project File</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>فایل <code>SampleMcpServer.csproj</code> را باز کنید و مطمئن شوید که <code>TargetFramework</code> روی <code>netstandard2.0</code> تنظیم شده و پکیجها بهدرستی اضافه شدهاند:</p>
|
||
|
||
<div class="file-path">SampleMcpServer.csproj</div>
|
||
<pre data-lang="xml"><code><Project Sdk="Microsoft.NET.Sdk">
|
||
|
||
<PropertyGroup>
|
||
<OutputType>Exe</OutputType>
|
||
<TargetFramework>netstandard2.0</TargetFramework>
|
||
<Nullable>enable</Nullable>
|
||
<LangVersion>latest</LangVersion>
|
||
</PropertyGroup>
|
||
|
||
<ItemGroup>
|
||
<PackageReference Include="ModelContextProtocol" Version="*" />
|
||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="*" />
|
||
</ItemGroup>
|
||
|
||
</Project></code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">توضیح فیلدها:</h4>
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>فیلد</th>
|
||
<th>توضیح</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td><code>OutputType</code></td>
|
||
<td><code>Exe</code> — پروژه یک برنامه اجرایی (console app) خواهد بود</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>TargetFramework</code></td>
|
||
<td><code>netstandard2.0</code> — سازگاری با .NET Framework 4.6.1+ و .NET Core 2.0+</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>Nullable</code></td>
|
||
<td><code>enable</code> — فعالسازی بررسی nullability (امنیت بیشتر)</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>LangVersion</code></td>
|
||
<td><code>latest</code> — استفاده از آخرین امکانات زبان C#</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<div class="warning-box">
|
||
<strong>⚠️ نکته مهم:</strong> اگر از <code>Version="*"</code> استفاده میکنید، NuGet آخرین نسخه پایدار را نصب میکند. در محیط عملیاتی توصیه میشود نسخه دقیق (مانند <code>"0.4.0-preview"</code>) را مشخص کنید تا از تغییرات ناخواسته جلوگیری شود.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 5: Define a Tool -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۵</div>
|
||
<div class="section-title">
|
||
<h3>تعریف اولین Tool (ابزار MCP)</h3>
|
||
<div class="subtitle">Define Your First MCP Tool</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>در ریشه پروژه، فایل جدیدی به نام <code>EchoTool.cs</code> بسازید. در این فایل، یک کلاس ساده با متد <code>Echo</code> تعریف میکنیم که هر پیامی را به مدل بازمیگرداند:</p>
|
||
|
||
<div class="file-path">EchoTool.cs</div>
|
||
<pre data-lang="csharp"><code>using System.ComponentModel;
|
||
using ModelContextProtocol.Server;
|
||
|
||
[McpServerToolType]
|
||
public static class EchoTool
|
||
{
|
||
[McpServerTool, Description("Echoes the message back to the client.")]
|
||
public static string Echo(string message) => $"hello {message}";
|
||
}</code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">توضیح Attributeها و اجزا:</h4>
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>جزء</th>
|
||
<th>نقش</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td><code>[McpServerToolType]</code></td>
|
||
<td>به SDK اعلام میکند که این کلاس شامل Tool است و باید اسکن شود</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>[McpServerTool]</code></td>
|
||
<td>متد را بهعنوان یک Tool قابل فراخوانی توسط LLM ثبت میکند</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>[Description(...)]</code></td>
|
||
<td>توضیح انسانی که به مدل زبانی داده میشود تا بداند این Tool چه کاری انجام میدهد</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>string message</code></td>
|
||
<td>پارامتر ورودی — SDK بهطور خودکار JSON Schema را از این signature تولید میکند</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>string</code> (return type)</td>
|
||
<td>نوع بازگشتی — میتواند <code>string</code>، <code>Task<string></code> یا هر شیء سریالایزپذیر باشد</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<div class="success-box">
|
||
<strong>✓ چرا این سادگی مهم است؟</strong> شما نیازی به نوشتن JSON Schema دستی، یا پیادهسازی هندلرهای پیچیده ندارید. SDK از روی signature متد و Attributeها، بهطور خودکار همه چیز را میسازد. این رویکرد «Attribute-Driven» کار را بسیار ساده میکند.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 6: Program.cs -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۶</div>
|
||
<div class="section-title">
|
||
<h3>پیکربندی و اجرای سرور در Program.cs</h3>
|
||
<div class="subtitle">Configure and Run the Server</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>محتوای فایل <code>Program.cs</code> را با کد زیر جایگزین کنید. این کد، Host را میسازد، لاگها را به stderr هدایت میکند، و سرور را با Transport از نوع stdio راهاندازی مینماید:</p>
|
||
|
||
<div class="file-path">Program.cs</div>
|
||
<pre data-lang="csharp"><code>using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.Hosting;
|
||
using Microsoft.Extensions.Logging;
|
||
using ModelContextProtocol.Server;
|
||
|
||
var builder = Host.CreateApplicationBuilder(args);
|
||
|
||
builder.Logging.AddConsole(consoleLogOptions =>
|
||
{
|
||
// Configure all logs to go to stderr so they don't corrupt the MCP protocol stream.
|
||
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
|
||
});
|
||
|
||
builder.Services
|
||
.AddMcpServer()
|
||
.WithStdioServerTransport()
|
||
.WithToolsFromAssembly();
|
||
|
||
await builder.Build().RunAsync();</code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">تشریح خطبهخط:</h4>
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>خط</th>
|
||
<th>توضیح</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td><code>Host.CreateApplicationBuilder(args)</code></td>
|
||
<td>ساخت Builder مدرن .NET با پشتیبانی از Configuration و DI</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>AddConsole(...LogToStandardErrorThreshold...)</code></td>
|
||
<td><strong>بحرانی:</strong> هدایت لاگها به stderr. اگر لاگها به stdout بروند، با پروتکل MCP تداخل میکنند و ارتباط قطع میشود</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>AddMcpServer()</code></td>
|
||
<td>ثبت سرویسهای MCP در DI Container</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>WithStdioServerTransport()</code></td>
|
||
<td>استفاده از stdin/stdout برای ارتباط — برای کلاینتهای دسکتاپ ایدهآل است</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>WithToolsFromAssembly()</code></td>
|
||
<td>اسکن خودکار اسمبلی و ثبت هر کلاسی که با <code>[McpServerToolType]</code> علامتگذاری شده</td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>await builder.Build().RunAsync()</code></td>
|
||
<td>ساخت Host و شروع به گوش دادن برای درخواستها</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<div class="danger-box">
|
||
<strong>⚠️ هشدار بسیار مهم:</strong> در استفاده از stdio، <strong>هرگز</strong> از <code>Console.WriteLine</code> برای لاگ استفاده نکنید. این کار پیامهای پروتکل را خراب میکند. تمام لاگها باید از طریق <code>ILogger</code> و با تنظیم <code>LogToStandardErrorThreshold</code> به stderr هدایت شوند. اگر این نکته رعایت نشود، سرور بهظاهر اجرا میشود اما کلاینت نمیتواند با آن ارتباط برقرار کند.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 7: Build & Run -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۷</div>
|
||
<div class="section-title">
|
||
<h3>ساخت و اجرا</h3>
|
||
<div class="subtitle">Build & Run</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>در پوشه پروژه، دستور زیر را اجرا کنید:</p>
|
||
|
||
<pre data-lang="bash"><code>dotnet build
|
||
dotnet run</code></pre>
|
||
|
||
<p>سرور شما اجرا خواهد شد و منتظر دریافت پیامها از طریق stdin خواهد بود. در این حالت، سرور در حالت <strong>منفعل (Passive)</strong> است و تنها زمانی که کلاینت MCP به آن متصل شود، شروع به کار خواهد کرد.</p>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">چه اتفاقی در پسزمینه میافتد؟</h4>
|
||
<ol style="padding-right: 20px; margin-top: 10px; font-size: 14px; line-height: 2;">
|
||
<li><strong>SDK</strong> اسمبلی را اسکن کرده و کلاس <code>EchoTool</code> را کشف میکند.</li>
|
||
<li>از روی متد <code>Echo</code>، یک Tool با پارامتر ورودی <code>message: string</code> و نوع بازگشتی <code>string</code> استخراج میکند.</li>
|
||
<li>یک <strong>JSON Schema</strong> خودکار برای این Tool تولید میکند.</li>
|
||
<li>سرور شروع به گوش دادن به stdin میکند.</li>
|
||
<li>وقتی کلاینت متصل میشود، ابتدا یک پیام <code>initialize</code> میفرستد و سرور با معرفی خودش و ابزارهای موجود پاسخ میدهد.</li>
|
||
<li>سپس کلاینت میتواند پیامهای <code>tools/list</code> یا <code>tools/call</code> بفرستد.</li>
|
||
</ol>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 چه چیزی روی stdout میرود؟</strong> فقط پیامهای JSON-RPC پروتکل MCP. هیچ خروجی اضافی مجاز نیست. اگر میخواهید تست کنید، فقط از طریق یک کلاینت MCP اقدام کنید.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 8: Test with Client -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۸</div>
|
||
<div class="section-title">
|
||
<h3>تست با کلاینت MCP</h3>
|
||
<div class="subtitle">Testing with an MCP Client</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>برای تست، میتوانید از <strong>Claude Desktop</strong>، <strong>VS Code Copilot</strong> یا هر MCP Client دیگری استفاده کنید. در ادامه، نمونه پیکربندی برای دو کلاینت پرکاربرد ارائه شده است.</p>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px;">الف) Claude Desktop</h4>
|
||
<p class="no-indent">فایل پیکربندی <code>claude_desktop_config.json</code> را باز کنید (در ویندوز در مسیر <code>%APPDATA%\Claude\</code> و در macOS در <code>~/Library/Application Support/Claude/</code>) و بخش <code>mcpServers</code> را اضافه کنید:</p>
|
||
|
||
<pre data-lang="json"><code>{
|
||
"mcpServers": {
|
||
"sample-echo": {
|
||
"command": "dotnet",
|
||
"args": [
|
||
"run",
|
||
"--project",
|
||
"C:\\Path\\To\\SampleMcpServer\\SampleMcpServer.csproj"
|
||
]
|
||
}
|
||
}
|
||
}</code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 25px;">ب) VS Code (Copilot + MCP)</h4>
|
||
<p class="no-indent">در فایل <code>.vscode/mcp.json</code>:</p>
|
||
|
||
<pre data-lang="json"><code>{
|
||
"servers": {
|
||
"sample-echo": {
|
||
"command": "dotnet",
|
||
"args": [
|
||
"run",
|
||
"--project",
|
||
"${workspaceFolder}/SampleMcpServer/SampleMcpServer.csproj"
|
||
]
|
||
}
|
||
}
|
||
}</code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 25px;">ج) تست دستی با JSON-RPC</h4>
|
||
<p class="no-indent">اگر میخواهید بدون کلاینت تست کنید، میتوانید مستقیماً پیام JSON-RPC بفرستید. در ترمینال سرور را اجرا کنید و سپس پیامهای زیر را روی stdin آن بنویسید:</p>
|
||
|
||
<pre data-lang="json"><code>// درخواست initialize
|
||
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}
|
||
|
||
// دریافت لیست ابزارها
|
||
{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}
|
||
|
||
// فراخوانی ابزار Echo
|
||
{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"Echo","arguments":{"message":"World"}}}</code></pre>
|
||
|
||
<div class="success-box">
|
||
<strong>✓ پاسخ مورد انتظار:</strong> پس از فراخوانی <code>Echo</code>، باید چیزی شبیه به این دریافت کنید:
|
||
<br><br>
|
||
<code>{"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"hello World"}]}}</code>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 9: Advanced Tools -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۹</div>
|
||
<div class="section-title">
|
||
<h3>افزودن Toolهای پیشرفتهتر</h3>
|
||
<div class="subtitle">Advanced Tools</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>Toolهای MCP میتوانند پیچیدهتر از یک Echo ساده باشند. در ادامه، چند الگوی پیشرفته آورده شده است:</p>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px;">الف) Tool با چند پارامتر و نوعهای مختلف</h4>
|
||
<pre data-lang="csharp"><code>[McpServerTool, Description("Adds two numbers and returns the result.")]
|
||
public static int Add(
|
||
[Description("First number")] int a,
|
||
[Description("Second number")] int b
|
||
) => a + b;</code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">ب) Tool با پارامتر اختیاری</h4>
|
||
<pre data-lang="csharp"><code>[McpServerTool, Description("Searches for a term, with optional limit.")]
|
||
public static string Search(
|
||
[Description("The search query")] string query,
|
||
[Description("Maximum results (default: 10)")] int limit = 10
|
||
) => $"Searching for '{query}' with limit {limit}";</code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">ج) Tool Async با CancellationToken</h4>
|
||
<pre data-lang="csharp"><code>[McpServerTool, Description("Fetches data from a remote API.")]
|
||
public static async Task<string> FetchData(
|
||
[Description("The resource identifier")] string id,
|
||
CancellationToken cancellationToken = default
|
||
)
|
||
{
|
||
using var http = new HttpClient();
|
||
return await http.GetStringAsync($"https://api.example.com/{id}", cancellationToken);
|
||
}</code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">د) Tool با Dependency Injection</h4>
|
||
<p class="no-indent">برای استفاده از سرویسهای موجود، میتوانید Tool را بهصورت non-static تعریف کنید و سرویسها را از طریق سازنده تزریق کنید:</p>
|
||
|
||
<pre data-lang="csharp"><code>[McpServerToolType]
|
||
public class DatabaseTool
|
||
{
|
||
private readonly IMyRepository _repository;
|
||
|
||
// DI automatically resolves the dependency
|
||
public DatabaseTool(IMyRepository repository)
|
||
{
|
||
_repository = repository;
|
||
}
|
||
|
||
[McpServerTool, Description("Retrieves a record by ID.")]
|
||
public async Task<string> GetRecord(string id, CancellationToken ct = default)
|
||
{
|
||
var record = await _repository.GetByIdAsync(id, ct);
|
||
return record?.ToJson() ?? "Not found";
|
||
}
|
||
}</code></pre>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 نکته:</strong> برای ثبت سرویسهای DI، پیش از <code>builder.Build()</code> آنها را اضافه کنید:
|
||
<br><br>
|
||
<code>builder.Services.AddScoped<IMyRepository, MyRepository>();</code>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 10: MCPSharp Alternative -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۱۰</div>
|
||
<div class="section-title">
|
||
<h3>روش جایگزین: استفاده از MCPSharp</h3>
|
||
<div class="subtitle">Alternative: MCPSharp Community Library</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>اگر به یک API سادهتر و مختصرتر علاقهمند هستید، میتوانید از کتابخانه جامعه <strong>MCPSharp</strong> استفاده کنید. این کتابخانه نیز از <code>netstandard2.0</code> پشتیبانی میکند و با تعداد خطوط کمتر، همان قابلیتها را ارائه میدهد.</p>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px;">نصب:</h4>
|
||
<pre data-lang="bash"><code>dotnet add package MCPSharp</code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">مثال کامل:</h4>
|
||
<pre data-lang="csharp"><code>using MCPSharp;
|
||
|
||
public class Calculator
|
||
{
|
||
[McpTool("add", "Adds two numbers")]
|
||
public static int Add(
|
||
[McpParameter(true)] int a,
|
||
[McpParameter(true)] int b
|
||
) => a + b;
|
||
|
||
[McpTool("multiply", "Multiplies two numbers")]
|
||
public static int Multiply(
|
||
[McpParameter(true)] int a,
|
||
[McpParameter(true)] int b
|
||
) => a * b;
|
||
}
|
||
|
||
await MCPServer.StartAsync("CalculatorServer", "1.0.0");</code></pre>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">مقایسه دو رویکرد:</h4>
|
||
<table class="styled-table">
|
||
<thead>
|
||
<tr>
|
||
<th>ویژگی</th>
|
||
<th>ModelContextProtocol (رسمی)</th>
|
||
<th>MCPSharp (جامعه)</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>نگهداری</td>
|
||
<td>Microsoft + Anthropic</td>
|
||
<td>جامعه (Community)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>پشتیبانی از .NET Standard 2.0</td>
|
||
<td style="color: var(--success); font-weight: 600;">✓</td>
|
||
<td style="color: var(--success); font-weight: 600;">✓</td>
|
||
</tr>
|
||
<tr>
|
||
<td>میزان کد Boilerplate</td>
|
||
<td>متوسط</td>
|
||
<td style="color: var(--success); font-weight: 600;">حداقل</td>
|
||
</tr>
|
||
<tr>
|
||
<td>پشتیبانی از DI</td>
|
||
<td style="color: var(--success); font-weight: 600;">✓ کامل</td>
|
||
<td>محدود</td>
|
||
</tr>
|
||
<tr>
|
||
<td>Transport HTTP</td>
|
||
<td style="color: var(--success); font-weight: 600;">✓</td>
|
||
<td>در حال توسعه</td>
|
||
</tr>
|
||
<tr class="highlight-row">
|
||
<td><strong>توصیه برای پروژههای عملیاتی</strong></td>
|
||
<td colspan="2" style="text-align: center; color: var(--success); font-weight: 700;">ModelContextProtocol (رسمی)</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<div class="warning-box">
|
||
<strong>⚠️ توصیه:</strong> برای پروژههای عملیاتی و سازمانی، استفاده از کتابخانه رسمی <code>ModelContextProtocol</code> توصیه میشود زیرا پشتیبانی بلندمدت، بهروزرسانیهای امنیتی، و سازگاری با آخرین تغییرات پروتکل MCP را تضمین میکند. MCPSharp برای پروژههای کوچک، یادگیری، یا Prototype مناسب است.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 11: HTTP Transport -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۱۱</div>
|
||
<div class="section-title">
|
||
<h3>گزینه پیشرفته: HTTP Transport</h3>
|
||
<div class="subtitle">Optional: HTTP Transport</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>اگر میخواهید سرور MCP شما از راه دور (Remote) قابل دسترسی باشد (مثلاً در یک کانتینر Docker یا روی یک سرور Cloud)، میتوانید بهجای <code>stdio</code> از <strong>HTTP Transport</strong> استفاده کنید.</p>
|
||
|
||
<div class="info-box">
|
||
<strong>💡 چه زمانی HTTP؟</strong>
|
||
<ul style="padding-right: 20px; margin-top: 10px;">
|
||
<li>چندین کلاینت بهطور همزمان میخواهند وصل شوند</li>
|
||
<li>سرور در محیطی جدا از کلاینت اجرا میشود (Docker، Kubernetes، Cloud)</li>
|
||
<li>نیاز به احراز هویت مرکزی و Rate Limiting دارید</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">تغییرات لازم در Program.cs:</h4>
|
||
<pre data-lang="csharp"><code>using Microsoft.AspNetCore.Builder;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.Hosting;
|
||
using ModelContextProtocol.Server;
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
builder.Services
|
||
.AddMcpServer()
|
||
.WithHttpTransport() // ← بهجای WithStdioServerTransport
|
||
.WithToolsFromAssembly();
|
||
|
||
var app = builder.Build();
|
||
|
||
app.MapMcp(); // ← مسیر /mcp را فعال میکند
|
||
|
||
app.Run();</code></pre>
|
||
|
||
<p class="no-indent">همچنین باید پکیج <code>Microsoft.AspNetCore</code> را اضافه کنید و نوع پروژه را از <code>Microsoft.NET.Sdk</code> به <code>Microsoft.NET.Sdk.Web</code> تغییر دهید.</p>
|
||
|
||
<div class="warning-box">
|
||
<strong>⚠️ توجه:</strong> HTTP Transport در .NET Standard 2.0 بهطور کامل پشتیبانی نمیشود. برای استفاده از این قابلیت، بهتر است پروژه را به <code>net8.0</code> یا بالاتر ارتقا دهید. اگر مجبور به استفاده از .NET Standard 2.0 هستید، از stdio استفاده کنید و یک پروسه فرزند بسازید.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 12: Best Practices -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۱۲</div>
|
||
<div class="section-title">
|
||
<h3>بهترین روشها و نکات عملیاتی</h3>
|
||
<div class="subtitle">Best Practices</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<ul class="checklist">
|
||
<li>هر Tool را با <code>[Description]</code> واضح و دقیق توصیف کنید — مدل از این توضیح برای تصمیمگیری استفاده میکند</li>
|
||
<li>پارامترها را با نوعهای مشخص (نه <code>object</code>) تعریف کنید تا JSON Schema دقیقتری تولید شود</li>
|
||
<li>برای عملیات طولانی، از <code>Task</code> و <code>CancellationToken</code> استفاده کنید</li>
|
||
<li>خطاها را با Exceptionهای واضح پرتاب کنید — SDK آنها را به JSON-RPC Error تبدیل میکند</li>
|
||
<li>ورودیها را اعتبارسنجی کنید (Validation) — مدل ممکن است ورودی نامعتبر بفرستد</li>
|
||
<li>لاگها را هرگز روی stdout ننویسید — از <code>ILogger</code> و stderr استفاده کنید</li>
|
||
<li>از <code>[McpServerToolType]</code> روی کلاسهای Helper استفاده کنید تا ابزارها در فایلهای مختلف تقسیم شوند</li>
|
||
<li>در محیط عملیاتی، Transport را بر اساس سناریو انتخاب کنید (stdio برای دسکتاپ، HTTP برای Cloud)</li>
|
||
<li>ورژن پروتکل MCP را در نظر بگیرید — کلاینت و سرور باید سازگار باشند</li>
|
||
<li>برای تست خودکار، از JSON-RPC خام در سناریوهای Integration Test استفاده کنید</li>
|
||
</ul>
|
||
|
||
<h4 style="color: var(--primary); font-size: 15px; margin-top: 25px;">الگوی پیشنهادی ساختار پروژه:</h4>
|
||
<div class="file-tree">
|
||
<span class="folder">SampleMcpServer/</span>
|
||
├── <span class="folder">Tools/</span>
|
||
│ ├── EchoTool.cs
|
||
│ ├── CalculatorTool.cs
|
||
│ └── DatabaseTool.cs
|
||
├── <span class="folder">Services/</span>
|
||
│ ├── IMyRepository.cs
|
||
│ └── MyRepository.cs
|
||
├── <span class="folder">Models/</span>
|
||
│ └── RecordDto.cs
|
||
├── Program.cs
|
||
├── appsettings.json
|
||
└── SampleMcpServer.csproj
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ============================================================= -->
|
||
<!-- SECTION 13: Summary -->
|
||
<!-- ============================================================= -->
|
||
<div class="section">
|
||
<div class="section-header">
|
||
<div class="section-number">۱۳</div>
|
||
<div class="section-title">
|
||
<h3>جمعبندی</h3>
|
||
<div class="subtitle">Summary</div>
|
||
</div>
|
||
</div>
|
||
<div class="section-content">
|
||
<p>در این مستند، بهصورت گامبهگام، ساخت یک MCP Server با C# و .NET Standard 2.0 را بررسی کردیم. مراحلی که پشت سر گذاشتیم:</p>
|
||
|
||
<ul class="checklist">
|
||
<li>با مفهوم MCP و سه قابلیت اصلی آن (Tools، Resources، Prompts) آشنا شدیم</li>
|
||
<li>پیشنیازها و محیط توسعه را آماده کردیم</li>
|
||
<li>یک پروژه Console جدید با <code>dotnet new console</code> ساختیم</li>
|
||
<li>پکیجهای <code>ModelContextProtocol</code> و <code>Microsoft.Extensions.Hosting</code> را نصب کردیم</li>
|
||
<li>فایل <code>.csproj</code> را برای <code>netstandard2.0</code> پیکربندی کردیم</li>
|
||
<li>اولین Tool خود را با <code>[McpServerTool]</code> تعریف کردیم</li>
|
||
<li>سرور را در <code>Program.cs</code> با stdio Transport راهاندازی کردیم</li>
|
||
<li>نحوه تست با Claude Desktop و VS Code را فرا گرفتیم</li>
|
||
<li>الگوهای پیشرفتهتر (Async، DI، پارامترهای اختیاری) را بررسی کردیم</li>
|
||
<li>با کتابخانه جایگزین MCPSharp آشنا شدیم</li>
|
||
<li>روش استفاده از HTTP Transport برای سناریوهای Cloud را دیدیم</li>
|
||
<li>بهترین روشهای عملیاتی را مرور کردیم</li>
|
||
</ul>
|
||
|
||
<div class="conclusion-box">
|
||
<h3>🎯 نتیجه نهایی</h3>
|
||
<p>شما اکنون یک MCP Server کامل و عملیاتی در اختیار دارید که:</p>
|
||
<p style="font-size: 13px; line-height: 2;">
|
||
<strong>با استاندارد باز MCP سازگار است، از .NET Standard 2.0 پشتیبانی میکند، بهراحتی با Claude Desktop و GitHub Copilot یکپارچه میشود، و منطق تجاری موجود شما را در دسترس مدلهای زبانی قرار میدهد.</strong>
|
||
</p>
|
||
<div class="highlight">
|
||
<strong>💎 ارزش پیشنهادی:</strong><br>
|
||
یکپارچگی استاندارد با LLM + بدون تغییر ساختار موجود پروژه + قابلیت حمل بالا + سادگی توسعه
|
||
</div>
|
||
</div>
|
||
|
||
<div class="info-box">
|
||
<strong>📞 گام بعدی پیشنهادی:</strong> پس از ساخت این سرور ساده، میتوانید:
|
||
<ul style="padding-right: 20px; margin-top: 10px;">
|
||
<li>سرویسهای موجود خود را بهعنوان Toolهای MCP ثبت کنید (مانند RAG، مدیریت مکالمه، پردازش فایل)</li>
|
||
<li>سرور را در CI/CD قرار دهید و بهصورت خودکار منتشر کنید</li>
|
||
<li>با یک MCP Client متصل شوید و تست End-to-End انجام دهید</li>
|
||
<li>در صورت نیاز، به سمت HTTP Transport و استقرار در کانتینر حرکت کنید</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="page-info">
|
||
این مستند آموزشی بوده و برای تیمهای فنی و توسعهدهندگان تهیه شده است. استفاده از آن در پروژههای داخلی و تجاری مجاز است.
|
||
</div>
|
||
|
||
</div>
|
||
|
||
<footer class="document-footer">
|
||
<div class="footer-brand">تهیه و تنظیم: تیم معماری xSaherelmWorkspace</div>
|
||
<div class="footer-divider"></div>
|
||
<div>تاریخ تهیه: آبان ۱۴۰۵ | نسخه: ۱.۰ | طبقهبندی: آموزشی-فنی</div>
|
||
</footer>
|
||
|
||
</div>
|
||
|
||
</body>
|
||
</html> |