关于MCP SSE 服务器的工作原理

模型上下文协议(Model Context Protocol,简称MCP) 是一种全新的开放协议,专门用于标准化地为大语言模型(LLMs)提供应用场景和数据背景。 你可以把MCP想象成AI领域的“USB-C接口”,它能让不同的AI模型与外部工具和数据源轻松连接

近来学习了一下,先是使用stdio的方式叫cursor做了一个,完全没有问题。但是sse的方式叫cursor 干始终不成功, 找了一轮,发现youtube的教程视频里主持,也没有搞定sse的服务器,balahblah说了一堆,就要move on ....于是研究了一下。

1. 首先MCP SSE是基于http协议的一个应用,服务器和客户端主要通过json rc的方式进行沟通。

2. MCP SSE Client会发起多个连接,但是第一个连接是http://yourhost:port/sse,  这个连接是沟通的第一步,它会使用chunked的回传数据,意思是不告诉client这个数据有多少,这样就它就可以一直连着了。 所以这个链接就是一个用来通知的链接。你现在就明白了为什么 ,就叫SSE(Server-Sent Events )。 它首先按以下格式信息给client, 然后就是定时的ping包了, 每次都只是一个chunk,估计后期server会有推送也会使用这个链接通知client.

event: 事件的名字
data:  事件的数据

      2.1 第一个event是,这个直接返回,就是给client分配一个session_id, 方便后面多个连接过来服务器可以分清谁和谁。


51
event: endpoint
data: /messages/?session_id=07aa8f90d79a49eaad802693cdd05b5b

      client收到这个,就会以http://yourhost:port/messages/?session_id=07aa8f90d79a49eaad802693cdd05b5b , 新发起一个连接去请求mcp sse server

     2.2 第二个event是event: message, 这个data 是一个json来的,就是告诉client,当前mcp server的能力,还有服务器的基本信息。

124
event: message
data: {"jsonrpc": "2.0","id": 0,"result": {"protocolVersion": "2024-11-05","capabilities": {"experimental": {},"prompts": {"listChanged": false},"resources": {"subscribe": false,"listChanged": false},"tools": {"listChanged": false}},"serverInfo": {"name": "mem0-mcp","version": "1.3.0"}}
}

     2.3 第三个event也是一个message , 用来告诉client 服务器提供的tools有哪些。

{"jsonrpc": "2.0","id": 1,"result": {"tools": [{"name": "add_coding_preference","description": "Add a new coding preference to mem0. This tool stores code snippets, implementation details,\n    and coding patterns for future reference. Store every code snippet. When storing code, you should include:\n    - Complete code with all necessary imports and dependencies\n    - Language/framework version information (e.g., \"Python 3.9\", \"React 18\")\n    - Full implementation context and any required setup/configuration\n    - Detailed comments explaining the logic, especially for complex sections\n    - Example usage or test cases demonstrating the code\n    - Any known limitations, edge cases, or performance considerations\n    - Related patterns or alternative approaches\n    - Links to relevant documentation or resources\n    - Environment setup requirements (if applicable)\n    - Error handling and debugging tips\n    The preference will be indexed for semantic search and can be retrieved later using natural language queries.","inputSchema": {"properties": {"text": {"title": "Text","type": "string"}},"required": ["text"],"title": "add_coding_preferenceArguments","type": "object"}},{"name": "get_all_coding_preferences","description": "Retrieve all stored coding preferences for the default user. Call this tool when you need \n    complete context of all previously stored preferences. This is useful when:\n    - You need to analyze all available code patterns\n    - You want to check all stored implementation examples\n    - You need to review the full history of stored solutions\n    - You want to ensure no relevant information is missed\n    Returns a comprehensive list of:\n    - Code snippets and implementation patterns\n    - Programming knowledge and best practices\n    - Technical documentation and examples\n    - Setup and configuration guides\n    Results are returned in JSON format with metadata.","inputSchema": {"properties": {},"title": "get_all_coding_preferencesArguments","type": "object"}},{"name": "search_coding_preferences","description": "Search through stored coding preferences using semantic search. This tool should be called \n    for EVERY user query to find relevant code and implementation details. It helps find:\n    - Specific code implementations or patterns\n    - Solutions to programming problems\n    - Best practices and coding standards\n    - Setup and configuration guides\n    - Technical documentation and examples\n    The search uses natural language understanding to find relevant matches, so you can\n    describe what you're looking for in plain English. Always search the preferences before \n    providing answers to ensure you leverage existing knowledge.","inputSchema": {"properties": {"query": {"title": "Query","type": "string"}},"required": ["query"],"title": "search_coding_preferencesArguments","type": "object"}}]}
}

