Reduce stdout hedge in examples

This PR reduces redundant stdout in examples by replacing
multiple `puts` with a heredoc and by removing unnecessary `puts`.
This commit is contained in:
Koichi ITO 2025-09-02 01:37:27 +09:00
commit 03e35c73c3
4 changed files with 66 additions and 65 deletions

View file

@ -50,7 +50,7 @@ class MCPHTTPClient
}, },
}) })
puts "Response: #{JSON.pretty_generate(result)}" puts "Response: #{JSON.pretty_generate(result)}"
puts
result result
end end
@ -58,7 +58,7 @@ class MCPHTTPClient
puts "=== Sending ping ===" puts "=== Sending ping ==="
result = send_request("ping") result = send_request("ping")
puts "Response: #{JSON.pretty_generate(result)}" puts "Response: #{JSON.pretty_generate(result)}"
puts
result result
end end
@ -66,7 +66,7 @@ class MCPHTTPClient
puts "=== Listing tools ===" puts "=== Listing tools ==="
result = send_request("tools/list") result = send_request("tools/list")
puts "Response: #{JSON.pretty_generate(result)}" puts "Response: #{JSON.pretty_generate(result)}"
puts
result result
end end
@ -77,7 +77,7 @@ class MCPHTTPClient
arguments: arguments, arguments: arguments,
}) })
puts "Response: #{JSON.pretty_generate(result)}" puts "Response: #{JSON.pretty_generate(result)}"
puts
result result
end end
@ -85,7 +85,7 @@ class MCPHTTPClient
puts "=== Listing prompts ===" puts "=== Listing prompts ==="
result = send_request("prompts/list") result = send_request("prompts/list")
puts "Response: #{JSON.pretty_generate(result)}" puts "Response: #{JSON.pretty_generate(result)}"
puts
result result
end end
@ -96,7 +96,7 @@ class MCPHTTPClient
arguments: arguments, arguments: arguments,
}) })
puts "Response: #{JSON.pretty_generate(result)}" puts "Response: #{JSON.pretty_generate(result)}"
puts
result result
end end
@ -104,7 +104,7 @@ class MCPHTTPClient
puts "=== Listing resources ===" puts "=== Listing resources ==="
result = send_request("resources/list") result = send_request("resources/list")
puts "Response: #{JSON.pretty_generate(result)}" puts "Response: #{JSON.pretty_generate(result)}"
puts
result result
end end
@ -114,7 +114,7 @@ class MCPHTTPClient
uri: uri, uri: uri,
}) })
puts "Response: #{JSON.pretty_generate(result)}" puts "Response: #{JSON.pretty_generate(result)}"
puts
result result
end end
@ -131,7 +131,6 @@ class MCPHTTPClient
response = http.request(request) response = http.request(request)
result = JSON.parse(response.body) result = JSON.parse(response.body)
puts "Response: #{JSON.pretty_generate(result)}" puts "Response: #{JSON.pretty_generate(result)}"
puts
@session_id = nil @session_id = nil
result result
@ -140,10 +139,11 @@ end
# Main script # Main script
if __FILE__ == $PROGRAM_NAME if __FILE__ == $PROGRAM_NAME
puts "MCP HTTP Client Example" puts <<~MESSAGE
puts "Make sure the HTTP server is running (ruby examples/http_server.rb)" MCP HTTP Client Example
puts "=" * 50 Make sure the HTTP server is running (ruby examples/http_server.rb)
puts #{"=" * 50}
MESSAGE
client = MCPHTTPClient.new client = MCPHTTPClient.new

View file

@ -154,15 +154,17 @@ rack_app = Rack::Builder.new do
end end
# Start the server # Start the server
puts "Starting MCP HTTP server on http://localhost:9292" puts <<~MESSAGE
puts "Use POST requests to initialize and send JSON-RPC commands" Starting MCP HTTP server on http://localhost:9292
puts "Example initialization:" Use POST requests to initialize and send JSON-RPC commands
puts ' curl -i http://localhost:9292 --json \'{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}\'' Example initialization:
puts "" curl -i http://localhost:9292 --json '{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}'
puts "The server will return a session ID in the Mcp-Session-Id header."
puts "Use this session ID for subsequent requests." The server will return a session ID in the Mcp-Session-Id header.
puts "" Use this session ID for subsequent requests.
puts "Press Ctrl+C to stop the server"
Press Ctrl+C to stop the server
MESSAGE
# Run the server # Run the server
# Use Rackup to run the server # Use Rackup to run the server

View file

