rig-mcp-server
Serve Rig agents and tools over the
Model Context Protocol, so any MCP client can use them:
Claude Code, Cursor, Zed, Claude Desktop, or another agent. Built on the official MCP
Rust SDK, rmcp.
Rig ships an MCP client (rig-rmcp, Rig agents using MCP tools). This crate is the
other direction: Rig agents as MCP tools.
Dependencies for the first example:
[]
= "0.47"
= "1"
= { = "1", = ["derive"] }
= "1"
use Agent;
use ;
use ;
use JsonSchema;
use ;
use ;
/// Adds two numbers. A plain Rig tool: it runs without a model.
;
async
What a client sees
| Exposed | As an MCP tool |
|---|---|
agent(agent) |
Named after the agent (made provider-safe), described by its description. Takes message and an optional session_id. Answers with its text and any images it made, plus {answer, session_id} as structured content; pass the session_id back to continue the conversation. |
typed_agent::<T>(agent) |
Same input; runs as prompt_typed::<T> and answers with the T as structured content, declared by the tool's outputSchema (the JSON schema of T). |
tool(t) / dynamic_tool(t) |
The Rig tool as itself: name, description, parameters and annotations (readOnlyHint, destructiveHint, ...). Runs without a model; its failure is an error result the model reads. A lone JSON object it returns is also structured content. |
While an agent writes, its text streams to the client as progress notifications (when the client asks for progress). When the client cancels a call, the agent run stops.
An agent with tools needs AgentBuilder::default_max_turns(n) with n >= 2. Without it the run stops after its first model call, and the error says so.
Resources and prompts
Besides tools, a server can offer fixed resources (resources/list and
resources/read) and prompts (prompts/list and prompts/get). A server that has
none of either does not advertise those capabilities.
use HashMap;
use McpServer;
use ;
resource(uri, name, mime_type, text)offers text.binary_resource(uri, name, mime_type, bytes)offers bytes, sent base64-encoded.prompt(name, description, arguments, render)offers a prompt.rendergets the arguments the client passed, all strings keyed by name, and returns the messages. A call without a required argument gets an invalid-params error.
Resource subscriptions and list-changed notifications are not supported. Resource templates are opt-in, see below. Argument completion is opt-in, see below.
Resource templates
resource_template(uri_template, name, description, mime_type, read) offers a
template, listed by resources/templates/list. Any URI that matches it is read with
read, which gets the variables the URI matched and returns the text. Variables are
written {name}: each matches one path segment (no literal /), and its value is
percent-decoded (%2F reads as / inside the value). Only simple variables are
supported, not the RFC 6570 operators such as {+path}. A URI whose value is not
valid UTF-8 after decoding is not found.
use ;
let server = new.resource_template;
A static resource with the same URI wins over a template. When two templates match a
URI, the one registered first is read. A malformed template, or
two with the same text, is refused when the server is served (BuildError). An error
from read reaches the client as a fixed internal error. With a completer, the
template's text is a ref/resource target and the argument is one of its variables.
resource_template_presented takes a Presentation as well.
Argument completion
completer(f) answers completion/complete for prompt arguments and resources. f
is async. It gets a CompletionRequest (the prompt or resource, the argument name, what
the client has typed, and the arguments already resolved) and returns the candidates.
use ;
- A prompt name, argument name or resource URI the server does not offer is refused
with invalid params (
-32602) beforefruns. - Without a completer the
completionscapability is not advertised, andcompletion/completeis refused as an unknown method. - The server sends at most 100 values. When it cuts the list,
hasMoreistrueandtotalis the full count. - An error from
freaches the client as an internal error with the messagecompletion failed. Its text is logged by kind only and not sent.
Titles and icons
The server, and each tool, resource and prompt, can carry a human-readable title and icons (MCP 2025-11-25). They are opt-in: nothing is sent unless you set it.
use ;
use Icon;
title(..)andicon(..)on the server set them ininitialize; callicononce per icon.tool_presented,dynamic_tool_presented,resource_presentedandprompt_presentedtake aPresentation(a title and icons) after the arguments their plain form takes.tool,resourceandpromptare the same with nothing set.- An icon
srcmust be anhttp://orhttps://URL or adata:URI. Serving fails withBuildError::InvalidIconotherwise.
Binary resources do not take a title or icons yet.
Transports
use ;
// stdio: a local client launches the binary. Stdout carries the protocol,
// so log to stderr only.
async
// Streamable HTTP at /mcp, as an Axum router.
async
// Any rmcp transport, such as a reader and writer pair or a socket.
async
Over HTTP the endpoint answers only to localhost, 127.0.0.1 and ::1 by default,
which keeps a local server safe from DNS rebinding; set allowed_hosts to the public
host name when deploying. With bearer_token, calls without the token get HTTP 401.
A request body over max_request_bytes (4 MiB by default) gets HTTP 413.
In Claude Code
Over HTTP, with the token from bearer_token:
Sessions and limits
Agent sessions live in memory by default (at most 1000, oldest forgotten first), or in any
rig-memory store with history_store (DirStore
on disk, rig-sqlite, rig-postgres), so they survive restarts. Each call sends the
model only the newest messages that fit history_window (32k tokens by default). A
session_id must be 1-128 of [A-Za-z0-9_-].
Every call to an agent spends model tokens, so a server bounds them:
| Knob | Default | Beyond it |
|---|---|---|
max_concurrent_calls |
32 | an error result saying the server is busy, before any model call |
max_run_duration |
10 min | the run is stopped, an error result |
bearer_token (HTTP) |
none | HTTP 401 |
max_request_bytes (HTTP) |
4 MiB | HTTP 413, before the body is parsed |
Each agent call runs in an mcp.agent_call tracing span (with mcp.session_id); Rig's
spans for the model calls and tools nest under it.
On AgentCore
AWS Bedrock AgentCore Runtime runs an MCP server as an ARM64 container on 0.0.0.0:8000,
serving /mcp. It recommends a GET /ping health route, which AgentCore uses to decide
whether the session is alive. Call ping_route() to serve it:
use ;
/ping answers {"status": "Healthy"} with HTTP 200 and takes no bearer token. It is off by
default, so a router that already has its own /ping does not clash with it. Set the public
host name with allowed_hosts as usual; /mcp still checks Host. /ping answers any
Host by design: it reveals only that the server is up. With ping_route on, merging a
router that has its own /ping panics (a duplicate axum route).
Tracing
With the otel feature (through rig-otel, no exporters of its own), a
call continues the client's trace, so the client, the mcp.agent_call span and Rig's
model and tool spans under it make one trace. The W3C traceparent, tracestate and
baggage are read from the HTTP request headers (Streamable HTTP), else from the
request's _meta (any transport, stdio too; the SEP-414 convention). Plain tools run in an
mcp.tool_call span that continues the trace the same way.
Every span of an agent call carries session.id: the
X-Amzn-Bedrock-AgentCore-Runtime-Session-Id header when AWS Bedrock AgentCore Runtime
sends one, else the call's own session_id. Export the spans with rig_otel::init().
= { = "0.47", = ["otel"] }
Content recording
Prompts and replies go on the agent's spans only when it records content. It is off by
default, as in Rig. Set it on the agent you pass in with
AgentBuilder::record_content_telemetry(true).
Testing
cargo test drives the server with the official rmcp client in memory: listing,
schemas and annotations, plain tools, agent sessions, streamed progress, typed answers,
cancellation, time limits, resources and prompts (listing, reads, rendering, missing
arguments, capabilities), HTTP with and without the token, and a Rig agent using the
served tools through rig-rmcp (Rig to MCP to Rig).
interop/run.sh checks the server against an independent implementation, the official
MCP Python SDK, over stdio and Streamable HTTP, keyless (cargo run --example mcp_echo_server). CI runs both, plus MSRV and cargo-deny.
Not supported
resource subscriptions, list-changed notifications, sampling and elicitation; the SEP-2663 tasks extension (long calls run to completion, reporting progress); OAuth (bearer only).