Skip to main content

Module mcp_server

Module mcp_server 

Source
Expand description

Runtime-agnostic MCP server support, including optional attachment to ACP sessions. Runtime-agnostic MCP server support.

This module provides infrastructure for serving MCP directly without tying the core SDK to a particular MCP implementation or async runtime. With the unstable_mcp_over_acp feature, the same servers can be attached to ACP session setup requests through the with_mcp_server builder methods. Stable protocol v1 and draft protocol v2 both support global proxy attachment and per-session attachment. V2 uses Proxy.v2().with_mcp_server(...) or V2SessionBuilder::with_mcp_server(...) for new sessions and V2ResumeSessionBuilder::with_mcp_server(...) for resumed sessions. With unstable_session_fork, V2ForkSessionBuilder::with_mcp_server(...) attaches a server to a forked session. V2 attachment additionally requires unstable_protocol_v2.

§Building MCP servers with tools

The opt-in schemars feature provides McpTool, McpToolRegistry and its metadata types, and the tool_fn / tool_fn_mut functions for automatically generating JSON Schemas from Rust input and output types. The agent-client-protocol-rmcp crate provides the builder APIs for MCP tools backed by the rmcp crate and enables this feature.

Custom servers using crate::mcp_server::McpServerConnect, crate::mcp_server::McpServer, and the connection types remain available without schemars, including ACP attachment when unstable_mcp_over_acp is enabled.

§Custom MCP Server Implementations

You can implement crate::mcp_server::McpServerConnect to create custom MCP servers:

ⓘ
use agent_client_protocol::mcp_server::{McpConnectionTo, McpServer, McpServerConnect};
use agent_client_protocol::{DynConnectTo, NullRun, Role, role};

struct MyCustomServer;

impl<R: Role> McpServerConnect<R> for MyCustomServer {
    fn name(&self) -> String {
        "my-custom-server".to_string()
    }

    fn connect(&self, cx: McpConnectionTo<R>) -> DynConnectTo<role::mcp::Client> {
        // Return a component that serves MCP requests
        DynConnectTo::new(my_mcp_component(cx))
    }
}

let server = McpServer::new(MyCustomServer, NullRun);

Structs§

McpConnectionTo
Connection information available to an MCP server.
McpOperationCancellationunstable_mcp_over_acp
Cancellation from the caller, provider removal, or connection shutdown.
McpRequestunstable_mcp_over_acp
One MCP invocation against a reusable service.
McpRequestContextunstable_mcp_over_acp
Authority for a single operation, not for the lifetime of its service.
McpServer
A runtime-agnostic MCP server.
McpToolMetadataschemars
Runtime-neutral metadata for an MCP tool.
McpToolRegistryschemars
Runtime-neutral registry for MCP tools.
RegisteredMcpToolschemars
A registered MCP tool that can be dispatched with erased JSON values.

Enums§

EnabledToolsschemars
Tracks which tools are enabled.
McpConnectionContext
Describes how an MCP server connection was established.
McpOutcomeunstable_mcp_over_acp
An MCP outcome, distinct from failure of the ACP binding.

Constants§

MCP_BACKEND_FAILUREunstable_mcp_over_acp
The MCP backend failed independently of an MCP application error.
MCP_SERVER_UNAVAILABLEunstable_mcp_over_acp
The declared MCP provider is no longer available.

Traits§

McpServerConnect
Trait for types that can create MCP server connections.
McpServiceunstable_mcp_over_acp
Reusable MCP application state with owned invocation futures.
McpToolschemars
Trait for defining MCP tools.

Functions§

tool_fnschemars
Create a stateless function-backed MCP tool and its concurrent runner.
tool_fn_mutschemars
Create a “single-threaded” function-backed MCP tool and its runner.

Type Aliases§

McpToolSchemaschemars
JSON Schema object used to describe MCP tool inputs and outputs.