Skip to content

Commit a84b56c

Browse files
authored
Merge pull request #85 from SheldonNunes/sheldon.fix-tools-with-no-args
Fix tools with no args
2 parents 6bace86 + 7473722 commit a84b56c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/mcp/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def accepts_server_context?(method_object)
314314
end
315315

316316
def call_tool_with_args(tool, arguments)
317-
args = arguments.transform_keys(&:to_sym)
317+
args = arguments&.transform_keys(&:to_sym) || {}
318318

319319
if accepts_server_context?(tool.method(:call))
320320
tool.call(**args, server_context: server_context).to_h

test/mcp/server_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ class ServerTest < ActiveSupport::TestCase
1717
input_schema: { type: "object", properties: { message: { type: "string" } }, required: ["message"] },
1818
) { raise StandardError, "Tool error" }
1919

20+
@tool_with_no_args = Tool.define(
21+
name: "tool_with_no_args",
22+
description: "This tool performs specific functionality...",
23+
annotations: {
24+
title: "Tool with no args",
25+
read_only_hint: true,
26+
},
27+
) do
28+
Tool::Response.new([{ type: "text", content: "OK" }])
29+
end
30+
2031
@prompt = Prompt.define(
2132
name: "test_prompt",
2233
description: "Test prompt",
@@ -896,6 +907,27 @@ def call(message:, server_context: nil)
896907
assert_equal "OK", response[:result][:content][0][:content]
897908
end
898909

910+
test "tools/call with no args" do
911+
server = Server.new(tools: [@tool_with_no_args])
912+
913+
response = server.handle(
914+
{
915+
jsonrpc: "2.0",
916+
id: 1,
917+
method: "tools/call",
918+
params: {
919+
name: "tool_with_no_args",
920+
},
921+
},
922+
)
923+
924+
assert_equal "2.0", response[:jsonrpc]
925+
assert_equal 1, response[:id]
926+
assert response[:result], "Expected result key in response"
927+
assert_equal "text", response[:result][:content][0][:type]
928+
assert_equal "OK", response[:result][:content][0][:content]
929+
end
930+
899931
class TestTool < Tool
900932
tool_name "test_tool"
901933
description "a test tool for testing"

0 commit comments

Comments
 (0)