pforge_runtime/
lib.rs

1//! # pforge-runtime
2//!
3//! Core runtime library for pforge - a zero-boilerplate MCP server framework.
4//!
5//! This crate provides the execution engine for MCP servers defined via YAML configuration,
6//! including handler registration, dispatch, middleware, state management, and fault tolerance.
7//!
8//! ## Quick Start
9//!
10//! ```rust
11//! use pforge_runtime::{Handler, HandlerRegistry, Result};
12//! use serde::{Deserialize, Serialize};
13//! use schemars::JsonSchema;
14//!
15//! // Define input/output types
16//! #[derive(Debug, Deserialize, JsonSchema)]
17//! struct GreetInput {
18//!     name: String,
19//! }
20//!
21//! #[derive(Debug, Serialize, JsonSchema)]
22//! struct GreetOutput {
23//!     message: String,
24//! }
25//!
26//! // Implement handler
27//! struct GreetHandler;
28//!
29//! #[async_trait::async_trait]
30//! impl Handler for GreetHandler {
31//!     type Input = GreetInput;
32//!     type Output = GreetOutput;
33//!     type Error = pforge_runtime::Error;
34//!
35//!     async fn handle(&self, input: Self::Input) -> Result<Self::Output> {
36//!         Ok(GreetOutput {
37//!             message: format!("Hello, {}!", input.name),
38//!         })
39//!     }
40//! }
41//!
42//! # #[tokio::main]
43//! # async fn main() -> Result<()> {
44//! // Register and dispatch
45//! let mut registry = HandlerRegistry::new();
46//! registry.register("greet", GreetHandler);
47//!
48//! let input = serde_json::json!({"name": "World"});
49//! let input_bytes = serde_json::to_vec(&input)?;
50//! let output_bytes = registry.dispatch("greet", &input_bytes).await?;
51//! let output: serde_json::Value = serde_json::from_slice(&output_bytes)?;
52//!
53//! assert_eq!(output["message"], "Hello, World!");
54//! # Ok(())
55//! # }
56//! ```
57//!
58//! ## Features
59//!
60//! - **Zero-overhead dispatch**: O(1) average-case handler lookup with FxHash
61//! - **Type safety**: Full compile-time type checking with Serde + JsonSchema
62//! - **Async-first**: Built on tokio for high-performance async execution
63//! - **Fault tolerance**: Circuit breaker, retry with exponential backoff, timeouts
64//! - **State management**: In-memory backend (trueno-db KV persistence coming in Phase 6)
65//! - **Middleware**: Composable request/response processing chain
66//! - **MCP protocol**: Full support for resources, prompts, and tools
67
68pub mod error;
69pub mod handler;
70pub mod handlers;
71pub mod middleware;
72pub mod prompt;
73pub mod recovery;
74pub mod registry;
75pub mod resource;
76pub mod server;
77pub mod state;
78pub mod telemetry;
79pub mod timeout;
80pub mod transport;
81
82pub use error::{Error, Result};
83pub use handler::Handler;
84pub use handlers::{CliHandler, HttpHandler, PipelineHandler};
85pub use middleware::{LoggingMiddleware, Middleware, MiddlewareChain, ValidationMiddleware};
86pub use prompt::{PromptManager, PromptMetadata};
87pub use recovery::{
88    CircuitBreaker, CircuitBreakerConfig, CircuitState, ErrorTracker, FallbackHandler,
89    RecoveryMiddleware,
90};
91pub use registry::HandlerRegistry;
92pub use resource::{ResourceHandler, ResourceManager};
93pub use server::McpServer;
94pub use state::{MemoryStateManager, StateManager};
95// trueno-db KV persistence (Phase 6)
96#[cfg(feature = "persistence")]
97pub use state::TruenoKvStateManager;
98pub use telemetry::{
99    ComponentHealth, HealthCheck, HealthStatus, MetricsCollector, TelemetryMiddleware,
100};
101pub use timeout::{
102    retry_with_policy, with_timeout, RetryMiddleware, RetryPolicy, TimeoutMiddleware,
103};