Skip to main content

mcp_gateway/
lib.rs

1// SPDX-FileCopyrightText: 2026 Mikko Parkkola
2// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
3//! MCP Gateway Library
4//!
5//! Universal Model Context Protocol (MCP) Gateway with a compact Meta-MCP tool surface.
6//!
7//! # Features
8//!
9//! - **Meta-MCP Mode**: 12 tools minimum, 14 in the README benchmark scenario, 15 when webhook status is surfaced
10//! - **Streaming**: Real-time notifications via SSE (MCP 2025-03-26 Streamable HTTP)
11//! - **Notification Multiplexer**: Routes backend notifications to connected clients
12//! - **Multi-Transport**: stdio, Streamable HTTP, SSE support
13//! - **Failsafes**: Circuit breakers, retries, timeouts, rate limiting
14//! - **Production Ready**: Health checks, metrics, graceful shutdown
15//!
16//! # Protocol Version
17//!
18//! Implements MCP protocol versions:
19//! - 2025-11-25 (latest - tasks, elicitation, audio, tool annotations)
20//! - 2025-06-18
21//! - 2025-03-26 (Streamable HTTP)
22//! - 2024-11-05
23//! - 2024-10-07
24
25#![deny(unsafe_code)]
26#![warn(missing_docs)]
27// Macro-generated arrays (e.g. include_bytes!) may exceed the 16 KiB stack
28// threshold.  Clippy cannot show a source location for these — allow crate-wide.
29#![allow(clippy::large_stack_arrays)]
30
31#[cfg(feature = "a2a")]
32pub mod a2a;
33pub mod attestation;
34pub mod autotag;
35pub mod backend;
36pub mod cache;
37pub mod capability;
38pub mod chains;
39pub mod cli;
40pub mod config;
41pub mod config_persistence;
42pub mod config_reload;
43pub mod context_compression;
44pub mod context_integrity;
45pub mod control_plane;
46pub mod cost_accounting;
47pub mod discovery;
48pub mod error;
49pub mod failsafe;
50mod fs_lock;
51pub mod gateway;
52mod hashing;
53pub mod idempotency;
54pub mod identity_grants;
55pub mod identity_propagation;
56pub mod key_server;
57pub mod kill_switch;
58pub mod kubernetes;
59#[cfg(feature = "metrics")]
60pub mod metrics;
61pub mod mtls;
62pub mod oauth;
63pub mod playbook;
64pub mod projection;
65pub mod protocol;
66pub mod protocol_imports;
67pub mod provider;
68pub mod ranking;
69pub mod registry;
70pub mod routing_profile;
71pub mod runtime;
72pub mod scheduler;
73pub mod secret_injection;
74pub mod secrets;
75pub mod security;
76#[cfg(feature = "semantic-search")]
77pub mod semantic_search;
78pub mod session_sandbox;
79pub mod simhash;
80pub mod skills;
81pub mod stats;
82#[cfg(feature = "tool-profiles")]
83pub mod tool_profiles;
84pub mod tool_registry;
85pub mod tracing_context;
86pub mod transform;
87pub mod transition;
88pub mod transport;
89pub mod trust;
90pub mod tunnel;
91pub mod validator;
92
93pub use error::{Error, Result};
94
95use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
96
97/// MCP Protocol version supported by this gateway (latest)
98pub const MCP_PROTOCOL_VERSION: &str = "2025-11-25";
99
100/// Setup tracing/logging
101///
102/// All log output is written to **stderr**. In stdio transport mode
103/// (`serve --stdio`) stdout is reserved exclusively for newline-delimited
104/// JSON-RPC frames; emitting logs there corrupts the stream and breaks MCP
105/// clients (see issue #224). Writing to stderr is also correct for the HTTP
106/// server mode, where supervisors (systemd, Docker) capture stderr normally.
107///
108/// # Errors
109///
110/// This function currently always succeeds but returns `Result` for
111/// forward compatibility with fallible tracing configurations.
112pub fn setup_tracing(level: &str, format: Option<&str>) -> Result<()> {
113    let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(level));
114
115    let subscriber = tracing_subscriber::registry().with(filter);
116
117    match format {
118        Some("json") => {
119            subscriber
120                .with(fmt::layer().json().with_writer(std::io::stderr))
121                .init();
122        }
123        _ => {
124            subscriber
125                .with(fmt::layer().with_writer(std::io::stderr))
126                .init();
127        }
128    }
129
130    Ok(())
131}