# Use MCP tools in agents

> Connect 10xGraph agents to MCP servers with fastmcp.Client over a config dict, stdio or URL, and mix remote MCP tools with local Python tools in one graph.

Source: https://10xgraph.com/docs/guides/use-mcp
Last updated: 2026-11-26

10xGraph agents can call tools from [Model Context Protocol (MCP)](https://modelcontextprotocol.io) servers alongside local Python functions.

## Prerequisites

Install the MCP extra to add fastmcp and mcp libraries:

```bash
pip install "10xgraph[mcp,openai]"
```

The samples use `gpt-4o` and need `OPENAI_API_KEY`.

## Step 1: Create an MCP client

The `fastmcp.Client` class connects to MCP servers and fetches their available tools. It supports three transport modes: a config dict (for multiple servers or HTTP), a local subprocess (stdio), or a remote URL. Choose the one that matches your server.

### Using a config dict for multiple servers or HTTP

If your MCP server is accessed over HTTP or you need to configure multiple servers at once, use a dict with the server config:

```python
from fastmcp import Client

config = {
    "mcpServers": {
        "github": {
            "url": "https://api.githubcopilot.com/mcp/",
            "headers": {"Authorization": "Bearer YOUR_GITHUB_TOKEN"},
            "transport": "streamable-http",
        },
        "filesystem": {
            "command": "python",
            "args": ["mcp_filesystem_server.py"],
        },
    }
}

client = Client(config)
```

### Using a stdio subprocess server

To run an MCP server as a subprocess on your machine, pass the path of the server script and fastmcp starts it over stdio:

```python
from fastmcp import Client

# Starts the server and communicates over stdio
client = Client("my_mcp_server.py")
```

### Using a remote HTTP server

For an MCP server listening on HTTP, pass the URL directly:

```python
from fastmcp import Client

client = Client("https://my-mcp-server.example.com/mcp")
```

## Step 2: Wire the MCP client into ToolNode

Create a `ToolNode` and pass the MCP client. The `ToolNode` lists the server's tools on the first model call and caches the list (see [Tune how a ToolNode uses an MCP server](#tune-how-a-toolnode-uses-an-mcp-server)). If the server cannot be reached, it offers no MCP tools for that call and lists again on the next one.

```python
from tenxgraph.core.graph import ToolNode

tool_node = ToolNode(
    tools=[],       # no local tools, or add them here
    client=client,
)
```

## Step 3: Build and wire the graph

The graph structure is identical to one using local tools. Create an `Agent` that uses the `ToolNode`, add it to the graph alongside a routing function, and compile:

```python
from tenxgraph.core.graph import StateGraph, Agent, ToolNode
from tenxgraph.core.state import AgentState, Message
from tenxgraph.utils import END

tool_node = ToolNode(tools=[], client=client)

agent = Agent(
    model="gpt-4o",
    system_prompt=[{"role": "system", "content": "You are a helpful assistant."}],
    tool_node=tool_node,
)

def should_use_tools(state: AgentState) -> str:
    """Route to tools if the agent requested them."""
    last = state.context[-1] if state.context else None
    if last and last.role == "assistant" and getattr(last, "tools_calls", None):
        return "tools"
    return END

graph = StateGraph()
graph.add_node("agent", agent)
graph.add_node("tools", tool_node)
graph.add_conditional_edges("agent", should_use_tools, {"tools": "tools", END: END})
graph.add_edge("tools", "agent")
graph.set_entry_point("agent")

app = graph.compile()
```

## Step 4: Invoke the agent

Call the compiled graph with a message. The agent will see the MCP tools available and call them when appropriate:

```python
result = app.invoke(
    {"messages": [Message.text_message("List the latest commits in owner/repo-name.")]},
    config={"thread_id": "mcp-demo-1"},
)
print(result["messages"][-1].text())
```

Verify that the agent used the MCP tools by checking the message history for `ToolCallBlock` and `ToolResultBlock` entries.

## Mixing local and remote tools

You can register both local Python functions and MCP tools in the same `ToolNode`. The runtime automatically routes each tool call to the correct backend (local or remote):

```python
# Local functions
def multiply(a: float, b: float) -> float:
    """Multiply two numbers."""
    return a * b

def my_custom_tool(query: str) -> str:
    return f"Custom result: {query}"

# Both local and MCP tools available
tool_node = ToolNode(
    tools=[multiply, my_custom_tool],
    client=client,
)
```

When the model calls a tool, `ToolNode` runs it on the MCP server if the server listed that name, otherwise it runs the local function. An MCP tool with the same name as a local or remote tool raises `ValueError`; rename one or set `mcp_prefix`.

## Tell the MCP server who is calling

Many MCP servers need to know who is calling them (for access control, logging, or per-user state). There are two ways, and they differ in what the server can trust:

| | `pass_user_info_to_mcp=True` | `mcp_auth=` |
|---|---|---|
| What is sent | The caller as unsigned request metadata (`meta["user"]`) | HTTP headers from your function, such as a short-lived signed token |
| Can the server verify it | No | Yes, if the token is signed and the server checks it |
| Use it for | A trusted internal server inside a network boundary you control, or identity that is informational only | A server that enforces per-user or per-tenant access |

In both cases, never add `user`, `user_id`, `org_id` or tenant arguments to MCP tools: arguments are chosen by the model and can be forged.

## Send user context as metadata

Set `pass_user_info_to_mcp=True` on `ToolNode` to send the caller in `config["user"]` to the MCP server. It is sent as request metadata (`meta`) on the MCP call, not as a tool argument, and the server reads it from `ctx.request_context.meta`. Behind the 10xGraph API, `config["user"]` is the verified `Identity`, and the server receives its JSON form: `user_id`, `kind`, `tenant` (for example `{"org_id": "acme"}`), `roles`, `permissions`, `reach` and the token `claims`. A dict you pass yourself is sent as is. If `config` has no `user` but has a `user_id`, the server receives `{"user_id": ...}` instead. If the client's `call_tool` has no `meta` parameter, a warning is logged and the user info is not sent.

```python
tool_node = ToolNode(
    tools=[],
    client=client,
    pass_user_info_to_mcp=True,
)
```

The MCP server reads it from the request metadata, not from a tool argument. Declare a `ctx: Context` parameter on the server tool. FastMCP injects it and it is not part of the schema the model sees. The user dict is under the key `user` in `ctx.request_context.meta`. On fastmcp 4.x `meta` is a dict; on 3.x it is a model, so dump it first:

```python
# Code on the MCP server (fastmcp)
from fastmcp import Context, FastMCP

mcp = FastMCP("secure")

@mcp.tool()
def secure_action(query: str, ctx: Context) -> str:
    meta = ctx.request_context.meta
    if meta is not None and not isinstance(meta, dict):
        meta = meta.model_dump()  # fastmcp 3.x
    user = (meta or {}).get("user") or {}
    if "admin" not in user.get("roles", []):
        raise PermissionError(f"User {user.get('user_id')} does not have permission")
    return f"Action completed by {user.get('user_id')}"
```

Pass the user dict in the invoke config. When requests go through the 10xGraph API server with authentication, the server sets `config["user"]` for you. When you call the graph directly, pass it yourself:

```python
result = app.invoke(
    {"messages": [Message.text_message("Do something.")]},
    config={
        "thread_id": "mcp-auth-1",
        "user": {"user_id": "user-123", "roles": ["admin"]},
    },
)
```

## Authenticate each call with `mcp_auth`

`mcp_auth` is an async function that receives an `MCPAuthContext` (`identity`, `credential`, `tool_name`, `server`, `thread_id`, `run_id`) and returns the HTTP headers for that one MCP request. It runs before every tool listing and tool call. For the tool list, `tool_name` is `None` and `identity` is whichever run asked first, or `None` when nothing supplied a user (for example `refresh_mcp_tools()` called on its own). The list is shared by all users, so for a listing without a user return a token for the agent alone, one the server lets list tools but not call them. It needs fastmcp 4.1.7 or later and an HTTP transport (streamable HTTP or SSE). This example signs a 60 second token that names the user and the agent as the actor; the server checks the signature, `aud` and `exp`, and takes the user from the token:

```python
import os
import time

import jwt  # pip install pyjwt[crypto]

from tenxgraph.core.graph.tool_node import MCPAuthContext

async def mcp_auth(ctx: MCPAuthContext) -> dict[str, str]:
    now = int(time.time())
    claims = {"iss": "support-agent", "aud": "my-mcp-server", "iat": now, "exp": now + 60}
    if ctx.identity is not None:
        # A tool call on behalf of the signed-in user.
        claims |= {
            "sub": ctx.identity.user_id,
            "org_id": (ctx.identity.tenant or {}).get("org_id"),
            "act": {"sub": "support-agent"},
        }
    else:
        # Fetching the tool list: the agent alone, which may list tools but not call them.
        claims |= {"sub": "support-agent", "roles": ["service"]}
    token = jwt.encode(claims, os.environ["MCP_SIGNING_KEY"], algorithm="HS256")
    return {"Authorization": f"Bearer {token}"}

tool_node = ToolNode(tools=[], client=client, mcp_auth=mcp_auth)
```

`ReactAgent`, `PlanActReflectAgent`, `StructuredOutputAgent` and `AudioAgent` take `mcp_auth=` too. The tool list is cached per node and shared by all users, so the server must not return a different tool list per user. Token exchange, `forward_credential` and a longer `RS256` example are in [Calling services as the user](/docs/guides/call-services-as-the-user).

## Tune how a `ToolNode` uses an MCP server

These `ToolNode` options bound what an MCP server can put in front of the model. All are optional.

```python
tool_node = ToolNode(
    tools=[],
    client=client,
    mcp_prefix="github_",             # the model sees github_create_issue, the server gets create_issue
    mcp_max_result_chars=20_000,      # cut one tool result at 20,000 characters
    mcp_max_description_chars=500,    # cut each tool description at 500 characters
    mcp_cache_tools=True,             # default: list the server's tools once
    mcp_connect_timeout=30.0,         # default: seconds to wait for the connection to open
)
```

- **Prefix.** `mcp_prefix` lets two `ToolNode`s expose servers that have a tool with the same name. `mcp_include` and `mcp_exclude` still use the server's own names.
- **Size caps.** A result over `mcp_max_result_chars` is cut and a marker with the number of dropped characters is appended. A description over `mcp_max_description_chars` is cut and ends in `...`. Descriptions go to the model on every call, so a long one costs tokens each turn. Neither cap has a default.
- **Tool list caching.** The tool list is fetched once per `ToolNode` and reused. Tools you add to the server later are not seen until you call `await tool_node.refresh_mcp_tools()`, or you build the node with `mcp_cache_tools=False` to list the server on every model call. A listing that fails is not cached.
- **Connect timeout.** `mcp_connect_timeout` bounds only the connect, never a tool call. Set `None` to wait without a limit.

To check what an MCP tool returns before the model reads it, register an `after_invoke` callback for `InvocationType.MCP`. See [Validate tool, MCP and RAG output](/docs/guides/protect-against-prompt-injection#validate-tool-mcp-and-rag-output). The `END` event of a tool call, which publishers and the run trace see, carries the value `after_invoke` returned, not the raw MCP result.

## Filtering MCP tools by tag

MCP servers can tag their tools with metadata. If the server supports it, you can filter which tools are offered to the Agent using `tools_tags` on the `Agent`. MCP tools are matched against the server's fastmcp tags:

```python
agent = Agent(
    model="gpt-4o",
    tool_node=tool_node,
    tools_tags={"read"},   # only expose tools tagged "read"
)
```

The Agent will only see MCP tools that share at least one of the specified tags. Local tools registered with `@tool(tags=[...])` are filtered the same way; untagged local tools are always offered.

## Testing MCP tools without a server

Pass a `MockMCPClient` from `tenxgraph.qa.testing` where you would pass the `fastmcp.Client`:

```python
from tenxgraph.qa.testing import MockMCPClient

mock_client = MockMCPClient()
mock_client.add_tool(
    name="search",
    description="Search for information",
    parameters={"query": {"type": "string"}},
    handler=lambda query: f"Results for: {query}",
)
tool_node = ToolNode(tools=[], client=mock_client)

# build and run the graph as in Step 3, then:
mock_client.assert_called("search")
```

Pair it with `TestAgent` so the test makes no model call. The call-tracking methods are in [MockMCPClient](/docs/testing/unit-tests#mockmcpclient).

## Using ReactAgent with MCP

`ReactAgent` is a prebuilt agent that includes tool calling built-in. Pass the MCP client directly:

```python
from tenxgraph.prebuilt.agent import ReactAgent

agent = ReactAgent(
    model="gpt-4o",
    tools=[],
    client=client,
    pass_user_info_to_mcp=True,  # trusted internal server; use mcp_auth=... for per-user access control
)
app = agent.compile()
```

## Run it in production

MCP servers run with whatever access you give them, and a tool name that clashes with another tool raises `ValueError` at setup. Pin and review the servers your graph connects to before exposing the agent. See the [Security model](/docs/production/security-model) and [What 10xGraph does not protect](/docs/production/security-model#what-10xgraph-does-not-protect).

## Next steps

- [Build a graph](/docs/guides/build-a-graph) for complete graph construction and routing.
- [Configure Agent](/docs/guides/configure-agent) for all Agent options that work alongside MCP.
- [Use the tool decorator](/docs/guides/use-tool-decorator) to write custom local tools.
- [Test and evaluate](/docs/testing/unit-tests) to integrate MCP tests into your test suite.

## Frequently asked questions

### Do MCP tools run in parallel like local tools?

Yes. The LLM can request multiple MCP tools in one turn, and ToolNode executes them concurrently.
