Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions examples/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ def initialize_session
},
})
puts "Response: #{JSON.pretty_generate(result)}"
puts

result
end

def ping
puts "=== Sending ping ==="
result = send_request("ping")
puts "Response: #{JSON.pretty_generate(result)}"
puts

result
end

def list_tools
puts "=== Listing tools ==="
result = send_request("tools/list")
puts "Response: #{JSON.pretty_generate(result)}"
puts

result
end

Expand All @@ -77,15 +77,15 @@ def call_tool(name, arguments)
arguments: arguments,
})
puts "Response: #{JSON.pretty_generate(result)}"
puts

result
end

def list_prompts
puts "=== Listing prompts ==="
result = send_request("prompts/list")
puts "Response: #{JSON.pretty_generate(result)}"
puts

result
end

Expand All @@ -96,15 +96,15 @@ def get_prompt(name, arguments)
arguments: arguments,
})
puts "Response: #{JSON.pretty_generate(result)}"
puts

result
end

def list_resources
puts "=== Listing resources ==="
result = send_request("resources/list")
puts "Response: #{JSON.pretty_generate(result)}"
puts

result
end

Expand All @@ -114,7 +114,7 @@ def read_resource(uri)
uri: uri,
})
puts "Response: #{JSON.pretty_generate(result)}"
puts

result
end

Expand All @@ -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
Expand All @@ -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

Expand Down
20 changes: 11 additions & 9 deletions examples/http_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 10 additions & 12 deletions examples/streamable_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def main
end

puts "=== MCP SSE Test Client ==="
puts ""

# Step 1: Initialize session
logger.info("Initializing session...")
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
63 changes: 32 additions & 31 deletions examples/streamable_http_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")