feat: MCP server支持使用sse模式

This commit is contained in:
kevin1sMe
2025-05-04 23:33:07 +08:00
parent 0f3806d358
commit 8f266bea0d
4 changed files with 36 additions and 25 deletions
+29 -21
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
@@ -26,6 +26,9 @@
"command": "npx", "command": "npx",
"args": ["-y", "@simonb97/server-win-cli"], "args": ["-y", "@simonb97/server-win-cli"],
"link": "https://github.com/SimonB97/win-cli-mcp-server" "link": "https://github.com/SimonB97/win-cli-mcp-server"
},
"perplexity-ask": {
"url": "http://your-server-ip:your-port/sse"
} }
} }
} }
+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