MCP SSE交互完整过程

有关MCP的介绍文章很多,本文不细说,做一个时序图将完整过程说清楚。MCP协议主要通过两种技术实现:标准输入输出(stdio)和服务器发送事件(SSE),stdio(标准输入输出)是MCP协议在本地或容器化环境中的主要实现方式。如果把客户端和服务端分离,在网络中通过API调用就使用SSE,这里主要就讲SSE的过程。

SSE概述

SSE是一种基于HTTP协议的轻量级技术,允许服务器向客户端(通常是浏览器)推送实时更新的数据。通过持久化的连接实现服务器到客户端的单向数据流。大致步骤如下:

(1)客户端通过HTTP请求向服务器发起SSE连接,并保持长连接;

(2)客户端新建连接,向服务端请求列出工具和携带参数请求执行工具等操作;

(3)服务端通过首次长连接返回执行结果;

MCP SSE完整过程

包含:请求用户、客户端、服务端、大模型,描述完整MCP SSE过程。

过程描述:

0、编写MCP Server工具,使用Python通过装饰器将工具注册到Server端上;

1、MCP Client对Server发送请求,GET方式请求/sse,并保持长连接状态等待数据实时返回;

2、Server端返回sessionID;

3、Client发送initialize包含协商信息;

4、Server端响应确认;

5、Client发送initialized确认;

6、用户在客户端请求提问:杭州天气;

7、Client发起向Server端的tools/list请求;

8、Server端返回注册好的工具列表及所需参数;

9、Client封装请求内容:

复制代码
{
    "messages": [
        {
            "role": "user",
            "content": "杭州的天气"
       }
   ],
   "model": "gpt-4o-mini",
   "max_tokens": 1000,
   "tools": [
       {
           "type": "function",
           "function": {
               "name": "get_weather",
               "description": "获取指定城市的天气信息",
               "parameters": {
                   "properties": {
                       "city": {
                           "title": "City",
                           "type": "string"
                       }
                   },
                   "required": [
                       "city"
                   ],
                   "title": "get_weatherArguments",
                   "type": "object"
               }
           }
       },
       {
           "type": "function",
           "function": {
               "name": "suggest_activity",
               "description": "根据天气描述推荐适合的活动",
               "parameters": {
                   "properties": {
                       "condition": {
                           "title": "Condition",
                           "type": "string"
                       }
                   },
                   "required": [
                       "condition"
                   ],
                   "title": "suggest_activityArguments",
                   "type": "object"
               }
           }
       }
   ]
}

10、Client将以上内容发送大模型调用API请求;

11、大模型返回tool_calls内容;

复制代码
{
    "id": "chatcmpl-BWdzaegB1h3XJMc4ZcgL4TWR3wmcr",
    "model": "gpt-4o-mini-2024-07-18",
    "object": "chat.completion",
    "created": 1747119926,
   "choices": [
       {
           "index": 0,
           "message": {
               "role": "assistant",
               "tool_calls": [
                   {
                       "id": "call_OaxrnCKcBANGqm4mheSd9RSD",
                       "type": "function",
                       "function": {
                           "name": "get_weather",
                           "arguments": "{\"city\":\"杭州\"}"
                       }
                   }
               ]
           },
           "finish_reason": "tool_calls"
       }
   ],
   "system_fingerprint": "fp_7a53abb7a2",
   "usage": {
       "prompt_tokens": 88,
       "completion_tokens": 15,
       "total_tokens": 103,
       "prompt_tokens_details": {
           "audio_tokens": 0,
           "cached_tokens": 0
       },
       "completion_tokens_details": {
           "accepted_prediction_tokens": 0,
           "audio_tokens": 0,
           "reasoning_tokens": 0,
           "rejected_prediction_tokens": 0
       }
   }
}

12、Client将大模型返回的name和arguments内容,POST请求Server;

13、Server接受参数执行对应的工具,并通过第一次长连接返回数据;

14、Client第二次将返回结果发送大模型调用API请求;

复制代码
{
    "messages": [
        {
            "role": "user",
            "content": "杭州的天气"
       },
       {
           "role": "assistant",
           "content": null,
           "tool_calls": [
               {
                   "id": "call_4Sbmkb2kNltmzVMkJJDDgGKd",
                   "function": {
                       "arguments": "{\"city\":\"杭州\"}",
                       "name": "get_weather"
                   },
                   "type": "function"
               }
           ]
       },
       {
           "role": "tool",
           "tool_call_id": "call_4Sbmkb2kNltmzVMkJJDDgGKd",
           "content": "杭州:晴,25°C"
       }
   ],
   "model": "gpt-4o-mini",
   "max_tokens": 1000
}

15、大模型返回结果,最终客户端将content内容返回给用户;

复制代码
{
    "id": "chatcmpl-BWe2yvHUm17yPRxTRuUS1FCYezPn2",
    "model": "gpt-4o-mini-2024-07-18",
    "object": "chat.completion",
    "created": 1747120136,
   "choices": [
       {
           "index": 0,
           "message": {
               "role": "assistant",
               "content": "杭州的天气是晴,气温25°C。"
           },
           "finish_reason": "stop"
       }
   ],
   "system_fingerprint": "fp_7a53abb7a2",
   "usage": {
       "prompt_tokens": 38,
       "completion_tokens": 12,
       "total_tokens": 50,
       "prompt_tokens_details": {
           "audio_tokens": 0,
           "cached_tokens": 0
       },
       "completion_tokens_details": {
           "accepted_prediction_tokens": 0,
           "audio_tokens": 0,
           "reasoning_tokens": 0,
           "rejected_prediction_tokens": 0
       }
   }
}