Skip to content

Commit 99fd787

Browse files
committed
Test Stdio Transport README example
1 parent 0c76496 commit 99fd787

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ end
8484

8585
If you want to build a local command-line application, you can use the stdio transport:
8686

87+
<!-- SNIPPET ID: stdio_transport -->
8788
```ruby
8889
#!/usr/bin/env ruby
8990
require "mcp"

test/integration/readme_code_snippets_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,44 @@ class ReadmeCodeSnippetsTest < ActiveSupport::TestCase
2525
)
2626
end
2727

28+
test "Stdio Transport example works exactly as documented in README" do
29+
# This snippet is a standalone example server/transport, so we run it directly and send it requests
30+
stdout = Tempfile.create(["stdio_transport", ".rb"]) do |file|
31+
FileUtils.chmod("+x", file.path) # Make executable
32+
File.write(file.path, extract_readme_code_snippet("stdio_transport"))
33+
34+
# Reuse example input from README, but drop command to start the server (`$ ...` line)
35+
stdin_data = extract_readme_code_snippet("running_stdio_server", language: "console").lines(chomp: true).grep_v(/^\$\s/).join("\n")
36+
37+
stdout, stderr, status = Open3.capture3(file.path, stdin_data:)
38+
assert_predicate(status, :success?, "Expected #{file.path} to exit with success, but got exit status #{status.exitstatus}\n\nSTDOUT: #{stdout}\n\nSTDERR: #{stderr}")
39+
assert_empty(stderr, "Expected no stderr in: #{stderr}")
40+
refute_empty(stdout, "Expected stdout not to be empty")
41+
42+
stdout
43+
end
44+
45+
assert_json_lines(
46+
[
47+
{ jsonrpc: "2.0", id: "1", result: {} },
48+
{
49+
jsonrpc: "2.0",
50+
id: "2",
51+
result: {
52+
tools: [
53+
{
54+
name: "example_tool",
55+
description: "A simple example tool that echoes back its arguments",
56+
inputSchema: { type: "object", properties: { message: { type: "string" } }, required: ["message"] },
57+
},
58+
],
59+
},
60+
},
61+
],
62+
stdout,
63+
)
64+
end
65+
2866
private
2967

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

0 commit comments

Comments
 (0)