mcp-host
Production-grade Rust crate for building Model Context Protocol (MCP) servers.
Features
- Full MCP Protocol: JSON-RPC 2.0 with protocol version negotiation
- Tools, Resources, Prompts: All three core MCP primitives
- Bidirectional Communication: Server→Client requests (roots/list, sampling/createMessage)
- Contextual Visibility: Per-session tool/resource/prompt filtering
- Transport Abstraction: STDIO transport with bidirectional support
- 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
[]
= { = "https://github.com/seuros/mcphost-rs" }
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 | Yes |
macros |
Proc macros for tool/resource definition | Yes |
http |
HTTP transport via rama | No |
telemetry |
OpenTelemetry integration | No |
file-watcher |
File system watching via notify | No |
Supported MCP Methods
Client→Server
initialize/pingtools/list/tools/callresources/list/resources/readprompts/list/prompts/get
Server→Client (Bidirectional)
roots/list- Request workspace roots from clientsampling/createMessage- Request LLM completion from client
Examples
# Basic server with tools, resources, and prompts
# Server with background notifications
# Server with resilience patterns (circuit breakers, retry, rate limiting)
# Server with contextual visibility and bidirectional communication
License
MIT