Skip to content

Commit 7bee412

Browse files
committed
Test README server context example
1 parent abce3e1 commit 7bee412

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ server_context: { [String, Symbol] => Any }
188188
```
189189

190190
**Example:**
191+
192+
<!-- SNIPPET ID: server_context -->
191193
```ruby
192194
server = MCP::Server.new(
193195
name: "my_server",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require "mcp"
4+
5+
current_user = Object.new
6+
def current_user.id = 123
7+
8+
request = Object.new
9+
def request.uuid = "...uuid..."
10+
11+
b = binding
12+
13+
eval(File.read("code_snippet.rb"), b) # rubocop:disable Security/Eval -- We need to run the snippet in this context to extract the server
14+
15+
server = b.local_variable_get(:server)
16+
17+
puts server.server_context.to_json

test/integration/readme_code_snippets_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ class ReadmeCodeSnippetsTest < ActiveSupport::TestCase
117117
assert_equal(expected_output, stdout)
118118
end
119119

120+
test "Server Context example works exactly as documented in README" do
121+
assert_json_lines(
122+
[
123+
{ user_id: 123, request_id: "...uuid..." },
124+
],
125+
run_code_snippet("server_context"),
126+
)
127+
end
128+
120129
private
121130

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

0 commit comments

Comments
 (0)