rust-sdk

The official Rust SDK for the Model Context Protocol

1,707
249
<div align = "right"> <a href="docs/readme/README.zh-cn.md">简体中文(待更新)</a> </div>

RMCP

Crates.io Version

<!-- ![Release status](https://github.com/modelcontextprotocol/rust-sdk/actions/workflows/release.yml/badge.svg) --> <!-- [![docs.rs](todo)](todo) -->

Coverage

An official Rust Model Context Protocol SDK implementation with tokio async runtime.

This repository contains the following crates:

  • rmcp: The core crate providing the RMCP protocol implementation( If you want to get more information, please visit rmcp)
  • rmcp-macros: A procedural macro crate for generating RMCP tool implementations(If you want to get more information, please visit rmcp-macros)

Usage

Import the crate

rmcp = { version = "0.2.0", features = ["server"] }
## or dev channel
rmcp = { git = "https://github.com/modelcontextprotocol/rust-sdk", branch = "main" }

Third Dependencies

Basic dependencies:

Build a Client

<details> <summary>Start a client</summary>
use rmcp::{ServiceExt, transport::{TokioChildProcess, ConfigureCommandExt}};
use tokio::process::Command;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
 let client = ().serve(TokioChildProcess::new(Command::new("npx").configure(|cmd| {
 cmd.arg("-y").arg("@modelcontextprotocol/server-everything");
 }))?).await?;
 Ok(())
}
</details>

Build a Server

<details> <summary>Build a transport</summary>
use tokio::io::{stdin, stdout};
let transport = (stdin(), stdout());
</details> <details> <summary>Build a service</summary>

You can easily build a service by using ServerHandler or ClientHandler.

let service = common::counter::Counter::new();
</details> <details> <summary>Start the server</summary>
// this call will finish the initialization process
let server = service.serve(transport).await?;
</details> <details> <summary>Interact with the server</summary>

Once the server is initialized, you can send requests or notifications:

// request
let roots = server.list_roots().await?;

// or send notification
server.notify_cancelled(...).await?;
</details> <details> <summary>Waiting for service shutdown</summary>
let quit_reason = server.waiting().await?;
// or cancel it
let quit_reason = server.cancel().await?;
</details>

Examples

See examples

OAuth Support

See oauth_support for details.

Related Resources

Related Projects

Development

Tips for Contributors

See docs/CONTRIBUTE.MD to get some tips for contributing.

Using Dev Container

If you want to use dev container, see docs/DEVCONTAINER.md for instructions on using Dev Container for development.

Repository

MO
modelcontextprotocol

modelcontextprotocol/rust-sdk

Created

February 18, 2025

Updated

July 7, 2025

Language

Rust

Category

Developer Tools