MCP 接口范例(AI生成的,供参考学习)
·
Claude Code配置:
编辑文件:.mcp.json
{
"mcpServers": {
"finSystem": {
"type": "http",
"url": "http://localhost:5257/mcp"
}
}
}
通过一个MCP HTTP接口提供两个工具:
获取服务器时间:get_server_time
获取服务器名称:get_server_name
[AllowAnonymous] public class HomeController : ApiBase { private static readonly JsonSerializerOptions _jsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = false }; /// <summary> /// 标准 MCP Streamable HTTP 端点,实现 JSON-RPC 2.0 协议。 /// 支持 initialize / tools/list / tools/call 三种方法,供 Claude Code 客户端调用。 /// </summary> [AllowAnonymous] [RoutePattern(HttpMethod = "POST", Pattern = "/mcp", IgnorePrefix = true)] public async Task<IResult> McpEndpoint([FromBody] JsonObject body, CancellationToken cancellationToken) { if (body == null) { return Results.Json(McpError(null, -32700, "Parse error"), _jsonOptions); } var id = body["id"]; var method = body["method"]?.GetValue<string>(); if (string.IsNullOrWhiteSpace(method)) { return Results.Json(McpError(id, -32600, "Invalid Request: missing method"), _jsonOptions); } return method switch { "initialize" => Results.Json(McpResult(id, new { protocolVersion = "2024-11-05", capabilities = new { tools = new { } }, serverInfo = new { name = "fincore-mcp", version = "1.0.0" } }), _jsonOptions), "tools/list" => Results.Json(McpResult(id, new { tools = new[] { new { name = "get_server_time", description = "获取 XX系统 服务器当前时间,包含 UTC 时间、东八区(Asia/Shanghai)本地时间及 Unix 时间戳。", inputSchema = new { type = "object", properties = new { }, required = Array.Empty<string>() } }, new { name = "get_server_name", description = "获取 XX系统 服务器名称。", inputSchema = new { type = "object", properties = new { }, required = Array.Empty<string>() } } } }), _jsonOptions), "tools/call" => HandleToolCall(id, body), // notifications 无需响应(JSON-RPC notification 没有 id) _ when id == null => Results.Ok(), _ => Results.Json(McpError(id, -32601, $"Method not found: {method}"), _jsonOptions) }; } private static IResult HandleToolCall(JsonNode? id, JsonObject body) { var toolName = body["params"]?["name"]?.GetValue<string>(); if (toolName == "get_server_time") { var utcNow = DateTime.UtcNow; var shanghaiZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"); var localNow = TimeZoneInfo.ConvertTimeFromUtc(utcNow, shanghaiZone); var content = new[] { new { type = "text", text = JsonSerializer.Serialize(new { utc = utcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"), local = localNow.ToString("yyyy-MM-ddTHH:mm:ss"), timezone = "Asia/Shanghai (UTC+8)", unixTimestamp = new DateTimeOffset(utcNow).ToUnixTimeSeconds() }, _jsonOptions) } }; return Results.Json(McpResult(id, new { content, isError = false }), _jsonOptions); } else if (toolName == "get_server_name") { var content = new[] { new { type = "text", text = JsonSerializer.Serialize(new { servername = "这是一个财务系统", }, _jsonOptions) } }; return Results.Json(McpResult(id, new { content, isError = false }), _jsonOptions); } return Results.Json(McpError(id, -32602, $"Unknown tool: {toolName}"), _jsonOptions); } private static object McpResult(JsonNode? id, object result) => new { jsonrpc = "2.0", id, result }; private static object McpError(JsonNode? id, int code, string message) => new { jsonrpc = "2.0", id, error = new { code, message } }; }
更多推荐

所有评论(0)