huggingface/smolagents · error · ValueError

Loading tools from MCP requires you to acknowledge you trust

Error message

Loading tools from MCP requires you to acknowledge you trust the MCP server, as it will execute code on your local machine: pass `trust_remote_code=True`.

What it means

Loading tools from an MCP server executes code from that server on your local machine, so smolagents requires explicit opt-in. from_mcp refuses to proceed unless `trust_remote_code=True` is passed, mirroring the transformers convention for remote code.

Source

Thrown at src/smolagents/tools.py:1053

        try:
            from mcpadapt.core import MCPAdapt
            from mcpadapt.smolagents_adapter import SmolAgentsAdapter
        except ImportError:
            raise ImportError(
                """Please install 'mcp' extra to use ToolCollection.from_mcp: `pip install 'smolagents[mcp]'`."""
            )
        if isinstance(server_parameters, dict):
            transport = server_parameters.get("transport")
            if transport is None:
                transport = "streamable-http"
                server_parameters["transport"] = transport
            if transport not in {"sse", "streamable-http"}:
                raise ValueError(
                    f"Unsupported transport: {transport}. Supported transports are 'streamable-http' and 'sse'."
                )
        if not trust_remote_code:
            raise ValueError(
                "Loading tools from MCP requires you to acknowledge you trust the MCP server, "
                "as it will execute code on your local machine: pass `trust_remote_code=True`."
            )
        with MCPAdapt(server_parameters, SmolAgentsAdapter(structured_output=structured_output)) as tools:
            yield cls(tools)


def tool(tool_function: Callable) -> Tool:
    """
    Convert a function into an instance of a dynamically created Tool subclass.

    Args:
        tool_function (`Callable`): Function to convert into a Tool subclass.
            Should have type hints for each input and a type hint for the output.
            Should also have a docstring including the description of the function
            and an 'Args:' part where each argument is described.
    """
    tool_json_schema = get_json_schema(tool_function)["function"]

View on GitHub (pinned to 30bb116109)

Solutions

  1. Pass `trust_remote_code=True` once you have verified you trust the MCP server
  2. Audit the MCP server's tool code before enabling the flag
  3. Pin the MCP server URL to a known, TLS-protected endpoint when enabling trust

Example fix

# before
tools = ToolCollection.from_mcp({"url": "http://localhost:8000/mcp"})
# after
tools = ToolCollection.from_mcp({"url": "http://localhost:8000/mcp"}, trust_remote_code=True)
Defensive patterns

Strategy: validation

Validate before calling

if not TRUSTED_MCP_SERVERS:  # your allowlist of vetted server URLs
    raise RuntimeError("Refusing to load untrusted MCP tools")
tools = ToolCollection.from_mcp(server_parameters, trust_remote_code=True)

Try / catch

try:
    ToolCollection.from_mcp(params, trust_remote_code=True)
except ValueError as e:
    if "trust_remote_code" in str(e):
        # explicit human decision point before retrying
        raise PermissionError("MCP server not on trusted allowlist") from e
    raise

Prevention

When it happens

Trigger: Calling `ToolCollection.from_mcp(server_parameters)` without the `trust_remote_code=True` keyword argument.

Common situations: Copy-pasting examples that omit the flag; automated pipelines where the flag was dropped during refactor; users unaware of the security implications of MCP tool loading.

Related errors


AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28). Data as JSON: /api/errors/347a16b100150bda. Report an issue: GitHub.