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 Future;
use Arc;
use Mutex;
// Implement the server handler
// Run the server
async
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
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, see [StreamableHttpClientTransport] for 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
| transport | client | server |
|---|---|---|
| std IO | [child_process::TokioChildProcess] |
[io::stdio] |
| streamable http | [streamable_http_client::StreamableHttpClientTransport] |
[streamable_http_server::session::create_session] |
IntoTransport trait
[IntoTransport] is a helper trait that implicitly convert a type into a transport type.
These types is automatically implemented [IntoTransport] trait
- A type that already implement both [
futures::Sink] and [futures::Stream] trait, or a tuple(Tx, Rx)whereTxis [futures::Sink] andRxis [futures::Stream]. - A type that implement both [
tokio::io::AsyncRead] and [tokio::io::AsyncWrite] trait. or a tuple(R, W)whereRis [tokio::io::AsyncRead] andWis [tokio::io::AsyncWrite]. - A type that implement Worker trait.
- A type that implement [
Transport] trait.
License
This project is licensed under the terms specified in the repository's LICENSE file.