feat: add opt-in tool output validation
This commit is contained in:
parent
d1e0afa158
commit
407b064140
2 changed files with 245 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -586,9 +586,11 @@ module MCP
|
|||
|
||||
progress_token = request.dig(:_meta, :progressToken)
|
||||
|
||||
call_tool_with_args(
|
||||
result = call_tool_with_args(
|
||||
tool, arguments, server_context_with_meta(request), progress_token: progress_token, session: session, related_request_id: related_request_id, cancellation: cancellation
|
||||
)
|
||||
validate_tool_call_result!(tool, result)
|
||||
result
|
||||
rescue RequestHandlerError, CancelledError
|
||||
# CancelledError is intentionally not wrapped so `handle_request` can turn it into
|
||||
# `JsonRpcHandler::NO_RESPONSE` per the MCP cancellation spec.
|
||||
|
|
@ -745,6 +747,14 @@ module MCP
|
|||
).to_h
|
||||
end
|
||||
|
||||
def validate_tool_call_result!(tool, result)
|
||||
return unless configuration.validate_tool_call_results
|
||||
return unless tool.output_schema
|
||||
return if result[:isError]
|
||||
|
||||
tool.output_schema.validate_result(result[:structuredContent])
|
||||
end
|
||||
|
||||
# Whether a tool/prompt handler opts in to receiving an `MCP::ServerContext`.
|
||||
# Recognizes `:keyrest` (`**kwargs`) because tools are invoked without a positional argument
|
||||
# (`tool.call(**args, server_context:)`), soa `**kwargs`-only signature safely captures `server_context:`.
|
||||
|
|
|
|||
Loading…
Reference in a new issue