MCPcopy Create free account
hub / github.com/IBM/mcp-cli / _init_tool_manager

Function _init_tool_manager

src/mcp_cli/run_command.py:85–137  ·  view source on GitHub ↗

Create and initialize a ToolManager instance. Uses _create_tool_manager() which can be patched via set_tool_manager_factory() for testing purposes. ENHANCED: Automatically selects appropriate namespace based on server type.

(
    config_file: str,
    servers: list[str],
    server_names: dict[int, str | None] | None = None,
    initialization_timeout: float = 120.0,
    runtime_config: "RuntimeConfig | None" = None,
)

Source from the content-addressed store, hash-verified

83# Tool-manager helpers #
84# --------------------------------------------------------------------------- #
85async def _init_tool_manager(
86 config_file: str,
87 servers: list[str],
88 server_names: dict[int, str | None] | None = None,
89 initialization_timeout: float = 120.0,
90 runtime_config: "RuntimeConfig | None" = None,
91) -> ToolManager:
92 """
93 Create and initialize a ToolManager instance.
94
95 Uses _create_tool_manager() which can be patched via set_tool_manager_factory()
96 for testing purposes.
97
98 ENHANCED: Automatically selects appropriate namespace based on server type.
99 """
100 tm = _create_tool_manager(
101 config_file,
102 servers,
103 server_names,
104 initialization_timeout=initialization_timeout,
105 runtime_config=runtime_config,
106 )
107
108 # ENHANCED: Let ToolManager automatically select the namespace
109 # It will use the server name for HTTP servers, "stdio" for STDIO servers
110 ok = await tm.initialize() # Remove the hardcoded namespace parameter
111
112 # Clean up any loggers that were created during initialization
113 from mcp_cli.config.logging import setup_logging
114 import os
115
116 log_level = os.environ.get("LOG_LEVEL", "WARNING")
117 setup_logging(level=log_level, quiet=False, verbose=False)
118
119 if not ok:
120 # Check if this is just because there are no servers
121 if not servers:
122 logger.info("No servers configured - continuing with empty tool manager")
123 # Still record and return the manager for chat without tools
124
125 _ALL_TM.append(tm)
126 return tm
127
128 # record it for the tests
129 _ALL_TM.append(tm)
130 # ensure close() is still invoked
131 try:
132 await tm.close()
133 finally:
134 raise RuntimeError("Failed to initialise ToolManager")
135
136 _ALL_TM.append(tm)
137 return tm
138
139
140async def _safe_close(tm) -> None:

Callers 5

_start_chatFunction · 0.90
run_commandFunction · 0.85
_innerFunction · 0.85

Calls 5

setup_loggingFunction · 0.90
_create_tool_managerFunction · 0.85
initializeMethod · 0.45
getMethod · 0.45
closeMethod · 0.45