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
//! WASM Server MCP Implementation
//!
//! This module provides a full MCP server implementation that runs in WASM environments,
//! including Cloudflare Workers, Deno Deploy, and other edge/serverless platforms.
//! It handles incoming HTTP requests and routes them to registered tool/resource/prompt handlers.
//!
//! # Features
//!
//! - Zero tokio dependencies - uses wasm-bindgen-futures for async
//! - Full MCP protocol support (tools, resources, prompts)
//! - Type-safe handler registration with automatic JSON schema generation
//! - Ergonomic API inspired by axum's IntoResponse pattern
//! - Idiomatic error handling with `?` operator support
//! - Context injection for accessing request metadata, session, and headers
//! - Integration with Cloudflare Workers SDK
//!
//! # Example
//!
//! ```ignore
//! use turbomcp_wasm::wasm_server::*;
//! use worker::*;
//! use serde::Deserialize;
//! use std::sync::Arc;
//!
//! #[derive(Deserialize, schemars::JsonSchema)]
//! struct HelloArgs {
//! name: String,
//! }
//!
//! // Simple handler - just return a String!
//! async fn hello(args: HelloArgs) -> String {
//! format!("Hello, {}!", args.name)
//! }
//!
//! // With error handling using ?
//! async fn fetch_data(args: FetchArgs) -> Result<Json<Data>, ToolError> {
//! let data = do_fetch(&args.url).await?;
//! Ok(Json(data))
//! }
//!
//! // With request context for session/header access
//! async fn auth_tool(ctx: Arc<RequestContext>, args: AuthArgs) -> Result<String, ToolError> {
//! if !ctx.is_authenticated() {
//! return Err(ToolError::new("Unauthorized"));
//! }
//! let session = ctx.session_id().unwrap_or("unknown");
//! Ok(format!("Session: {}", session))
//! }
//!
//! #[event(fetch)]
//! async fn fetch(req: Request, env: Env, _ctx: Context) -> Result<Response> {
//! let server = McpServer::builder("my-mcp-server", "1.0.0")
//! .tool("hello", "Say hello to someone", hello)
//! .tool("fetch", "Fetch data from URL", fetch_data)
//! .tool_with_ctx("auth", "Authenticated tool", auth_tool)
//! .build();
//!
//! server.handle(req).await
//! }
//! ```
//!
//! # Handler Return Types
//!
//! Tool handlers can return any type that implements `IntoToolResponse`:
//!
//! - `String`, `&str` - Returns as text content
//! - `Json<T>` - Serializes to JSON text
//! - `ToolResult` - Full control over the response
//! - `Result<T, E>` where `T: IntoToolResponse`, `E: Into<ToolError>` - Automatic error handling
//! - `()` - Empty success response
//! - `Option<T>` - None returns "No result"
//!
//! # Context Injection
//!
//! Handlers can receive request context by using `_with_ctx` variants:
//!
//! - `tool_with_ctx` - Tool handler with `Arc<RequestContext>` first parameter
//! - `resource_with_ctx` - Resource handler with context
//! - `prompt_with_ctx` - Prompt handler with context
//!
//! The `RequestContext` provides access to:
//! - `request_id()` - Unique request identifier
//! - `session_id()` - Session ID from headers
//! - `user_id()` - User ID (set by auth middleware)
//! - `headers()` - HTTP headers
//! - `header(name)` - Get a specific header (case-insensitive)
//! - `is_authenticated()` - Check authentication status
//! - `has_role(role)` - Check for a specific role
//! - `get_metadata(key)` - Get custom metadata
//!
//! # Building for WASM Environments
//!
//! ```bash
//! # Build for Cloudflare Workers
//! cargo build --target wasm32-unknown-unknown --release
//!
//! # Or using wrangler (Cloudflare)
//! wrangler dev
//! ```
// Re-export the extension trait for unified McpHandler support
// This enables "write once, run everywhere" - any McpHandler can be used
// directly in WASM via .handle_worker_request()
pub use WasmHandlerExt;
// Re-export the main server types
pub use ;
// Re-export composite server types for modular server composition
pub use ;
// Re-export visibility layer for progressive disclosure
pub use ;
// Re-export request context for handlers
pub use RequestContext;
// Re-export rich context for session state, logging, and progress
pub use ;
// Re-export result types
pub use ;
// Re-export the response trait and types for ergonomic handlers
pub use ;
// Re-export handler traits for advanced use cases
pub use IntoToolError;
pub use ;
// Re-export handler trait bounds for advanced use cases
pub use ;
// Re-export authentication middleware when auth feature is enabled
pub use ;
// Re-export streamable HTTP transport when streamable feature is enabled
pub use ;
// Re-export Durable Objects integration when streamable feature is enabled
pub use ;
// Re-export middleware types
pub use ;
/// Re-export worker types for convenience
pub use ;