pub struct McpServer { /* private fields */ }
Expand description
Main MCP server
Implementations§
Source§impl McpServer
impl McpServer
Sourcepub fn new(config: ServerConfig) -> Self
pub fn new(config: ServerConfig) -> Self
Create a new server
Sourcepub const fn config(&self) -> &ServerConfig
pub const fn config(&self) -> &ServerConfig
Get server configuration
Sourcepub const fn registry(&self) -> &Arc<HandlerRegistry>
pub const fn registry(&self) -> &Arc<HandlerRegistry>
Get handler registry
Sourcepub const fn router(&self) -> &Arc<RequestRouter>
pub const fn router(&self) -> &Arc<RequestRouter>
Get request router
Sourcepub const fn lifecycle(&self) -> &Arc<ServerLifecycle>
pub const fn lifecycle(&self) -> &Arc<ServerLifecycle>
Get server lifecycle
Sourcepub const fn metrics(&self) -> &Arc<ServerMetrics>
pub const fn metrics(&self) -> &Arc<ServerMetrics>
Get server metrics
Sourcepub fn shutdown_handle(&self) -> ShutdownHandle
pub fn shutdown_handle(&self) -> ShutdownHandle
Get a shutdown handle for graceful server termination
This handle enables external control over server shutdown, essential for:
- Production deployments: Graceful shutdown on SIGTERM/SIGINT
- Container orchestration: Kubernetes graceful pod termination
- Load balancer integration: Health check coordination
- Multi-component systems: Coordinated shutdown sequences
- Maintenance operations: Planned downtime and updates
§Examples
§Basic shutdown coordination
let server = ServerBuilder::new().build();
let shutdown_handle = server.shutdown_handle();
// Coordinate with other services
tokio::spawn(async move {
// Wait for external shutdown signal
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
println!("Shutdown signal received, terminating gracefully...");
shutdown_handle.shutdown().await;
});
// Server will gracefully shut down when signaled
// server.run_stdio().await?;
§Container/Kubernetes deployment
let server = ServerBuilder::new().build();
let shutdown_handle = server.shutdown_handle();
let shutdown_handle_clone = shutdown_handle.clone();
// Handle multiple signal types with proper platform support
tokio::spawn(async move {
#[cfg(unix)]
{
use tokio::signal::unix::{signal, SignalKind};
let mut sigterm = signal(SignalKind::terminate()).unwrap();
tokio::select! {
_ = tokio::signal::ctrl_c() => {
println!("SIGINT received");
}
_ = sigterm.recv() => {
println!("SIGTERM received");
}
}
}
#[cfg(not(unix))]
{
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
println!("SIGINT received");
}
shutdown_handle_clone.shutdown().await;
});
// Server handles graceful shutdown automatically
// server.run_tcp("0.0.0.0:8080").await?;
Sourcepub async fn run_stdio(self) -> ServerResult<()>
pub async fn run_stdio(self) -> ServerResult<()>
Run the server with STDIO transport
Sourcepub async fn health(&self) -> HealthStatus
pub async fn health(&self) -> HealthStatus
Get health status
Trait Implementations§
Auto Trait Implementations§
impl Freeze for McpServer
impl !RefUnwindSafe for McpServer
impl Send for McpServer
impl Sync for McpServer
impl Unpin for McpServer
impl !UnwindSafe for McpServer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more