* feat: created transport trait and impl * feat(transport): abstract sse transport from the client definition * test: add test with js server (#65) * fix: use prefix to check mime type * fix(test): fix tool deserialization error (#68) * ci: add documentation generation job (#59) * ci: add documentation generation job 1. add doc ci in workflow 2. remove the readme in rmcp crate Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn> * docs: fix doc test in README.md 1) fix doc test in readme 2) fix some fmt Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn> --------- Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn> * fix(notification): fix wrongly error report in notification (#70) * chore: fix lint errors by creating a type alias * chore: resolve conflict * chore: fix fmt * chore: revert --------- Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn> Co-authored-by: Jefry Dewangga <jefrydco@gmail.com> Co-authored-by: 4t145 <u4t145@163.com> Co-authored-by: jokemanfire <hu.dingyang@zte.com.cn> |
||
|---|---|---|
| .github/workflows | ||
| crates | ||
| docs/readme | ||
| examples | ||
| .gitignore | ||
| Cargo.toml | ||
| LICENSE | ||
| README.md | ||
| rust-toolchain.toml | ||
| rustfmt.toml | ||
RMCP
An official rust Model Context Protocol SDK implementation with tokio async runtime.
Usage
Import
rmcp = { version = "0.1", features = ["server"] }
## or dev channel
rmcp = { git = "https://github.com/modelcontextprotocol/rust-sdk", branch = "main" }
Quick start
Start a client in one line:
use rmcp::{ServiceExt, transport::TokioChildProcess};
use tokio::process::Command;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ().serve(
TokioChildProcess::new(Command::new("npx").arg("-y").arg("@modelcontextprotocol/server-everything"))?
).await?;
Ok(())
}
1. Build a transport
use tokio::io::{stdin, stdout};
let transport = (stdin(), stdout());
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
SinkandStreamtrait. - A tuple of sink
Txand streamRx:(Tx, Rx). - The type that implement both [
tokio::io::AsyncRead] and [tokio::io::AsyncWrite] trait. - A tuple of [
tokio::io::AsyncRead]Rand [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 = common::counter::Counter::new();
3. Serve them together
// this call will finish the initialization process
let server = service.serve(transport).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 rmcp::{ServerHandler, model::ServerInfo, schemars, tool};
use super::counter::Counter;
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
pub struct SumRequest {
#[schemars(description = "the left hand side number")]
pub a: i32,
#[schemars(description = "the right hand side number")]
pub b: i32,
}
#[derive(Debug, Clone)]
pub struct Calculator;
// create a static toolbox to store the tool attributes
#[tool(tool_box)]
impl Calculator {
// async function
#[tool(description = "Calculate the sum of two numbers")]
async fn 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 right hand side number")]
b: i32,
) -> String {
(a - b).to_string()
}
}
// impl call_tool and list_tool by querying static toolbox
#[tool(tool_box)]
impl ServerHandler for Calculator {
fn get_info(&self) -> ServerInfo {
ServerInfo {
instructions: Some("A simple calculator".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.
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