diff --git a/examples/http_client.rb b/examples/http_client.rb index 31baf3db..3efbff9b 100644 --- a/examples/http_client.rb +++ b/examples/http_client.rb @@ -50,7 +50,7 @@ def initialize_session }, }) puts "Response: #{JSON.pretty_generate(result)}" - puts + result end @@ -58,7 +58,7 @@ def ping puts "=== Sending ping ===" result = send_request("ping") puts "Response: #{JSON.pretty_generate(result)}" - puts + result end @@ -66,7 +66,7 @@ def list_tools puts "=== Listing tools ===" result = send_request("tools/list") puts "Response: #{JSON.pretty_generate(result)}" - puts + result end @@ -77,7 +77,7 @@ def call_tool(name, arguments) arguments: arguments, }) puts "Response: #{JSON.pretty_generate(result)}" - puts + result end @@ -85,7 +85,7 @@ def list_prompts puts "=== Listing prompts ===" result = send_request("prompts/list") puts "Response: #{JSON.pretty_generate(result)}" - puts + result end @@ -96,7 +96,7 @@ def get_prompt(name, arguments) arguments: arguments, }) puts "Response: #{JSON.pretty_generate(result)}" - puts + result end @@ -104,7 +104,7 @@ def list_resources puts "=== Listing resources ===" result = send_request("resources/list") puts "Response: #{JSON.pretty_generate(result)}" - puts + result end @@ -114,7 +114,7 @@ def read_resource(uri) uri: uri, }) puts "Response: #{JSON.pretty_generate(result)}" - puts + result end @@ -131,7 +131,6 @@ def close_session response = http.request(request) result = JSON.parse(response.body) puts "Response: #{JSON.pretty_generate(result)}" - puts @session_id = nil result @@ -140,10 +139,11 @@ def close_session # 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 diff --git a/examples/http_server.rb b/examples/http_server.rb index c94bb72f..32933483 100644 --- a/examples/http_server.rb +++ b/examples/http_server.rb @@ -154,15 +154,17 @@ def template(args, server_context:) 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 diff --git a/examples/streamable_http_client.rb b/examples/streamable_http_client.rb index a965594d..b3a7477c 100644 --- a/examples/streamable_http_client.rb +++ b/examples/streamable_http_client.rb @@ -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 diff --git a/examples/streamable_http_server.rb b/examples/streamable_http_server.rb index b61fe066..3f10bc30 100644 --- a/examples/streamable_http_server.rb +++ b/examples/streamable_http_server.rb @@ -136,37 +136,38 @@ def call(message:, delay: 0) 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")