mcp-host 0.1.19

Production-grade MCP host crate for building Model Context Protocol servers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# 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/message` for LLM visibility

## Requirements

- Rust 1.92+ (edition 2024)
- Tokio runtime

## Installation

```toml
[dependencies]
mcp-host = "0.1"
```

## Quick Start

```rust
use mcp_host::prelude::*;
use serde_json::Value;

// Define a tool
struct EchoTool;

impl Tool for EchoTool {
    fn name(&self) -> &str { "echo" }

    fn description(&self) -> Option<&str> {
        Some("Echoes back the input message")
    }

    fn input_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "message": { "type": "string" }
            },
            "required": ["message"]
        })
    }

    fn execute<'a>(&'a self, ctx: ExecutionContext<'a>) -> ToolFuture<'a> {
        Box::pin(async move {
            let msg = ctx.params["message"].as_str().unwrap_or("?");
            Ok(ToolOutput::text(format!("Echo: {}", msg)))
        })
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let server = Server::new("my-server", "0.1.0");
    server.tool_registry().register(EchoTool);

    server.set_capabilities(ServerCapabilities {
        tools: Some(mcp_host::protocol::capabilities::ToolsCapability {
            list_changed: Some(false),
        }),
        ..Default::default()
    });

    server.run(StdioTransport::new()).await
}
```

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

```rust
// Implement custom tools
pub trait Tool: Send + Sync {
    fn name(&self) -> &str;
    fn description(&self) -> Option<&str>;
    fn input_schema(&self) -> Value;

    // Optional: contextual visibility (default: always visible)
    fn is_visible(&self, ctx: &VisibilityContext) -> bool { true }

    // Execute with full session context
    fn execute<'a>(&'a self, ctx: ExecutionContext<'a>) -> ToolFuture<'a>;
}

// Implement custom resources
pub trait Resource: Send + Sync {
    fn uri(&self) -> &str;
    fn name(&self) -> &str;
    fn description(&self) -> Option<&str>;
    fn mime_type(&self) -> Option<&str>;

    fn is_visible(&self, ctx: &VisibilityContext) -> bool { true }

    fn read<'a>(&'a self, ctx: ExecutionContext<'a>) -> ResourceReadFuture<'a>;
}

// Implement custom prompts
pub trait Prompt: Send + Sync {
    fn name(&self) -> &str;
    fn description(&self) -> Option<&str>;
    fn arguments(&self) -> Option<Vec<PromptArgument>>;

    fn is_visible(&self, ctx: &VisibilityContext) -> bool { true }

    fn get<'a>(&'a self, ctx: ExecutionContext<'a>) -> PromptFuture<'a>;
}
```

### Bidirectional Communication

Request data from clients:

```rust
// Request workspace roots (requires client roots capability)
let roots = server.request_roots("session-id", None).await?;
for root in roots {
    println!("Root: {} ({})", root.name.unwrap_or_default(), root.uri);
}

// Request LLM completion (requires client sampling capability)
let params = CreateMessageParams {
    messages: vec![SamplingMessage {
        role: "user".to_string(),
        content: SamplingContent::Text { text: "Hello!".to_string() },
    }],
    max_tokens: 1000,
    ..Default::default()
};
let result = server.request_sampling("session-id", params, None).await?;
```

### Visibility Context

Filter tools/resources/prompts based on session state:

```rust
struct GitCommitTool;

impl Tool for GitCommitTool {
    // Only visible when in a git repo with uncommitted changes
    fn is_visible(&self, ctx: &VisibilityContext) -> bool {
        ctx.environment
            .map(|e| e.has_git_repo() && !e.git_is_clean())
            .unwrap_or(false)
    }
    // ...
}

struct AdminOnlyResource;

impl Resource for AdminOnlyResource {
    // Only visible to admin users
    fn is_visible(&self, ctx: &VisibilityContext) -> bool {
        ctx.is_admin()
    }
    // ...
}
```

### Custom Context for Macros

`#[mcp_tool]`, `#[mcp_prompt]`, and resource macros can accept any context type
that implements `FromExecutionContext`. This lets you add your own helpers while
still accessing the built-in `Ctx` API.

```rust
use std::ops::Deref;
use mcp_host::prelude::*;

#[derive(Clone)]
struct MyCtx<'a> {
    inner: Ctx<'a>,
}

impl<'a> FromExecutionContext<'a> for MyCtx<'a> {
    fn from_execution_context(ctx: &'a ExecutionContext<'a>) -> Self {
        Self { inner: Ctx::from_execution_context(ctx) }
    }
}

impl<'a> Deref for MyCtx<'a> {
    type Target = Ctx<'a>;
    fn deref(&self) -> &Self::Target { &self.inner }
}

struct MyServer;

impl MyServer {
    #[mcp_tool(name = "echo")]
    async fn echo(&self, ctx: MyCtx<'_>, _params: Parameters<()>) -> ToolResult {
        if !ctx.is_admin() { /* ... */ }
        Ok(ToolOutput::text("ok"))
    }
}
```

### Rust 2024 Module Layout (One Handler Per File)

The PROMETHEUS example uses a Rails-style, one-handler-per-file layout.
This keeps tools/prompts/resources small and composable.

