MCP HTTP服务器(ASP.NET核心)
使用ASP.NET构建的模型上下文协议(MCP)HTTP服务器的示例实现。NET核心最低API。它暴露了一个 hello_world 该工具通过流友好的JSON响应演示工具注册、发现和执行。
先决条件
快速入门
# Restore dependencies
cd c:/s/AI/mcp-http-server
dotnet restore
# Run the server (HTTPS by default)
dotnet run --project src/McpHttpServer/McpHttpServer.csproj该服务监听中定义的HTTP和HTTPS端点 Properties/launchSettings.json 并支持JSON-RPC请求 /.
工具发现
curl http://localhost:5248/tools示例响应:
{
"tools": [
{
"name": "hello_world",
"description": "Returns a friendly greeting. Provide an optional 'name' field in the input.",
"inputSchema": {
"type": "object",
"title": "HelloWorldInput",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"description": "Optional name to include in the greeting."
}
}
}
}
]
}执行工具
curl https://localhost:7272/mcp/execute `
-H "Content-Type: application/json" `
-d '{"tool":"hello_world","input":{"name":"Azure"}}' `
-k服务器将JSON响应流式传输给调用者:
{"tool":"hello_world","contentType":"application/json","result":{"message":"Hello, Azure!"}}JSON-RPC使用情况
VS Code和其他支持MCP的客户端可以通过根端点上的JSON-RPC 2.0与服务器通信:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "hello_world",
"arguments": {
"name": "Azure"
}
}
}服务器回复:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tool": "hello_world",
"content": [
{
"type": "json",
"json": {
"message": "Hello, Azure!"
}
}
]
}
}测试和格式化
# Run integration tests
dotnet test
# Format the codebase (requires dotnet-format global tool)
dotnet format延伸
- 通过实施注册其他MCP工具
IMcpTool并将它们添加到DI容器中。 - 考虑添加结构化日志记录或OpenAPI元数据作为后续增强功能。
