Interface McpToolResultSanitizer

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

Thread-safe server-level hook that sanitizes completed MCP tool results, including their payload and application-owned result metadata.

Soklet invokes the hook after handler/interceptor processing and after checking that the payload is McpToolOutput, but before remaining result validation, output-schema validation, structured-content mirroring, envelope construction, and serialization. Only the returned result's payload and metadata are used by subsequent validation, budgets, and writing. Exceptions, null results, or non-tool payloads fail closed without exposing original or partial result data or exception-derived details.

A completed durable tool task is sanitized again on each authorized detailed tasks/get request, potentially on different server nodes. The supplied context belongs to the independently admitted polling request; the original tool name and raw arguments come from persisted task origin. The caller-specific result does not replace the persisted result. Implementations must be safe for concurrent invocation, deterministic and idempotent for repeated equivalent calls, and free of one-shot side effects. Applications may compose a sanitizer chain behind this one hook. Framework-generated tool input-validation and Java binding errors for McpProtocolVersion.V2025_11_25 and McpProtocolVersion.V2026_07_28 also traverse this hook with fixed, instance-free error text. Their raw arguments remain application-owned input; sanitizers must not copy unsafe input into client-visible feedback.

For selective changes, start with McpCompleteResult.toBuilder() and, when changing tool output, McpToolOutput.toBuilder(). Result metadata is out-of-band data, not a secret channel; a host may log or forward it. Neither this hook nor Apps metadata grants authorization. This hook does not sanitize task-status metadata, progress notifications, input-required results, or every JSON-RPC response, and does not translate application JSON.

Author:
Mark Allen
  • Method Details

    • sanitize

      Sanitizes one complete tool result.
      Parameters:
      requestContext - immutable current request context; for completed durable tasks, the authorized polling request
      toolName - original invoked tool name
      rawArguments - immutable original raw tool arguments
      completeResult - unsanitized complete result with a tool payload
      Returns:
      non-null result with a tool payload, to validate and serialize
      Throws:
      Exception - if application sanitization fails
    • nonSanitizingInstance

      Returns the shared sanitizer that leaves the supplied result unchanged. No application-level redaction is performed; normal framework validation and output limits still apply.
      Returns:
      shared unchanged-result sanitizer