mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
对接MCP异步消息进度通知;日志通知
This commit is contained in:
parent
ff3e308ce8
commit
4790fc3852
1 changed files with 10 additions and 1 deletions
|
|
@ -14,6 +14,8 @@ 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 mcp.client.sse import sse_client
|
||||||
from mcp.client.streamable_http import streamablehttp_client
|
from mcp.client.streamable_http import streamablehttp_client
|
||||||
|
from mcp.types import LoggingMessageNotificationParams
|
||||||
|
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
from core.utils.util import sanitize_tool_name
|
from core.utils.util import sanitize_tool_name
|
||||||
|
|
||||||
|
|
@ -41,6 +43,12 @@ class ServerMCPClient:
|
||||||
self.tools_dict: Dict[str, Any] = {}
|
self.tools_dict: Dict[str, Any] = {}
|
||||||
self.name_mapping: Dict[str, str] = {}
|
self.name_mapping: Dict[str, str] = {}
|
||||||
|
|
||||||
|
async def logging_callback(self, params: LoggingMessageNotificationParams):
|
||||||
|
self.logger.bind(tag=TAG).info(f"[Server Log - {params.level.upper()}] {params.data}")
|
||||||
|
|
||||||
|
async def progress_callback(self, progress: float, total: float | None, message: str | None) -> None:
|
||||||
|
self.logger.bind(tag=TAG).info(f"[Progress {progress}/{total}]: {message}")
|
||||||
|
|
||||||
async def initialize(self):
|
async def initialize(self):
|
||||||
"""初始化MCP客户端连接"""
|
"""初始化MCP客户端连接"""
|
||||||
if self._worker_task:
|
if self._worker_task:
|
||||||
|
|
@ -115,7 +123,7 @@ class ServerMCPClient:
|
||||||
|
|
||||||
real_name = self.name_mapping.get(name, name)
|
real_name = self.name_mapping.get(name, name)
|
||||||
loop = self._worker_task.get_loop()
|
loop = self._worker_task.get_loop()
|
||||||
coro = self.session.call_tool(real_name, args)
|
coro = self.session.call_tool(real_name, arguments=args, progress_callback=self.progress_callback)
|
||||||
|
|
||||||
if loop is asyncio.get_running_loop():
|
if loop is asyncio.get_running_loop():
|
||||||
return await coro
|
return await coro
|
||||||
|
|
@ -209,6 +217,7 @@ class ServerMCPClient:
|
||||||
read_stream=read_stream,
|
read_stream=read_stream,
|
||||||
write_stream=write_stream,
|
write_stream=write_stream,
|
||||||
read_timeout_seconds=timedelta(seconds=15),
|
read_timeout_seconds=timedelta(seconds=15),
|
||||||
|
logging_callback=self.logging_callback
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
await self.session.initialize()
|
await self.session.initialize()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue