pub trait Middleware: Send + Sync {
// Provided methods
fn on_request(
&self,
_ctx: &McpContext,
_request: &JsonRpcRequest,
) -> McpResult<MiddlewareDecision> { ... }
fn on_response(
&self,
_ctx: &McpContext,
_request: &JsonRpcRequest,
response: Value,
) -> McpResult<Value> { ... }
fn on_error(
&self,
_ctx: &McpContext,
_request: &JsonRpcRequest,
error: McpError,
) -> McpError { ... }
}Expand description
Middleware hook trait for request/response interception.
This is intentionally minimal: synchronous hooks only, with simple short-circuit and response transform capabilities. See the module-level documentation for ordering semantics.
Provided Methods§
Sourcefn on_request(
&self,
_ctx: &McpContext,
_request: &JsonRpcRequest,
) -> McpResult<MiddlewareDecision>
fn on_request( &self, _ctx: &McpContext, _request: &JsonRpcRequest, ) -> McpResult<MiddlewareDecision>
Invoked before routing the request.
Return Respond to skip normal dispatch and return a custom result.
Sourcefn on_response(
&self,
_ctx: &McpContext,
_request: &JsonRpcRequest,
response: Value,
) -> McpResult<Value>
fn on_response( &self, _ctx: &McpContext, _request: &JsonRpcRequest, response: Value, ) -> McpResult<Value>
Invoked after a successful handler result is produced.
Middleware can transform the response value (or return an error).
Sourcefn on_error(
&self,
_ctx: &McpContext,
_request: &JsonRpcRequest,
error: McpError,
) -> McpError
fn on_error( &self, _ctx: &McpContext, _request: &JsonRpcRequest, error: McpError, ) -> McpError
Invoked when a handler or middleware returns an error.
Middleware may rewrite the error before it is sent to the client.