mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
支持透传DeviceId ClientId到MCP服务器,进行个性化处理
This commit is contained in:
parent
9194ffa6b7
commit
bd5efcfdd5
2 changed files with 26 additions and 9 deletions
|
|
@ -40,13 +40,15 @@ class ServerMCPClient:
|
|||
self.tools_dict: Dict[str, Any] = {}
|
||||
self.name_mapping: Dict[str, str] = {}
|
||||
|
||||
async def initialize(self):
|
||||
async def initialize(self, headers=None):
|
||||
"""初始化MCP客户端连接"""
|
||||
if headers is None:
|
||||
headers = {}
|
||||
if self._worker_task:
|
||||
return
|
||||
|
||||
self._worker_task = asyncio.create_task(
|
||||
self._worker(), name="ServerMCPClientWorker"
|
||||
self._worker(headers), name="ServerMCPClientWorker"
|
||||
)
|
||||
await self._ready_evt.wait()
|
||||
|
||||
|
|
@ -143,7 +145,7 @@ class ServerMCPClient:
|
|||
# 所有检查都通过,连接正常
|
||||
return True
|
||||
|
||||
async def _worker(self):
|
||||
async def _worker(self, headers: dict):
|
||||
"""MCP客户端工作协程"""
|
||||
async with AsyncExitStack() as stack:
|
||||
try:
|
||||
|
|
@ -167,13 +169,18 @@ class ServerMCPClient:
|
|||
|
||||
# 建立SSEClient
|
||||
elif "url" in self.config:
|
||||
headers = dict(self.config.get("headers", {}))
|
||||
proxy_headers = dict(self.config.get("headers", {}))
|
||||
# TODO 兼容旧版本
|
||||
if "API_ACCESS_TOKEN" in self.config:
|
||||
headers["Authorization"] = f"Bearer {self.config['API_ACCESS_TOKEN']}"
|
||||
self.logger.bind(tag=TAG).warning(f"你正在使用旧过时的配置 API_ACCESS_TOKEN ,请在.mcp_server_settings.json中将API_ACCESS_TOKEN直接设置在headers中,例如 'Authorization': 'Bearer API_ACCESS_TOKEN'")
|
||||
proxy_headers["Authorization"] = f"Bearer {self.config['API_ACCESS_TOKEN']}"
|
||||
self.logger.bind(tag=TAG).warning(
|
||||
f"你正在使用旧过时的配置 API_ACCESS_TOKEN ,请在.mcp_server_settings.json中将API_ACCESS_TOKEN直接设置在headers中,例如 'Authorization': 'Bearer API_ACCESS_TOKEN'")
|
||||
# 统一使用小写请求头进行合并
|
||||
proxy_headers = {k.lower(): v for k, v in proxy_headers.items()}
|
||||
headers = {k.lower(): v for k, v in headers.items()}
|
||||
proxy_headers = {**proxy_headers, **headers}
|
||||
sse_r, sse_w = await stack.enter_async_context(
|
||||
sse_client(self.config["url"], headers=headers, timeout=self.config.get("timeout", 5), sse_read_timeout=self.config.get("sse_read_timeout", 60 * 5))
|
||||
sse_client(self.config["url"], headers=proxy_headers, timeout=self.config.get("timeout", 5), sse_read_timeout=self.config.get("sse_read_timeout", 60 * 5))
|
||||
)
|
||||
read_stream, write_stream = sse_r, sse_w
|
||||
|
||||
|
|
|
|||
|
|
@ -56,7 +56,12 @@ class ServerMCPManager:
|
|||
# 初始化服务端MCP客户端
|
||||
logger.bind(tag=TAG).info(f"初始化服务端MCP客户端: {name}")
|
||||
client = ServerMCPClient(srv_config)
|
||||
await client.initialize()
|
||||
# 透传DeviceId ClientId等信息给MCP 服务器,进行个性化处理
|
||||
headers = {
|
||||
"device-id": self.conn.headers.get("device-id", ""),
|
||||
"client-id": self.conn.headers.get("client-id", ""),
|
||||
}
|
||||
await client.initialize(headers)
|
||||
self.clients[name] = client
|
||||
client_tools = client.get_available_tools()
|
||||
self.tools.extend(client_tools)
|
||||
|
|
@ -131,7 +136,12 @@ class ServerMCPManager:
|
|||
config = self.load_config()
|
||||
if client_name in config:
|
||||
client = ServerMCPClient(config[client_name])
|
||||
await client.initialize()
|
||||
# 透传DeviceId ClientId等信息给MCP 服务器,进行个性化处理
|
||||
headers = {
|
||||
"device-id": self.conn.headers.get("device-id", ""),
|
||||
"client-id": self.conn.headers.get("client-id", ""),
|
||||
}
|
||||
await client.initialize(headers)
|
||||
self.clients[client_name] = client
|
||||
target_client = client
|
||||
logger.bind(tag=TAG).info(
|
||||
|
|
|
|||
Loading…
Reference in a new issue