Skip to main content

mcp_usage_tower/
lib.rs

1//! Tower hot path for MCP authentication, caching, and terminal-delivery billing.
2//!
3//! Request metadata is classified from the mandatory MCP headers. Bodies are
4//! deserialized only for cache keys, durable-task attribution, and the terminal
5//! response decision. Response bodies remain streaming: the wrapper observes
6//! frames as the client consumes them and records usage only after a terminal
7//! JSON-RPC response reaches end-of-stream.
8
9#![forbid(unsafe_code)]
10#![warn(missing_docs, clippy::pedantic)]
11#![allow(clippy::module_name_repetitions)]
12
13mod auth;
14mod cache;
15mod classify;
16mod deferred;
17mod layer;
18mod metrics;
19#[cfg(feature = "opentelemetry")]
20mod opentelemetry_metrics;
21#[cfg(test)]
22mod properties;
23mod task;
24
25pub use auth::{
26    InMemoryTenantStore, MIN_API_KEY_BYTES, Tenant, TenantStore, WeakApiKey, hash_api_key,
27    validate_api_key_strength,
28};
29pub use classify::{ClassificationError, ProtocolHeaders, classify_protocol_headers};
30pub use deferred::DeferredCompletions;
31pub use layer::{
32    Classification, ClassifiedCall, CredentialError, EdgeConfig, EdgeConfigError, MeterBody,
33    MeterLayer, MeterService, classify_request_headers, extract_api_key,
34};
35pub use mcp_usage_core::{TaskAttribution, TaskOriginKind};
36pub use metrics::{EdgeMetrics, MetricsSnapshot, UnaccountedReason};
37#[cfg(feature = "opentelemetry")]
38pub use opentelemetry_metrics::{OpenTelemetryMetrics, install_opentelemetry};
39pub use task::{InMemoryTaskStore, TaskAttributionStore, TaskStoreError, TaskStoreFuture};
40
41/// Standard protocol-version request header.
42pub const PROTOCOL_VERSION_HEADER: &str = "mcp-protocol-version";
43/// Mirrored JSON-RPC method request header.
44pub const METHOD_HEADER: &str = "mcp-method";
45/// Mirrored tool, prompt, or resource name request header.
46pub const NAME_HEADER: &str = "mcp-name";
47/// API-key header accepted in addition to `Authorization: Bearer`.
48pub const API_KEY_HEADER: &str = "x-api-key";