mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-29 01:03:51 +08:00
接收并打印MCP数据
This commit is contained in:
@@ -35,6 +35,27 @@ async def handleHelloMessage(conn, msg_json):
|
|||||||
if features:
|
if features:
|
||||||
conn.logger.bind(tag=TAG).info(f"客户端特性: {features}")
|
conn.logger.bind(tag=TAG).info(f"客户端特性: {features}")
|
||||||
conn.features = features
|
conn.features = features
|
||||||
|
if features.get("mcp"):
|
||||||
|
conn.logger.bind(tag=TAG).info("客户端支持MCP")
|
||||||
|
# 发送初始化
|
||||||
|
await conn.websocket.send(json.dumps({
|
||||||
|
"type": "mcp",
|
||||||
|
"payload": {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "notifications/initialized"
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
# 发送mcp消息,获取tools列表
|
||||||
|
await conn.websocket.send(json.dumps( {
|
||||||
|
"type": "mcp",
|
||||||
|
"payload": {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "tools/list",
|
||||||
|
"id": 10001,
|
||||||
|
"params": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
await conn.websocket.send(json.dumps(conn.welcome_msg))
|
await conn.websocket.send(json.dumps(conn.welcome_msg))
|
||||||
|
|
||||||
|
|||||||
@@ -74,6 +74,31 @@ async def handleTextMessage(conn, message):
|
|||||||
asyncio.create_task(handleIotStatus(conn, msg_json["states"]))
|
asyncio.create_task(handleIotStatus(conn, msg_json["states"]))
|
||||||
elif msg_json["type"] == "mcp":
|
elif msg_json["type"] == "mcp":
|
||||||
conn.logger.bind(tag=TAG).info(f"收到mcp消息:{message}")
|
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}")
|
||||||
|
|
||||||
elif msg_json["type"] == "server":
|
elif msg_json["type"] == "server":
|
||||||
# 记录日志时过滤敏感信息
|
# 记录日志时过滤敏感信息
|
||||||
conn.logger.bind(tag=TAG).info(
|
conn.logger.bind(tag=TAG).info(
|
||||||
|
|||||||
Reference in New Issue
Block a user