Skip to content

Commit ad22407

Browse files
authored
Merge pull request #76 from atesgoral/curl-json
Use the not-so-new --json arg of curl
2 parents 189d5fc + c8fdf25 commit ad22407

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

examples/README.md

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,50 @@ This directory contains examples of how to use the Model Context Protocol (MCP)
55
## Available Examples
66

77
### 1. STDIO Server (`stdio_server.rb`)
8+
89
A simple server that communicates over standard input/output. This is useful for desktop applications and command-line tools.
910

1011
**Usage:**
12+
1113
```console
1214
$ ruby examples/stdio_server.rb
1315
{"jsonrpc":"2.0","id":0,"method":"tools/list"}
1416
```
1517

1618
### 2. HTTP Server (`http_server.rb`)
19+
1720
A standalone HTTP server built with Rack that implements the MCP Streamable HTTP transport protocol. This demonstrates how to create a web-based MCP server with session management and Server-Sent Events (SSE) support.
1821

1922
**Features:**
23+
2024
- HTTP transport with Server-Sent Events (SSE) for streaming
2125
- Session management with unique session IDs
2226
- Example tools, prompts, and resources
2327
- JSON-RPC 2.0 protocol implementation
2428
- Full MCP protocol compliance
2529

2630
**Usage:**
31+
2732
```console
2833
$ ruby examples/http_server.rb
2934
```
3035

3136
The server will start on `http://localhost:9292` and provide:
37+
3238
- **Tools**:
3339
- `ExampleTool` - adds two numbers
3440
- `echo` - echoes back messages
3541
- **Prompts**: `ExamplePrompt` - echoes back arguments as a prompt
3642
- **Resources**: `test_resource` - returns example content
3743

3844
### 3. HTTP Client Example (`http_client.rb`)
45+
3946
A client that demonstrates how to interact with the HTTP server using all MCP protocol methods.
4047

4148
**Usage:**
49+
4250
1. Start the HTTP server in one terminal:
51+
4352
```console
4453
$ ruby examples/http_server.rb
4554
```
@@ -50,6 +59,7 @@ A client that demonstrates how to interact with the HTTP server using all MCP pr
5059
```
5160

5261
The client will demonstrate:
62+
5363
- Session initialization
5464
- Ping requests
5565
- Listing and calling tools
@@ -58,35 +68,43 @@ The client will demonstrate:
5868
- Session cleanup
5969

6070
### 4. Streamable HTTP Server (`streamable_http_server.rb`)
71+
6172
A specialized HTTP server designed to test and demonstrate Server-Sent Events (SSE) functionality in the MCP protocol.
6273

6374
**Features:**
75+
6476
- Tools specifically designed to trigger SSE notifications
6577
- Real-time progress updates and notifications
6678
- Detailed SSE-specific logging
6779

6880
**Available Tools:**
81+
6982
- `NotificationTool` - Send custom SSE notifications with optional delays
7083
- `echo` - Simple echo tool for basic testing
7184

7285
**Usage:**
86+
7387
```console
7488
$ ruby examples/streamable_http_server.rb
7589
```
7690

7791
The server will start on `http://localhost:9393` and provide detailed instructions for testing SSE functionality.
7892

7993
### 5. Streamable HTTP Client (`streamable_http_client.rb`)
94+
8095
An interactive client that connects to the SSE stream and provides a menu-driven interface for testing SSE functionality.
8196

8297
**Features:**
98+
8399
- Automatic SSE stream connection
84100
- Interactive menu for triggering various SSE events
85101
- Real-time display of received SSE notifications
86102
- Session management
87103

