pub struct MiddlewareChain { /* private fields */ }
Expand description
Chain of middleware handlers.
§Examples
use pmcp::shared::{MiddlewareChain, LoggingMiddleware, AuthMiddleware, RetryMiddleware};
use pmcp::types::{JSONRPCRequest, JSONRPCResponse, RequestId};
use std::sync::Arc;
use tracing::Level;
// Create a middleware chain
let mut chain = MiddlewareChain::new();
// Add different types of middleware in order
chain.add(Arc::new(LoggingMiddleware::new(Level::INFO)));
chain.add(Arc::new(AuthMiddleware::new("Bearer token-123".to_string())));
chain.add(Arc::new(RetryMiddleware::default()));
// Create a request to process
let mut request = JSONRPCRequest {
jsonrpc: "2.0".to_string(),
method: "prompts.get".to_string(),
params: Some(serde_json::json!({
"name": "code_review",
"arguments": {"language": "rust", "style": "detailed"}
})),
id: RequestId::from(1001i64),
};
// Process request through all middleware in order
chain.process_request(&mut request).await?;
// Create a response to process
let mut response = JSONRPCResponse {
jsonrpc: "2.0".to_string(),
id: RequestId::from(1001i64),
payload: pmcp::types::jsonrpc::ResponsePayload::Result(
serde_json::json!({"prompt": "Review the following code..."})
),
};
// Process response through all middleware
chain.process_response(&mut response).await?;
// The chain processes middleware in the order they were added
// 1. LoggingMiddleware logs the request/response
// 2. AuthMiddleware adds authentication
// 3. RetryMiddleware configures retry behavior
Implementations§
Source§impl MiddlewareChain
impl MiddlewareChain
Sourcepub fn add(&mut self, middleware: Arc<dyn Middleware>)
pub fn add(&mut self, middleware: Arc<dyn Middleware>)
Add a middleware to the chain.
Sourcepub async fn process_request(&self, request: &mut JSONRPCRequest) -> Result<()>
pub async fn process_request(&self, request: &mut JSONRPCRequest) -> Result<()>
Process a request through all middleware.
Sourcepub async fn process_response(
&self,
response: &mut JSONRPCResponse,
) -> Result<()>
pub async fn process_response( &self, response: &mut JSONRPCResponse, ) -> Result<()>
Process a response through all middleware.
Sourcepub async fn process_send(&self, message: &TransportMessage) -> Result<()>
pub async fn process_send(&self, message: &TransportMessage) -> Result<()>
Process an outgoing message through all middleware.
Sourcepub async fn process_receive(&self, message: &TransportMessage) -> Result<()>
pub async fn process_receive(&self, message: &TransportMessage) -> Result<()>
Process an incoming message through all middleware.
Trait Implementations§
Source§impl Debug for MiddlewareChain
impl Debug for MiddlewareChain
Auto Trait Implementations§
impl Freeze for MiddlewareChain
impl !RefUnwindSafe for MiddlewareChain
impl Send for MiddlewareChain
impl Sync for MiddlewareChain
impl Unpin for MiddlewareChain
impl !UnwindSafe for MiddlewareChain
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