fix: make discover_mcp_tools idempotent to prevent duplicate connections
When discover_mcp_tools() is called multiple times (e.g. direct call then model_tools import), return existing tool names instead of opening new connections that would orphan the previous ones.
This commit is contained in:
parent
aa2ecaef29
commit
593c549bc4
1 changed files with 12 additions and 0 deletions
|
|
@ -361,6 +361,9 @@ def discover_mcp_tools() -> List[str]:
|
||||||
Called from ``model_tools._discover_tools()``. Safe to call even when
|
Called from ``model_tools._discover_tools()``. Safe to call even when
|
||||||
the ``mcp`` package is not installed (returns empty list).
|
the ``mcp`` package is not installed (returns empty list).
|
||||||
|
|
||||||
|
Idempotent: if servers are already connected, returns the existing
|
||||||
|
tool names without creating duplicate connections.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List of all registered MCP tool names.
|
List of all registered MCP tool names.
|
||||||
"""
|
"""
|
||||||
|
|
@ -368,6 +371,15 @@ def discover_mcp_tools() -> List[str]:
|
||||||
logger.debug("MCP SDK not available -- skipping MCP tool discovery")
|
logger.debug("MCP SDK not available -- skipping MCP tool discovery")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
# Already connected -- return existing tool names (idempotent)
|
||||||
|
if _servers:
|
||||||
|
existing: List[str] = []
|
||||||
|
for name, server in _servers.items():
|
||||||
|
for mcp_tool in server._tools:
|
||||||
|
schema = _convert_mcp_schema(name, mcp_tool)
|
||||||
|
existing.append(schema["name"])
|
||||||
|
return existing
|
||||||
|
|
||||||
servers = _load_mcp_config()
|
servers = _load_mcp_config()
|
||||||
if not servers:
|
if not servers:
|
||||||
logger.debug("No MCP servers configured")
|
logger.debug("No MCP servers configured")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue