Skip to content

Commit 2e3d9cf

Browse files
committed
[WIP] Test README tool definition examples
This revealed that the `define` example doesn't work, and the fix is unclear.
1 parent 87a8653 commit 2e3d9cf

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ This gem provides a `MCP::Tool` class that can be used to create tools in two wa
284284

285285
1. As a class definition:
286286

287+
<!-- SNIPPET ID: tool_class_definition -->
287288
```ruby
288289
class MyTool < MCP::Tool
289290
description "This tool performs specific functionality..."
@@ -311,6 +312,7 @@ tool = MyTool
311312

312313
2. By using the `MCP::Tool.define` method with a block:
313314

315+
<!-- SNIPPET ID: tool_definition_with_block -->
314316
```ruby
315317
tool = MCP::Tool.define(
316318
name: "my_tool",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require "mcp"
4+
5+
require_relative "code_snippet"
6+
7+
b = binding
8+
eval(File.read("code_snippet.rb"), b) # rubocop:disable Security/Eval -- We need to run the snippet in this context to extract the tool
9+
tool = b.local_variable_get(:tool)
10+
11+
MCP.configure do |config|
12+
config.exception_reporter = ->(exception, server_context) do
13+
$stderr.puts "Exception: #{exception.inspect}"
14+
$stderr.puts "Exception cause: #{exception.cause.inspect}"
15+
end
16+
end
17+
18+
puts MCP::Server.new(tools: [tool]).handle_json(
19+
{
20+
jsonrpc: "2.0",
21+
id: "1",
22+
method: "tools/call",
23+
params: { name: "my_tool", arguments: { message: "Hello, world!" } },
24+
}.to_json,
25+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tool_class_definition.rb

test/integration/readme_code_snippets_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ class ReadmeCodeSnippetsTest < ActiveSupport::TestCase
137137
assert_equal(MCP::Configuration::DEFAULT_PROTOCOL_VERSION, run_code_snippet("unset_server_protocol_version").chomp)
138138
end
139139

140+
test "Tools examples work exactly as documented in README" do
141+
assert_json_lines(
142+
[
143+
{ jsonrpc: "2.0", id: "1", result: { content: [{ type: "text", text: "OK" }], isError: false } },
144+
],
145+
run_code_snippet("tool_class_definition"),
146+
)
147+
148+
skip "FIXME: this next code snippet is invalid and there doesn't seem to be a way to make both pass..."
149+
150+
assert_json_lines(
151+
[
152+
{ jsonrpc: "2.0", id: "1", result: { content: [{ type: "text", text: "OK" }], isError: false } },
153+
],
154+
run_code_snippet("tool_definition_with_block"),
155+
)
156+
end
157+
140158
private
141159

142160
def assert_json_lines(expected, actual, message = "Expected the given JSON lines")

0 commit comments

Comments
 (0)