MCP ExplorerExplorer

Rust Sdk

1441 MIT
FreeCommunity
AI Systems
The official Rust SDK for the Model Context Protocol

Overview

What is Rust Sdk

RMCP is an official Rust SDK implementation of the Model Context Protocol that utilizes the Tokio async runtime, allowing for efficient asynchronous programming in Rust applications. It is designed for building clients and servers that adhere to the RMCP specifications.

Use cases

RMCP can be used in various scenarios where asynchronous communication is required, such as implementing server-client architectures, handling notifications, and processing requests in real-time applications, particularly in contexts involving model data and collaborative environments.

How to use

To use RMCP, you need to include it in your project’s Cargo.toml file, either as a stable version or from the main branch on GitHub. You can start a client by setting up a transport with a Tokio subprocess and initializing a service for the server with proper input/output streams, followed by handling requests and notifications once the server is running.

Key features

Key features of RMCP include a core protocol implementation that facilitates asynchronous communication, a procedural macro crate for generating tool implementations, and built-in support for handling requests and notifications between clients and servers efficiently.

Where to use

RMCP is suitable for use in distributed systems, microservices architecture, or any application that requires a standardized protocol for exchanging information between different components or services in an asynchronous manner.

Content

RMCP

Wait for the first release.

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.1", features = ["server"] }
## or dev channel
rmcp = { git = "https://github.com/modelcontextprotocol/rust-sdk", branch = "main" }

Third Dependencies

Basic dependencies:

Build a Client

Start a client
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(())
}

Build a Server

Build a transport
use tokio::io::{stdin, stdout};
let transport = (stdin(), stdout());
Build a service

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

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

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?;
Waiting for service shutdown
let quit_reason = server.waiting().await?;
// or cancel it
let quit_reason = server.cancel().await?;

Examples

See examples

OAuth Support

See oauth_support for details.

Related Resources

Related Projects

Development with Dev Container

See docs/DEVCONTAINER.md for instructions on using Dev Container for development.

Tools

No tools

Comments