Skip to content

Commit 04174e3

Browse files
Copilotf
andcommitted
Add documentation and tests for streamable HTTP transport
Co-authored-by: f <[email protected]>
1 parent 53d2e76 commit 04174e3

File tree

3 files changed

+131
-4
lines changed

3 files changed

+131
-4
lines changed

README.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,39 @@ mcp tools npx -y @modelcontextprotocol/server-filesystem ~
148148

149149
#### HTTP SSE Transport
150150

151-
Uses HTTP and Server-Sent Events (SSE) to communicate with an MCP server via JSON-RPC 2.0. This is useful for connecting to remote servers that implement the MCP protocol.
151+
Uses HTTP and Server-Sent Events (SSE) to communicate with an MCP server via JSON-RPC 2.0. This is useful for connecting to remote servers that implement the legacy MCP protocol.
152152

153153
```bash
154-
mcp tools http://localhost:3001/sse
154+
mcp tools --transport=sse http://localhost:3001/sse
155155

156156
# Example: Use the everything sample server
157157
# docker run -p 3001:3001 --rm -it tzolov/mcp-everything-server:v1
158158
```
159159

160160
_Note:_ HTTP SSE currently supports only MCP protocol version 2024-11-05.
161161

162+
#### Streamable HTTP Transport (Recommended)
163+
164+
Uses streamable HTTP to communicate with an MCP server via JSON-RPC 2.0. This is the modern, recommended approach for connecting to remote servers that implement the MCP protocol. It supports both streaming responses and simple request/response patterns.
165+
166+
```bash
167+
# Default transport for HTTP URLs (explicit flag not needed)
168+
mcp tools http://localhost:3000
169+
170+
# Explicitly specify streamable HTTP transport
171+
mcp tools --transport=http http://localhost:3000
172+
173+
# Examples with remote servers
174+
mcp tools https://api.example.com/mcp
175+
mcp tools --transport=http https://ne.tools
176+
```
177+
178+
_Benefits of Streamable HTTP:_
179+
- **Session Management**: Supports stateful connections with session IDs
180+
- **Resumability**: Can reconnect and resume interrupted sessions (when supported by server)
181+
- **Flexible Responses**: Supports both streaming and direct JSON responses
182+
- **Modern Protocol**: Uses the latest MCP transport specification
183+
162184
### Output Formats
163185

164186
MCP Tools supports three output formats to accommodate different needs:
@@ -373,11 +395,12 @@ mcp new tool:calculate --sdk=ts
373395
# Create a project with a specific transport type
374396
mcp new tool:calculate --transport=stdio
375397
mcp new tool:calculate --transport=sse
398+
mcp new tool:calculate --transport=http
376399
```
377400

378401
The scaffolding creates a complete project structure with:
379402

380-
- Server setup with chosen transport (stdio or SSE)
403+
- Server setup with chosen transport (stdio, SSE, or streamable HTTP)
381404
- TypeScript configuration with modern ES modules
382405
- Component implementations with proper MCP interfaces
383406
- Automatic wiring of imports and initialization
@@ -749,6 +772,48 @@ mcp guard --allow tools:search_files npx -y @modelcontextprotocol/server-filesys
749772
mcp guard --deny tools:write_*,delete_*,create_*,move_* npx -y @modelcontextprotocol/server-filesystem ~
750773
```
751774

775+
### Streamable HTTP Usage
776+
777+
Create and run a local streamable HTTP server:
778+
779+
```bash
780+
# Create a new MCP server with streamable HTTP transport
781+
mkdir my-http-server && cd my-http-server
782+
mcp new tool:example_tool --transport=http
783+
784+
# Install dependencies and build
785+
npm install && npm run build
786+
787+
# Start the server (will run on http://localhost:3000)
788+
npm start
789+
```
790+
791+
In a separate terminal, connect to your local server:
792+
793+
```bash
794+
# Connect to local streamable HTTP server
795+
mcp tools http://localhost:3000
796+
797+
# Call a tool on the local server
798+
mcp call example_tool --params '{"input": "test"}' http://localhost:3000
799+
800+
# Use with different output formats
801+
mcp tools --format pretty http://localhost:3000
802+
```
803+
804+
Connect to remote streamable HTTP servers:
805+
806+
```bash
807+
# Connect to a remote MCP server
808+
mcp tools https://api.example.com/mcp
809+
810+
# Use SSE transport for legacy servers
811+
mcp tools --transport=sse http://legacy-server.com/sse
812+
813+
# Example with authentication headers (when supported)
814+
mcp tools --transport=http https://authenticated-mcp-server.com
815+
```
816+
752817
### Script Integration
753818

754819
Using the proxy mode with a simple shell script:

cmd/mcptools/transport_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/f/mcptools/cmd/mcptools/commands"
7+
)
8+
9+
func TestTransportFlag(t *testing.T) {
10+
// Save original values
11+
origTransport := commands.TransportOption
12+
13+
// Test default value
14+
if commands.TransportOption != "http" {
15+
t.Errorf("Expected default transport to be 'http', got '%s'", commands.TransportOption)
16+
}
17+
18+
// Test ProcessFlags with transport flag
19+
args := []string{"tools", "--transport", "sse", "http://localhost:3000"}
20+
remainingArgs := commands.ProcessFlags(args)
21+
22+
expectedArgs := []string{"tools", "http://localhost:3000"}
23+
if len(remainingArgs) != len(expectedArgs) {
24+
t.Errorf("Expected %d args, got %d", len(expectedArgs), len(remainingArgs))
25+
}
26+
27+
for i, arg := range expectedArgs {
28+
if remainingArgs[i] != arg {
29+
t.Errorf("Expected arg %d to be '%s', got '%s'", i, arg, remainingArgs[i])
30+
}
31+
}
32+
33+
if commands.TransportOption != "sse" {
34+
t.Errorf("Expected transport to be 'sse', got '%s'", commands.TransportOption)
35+
}
36+
37+
// Restore original values
38+
commands.TransportOption = origTransport
39+
}
40+
41+
func TestIsHTTP(t *testing.T) {
42+
testCases := []struct {
43+
url string
44+
expected bool
45+
}{
46+
{"http://localhost:3000", true},
47+
{"https://example.com", true},
48+
{"localhost:3000", true},
49+
{"stdio", false},
50+
{"", false},
51+
{"file:///path", false},
52+
}
53+
54+
for _, tc := range testCases {
55+
result := commands.IsHTTP(tc.url)
56+
if result != tc.expected {
57+
t.Errorf("IsHTTP(%s) = %v, expected %v", tc.url, result, tc.expected)
58+
}
59+
}
60+
}

templates/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ mcp new tool:hello_world resource:file prompt:hello
2727
# Create a project with a specific SDK (currently only TypeScript/ts supported)
2828
mcp new tool:hello_world --sdk=ts
2929

30-
# Create a project with a specific transport (stdio or sse)
30+
# Create a project with a specific transport (stdio, sse, or http)
3131
mcp new tool:hello_world --transport=stdio
3232
mcp new tool:hello_world --transport=sse
33+
mcp new tool:hello_world --transport=http
3334
```
3435

3536
## Available Templates
@@ -41,6 +42,7 @@ mcp new tool:hello_world --transport=sse
4142
- **prompt**: Prompt implementation template
4243
- **server_stdio**: Server with stdio transport
4344
- **server_sse**: Server with SSE transport
45+
- **server_http**: Server with streamable HTTP transport
4446
- **full_server**: Complete server with all three capabilities
4547

4648
## Project Structure

0 commit comments

Comments
 (0)