Skip to main content

Module rmcp

Module rmcp 

Source
Available on crate feature rmcp only.
Expand description

MCP (Model Context Protocol) integration via the rmcp crate.

This module provides McpClientHandler, a client handler that reacts to notifications/tools/list_changed by re-fetching the tool list and updating the ToolServer. Individual MCP tools are registered through the agent and tool-server rmcp_tool builder methods.

§Example

use rig_agent::tool::rmcp::McpClientHandler;
use rig_agent::tool::server::ToolServer;
use rmcp::ServiceExt;

// 1. Create a ToolServer and get a handle
let tool_server_handle = ToolServer::new().run();

// 2. Create a handler that auto-updates tools on list changes
let handler = McpClientHandler::new(client_info, tool_server_handle.clone());

// 3. Connect to the MCP server and register initial tools
let mcp_service = handler.connect(transport).await?;

// 4. Build an agent using the shared tool server handle
let agent = openai_client
    .agent(openai::GPT_5_2)
    .preamble("You are a helpful assistant.")
    .tool_server_handle(tool_server_handle)
    .build();

§Per-call metadata

Rig’s MCP adapter forwards an rmcp::model::Meta (re-exported here as Meta) placed in a ToolContext as the MCP request’s _meta (SEP-1319) — the idiomatic channel for per-call values such as auth tokens, session ids, or A2A context_id/task_id, which the model never sees:

use rig_agent::tool::rmcp::Meta;
use rig_agent::tool::ToolContext;

let mut meta = Meta::new();
meta.0.insert("authorization".into(), serde_json::json!("Bearer …"));
let mut context = ToolContext::new();
context.insert(meta);
let answer = agent.prompt("…").tool_context(context).await?;

§Response metadata

MCP responses retain their protocol data in the per-dispatch ToolContext. Result hooks can inspect the untouched rmcp::model::CallToolResult, its structuredContent as a serde_json::Value, and response Meta with event.tool_context.result::<T>(). These values are host-only; only the response’s ordered presentation content is sent to the model.

Structs§

McpClientHandler
An MCP client handler that automatically re-fetches the tool list when the server sends a notifications/tools/list_changed notification.
Meta
Re-export of rmcp::model::Meta: place one in a ToolContext to have Rig’s MCP registration methods forward it as a call’s _meta.

Enums§

McpClientError
Error type for McpClientHandler operations.

Constants§

DEFAULT_MCP_REFRESH_TIMEOUT
Default deadline for fetching an MCP server’s complete tool list.
DEFAULT_MCP_TOOL_TIMEOUT
Default per-call timeout applied to MCP tools (see issue #1914).