跟着就是ping包的返回,防止client死了。


2d
: ping - 2025-03-12 08:16:23.071429+00:00

3. endpoint请求

   拿到endpont后,client 使用post的请求endpoint,  这个只处理请求,目前看 返回则在第一个http连接里。

第二个链接 请求如下:, 这个调用initialize,对应上面的第一个message的event.

第二个链接, 这个只回复202 AcceptedPOST /messages/?session_id=9aa12073a4494d5580a5c30ed54c4bfd HTTP/1.1
host: 10.0.105.64:8080
connection: keep-alive
content-type: application/json
accept: */*
accept-language: *
sec-fetch-mode: cors
user-agent: node
accept-encoding: gzip, deflate
content-length: 253{"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"tools":true,"prompts":false,"resources":true,"logging":false,"roots":{"listChanged":false}},"clientInfo":{"name":"cursor-vscode","version":"1.0.0"}},"jsonrpc":"2.0","id":0}
HTTP/1.1 202 Accepteddate: Wed, 12 Mar 2025 08:08:32 GMT
server: uvicorn
content-length: 8Accepted

第三个链接,请求如下:


第三个链接,仅回复 202 AcceptedPOST /messages/?session_id=9aa12073a4494d5580a5c30ed54c4bfd HTTP/1.1
host: 10.0.105.64:8080
connection: keep-alive
content-type: application/json
accept: */*
accept-language: *
sec-fetch-mode: cors
user-agent: node
accept-encoding: gzip, deflate
content-length: 54{"method":"notifications/initialized","jsonrpc":"2.0"}
HTTP/1.1 202 Accepted
date: Wed, 12 Mar 2025 08:08:32 GMT
server: uvicorn
content-length: 8Accepted

第四个链接,请求如下,请求tools/list, 服务器在第一个get的链接,通过event的方式返回了这个列表给mcp sse client


第四个链接,仅加复 202 AcceptedPOST /messages/?session_id=9aa12073a4494d5580a5c30ed54c4bfd HTTP/1.1
host: 10.0.105.64:8080
connection: keep-alive
content-type: application/json
accept: */*
accept-language: *
sec-fetch-mode: cors
user-agent: node
accept-encoding: gzip, deflate
content-length: 46{"method":"tools/list","jsonrpc":"2.0","id":1}
HTTP/1.1 202 Accepted
date: Wed, 12 Mar 2025 08:08:32 GMT
server: uvicorn
content-length: 8Accepted

1-3完成就是属于初始化完成,mcp sse的client和server 连接起来了。

然后后面使用mcp call 调用的,在cursor chat里输入

call mcp tool search_coding_preferences about StdioServerTransport

就是新起一个http短链接,post到endpoint,如下

POST /messages/?session_id=97a44bcff590415e99cf803350ffd542 HTTP/1.1
host: 10.0.105.64:8080
connection: keep-alive
content-type: application/json
accept: */*
accept-language: *
sec-fetch-mode: cors
user-agent: node
accept-encoding: gzip, deflate
content-length: 143{"method":"tools/call","params":{"name":"search_coding_preferences","arguments":{"query":"about StdioServerTransport"}},"jsonrpc":"2.0","id":3}HTTP/1.1 202 Accepted
date: Wed, 12 Mar 2025 09:10:43 GMT
server: uvicorn
content-length: 8HTTP/1.1 202 Accepted
date: Wed, 12 Mar 2025 09:10:43 GMT
server: uvicorn
content-length: 8Accepted