@ -92,7 +92,6 @@ def main
end end
puts "=== MCP SSE Test Client ===" puts "=== MCP SSE Test Client ==="
puts ""
# Step 1: Initialize session # Step 1: Initialize session
logger.info("Initializing session...") logger.info("Initializing session...")
@ -134,13 +133,16 @@ def main
# Step 3: Interactive menu # Step 3: Interactive menu
loop do loop do
puts "\n=== Available Actions ===" puts <<~MESSAGE.chomp
puts "1. Send custom notification"
puts "2. Test echo" === Available Actions ===
puts "3. List tools" 1. Send custom notification
puts "0. Exit" 2. Test echo
puts "" 3. List tools
print("Choose an action: ") 0. Exit
Choose an action:#{" "}
MESSAGE
choice = gets.chomp choice = gets.chomp
@ -167,19 +169,15 @@ def main
else else
logger.error("Error: #{response[:body]["error"]}") logger.error("Error: #{response[:body]["error"]}")
end end
when "2" when "2"
print("Enter message to echo: ") print("Enter message to echo: ")
message = gets.chomp message = gets.chomp
make_request(session_id, "tools/call", { name: "echo", arguments: { message: message } }) make_request(session_id, "tools/call", { name: "echo", arguments: { message: message } })
when "3" when "3"
make_request(session_id, "tools/list") make_request(session_id, "tools/list")
when "0" when "0"
logger.info("Exiting...") logger.info("Exiting...")
break break
else else
puts "Invalid choice" puts "Invalid choice"
end end

View file

@ -136,37 +136,38 @@ rack_app = Rack::Builder.new do
end end
# Print usage instructions # Print usage instructions
puts "=== MCP Streaming HTTP Test Server ===" puts <<~MESSAGE
puts "" === MCP Streaming HTTP Test Server ===
puts "Starting server on http://localhost:9393"
puts "" Starting server on http://localhost:9393
puts "Available Tools:"
puts "1. NotificationTool - Returns messages that are sent via SSE when stream is active" Available Tools:
puts "2. echo - Simple echo tool" 1. NotificationTool - Returns messages that are sent via SSE when stream is active"
puts "" 2. echo - Simple echo tool
puts "Testing SSE:"
puts "" Testing SSE:
puts "1. Initialize session:"
puts " curl -i http://localhost:9393 \\" 1. Initialize session:
puts ' --json \'{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"sse-test","version":"1.0"}}}\'' curl -i http://localhost:9393 \\
puts "" --json '{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"sse-test","version":"1.0"}}}'
puts "2. Connect SSE stream (use the session ID from step 1):"
puts ' curl -i -N -H "Mcp-Session-Id: YOUR_SESSION_ID" http://localhost:9393' 2. Connect SSE stream (use the session ID from step 1):"
puts "" curl -i -N -H "Mcp-Session-Id: YOUR_SESSION_ID" http://localhost:9393
puts "3. In another terminal, test tools (responses will be sent via SSE if stream is active):"
puts "" 3. In another terminal, test tools (responses will be sent via SSE if stream is active):
puts " Echo tool:"
puts ' curl -i http://localhost:9393 -H "Mcp-Session-Id: YOUR_SESSION_ID" \\' Echo tool:
puts ' --json \'{"jsonrpc":"2.0","method":"tools/call","id":2,"params":{"name":"echo","arguments":{"message":"Hello SSE!"}}}\'' curl -i http://localhost:9393 -H "Mcp-Session-Id: YOUR_SESSION_ID" \\
puts "" --json '{"jsonrpc":"2.0","method":"tools/call","id":2,"params":{"name":"echo","arguments":{"message":"Hello SSE!"}}}'
puts " Notification tool (with 2 second delay):"
puts ' curl -i http://localhost:9393 -H "Mcp-Session-Id: YOUR_SESSION_ID" \\' Notification tool (with 2 second delay):
puts ' --json \'{"jsonrpc":"2.0","method":"tools/call","id":3,"params":{"name":"notification_tool","arguments":{"message":"Hello SSE!", "delay": 2}}}\'' curl -i http://localhost:9393 -H "Mcp-Session-Id: YOUR_SESSION_ID" \\
puts "" --json '{"jsonrpc":"2.0","method":"tools/call","id":3,"params":{"name":"notification_tool","arguments":{"message":"Hello SSE!", "delay": 2}}}'
puts "Note: When an SSE stream is active, tool responses will appear in the SSE stream and the POST request will return {\"accepted\": true}"
puts "" Note: When an SSE stream is active, tool responses will appear in the SSE stream and the POST request will return {"accepted": true}
puts "Press Ctrl+C to stop the server"
puts "" Press Ctrl+C to stop the server
MESSAGE
# Start the server # Start the server
Rackup::Handler.get("puma").run(rack_app, Port: 9393, Host: "localhost") Rackup::Handler.get("puma").run(rack_app, Port: 9393, Host: "localhost")