fastmcp_core/lib.rs
1//! Core types and traits for FastMCP.
2//!
3//! This crate provides the fundamental building blocks:
4//! - [`McpContext`] wrapping asupersync's [`Cx`]
5//! - Error types for MCP operations
6//! - Core traits for tool, resource, and prompt handlers
7//!
8//! # Design Principles
9//!
10//! - Zero-copy where possible
11//! - No runtime reflection (compile-time via macros)
12//! - All types support `Send + Sync`
13//! - Cancel-correct via asupersync integration
14//!
15//! # Role in the System
16//!
17//! `fastmcp-core` is the **foundation layer** shared by every other crate.
18//! It defines:
19//! - `McpContext`, the capability-carrying handle that wraps asupersync's `Cx`
20//! - The FastMCP error model (`McpError`, `McpErrorCode`, `McpResult`)
21//! - Budget/cancellation semantics that handlers and transports must obey
22//! - Outcome bridging utilities so server/client code can stay 4-valued
23//!
24//! If you are implementing a new transport, handler, or runtime adapter, this
25//! is the crate that gives you the shared primitives used everywhere else.
26//!
27//! # Asupersync Integration
28//!
29//! This crate uses [asupersync](https://github.com/Dicklesworthstone/asupersync) as its async
30//! runtime foundation, providing:
31//!
32//! - **Structured concurrency**: Tool handlers run in regions
33//! - **Cancel-correctness**: Graceful cancellation via checkpoints
34//! - **Budgeted timeouts**: Request timeouts via budget exhaustion
35//! - **Deterministic testing**: Lab runtime for reproducible tests
36
37#![forbid(unsafe_code)]
38// Allow dead code during Phase 0 development
39#![allow(dead_code)]
40
41mod auth;
42pub mod combinator;
43mod context;
44mod duration;
45mod error;
46pub mod logging;
47pub mod runtime;
48mod state;
49
50pub use auth::{AUTH_STATE_KEY, AccessToken, AuthContext};
51pub use context::{
52 CancelledError, ClientCapabilityInfo, ElicitationAction, ElicitationMode, ElicitationRequest,
53 ElicitationResponse, ElicitationSender, IntoOutcome, MAX_RESOURCE_READ_DEPTH,
54 MAX_TOOL_CALL_DEPTH, McpContext, NoOpElicitationSender, NoOpNotificationSender,
55 NoOpSamplingSender, NotificationSender, ProgressReporter, ResourceContentItem,
56 ResourceReadResult, ResourceReader, SamplingRequest, SamplingRequestMessage, SamplingResponse,
57 SamplingRole, SamplingSender, SamplingStopReason, ServerCapabilityInfo, ToolCallResult,
58 ToolCaller, ToolContentItem,
59};
60pub use duration::{ParseDurationError, parse_duration};
61pub use error::{
62 McpError, McpErrorCode, McpOutcome, McpResult, OutcomeExt, ResultExt, cancelled, err, ok,
63};
64pub use runtime::block_on;
65pub use state::{DISABLED_PROMPTS_KEY, DISABLED_RESOURCES_KEY, DISABLED_TOOLS_KEY, SessionState};
66
67// Re-export key asupersync types for convenience
68pub use asupersync::{Budget, Cx, LabConfig, LabRuntime, Outcome, RegionId, Scope, TaskId};