Skip to content

Commit 3f2a4b3

Browse files
authored
Merge pull request #125 from koic/reduce_stdout_headge_in_example
Reduce stdout hedge in examples
2 parents dd5b318 + 03e35c7 commit 3f2a4b3

File tree

4 files changed

+66
-65
lines changed

4 files changed

+66
-65
lines changed

examples/http_client.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ def initialize_session
5050
},
5151
})
5252
puts "Response: #{JSON.pretty_generate(result)}"
53-
puts
53+
5454
result
5555
end
5656

5757
def ping
5858
puts "=== Sending ping ==="
5959
result = send_request("ping")
6060
puts "Response: #{JSON.pretty_generate(result)}"
61-
puts
61+
6262
result
6363
end
6464

6565
def list_tools
6666
puts "=== Listing tools ==="
6767
result = send_request("tools/list")
6868
puts "Response: #{JSON.pretty_generate(result)}"
69-
puts
69+
7070
result
7171
end
7272

@@ -77,15 +77,15 @@ def call_tool(name, arguments)
7777
arguments: arguments,
7878
})
7979
puts "Response: #{JSON.pretty_generate(result)}"
80-
puts
80+
8181
result
8282
end
8383

8484
def list_prompts
8585
puts "=== Listing prompts ==="
8686
result = send_request("prompts/list")
8787
puts "Response: #{JSON.pretty_generate(result)}"
88-
puts
88+
8989
result
9090
end
9191

@@ -96,15 +96,15 @@ def get_prompt(name, arguments)
9696
arguments: arguments,
9797
})
9898
puts "Response: #{JSON.pretty_generate(result)}"
99-
puts
99+
100100
result
101101
end
102102

103103
def list_resources
104104
puts "=== Listing resources ==="
105105
result = send_request("resources/list")
106106
puts "Response: #{JSON.pretty_generate(result)}"
107-
puts
107+
108108
result
109109
end
110110

@@ -114,7 +114,7 @@ def read_resource(uri)
114114
uri: uri,
115115
})
116116
puts "Response: #{JSON.pretty_generate(result)}"
117-
puts
117+
118118
result
119119
end
120120

@@ -131,7 +131,6 @@ def close_session
131131
response = http.request(request)
132132
result = JSON.parse(response.body)
133133
puts "Response: #{JSON.pretty_generate(result)}"
134-
puts
135134

136135
@session_id = nil
137136
result
@@ -140,10 +139,11 @@ def close_session
140139

141140
# Main script
142141
if __FILE__ == $PROGRAM_NAME
143-
puts "MCP HTTP Client Example"
144-
puts "Make sure the HTTP server is running (ruby examples/http_server.rb)"
145-
puts "=" * 50
146-
puts
142+
puts <<~MESSAGE
143+
MCP HTTP Client Example
144+
Make sure the HTTP server is running (ruby examples/http_server.rb)
145+
#{"=" * 50}
146+
MESSAGE
147147

148148
client = MCPHTTPClient.new
149149

examples/http_server.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,17 @@ def template(args, server_context:)
154154
end
155155

156156
# Start the server
157-
puts "Starting MCP HTTP server on http://localhost:9292"
158-
puts "Use POST requests to initialize and send JSON-RPC commands"
159-
puts "Example initialization:"
160-
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"}}}\''
161-
puts ""
162-
puts "The server will return a session ID in the Mcp-Session-Id header."
163-
puts "Use this session ID for subsequent requests."
164-
puts ""
165-
puts "Press Ctrl+C to stop the server"
157+
puts <<~MESSAGE
158+
Starting MCP HTTP server on http://localhost:9292
159+
Use POST requests to initialize and send JSON-RPC commands
160+
Example initialization:
161+
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"}}}'
162+
163+
The server will return a session ID in the Mcp-Session-Id header.
164+
Use this session ID for subsequent requests.
165+
166+
Press Ctrl+C to stop the server
167+
MESSAGE
166168

167169
# Run the server
168170
# Use Rackup to run the server

examples/streamable_http_client.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def main
9292
end
9393

9494
puts "=== MCP SSE Test Client ==="
95-
puts ""
9695

9796
# Step 1: Initialize session
9897
logger.info("Initializing session...")
@@ -134,13 +133,16 @@ def main
134133

135134
# Step 3: Interactive menu
136135
loop do
137-
puts "\n=== Available Actions ==="
138-
puts "1. Send custom notification"
139-
puts "2. Test echo"
140-
puts "3. List tools"
141-
puts "0. Exit"
142-
puts ""
143-
print("Choose an action: ")
136+
puts <<~MESSAGE.chomp
137+
138+
=== Available Actions ===
139+
1. Send custom notification
140+
2. Test echo
141+
3. List tools
142+
0. Exit
143+
144+
Choose an action:#{" "}
145+
MESSAGE
144146

