{"id":5780,"date":"2026-02-18T11:13:02","date_gmt":"2026-02-18T03:13:02","guid":{"rendered":"http:\/\/xinblog.ltd\/?p=5780"},"modified":"2026-02-25T09:46:12","modified_gmt":"2026-02-25T01:46:12","slug":"7-mcp-%e4%bc%a0%e8%be%93%e5%b1%82-%e6%94%af%e6%8c%81%e7%9a%84%e4%bc%a0%e8%be%93%e5%8d%8f%e8%ae%ae%e5%8f%8a%e9%80%9a%e4%bf%a1%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=5780","title":{"rendered":"7.MCP-\u4f20\u8f93\u5c42-\u652f\u6301\u7684\u4f20\u8f93\u534f\u8bae\u53ca\u901a\u4fe1\u65b9\u5f0f"},"content":{"rendered":"<p>MCP\u534f\u8bae\u4e2d\u7684\u4f20\u8f93\u5c42<\/p>\n<p>\u5728\u4e0a\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u6d88\u606f\u7684\u6253\u5305\u65b9\u5f0f\u3002\u672c\u7ae0\u5c06\u91cd\u70b9\u8f6c\u5411\u6d88\u606f\u7684\u4f20\u8f93\u673a\u5236\uff0c\u4ecb\u7ecd MCP \u534f\u8bae\u6240\u652f\u6301\u7684\u51e0\u79cd\u4f20\u8f93\u534f\u8bae\u3002<\/p>\n<p>\u5728 MCP \u4e2d\uff0c\u5e38\u89c1\u7684\u4f1a\u8bdd\u7c7b\u578b\u5305\u62ec ClientSession \u548c ServerSession\u3002\u4e24\u8005\u5efa\u7acb\u8fde\u63a5\u540e\uff0c\u901a\u8fc7\u8f93\u5165\u8f93\u51fa\u6d41\u8fdb\u884c\u901a\u4fe1\u3002\u5177\u4f53\u7684\u4f20\u8f93\u534f\u8bae\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<p>1.Stdio Transport\uff1a\u57fa\u4e8e\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u7684\u8f7b\u91cf\u7ea7\u901a\u4fe1\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u672c\u5730\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002<\/p>\n<p>2.HTTP+SSE Transport\uff1a\u57fa\u4e8e HTTP \u534f\u8bae\uff0c\u652f\u6301\u670d\u52a1\u5668\u901a\u8fc7 SSE\uff08Server-Sent Events\uff09\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u6d88\u606f\uff0c\u5ba2\u6237\u7aef\u5219\u901a\u8fc7 HTTP POST \u53d1\u9001\u8bf7\u6c42\uff0c\u9002\u7528\u4e8e\u524d\u540e\u7aef\u5206\u79bb\u7684\u6d4f\u89c8\u5668\u573a\u666f\u3002<\/p>\n<p>3.Streamable HTTP Transport\uff1a\u4e00\u79cd\u66f4\u5b8c\u5584\u7684 Streamable HTTP \u5b9e\u73b0\uff0c\u53ef\u6839\u636e POST \u8bf7\u6c42\u8fd4\u56de JSON \u54cd\u5e94\u6216 SSE \u6d41\uff0c\u652f\u6301\u6df7\u5408\u8fd4\u56de\u6a21\u5f0f\u3002<\/p>\n<p>4.WebSocket Transport\uff1a\u57fa\u4e8e WebSocket \u7684\u5168\u53cc\u5de5\u901a\u4fe1\u901a\u9053\uff0c\u5efa\u7acb\u8fde\u63a5\u540e\u652f\u6301\u53cc\u5411\u5b9e\u65f6\u6d88\u606f\u63a8\u9001\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"1874\" height=\"1249\" src=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/img_256.png\" class=\"wp-image-5781\" alt=\"IMG_256\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/img_256.png 1874w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/img_256-300x200.png 300w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/img_256-1024x682.png 1024w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/img_256-768x512.png 768w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2025\/11\/img_256-1536x1024.png 1536w\" sizes=\"(max-width: 1874px) 100vw, 1874px\" \/><\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u5c06\u4f9d\u6b21\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u901a\u4fe1\u534f\u8bae\u3002<\/p>\n<p>1. \u6807\u51c6\u8f93\u5165\/\u8f93\u51fa\uff08stdio\uff09<\/p>\n<p>Stdio \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u65b9\u5f0f\uff0c\u901a\u8fc7\u6807\u51c6\u8f93\u5165\uff08stdin\uff09\u548c\u6807\u51c6\u8f93\u51fa\uff08stdout\uff09\u4f20\u9012 JSON-RPC \u6d88\u606f\u3002\u53ea\u9700\u5c06\u4e3b\u673a\uff08Host\uff09\u7684 stdout \u8fde\u63a5\u5230\u5de5\u5177\uff08Tool\uff09\u7684 stdin\uff0c\u5373\u53ef\u5b9e\u73b0\u6d88\u606f\u4f20\u8f93\u3002<\/p>\n<p>stdin\uff1a\u9ed8\u8ba4\u7684\u6807\u51c6\u8f93\u5165\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>stdout\uff1a\u9ed8\u8ba4\u7684\u6807\u51c6\u8f93\u51fa\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>stderr\uff1a\u7528\u4e8e\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u907f\u514d\u4e0e stdout \u6df7\u6dc6\u3002<\/p>\n<p>\u5728 MCP \u534f\u8bae\u4e2d\uff0c\u4f7f\u7528 stdio \u8fdb\u884c\u672c\u5730\u8fdb\u7a0b\u95f4\u901a\u4fe1\u65f6\uff0c\u4e3b\u673a\u7684 stdout \u4f1a\u8fde\u63a5\u5230\u5de5\u5177\u7684 stdin\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u70b9\u5728\u4e8e\u65e0\u9700\u7f51\u7edc\u7aef\u53e3\uff0c\u8f7b\u91cf\u900f\u660e\uff0c\u517c\u5bb9\u6027\u597d\u3002<\/p>\n<p>\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0cstdio_server()\u4f5c\u4e3a\u4e00\u4e2a\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u8d1f\u8d23\u7ba1\u7406 stdin \u548c stdout \u7684 UTF-8 \u6587\u672c\u6d41\u8bfb\u5199\u3002\u8fdb\u5165\u4e0a\u4e0b\u6587\u540e\uff0c\u4f1a\u542f\u52a8\u4e24\u4e2a\u534f\u7a0b\uff1a<\/p>\n<p>\u4e00\u4e2a\u534f\u7a0b\u4ece stdin \u8bfb\u53d6\u6587\u672c\uff0c\u53cd\u5e8f\u5217\u5316\u4e3a JSON-RPC \u6d88\u606f\uff08JSONRPCMessage\uff09\uff1b<\/p>\n<p>\u53e6\u4e00\u4e2a\u534f\u7a0b\u4ece write_stream \u4e2d\u83b7\u53d6\u8981\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u7684 SessionMessage\uff0c\u5e8f\u5217\u5316\u4e3a JSON \u540e\u5199\u5165 stdout\u3002<\/p>\n<table>\n<tr>\n<td>\n  # \u670d\u52a1\u7aef\uff1astdio_server \u7684\u6838\u5fc3\u5b9e\u73b0<\/p>\n<p>@asynccontextmanager<\/p>\n<p>async def stdio_server(stdin=None, stdout=None):<\/p>\n<p># \u5305\u88c5 stdin\/stdout \u4e3a UTF-8 \u6587\u672c\u5f02\u6b65\u6d41<\/p>\n<p>if not stdin:<\/p>\n<p>stdin = anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding=&#8221;utf-8&#8243;))<\/p>\n<p>if not stdout:<\/p>\n<p>stdout = anyio.wrap_file(TextIOWrapper(sys.stdout.buffer, encoding=&#8221;utf-8&#8243;))<\/p>\n<p># \u521b\u5efa\u8bfb\u5199\u5185\u5b58\u6d41<\/p>\n<p>read_w, read = anyio.create_memory_object_stream(0)<\/p>\n<p>write, write_r = anyio.create_memory_object_stream(0)<\/p>\n<p>async def reader():<\/p>\n<p>async with read_w:<\/p>\n<p>async for line in stdin:<\/p>\n<p>try:<\/p>\n<p>msg = types.JSONRPCMessage.model_validate_json(line)<\/p>\n<p>except Exception as e:<\/p>\n<p>await read_w.send(e)<\/p>\n<p>continue<\/p>\n<p>await read_w.send(SessionMessage(msg))<\/p>\n<p>async def writer():<\/p>\n<p>async with write_r:<\/p>\n<p>async for session_msg in write_r:<\/p>\n<p>json_str = session_msg.message.model_dump_json(by_alias=True, exclude_none=True)<\/p>\n<p>await stdout.write(json_str + &#8220;\\n&#8221;)<\/p>\n<p>await stdout.flush()<\/p>\n<p>async with anyio.create_task_group() as tg:<\/p>\n<p>tg.start_soon(reader)<\/p>\n<p>tg.start_soon(writer)<\/p>\n<p>yield read, write<\/td>\n<\/tr>\n<\/table>\n<p>2. SSE \u4f20\u8f93\uff08HTTP+SSE\uff09<\/p>\n<p>SSE \u5141\u8bb8\u901a\u8fc7 HTTP \u5efa\u7acb\u5355\u5411\u957f\u8fde\u63a5\uff0c\u4f7f\u670d\u52a1\u5668\u80fd\u591f\u6301\u7eed\u5411\u6d4f\u89c8\u5668\u7aef\u63a8\u9001\u6570\u636e\u3002\u5ba2\u6237\u7aef\u53ea\u9700\u53d1\u8d77\u4e00\u6b21 GET \u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u5373\u53ef\u6301\u7eed\u4ee5 text\/event-stream\u683c\u5f0f\u53d1\u9001\u6570\u636e\uff0c\u907f\u514d\u4e86\u5ba2\u6237\u7aef\u53cd\u590d\u8f6e\u8be2\u7684\u5f00\u9500\u3002<\/p>\n<p>\u5728\u5177\u4f53\u5b9e\u73b0\u4e2d\uff0c\u5ba2\u6237\u7aef sse_client\u4f1a\u542f\u52a8\u4e24\u4e2a\u5f02\u6b65\u5b50\u4efb\u52a1\uff1a<\/p>\n<p>\u4e00\u4e2a\u4efb\u52a1\u8bfb\u53d6\u670d\u52a1\u5668\u53d1\u6765\u7684 SSE \u4e8b\u4ef6\uff0c\u89e3\u6790\u7b2c\u4e00\u4e2a endpoint \u4e8b\u4ef6\u5e76\u53cd\u5e8f\u5217\u5316\uff0c\u7136\u540e\u8f6c\u53d1\u7ed9\u4e0a\u5c42\u7684 BaseSession\uff1b<\/p>\n<p>\u53e6\u4e00\u4e2a\u4efb\u52a1\uff08post_writer\uff09\u76d1\u542c\u4e0a\u5c42\u8981\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u7684 SessionMessage\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\uff0c\u901a\u8fc7 POST \u8bf7\u6c42\u53d1\u9001\u5230\u6307\u5b9a URL\uff0c\u4ece\u800c\u5b9e\u73b0 SSE \u8bfb\u3001POST \u5199\u7684\u53cc\u5de5\u901a\u4fe1\u3002<\/p>\n<p>\u5728\u670d\u52a1\u5668\u7aef\uff0c\u9700\u8981\u66b4\u9732\u4e24\u4e2a\u8def\u7531\uff1a<\/p>\n<p>\u4e00\u4e2a\u7528\u4e8e\u5904\u7406 SSE \u8fde\u63a5\u7684 GET \u8bf7\u6c42\uff0c\u5efa\u7acb\u8bfb\u5199\u6d41\u5e76\u751f\u6210 sessionId\uff0c\u4ee5 SSE \u4e8b\u4ef6\u6e90\u683c\u5f0f\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\uff1b<\/p>\n<p>\u53e6\u4e00\u4e2a\u7528\u4e8e\u63a5\u6536\u5ba2\u6237\u7aef\u7684 POST \u6d88\u606f\uff0c\u89e3\u6790\u8bf7\u6c42\u4f53\u4e2d\u7684 JSON-RPC \u6d88\u606f\u5e76\u5c01\u88c5\u4e3a SessionMessage\uff0c\u901a\u8fc7\u5bf9\u5e94\u7684 read_stream \u5199\u5165\u3002<\/p>\n<p>\u901a\u8fc7\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5e76\u5404\u81ea\u7ef4\u62a4 read_stream \u548c write_stream\uff0c\u5373\u53ef\u5229\u7528 BaseSession \u5904\u7406\u6d88\u606f\u5e27\u5316\u548c ID \u7ba1\u7406\uff0c\u800c\u65e0\u9700\u5173\u6ce8\u5e95\u5c42\u7ec6\u8282\u3002<\/p>\n<p>3. Streamable HTTP \u4f20\u8f93<\/p>\n<p>Streamable HTTP \u662f\u4e00\u79cd\u6807\u51c6\u7684\u652f\u6301\u7f51\u7edc\u901a\u4fe1\u7684\u53cc\u5411\u4f20\u8f93\u534f\u8bae\uff0c\u96c6\u6210\u4e86\u4e0a\u8ff0 SSE \u529f\u80fd\uff0c\u5b9e\u73b0\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u7684\u53cc\u5411\u901a\u4fe1\u3002<\/p>\n<p>\u5ba2\u6237\u7aef\u901a\u8fc7 HTTP POST \u53d1\u9001 JSON-RPC \u8bf7\u6c42\u6216\u901a\u77e5\uff1b<\/p>\n<p>\u670d\u52a1\u5668\u5219\u901a\u8fc7 SSE \u5728\u540c\u4e00 URL \u4e0a\u4fdd\u6301\u957f\u8fde\u63a5\uff0c\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u6d88\u606f\u3002<\/p>\n<p>\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u5bf9\u4e8e\u77ed\u5e73\u5feb\u7684\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u53ef\u76f4\u63a5\u8fd4\u56de application\/json\u683c\u5f0f\u7684\u666e\u901a\u54cd\u5e94\uff1b\u5bf9\u4e8e\u9700\u8981\u6301\u7eed\u63a8\u9001\u7684\u573a\u666f\uff0c\u5219\u53ef\u5728\u540c\u4e00 POST \u8bf7\u6c42\u4e2d\u5207\u6362\u4e3a text\/event-stream\u6a21\u5f0f\uff0c\u6301\u7eed\u53d1\u9001\u54cd\u5e94\u76f4\u81f3\u6d88\u606f\u7ed3\u675f\u3002<\/p>\n<p>4. WebSocket \u4f20\u8f93<\/p>\n<p>WebSocket \u4e3a MCP \u63d0\u4f9b\u4e86\u771f\u6b63\u7684\u53cc\u5411\u5168\u53cc\u5de5\u7f51\u7edc\u901a\u9053\u3002\u4e0e\u4e4b\u524d\u4f7f\u7528 POST\/GET \u5206\u522b\u5904\u7406\u8bfb\u5199\u4e0d\u540c\uff0cWebSocket \u5728\u63e1\u624b\u5b8c\u6210\u540e\u4f1a\u5efa\u7acb\u4e00\u6761\u6301\u4e45\u7684 TCP \u8fde\u63a5\uff0c\u53cc\u65b9\u53ef\u968f\u65f6\u53d1\u9001\u6587\u672c\u5e27\uff08\u5373\u5e8f\u5217\u5316\u540e\u7684 JSON-RPC \u6d88\u606f\uff09\u3002<\/p>\n<p>\u603b\u7ed3 \u4ee5\u4e0a\u56db\u79cd\u4f20\u8f93\u673a\u5236\u5728 MCP \u4e2d\u5e76\u5217\u5b58\u5728\uff0c\u5747\u4e3a\u53ef\u9009\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u7528\u4e8e\u627f\u8f7d JSON-RPC \u6d88\u606f\u4ea4\u4e92\uff0c\u5404\u6709\u5176\u9002\u7528\u573a\u666f\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\n  \u4f20\u8f93\u534f\u8bae<\/th>\n<th>\n  \u7279\u70b9<\/th>\n<th>\n  \u9002\u7528\u573a\u666f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n  Stdio<\/td>\n<td>\n  \u8f7b\u91cf\u3001\u65e0\u7f51\u7edc\u4f9d\u8d56<\/td>\n<td>\n  \u672c\u5730\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/td>\n<\/tr>\n<tr>\n<td>\n  HTTP+SSE<\/td>\n<td>\n  \u57fa\u4e8e HTTP\uff0c\u652f\u6301\u670d\u52a1\u5668\u63a8\u9001<\/td>\n<td>\n  \u6d4f\u89c8\u5668\u573a\u666f\uff0c\u524d\u540e\u7aef\u5206\u79bb<\/td>\n<\/tr>\n<tr>\n<td>\n  Streamable HTTP<\/td>\n<td>\n  \u7075\u6d3b\u652f\u6301\u5373\u65f6\u54cd\u5e94\u4e0e\u6d41\u5f0f\u8fd4\u56de<\/td>\n<td>\n  \u9700\u8981\u6df7\u5408\u54cd\u5e94\u6a21\u5f0f\u7684\u7f51\u7edc\u901a\u4fe1<\/td>\n<\/tr>\n<tr>\n<td>\n  WebSocket<\/td>\n<td>\n  \u5168\u53cc\u5de5\u5b9e\u65f6\u901a\u4fe1<\/td>\n<td>\n  \u9ad8\u6027\u80fd\u3001\u4f4e\u5ef6\u8fdf\u7684\u7f51\u7edc\u573a\u666f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u4f20\u8f93\u534f\u8bae\uff0c\u6570\u636e\u6700\u7ec8\u90fd\u4f1a\u4ea4\u7531\u540c\u4e00\u4e2a BaseSession \u8fdb\u884c\u6d88\u606f\u5e27\u5316\u4e0e ID \u7ba1\u7406\u3002\u5404\u4f20\u8f93\u5b9e\u73b0\u5185\u90e8\u53ea\u9700\u7ef4\u62a4\u5bf9\u5e94\u7684 read_stream \u548c write_stream \u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MCP\u534f\u8bae\u4e2d\u7684\u4f20\u8f93\u5c42 \u5728\u4e0a\u4e00\u7ae0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5780"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5780"}],"version-history":[{"count":1,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5780\/revisions"}],"predecessor-version":[{"id":5782,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5780\/revisions\/5782"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5780"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}