Skip to main content

What is MCP?

Model Context Protocol (MCP) is an open standard that enables communication between AI models and external tools and services. Thousands of MCP servers exist for filesystems, databases, browsers, SaaS APIs, and more. Timbal agents can connect to any MCP server with MCPServer. The server’s tools are discovered at runtime and exposed to the LLM exactly like native tools — schemas, streaming, tracing, and error handling included.
This page covers consuming MCP servers from your agents. For the reverse direction — connecting your editor to the Timbal platform’s MCP server — see MCP Integration.

Basic Usage

Add an MCPServer to the agent’s tools list. Two transports are supported:
That’s it — before each LLM call, the agent lists the server’s tools and offers them to the model alongside any other tools. When the model calls one, the arguments are forwarded to the server and the result comes back as a tool result. With name= set, a tool declared as list_files on the filesystem server appears to the LLM as filesystem__list_files. The bare name is still used on the wire.

The project API

For routes exposed by the TypeScript SDK’s timbalMcp() plugin, use:
The endpoint and authorization resolve at execution time from the current run. Local development uses the API port’s /mcp; deployed projects use the project origin’s /api/mcp. Do not point MCP at the UI origin’s /mcp, capture a caller bearer during module import, or put a project service secret in source. Service mode cannot be combined with a manual URL or headers. Each run gets its own connection: opened on its first discovery or tool call and reused by the run’s other calls. It is closed when the run ends or once it has been idle for 60 seconds, whichever comes first, so the run’s calls share one session only while they are less than 60 seconds apart: a call after a longer gap opens a new connection, and so does background work that outlives the run. Connections are never shared across runs, so concurrent users never share a connection authenticated as someone else, and a run whose credential changes (a refreshed token) gets a new one. Calls outside a run open and close their own. Discovery does not invoke application tools, and tool approvals still apply.

Configuration

Authentication

For servers that require auth, pass headers (http) or environment variables (stdio). Read secrets from the environment instead of hardcoding them:

How Results Are Handled

MCP tool results are converted so the LLM always receives something useful:
  • Text content becomes a plain string (or a list of strings for multiple blocks)
  • Images, audio, and binary resources become Timbal Files, forwarded to the model as file content — vision models can see returned images directly
  • Structured content is returned as-is when the server sends no text representation
  • Errors (isError) raise inside the tool call, so the model receives a proper error tool result and can self-correct

Voice interruption mode

interruption_mode is copied onto every tool this server exposes. The default is allow. On a voice call, disable_during_tool or disable_during_tool_and_turn keeps the caller from barging in through those tools. See Keep a tool from being cut off.

Approval from Annotations

MCP tools carry ToolAnnotations such as readOnlyHint and destructiveHint. readOnlyHint: true marks the tool read_only, so a tool_policy="read_only" run offers it. approval= turns the annotations into a client-side gate that runs through Timbal’s normal approval flow — a server cannot skip it:
  • "destructive" gates tools whose annotations mark them destructive (destructiveHint not false and readOnlyHint not true). Tools with no annotations at all are not gated — gating them would mean gating every tool of every server that never annotated. Note the spec default: a tool that sets any annotation without destructiveHint: false counts as destructive, so a server that annotates readOnlyHint: false on every writer gates every writer. Those same destructive tools also get defer_until_eot=True, whatever approval is set to (including no approval at all). With speculative generation on, every tool call waits until the turn is confirmed, annotated or not, and a resumed turn drops the call before the handler starts. The flag marks work that cannot be undone. It does not change a session that is not speculating.
  • "all" gates every tool.
  • A one-argument callable receives the server’s mcp.types.Tool once, at resolve time, and returns True to gate every call of that tool.
  • A two-argument callable receives the tool and the call’s arguments and is evaluated per call, so one tool that multiplexes subcommands can gate some and wave others through. It may be async and may return a bool, a dict, or an ApprovalPolicyDecision (the same contract as requires_approval):
Gated tools emit an ApprovalEvent with kind="mcp_tool", the server’s input schema as input_schema, and ui={"server", "tool", "title", "annotations"}. Resume exactly like any other approval: resume={approval_id: True}. The gate runs client-side before anything is sent, so it is unaffected by parallel tool calls and survives reconnecting or rebuilding the MCPServer between the pause and the resume — the approval id is derived from the tool path and input, not the session.

Elicitation

