-
Notifications
You must be signed in to change notification settings - Fork 408
Move whole rmcp crate to offcial rust sdk #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2cfd6f8
get rid of async-trait
4t145 b2b217b
move rmcp to official-rust-sdk
fe6e2b8
replace master with main
6a3f2ba
update readme, change license
4t145 c8fd70d
fix typo
4t145 8678afb
Update README.md
4t145 6532bd5
fix typos
9e8baf8
Merge remote-tracking branch 'upstream/main' into rmcp
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| name: Release | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ release ] | ||
| tags: | ||
| - 'release-*' | ||
| pull_request: | ||
| branches: [ release ] | ||
| env: | ||
| CARGO_TERM_COLOR: always | ||
| ARTIFACT_DIR: release-artifacts | ||
|
|
||
| jobs: | ||
| release: | ||
| name: Release crates | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
|
|
||
| - name: Install Rust | ||
| uses: dtolnay/rust-toolchain@stable | ||
|
|
||
| - name: Cache dependencies | ||
| uses: actions/cache@v3 | ||
| with: | ||
| path: | | ||
| ~/.cargo/registry | ||
| ~/.cargo/git | ||
| target | ||
| key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} | ||
| restore-keys: ${{ runner.os }}-cargo- | ||
|
|
||
| - name: Check formatting | ||
| run: cargo fmt --all -- --check | ||
| - name: Run clippy | ||
| run: cargo clippy --all-targets --all-features -- -D warnings | ||
| - name: Cargo login | ||
| run: cargo login ${{ secrets.CRATES_TOKEN }} | ||
| - name: Publish macros dry run | ||
| run: cargo publish -p rmcp-macros --dry-run | ||
| continue-on-error: true | ||
| - name: Publish rmcp dry run | ||
| run: cargo publish -p rmcp --dry-run | ||
| continue-on-error: true | ||
| - name: Publish macro | ||
| if: ${{ startsWith(github.ref, 'refs/tags/release') }} | ||
| continue-on-error: true | ||
| run: cargo publish -p rmcp-macros | ||
| - name: Publish rmcp | ||
| if: ${{ startsWith(github.ref, 'refs/tags/release') }} | ||
| continue-on-error: true | ||
| run: cargo publish -p rmcp | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,22 @@ | ||
| [workspace] | ||
| members = [ | ||
| "crates/*", | ||
| "examples/clients", | ||
| "examples/servers", | ||
| "examples/macros" | ||
| ] | ||
| members = ["crates/rmcp", "crates/rmcp-macros", "examples/*"] | ||
| resolver = "2" | ||
|
|
||
| [workspace.dependencies] | ||
| mcp-core = { path = "./crates/mcp-core" } | ||
| mcp-macros = { path = "./crates/mcp-macros" } | ||
| rmcp = { version = "0.1.5", path = "./crates/rmcp" } | ||
| rmcp-macros = { version = "0.1.5", path = "./crates/rmcp-macros" } | ||
|
|
||
| [workspace.package] | ||
| edition = "2021" | ||
| version = "1.0.7" | ||
| authors = ["Block <[email protected]>"] | ||
| license = "MIT" | ||
| repository = "https://github.com/modelcontextprotocol/rust-sdk/" | ||
| edition = "2024" | ||
| version = "0.1.5" | ||
| authors = ["4t145 <[email protected]>"] | ||
| license = "MIT/Apache-2.0" | ||
| repository = "https://github.commodelcontextprotocol/rust-sdk/" | ||
| description = "Rust SDK for the Model Context Protocol" | ||
| keywords = ["mcp", "sdk", "tokio", "modelcontextprotocol"] | ||
| homepage = "https://github.com/modelcontextprotocol/rust-sdk" | ||
| categories = [ | ||
| "network-programming", | ||
| "asynchronous", | ||
| ] | ||
| readme = "README.md" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,169 @@ | ||
| # rust-sdk | ||
| The official Rust SDK for the Model Context Protocol | ||
| <div align = "right"> | ||
| <a href="docs/readme/README.zh-cn.md">简体中文</a> | ||
| </div> | ||
|
|
||
| # RMCP | ||
| [](https://crates.io/crates/rmcp) | ||
|  | ||
| [](https://docs.rs/rmcp/latest/rmcp) | ||
|
|
||
| A better and clean rust Model Context Protocol SDK implementation with tokio async runtime. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Import | ||
| ```toml | ||
| rmcp = { version = "0.1", features = ["server"] } | ||
| ## or dev channel | ||
| rmcp = { git = "https://github.com/modelcontextprotocol/rust-sdk", branch = "dev" } | ||
| ``` | ||
|
|
||
| ### Quick start | ||
| Start a client in one line: | ||
| ```rust | ||
| use rmcp::{ServiceExt, transport::TokioChildProcess}; | ||
| use tokio::process::Command; | ||
|
|
||
| let client = ().serve( | ||
| TokioChildProcess::new(Command::new("npx").arg("-y").arg("@modelcontextprotocol/server-everything"))? | ||
| ).await?; | ||
| ``` | ||
|
|
||
| #### 1. Build a transport | ||
|
|
||
| ```rust, ignore | ||
| use tokio::io::{stdin, stdout}; | ||
| let transport = (stdin(), stdout()); | ||
| ``` | ||
|
|
||
| The transport type must implemented [`IntoTransport`](crate::transport::IntoTransport) trait, which allow split into a sink and a stream. | ||
|
|
||
| For client, the sink item is [`ClientJsonRpcMessage`](crate::model::ClientJsonRpcMessage) and stream item is [`ServerJsonRpcMessage`](crate::model::ServerJsonRpcMessage) | ||
|
|
||
| For server, the sink item is [`ServerJsonRpcMessage`](crate::model::ServerJsonRpcMessage) and stream item is [`ClientJsonRpcMessage`](crate::model::ClientJsonRpcMessage) | ||
|
|
||
| ##### These types is automatically implemented [`IntoTransport`](crate::transport::IntoTransport) trait | ||
| 1. The types that already implement both [`Sink`](futures::Sink) and [`Stream`](futures::Stream) trait. | ||
| 2. A tuple of sink `Tx` and stream `Rx`: `(Tx, Rx)`. | ||
| 3. The type that implement both [`tokio::io::AsyncRead`] and [`tokio::io::AsyncWrite`] trait. | ||
| 4. 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](examples/README.md) | ||
|
|
||
| #### 2. Build a service | ||
| You can easily build a service by using [`ServerHandler`](crates/rmcp/src/handler/server.rs) or [`ClientHandler`](crates/rmcp/src/handler/client.rs). | ||
|
|
||
| ```rust, ignore | ||
| let service = common::counter::Counter::new(); | ||
| ``` | ||
|
|
||
| #### 3. Serve them together | ||
| ```rust, ignore | ||
| // 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: | ||
|
|
||
| ```rust, ignore | ||
| // request | ||
| let roots = server.list_roots().await?; | ||
| // or send notification | ||
| server.notify_cancelled(...).await?; | ||
| ``` | ||
|
|
||
| #### 5. Waiting for service shutdown | ||
| ```rust, ignore | ||
| 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](examples/servers/src/common/calculator.rs). | ||
| ```rust, ignore | ||
| 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. | ||
| ```rust, ignore | ||
| let service = service.into_dyn(); | ||
| ``` | ||
|
|
||
|
|
||
| ### Examples | ||
| See [examples](examples/README.md) | ||
|
|
||
| ### Features | ||
| - `client`: use client side sdk | ||
| - `server`: use server side sdk | ||
| - `macros`: macros default | ||
| #### Transports | ||
| - `transport-io`: Server stdio transport | ||
| - `transport-sse-server`: Server SSE transport | ||
| - `transport-child-process`: Client stdio transport | ||
| - `transport-sse`: Client sse transport | ||
|
|
||
| ## Related Resources | ||
| - [MCP Specification](https://spec.modelcontextprotocol.io/specification/2024-11-05/) | ||
|
|
||
| - [Schema](https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.ts) | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.