🦀 MCP in Rust: A Practical Guide using rmcp
This guide demonstrates how to use the Model Context Protocol (MCP) in Rust with the rmcp
crate. MCP allows AI systems to interact with tools via JSON-RPC 2.0. Rust's performance and safety make it a great choice for building these tools.
We'll build several example MCP servers, from simple file operations to more complex KYC tasks.
1. Environment Setup
First, add the necessary dependencies to your Cargo.toml
:
2. General MCP Server Examples
Let's create some basic tools exposed via MCP.
2.1. File Explorer
This tool provides functions to list directory contents and read files.
#[tool_box]
marks the impl
block, making its methods discoverable MCP tools.#[tool(description = "...")]
provides a human-readable description for the AI model.#[derive(Deserialize, JsonSchema)]
on PathRequest
allows serde
to parse incoming JSON into the struct and schemars
to generate a schema for validation/description.#[tool(aggr)]
tells rmcp
to map the incoming JSON parameters object directly to the fields of the PathRequest
struct.std::fs
functions are used for the core logic. anyhow::Result
simplifies error handling.2.2. Currency Converter
This tool uses an external API to convert currencies.
async fn
because reqwest::get
is an asynchronous operation. Requires an async runtime like tokio
.serde
to deserialize the JSON response from the API into the ApiResponse
struct.2.3. Task Manager
A stateful tool to manage a simple list of tasks.
Arc<Mutex<Vec<Task>>>
for sharing the task list safely across concurrent requests (if the server runs multithreaded).list_tasks
returns the list of Task
structs directly. rmcp
handles serializing this into the JSON-RPC response.2.4. Weather Forecast
Fetches weather from the wttr.in
service.
async fn
and reqwest
for an external HTTP request.wttr.in
.2.5. JSON Formatter
A simple tool to prettify a JSON string.
#[tool(param)]
for methods expecting a single unnamed parameter in the params
field of the JSON-RPC request (e.g., "params": "{\"key\":\"value\"}"
).serde_json
to parse and then re-serialize the JSON data with pretty printing.3. KYC Tool Set — Servers & Client Interaction
Now, let's look at more specialized tools, common in KYC (Know Your Customer) processes. We'll show both the server-side tool definition and how a client might call it.
(Note: These KYC examples are illustrative and simplified.)
3.1. Identity OCR Tool
Server: Extracts text from an image file using the external tesseract
OCR tool.
std::process::Command
to execute the tesseract
command-line tool.tesseract
to be installed on the server machine where this Rust code runs.Client: Example of calling this tool using rmcp
client (raw request).
serde_json::json!
.client.send_raw
. The result
will be the full JSON-RPC response { "jsonrpc": "2.0", "id": 1, "result": "extracted text..." }
or an error object.3.2. Bank Account Verification
Server: A simplified check matching name and IBAN details.
Client: Example call.
verify_bank
method with appropriate parameters.4. Conclusion
You've seen how to create various MCP tools in Rust using the rmcp
crate, covering stateless and stateful servers, interaction with external APIs, and running external processes. The #[tool_box]
and #[tool]
attributes, combined with serde
and schemars
, provide a powerful way to expose Rust functions to AI models or other systems speaking JSON-RPC 2.0.