feat: add opt-in tool output validation

This commit is contained in:
Ates Goral 2026-05-11 22:04:15 -04:00
commit 407b064140

View file

@ -16,7 +16,7 @@ module MCP
attr_writer :instrumentation_callback
def initialize(exception_reporter: nil, around_request: nil, instrumentation_callback: nil, protocol_version: nil,
validate_tool_call_arguments: true)
validate_tool_call_arguments: true, validate_tool_call_results: false)
@exception_reporter = exception_reporter
@around_request = around_request
@instrumentation_callback = instrumentation_callback
@ -25,8 +25,10 @@ module MCP
validate_protocol_version!(protocol_version)
end
validate_value_of_validate_tool_call_arguments!(validate_tool_call_arguments)
validate_value_of_validate_tool_call_results!(validate_tool_call_results)
@validate_tool_call_arguments = validate_tool_call_arguments
@validate_tool_call_results = validate_tool_call_results
end
def protocol_version=(protocol_version)
@ -41,6 +43,12 @@ module MCP
@validate_tool_call_arguments = validate_tool_call_arguments
end
def validate_tool_call_results=(validate_tool_call_results)
validate_value_of_validate_tool_call_results!(validate_tool_call_results)
@validate_tool_call_results = validate_tool_call_results
end
def protocol_version
@protocol_version || LATEST_STABLE_PROTOCOL_VERSION
end
@ -80,11 +88,16 @@ module MCP
end
attr_reader :validate_tool_call_arguments
attr_reader :validate_tool_call_results
def validate_tool_call_arguments?
!!@validate_tool_call_arguments
end
def validate_tool_call_results?
!!@validate_tool_call_results
end
def merge(other)
return self if other.nil?
@ -113,6 +126,7 @@ module MCP
end
validate_tool_call_arguments = other.validate_tool_call_arguments
validate_tool_call_results = other.validate_tool_call_results
Configuration.new(
exception_reporter: exception_reporter,
@ -120,6 +134,7 @@ module MCP
instrumentation_callback: instrumentation_callback,
protocol_version: protocol_version,
validate_tool_call_arguments: validate_tool_call_arguments,
validate_tool_call_results: validate_tool_call_results,
)
end
@ -138,6 +153,12 @@ module MCP
end
end
def validate_value_of_validate_tool_call_results!(validate_tool_call_results)
unless validate_tool_call_results.is_a?(TrueClass) || validate_tool_call_results.is_a?(FalseClass)
raise ArgumentError, "validate_tool_call_results must be a boolean"
end
end
def default_exception_reporter
@default_exception_reporter ||= ->(exception, server_context) {}
end