145147
choice = gets.chomp
146148

@@ -167,19 +169,15 @@ def main
167169
else
168170
logger.error("Error: #{response[:body]["error"]}")
169171
end
170-
171172
when "2"
172173
print("Enter message to echo: ")
173174
message = gets.chomp
174175
make_request(session_id, "tools/call", { name: "echo", arguments: { message: message } })
175-
176176
when "3"
177177
make_request(session_id, "tools/list")
178-
179178
when "0"
180179
logger.info("Exiting...")
181180
break
182-
183181
else
184182
puts "Invalid choice"
185183
end

examples/streamable_http_server.rb

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -136,37 +136,38 @@ def call(message:, delay: 0)
136136
end
137137

138138
# Print usage instructions
139-
puts "=== MCP Streaming HTTP Test Server ==="
140-
puts ""
141-
puts "Starting server on http://localhost:9393"
142-
puts ""
143-
puts "Available Tools:"
144-
puts "1. NotificationTool - Returns messages that are sent via SSE when stream is active"
145-
puts "2. echo - Simple echo tool"
146-
puts ""
147-
puts "Testing SSE:"
148-
puts ""
149-
puts "1. Initialize session:"
150-
puts " curl -i http://localhost:9393 \\"
151-
puts ' --json \'{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"sse-test","version":"1.0"}}}\''
152-
puts ""
153-
puts "2. Connect SSE stream (use the session ID from step 1):"
154-
puts ' curl -i -N -H "Mcp-Session-Id: YOUR_SESSION_ID" http://localhost:9393'
155-
puts ""
156-
puts "3. In another terminal, test tools (responses will be sent via SSE if stream is active):"
157-
puts ""
158-
puts " Echo tool:"
159-
puts ' curl -i http://localhost:9393 -H "Mcp-Session-Id: YOUR_SESSION_ID" \\'
160-
puts ' --json \'{"jsonrpc":"2.0","method":"tools/call","id":2,"params":{"name":"echo","arguments":{"message":"Hello SSE!"}}}\''
161-
puts ""
162-
puts " Notification tool (with 2 second delay):"
163-
puts ' curl -i http://localhost:9393 -H "Mcp-Session-Id: YOUR_SESSION_ID" \\'
164-
puts ' --json \'{"jsonrpc":"2.0","method":"tools/call","id":3,"params":{"name":"notification_tool","arguments":{"message":"Hello SSE!", "delay": 2}}}\''
165-
puts ""
166-
puts "Note: When an SSE stream is active, tool responses will appear in the SSE stream and the POST request will return {\"accepted\": true}"
167-
puts ""
168-
puts "Press Ctrl+C to stop the server"
169-
puts ""
139+
puts <<~MESSAGE
140+
=== MCP Streaming HTTP Test Server ===
141+
142+
Starting server on http://localhost:9393
143+
144+
Available Tools:
145+
1. NotificationTool - Returns messages that are sent via SSE when stream is active"
146+
2. echo - Simple echo tool
147+
148+
Testing SSE:
149+
150+
1. Initialize session:
151+
curl -i http://localhost:9393 \\
152+
--json '{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"sse-test","version":"1.0"}}}'
153+
154+
2. Connect SSE stream (use the session ID from step 1):"
155+
curl -i -N -H "Mcp-Session-Id: YOUR_SESSION_ID" http://localhost:9393
156+
157+
3. In another terminal, test tools (responses will be sent via SSE if stream is active):
158+
159+
Echo tool:
160+
curl -i http://localhost:9393 -H "Mcp-Session-Id: YOUR_SESSION_ID" \\
161+
--json '{"jsonrpc":"2.0","method":"tools/call","id":2,"params":{"name":"echo","arguments":{"message":"Hello SSE!"}}}'
162+
163+
Notification tool (with 2 second delay):
164+
curl -i http://localhost:9393 -H "Mcp-Session-Id: YOUR_SESSION_ID" \\
165+
--json '{"jsonrpc":"2.0","method":"tools/call","id":3,"params":{"name":"notification_tool","arguments":{"message":"Hello SSE!", "delay": 2}}}'
166+
167+
Note: When an SSE stream is active, tool responses will appear in the SSE stream and the POST request will return {"accepted": true}
168+
169+
Press Ctrl+C to stop the server
170+
MESSAGE
170171

171172
# Start the server
172173
Rackup::Handler.get("puma").run(rack_app, Port: 9393, Host: "localhost")

0 commit comments

Comments
 (0)