with_plugins

Macro with_plugins 

Source
macro_rules! with_plugins {
    ($client:expr, $method:expr, $request_data:expr, $protocol_call:block) => { ... };
}
Expand description

Execute a protocol call with full plugin middleware support

This macro handles the complete plugin execution pipeline:

  1. Creates RequestContext
  2. Executes before_request plugin chain
  3. Executes the provided protocol call
  4. Creates ResponseContext
  5. Executes after_response plugin chain
  6. Returns the final result

§Usage

use turbomcp_client::with_plugins;
use std::collections::HashMap;

impl Client {
    pub async fn call_tool(&mut self, name: &str, args: Option<HashMap<String, serde_json::Value>>) -> turbomcp_core::Result<serde_json::Value> {
        let request_data = turbomcp_protocol::types::CallToolRequest {
            name: name.to_string(),
            arguments: Some(args.unwrap_or_default()),
        };

        with_plugins!(self, "tools/call", request_data, {
            // Your protocol call here - plugins execute automatically
            let result: turbomcp_protocol::types::CallToolResult = self.protocol
                .request("tools/call", Some(serde_json::to_value(&request_data)?))
                .await?;
             
            Ok(self.extract_tool_content(&result))
        })
    }
}

The macro automatically:

  • ✅ Creates proper RequestContext with JSON-RPC structure
  • ✅ Executes all registered plugins before the request
  • ✅ Times the operation for metrics
  • ✅ Creates ResponseContext with results and timing
  • ✅ Executes all registered plugins after the response
  • ✅ Handles errors gracefully with proper context
  • ✅ Returns the final processed result