MCP 使用 SSE(Server-Sent Events)协议,需要两步配合。
第一步:建立 SSE 连接(GET)
在 Postman 中新建一个 GET 请求:
bash
GET http://localhost:8888/api/mcp
发送后会一直挂着(长连接),你会看到类似这样的 SSE 事件:
bash
event: endpoint
data: /api/mcp/message?sessionId=6dd06aab-01c5-44f4-a80a-d616aac5e273
复制这个 sessionId 值,第二步要用。
不要关闭这个请求 tab,保持连接。
第二步:发送 JSON-RPC 请求(POST)
新建一个 POST 请求:
bash
POST http://localhost:8888/api/mcp/message?sessionId=6dd06aab-01c5-44f4-a80a-d616aac5e273
sessionId用第一步拿到的实际值替换。
Headers:
bash
Content-Type: application/json
先发 initialize(必须先发这个)
Body → raw → JSON:
bash
{
"jsonrpc": "2.0", // 固定值,声明使用 JSON-RPC 2.0 版本
"id": 0, // 请求编号,用于匹配请求和响应(1对1)
"method": "initialize", // 要执行的方法名
"params": { //params(MCP 特有的参数)大括号内的都不需要改
"protocolVersion": "2024-11-05",//客户端支持的 MCP 协议版本号
"capabilities": {}, //客户端声明自己支持哪些 MCP 功能
"clientInfo": {
"name": "postman-test",// 客户端名称
"version": "1.0.0" //客户端版本
}
}
}
点发送后,回到第一步的 GET 请求 tab ,应该能看到返回的 SSE 事件,里面有 serverInfo。
返回结果
bash
{
"jsonrpc": "2.0",
"id": 0,
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {
"completions": {},
"logging": {},
"prompts": {
"listChanged": true
},
"resources": {
"subscribe": false,
"listChanged": true
},
"tools": {
"listChanged": true
}
},
"serverInfo": {
"name": "foss-mcp-server",
"version": "1.0.0"
}
}
}
再发 initialized 通知
bash
{
"jsonrpc": "2.0",
"method": "notifications/initialized"
}
查看工具列表
bash
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}
GET tab 里应该返回:
bash
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [
{
"name": "helloWord",
"description": "搜索开源软件信息",
"inputSchema": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "群聊的id"
}
},
"required": [
"name"
],
"additionalProperties": false
}
},
{
"name": "hello",
"description": "搜索开源软件信息",
"inputSchema": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
],
"additionalProperties": false
}
}
]
}
}
调用工具
bash
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "hello",
"arguments": {
"name": "世界"
}
}
}
GET tab 返回:
bash
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"content": [
{
"type": "text",
"text": "\"Hello, 世界! 调用成功啦!\""
}
],
"isError": false
}
}
Server 日志应该输出:
>>> [TOOL EXEC] 收到工具调用!参数 name=世界
>>> [TOOL EXEC] 执行完毕,返回:Hello, 世界! 调用成功啦!
完整操作流程图
Postman Tab 1 (GET) Postman Tab 2 (POST)
───────────────── ─────────────────────
GET /api/mcp
→ 挂起,拿 sessionId ───→ POST /api/mcp/message?sessionId=xxx
← 返回 SSE 事件 ←── Body: initialize
← 返回 SSE 事件 ←── Body: notifications/initialized
← 返回 SSE 事件 ←── Body: tools/list
← 返回 SSE 事件 ←── Body: tools/call
关键点:GET 请求必须一直保持连接,POST 的响应是通过 GET 的 SSE 流返回的。