第一个链接就是会有一个通知返回如下:

event: message
data: {"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"[]"}],"isError":false}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/73160.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机:基于深度学习的Web应用安全漏洞检测与扫描

目录 前言 课题背景和意义 实现技术思路 一、算法理论基础 1.1 网络爬虫 1.2 漏洞检测 二、 数据集 三、实验及结果分析 3.1 实验环境搭建 3.2 模型训练 最后 前言 📅大四是整个大学期间最忙碌的时光,一边要忙着备考或实习为毕业后面临的就业升学做准备,…

win32汇编环境,网络编程入门之二

;运行效果 ;win32汇编环境,网络编程入门之二 ;本教程在前一教程的基础上,研究一下如何得到服务器的返回的信息 ;正常的逻辑是连接上了,然后我发送什么,它返回什么,但是这有一个很尴尬的问题。 ;就是如何表现出来。因为网络可能有延迟,这个延迟并不确定有多久。 ;而程序是顺…

【高分论文密码】AI大模型和R语言的全类型科研图形绘制,从画图、标注、改图、美化、组合、排序分解科研绘图每个步骤

在科研成果竞争日益激烈的当下,「一图胜千言」已成为高水平SCI期刊的硬性门槛——数据显示很多情况的拒稿与图表质量直接相关。科研人员普遍面临的工具效率低、设计规范缺失、多维数据呈现难等痛点,因此科研绘图已成为成果撰写中的至关重要的一个环节&am…

大语言模型-1.2-大模型技术基础

简介 本博客内容是《大语言模型》一书的读书笔记,该书是中国人民大学高瓴人工智能学院赵鑫教授团队出品,覆盖大语言模型训练与使用的全流程,从预训练到微调与对齐,从使用技术到评测应用,帮助学员全面掌握大语言模型的…

uni-app打包成H5使用相对路径

网上找了一圈,没用,各种试,终于给试出来了,主要是网络上的没有第二步,只有第一步,导致打包之后请求的路径没有带上域名 运行的基础路径设置为./ config.js文件里面的baseUrl路径改成空字符,千万…

Android UI性能优化

Android UI性能优化 一、UI性能优化基础 1.1 UI渲染原理 Android系统的UI渲染是通过一个被称为"UI线程"或"主线程"的单线程模型来完成的。系统会以16ms(约60fps)的固定时间间隔发送VSYNC信号,触发UI的渲染流程。如果一帧的处理时间超过16ms,就会出现丢…

【16】单片机编程核心技巧:移位运算的应用

【16】单片机编程核心技巧:移位运算的应用 七律 移位 左迁乘二寄存移,右徙除二暂寄时。 二进玄机藏位里,一移妙法化玄机。 合璧分疆拼字节,置位清零控毫厘。 速效堪超乘除算,单片机中展神威。 摘要 移位运算是单片…

【Linux内核系列】:文件系统

🔥 本文专栏:Linux 🌸作者主页:努力努力再努力wz ★★★ 本文前置知识: 文件系统初识 那么在我们此前关于文件的学习中,我们学习的都是进程与打开的文件之间的关系,以及打开的文件如何进行管理…

git commit messege 模板设置 (规范化管理git)

配置方法 git config --global core.editor vim (设置 Git 的默认编辑器为 Vim)在用户根目录下(~),创建一个.git_commit_msg文件,然后把下面的内容拷贝到文件中并保存。 [version][模块][类型]{解决xxx问题…

Python和Docker实现AWS ECR/ECS上全自动容器化部署网站前端

以类似ChatGPT的网站前端界面的HTML页面、CSS样式表和JavaScript脚本为例,用Python代码将整个前端代码文件的目录,其中包括所有创建的前端代码文件用Docker打包成镜像文件,提前检查Docker软件是否已经安装,并如果容器服务不存在&a…

无人机全景应用解析与技术演进趋势

无人机全景应用解析与技术演进趋势 ——从立体安防到万物互联的空中革命 一、现有应用场景全景解析 (一)公共安全领域 1. 立体安防体系 空中哨兵:搭载 77 GHz 77\text{GHz} 77GHz毫米波雷达(探测距离 5 km 5\text{km} 5km&…

ChatGPT4.5详细介绍和API调用详细教程

OpenAI在2月27日发布GPT-4.5的研究预览版——这是迄今为止OpenAI最强大、最出色的聊天模型。GPT-4.5在扩大预训练和微调规模方面迈出了重要的一步。通过扩大无监督学习的规模,GPT-4.5提升了识别内容中的模式、建立内容关联和生成对于内容的见解的能力,但…

AI 中对内存的庞大需求

刚接触AI时,只知道AI对显存的要求很高,但慢慢发现,AI对内存的要求也越来越高了。 最近尝试玩下 wan 2.1 ,进行图生视频,使用comfyui官方工作流,720p(720*1280)53帧,结果…

如何选择适合您智能家居解决方案的通信协议?

如何选择适合您智能家居解决方案的通信协议? 在开发智能家居产品时,选择合适的通信协议对于设备的高效运行及其在智能家居系统中的互操作性至关重要。市面上协议众多,了解它们的特性并在做决定前考虑各种因素是非常必要的。以下是一些帮助您…

L3-1 夺宝大赛

输入样例 1: 5 7 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 0 2 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 7 1 5 7 1 1 1 5 5 3 1 3 5 1 4输出样例 1: 7 6样例 1 说明: 七支队伍到达大本营的时间顺次为:7、不可能、5、3、3、5、6&#xff0c…

C# AOT生成的hellowwordEXE运行占用多少内存1-5MB?

C# 使用 AOT(Ahead - Of - Time,提前编译)生成的 "Hello, World!" 可执行文件在运行时占用的内存会受到多种因素的影响,以下是详细分析: 影响内存占用的因素 操作系统:不同的操作系统&#xff0…

nextJs在DOM视图中渲染未转为状态值的localStorage导致报错

报错但不限于如下&#xff1a; error: hydration failed because the initial ui does not match what was rendered on the server. Did not expect server HTML to contain a <span> in <div>. hook.js:608 warning: expected server html to contain a match…

macOS 安装 Homebrew、nvm 及安装切换 node 版本

一、安装Homebrew 提示&#xff1a;在安装 nvm 时&#xff0c;如果使用 brew 方式安装&#xff0c;就要先安装 Homebrew 1、打开终端&#xff0c;输入以下指令&#xff08;官网可获取最新命令&#xff09;&#xff1a; 国外镜像 /bin/bash -c "$(curl -fsSL https://ra…

海思高安主控芯片兼容编译fastboot流程

华为海思主控芯片有高安和非高安之分&#xff0c;主要是安全性上区别&#xff0c;启动程序不同&#xff0c;一般无法共用。但实际生产中可能出现混料或者同一款产品不同批次一个是高安的一个是非高安的&#xff0c;这时就需要软件上做兼容&#xff0c;实际是高安固件是可以做到…

大模型在甲状腺肿瘤预测及治疗方案制定中的应用研究

目录 一、引言 1.1 研究背景与意义 1.2 研究目的与创新点 1.3 研究方法与数据来源 二、甲状腺肿瘤概述 2.1 甲状腺肿瘤分类及特征 2.2 甲状腺肿瘤的发病率与危害 2.3 现有诊断与治疗手段概述 三、大模型技术原理与应用现状 3.1 大模型的基本原理与架构 3.2 大模型在…