# MCP 服务器调用说明
## MCP 协议调用方式
MCP(Model Context Protocol)使用 **JSON-RPC 协议**,所有调用都通过 `/mcp` 端点进行,而不是传统的 RESTful API。
## 调用流程
### 1. 获取工具列表(tools/list)
客户端通过 POST 请求到 `/mcp`,发送 JSON-RPC 请求:
```bash
curl -X POST http://localhost:8000/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list",
"params": {}
}'
```
**响应:**
```json
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [
{
"name": "hello_world",
"description": "一个简单的Hello World工具,支持多语言问候",
"inputSchema": {
"type": "object",
"properties": {
"name": {...},
"language": {...},
"count": {...}
}
}
}
]
}
}
```
### 2. 调用工具(tools/call)
客户端通过 POST 请求到 `/mcp`,发送工具调用:
```bash
curl -X POST http://localhost:8000/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "hello_world",
"arguments": {
"name": "张三",
"language": "zh",
"count": 3
}
}
}'
```
**响应:**
```json
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"content": [
{
"type": "text",
"text": "1. 你好,张三!\n2. 你好,张三!\n3. 你好,张三!\n\n[元数据: {\"language\": \"zh\", \"name\": \"张三\", \"count\": 3}]"
}
]
}
}
```
## 为什么没有直接的 RESTful 接口?
MCP 协议设计为:
- **统一的端点**:所有操作都通过 `/mcp` 端点
- **JSON-RPC 协议**:使用标准的 JSON-RPC 2.0 协议
- **方法驱动**:通过 `method` 字段区分不同的操作(`tools/list`, `tools/call` 等)
这样的设计使得:
1. 客户端只需要知道一个端点
2. 协议标准化,易于实现
3. 支持多种传输方式(stdio, HTTP, SSE等)
## 辅助端点(可选)
虽然主要调用通过 `/mcp`,但我们也提供了一些辅助端点:
### GET /mcp
显示服务器信息(不是调用接口)
### GET /tools
直接获取工具列表(方便调试,不是标准MCP协议)
### GET /health
健康检查
## Claude Desktop 如何调用
当你在 Claude Desktop 中配置了 MCP 服务器后:
1. **Claude Desktop 启动时**:
- 发送 `initialize` 请求到 `/mcp`
- 发送 `tools/list` 请求获取可用工具
2. **用户使用工具时**:
- Claude Desktop 发送 `tools/call` 请求到 `/mcp`
- 传递工具名称和参数
- 接收结果并显示给用户
3. **所有调用都通过 `/mcp` 端点**
## 测试示例
### PowerShell 测试脚本
```powershell
# 1. 获取工具列表
$body = @{
jsonrpc = "2.0"
id = 1
method = "tools/list"
params = @{}
} | ConvertTo-Json
Invoke-RestMethod -Uri "http://localhost:8000/mcp" `
-Method Post `
-ContentType "application/json" `
-Body $body
# 2. 调用 hello_world 工具
$body = @{
jsonrpc = "2.0"
id = 2
method = "tools/call"
params = @{
name = "hello_world"
arguments = @{
name = "张三"
language = "zh"
count = 3
}
}
} | ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri "http://localhost:8000/mcp" `
-Method Post `
-ContentType "application/json" `
-Body $body
```
## 总结
- ✅ **主入口**:`/mcp`(GET 显示信息,POST 处理调用)
- ✅ **协议**:JSON-RPC 2.0
- ✅ **方法**:`tools/list`, `tools/call`, `initialize`
- ✅ **没有**:`/hello_world` 这样的 RESTful 接口
- ✅ **所有调用**:都通过 POST `/mcp` 进行