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:
243
+
The server's protocol version can be overridden via the `Configuration#protocol_version` method:
236
244
245
+
<!-- SNIPPET ID: set_server_protocol_version -->
237
246
```ruby
238
-
MCP::Server.protocol_version ="2024-11-05"
247
+
MCP.configure do |config|
248
+
config.protocol_version ="2024-11-05"
249
+
end
239
250
```
240
251
241
252
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://spec.modelcontextprotocol.io/specification/2024-11-05/) for the protocol version to understand the supported features for the version being set.
@@ -274,6 +288,7 @@ This gem provides a `MCP::Tool` class that can be used to create tools in two wa
274
288
275
289
1. As a class definition:
276
290
291
+
<!-- SNIPPET ID: tool_class_definition -->
277
292
```ruby
278
293
classMyTool < MCP::Tool
279
294
description "This tool performs specific functionality..."
@@ -301,6 +316,7 @@ tool = MyTool
301
316
302
317
2. By using the `MCP::Tool.define` method with a block:
303
318
319
+
<!-- SNIPPET ID: tool_definition_with_block -->
304
320
```ruby
305
321
tool =MCP::Tool.define(
306
322
name:"my_tool",
@@ -337,12 +353,13 @@ The `MCP::Prompt` class provides two ways to create prompts:
337
353
338
354
1. As a class definition with metadata:
339
355
356
+
<!-- SNIPPET ID: prompt_class_definition -->
340
357
```ruby
341
358
classMyPrompt < MCP::Prompt
342
359
prompt_name "my_prompt"# Optional - defaults to underscored class name
343
360
description "This prompt performs specific functionality..."
344
361
arguments [
345
-
Prompt::Argument.new(
362
+
MCP::Prompt::Argument.new(
346
363
name:"message",
347
364
description:"Input message",
348
365
required:true
@@ -351,16 +368,16 @@ class MyPrompt < MCP::Prompt
351
368
352
369
class << self
353
370
deftemplate(args, server_context:)
354
-
Prompt::Result.new(
371
+
MCP::Prompt::Result.new(
355
372
description:"Response description",
356
373
messages: [
357
-
Prompt::Message.new(
374
+
MCP::Prompt::Message.new(
358
375
role:"user",
359
-
content:Content::Text.new("User message")
376
+
content:MCP::Content::Text.new("User message")
360
377
),
361
-
Prompt::Message.new(
378
+
MCP::Prompt::Message.new(
362
379
role:"assistant",
363
-
content:Content::Text.new(args["message"])
380
+
content:MCP::Content::Text.new(args[:message])
364
381
)
365
382
]
366
383
)
@@ -373,28 +390,29 @@ prompt = MyPrompt
373
390
374
391
2. Using the `MCP::Prompt.define` method:
375
392
393
+
<!-- SNIPPET ID: prompt_definition_with_block -->
376
394
```ruby
377
395
prompt =MCP::Prompt.define(
378
396
name:"my_prompt",
379
397
description:"This prompt performs specific functionality...",
380
398
arguments: [
381
-
Prompt::Argument.new(
399
+
MCP::Prompt::Argument.new(
382
400
name:"message",
383
401
description:"Input message",
384
402
required:true
385
403
)
386
404
]
387
405
) do |args, server_context:|
388
-
Prompt::Result.new(
406
+
MCP::Prompt::Result.new(
389
407
description:"Response description",
390
408
messages: [
391
-
Prompt::Message.new(
409
+
MCP::Prompt::Message.new(
392
410
role:"user",
393
-
content:Content::Text.new("User message")
411
+
content:MCP::Content::Text.new("User message")
394
412
),
395
-
Prompt::Message.new(
413
+
MCP::Prompt::Message.new(
396
414
role:"assistant",
397
-
content:Content::Text.new(args["message"])
415
+
content:MCP::Content::Text.new(args[:message])
398
416
)
399
417
]
400
418
)
@@ -415,6 +433,7 @@ e.g. around authentication state or user preferences.
415
433
416
434
Register prompts with the MCP server:
417
435
436
+
<!-- SNIPPET ID: prompts_usage -->
418
437
```ruby
419
438
server =MCP::Server.new(
420
439
name:"my_server",
@@ -433,6 +452,7 @@ The server will handle prompt listing and execution through the MCP protocol met
433
452
The server allows registering a callback to receive information about instrumentation.
434
453
To register a handler pass a proc/lambda to as `instrumentation_callback` into the server constructor.
0 commit comments