pub trait ResponseTransformer: Send + Sync {
// Required methods
fn transform_response(&self, response: Value) -> Value;
fn transform_schema(&self, schema: Value) -> Value;
}Expand description
Transforms tool responses and their corresponding schemas.
Implementors must ensure transform_response and
transform_schema are consistent - if a field is removed
from responses, it should also be removed from the schema.
§Usage
Response transformers can be applied globally to all tools or per-tool:
use std::sync::Arc;
use rmcp_openapi::{Server, ResponseTransformer};
// Global transformer
let mut server = Server::builder()
.openapi_spec(spec)
.base_url(url)
.response_transformer(Arc::new(RemoveNulls))
.build();
server.load_openapi_spec()?;
// Per-tool override
server.set_tool_transformer("verbose-endpoint", Arc::new(AggressiveFilter))?;§Resolution Order
- Per-tool transformer (if set) takes precedence
- Else global server transformer
- Else no transformation
Required Methods§
Sourcefn transform_response(&self, response: Value) -> Value
fn transform_response(&self, response: Value) -> Value
Transform the response body before returning to MCP client.
This method is called after each successful tool call to transform
the JSON response body. The transformation should be consistent with
transform_schema - any fields removed here
should also be removed from the schema.
§Arguments
response- The JSON response body from the HTTP call
§Returns
The transformed response body
Sourcefn transform_schema(&self, schema: Value) -> Value
fn transform_schema(&self, schema: Value) -> Value
Transform the output schema to match response transformations.
This method is called when:
- Loading the OpenAPI spec (for global transformers)
- Setting a per-tool transformer
The schema transformation should reflect what transform_response
will do to the actual responses.
§Arguments
schema- The JSON Schema for the tool’s output
§Returns
The transformed schema