独立处理MCP Message

This commit is contained in:
myifeng
2025-05-28 19:28:36 +08:00
parent d7f3b3caf5
commit 8ea48f9875
3 changed files with 134 additions and 41 deletions
+2 -24
View File
@@ -1,6 +1,7 @@
import json
from core.handle.abortHandle import handleAbortMessage
from core.handle.helloHandle import handleHelloMessage
from core.handle.mcpHandle import handleMCPMessage
from core.utils.util import remove_punctuation_and_length, filter_sensitive_info
from core.handle.receiveAudioHandle import startToChat, handleAudioMessage
from core.handle.sendAudioHandle import send_stt_message, send_tts_message
@@ -75,30 +76,7 @@ async def handleTextMessage(conn, message):
elif msg_json["type"] == "mcp":
conn.logger.bind(tag=TAG).info(f"收到mcp消息:{message}")
if "payload" in msg_json:
payload = msg_json["payload"]
id = payload.get("id", None)
if id is None:
conn.logger.bind(tag=TAG).error("MCP消息缺少ID")
return
# result: 包含工具列表和上下文
# TODO: 处理MCP消息时,进行不同的处理
if "jsonrpc" not in payload or payload["jsonrpc"] != "2.0":
conn.logger.bind(tag=TAG).error("MCP消息缺少jsonrpc版本")
return
if "result" in payload:
result = payload["result"]
if "tools" in result:
# 初始化处理工具列表
tools = result["tools"]
conn.features["mcp"] = True
# 设置工具列表到连接对象,方便后续使用
conn.features["mcp_tools"] = tools
conn.logger.bind(tag=TAG).info(f"收到MCP工具列表:{tools}")
elif "context" in result:
# 处理上下文,上下文根据id进行管理,上报的id对应Server下发时的id
context = result["context"]
conn.logger.bind(tag=TAG).info(f"收到MCP上下文:{context}, ID: {id}")
asyncio.create_task(handleMCPMessage(conn, msg_json["payload"]))
elif msg_json["type"] == "server":
# 记录日志时过滤敏感信息
conn.logger.bind(tag=TAG).info(