Skip to main content

Module mcp

Module mcp 

Source
Expand description

Model Context Protocol (MCP) server — HTTP Streamable HTTP transport.

McpServer implements Application so it can be passed directly to [Server::run]. Unmatched requests fall through to the built-in App controller chain (static files, health probes, etc.).

§Quick start

use rust_web_server::server::Server;
use rust_web_server::mcp::{McpServer, McpContent, PromptMessage};
let mcp = McpServer::new("my-server", "1.0")
    // A tool: callable by the AI, like a function
    .tool(
        "echo",
        "Echo text back",
        r#"{"type":"object","properties":{"text":{"type":"string"}},"required":["text"]}"#,
        |args| {
            let text = rust_web_server::mcp::extract_arg(args, "text")
                .unwrap_or_else(|| "(nothing)".to_string());
            Ok(McpContent::text(text))
        },
    )
    // A resource: data the AI can read by URI
    .resource(
        "docs://{topic}",
        "Documentation",
        "Return documentation for a topic",
        |uri| Ok(McpContent::text(format!("Documentation for: {uri}"))),
    )
    // A prompt template: reusable message structures
    .prompt(
        "summarize",
        "Summarize the given text",
        |args| {
            let text = rust_web_server::mcp::extract_arg(args, "text")
                .unwrap_or_else(|| "some text".to_string());
            Ok(vec![PromptMessage::user(format!("Please summarize: {text}"))])
        },
    );

// let (listener, pool) = Server::setup().unwrap();
// Server::run(listener, pool, mcp);

§MCP endpoint

All JSON-RPC messages are sent as POST /mcp (override with .at()). The server implements the MCP 2024-11-05 specification.

§Environment variables

None — configure the server programmatically via the builder.

Structs§

McpContent
Content returned by tool and resource handlers.
McpContext
Per-request context passed to tool handlers registered via McpServer::tool_with_context — caller identity and session info that a plain Fn(&str) -> ... tool handler has no way to see.
McpServer
An HTTP server that implements the MCP 2024-11-05 protocol.
PromptArgDef
Argument definition for a prompt template.
PromptMessage
A single message in a prompt response.
ToolAnnotations
Behavioral hints for a tool, per the MCP 2025-03-26 spec’s tool annotations. Clients (Claude Desktop and others) use these to decide whether to warn or ask for confirmation before calling a tool — e.g. skip confirmation for a read-only tool, or warn before a destructive one.

Functions§

extract_arg
Extract a string argument from a tool/prompt arguments JSON object.