RMCP: Rust Model Context Protocol
rmcp is the official Rust implementation of the Model Context Protocol (MCP), a protocol designed for AI assistants to communicate with other services. This library can be used to build both servers that expose capabilities to AI assistants and clients that interact with such servers.
Quick Start
Server Implementation
Creating a server with tools is simple using the #[tool] macro:
use ;
use Arc;
use Mutex;
// Implement the server handler
// Run the server
async
Structured Output
Tools can return structured JSON data with schemas. Use the [Json] wrapper:
# use ;
# use JsonSchema;
# use ;
#
#
#
#
#
#
The #[tool] macro automatically generates an output schema from the CalculationResult type.
Tasks
RMCP implements the task lifecycle from SEP-1686 so long-running or asynchronous tool calls can be queued and polled safely.
- Create: set the
taskfield onCallToolRequestParamto ask the server to enqueue the tool call. The response is aCreateTaskResultthat includes the generatedtask.task_id. - Inspect: use
tasks/get(GetTaskInfoRequest) to retrieve metadata such as status, timestamps, TTL, and poll interval. - Await results: call
tasks/result(GetTaskResultRequest) to block until the task completes and receive either the finalCallToolResultpayload or a protocol error. - Cancel: call
tasks/cancel(CancelTaskRequest) to request termination of a running task.
To expose task support, enable the tasks capability when building ServerCapabilities. The #[task_handler] macro and OperationProcessor utility provide reference implementations for enqueuing, tracking, and collecting task results.
Client Implementation
Creating a client to interact with a server:
use ;
use Command;
async
For more examples, see the examples directory in the repository.
Transport Options
RMCP supports multiple transport mechanisms, each suited for different use cases:
transport-async-rw
Low-level interface for asynchronous read/write operations. This is the foundation for many other transports.
transport-io
For working directly with I/O streams (tokio::io::AsyncRead and tokio::io::AsyncWrite).
transport-child-process
Run MCP servers as child processes and communicate via standard I/O.
Example:
use TokioChildProcess;
use Command;
let transport = new?;
let service = client.serve.await?;
Access with peer interface when handling message
You can get the Peer struct from NotificationContext and RequestContext.
# use ;
# ;
Manage Multi Services
For many cases you need to manage several service in a collection, you can call into_dyn to convert services into the same type.
let service = service.into_dyn;
Feature Flags
RMCP uses feature flags to control which components are included:
client: Enable client functionalityserver: Enable server functionality and the tool systemmacros: Enable the#[tool]macro (enabled by default)- Transport-specific features:
transport-async-rw: Async read/write supporttransport-io: I/O stream supporttransport-child-process: Child process supporttransport-streamable-http-client/transport-streamable-http-server: HTTP streaming (client agnostic, seeStreamableHttpClientTransportfor details)transport-streamable-http-client-reqwest: a defaultreqwestimplementation of the streamable http client
auth: OAuth2 authentication supportschemars: JSON Schema generation (for tool definitions)
Transports
transport-io: Server stdio transporttransport-child-process: Client stdio transporttransport-streamable-http-serverstreamable http server transporttransport-streamable-http-clientstreamable http client transport
The transport type must implement the Transport trait, which allows it to send messages concurrently and receive messages sequentially.
There are 2 pairs of standard transport types:
| transport | client | server |
|---|---|---|
| std IO | TokioChildProcess |
stdio |
| streamable http | StreamableHttpClientTransport |
StreamableHttpService |
IntoTransport trait
IntoTransport is a helper trait that implicitly converts a type into a transport type.
These types automatically implement IntoTransport:
- A type that implements both
futures::Sinkandfutures::Stream, or a tuple(Tx, Rx)whereTxisfutures::SinkandRxisfutures::Stream. - A type that implements both
tokio::io::AsyncReadandtokio::io::AsyncWrite, or a tuple(R, W)whereRistokio::io::AsyncReadandWistokio::io::AsyncWrite. - A type that implements the
Workertrait. - A type that implements the
Transporttrait.
License
This project is licensed under the terms specified in the repository's LICENSE file.