You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This adds sanity tests to ensure that example code in the `examples/`
directory and `README.md` isn't outright wrong. These are not intended
to serve as unit tests for the example functionality, just as smoke
tests to catch things like API changes that require updating examples.
Including a sanity test ensures that the example code isn't outright
wrong. This is not intended to serve as unit tests for the example
functionality.
Some minor changes are included which facilitate this work:
- Use `console` instead of `bash` codeblock language
`console` highlights `$ ` prefixed lines differently from the
following lines, which clearly distinguishes between commands and
input/output.
- Set `file_fixture_path`
This allows us to use `ActiveSupport::TestCase#file_fixture`.
- Add `ReadmeTestHelper`
This helper provides utilities for extracting code snippets from
`README.md`.
Some of these tests revealed that either the examples were busted, or
even bugs in the implementation.
- Test README per-server configuration example
This test revealed that the `define_` helper methods were failing to
ensure the server supports the type of capability they were
defining.
- Test README protocol version examples
This revealed that the `protocol_version` accessors weren't
available on the `Server` at all, and the examples were incorrect.
- Test README tool definition examples
This revealed that the `define` example doesn't work, and the fix is unclear.
The server's protocol version can be overridden using the `protocol_version` class method:
278
+
The server's protocol version can be overridden via the `Configuration#protocol_version` method:
271
279
280
+
<!-- SNIPPET ID: set_server_protocol_version -->
272
281
```ruby
273
-
MCP::Server.protocol_version ="2024-11-05"
282
+
MCP.configure do |config|
283
+
config.protocol_version ="2024-11-05"
284
+
end
274
285
```
275
286
276
287
This will make all new server instances use the specified protocol version instead of the default version. The protocol version can be reset to the default by setting it to `nil`:
Be sure to check the [MCP spec](https://modelcontextprotocol.io/specification/2025-03-26) for the protocol version to understand the supported features for the version being set.
@@ -309,6 +323,7 @@ This gem provides a `MCP::Tool` class that can be used to create tools in two wa
309
323
310
324
1. As a class definition:
311
325
326
+
<!-- SNIPPET ID: tool_class_definition -->
312
327
```ruby
313
328
classMyTool < MCP::Tool
314
329
description "This tool performs specific functionality..."
@@ -336,6 +351,7 @@ tool = MyTool
336
351
337
352
2. By using the `MCP::Tool.define` method with a block:
338
353
354
+
<!-- SNIPPET ID: tool_definition_with_block -->
339
355
```ruby
340
356
tool =MCP::Tool.define(
341
357
name:"my_tool",
@@ -372,12 +388,13 @@ The `MCP::Prompt` class provides two ways to create prompts:
372
388
373
389
1. As a class definition with metadata:
374
390
391
+
<!-- SNIPPET ID: prompt_class_definition -->
375
392
```ruby
376
393
classMyPrompt < MCP::Prompt
377
394
prompt_name "my_prompt"# Optional - defaults to underscored class name
378
395
description "This prompt performs specific functionality..."
379
396
arguments [
380
-
Prompt::Argument.new(
397
+
MCP::Prompt::Argument.new(
381
398
name:"message",
382
399
description:"Input message",
383
400
required:true
@@ -386,16 +403,16 @@ class MyPrompt < MCP::Prompt
386
403
387
404
class << self
388
405
deftemplate(args, server_context:)
389
-
Prompt::Result.new(
406
+
MCP::Prompt::Result.new(
390
407
description:"Response description",
391
408
messages: [
392
-
Prompt::Message.new(
409
+
MCP::Prompt::Message.new(
393
410
role:"user",
394
-
content:Content::Text.new("User message")
411
+
content:MCP::Content::Text.new("User message")
395
412
),
396
-
Prompt::Message.new(
413
+
MCP::Prompt::Message.new(
397
414
role:"assistant",
398
-
content:Content::Text.new(args["message"])
415
+
content:MCP::Content::Text.new(args[:message])
399
416
)
400
417
]
401
418
)
@@ -408,28 +425,29 @@ prompt = MyPrompt
408
425
409
426
2. Using the `MCP::Prompt.define` method:
410
427
428
+
<!-- SNIPPET ID: prompt_definition_with_block -->
411
429
```ruby
412
430
prompt =MCP::Prompt.define(
413
431
name:"my_prompt",
414
432
description:"This prompt performs specific functionality...",
415
433
arguments: [
416
-
Prompt::Argument.new(
434
+
MCP::Prompt::Argument.new(
417
435
name:"message",
418
436
description:"Input message",
419
437
required:true
420
438
)
421
439
]
422
440
) do |args, server_context:|
423
-
Prompt::Result.new(
441
+
MCP::Prompt::Result.new(
424
442
description:"Response description",
425
443
messages: [
426
-
Prompt::Message.new(
444
+
MCP::Prompt::Message.new(
427
445
role:"user",
428
-
content:Content::Text.new("User message")
446
+
content:MCP::Content::Text.new("User message")
429
447
),
430
-
Prompt::Message.new(
448
+
MCP::Prompt::Message.new(
431
449
role:"assistant",
432
-
content:Content::Text.new(args["message"])
450
+
content:MCP::Content::Text.new(args[:message])
433
451
)
434
452
]
435
453
)
@@ -450,6 +468,7 @@ e.g. around authentication state or user preferences.
450
468
451
469
Register prompts with the MCP server:
452
470
471
+
<!-- SNIPPET ID: prompts_usage -->
453
472
```ruby
454
473
server =MCP::Server.new(
455
474
name:"my_server",
@@ -468,6 +487,7 @@ The server will handle prompt listing and execution through the MCP protocol met
468
487
The server allows registering a callback to receive information about instrumentation.
469
488
To register a handler pass a proc/lambda to as `instrumentation_callback` into the server constructor.
0 commit comments