Merge pull request #1106 from kevin1sMe/feat-mcp

feat: MCP server支持使用sse模式
This commit is contained in:
玄凤科技
2025-05-06 15:38:11 +08:00
committed by GitHub
4 changed files with 35 additions and 26 deletions
+30 -22
View File
@@ -4,6 +4,7 @@ from contextlib import AsyncExitStack
import os, shutil import os, shutil
from mcp import ClientSession, StdioServerParameters from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client from mcp.client.stdio import stdio_client
from mcp.client.sse import sse_client
from config.logger import setup_logging from config.logger import setup_logging
@@ -21,29 +22,36 @@ class MCPClient:
async def initialize(self): async def initialize(self):
args = self.config.get("args", []) args = self.config.get("args", [])
command = ( if "command" in self.config:
shutil.which("npx") command = (
if self.config["command"] == "npx" shutil.which("npx")
else self.config["command"] if self.config["command"] == "npx"
) else self.config["command"]
)
env={**os.environ} env = {**os.environ}
if self.config.get("env"): if self.config.get("env"):
env.update(self.config["env"]) env.update(self.config["env"])
server_params = StdioServerParameters(
server_params = StdioServerParameters( command=command,
command=command, args=args,
args=args, env=env
env=env )
) stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
self.stdio, self.write = stdio_transport
stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params)) time_out_delta = timedelta(seconds=15)
self.stdio, self.write = stdio_transport self.session = await self.exit_stack.enter_async_context(
time_out_delta = timedelta(seconds=15) ClientSession(read_stream=self.stdio, write_stream=self.write, read_timeout_seconds=time_out_delta)
self.session = await self.exit_stack.enter_async_context(ClientSession(read_stream=self.stdio, write_stream=self.write, read_timeout_seconds=time_out_delta)) )
elif "url" in self.config:
sse_transport = await self.exit_stack.enter_async_context(sse_client(self.config["url"]))
self.sse_read, self.sse_write = sse_transport
self.session = await self.exit_stack.enter_async_context(
ClientSession(read_stream=self.sse_read, write_stream=self.sse_write)
)
else:
raise ValueError("MCPClient config must have 'command' or 'url'.")
await self.session.initialize() await self.session.initialize()
# List available tools # List available tools
response = await self.session.list_tools() response = await self.session.list_tools()
tools = response.tools tools = response.tools
+2 -2
View File
@@ -50,9 +50,9 @@ class MCPManager:
"""初始化所有MCP服务""" """初始化所有MCP服务"""
config = self.load_config() config = self.load_config()
for name, srv_config in config.items(): for name, srv_config in config.items():
if not srv_config.get("command"): if not srv_config.get("command") and not srv_config.get("url"):
self.logger.bind(tag=TAG).warning( self.logger.bind(tag=TAG).warning(
f"Skipping server {name}: command not specified" f"Skipping server {name}: neither command nor url specified"
) )
continue continue
+2 -1
View File
@@ -3,7 +3,8 @@
"在data目录下创建.mcp_server_settings.json文件,可以选择下面的MCP服务,也可以自行添加新的MCP服务。", "在data目录下创建.mcp_server_settings.json文件,可以选择下面的MCP服务,也可以自行添加新的MCP服务。",
"后面不断测试补充好用的mcp服务,欢迎大家一起补充。", "后面不断测试补充好用的mcp服务,欢迎大家一起补充。",
"记得删除注释行,des属性仅为说明,不会被解析。", "记得删除注释行,des属性仅为说明,不会被解析。",
"des和link属性,仅为说明安装方式,方便大家查看原始链接,不是必须项。" "des和link属性,仅为说明安装方式,方便大家查看原始链接,不是必须项。",
"当前支持stdio/sse两种模式。"
], ],
"mcpServers": { "mcpServers": {
"filesystem": { "filesystem": {
+1 -1
View File
@@ -22,7 +22,7 @@ mem0ai==0.1.62
bs4==0.0.2 bs4==0.0.2
modelscope==1.23.2 modelscope==1.23.2
sherpa_onnx==1.11.0 sherpa_onnx==1.11.0
mcp==1.4.1 mcp==1.7.1
cnlunar==0.2.0 cnlunar==0.2.0
PySocks==1.7.1 PySocks==1.7.1
dashscope==1.23.1 dashscope==1.23.1