mcp-host
Production-grade Rust crate for building Model Context Protocol (MCP) servers.
Features
- MCP Protocol 2025-11-25: Full JSON-RPC 2.0 with protocol version negotiation
- Tools, Resources, Prompts: All three core MCP primitives
- Elicitation: Request structured user input with JSON Schema validation
- Tasks: Async task execution with status tracking
- Bidirectional Communication: Server→Client requests (roots/list, sampling/createMessage)
- Contextual Visibility: Per-session tool/resource/prompt filtering
- Transport Abstraction: STDIO and HTTP transports
- Middleware Chain: Extensible request processing pipeline
- Session Management: Thread-safe multi-session support with lifecycle state machine
- Background Notifications: Send notifications from background tasks
- Fluent Builder API: Ergonomic server construction
Resilience Patterns
- Circuit Breakers: Per-tool failure protection using
breaker-machines - Retry with Backoff: Exponential backoff with jitter via
chrono-machines - Rate Limiting: GCRA algorithm from
throttle-machines - Structured Logging: MCP-compliant
notifications/messagefor LLM visibility
Requirements
- Rust 1.92+ (edition 2024)
- Tokio runtime
Installation
[]
= "0.1"
Quick Start
use *;
use async_trait;
use Value;
// Define a tool
;
async
Architecture
Core Components
| Component | Description |
|---|---|
Server |
Main MCP server with request routing and session management |
ToolRegistry |
Thread-safe registry for tool implementations |
ResourceManager |
Manages URI-addressable resources |
PromptManager |
Manages reusable prompt templates |
MiddlewareChain |
Request processing pipeline |
Traits
// Implement custom tools
// Implement custom resources
// Implement custom prompts
Bidirectional Communication
Request data from clients:
// Request workspace roots (requires client roots capability)
let roots = server.request_roots.await?;
for root in roots
// Request LLM completion (requires client sampling capability)
let params = CreateMessageParams ;
let result = server.request_sampling.await?;
Visibility Context
Filter tools/resources/prompts based on session state:
;
;
Server Builder
use *;
let server = server
.with_tools
.with_resources
.with_prompts
.with_logging
.with_logging_middleware
.with_validation_middleware
.build;
Resilience Configuration
use *;
let server = server
.with_tools
.with_resources
// Circuit breaker: opens after 3 failures in 60s
.with_circuit_breaker
// Retry: exponential backoff with full jitter
.with_retry
// Rate limit: 100 req/s with burst of 20
.with_rate_limit
.build;
MCP Logging (LLM-visible)
use *;
// Create logger with notification channel
let logger = new;
// Log messages are visible to the LLM via notifications/message
logger.info;
logger.warning;
logger.error;
Notifications
Send notifications from background tasks:
let server = new;
let notification_tx = server.notification_sender;
// Spawn background task
spawn;
Feature Flags
| Flag | Description | Default |
|---|---|---|
stdio |
STDIO transport support | ✓ |
http |
HTTP transport via rama | |
macros |
Proc macros (#[mcp_tool], #[mcp_tool_router]) |
|
full |
All features enabled |
# Minimal (STDIO only)
= "0.1"
# With HTTP transport
= { = "0.1", = ["http"] }
# With macros for ergonomic tool definition
= { = "0.1", = ["macros"] }
# Everything
= { = "0.1", = ["full"] }
Supported MCP Methods
Client→Server
initialize/pingtools/list/tools/callresources/list/resources/read/resources/subscribe/resources/unsubscribeprompts/list/prompts/getcompletion/completetasks/list/tasks/get/tasks/cancel
Server→Client (Bidirectional)
roots/list- Request workspace roots from clientsampling/createMessage- Request LLM completion from clientelicitation/create- Request structured user input with schema validation
Examples
# Run the comprehensive PROMETHEUS example with all features
# Run with HTTP transport (requires http feature)
License
BSD-3-Clause