Skip to content

Commit cc9d530

Browse files
authored
Merge pull request #2437 from xinnan-tech/py_mcp_server_fix
update:服务端MCP新增支持Streamable HTTP传输协议
2 parents cde093d + 5149a80 commit cc9d530

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

main/xiaozhi-server/core/providers/tools/server_mcp/mcp_client.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from mcp import ClientSession, StdioServerParameters
1414
from mcp.client.stdio import stdio_client
1515
from mcp.client.sse import sse_client
16+
from mcp.client.streamable_http import streamablehttp_client
1617
from config.logger import setup_logging
1718
from core.utils.util import sanitize_tool_name
1819

@@ -172,10 +173,33 @@ async def _worker(self):
172173
if "API_ACCESS_TOKEN" in self.config:
173174
headers["Authorization"] = f"Bearer {self.config['API_ACCESS_TOKEN']}"
174175
self.logger.bind(tag=TAG).warning(f"你正在使用旧过时的配置 API_ACCESS_TOKEN ,请在.mcp_server_settings.json中将API_ACCESS_TOKEN直接设置在headers中,例如 'Authorization': 'Bearer API_ACCESS_TOKEN'")
175-
sse_r, sse_w = await stack.enter_async_context(
176-
sse_client(self.config["url"], headers=headers, timeout=self.config.get("timeout", 5), sse_read_timeout=self.config.get("sse_read_timeout", 60 * 5))
177-
)
178-
read_stream, write_stream = sse_r, sse_w
176+
177+
# 根据transport类型选择不同的客户端,默认为SSE
178+
transport_type = self.config.get("transport", "sse")
179+
180+
if transport_type == "streamable-http" or transport_type == "http":
181+
# 使用 Streamable HTTP 传输
182+
http_r, http_w, get_session_id = await stack.enter_async_context(
183+
streamablehttp_client(
184+
url=self.config["url"],
185+
headers=headers,
186+
timeout=self.config.get("timeout", 30),
187+
sse_read_timeout=self.config.get("sse_read_timeout", 60 * 5),
188+
terminate_on_close=self.config.get("terminate_on_close", True)
189+
)
190+
)
191+
read_stream, write_stream = http_r, http_w
192+
else:
193+
# 使用传统的 SSE 传输
194+
sse_r, sse_w = await stack.enter_async_context(
195+
sse_client(
196+
url=self.config["url"],
197+
headers=headers,
198+
timeout=self.config.get("timeout", 5),
199+
sse_read_timeout=self.config.get("sse_read_timeout", 60 * 5)
200+
)
201+
)
202+
read_stream, write_stream = sse_r, sse_w
179203

180204
else:
181205
raise ValueError("MCP客户端配置必须包含'command'或'url'")

main/xiaozhi-server/mcp_server_settings.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"后面不断测试补充好用的mcp服务,欢迎大家一起补充。",
55
"记得删除注释行,des属性仅为说明,不会被解析。",
66
"des和link属性,仅为说明安装方式,方便大家查看原始链接,不是必须项。",
7-
"当前支持stdio/sse两种模式"
7+
"当前支持三种传输模式:stdio(标准输入输出), sse(Server-Sent Events), streamable-http(流式HTTP)"
88
],
99
"mcpServers": {
1010
"Home Assistant": {
@@ -41,7 +41,16 @@
4141
"url": "http://localhost:8080/sse",
4242
"headers": {
4343
"Authorization": "Bearer YOUR TOKEN"
44-
}
44+
},
45+
"des": "使用SSE传输模式(默认)"
46+
},
47+
"streamable-http-mcp-server": {
48+
"url": "http://localhost:8000/mcp",
49+
"transport": "streamable-http",
50+
"headers": {
51+
"Authorization": "Bearer YOUR TOKEN"
52+
},
53+
"des": "使用Streamable HTTP传输模式,适用于生产环境的Web部署"
4554
}
4655
}
4756
}

0 commit comments

Comments
 (0)