پس از اعمال تغییرات بالا، برای اطمینان از اجرای صحیح، مراحل زیر را طی کنید:
مرحله ۱: بررسی Build و اجرا
dotnet build xAiMcpServer.csproj
dotnet run --project xAiMcpServer.csproj --urls "https://localhost:7001"
dotnet run --project xAiMcpServer.csproj --urls "https://localhost:7001" --launch-profile "https"
مرحله ۲: تست Endpointهای اصلی
curl -k https://localhost:7001/
curl -k https://localhost:7001/swagger/index.html
curl -k https://localhost:7001/health
curl -k -X POST https://localhost:7001/mcp `
-H "Content-Type: application/json" `
-d '{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}'
curl -k -X POST https://localhost:7001/mcp `
-H "Content-Type: application/json" `
-d '{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/list\"}'
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\"}}}'
مرحله ۳: استفاده از MCP Inspector (توصیه میشود)
ابزار رسمی MCP Inspector برای تست تعاملی سرور:
npx @modelcontextprotocol/inspector
مرحله ۴: اضافه کردن Health Check Endpoint
یک Endpoint ساده برای بررسی سلامت و اجرای صحیح:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace xAiMcpServer.Controllers
{
[ApiController]
[Route("health")]
[AllowAnonymous]
public class HealthController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return Ok(new
{
status = "Healthy",
service = "xAiMcpServer",
timestamp = DateTime.UtcNow,
environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
?? "Production",
version = typeof(Program).Assembly.GetName().Version?.ToString()
});
}
}
}
مرحله ۵: لاگگیری ساختاریافته
در Program.cs بعد از builder.Build() اضافه کنید:
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("🚀 xAiMcpServer started on {Urls}",
app.Urls.Count > 0 ? string.Join(", ", app.Urls) : "configured endpoints");
logger.LogInformation("📡 MCP endpoint: /mcp");
logger.LogInformation("📖 Swagger UI: /swagger");
logger.LogInformation("❤️ Health: /health");