Files
xSaherelmWorkspace/Documents/Docs/Developer/MCP Server HttpTransport 7.html
T

1069 lines
42 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>تحلیل و رفع مشکل اجرای پروژه xAiMcpServer</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--danger: #e53e3e;
--warning: #dd6b20;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 950px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
position: relative;
z-index: 1;
}
.company-badge { display: flex; align-items: center; gap: 12px; }
.company-logo {
width: 60px; height: 60px;
background: var(--accent);
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.doc-meta { text-align: left; font-size: 12px; opacity: 0.9; }
.doc-meta div { margin-bottom: 3px; }
.document-title { text-align: center; position: relative; z-index: 1; padding: 20px 0; }
.document-title .label {
display: inline-block;
background: rgba(201,169,97,0.2);
color: var(--accent);
padding: 4px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-bottom: 15px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-title h1 {
font-size: 28px;
font-weight: 800;
margin-bottom: 10px;
letter-spacing: -0.5px;
}
.document-title h2 { font-size: 16px; font-weight: 400; opacity: 0.9; }
.document-body { padding: 40px; }
.intro-text {
background: linear-gradient(to left, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin-bottom: 35px;
font-size: 14px;
color: var(--text-muted);
}
.section { margin-bottom: 40px; page-break-inside: avoid; }
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 20px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
}
.section-title { flex: 1; }
.section-title h3 {
font-size: 19px;
font-weight: 700;
color: var(--primary);
margin-bottom: 2px;
}
.section-title .subtitle { font-size: 12px; color: var(--text-muted); }
.section-content { padding-right: 10px; }
.section-content p { margin-bottom: 12px; text-align: justify; text-indent: 20px; }
.section-content p.no-indent { text-indent: 0; }
.highlight-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #f6e05e;
border-right: 4px solid var(--accent);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.highlight-box strong { color: var(--accent-dark); }
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.danger-box {
background: linear-gradient(to left, #fff5f5, #fed7d7);
border: 1px solid #feb2b2;
border-right: 4px solid var(--danger);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.warning-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #fbd38d;
border-right: 4px solid var(--warning);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.styled-table thead {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
}
.styled-table th { padding: 14px 16px; text-align: right; font-weight: 600; font-size: 13px; }
.styled-table td { padding: 13px 16px; border-bottom: 1px solid var(--border); font-size: 13px; }
.styled-table tbody tr:last-child td { border-bottom: none; }
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
.styled-table .highlight-row { background: #fffaf0 !important; font-weight: 600; }
.comparison-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 25px 0;
}
.comparison-card {
background: #f8fafc;
border: 2px solid var(--border);
border-radius: 10px;
padding: 20px;
position: relative;
}
.comparison-card.recommended {
border-color: var(--success);
background: linear-gradient(to left, #f0fff4, #e6fffa);
}
.comparison-card .card-badge {
position: absolute;
top: -10px;
right: 20px;
background: var(--success);
color: white;
padding: 3px 12px;
border-radius: 15px;
font-size: 11px;
font-weight: 600;
}
.comparison-card h4 {
font-size: 16px;
font-weight: 700;
color: var(--primary);
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 2px solid var(--border);
}
.comparison-card.recommended h4 { border-bottom-color: var(--success); }
.comparison-card ul { list-style: none; padding: 0; }
.comparison-card ul li {
padding: 8px 0;
border-bottom: 1px solid var(--border);
font-size: 13px;
display: flex;
justify-content: space-between;
}
.comparison-card ul li:last-child { border-bottom: none; }
.comparison-card ul li .value { font-weight: 600; color: var(--primary); }
.stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
margin: 25px 0;
}
.stat-card {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
box-shadow: 0 4px 12px rgba(26,54,93,0.2);
}
.stat-card .stat-value {
font-size: 24px;
font-weight: 800;
margin-bottom: 5px;
color: var(--accent);
line-height: 1.3;
}
.stat-card .stat-label { font-size: 12px; opacity: 0.9; }
.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);
}
.code-block {
background: #1a202c;
color: #e2e8f0;
padding: 20px;
border-radius: 8px;
font-family: 'Consolas', 'Courier New', monospace;
font-size: 12.5px;
line-height: 1.7;
margin: 15px 0;
overflow-x: auto;
direction: ltr;
text-align: left;
border-right: 4px solid var(--accent);
}
.code-block .comment { color: #68d391; }
.code-block .keyword { color: #f6ad55; }
.code-block .type { color: #90cdf4; }
.code-block .string { color: #fbb6ce; }
.code-header {
background: #2d3748;
color: #a0aec0;
padding: 8px 20px;
border-radius: 8px 8px 0 0;
font-family: 'Consolas', monospace;
font-size: 12px;
margin-top: 15px;
direction: ltr;
text-align: left;
}
.code-header + .code-block {
margin-top: 0;
border-radius: 0 0 8px 8px;
}
.checklist { list-style: none; padding: 0; margin: 20px 0; }
.checklist li {
background: #f8fafc;
border-right: 3px solid var(--primary-light);
padding: 15px 20px;
margin-bottom: 10px;
border-radius: 6px;
font-size: 13px;
}
.checklist li strong { color: var(--primary); display: block; margin-bottom: 5px; }
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.document-footer .footer-brand {
font-weight: 700;
color: var(--accent);
margin-bottom: 5px;
font-size: 14px;
}
.footer-divider { width: 60px; height: 2px; background: var(--accent); margin: 10px auto; }
.page-info {
text-align: center;
padding: 15px;
font-size: 11px;
color: var(--text-muted);
border-top: 1px solid var(--border);
margin-top: 30px;
}
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; border-radius: 0; }
.section { page-break-inside: avoid; }
}
@media (max-width: 768px) {
.document-body { padding: 25px 20px; }
.document-header { padding: 30px 20px; }
.comparison-grid { grid-template-columns: 1fr; }
.stats-grid { grid-template-columns: 1fr; }
.header-top { flex-direction: column; gap: 15px; }
.doc-meta { text-align: center; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="company-badge">
<div class="company-logo"></div>
</div>
<div class="doc-meta">
<div>شماره مستند: TECH-1405-002</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: آبان ۱۴۰۵</div>
<div>طبقه‌بندی: فنی</div>
</div>
</div>
<div class="document-title">
<span class="label">TECHNICAL DIAGNOSIS & SOLUTION</span>
<h1>تحلیل و رفع مشکل اجرای xAiMcpServer</h1>
<h2>ریشه‌یابی خطا + راه‌حل کامل + کنترلرها + راه‌های تضمین اجرا</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>همکار گرامی،</strong><br>
پروژه <code>xAiMcpServer</code> به دلیل <strong>ترکیب نادرست دو فریم‌ورک متفاوت (Grapevine و ASP.NET Core)</strong> در فایل <code>Program.cs</code> و <code>Startup.cs</code> اجرا نمی‌شود. در این مستند، ابتدا ریشه مشکل شناسایی می‌شود، سپس کد صحیح بازنویسی می‌گردد و در نهایت راه‌های جامع برای تضمین اجرای صحیح و تست آن ارائه می‌شود.
</div>
<!-- Section 1 -->
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>خلاصه اجرایی</h3>
<div class="subtitle">Executive Summary</div>
</div>
</div>
<div class="section-content">
<p>پروژه <code>xAiMcpServer</code> در واقع یک <strong>ASP.NET Core Web API</strong> است (به دلیل استفاده از <code>XBaseIdentityApiV1Controller</code>، DI Container، Swagger و ...). اما فایل <code>Program.cs</code> از <strong>Grapevine</strong> (یک کتابخانه HTTP سبک و کاملاً متفاوت) برای اجرا استفاده می‌کند. نتیجه این است که:</p>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value">✓</div>
<div class="stat-label">Startup ثبت می‌شود ولی Configure اجرا نمی‌شود</div>
</div>
<div class="stat-card">
<div class="stat-value">✗</div>
<div class="stat-label">کنترلرها هیچ‌گاه Map نمی‌شوند</div>
</div>
<div class="stat-card">
<div class="stat-value">✗</div>
<div class="stat-label">MCP SDK Endpoint فعال نمی‌شود</div>
</div>
</div>
<div class="danger-box">
<strong>⚠️ علت اصلی:</strong> متد <code>RestServerBuilder.From&lt;Startup&gt;()</code> در Grapevine، فقط <code>ConfigureServer(IRestServer)</code> را فراخوانی می‌کند و <strong>هرگز <code>Configure(IApplicationBuilder, IWebHostEnvironment)</code> را صدا نمی‌زند</strong>. چون تمام Routing، Controllers، Authentication و MCP در متد <code>Configure</code> تعریف شده‌اند، هیچ‌کدام از آن‌ها اجرا نمی‌شوند.
</div>
</div>
</div>
<!-- Section 2 -->
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>ریشه‌یابی دقیق مشکل</h3>
<div class="subtitle">Root Cause Analysis</div>
</div>
</div>
<div class="section-content">
<p>مشکلات موجود را می‌توان به ۵ دسته تقسیم کرد:</p>
<table class="styled-table">
<thead>
<tr>
<th>#</th>
<th>مشکل</th>
<th>فایل</th>
<th>اثر</th>
</tr>
</thead>
<tbody>
<tr>
<td>۱</td>
<td>استفاده از Grapevine در <code>Program.cs</code> به جای ASP.NET Core Host</td>
<td><code>Program.cs</code></td>
<td style="color: var(--danger); font-weight: 600;">کشنده — هیچ کنترلی اجرا نمی‌شود</td>
</tr>
<tr>
<td>۲</td>
<td>متد <code>Configure</code> هرگز توسط Grapevine فراخوانی نمی‌شود</td>
<td><code>Startup.cs</code></td>
<td style="color: var(--danger); font-weight: 600;">Middleware Pipeline اجرا نمی‌شود</td>
</tr>
<tr>
<td>۳</td>
<td>ترکیب دو سیستم MCP هم‌زمان: (الف) <code>McpController</code> با Route mcp و (ب) <code>.AddMcpServer().WithHttpTransport()</code> از SDK رسمی</td>
<td><code>Startup.cs</code> + <code>McpController.cs</code></td>
<td style="color: var(--warning); font-weight: 600;">تداخل و ابهام در Endpoint</td>
</tr>
<tr>
<td>۴</td>
<td>استفاده از <code>IHostBuilder</code> (Console Host) به جای <code>WebApplication</code> یا <code>ConfigureWebHostDefaults</code></td>
<td><code>Program.cs</code></td>
<td style="color: var(--danger); font-weight: 600;">Web Host راه‌اندازی نمی‌شود</td>
</tr>
<tr>
<td>۵</td>
<td>عدم ثبت صریح Endpoint برای <code>MapMcp()</code> در صورت استفاده از SDK رسمی</td>
<td><code>Startup.cs</code></td>
<td style="color: var(--warning); font-weight: 600;">MCP Tools در دسترس نیستند</td>
</tr>
</tbody>
</table>
<div class="info-box">
<strong>💡 نکته کلیدی:</strong> پروژه شما در واقع یک <strong>ASP.NET Core Web API</strong> است که می‌خواهد روی <code>https://localhost:7001/mcp</code> هم به عنوان یک MCP Server عمل کند. برای این کار باید یا از SDK رسمی <code>ModelContextProtocol</code> استفاده کنید، یا کنترلر سفارشی <code>XAiMcpControllerBase</code> را نگه دارید. استفاده از هر دو در یک پروژه باعث تداخل می‌شود.
</div>
</div>
</div>
<!-- Section 3 -->
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>راه‌حل ۱: بازنویسی Program.cs با ASP.NET Core</h3>
<div class="subtitle">Fix Program.cs - Use WebApplication</div>
</div>
</div>
<div class="section-content">
<p class="no-indent">فایل <code>Program.cs</code> فعلی با Grapevine کار می‌کند که باعث می‌شود <code>Startup.Configure</code> هرگز اجرا نشود. نسخه صحیح با <code>WebApplication</code> به شکل زیر است:</p>
<div class="code-header">Program.cs — نسخه صحیح</div>
<div class="code-block"><pre><span class="keyword">using</span> System.Text;
<span class="keyword">using</span> Microsoft.AspNetCore.Builder;
<span class="keyword">using</span> Microsoft.Extensions.Hosting;
<span class="keyword">using</span> xAiMcpServer;
<span class="keyword">var</span> builder = WebApplication.CreateBuilder(args);
<span class="comment">// Register Encoding Provider</span>
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
<span class="comment">// Use Startup pattern for compatibility with current project</span>
builder.Host
.UseDefaultServiceProvider(options =&gt;
{
options.ValidateScopes = builder.Environment.IsDevelopment();
options.ValidateOnBuild = true;
});
<span class="keyword">var</span> startup = <span class="keyword">new</span> Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);
<span class="keyword">var</span> app = builder.Build();
startup.Configure(app, app.Environment);
app.Run();</pre></div>
<div class="success-box">
<strong>✓ چرا این کد درست است؟</strong>
<ul style="padding-right: 20px; margin-top: 8px;">
<li><code>WebApplication.CreateBuilder</code> یک Web Host کامل می‌سازد.</li>
<li>متد <code>Configure</code> از Startup به‌درستی فراخوانی می‌شود.</li>
<li>Swagger، Controllers، Authentication و MCP همگی فعال می‌شوند.</li>
<li>پورت از <code>launchSettings.json</code> یا <code>appsettings.json</code> خوانده می‌شود (پیش‌فرض 7001 در نظر گرفته شده).</li>
</ul>
</div>
</div>
</div>
<!-- Section 4 -->
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>راه‌حل ۲: اصلاح Startup.cs برای اجرای Endpointها</h3>
<div class="subtitle">Fix Startup.cs - Map Controllers & MCP</div>
</div>
</div>
<div class="section-content">
<p class="no-indent">مشکل دیگر این است که در <code>Startup.cs</code> فعلی، MCP SDK ثبت شده ولی Endpoint آن Map نشده است. همچنین باید تصمیم بگیرید از کدام سیستم MCP استفاده می‌کنید: <strong>کنترلر سفارشی</strong> یا <strong>SDK رسمی</strong>.</p>
<div class="comparison-grid">
<div class="comparison-card">
<h4>🔵 گزینه A: SDK رسمی (ModelContextProtocol)</h4>
<ul>
<li><span>پکیج اصلی</span> <span class="value">ModelContextProtocol.AspNetCore</span></li>
<li><span>کلاس‌ها</span> <span class="value">XEchoTools</span></li>
<li><span>Endpoint</span> <span class="value">MapMcp()</span></li>
<li><span>Discovery</span> <span class="value">خودکار</span></li>
</ul>
</div>
<div class="comparison-card recommended">
<span class="card-badge">پیشنهاد می‌شود ✓</span>
<h4>🟢 گزینه B: کنترلر سفارشی (XAiMcpHandler)</h4>
<ul>
<li><span>پکیج اصلی</span> <span class="value">Grapevine.Extensions.Mcp</span></li>
<li><span>کلاس‌ها</span> <span class="value">McpController</span></li>
<li><span>Endpoint</span> <span class="value">MapControllers()</span></li>
<li><span>Discovery</span> <span class="value">دستی از XAiMcpHandler</span></li>
</ul>
</div>
</div>
<div class="warning-box">
<strong>⚠️ نکته مهم:</strong> انتخاب یک مسیر کافی است. اگر هر دو را هم‌زمان فعال کنید، هر دو روی <code>/mcp</code> سوار می‌شوند و تداخل می‌کنند.
</div>
<p class="no-indent" style="margin-top: 20px;"><strong>گزینه B (کنترلر سفارشی) را انتخاب می‌کنیم</strong> چون ساختار موجود پروژه (XAiMcpControllerBase) به همین شکل نوشته شده است:</p>
<div class="code-header">Startup.cs — نسخه اصلاح‌شده</div>
<div class="code-block"><pre><span class="keyword">using</span> System;
<span class="keyword">using</span> System.IO;
<span class="keyword">using</span> System.Reflection;
<span class="keyword">using</span> xAiMcpServer.DI;
<span class="keyword">using</span> Newtonsoft.Json;
<span class="keyword">using</span> xIdentityService.DI;
<span class="keyword">using</span> xCommons.Extensions;
<span class="keyword">using</span> xAiService.Extensions;
<span class="keyword">using</span> xCommons.Configurations;
<span class="keyword">using</span> Microsoft.AspNetCore.Builder;
<span class="keyword">using</span> Microsoft.AspNetCore.Hosting;
<span class="keyword">using</span> Microsoft.Extensions.Hosting;
<span class="keyword">using</span> Newtonsoft.Json.Serialization;
<span class="keyword">using</span> Microsoft.Extensions.Configuration;
<span class="keyword">using</span> Microsoft.Extensions.DependencyInjection;
<span class="keyword">using</span> IdentityModel.AspNetCore.OAuth2Introspection;
<span class="keyword">namespace</span> xAiMcpServer
{
<span class="keyword">public class</span> Startup
{
<span class="keyword">public</span> Startup(IConfiguration configuration)
{
Configuration = configuration;
}
<span class="keyword">public</span> IConfiguration Configuration { <span class="keyword">get</span>; }
<span class="keyword">public void</span> ConfigureServices(IServiceCollection services)
{
<span class="comment">// 1) MCP Handler (کنترلر سفارشی)</span>
services.AddXAiMcpHandler();
<span class="comment">// 2) XCommons (Validation, AppConfig, ...)</span>
services.AddXCommons(Configuration);
services.AddXAppConfiguration(Configuration);
<span class="keyword">var</span> appConfiguration = services.GetRegisteredService&lt;XAppConfiguration&gt;();
<span class="comment">// 3) CORS</span>
services.AddXCors(appConfiguration.AllowedOrigins);
<span class="comment">// 4) Swagger</span>
<span class="keyword">var</span> xmlFile = $<span class="string">"{Assembly.GetExecutingAssembly().GetName().Name}.xml"</span>;
<span class="keyword">var</span> xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
services.AddXSwagger(Configuration, xmlFilePath: xmlPath);
<span class="comment">// 5) API Versioning</span>
services.AddXApiV1Versioning();
<span class="comment">// 6) OAuth Introspection HttpClient (SSL bypass for dev)</span>
services.AddHttpClient(OAuth2IntrospectionDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() =&gt; <span class="keyword">new</span> HttpClientHandler
{
ServerCertificateCustomValidationCallback =
(sender, certificate, chain, sslPolicyErrors) =&gt; <span class="keyword">true</span>
});
<span class="comment">// 7) Authorization</span>
services.AddXAuthorization();
<span class="comment">// 8) MVC / Controllers</span>
services.AddControllers()
.AddNewtonsoftJson(x =&gt;
{
x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
x.SerializerSettings.ContractResolver = <span class="keyword">new</span> CamelCasePropertyNamesContractResolver();
});
<span class="comment">// 9) Identity</span>
services.AddXIdentityService(
lifeTime: ServiceLifetime.Scoped,
configuration: Configuration);
}
<span class="keyword">public void</span> Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
<span class="keyword">var</span> isDev = env.IsDevelopment();
<span class="keyword">if</span> (isDev) app.UseDeveloperExceptionPage();
<span class="keyword">else</span> app.UseHsts();
app.UseHttpsRedirection();
<span class="keyword">if</span> (isDev) app.UseXSwagger();
app.UseXCors();
app.UseRouting();
app.UseXIdentityService();
app.UseAuthorization();
app.UseEndpoints(endpoints =&gt;
{
<span class="comment">// ✅ این خط کنترلرها (از جمله McpController و StartupController) را Map می‌کند</span>
endpoints.MapControllers();
});
}
}
}</pre></div>
<div class="success-box">
<strong>✓ تفاوت‌های کلیدی با نسخه قبلی:</strong>
<ul style="padding-right: 20px; margin-top: 8px;">
<li>حذف <code>.AddMcpServer().WithHttpTransport().WithToolsFromAssembly()</code> برای جلوگیری از تداخل با کنترلر سفارشی.</li>
<li>حذف متد <code>ConfigureServer(IRestServer)</code> که مخصوص Grapevine بود.</li>
<li>حفظ کامل <code>Configure</code> برای ASP.NET Core.</li>
<li>استفاده فقط از <code>MapControllers()</code>.</li>
</ul>
</div>
</div>
</div>
<!-- Section 5 -->
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>بررسی صحت ثبت کنترلرها</h3>
<div class="subtitle">Verify Controllers Registration</div>
</div>
</div>
<div class="section-content">
<p class="no-indent">با تنظیمات بالا، سه کنترلر در پروژه وجود دارند:</p>
<table class="styled-table">
<thead>
<tr>
<th>کنترلر</th>
<th>Base Class</th>
<th>Route</th>
<th>هدف</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>McpController</code></td>
<td><code>XAiMcpControllerBase</code></td>
<td><code>/mcp</code></td>
<td>دریافت درخواست‌های JSON-RPC از MCP Client</td>
</tr>
<tr>
<td><code>StartupController</code></td>
<td><code>XBaseController</code></td>
<td><code>/</code></td>
<td>Welcome Message + Health</td>
</tr>
</tbody>
</table>
<div class="highlight-box">
<strong>📌 نکته:</strong> کنترلر <code>StartupController</code> در حال حاضر در Namespace <code>xAiApi.Controllers</code> تعریف شده است (به اشتباه)، در حالی که فایل آن در پروژه <code>xAiMcpServer</code> است. بهتر است Namespace آن به <code>xAiMcpServer.Controllers</code> تغییر کند تا Map خودکار کنترلرها به مشکل نخورد.
</div>
<div class="code-header">رفع Namespace در StartupController.cs</div>
<div class="code-block"><pre><span class="comment">// فایل: xAiMcpServer\Controllers\StartupController.cs</span>
<span class="keyword">namespace</span> xAiMcpServer.Controllers <span class="comment">// ← اصلاح شود</span>
{
<span class="comment">// ...</span>
}</pre></div>
</div>
</div>
<!-- Section 6 -->
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>تضمین اجرای صحیح — چک‌لیست جامع</h3>
<div class="subtitle">Verification & Testing Strategy</div>
</div>
</div>
<div class="section-content">
<p class="no-indent">پس از اعمال تغییرات بالا، برای اطمینان از اجرای صحیح، مراحل زیر را طی کنید:</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">مرحله ۱: بررسی Build و اجرا</h4>
<div class="code-header">PowerShell / Terminal</div>
<div class="code-block"><pre>dotnet build xAiMcpServer.csproj
dotnet run --project xAiMcpServer.csproj --urls "https://localhost:7001"
<span class="comment"># اگر خطا داشت، این دستور را اجرا کنید:</span>
dotnet run --project xAiMcpServer.csproj --urls "https://localhost:7001" --launch-profile "https"</pre></div>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">مرحله ۲: تست Endpointهای اصلی</h4>
<div class="code-header">PowerShell / Terminal</div>
<div class="code-block"><pre><span class="comment"># 1) تست Startup (Welcome Message) — باید 200 برگرداند</span>
curl -k https://localhost:7001/
<span class="comment"># 2) تست Swagger — باید HTML صفحه Swagger UI برگرداند</span>
curl -k https://localhost:7001/swagger/index.html
<span class="comment"># 3) تست Endpoint سلامت</span>
curl -k https://localhost:7001/health
<span class="comment"># 4) تست MCP initialize (JSON-RPC 2.0)</span>
curl -k -X POST https://localhost:7001/mcp `
-H "Content-Type: application/json" `
-d '{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}'
<span class="comment"># 5) تست MCP tools/list</span>
curl -k -X POST https://localhost:7001/mcp `
-H "Content-Type: application/json" `
-d '{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/list\"}'
<span class="comment"># 6) تست MCP tools/call</span>
curl -k -X POST https://localhost:7001/mcp `
-H "Content-Type: application/json" `
-d '{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"tools/call\",\"params\":{\"name\":\"search_knowledge\",\"arguments\":{\"query\":\"test\"}}}'</pre></div>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">مرحله ۳: استفاده از MCP Inspector (توصیه می‌شود)</h4>
<p class="no-indent">ابزار رسمی MCP Inspector برای تست تعاملی سرور:</p>
<div class="code-header">Terminal</div>
<div class="code-block"><pre>npx @modelcontextprotocol/inspector
<span class="comment"># سپس در مرورگر باز کنید:</span>
<span class="comment"># http://localhost:6274</span>
<span class="comment"># در بخش Transport: HTTP (یا SSE)</span>
<span class="comment"># URL: https://localhost:7001/mcp</span>
<span class="comment"># روی Connect کلیک کنید → باید tools را ببینید</span></pre></div>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">مرحله ۴: اضافه کردن Health Check Endpoint</h4>
<p class="no-indent">یک Endpoint ساده برای بررسی سلامت و اجرای صحیح:</p>
<div class="code-header">HealthController.cs (جدید)</div>
<div class="code-block"><pre><span class="keyword">using</span> Microsoft.AspNetCore.Authorization;
<span class="keyword">using</span> Microsoft.AspNetCore.Mvc;
<span class="keyword">namespace</span> xAiMcpServer.Controllers
{
[ApiController]
[Route(<span class="string">"health"</span>)]
[AllowAnonymous]
<span class="keyword">public class</span> HealthController : ControllerBase
{
[HttpGet]
<span class="keyword">public</span> IActionResult Get()
{
<span class="keyword">return</span> Ok(<span class="keyword">new</span>
{
status = <span class="string">"Healthy"</span>,
service = <span class="string">"xAiMcpServer"</span>,
timestamp = DateTime.UtcNow,
environment = Environment.GetEnvironmentVariable(<span class="string">"ASPNETCORE_ENVIRONMENT"</span>)
?? <span class="string">"Production"</span>,
version = <span class="keyword">typeof</span>(Program).Assembly.GetName().Version?.ToString()
});
}
}
}</pre></div>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">مرحله ۵: لاگ‌گیری ساختاریافته</h4>
<p class="no-indent">در <code>Program.cs</code> بعد از <code>builder.Build()</code> اضافه کنید:</p>
<div class="code-header">Program.cs — افزودن لاگ راه‌اندازی</div>
<div class="code-block"><pre><span class="keyword">var</span> logger = app.Services.GetRequiredService&lt;ILogger&lt;Program&gt;&gt;();
logger.LogInformation(<span class="string">"🚀 xAiMcpServer started on {Urls}"</span>,
app.Urls.Count &gt; 0 ? <span class="keyword">string</span>.Join(<span class="string">", "</span>, app.Urls) : <span class="string">"configured endpoints"</span>);
logger.LogInformation(<span class="string">"📡 MCP endpoint: /mcp"</span>);
logger.LogInformation(<span class="string">"📖 Swagger UI: /swagger"</span>);
logger.LogInformation(<span class="string">"❤️ Health: /health"</span>);</pre></div>
</div>
</div>
<!-- Section 7 -->
<div class="section">
<div class="section-header">
<div class="section-number">۷</div>
<div class="section-title">
<h3>چک‌لیست نهایی تضمین اجرا</h3>
<div class="subtitle">Final Verification Checklist</div>
</div>
</div>
<div class="section-content">
<ul class="checklist">
<li>
<strong>☐ ۱. Program.cs جایگزین شد</strong>
از <code>WebApplication.CreateBuilder</code> استفاده می‌شود، نه Grapevine.
</li>
<li>
<strong>☐ ۲. Startup.cs اصلاح شد</strong>
متد <code>ConfigureServer</code> حذف و <code>MapControllers()</code> موجود است.
</li>
<li>
<strong>☐ ۳. Namespace StartupController اصلاح شد</strong>
از <code>xAiApi.Controllers</code> به <code>xAiMcpServer.Controllers</code> تغییر یافت.
</li>
<li>
<strong>☐ ۴. تنها یک سیستم MCP فعال است</strong>
یا SDK رسمی (<code>MapMcp</code>) یا کنترلر سفارشی — نه هر دو.
</li>
<li>
<strong>☐ ۵. Endpoint سلامت اضافه شد</strong>
<code>GET /health</code> پاسخ 200 می‌دهد.
</li>
<li>
<strong>☐ ۶. Swagger باز می‌شود</strong>
<code>https://localhost:7001/swagger</code> لیست کنترلرها را نشان می‌دهد.
</li>
<li>
<strong>☐ ۷. تست initialize موفق است</strong>
<code>POST /mcp</code> با <code>method:"initialize"</code> پاسخ JSON-RPC معتبر می‌دهد.
</li>
<li>
<strong>☐ ۸. تست tools/list موفق است</strong>
حداقل یک Tool (<code>search_knowledge</code>) برگردانده می‌شود.
</li>
<li>
<strong>☐ ۹. MCP Inspector متصل می‌شود</strong>
Inspector tools را می‌بیند و می‌تواند فراخوانی کند.
</li>
<li>
<strong>☐ ۱۰. لاگ‌ها پیام راه‌اندازی را نشان می‌دهند</strong>
در Console عبارت <code>"xAiMcpServer started"</code> دیده می‌شود.
</li>
</ul>
<div class="info-box">
<strong>🔧 اگر با خطای SSL مواجه شدید:</strong> از <code>dotnet dev-certs https --trust</code> استفاده کنید، یا با <code>curl -k</code> تست کنید.
</div>
<div class="warning-box">
<strong>⚠️ اگر پورت 7001 اشغال بود:</strong> در <code>appsettings.json</code> یا <code>launchSettings.json</code> پورت را تغییر دهید یا پروسه اشغال‌کننده را با <code>netstat -ano | findstr :7001</code> پیدا و متوقف کنید.
</div>
</div>
</div>
<!-- Section 8 -->
<div class="section">
<div class="section-header">
<div class="section-number">۸</div>
<div class="section-title">
<h3>نمونه کامل Program.cs نهایی</h3>
<div class="subtitle">Final Complete Program.cs</div>
</div>
</div>
<div class="section-content">
<p class="no-indent">برای سهولت، کل فایل <code>Program.cs</code> نهایی را در یک نگاه:</p>
<div class="code-header">Program.cs — نسخه نهایی (کپی-پیست)</div>
<div class="code-block"><pre><span class="keyword">using</span> System.Text;
<span class="keyword">using</span> Microsoft.AspNetCore.Builder;
<span class="keyword">using</span> Microsoft.Extensions.DependencyInjection;
<span class="keyword">using</span> Microsoft.Extensions.Hosting;
<span class="keyword">using</span> Microsoft.Extensions.Logging;
<span class="keyword">namespace</span> xAiMcpServer
{
<span class="keyword">public class</span> Program
{
<span class="keyword">public static void</span> Main(<span class="keyword">string</span>[] args)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
<span class="keyword">var</span> builder = WebApplication.CreateBuilder(args);
<span class="comment">// Configure services via Startup</span>
<span class="keyword">var</span> startup = <span class="keyword">new</span> Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);
<span class="keyword">var</span> app = builder.Build();
<span class="comment">// Configure pipeline via Startup</span>
startup.Configure(app, app.Environment);
<span class="comment">// Startup log</span>
<span class="keyword">var</span> logger = app.Services.GetRequiredService&lt;ILogger&lt;Program&gt;&gt;();
logger.LogInformation(<span class="string">"🚀 xAiMcpServer started"</span>);
logger.LogInformation(<span class="string">"📡 MCP: /mcp"</span>);
logger.LogInformation(<span class="string">"📖 Swagger: /swagger"</span>);
logger.LogInformation(<span class="string">"❤️ Health: /health"</span>);
app.Run();
}
}
}</pre></div>
<div class="success-box">
<strong>✓ نتیجه:</strong> با این تنظیمات، پروژه به‌درستی به عنوان ASP.NET Core Web API اجرا می‌شود، <code>McpController</code> روی <code>/mcp</code> پاسخ می‌دهد، Swagger در دسترس است و می‌توانید با MCP Inspector اتصال را تست کنید.
</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">Final Summary</div>
</div>
</div>
<div class="section-content">
<div class="conclusion-box">
<h3>🎯 مشکل اصلی در یک جمله</h3>
<p>ترکیب Grapevine و ASP.NET Core در یک پروژه، باعث می‌شود متد <code>Configure</code> هرگز اجرا نشود و در نتیجه هیچ کنترلر، Swagger یا MCP Endpoint فعال نگردد.</p>
<div class="highlight">
<strong>💎 راه‌حل پیشنهادی:</strong><br>
۱. <code>Program.cs</code> را با <code>WebApplication.CreateBuilder</code> بازنویسی کنید.<br>
۲. فقط یک سیستم MCP (کنترلر سفارشی یا SDK رسمی) را فعال نگه دارید.<br>
۳. Endpoint <code>/health</code> اضافه کنید و با MCP Inspector تست نمایید.
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> پس از اعمال تغییرات، ابتدا <code>dotnet build</code> بگیرید، سپس با <code>curl</code> سه Endpoint اصلی (<code>/</code>، <code>/health</code>، <code>/mcp</code>) را تست کنید. اگر هر سه پاس شدند، اتصال از طریق MCP Inspector را امتحان کنید.
</div>
</div>
</div>
<div class="page-info">
این مستند فنی صرفاً جهت رفع مشکل و بهبود پروژه <code>xAiMcpServer</code> تهیه شده است.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">تحلیل فنی و راه‌حل اجرایی — xAiMcpServer</div>
<div class="footer-divider"></div>
<div>تاریخ تهیه: آبان ۱۴۰۵ &nbsp;|&nbsp; نسخه: ۱.۰ &nbsp;|&nbsp; طبقه‌بندی: فنی</div>
</footer>
</div>
</body>
</html>