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
= { = "0.1", = ["server"] }
## or dev channel
= { = "https://github.com/4t145/rmcp", = "dev" }
Quick start
Start a client in one line:
use ;
use Command;
let client = .serve.await?;
1. Build a transport
use ;
let transport = ;
The transport type must implemented IntoTransport
trait, which allow split into a sink and a stream.
For client, the sink item is ClientJsonRpcMessage
and stream item is ServerJsonRpcMessage
For server, the sink item is ServerJsonRpcMessage
and stream item is ClientJsonRpcMessage
These types is automatically implemented IntoTransport
trait
- The types that already implement both
Sink
andStream
trait. - A tuple of sink
Tx
and streamRx
:(Tx, Rx)
. - The type that implement both [
tokio::io::AsyncRead
] and [tokio::io::AsyncWrite
] trait. - A tuple of [
tokio::io::AsyncRead
]R
and [tokio::io::AsyncWrite
]W
:(R, W)
.
For example, you can see how we build a transport through TCP stream or http upgrade so easily. examples
2. Build a service
You can easily build a service by using ServerHandler
or ClientHandler
.
let service = new;
3. Serve them together
// this call will finish the initialization process
let server = service.serve.await?;
4. 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?;
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;
;
// create a static toolbox to store the tool attributes
// impl call_tool and list_tool by querying static toolbox
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.
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;
Examples
See examples
Features
client
: use client side sdkserver
: use server side sdkmacros
: macros default
Transports
transport-io
: Server stdio transporttransport-sse-server
: Server SSE transporttransport-child-process
: Client stdio transporttransport-sse
: Client sse transport