88104
**Usage:**
105+
89106
1. Start the SSE test server in one terminal:
107+
90108
```console
91109
$ ruby examples/streamable_http_server.rb
92110
```
@@ -97,6 +115,7 @@ An interactive client that connects to the SSE stream and provides a menu-driven
97115
```
98116

99117
The client will:
118+
100119
- Initialize a session automatically
101120
- Connect to the SSE stream
102121
- Provide an interactive menu to trigger notifications
@@ -107,24 +126,25 @@ The client will:
107126
You can also test SSE functionality manually using cURL:
108127

109128
1. Initialize a session:
129+
110130
```console
111-
SESSION_ID=$(curl -D - -s -o /dev/null -X POST http://localhost:9393 \
112-
-H "Content-Type: application/json" \
113-
-d '{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"curl-test","version":"1.0"}}}' | grep -i "Mcp-Session-Id:" | cut -d' ' -f2- | tr -d '\r')
131+
SESSION_ID=$(curl -D - -s -o /dev/null http://localhost:9393 \
132+
--json '{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"curl-test","version":"1.0"}}}' | grep -i "Mcp-Session-Id:" | cut -d' ' -f2- | tr -d '\r')
114133
```
115134

116135
2. Connect to SSE stream (in one terminal):
136+
117137
```console
118138
curl -i -N -H "Mcp-Session-Id: $SESSION_ID" http://localhost:9393
119139
```
120140

121141
3. Trigger notifications (in another terminal):
142+
122143
```console
123144
# Send immediate notification
124-
curl -i -X POST http://localhost:9393 \
125-
-H "Content-Type: application/json" \
145+
curl -i http://localhost:9393 \
126146
-H "Mcp-Session-Id: $SESSION_ID" \
127-
-d '{"jsonrpc":"2.0","method":"tools/call","id":2,"params":{"name":"notification_tool","arguments":{"message":"Hello from cURL!"}}}'
147+
--json '{"jsonrpc":"2.0","method":"tools/call","id":2,"params":{"name":"notification_tool","arguments":{"message":"Hello from cURL!"}}}'
128148
```
129149

130150
## Streamable HTTP Transport Details
@@ -134,14 +154,17 @@ curl -i -X POST http://localhost:9393 \
134154
The HTTP server implements the MCP Streamable HTTP transport protocol:
135155

136156
1. **Initialize Session**:
157+
137158
- Client sends POST request with `initialize` method
138159
- Server responds with session ID in `Mcp-Session-Id` header
139160

140161
2. **Establish SSE Connection** (optional):
162+
141163
- Client sends GET request with `Mcp-Session-Id` header
142164
- Server establishes Server-Sent Events stream for notifications
143165

144166
3. **Send Requests**:
167+
145168
- Client sends POST requests with JSON-RPC 2.0 format
146169
- Server processes and responds with results
147170

@@ -151,24 +174,24 @@ The HTTP server implements the MCP Streamable HTTP transport protocol:
151174
### Example cURL Commands
152175

153176
Initialize a session:
177+
154178
```console
155-
curl -i -X POST http://localhost:9292 \
156-
-H "Content-Type: application/json" \
157-
-d '{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}'
179+
curl -i http://localhost:9292 \
180+
--json '{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}'
158181
```
159182

160183
List tools (using the session ID from initialization):
184+
161185
```console
162-
curl -i -X POST http://localhost:9292 \
163-
-H "Content-Type: application/json" \
186+
curl -i http://localhost:9292 \
164187
-H "Mcp-Session-Id: YOUR_SESSION_ID" \
165-
-d '{"jsonrpc":"2.0","method":"tools/list","id":2}'
188+
--json '{"jsonrpc":"2.0","method":"tools/list","id":2}'
166189
```
167190

168191
Call a tool:
192+
169193
```console
170-
curl -i -X POST http://localhost:9292 \
171-
-H "Content-Type: application/json" \
194+
curl -i http://localhost:9292 \
172195
-H "Mcp-Session-Id: YOUR_SESSION_ID" \
173-
-d '{"jsonrpc":"2.0","method":"tools/call","id":3,"params":{"name":"ExampleTool","arguments":{"a":5,"b":3}}}'
196+
--json '{"jsonrpc":"2.0","method":"tools/call","id":3,"params":{"name":"ExampleTool","arguments":{"a":5,"b":3}}}'
174197
```

examples/http_server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def template(args, server_context:)
156156
puts "Starting MCP HTTP server on http://localhost:9292"
157157
puts "Use POST requests to initialize and send JSON-RPC commands"
158158
puts "Example initialization:"
159-
puts ' curl -i -X POST http://localhost:9292 -H "Content-Type: application/json" -d \'{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}\''
159+
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"}}}\''
160160
puts ""
161161
puts "The server will return a session ID in the Mcp-Session-Id header."
162162
puts "Use this session ID for subsequent requests."

examples/streamable_http_server.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,21 @@ def call(message:, delay: 0)
147147
puts "Testing SSE:"
148148
puts ""
149149
puts "1. Initialize session:"
150-
puts ' curl -i -X POST http://localhost:9393 -H "Content-Type: application/json" \\'
151-
puts ' -d \'{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"sse-test","version":"1.0"}}}\''
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"}}}\''
152152
puts ""
153153
puts "2. Connect SSE stream (use the session ID from step 1):"
154154
puts ' curl -i -N -H "Mcp-Session-Id: YOUR_SESSION_ID" http://localhost:9393'
155155
puts ""
156156
puts "3. In another terminal, test tools (responses will be sent via SSE if stream is active):"
157157
puts ""
158158
puts " Echo tool:"
159-
puts ' curl -i -X POST http://localhost:9393 -H "Content-Type: application/json" -H "Mcp-Session-Id: YOUR_SESSION_ID" \\'
160-
puts ' -d \'{"jsonrpc":"2.0","method":"tools/call","id":2,"params":{"name":"echo","arguments":{"message":"Hello SSE!"}}}\''
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!"}}}\''
161161
puts ""
162162
puts " Notification tool (with 2 second delay):"
163-
puts ' curl -i -X POST http://localhost:9393 -H "Content-Type: application/json" -H "Mcp-Session-Id: YOUR_SESSION_ID" \\'
164-
puts ' -d \'{"jsonrpc":"2.0","method":"tools/call","id":3,"params":{"name":"notification_tool","arguments":{"message":"Hello SSE!", "delay": 2}}}\''
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}}}\''
165165
puts ""
166166
puts "Note: When an SSE stream is active, tool responses will appear in the SSE stream and the POST request will return {\"accepted\": true}"
167167
puts ""

0 commit comments

Comments
 (0)