{"record":{"id":"3ed8e9f5704bc0b2","repo":"huggingface/smolagents","slug":"couldn-t-retrieve-tools-from-mcp-server-run-mcp","errorCode":null,"errorMessage":"Couldn't retrieve tools from MCP server, run `mcp_client.connect()` first before accessing `tools`","messagePattern":"Couldn't retrieve tools from MCP server, run `mcp_client\\.connect\\(\\)` first before accessing `tools`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/smolagents/mcp_client.py","lineNumber":151,"sourceCode":"    ):\n        \"\"\"Disconnect from the MCP server\"\"\"\n        self._adapter.__exit__(exc_type, exc_value, exc_traceback)\n\n    def get_tools(self) -> list[Tool]:\n        \"\"\"The SmolAgents tools available from the MCP server.\n\n        Note: for now, this always returns the tools available at the creation of the session,\n        but it will in a future release return also new tools available from the MCP server if\n        any at call time.\n\n        Raises:\n            ValueError: If the MCP server tools is None (usually assuming the server is not started).\n\n        Returns:\n            list[Tool]: The SmolAgents tools available from the MCP server.\n        \"\"\"\n        if self._tools is None:\n            raise ValueError(\n                \"Couldn't retrieve tools from MCP server, run `mcp_client.connect()` first before accessing `tools`\"\n            )\n        return self._tools\n\n    def __enter__(self) -> list[Tool]:\n        \"\"\"Connect to the MCP server and return the tools directly.\n\n        Note that because of the `.connect` in the init, the mcp_client\n        is already connected at this point.\n        \"\"\"\n        return self._tools\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_value: BaseException | None,\n        exc_traceback: TracebackType | None,\n    ):","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/mcp_client.py#L133-L169","documentation":"MCPClient.get_tools() returns the tools discovered from the MCP server, which are only populated after a successful connect(). If connect() was never called (or hasn't finished), self._tools is None and get_tools raises ValueError with an instruction to connect first. This typically surfaces as an attribute-style access `mcp_client.tools` since tools is a property delegating to get_tools.","triggerScenarios":"Accessing mcp_client.tools before calling mcp_client.connect(), or constructing MCPClient and immediately reading .tools instead of using it as a context manager.","commonSituations":"Skipping the `with MCPClient(...) as tools:` pattern from the docs and calling connect()/tools manually in the wrong order; connect() failing silently in error handling paths.","solutions":["Use the context-manager form: `with MCPClient(params) as tools:` which connects and returns tools","Or call mcp_client.connect() explicitly before accessing .tools","Ensure connect() succeeded (no swallowed exceptions) before reading tools"],"exampleFix":"# before\nclient = MCPClient(params)\ntools = client.tools  # ValueError\n\n# after\nclient = MCPClient(params)\nclient.connect()\ntools = client.tools\n# or: with MCPClient(params) as tools: ...","handlingStrategy":"validation","validationCode":"client = MCPClient(params)\nclient.connect()\nassert client._tools is not None, 'connect() did not populate tools'\ntools = client.tools","typeGuard":"def mcp_client_ready(client) -> bool:\n    return getattr(client, \"_tools\", None) is not None","tryCatchPattern":"try:\n    tools = client.tools\nexcept ValueError as e:\n    if 'run `mcp_client.connect()`' in str(e):\n        client.connect()\n        tools = client.tools","preventionTips":["Prefer `with MCPClient(...) as tools:` context-manager usage","Call connect() before any .tools access","Make sure connect() exceptions aren't swallowed before reading tools"],"tags":["smolagents","mcp","lifecycle","not-connected"],"backgroundTag":"client-not-connected","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}