Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 18 additions & 28 deletions lib/mcp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,56 +262,36 @@ def call_tool(request)
tool = tools[tool_name]
unless tool
add_instrumentation_data(tool_name:, error: :tool_not_found)
return Tool::Response.new(
[{
type: "text",
text: "Tool not found: #{tool_name}",
}],
error: true,
).to_h

return error_tool_response("Tool not found: #{tool_name}")
end

arguments = request[:arguments] || {}
add_instrumentation_data(tool_name:)

if tool.input_schema&.missing_required_arguments?(arguments)
add_instrumentation_data(error: :missing_required_arguments)

missing = tool.input_schema.missing_required_arguments(arguments).join(", ")
return Tool::Response.new(
[{
type: "text",
text: "Missing required arguments: #{missing}",
}],
error: true,
).to_h
return error_tool_response("Missing required arguments: #{missing}")
end

if configuration.validate_tool_call_arguments && tool.input_schema
begin
tool.input_schema.validate_arguments(arguments)
rescue Tool::InputSchema::ValidationError => e
add_instrumentation_data(error: :invalid_schema)
return Tool::Response.new(
[{
type: "text",
text: e.message,
}],
error: true,
).to_h

return error_tool_response(e.message)
end
end

begin
call_tool_with_args(tool, arguments)
rescue => e
report_exception(e, { request: request })
Tool::Response.new(
[{
type: "text",
text: "Internal error calling tool #{tool_name}: #{e.message}",
}],
error: true,
).to_h

error_tool_response("Internal error calling tool #{tool_name}: #{e.message}")
end
end

Expand Down Expand Up @@ -359,6 +339,16 @@ def index_resources_by_uri(resources)
end
end

def error_tool_response(text)
Tool::Response.new(
[{
type: "text",
text: text,
}],
error: true,
).to_h
end

def accepts_server_context?(method_object)
parameters = method_object.parameters

Expand Down