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,
)
| 83 | # Tool-manager helpers # |
| 84 | # --------------------------------------------------------------------------- # |
| 85 | async 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 | |
| 140 | async def _safe_close(tm) -> None: |