Skip to main content

mcp_usage_kit/
lib.rs

1//! Tower metering for `rmcp` Streamable HTTP servers.
2//!
3//! This is the small public facade intended for application dependencies. It
4//! re-exports the protocol semantics, billing pipeline, and edge configuration
5//! so an application needs only one `cargo add`.
6//!
7//! ```no_run
8//! use std::sync::Arc;
9//! use mcp_usage_kit::{EdgeConfig, InMemoryTenantStore, MeterLayer, Tenant};
10//! # use tower::Layer;
11//! # let rmcp_service = tower::service_fn(|_: http::Request<http_body_util::Full<bytes::Bytes>>| async {
12//! #     Ok::<_, std::convert::Infallible>(http::Response::new(http_body_util::Full::new(bytes::Bytes::new())))
13//! # });
14//!
15//! let tenants = Arc::new(InMemoryTenantStore::new());
16//! tenants.insert_unchecked("development-key", Tenant::new("acme", "cus_acme"));
17//! let metered = MeterLayer::new(EdgeConfig::new(tenants)).layer(rmcp_service);
18//! # let _ = metered;
19//! ```
20
21#![forbid(unsafe_code)]
22#![warn(missing_docs, clippy::pedantic)]
23
24/// Pure MCP billing semantics.
25pub use mcp_usage_core as core;
26/// Buffered billing types and exporters.
27pub use mcp_usage_export as export;
28/// Lower-level Tower edge implementation.
29pub use mcp_usage_tower as tower;
30
31pub use mcp_usage_core::{
32    Billable, Call, Charge, FreeReason, LimitDecision, LimitReason, Limits, Method, PriceBook,
33    TaskAttribution, TaskOriginKind, Usage, assess_limits, decide, decide_with_task_attribution,
34    decide_with_task_origin,
35};
36// `ExportError`, `ExportFuture`, and `RecordError` appear in the signatures of
37// `BatchExporter` and `UsageRecorder`, so an application cannot implement either
38// re-exported trait without them. `MeterBody` is the response body of the
39// re-exported `MeterService` and is unavoidable in an application's own type
40// signatures. The point of this crate is one `cargo add`, so anything reachable
41// from a re-exported item's signature is re-exported too.
42pub use mcp_usage_export::{
43    AggregatedUsage, BatchExporter, BillingPipeline, CompositeExporter, ExportError, ExportFuture,
44    FnExporter, LogExporter, MeterEventDeadLetter, MeterEventDeadLetterReason, MeterEventExporter,
45    MeterEventOutcome, MeterEventProvider, MeterEventProviderError, MeterEventProviderFuture,
46    NoopRecorder, RecordError, RecordOutcome, RetryPolicy, SharedExporter, SharedRecorder,
47    UsageBuffer, UsageEvent, UsageRecorder,
48};
49pub use mcp_usage_tower::{
50    API_KEY_HEADER, Classification, ClassifiedCall, CredentialError, DeferredCompletions,
51    EdgeConfig, EdgeConfigError, EdgeMetrics, InMemoryTaskStore, InMemoryTenantStore,
52    METHOD_HEADER, MIN_API_KEY_BYTES, MeterBody, MeterLayer, MeterService, MetricsSnapshot,
53    NAME_HEADER, PROTOCOL_VERSION_HEADER, TaskAttributionStore, TaskStoreError, TaskStoreFuture,
54    Tenant, TenantStore, WeakApiKey, classify_request_headers, extract_api_key, hash_api_key,
55    validate_api_key_strength,
56};
57
58#[cfg(feature = "opentelemetry")]
59pub use mcp_usage_tower::{OpenTelemetryMetrics, install_opentelemetry};
60
61/// Distributed task-attribution adapters.
62#[cfg(any(feature = "postgres", feature = "redis"))]
63pub use mcp_usage_store as store;