pub trait ServerHandler: Send + Sync {
// Required method
fn server_info(&self) -> ServerInfo;
// Provided methods
fn capabilities(&self) -> ServerCapabilities { ... }
fn instructions(&self) -> Option<String> { ... }
fn on_initialized(
&self,
_ctx: &Context<'_>,
) -> impl Future<Output = ()> + Send { ... }
fn on_shutdown(&self) -> impl Future<Output = ()> + Send { ... }
}Expand description
Core server handler trait - required for all MCP servers.
This trait defines the minimal requirements for an MCP server. All servers must implement this trait. Additional capabilities are added by implementing optional handler traits.
Note: Context uses lifetime references (no 'static requirement).
Required Methods§
Sourcefn server_info(&self) -> ServerInfo
fn server_info(&self) -> ServerInfo
Return information about this server.
This is called during the initialization handshake.
Provided Methods§
Sourcefn capabilities(&self) -> ServerCapabilities
fn capabilities(&self) -> ServerCapabilities
Return the capabilities of this server.
The default implementation returns empty capabilities. Override this to advertise specific capabilities.
Sourcefn instructions(&self) -> Option<String>
fn instructions(&self) -> Option<String>
Return optional instructions for using this server.
These instructions are sent to the client during initialization and can help the AI assistant understand how to use this server.
Sourcefn on_initialized(&self, _ctx: &Context<'_>) -> impl Future<Output = ()> + Send
fn on_initialized(&self, _ctx: &Context<'_>) -> impl Future<Output = ()> + Send
Called after initialization is complete.
This is a good place to set up any state that requires the connection to be established.
Sourcefn on_shutdown(&self) -> impl Future<Output = ()> + Send
fn on_shutdown(&self) -> impl Future<Output = ()> + Send
Called when the connection is about to be closed.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.