rmcp-macros 0.1.1

Rust SDK for Model Context Protocol macros library
Documentation

RMCP

Crates.io Version Release status docs.rs

A better and clean rust Model Context Protocol SDK implementation with tokio async runtime.

Comparing to official SDK

The Official SDK has too much limit and it was originally built for goose rather than general using purpose.

All the features listed on specification would be implemented in this crate. And the first and most important thing is, this crate has the correct and intact data types. See it yourself.

Usage

Import from github

rmcp = { git = "https://github.com/4t145/rust-mcp-sdk", features = ["server"] }

Quick start

1. Build a transport

  • A transport type for client should be a Sink of ClientJsonRpcMessage and a Stream of ServerJsonRpcMessage
  • A transport type for server should be a Sink of ServerJsonRpcMessage and a Stream of ClientJsonRpcMessage

We already have some transport type or builder function in rmcp::transport.

use rmcp::transport::io::async_rw;
use tokio::io::{stdin, stdout};
let transport = async_rw(stdin(), stdout());

2. Build a service

You can easily build a service by using ServerHandlerService or ClientHandlerService.

use rmcp::ServerHandlerService;
let service = ServerHandlerService::new(common::counter::Counter::new());

You can reference the server examples.

3. Serve them together

// this call will finishe the initialization process
let server = rmcp::serve_server(service, transport).await?;

4. Get remote interface by peer()

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

// or send notification
server.peer().notify_cancelled(...).await?;

For client, you will get server's api. And for server, you will get client api.

5. Waiting for service shutdown

let quit_reason = server.waiting().await?;
// or cancel it
let quit_reason = server.cancel().await?;

Use marcos to declaring tool

Use toolbox and tool macros to create tool quickly.

Check this file.

use rmcp::{ServerHandler, model::ServerInfo, schemars, tool, tool_box};

use super::counter::Counter;

#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
pub struct SumRequest {
    #[schemars(description = "the left hand side number")]
    pub a: i32,
    pub b: i32,
}
#[derive(Debug, Clone)]
pub struct Calculater;
impl Calculater {
    // async function
    #[tool(description = "Calculate the sum of two numbers")]
    fn async sum(&self, #[tool(aggr)] SumRequest { a, b }: SumRequest) -> String {
        (a + b).to_string()
    }

    // sync function
    #[tool(description = "Calculate the sum of two numbers")]
    fn sub(
        &self,
        #[tool(param)]
        // this macro will transfer the schemars and serde's attributes
        #[schemars(description = "the left hand side number")]
        a: i32,
        #[tool(param)]
        #[schemars(description = "the left hand side number")]
        b: i32,
    ) -> String {
        (a - b).to_string()
    }

    // create a static toolbox to store the tool attributes
    tool_box!(Calculater { sum, sub });
}

impl ServerHandler for Calculater {
    // impl call_tool and list_tool by quering static toolbox
    tool_box!(@derive);
    
    fn get_info(&self) -> ServerInfo {
        ServerInfo {
            instructions: Some("A simple caculator".into()),
            ..Default::default()
        }
    }
}

The only thing you should do is to make the function's return type implement IntoCallToolResult.

And you can just implement IntoContents, and the return value will be marked as success automatically.

If you return a type of Result<T, E> where T and E both implemented IntoContents, it's also OK.

Examples

See examples

Features

  • client: use client side sdk
  • server: use server side sdk

Related Resources