Skip to main content

Chapter 15: Creating a Rust MCP Server

TL;DR

In Worka v2, your pack exposes tools from src/lib.rs using the rmcp SDK. Tools are typed, schema‑validated, and executed in‑process inside the WASM runtime.


For developers who want to write high-performance backend logic, Rust is an excellent choice. The Worka ecosystem provides the rmcp crate to make building MCP servers in Rust simple and efficient.

The SDK handles MCP protocol details so you can focus on tool logic.

Step 1: The Tool Function

As we saw in the TL;DR, the core of your server is one or more async functions that will act as your tools. Each tool function must have the same signature:

async fn my_tool_function(req: ToolRequest) -> ToolResponse
  • req: ToolRequest: This struct contains the parameters sent from the client (the Host application). The actual parameters are in req.params, which is a serde_json::Value.
  • -> ToolResponse: Your function must return a ToolResponse, which can be either a success or an error.

Step 2: Handling Parameters and Responses

The recommended way to handle inputs and outputs is to use serde to define structs for them. This gives you type safety and makes your code much cleaner.

Input: You deserialize the generic req.params into your specific struct. It's good practice to handle potential errors if the client sends malformed data.

#[derive(Deserialize)]
struct MyToolParams {
some_input: String,
}

let params: MyToolParams = match serde_json::from_value(req.params) {
Ok(p) => p,
Err(e) => return ToolResponse::invalid_params(e.to_string()),
};

Output: You construct your response struct and use the ToolResponse::success() helper to serialize it to JSON and wrap it in the correct MCP response format.

#[derive(Serialize)]
struct MyToolResponse {
some_output: String,
}

let response_data = MyToolResponse { ... };
ToolResponse::success(json!(response_data))

Step 3: Register tools

Register tools in the server builder so the host can discover them. You do not open ports or run HTTP servers in Worka v2; the host calls your tools directly through the in‑process MCP transport.