ServerHandler

Trait ServerHandler 

Source
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§

Source

fn server_info(&self) -> ServerInfo

Return information about this server.

This is called during the initialization handshake.

Provided Methods§

Source

fn capabilities(&self) -> ServerCapabilities

Return the capabilities of this server.

The default implementation returns empty capabilities. Override this to advertise specific capabilities.

Source

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.

Source

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.

Source

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.

Implementations on Foreign Types§

Source§

impl<T: ServerHandler> ServerHandler for Arc<T>

Source§

fn server_info(&self) -> ServerInfo

Source§

fn capabilities(&self) -> ServerCapabilities

Source§

fn instructions(&self) -> Option<String>

Source§

fn on_initialized(&self, ctx: &Context<'_>) -> impl Future<Output = ()> + Send

Source§

fn on_shutdown(&self) -> impl Future<Output = ()> + Send

Implementors§