گام ۱ — تنظیم بستهها
xAiMcpServer.csproj
<ItemGroup>
<PackageReference Include="Grapevine.Extensions.Mcp" Version="5.0.2" />
<PackageReference Include="ModelContextProtocol" Version="2.2.0" />
</ItemGroup>
از نصب ModelContextProtocol.AspNetCore خودداری کنید.
گام ۲ — تعریف Tool
Tools/EchoTool.cs
using System.ComponentModel;
using ModelContextProtocol.Server;
namespace xAiMcpServer.Tools
{
[McpServerToolType]
public static class EchoTool
{
[McpServerTool, Description("Echoes the message back to the client.")]
public static string Echo(string message) => $"hello {message}";
}
}
گام ۳ — پیادهسازی Program.cs
Program.cs
using System;
using Grapevine;
using Grapevine.Extensions.Mcp;
using ModelContextProtocol.Server;
namespace xAiMcpServer
{
public class Program
{
public static void Main(string[] args)
{
var builder = new RestServerBuilder(
// لامبدای اول: ثبت سرویسهای MCP (نوع: IServiceCollection)
services => services
.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly(),
// لامبدای دوم: تنظیم سرور Grapevine (نوع صریح: IRestServer)
(IRestServer server) =>
{
server.Prefixes.Add("http://localhost:5000/");
}
);
var server = builder.Build();
server.MapMcp("/mcp"); // فعالسازی مسیر JSON-RPC
server.Start();
Console.WriteLine("MCP Server running on http://localhost:5000/mcp");
Console.WriteLine("Press Enter to stop...");
Console.ReadLine();
server.Stop();
}
}
}
✓ نکات کلیدی:
- دو لامبدا باید با کاما از هم جدا شوند.
- نوع پارامتر دوم باید صریحاً
(IRestServer server) نوشته شود تا خطای Prefixes رخ ندهد.
- فراخوانی
server.MapMcp("/mcp") برای ثبت مسیر MCP الزامی است.
گام ۴ — اتصال از xAiApi
POST /McpServer
{
"name": "xAiMcpServer-Tools",
"endpoint": "http://localhost:5000/mcp",
"enabled": true,
"scope": "Owner",
"timeoutSeconds": 30,
"description": "ابزارهای سازمانی از xAiMcpServer (Grapevine)"
}