//! MCP Gateway Library
//!
//! Universal Model Context Protocol (MCP) Gateway with a compact Meta-MCP tool surface.
//!
//! # Features
//!
//! - **Meta-MCP Mode**: 12 tools minimum, 14 in the README benchmark scenario, 15 when webhook status is surfaced
//! - **Streaming**: Real-time notifications via SSE (MCP 2025-03-26 Streamable HTTP)
//! - **Notification Multiplexer**: Routes backend notifications to connected clients
//! - **Multi-Transport**: stdio, Streamable HTTP, SSE support
//! - **Failsafes**: Circuit breakers, retries, timeouts, rate limiting
//! - **Production Ready**: Health checks, metrics, graceful shutdown
//!
//! # Protocol Version
//!
//! Implements MCP protocol versions:
//! - 2025-11-25 (latest - tasks, elicitation, audio, tool annotations)
//! - 2025-06-18
//! - 2025-03-26 (Streamable HTTP)
//! - 2024-11-05
//! - 2024-10-07

#![deny(unsafe_code)]
#![warn(missing_docs)]
// Macro-generated arrays (e.g. include_bytes!) may exceed the 16 KiB stack
// threshold.  Clippy cannot show a source location for these — allow crate-wide.
#![allow(clippy::large_stack_arrays)]

#[cfg(feature = "a2a")]
pub mod a2a;
pub mod attestation;
pub mod autotag;
pub mod backend;
pub mod cache;
pub mod capability;
pub mod chains;
pub mod cli;
pub mod config;
pub mod config_persistence;
pub mod config_reload;
pub mod context_compression;
pub mod context_integrity;
pub mod control_plane;
pub mod cost_accounting;
pub mod discovery;
pub mod error;
pub mod failsafe;
pub mod gateway;
mod hashing;
pub mod idempotency;
pub mod identity_grants;
pub mod identity_propagation;
pub mod key_server;
pub mod kill_switch;
pub mod kubernetes;
#[cfg(feature = "metrics")]
pub mod metrics;
pub mod mtls;
pub mod oauth;
pub mod playbook;
pub mod projection;
pub mod protocol;
pub mod protocol_imports;
pub mod provider;
pub mod ranking;
pub mod registry;
pub mod routing_profile;
pub mod runtime;
pub mod scheduler;
pub mod secret_injection;
pub mod secrets;
pub mod security;
#[cfg(feature = "semantic-search")]
pub mod semantic_search;
pub mod session_sandbox;
pub mod simhash;
pub mod skills;
pub mod stats;
#[cfg(feature = "tool-profiles")]
pub mod tool_profiles;
pub mod tool_registry;
pub mod tracing_context;
pub mod transform;
pub mod transition;
pub mod transport;
pub mod trust;
pub mod tunnel;
pub mod validator;

pub use error::{Error, Result};

use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};

/// MCP Protocol version supported by this gateway (latest)
pub const MCP_PROTOCOL_VERSION: &str = "2025-11-25";

/// Setup tracing/logging
///
/// All log output is written to **stderr**. In stdio transport mode
/// (`serve --stdio`) stdout is reserved exclusively for newline-delimited
/// JSON-RPC frames; emitting logs there corrupts the stream and breaks MCP
/// clients (see issue #224). Writing to stderr is also correct for the HTTP
/// server mode, where supervisors (systemd, Docker) capture stderr normally.
///
/// # Errors
///
/// This function currently always succeeds but returns `Result` for
/// forward compatibility with fallible tracing configurations.
pub fn setup_tracing(level: &str, format: Option<&str>) -> Result<()> {
    let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(level));

    let subscriber = tracing_subscriber::registry().with(filter);

    match format {
        Some("json") => {
            subscriber
                .with(fmt::layer().json().with_writer(std::io::stderr))
                .init();
        }
        _ => {
            subscriber
                .with(fmt::layer().with_writer(std::io::stderr))
                .init();
        }
    }

    Ok(())
}
