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
//! `rmcp-server-kit` - production-grade reusable framework for building
//! [Model Context Protocol](https://modelcontextprotocol.io/) servers in Rust.
//!
//! Application crates depend on `rmcp-server-kit` and supply their own
//! [`rmcp::handler::server::ServerHandler`] implementation; the kit provides
//! transport, security, and observability around it.
//!
//! # What you get
//!
//! - **Streamable HTTP transport** with TLS / mTLS termination, configurable
//! keep-alive and session idle timeouts, CORS, compression, body-size and
//! concurrency caps, and graceful shutdown on `SIGINT`/`SIGTERM`.
//! - **Authentication**: API keys (Argon2-hashed, constant-time compared),
//! mTLS client certificates with optional CDP-driven CRL revocation, and -
//! under the `oauth` feature - OAuth 2.1 Bearer JWT validation against a
//! cached JWKS endpoint.
//! - **RBAC** with per-tool argument allow-lists and per-IP per-tool rate
//! limiting; policies and API keys are hot-reloadable at runtime via
//! [`transport::ReloadHandle`] (lock-free [`arc_swap`] swaps).
//! - **Observability**: `tracing` with JSON or pretty formats, optional audit
//! file sink, `/healthz` + `/readyz` probes, `/version`, `/admin/*`
//! diagnostics, and - under the `metrics` feature - a Prometheus
//! `/metrics` endpoint on a separate listener.
//! - **OWASP-grade defaults**: HSTS, CSP, `X-Frame-Options`, MCP `Origin`
//! validation, and per-hop SSRF guards on outbound HTTP.
//!
//! # Quick start
//!
//! ```no_run
//! use rmcp::{
//! handler::server::ServerHandler,
//! model::{ServerCapabilities, ServerConfig},
//! };
//! use rmcp_server_kit::{
//! config::ObservabilityConfig,
//! observability::init_tracing_from_config_strict,
//! transport::{McpServerConfig, serve},
//! };
//!
//! #[derive(Clone)]
//! struct MyHandler;
//!
//! impl ServerHandler for MyHandler {
//! fn get_info(&self) -> ServerConfig {
//! ServerConfig::new(ServerCapabilities::builder().enable_tools().build())
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() -> rmcp_server_kit::Result<()> {
//! let mut observability = ObservabilityConfig::default();
//! observability.log_level = "info".into();
//! let _tracing_guard = init_tracing_from_config_strict(&observability)?;
//!
//! let config = McpServerConfig::new(
//! "127.0.0.1:8080",
//! "my-mcp-server",
//! env!("CARGO_PKG_VERSION"),
//! );
//!
//! serve(config.validate()?, || MyHandler).await
//! }
//! ```
//!
//! See [`examples/`](https://github.com/andrico21/rmcp-server-kit/tree/main/examples)
//! for richer setups (API-key + RBAC, OAuth resource server) and
//! [`docs/GUIDE.md`](https://github.com/andrico21/rmcp-server-kit/blob/main/docs/GUIDE.md)
//! for the full TOML configuration reference.
//!
//! # Cargo features
//!
//! All features are **off by default**:
//!
//! - `oauth` - OAuth 2.1 Bearer JWT validation, JWKS cache, and optional
//! OAuth proxy endpoints. Pulls in [`jsonwebtoken`] and [`urlencoding`].
//! Required to use the [`oauth`] module.
//! - `oauth-mtls-client` - RFC 8705 §2 mTLS client authentication for the
//! OAuth token-exchange endpoint. Implies `oauth`. Without this feature,
//! [`oauth::OAuthConfig::validate`] rejects any configuration that sets
//! [`oauth::TokenExchangeConfig::client_cert`].
//! - `metrics` - Prometheus registry and `/metrics` listener. Pulls in
//! the [`prometheus`] crate. Required to use the [`metrics`] module.
//! - `test-helpers` - exposes test-only helpers from [`mtls_revocation`] and,
//! when `oauth` is also enabled, [`oauth`], for downstream integration tests.
//! **Not part of the stable API surface** - no semver guarantees across minor
//! releases. **never enable in a production build:** some helpers deliberately
//! bypass SSRF screening, the JWKS refresh cooldown, the CDP discovery rate
//! limiter, and CRL verifier publication.
//!
//! # ⚠️ stdio transport is unauthenticated
//!
//! [`transport::serve_stdio`] runs MCP over the process's stdin/stdout for
//! local subprocess scenarios (desktop clients, IDE integrations). It
//! **bypasses authentication, RBAC, TLS, Origin validation, and rate
//! limiting** - the surrounding OS process boundary is the only trust
//! boundary. Never expose `serve_stdio` to untrusted callers; for any
//! network-reachable deployment use [`transport::serve`] over HTTPS instead.
/// Reusable server and observability configuration primitives.
/// Process-global switches controlling plaintext-vs-redacted diagnostics.
/// Generic error type and `Result` alias for server-side code.
/// Tracing / JSON logs / audit file initialization.
/// Streamable HTTP transport and server entry points.
/// Authentication state (API keys, mTLS, OAuth JWT) and middleware.
/// Role-based access control policy engine and middleware.
/// Memory-bounded keyed rate limiter (LRU + idle eviction).
// Module-level docs live in cancel.rs (`//!`); an outer doc here would
// concatenate into an over-long first rustdoc paragraph.
/// Admin diagnostic endpoints (status, auth keys metadata, counters, RBAC).
/// Re-exports for the [`secrecy`] crate's secret-wrapper types.
pub
pub
pub
pub
pub
pub
/// Opt-in tool-call hooks (before/after) and result-size cap.
/// OAuth 2.1 JWKS cache, token validation, and token exchange helpers.
/// Prometheus metrics registry shared across server components.
/// CDP-driven CRL revocation support for mTLS.
// Explicit re-exports rather than a glob. A `pub use crate::error::*;` would
// silently promote every future `pub` item added to `error.rs` into the crate
// root's stable API surface -- `cargo-semver-checks` flags removals, not
// accidental additions, so the mistake would ship unnoticed. Listing the three
// items keeps the root surface a deliberate choice.
pub use crateMcpxError;
pub use crate;