rust_mcp_sdk/mcp_http/middleware/
logging_middleware.rs

1//! A very simple example middleware for inspiration.
2//!
3//! This demonstrates how to implement a basic logging middleware
4//! using the `Middleware` trait. It logs incoming requests and outgoing
5//! responses. In a real-world application, you might extend this to
6//! include structured logging, tracing, timing, or error reporting.
7use crate::{
8    mcp_http::{types::GenericBody, McpAppState, Middleware, MiddlewareNext},
9    mcp_server::error::TransportServerResult,
10};
11use async_trait::async_trait;
12use http::{Request, Response};
13use std::sync::Arc;
14
15/// A minimal middleware that logs request URIs and response statuses.
16///
17/// This is just a *very, very* simple example meant for inspiration.
18/// It shows how to wrap a request/response cycle inside a middleware layer.
19pub struct LoggingMiddleware;
20
21#[async_trait]
22impl Middleware for LoggingMiddleware {
23    async fn handle<'req>(
24        &self,
25        req: Request<&'req str>,
26        state: Arc<McpAppState>,
27        next: MiddlewareNext<'req>,
28    ) -> TransportServerResult<Response<GenericBody>> {
29        println!("➡️ Logging request: {}", req.uri());
30        let res = next(req, state).await?;
31        println!("⬅️ Logging response: {}", res.status());
32        Ok(res)
33    }
34}