Allow Client#call_tool to accept a tool name
## Motivation and Context
`Client#call_tool` previously required an `MCP::Client::Tool` object to call a tool.
This made the API harder to use compared to other MCP SDK implementations,
which accept a tool name string directly:
- Python SDK: `session.call_tool(name, arguments)` accepts a name string.
https://github.com/modelcontextprotocol/python-sdk/blob/v1.26.0/src/mcp/client/session.py#L370
- TypeScript SDK: `client.callTool({ name, arguments })` accepts a name string.
ccb78f2/packages/client/src/client/client.ts (L834)
## How Has This Been Tested?
Updated existing tests and added new tests.
## Breaking Change
No. This change adds a `name:` parameter so callers can pass a tool name string
directly without looking up a `Tool` object first. The existing `tool:` parameter
continues to work for backward compatibility.
This commit is contained in:
parent
1b8e45fef3
commit
73070f1fd7
1 changed files with 37 additions and 3 deletions
|
|
@ -67,6 +67,32 @@ module MCP
|
|||
assert_equal([{ type: "text", text: "Hello, world!" }], content)
|
||||
end
|
||||
|
||||
def test_call_tool_by_name
|
||||
transport = mock
|
||||
arguments = { foo: "bar" }
|
||||
mock_response = {
|
||||
"result" => { "content" => [{ "type": "text", "text": "Hello, world!" }] },
|
||||
}
|
||||
|
||||
transport.expects(:send_request).with do |args|
|
||||
args.dig(:request, :params, :name) == "tool1" &&
|
||||
args.dig(:request, :params, :arguments) == arguments
|
||||
end.returns(mock_response).once
|
||||
|
||||
client = Client.new(transport: transport)
|
||||
result = client.call_tool(name: "tool1", arguments: arguments)
|
||||
content = result.dig("result", "content")
|
||||
|
||||
assert_equal([{ type: "text", text: "Hello, world!" }], content)
|
||||
end
|
||||
|
||||
def test_call_tool_raises_when_no_name_or_tool
|
||||
client = Client.new(transport: mock)
|
||||
|
||||
error = assert_raises(ArgumentError) { client.call_tool(arguments: { foo: "bar" }) }
|
||||
assert_equal("Either `name:` or `tool:` must be provided.", error.message)
|
||||
end
|
||||
|
||||
def test_resources_sends_request_to_transport_and_returns_resources_array
|
||||
transport = mock
|
||||
mock_response = {
|
||||
|
|
|
|||
Loading…
Reference in a new issue