Skip to content

Commit 90f7746

Browse files
committed
Test Configuration README example
1 parent 99fd787 commit 90f7746

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ $ ./examples/stdio_server.rb
134134

135135
The gem can be configured using the `MCP.configure` block:
136136

137+
<!-- SNIPPET ID: configuration -->
137138
```ruby
138139
MCP.configure do |config|
139140
config.exception_reporter = ->(exception, server_context) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require "mcp"
4+
5+
# Stub Bugsnag
6+
class Bugsnag
7+
class Report
8+
attr_reader :metadata
9+
10+
def initialize
11+
@metadata = {}
12+
end
13+
14+
def add_metadata(key, value)
15+
@metadata[key] = value
16+
end
17+
end
18+
19+
class << self
20+
def notify(exception)
21+
report = Report.new
22+
yield report
23+
puts "Bugsnag notified of #{exception.inspect} with metadata #{report.metadata.inspect}"
24+
end
25+
end
26+
end
27+
28+
require_relative "code_snippet"
29+
30+
puts MCP::Server.new(
31+
tools: [
32+
MCP::Tool.define(name: "error_tool") { raise "boom" },
33+
],
34+
).handle_json(
35+
{
36+
jsonrpc: "2.0",
37+
id: "1",
38+
method: "tools/call",
39+
params: { name: "error_tool", arguments: {} },
40+
}.to_json,
41+
)

test/integration/readme_code_snippets_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,32 @@ class ReadmeCodeSnippetsTest < ActiveSupport::TestCase
6363
)
6464
end
6565

66+
test "Configuration example works exactly as documented in README" do
67+
stdout = run_code_snippet("configuration")
68+
69+
request = {
70+
jsonrpc: "2.0",
71+
id: "1",
72+
method: "tools/call",
73+
params: { name: "error_tool", arguments: {} },
74+
}.to_json
75+
76+
error = MCP::Server::RequestHandlerError.new("Internal error calling tool error_tool", request)
77+
metadata = { model_context_protocol: { request: } }
78+
79+
response = {
80+
jsonrpc: "2.0",
81+
id: "1",
82+
error: { code: -32603, message: "Internal error", data: "Internal error calling tool error_tool" },
83+
}.to_json
84+
85+
assert_equal(<<~STDOUT, stdout)
86+
Bugsnag notified of #{error.inspect} with metadata #{metadata.inspect}
87+
Got instrumentation data {method: "tools/call", tool_name: "error_tool", error: :internal_error, duration: 1.23}
88+
#{response}
89+
STDOUT
90+
end
91+
6692
private
6793

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

0 commit comments

Comments
 (0)