Servers can ask the user a question mid tool call (elicitation) — for example, “Swap KB 1069 → 1586 (562 agents → 3)?” before a destructive operation. Timbal advertises the capability by default and bridges each request to suspend(): the run ends with status input_required and an InteractionEvent of kind="mcp_elicitation".
Resume values: Because the tool is re-invoked on resume, the server’s tool must be idempotent up to the elicit() call and its message deterministic for identical input — the suspension id is a hash of the tool path, the server’s message and schema, and the LLM’s tool-call id. Two or more tool calls in flight on the same session cannot be told apart when one of them elicits (the protocol carries no correlation), so the question is answered cancel for it: a call the server then fails gets an error asking the model to call the tool on its own; a call the server answers normally (a server that treats a cancelled confirmation as “not swapped”) succeeds with a note appended to its text result saying the question was auto-cancelled — every call that was in flight carries the note, since which one asked is unknowable. Prefer approval= for confirmations on tools the model is likely to call in parallel. URL-mode elicitations surface payload["url"]; resume with True once the out-of-band flow is complete. Set elicitation=False to hide the capability. Servers should then fail closed on tools that need confirmation rather than proceeding silently.
Stateless HTTP servers cannot do push elicitation or sampling. A 2025-era server that issues no Mcp-Session-Id treats every POST as independent, so the client’s answer lands on a transport that has no idea who asked and the server’s elicit() waits forever. Timbal detects this at connect time (a warning is logged) and, when such a server asks, fails the tool call immediately with a clear error instead of hanging. Use approval= for confirmations against stateless servers. One case is invisible to any client: a server that sends the request without related_request_id drops it before it leaves the server; only timeout= bounds that.

Server Notifications

  • tools/list_changed marks the cached tool list stale. It is refetched at the next run, never mid-run — swapping tools under the model’s feet would also invalidate the prompt cache.
  • Log messages (notifications/message) are forwarded to structlog as "MCP server log" with server, level, logger, and data.
  • Sampling (sampling/createMessage) is served with sampling_model= when set. The call runs as an mcp_sampling LLM span under the in-flight tool call, so its usage shows up in the trace.

Connection Lifecycle

Connections are lazy: nothing is spawned or contacted until the agent first needs the server’s tools. The tool list is fetched once, sorted by name (so the LLM’s tools prefix stays stable for prompt caching), and cached for the life of the connection. Close the connection when you’re done:
For long-running processes (servers, bots), create the MCPServer once and reuse it across runs — reconnecting per request adds latency, especially for stdio servers that spawn a subprocess.
Each connection is owned by a dedicated task, so close() can be called from any task of the event loop that opened it, and in any order across servers. From another event loop it raises RuntimeError without touching the connection, because only the loop that owns it can close it. Call close() there instead:
fragment
A connection whose event loop has closed went with it, and close() from any loop forgets it. With service="api", close() closes the connections of runs on its own loop; those of runs on another loop are left to their run, which closes them when it ends.

One event loop per server

A standalone MCPServer, one that holds its own connection, belongs to the event loop that first uses it, until close() finishes on that loop or the loop closes. Calling one of its tools, or resolve(), from another event loop in the meantime raises RuntimeError. A session and its locks can only be driven from their own loop.
  • Several loops at once (threads that each run their own loop): give each loop its own MCPServer.
  • One loop after another works as usual: a script that calls asyncio.run() several times, or a test suite with a loop per test. Each loop has closed by the time the next one uses the server, which then opens a fresh connection. To hand a server to another loop while the first is still open, close() it on the first loop.
