Skip to main content

Crate mcpserver

Crate mcpserver 

Source
Expand description

mcpserver — A Rust library for building MCP (Model Context Protocol) servers.

Implements the MCP 2025-03-26 specification as a pure protocol handler. Define tools and resources in JSON, register async handlers, and call Server::handle() from any HTTP framework, Lambda function, or test harness.

§Quick start

use mcpserver::{Server, FnToolHandler, text_result, JsonRpcRequest};
use serde_json::Value;

let mut server = Server::builder()
    .tools_json(r#"[{"name":"echo","description":"echoes","inputSchema":{"type":"object","properties":{"message":{"type":"string"}},"required":["message"]}}]"#.as_bytes())
    .server_info("my-server", "0.1.0")
    .build();

server.handle_tool("echo", FnToolHandler::new(|args: Value, _context: Value| async move {
    let msg = args.get("message").and_then(|v| v.as_str()).unwrap_or("");
    Ok(text_result(msg))
}));

// Use from any HTTP framework — just deserialize the body and call handle():
// The second argument is request context (e.g. decoded JWT claims).
let req: JsonRpcRequest = serde_json::from_str(r#"{"jsonrpc":"2.0","id":1,"method":"ping"}"#).unwrap();
let resp = server.handle(req, serde_json::json!({})).await;
// resp implements Serialize — pass it to axum::Json, serde_json, etc.
let json = serde_json::to_string(&resp).unwrap();

Re-exports§

pub use loader::load_resources;
pub use loader::load_tools;
pub use loader::parse_resources;
pub use loader::parse_tools;
pub use server::FnToolHandler;
pub use server::ResourceHandler;
pub use server::Server;
pub use server::ServerBuilder;
pub use server::ToolHandler;
pub use types::error_result;
pub use types::new_error_response;
pub use types::text_result;
pub use types::ContentBlock;
pub use types::JsonRpcRequest;
pub use types::JsonRpcResponse;
pub use types::McpError;
pub use types::McpResponse;
pub use types::Resource;
pub use types::ResourceContent;
pub use types::RpcError;
pub use types::Tool;
pub use types::ToolResult;
pub use types::PROTOCOL_VERSION;

Modules§

loader
server
types