RMCP
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
= { = "https://github.com/4t145/rust-mcp-sdk", = ["server"] }
Quick start
1. Build a transport
- A transport type for client should be a
Sink
ofClientJsonRpcMessage
and aStream
ofServerJsonRpcMessage
- A transport type for server should be a
Sink
ofServerJsonRpcMessage
and aStream
ofClientJsonRpcMessage
We already have some transport type or builder function in rmcp::transport
.
use async_rw;
use ;
let transport = async_rw;
2. Build a service
You can easily build a service by using ServerHandlerService
or ClientHandlerService
.
use ServerHandlerService;
let service = new;
You can reference the server examples.
3. Serve them together
// this call will finishe the initialization process
let server = serve_server.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 ;
use Counter;
;
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 sdkserver
: use server side sdk