```text
examples/
  prometheus_project.rs
  prometheus_project/
    tools.rs
    prompts.rs
    resources.rs
    tools/
      echo.rs
      read_file.rs
      ...
    prompts/
      greeting.rs
      ...
    resources/
      server_config.rs
      timeline_data.rs
      ...
```

```rust
// examples/prometheus_project/tools/echo.rs
use super::super::*;
use mcp_host::registry::router::McpToolRouter;

impl PrometheusServer {
    #[mcp_tool(name = "echo", read_only = true, idempotent = true)]
    async fn echo(&self, _ctx: PrometheusCtx<'_>, params: Parameters<EchoParams>) -> ToolResult {
        Ok(ToolOutput::text(format!("Echo: {}", params.0.message)))
    }
}

pub fn mount(router: McpToolRouter<PrometheusServer>) -> McpToolRouter<PrometheusServer> {
    router.with_tool(PrometheusServer::echo_tool_info(), PrometheusServer::echo_handler, None)
}
```

```rust
// examples/prometheus_project/tools.rs
#[path = "tools/echo.rs"]
pub mod echo;

use mcp_host::registry::router::McpToolRouter;
use crate::PrometheusServer;

pub fn router() -> McpToolRouter<PrometheusServer> {
    let router = McpToolRouter::new();
    echo::mount(router)
}
```

```rust
// examples/prometheus_project.rs
#[path = "prometheus_project/tools.rs"]
mod tools;
#[path = "prometheus_project/prompts.rs"]
mod prompts;
#[path = "prometheus_project/resources.rs"]
mod resources;

impl PrometheusServer {
    fn router() -> mcp_host::registry::router::McpRouter<Self> {
        mcp_host::registry::router::McpRouter::new(
            tools::router(),
            prompts::router(),
            resources::router(),
            resources::template_router(),
        )
    }
}
```

### Server Builder

```rust
use mcp_host::prelude::*;

let server = server("my-server", "1.0.0")
    .with_tools(true)
    .with_resources(true, false)
    .with_prompts(true)
    .with_logging()
    .with_logging_middleware()
    .with_validation_middleware()
    .build();
```

### Resilience Configuration

```rust
use mcp_host::prelude::*;

let server = server("resilient-server", "1.0.0")
    .with_tools(true)
    .with_resources(true, false)
    // Circuit breaker: opens after 3 failures in 60s
    .with_circuit_breaker(ToolBreakerConfig {
        failure_threshold: 3,
        failure_window_secs: 60.0,
        half_open_timeout_secs: 10.0,
        success_threshold: 2,
    })
    // Retry: exponential backoff with full jitter
    .with_retry(ResourceRetryConfig {
        max_attempts: 3,
        base_delay_ms: 100,
        multiplier: 2.0,
        max_delay_ms: 5000,
        jitter_factor: 1.0,
    })
    // Rate limit: 100 req/s with burst of 20
    .with_rate_limit(100.0, 20)
    .build();
```

### MCP Logging (LLM-visible)

```rust
use mcp_host::prelude::*;

// Create logger with notification channel
let logger = McpLogger::new(notification_tx, "my-tool");

// Log messages are visible to the LLM via notifications/message
logger.info("Tool initialized successfully");
logger.warning("Rate limit approaching threshold");
logger.error("External API unavailable");
```

## Notifications

Send notifications from background tasks:

```rust
let server = Server::new("server", "1.0.0");
let notification_tx = server.notification_sender();

// Spawn background task
tokio::spawn(async move {
    notification_tx.send(JsonRpcNotification::new(
        "notifications/progress",
        Some(serde_json::json!({ "progress": 50 })),
    )).ok();
});
```

## Feature Flags

| Flag | Description | Default |
|------|-------------|---------|
| `stdio` | STDIO transport support ||
| `http` | HTTP transport via rama | |
| `macros` | Proc macros (`#[mcp_tool]`, `#[mcp_prompt]`, `#[mcp_resource]`, `#[mcp_router]`) | |
| `full` | All features enabled | |

```toml
# Minimal (STDIO only)
mcp-host = "0.1"

# With HTTP transport
mcp-host = { version = "0.1", features = ["http"] }

# With macros for ergonomic tool definition
mcp-host = { version = "0.1", features = ["macros"] }

# Everything
mcp-host = { version = "0.1", features = ["full"] }
```

## Supported MCP Methods

### Client→Server

- `initialize` / `ping`
- `tools/list` / `tools/call`
- `resources/list` / `resources/read` / `resources/subscribe` / `resources/unsubscribe`
- `prompts/list` / `prompts/get`
- `completion/complete`
- `tasks/list` / `tasks/get` / `tasks/cancel`

### Server→Client (Bidirectional)

- `roots/list` - Request workspace roots from client
- `sampling/createMessage` - Request LLM completion from client
- `elicitation/create` - Request structured user input with schema validation

## Examples

```bash
# Run the comprehensive PROMETHEUS example with all features
cargo run --example prometheus_project --features macros

# Run with HTTP transport (requires http feature)
cargo run --example prometheus_project --features "macros,http" -- --http --port 8080
```

## License

BSD-3-Clause