feat: add opt-in tool output validation
This commit is contained in:
parent
d1e0afa158
commit
407b064140
1 changed files with 245 additions and 2 deletions
|
|
@ -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