A service="api" server is the exception. It opens a connection per run, on that run’s loop, so one instance can be shared by runs on several loops.
In a process that forks workers (a pre-forking server), open MCP connections after the fork. A child that keeps running the parent’s event loop also runs the parent’s tasks on it, and they can close the parent’s connection: an HTTP session’s DELETE, or a stdio server stopped. A close() the parent had started can also close the connection the child opened since. The child’s MCPServer forgets the parent’s connection and opens one of its own on first use.
Failure handling on top of the SDK:
  • Transport dies mid-call (server process exits, HTTP connection refused): the in-flight call fails immediately with connection lost during tools/call ... instead of hanging, the connection is forgotten, and the next call reconnects. The cached tools stay valid.
  • Response ends before its answer (an HTTP gateway cuts the stream of a server that died mid-call, or a JSON answer is cut short or is not JSON): the call fails at once with the same connection lost during tools/call ... error, instead of waiting for timeout. Other calls on the session are unaffected and nothing reconnects. A stream whose events carried an id is resumed first; the call fails only if resuming does.
  • Stale session (an HTTP server restarted or expired the session while you were idle, so it 404s your Mcp-Session-Id with Session terminated): the session is reset and the request retried once, transparently. Safe for any request — a 404 means the server never saw it.
  • Error status on one request (a 500 from the server, a gateway’s 503, a 429): that call fails with an McpError whose code is the status (MCP server 'name': tools/call failed: HTTP 503 Service Unavailable), and other calls on the session are unaffected. The session is then pinged: if the ping gets an error status too, or the server says it no longer knows the session (a server restarted behind a load balancer, or one that answers an unknown session with 400, as the TypeScript SDK’s examples do), the session is reset and the next call reconnects; otherwise nothing reconnects. During a reset, HTTP teardown gets at most one second, or the request timeout if shorter, to finish the session DELETE. A ping told to try later (429, or 503 with Retry-After) keeps the session, so a rate limit does not fail the other calls. tools/list is asked once more after a reset, a 5xx or a 408.
  • Tools listed after a call. On a fresh session the SDK may request tools/list to validate a completed tool’s result. If a tool appears on several pages, both discovery and result validation use its last definition. While a fresh session is still listing pages, calls share the lookup. If a newer page disagrees with a confirmed schema, the call waits for the remaining pages up to its existing deadline. When a listing ends early, a changed definition can replace the cached one once the client has seen as many occurrences of that tool as in the cached listing. Until then, a known duplicate keeps its last known definition. A tool with no cached definition uses the latest page available. A later call retries an incomplete lookup: the next one at once, then after 1, 2, 4 seconds and so on, up to a minute, until a listing completes; calls in between use what the last listing gave. An incomplete listing that only repeats what a complete one confirmed leaves it confirmed, unless the server announced a change (tools/list_changed) since. If the schema is still missing when the listing fails, the 10-second lookup limit expires, or the call reaches its own timeout, the client returns the completed result, logs a warning, and records mcp_unvalidated ({tool, reason}) in the tool span’s metadata. The listing goes on in the background, shared by the calls that need it, so its schemas are kept once it finishes. Before the next request uses that session, callers share a health check for ambiguous HTTP errors, including 502 and 503, and for a listing that got no answer in time. An expired session or closed writer reconnects directly. Rate limits (429, or 503 with Retry-After) keep the session. If the probe times out, the client replaces an idle session. If another tool is still executing there, the waiting request gets a timeout and a later request can check again.
  • Notifications and answers to the server (notifications/initialized, the reply to a server’s ping): one refused with an error status is dropped with a warning (MCP server refused a message; dropped). When a server refuses a callback reply during a tool call (elicitation, sampling, roots), the client still waits for the tool’s own result. If the stream ends unanswered or the call reaches its deadline, the error includes the callback reply’s HTTP status. A message that cannot be sent at all ends the session, and the next call reconnects.
  • Server unreachable: ConnectionError("MCP server 'name' failed to connect: ConnectError: ...") with the real cause, not anyio’s unhandled errors in a TaskGroup.
  • Server accepts but never answers: timeout= (default 600s, per tool via tool_timeouts=) turns it into a TimeoutError; the session stays up and the next call works. The request’s HTTP response, and any resumption of its event stream, is let go of when its call stops waiting (timed out or cancelled), so a server that keeps a stream open, or ends each resumed stream cleanly, is not read or polled for nobody. The handshake is bounded only by connect_timeout= (default None, but the TCP connect itself is capped at 30s).
  • Slow HTTP tools: the transport’s read timeout is unbounded, so a tools/call that takes minutes completes. (A bare httpx.AsyncClient applied its 5-second default to the SSE read and the SDK swallowed the resulting ReadTimeout, so any HTTP tool slower than 5s hung the call forever.) Connect stays bounded at 30s (or connect_timeout) even when connect_timeout=None.
A tools/call that loses its connection mid-flight is never retried automatically — the tool may already have run. tools/list is.

Dynamic Resolution

MCPServer is a ToolSet: tools are resolved at runtime, not import time. This means the available tools always reflect what the server currently offers, and you can mix MCP servers freely with functions, built-in tools, and other agents in the same tools list. A server that paginates its tools/list (nextCursor) is listed page by page to the last, so every tool it offers is resolved.

Codegen CLI

The codegen CLI can add MCP servers to an agent’s source file:
Re-running add-mcp with the same --name replaces the server spec. Remove a server with remove-tool --name <server_name> — the assignment and import are cleaned up automatically.