macro_rules! with_context {
($ctx:expr, $body:expr) => { ... };
}Expand description
Macro to run code with request context.
ยงExamples
use pmcp::{with_context, shared::context::RequestContext};
use pmcp::types::RequestId;
// Create a context
let context = RequestContext::new(RequestId::from(123i64))
.with_user_id("user-456".to_string())
.with_metadata("region".to_string(), serde_json::json!("us-east-1"));
// Run async code with the context
let result = with_context!(context, {
// This code runs with the context available
let current = RequestContext::current().expect("Context should be set");
assert_eq!(current.user_id, Some("user-456".to_string()));
// Perform some async operation
async {
// The context is available in nested async blocks too
let ctx = RequestContext::current().unwrap();
assert_eq!(ctx.request_id, RequestId::from(123i64));
42
}.await
});
assert_eq!(result, 42);