diff --git a/lib/mcp/client.rb b/lib/mcp/client.rb index 4deeefa4..1d20b80e 100644 --- a/lib/mcp/client.rb +++ b/lib/mcp/client.rb @@ -478,7 +478,7 @@ def prompts(cancellation: nil) # # @param name [String] The name of the tool to call. # @param tool [MCP::Client::Tool] The tool to be called. - # @param arguments [Object, nil] The arguments to pass to the tool. + # @param arguments [Object, nil] The arguments to pass to the tool. Omitted from the request when nil. # @param progress_token [String, Integer, nil] A token to request progress notifications from the server during tool execution. # @param meta [Hash, nil] Additional `_meta` entries to send with the request, # e.g. the W3C Trace Context keys reserved by SEP-414 @@ -516,7 +516,8 @@ def call_tool(name: nil, tool: nil, arguments: nil, progress_token: nil, meta: n tool_name = name || tool&.name raise ArgumentError, "Either `name:` or `tool:` must be provided." unless tool_name - params = { name: tool_name, arguments: arguments } + params = { name: tool_name } + params[:arguments] = arguments unless arguments.nil? meta_entries = meta ? meta.dup : {} if progress_token meta_entries.delete("progressToken") diff --git a/test/mcp/client_test.rb b/test/mcp/client_test.rb index 5864a9af..8cef8be0 100644 --- a/test/mcp/client_test.rb +++ b/test/mcp/client_test.rb @@ -571,6 +571,15 @@ def test_call_tool_sends_request_to_transport_and_returns_content assert_equal([{ type: "text", text: "Hello, world!" }], content) end + def test_call_tool_omits_arguments_when_none_are_given + transport = mock + transport.expects(:send_request).with do |args| + args.dig(:request, :params) == { name: "ping" } + end.returns({ "result" => { "content" => [] } }) + + Client.new(transport: transport).call_tool(name: "ping") + end + def test_call_tool_by_name transport = mock arguments = { foo: "bar" }