|
| 1 | +# typed: true |
1 | 2 | # frozen_string_literal: true |
2 | 3 |
|
3 | 4 | require "test_helper" |
@@ -256,6 +257,43 @@ class ServerTest < ActiveSupport::TestCase |
256 | 257 | assert_instrumentation_data({ method: "tools/call", tool_name: }) |
257 | 258 | end |
258 | 259 |
|
| 260 | + test "#handle_json tools/call executes tool and returns result, when the tool is typed with Sorbet" do |
| 261 | + class TypedTestTool < Tool |
| 262 | + tool_name "test_tool" |
| 263 | + description "a test tool for testing" |
| 264 | + input_schema({ properties: { message: { type: "string" } }, required: ["message"] }) |
| 265 | + |
| 266 | + class << self |
| 267 | + extend T::Sig |
| 268 | + |
| 269 | + sig { params(message: String, server_context: T.nilable(T.untyped)).returns(Tool::Response) } |
| 270 | + def call(message:, server_context: nil) |
| 271 | + Tool::Response.new([{ type: "text", content: "OK" }]) |
| 272 | + end |
| 273 | + end |
| 274 | + end |
| 275 | + |
| 276 | + request = JSON.generate({ |
| 277 | + jsonrpc: "2.0", |
| 278 | + method: "tools/call", |
| 279 | + params: { name: "test_tool", arguments: { message: "Hello, world!" } }, |
| 280 | + id: 1, |
| 281 | + }) |
| 282 | + |
| 283 | + server = Server.new( |
| 284 | + name: @server_name, |
| 285 | + tools: [TypedTestTool], |
| 286 | + prompts: [@prompt], |
| 287 | + resources: [@resource], |
| 288 | + resource_templates: [@resource_template], |
| 289 | + ) |
| 290 | + |
| 291 | + raw_response = server.handle_json(request) |
| 292 | + response = JSON.parse(raw_response, symbolize_names: true) if raw_response |
| 293 | + |
| 294 | + assert_equal({ content: [{ type: "text", content: "OK" }], isError: false }, response[:result]) |
| 295 | + end |
| 296 | + |
259 | 297 | test "#handle tools/call returns internal error and reports exception if the tool raises an error" do |
260 | 298 | @server.configuration.exception_reporter.expects(:call).with do |exception, server_context| |
261 | 299 | assert_not_nil exception |
|
0 commit comments