From d42930e3a1f23f014008ad32cf5288653528ebd8 Mon Sep 17 00:00:00 2001 From: liangweihao <734499798@qq.com> Date: Thu, 12 Feb 2026 16:56:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8http?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mcp_docx_server.py | 47 ++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/mcp_docx_server.py b/mcp_docx_server.py index b65863e..29e4b2c 100644 --- a/mcp_docx_server.py +++ b/mcp_docx_server.py @@ -6,21 +6,20 @@ - list_docx_images:列出 DOCX 中的图片信息 - edit_docx: 进行文本替换 / 关键字上色 / 图片替换 -支持两种传输方式: -- stdio(默认,本地使用) -- sse(远程调用,通过 HTTP SSE 协议) +当前推荐的传输方式: +- stdio(本地调试) +- streamable-http(远程 HTTP,路径固定为 /mcp,推荐) 用法: - # 本地 stdio 模式 - python mcp_docx_server.py + # 本地 stdio 模式(默认) + python mcp_docx_server.py --transport stdio - # SSE 远程模式(默认 0.0.0.0:8080) - python mcp_docx_server.py --transport sse - python mcp_docx_server.py --transport sse --host 0.0.0.0 --port 8080 + # HTTP 远程模式(推荐,默认 0.0.0.0:8080,对外暴露 /mcp) + python mcp_docx_server.py --transport http + python mcp_docx_server.py --transport http --host 0.0.0.0 --port 8080 - # 客户端连接地址: - # SSE 端点: http://:/sse - # 消息端点: http://:/messages/ + # 客户端连接地址(http 模式): + # MCP 端点: http://:/mcp 注意:底层仍然完全复用 mcp_docx.py 中的逻辑,只是通过 MCP SDK 对外提供。 """ @@ -35,7 +34,8 @@ from mcp_docx import get_images_info, process, _parse_span_replacement mcp = FastMCP( - "docx-editor" + "docx-editor", + description="DOCX 文本与图片编辑 MCP 服务器", ) @@ -149,29 +149,32 @@ if __name__ == "__main__": parser = argparse.ArgumentParser(description="DOCX MCP 服务器") parser.add_argument( "--transport", - choices=["stdio", "sse"], + choices=["stdio", "http"], default="stdio", - help="传输方式:stdio(本地)或 sse(远程 SSE)", + help="传输方式:stdio(本地)或 http(远程 HTTP /streamable-http)", ) parser.add_argument( "--host", default="0.0.0.0", - help="SSE 模式监听地址(默认 0.0.0.0)", + help="HTTP 模式监听地址(默认 0.0.0.0)", ) parser.add_argument( "--port", type=int, default=8080, - help="SSE 模式监听端口(默认 8080)", + help="HTTP 模式监听端口(默认 8080)", ) args = parser.parse_args() - if args.transport == "sse": - # SSE 远程模式:通过 HTTP 暴露 MCP 服务 - mcp.settings.host = args.host - mcp.settings.port = args.port - print(f"🚀 MCP SSE 服务器启动中 → http://{args.host}:{args.port}/sse") - mcp.run(transport="sse") + if args.transport == "http": + # HTTP 远程模式:通过 streamable-http 暴露 MCP 服务,端点 /mcp + print(f"🚀 MCP HTTP 服务器启动中 → http://{args.host}:{args.port}/mcp") + mcp.run( + transport="streamable-http", + host=args.host, + port=args.port, + ) else: # 本地 stdio 模式 + print("🚀 MCP stdio 模式启动中(本地使用)") mcp.run(transport="stdio")