mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
update:mcp服务端添加注入mac地址
This commit is contained in:
parent
349e474556
commit
5c730468df
2 changed files with 22 additions and 3 deletions
|
|
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||
|
||||
from datetime import timedelta
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import concurrent.futures
|
||||
|
|
@ -23,14 +24,16 @@ TAG = __name__
|
|||
class ServerMCPClient:
|
||||
"""服务端MCP客户端,用于连接和管理MCP服务"""
|
||||
|
||||
def __init__(self, config: Dict[str, Any]):
|
||||
def __init__(self, config: Dict[str, Any], conn=None):
|
||||
"""初始化服务端MCP客户端
|
||||
|
||||
Args:
|
||||
config: MCP服务配置字典
|
||||
conn: 连接对象,用于获取设备信息
|
||||
"""
|
||||
self.logger = setup_logging()
|
||||
self.config = config
|
||||
self.conn = conn
|
||||
|
||||
self._worker_task: Optional[asyncio.Task] = None
|
||||
self._ready_evt = asyncio.Event()
|
||||
|
|
@ -113,7 +116,23 @@ class ServerMCPClient:
|
|||
if not self.session:
|
||||
raise RuntimeError("服务端MCP客户端未初始化")
|
||||
|
||||
# 加入MAC地址
|
||||
if self.conn and hasattr(self.conn, 'device_id') and self.conn.device_id:
|
||||
tool_data = self.tools_dict.get(name)
|
||||
if tool_data and hasattr(tool_data, 'inputSchema') and isinstance(tool_data.inputSchema, dict):
|
||||
properties = tool_data.inputSchema.get('properties', {})
|
||||
if 'mac_address' in properties:
|
||||
args['mac_address'] = self.conn.device_id
|
||||
self.logger.bind(tag=TAG).info(
|
||||
f"已将设备MAC地址 {self.conn.device_id} 加入到服务端MCP工具调用参数中"
|
||||
)
|
||||
|
||||
real_name = self.name_mapping.get(name, name)
|
||||
|
||||
self.logger.bind(tag=TAG).info(
|
||||
f"调用服务端MCP工具: {real_name},参数: {json.dumps(args, ensure_ascii=False)}"
|
||||
)
|
||||
|
||||
loop = self._worker_task.get_loop()
|
||||
coro = self.session.call_tool(real_name, args)
|
||||
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ class ServerMCPManager:
|
|||
try:
|
||||
# 初始化服务端MCP客户端
|
||||
logger.bind(tag=TAG).info(f"初始化服务端MCP客户端: {name}")
|
||||
client = ServerMCPClient(srv_config)
|
||||
client = ServerMCPClient(srv_config, self.conn)
|
||||
await client.initialize()
|
||||
self.clients[name] = client
|
||||
client_tools = client.get_available_tools()
|
||||
|
|
@ -130,7 +130,7 @@ class ServerMCPManager:
|
|||
# 重新初始化客户端
|
||||
config = self.load_config()
|
||||
if client_name in config:
|
||||
client = ServerMCPClient(config[client_name])
|
||||
client = ServerMCPClient(config[client_name], self.conn)
|
||||
await client.initialize()
|
||||
self.clients[client_name] = client
|
||||
target_client = client
|
||||
|
|
|
|||
Loading…
Reference in a new issue