2025-07-10-MCP协议HTTP交互过程

 

Here’s the table of contents:

  1. MCP 协议 SSE 模式下的 HTTP 请求过程
    1. 1. 建立 SSE 连接(GET)
    2. 2. 初始化(POST)
    3. 3. 通知已初始化(POST)
    4. 4. 获取工具列表(POST)
    5. 5. 获取资源列表(POST)
    6. 6. 获取提示词列表(POST)
    7. 7. 获取资源模板列表(POST)
    8. 8. 调用 list_tables 工具(POST)

MCP 协议 SSE 模式下的 HTTP 请求过程

 下面演示了从连接MCP服务到执行资源获取、提示词获取、工具调用的完整HTTP交互过程。【备注:执行结果需要订阅1中的数据流获取】

1. 建立 SSE 连接(GET)

curl -N -H "Accept: text/event-stream" "http://localhost:8000/mcp-chat-insight/sse"

2. 初始化(POST)

curl -X POST "http://localhost:8000/mcp-chat-insight/messages/?session_id=24f9f470dd0a4432a0acd4d5222d4d8a" \
  -H "Content-Type: application/json" \
  -d '{"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"Postman Client - df1716d8-d6a3-4b88-8669-021162b72341","version":"1.0.0"}},"jsonrpc":"2.0","id":0}'

3. 通知已初始化(POST)

curl -X POST "http://localhost:8000/mcp-chat-insight/messages/?session_id=24f9f470dd0a4432a0acd4d5222d4d8a" \
  -H "Content-Type: application/json" \
  -d '{"method":"notifications/initialized","jsonrpc":"2.0"}'

4. 获取工具列表(POST)

curl -X POST "http://localhost:8000/mcp-chat-insight/messages/?session_id=24f9f470dd0a4432a0acd4d5222d4d8a" \
  -H "Content-Type: application/json" \
  -d '{"method":"tools/list","jsonrpc":"2.0","id":1}'

5. 获取资源列表(POST)

curl -X POST "http://localhost:8000/mcp-chat-insight/messages/?session_id=24f9f470dd0a4432a0acd4d5222d4d8a" \
  -H "Content-Type: application/json" \
  -d '{"method":"resources/list","jsonrpc":"2.0","id":2}'

6. 获取提示词列表(POST)

curl -X POST "http://localhost:8000/mcp-chat-insight/messages/?session_id=24f9f470dd0a4432a0acd4d5222d4d8a" \
  -H "Content-Type: application/json" \
  -d '{"method":"prompts/list","jsonrpc":"2.0","id":3}'

7. 获取资源模板列表(POST)

curl -X POST "http://localhost:8000/mcp-chat-insight/messages/?session_id=24f9f470dd0a4432a0acd4d5222d4d8a" \
  -H "Content-Type: application/json" \
  -d '{"method":"resources/templates/list","jsonrpc":"2.0","id":4}'

8. 调用 list_tables 工具(POST)

curl -X POST "http://localhost:8000/mcp-chat-insight/messages/?session_id=24f9f470dd0a4432a0acd4d5222d4d8a" \
  -H "Content-Type: application/json" \
  -d '{"method":"tools/call","params":{"name":"list_tables","arguments":{}},"jsonrpc":"2.0","id":5}'