mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-07-16 13:08:02 +00:00
fix(mcp_manager): remove timeout from MCP connection attempts and handle registration cleanup
This commit is contained in:
@@ -1048,7 +1048,7 @@ async def _startup_event():
|
|||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
logger.warning(f"Built-in MCP registration failed (non-critical): {type(e).__name__}: {e}")
|
logger.warning(f"Built-in MCP registration failed (non-critical): {type(e).__name__}: {e}")
|
||||||
try:
|
try:
|
||||||
await asyncio.wait_for(mcp_manager.connect_all_enabled(), timeout=20)
|
await mcp_manager.connect_all_enabled()
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
logger.warning("User MCP startup timed out (non-critical)")
|
logger.warning("User MCP startup timed out (non-critical)")
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
|
|||||||
+34
-17
@@ -194,43 +194,41 @@ class McpManager:
|
|||||||
)
|
)
|
||||||
|
|
||||||
stack = AsyncExitStack()
|
stack = AsyncExitStack()
|
||||||
|
registered = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
transport = await stack.enter_async_context(stdio_client(server_params))
|
transport = await stack.enter_async_context(stdio_client(server_params))
|
||||||
read_stream, write_stream = transport
|
read_stream, write_stream = transport
|
||||||
session = await stack.enter_async_context(ClientSession(read_stream, write_stream))
|
session = await stack.enter_async_context(ClientSession(read_stream, write_stream))
|
||||||
|
|
||||||
await session.initialize()
|
await session.initialize()
|
||||||
|
|
||||||
# Discover tools
|
|
||||||
tools_result = await session.list_tools()
|
tools_result = await session.list_tools()
|
||||||
except Exception:
|
|
||||||
await stack.aclose()
|
|
||||||
raise
|
|
||||||
tools = []
|
tools = []
|
||||||
for tool in tools_result.tools:
|
for tool in tools_result.tools:
|
||||||
tools.append({
|
tools.append({
|
||||||
"name": tool.name,
|
"name": tool.name,
|
||||||
"description": tool.description or "",
|
"description": tool.description or "",
|
||||||
"input_schema": tool.inputSchema if hasattr(tool, 'inputSchema') else {},
|
"input_schema": tool.inputSchema if hasattr(tool, "inputSchema") else {},
|
||||||
# MCP tool annotations (readOnlyHint / destructiveHint) drive
|
# MCP tool annotations (readOnlyHint / destructiveHint) drive
|
||||||
# plan-mode read-only gating. Absent on many servers, so we
|
# plan-mode read-only gating. Absent on many servers, so we
|
||||||
# fall back to a name heuristic in mcp_tool_is_readonly().
|
# fall back to a name heuristic in mcp_tool_is_readonly().
|
||||||
"annotations": getattr(tool, 'annotations', None),
|
"annotations": getattr(tool, "annotations", None),
|
||||||
})
|
})
|
||||||
|
|
||||||
self._sessions[server_id] = session
|
|
||||||
self._stacks[server_id] = stack
|
|
||||||
self._tools[server_id] = tools
|
|
||||||
# Extract identity hints from env vars (e.g. email address, API name)
|
# Extract identity hints from env vars (e.g. email address, API name)
|
||||||
# so tool descriptions can distinguish between multiple instances of
|
# so tool descriptions can distinguish between multiple instances of
|
||||||
# the same MCP server (e.g. two email accounts).
|
# the same MCP server (e.g. two email accounts).
|
||||||
identity_hints = []
|
identity_hints = []
|
||||||
for k, v in (env or {}).items():
|
for k, v in (env or {}).items():
|
||||||
k_lower = k.lower()
|
k_lower = k.lower()
|
||||||
if any(x in k_lower for x in ['email_address', 'account', 'user', 'username']):
|
if any(x in k_lower for x in ["email_address", "account", "user", "username"]):
|
||||||
identity_hints.append(v)
|
identity_hints.append(v)
|
||||||
identity = ", ".join(identity_hints) if identity_hints else ""
|
identity = ", ".join(identity_hints) if identity_hints else ""
|
||||||
|
|
||||||
|
self._sessions[server_id] = session
|
||||||
|
self._stacks[server_id] = stack
|
||||||
|
self._tools[server_id] = tools
|
||||||
self._connections[server_id] = {
|
self._connections[server_id] = {
|
||||||
"status": "connected",
|
"status": "connected",
|
||||||
"name": name,
|
"name": name,
|
||||||
@@ -239,12 +237,22 @@ class McpManager:
|
|||||||
"identity": identity,
|
"identity": identity,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registered = True
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if not registered:
|
||||||
|
await stack.aclose()
|
||||||
|
|
||||||
logger.info(f"MCP server connected: {name} ({server_id}) - {len(tools)} tools via stdio")
|
logger.info(f"MCP server connected: {name} ({server_id}) - {len(tools)} tools via stdio")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
logger.warning("MCP package not installed. Install with: pip install mcp")
|
logger.warning("MCP package not installed. Install with: pip install mcp")
|
||||||
self._connections[server_id] = {"status": "error", "error": "mcp package not installed", "name": name}
|
self._connections[server_id] = {
|
||||||
|
"status": "error",
|
||||||
|
"error": "mcp package not installed",
|
||||||
|
"name": name,
|
||||||
|
}
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def _connect_sse(self, server_id: str, name: str, url: str) -> bool:
|
async def _connect_sse(self, server_id: str, name: str, url: str) -> bool:
|
||||||
@@ -255,18 +263,16 @@ class McpManager:
|
|||||||
from contextlib import AsyncExitStack
|
from contextlib import AsyncExitStack
|
||||||
|
|
||||||
stack = AsyncExitStack()
|
stack = AsyncExitStack()
|
||||||
|
registered = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
transport = await stack.enter_async_context(sse_client(url))
|
transport = await stack.enter_async_context(sse_client(url))
|
||||||
read_stream, write_stream = transport
|
read_stream, write_stream = transport
|
||||||
session = await stack.enter_async_context(ClientSession(read_stream, write_stream))
|
session = await stack.enter_async_context(ClientSession(read_stream, write_stream))
|
||||||
|
|
||||||
await session.initialize()
|
await session.initialize()
|
||||||
|
|
||||||
# Discover tools
|
|
||||||
tools_result = await session.list_tools()
|
tools_result = await session.list_tools()
|
||||||
except Exception:
|
|
||||||
await stack.aclose()
|
|
||||||
raise
|
|
||||||
tools = []
|
tools = []
|
||||||
for tool in tools_result.tools:
|
for tool in tools_result.tools:
|
||||||
tools.append({
|
tools.append({
|
||||||
@@ -289,9 +295,15 @@ class McpManager:
|
|||||||
"tool_count": len(tools),
|
"tool_count": len(tools),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registered = True
|
||||||
|
|
||||||
logger.info(f"MCP server connected: {name} ({server_id}) - {len(tools)} tools via SSE")
|
logger.info(f"MCP server connected: {name} ({server_id}) - {len(tools)} tools via SSE")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if not registered:
|
||||||
|
await stack.aclose()
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
logger.warning("MCP package not installed. Install with: pip install mcp")
|
logger.warning("MCP package not installed. Install with: pip install mcp")
|
||||||
self._connections[server_id] = {"status": "error", "error": "mcp package not installed", "name": name}
|
self._connections[server_id] = {"status": "error", "error": "mcp package not installed", "name": name}
|
||||||
@@ -446,6 +458,11 @@ class McpManager:
|
|||||||
)
|
)
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
logger.warning("Timed out connecting to %s", srv.name)
|
logger.warning("Timed out connecting to %s", srv.name)
|
||||||
|
self._connections[srv.id] = {
|
||||||
|
"status": "timeout",
|
||||||
|
"error": f"Timed out after 20 seconds",
|
||||||
|
"name": srv.name,
|
||||||
|
}
|
||||||
|
|
||||||
async def call_tool(self, qualified_name: str, arguments: Dict) -> Dict:
|
async def call_tool(self, qualified_name: str, arguments: Dict) -> Dict:
|
||||||
"""Call an MCP tool by its qualified name (mcp__{server_id}__{tool_name}).
|
"""Call an MCP tool by its qualified name (mcp__{server_id}__{tool_name}).
|
||||||
|
|||||||
Reference in New Issue
Block a user