Skip to content

Commit d0962ec

Browse files
authored
docs: add an overview to rmcp/src/lib.rs (#116)
1 parent 1fc5be6 commit d0962ec

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

crates/rmcp/src/lib.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,92 @@
1+
//! The official Rust SDK for the Model Context Protocol (MCP).
2+
//!
3+
//! The MCP is a protocol that allows AI assistants to communicate with other
4+
//! services. `rmcp` is the official Rust implementation of this protocol.
5+
//!
6+
//! There are two ways in which the library can be used, namely to build a
7+
//! server or to build a client.
8+
//!
9+
//! ## Server
10+
//!
11+
//! A server is a service that exposes capabilities. For example, a common
12+
//! use-case is for the server to make multiple tools available to clients such
13+
//! as Claude Desktop or the Cursor IDE.
14+
//!
15+
//! For example, to implement a server that has a tool that can count, you would
16+
//! make an object for that tool and add an implementation with the `#[tool(tool_box)]` macro:
17+
//!
18+
//! ```rust
19+
//! use std::sync::Arc;
20+
//! use rmcp::{Error as McpError, model::*, tool};
21+
//! use tokio::sync::Mutex;
22+
//!
23+
//! #[derive(Clone)]
24+
//! pub struct Counter {
25+
//! counter: Arc<Mutex<i32>>,
26+
//! }
27+
//!
28+
//! #[tool(tool_box)]
29+
//! impl Counter {
30+
//! fn new() -> Self {
31+
//! Self {
32+
//! counter: Arc::new(Mutex::new(0)),
33+
//! }
34+
//! }
35+
//!
36+
//! #[tool(description = "Increment the counter by 1")]
37+
//! async fn increment(&self) -> Result<CallToolResult, McpError> {
38+
//! let mut counter = self.counter.lock().await;
39+
//! *counter += 1;
40+
//! Ok(CallToolResult::success(vec![Content::text(
41+
//! counter.to_string(),
42+
//! )]))
43+
//! }
44+
//! }
45+
//! ```
46+
//!
47+
//! Next also implement [ServerHandler] for `Counter` and start the server inside
48+
//! `main` by calling `Counter::new().serve(...)`. See the examples directory in the repository for more information.
49+
//!
50+
//! ## Client
51+
//!
52+
//! A client can be used to interact with a server. Clients can be used to get a
53+
//! list of the available tools and to call them. For example, we can `uv` to
54+
//! start a MCP server in Python and then list the tools and call `git status`
55+
//! as follows:
56+
//!
57+
//! ```rust
58+
//! use anyhow::Result;
59+
//! use rmcp::{model::CallToolRequestParam, service::ServiceExt, transport::TokioChildProcess};
60+
//! use tokio::process::Command;
61+
//!
62+
//! async fn client() -> Result<()> {
63+
//! let service = ()
64+
//! .serve(TokioChildProcess::new(
65+
//! Command::new("uvx").arg("mcp-server-git"),
66+
//! )?)
67+
//! .await?;
68+
//!
69+
//! // Initialize
70+
//! let server_info = service.peer_info();
71+
//! println!("Connected to server: {server_info:#?}");
72+
//!
73+
//! // List tools
74+
//! let tools = service.list_tools(Default::default()).await?;
75+
//! println!("Available tools: {tools:#?}");
76+
//!
77+
//! // Call tool 'git_status' with arguments = {"repo_path": "."}
78+
//! let tool_result = service
79+
//! .call_tool(CallToolRequestParam {
80+
//! name: "git_status".into(),
81+
//! arguments: serde_json::json!({ "repo_path": "." }).as_object().cloned(),
82+
//! })
83+
//! .await?;
84+
//! println!("Tool result: {tool_result:#?}");
85+
//!
86+
//! service.cancel().await?;
87+
//! Ok(())
88+
//! }
89+
//! ```
190
mod error;
291
pub use error::Error;
392

0 commit comments

Comments
 (0)