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.
wait for the first release.
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
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-sse-client
/transport-sse-server
: SSE support (client agnostic)transport-sse-client-reqwest
: a defaultreqwest
implementation of the SSE client
transport-streamable-http-client
/transport-streamable-http-server
: HTTP streaming (client agnostic, see [StreamableHttpClientTransport
] for details)transport-streamable-http-client-reqwest
: a defaultreqwest
implementation of the streamable http client
auth
: OAuth2 authentication supportschemars
: JSON Schema generation (for tool definitions)
Transports
transport-io
: Server stdio transporttransport-sse-server
: Server SSE transporttransport-child-process
: Client stdio transporttransport-sse-client
: Client sse transporttransport-streamable-http-server
streamable http server transporttransport-streamable-http-client
streamable 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 ] |
sse | [sse_client::SseClientTransport ] |
[sse_server::SseServer ] |
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)
whereTx
is [futures::Sink
] andRx
is [futures::Stream
]. - A type that implement both [
tokio::io::AsyncRead
] and [tokio::io::AsyncWrite
] trait. or a tuple(R, W)
whereR
is [tokio::io::AsyncRead
] andW
is [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.