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

View file

@ -154,15 +154,17 @@ rack_app = Rack::Builder.new do
end
# Start the server
puts "Starting MCP HTTP server on http://localhost:9292"
puts "Use POST requests to initialize and send JSON-RPC commands"
puts "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 ""
puts "The server will return a session ID in the Mcp-Session-Id header."
puts "Use this session ID for subsequent requests."
puts ""
puts "Press Ctrl+C to stop the server"
puts <<~MESSAGE
Starting MCP HTTP server on http://localhost:9292
Use POST requests to initialize and send JSON-RPC commands
Example initialization:
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"}}}'
The server will return a session ID in the Mcp-Session-Id header.
Use this session ID for subsequent requests.
Press Ctrl+C to stop the server
MESSAGE
# Run the server
# Use Rackup to run the server

View file

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

View file

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