使用http
This commit is contained in:
@@ -6,21 +6,20 @@
|
|||||||
- list_docx_images:列出 DOCX 中的图片信息
|
- list_docx_images:列出 DOCX 中的图片信息
|
||||||
- edit_docx: 进行文本替换 / 关键字上色 / 图片替换
|
- edit_docx: 进行文本替换 / 关键字上色 / 图片替换
|
||||||
|
|
||||||
支持两种传输方式:
|
当前推荐的传输方式:
|
||||||
- stdio(默认,本地使用)
|
- stdio(本地调试)
|
||||||
- sse(远程调用,通过 HTTP SSE 协议)
|
- streamable-http(远程 HTTP,路径固定为 /mcp,推荐)
|
||||||
|
|
||||||
用法:
|
用法:
|
||||||
# 本地 stdio 模式
|
# 本地 stdio 模式(默认)
|
||||||
python mcp_docx_server.py
|
python mcp_docx_server.py --transport stdio
|
||||||
|
|
||||||
# SSE 远程模式(默认 0.0.0.0:8080)
|
# HTTP 远程模式(推荐,默认 0.0.0.0:8080,对外暴露 /mcp)
|
||||||
python mcp_docx_server.py --transport sse
|
python mcp_docx_server.py --transport http
|
||||||
python mcp_docx_server.py --transport sse --host 0.0.0.0 --port 8080
|
python mcp_docx_server.py --transport http --host 0.0.0.0 --port 8080
|
||||||
|
|
||||||
# 客户端连接地址:
|
# 客户端连接地址(http 模式):
|
||||||
# SSE 端点: http://<host>:<port>/sse
|
# MCP 端点: http://<host>:<port>/mcp
|
||||||
# 消息端点: http://<host>:<port>/messages/
|
|
||||||
|
|
||||||
注意:底层仍然完全复用 mcp_docx.py 中的逻辑,只是通过 MCP SDK 对外提供。
|
注意:底层仍然完全复用 mcp_docx.py 中的逻辑,只是通过 MCP SDK 对外提供。
|
||||||
"""
|
"""
|
||||||
@@ -35,7 +34,8 @@ from mcp_docx import get_images_info, process, _parse_span_replacement
|
|||||||
|
|
||||||
|
|
||||||
mcp = FastMCP(
|
mcp = FastMCP(
|
||||||
"docx-editor"
|
"docx-editor",
|
||||||
|
description="DOCX 文本与图片编辑 MCP 服务器",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -149,29 +149,32 @@ if __name__ == "__main__":
|
|||||||
parser = argparse.ArgumentParser(description="DOCX MCP 服务器")
|
parser = argparse.ArgumentParser(description="DOCX MCP 服务器")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--transport",
|
"--transport",
|
||||||
choices=["stdio", "sse"],
|
choices=["stdio", "http"],
|
||||||
default="stdio",
|
default="stdio",
|
||||||
help="传输方式:stdio(本地)或 sse(远程 SSE)",
|
help="传输方式:stdio(本地)或 http(远程 HTTP /streamable-http)",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--host",
|
"--host",
|
||||||
default="0.0.0.0",
|
default="0.0.0.0",
|
||||||
help="SSE 模式监听地址(默认 0.0.0.0)",
|
help="HTTP 模式监听地址(默认 0.0.0.0)",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--port",
|
"--port",
|
||||||
type=int,
|
type=int,
|
||||||
default=8080,
|
default=8080,
|
||||||
help="SSE 模式监听端口(默认 8080)",
|
help="HTTP 模式监听端口(默认 8080)",
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.transport == "sse":
|
if args.transport == "http":
|
||||||
# SSE 远程模式:通过 HTTP 暴露 MCP 服务
|
# HTTP 远程模式:通过 streamable-http 暴露 MCP 服务,端点 /mcp
|
||||||
mcp.settings.host = args.host
|
print(f"🚀 MCP HTTP 服务器启动中 → http://{args.host}:{args.port}/mcp")
|
||||||
mcp.settings.port = args.port
|
mcp.run(
|
||||||
print(f"🚀 MCP SSE 服务器启动中 → http://{args.host}:{args.port}/sse")
|
transport="streamable-http",
|
||||||
mcp.run(transport="sse")
|
host=args.host,
|
||||||
|
port=args.port,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# 本地 stdio 模式
|
# 本地 stdio 模式
|
||||||
|
print("🚀 MCP stdio 模式启动中(本地使用)")
|
||||||
mcp.run(transport="stdio")
|
mcp.run(transport="stdio")
|
||||||
|
|||||||
Reference in New Issue
Block a user