pulseengine_mcp_server/
builder_trait.rs

1//! Simplified builder trait for reducing generated code in server implementations
2//!
3//! This module provides a basic trait that servers can implement to get
4//! common functionality without generating repetitive code.
5
6use crate::McpBackend;
7
8/// Simplified builder trait that provides common functionality for all MCP servers
9pub trait McpServerBuilder: McpBackend + Sized {
10    /// Create server with default configuration
11    fn with_defaults() -> Self
12    where
13        Self: Default,
14    {
15        Self::default()
16    }
17
18    /// Configure stdio logging to redirect logs to stderr
19    fn configure_stdio_logging() {
20        #[cfg(feature = "stdio-logging")]
21        {
22            use tracing_subscriber::{
23                EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt,
24            };
25
26            let filter_layer = EnvFilter::try_from_default_env()
27                .or_else(|_| EnvFilter::try_new("info"))
28                .unwrap();
29
30            let fmt_layer = fmt::layer()
31                .with_writer(std::io::stderr)
32                .with_target(false)
33                .with_file(false)
34                .with_line_number(false)
35                .with_thread_ids(false)
36                .with_thread_names(false);
37
38            tracing_subscriber::registry()
39                .with(filter_layer)
40                .with(fmt_layer)
41                .try_init()
42                .unwrap_or(()); // Ignore if already initialized
43        }
44
45        #[cfg(not(feature = "stdio-logging"))]
46        {
47            eprintln!("Warning: stdio logging configuration requires 'stdio-logging' feature");
48            eprintln!(
49                "Add to Cargo.toml: pulseengine-mcp-macros = {{ version = \"*\", features = [\"stdio-logging\"] }}"
50            );
51            eprintln!("And: tracing-subscriber = \"0.3\"");
52        }
53    }
54}
55
56/// Simplified service type alias for convenience
57pub type McpService<B> = B;