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.
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.

Functions§

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