Skip to content

Commit b786693

Browse files
committed
update demo
1 parent 60fa906 commit b786693

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

examples/custom_context/main.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,9 @@ func NewMCPServer() *MCPServer {
122122
}
123123
}
124124

125-
func (s *MCPServer) ServeSSE(addr string) *server.SSEServer {
126-
return server.NewSSEServer(s.server,
127-
server.WithBaseURL(fmt.Sprintf("http://%s", addr)),
128-
server.WithSSEContextFunc(authFromRequest),
125+
func (s *MCPServer) ServeHTTP() *server.StreamableHTTPServer {
126+
return server.NewStreamableHTTPServer(s.server,
127+
server.WithHTTPContextFunc(authFromRequest),
129128
)
130129
}
131130

@@ -135,12 +134,12 @@ func (s *MCPServer) ServeStdio() error {
135134

136135
func main() {
137136
var transport string
138-
flag.StringVar(&transport, "t", "stdio", "Transport type (stdio or sse)")
137+
flag.StringVar(&transport, "t", "stdio", "Transport type (stdio or http)")
139138
flag.StringVar(
140139
&transport,
141140
"transport",
142141
"stdio",
143-
"Transport type (stdio or sse)",
142+
"Transport type (stdio or http)",
144143
)
145144
flag.Parse()
146145

@@ -151,15 +150,15 @@ func main() {
151150
if err := s.ServeStdio(); err != nil {
152151
log.Fatalf("Server error: %v", err)
153152
}
154-
case "sse":
155-
sseServer := s.ServeSSE("localhost:8080")
156-
log.Printf("SSE server listening on :8080")
157-
if err := sseServer.Start(":8080"); err != nil {
153+
case "http":
154+
httpServer := s.ServeHTTP()
155+
log.Printf("HTTP server listening on :8080")
156+
if err := httpServer.Start(":8080"); err != nil {
158157
log.Fatalf("Server error: %v", err)
159158
}
160159
default:
161160
log.Fatalf(
162-
"Invalid transport type: %s. Must be 'stdio' or 'sse'",
161+
"Invalid transport type: %s. Must be 'stdio' or 'http'",
163162
transport,
164163
)
165164
}

examples/everything/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -472,17 +472,17 @@ func handleNotification(
472472

473473
func main() {
474474
var transport string
475-
flag.StringVar(&transport, "t", "stdio", "Transport type (stdio or sse)")
476-
flag.StringVar(&transport, "transport", "stdio", "Transport type (stdio or sse)")
475+
flag.StringVar(&transport, "t", "stdio", "Transport type (stdio or http)")
476+
flag.StringVar(&transport, "transport", "stdio", "Transport type (stdio or http)")
477477
flag.Parse()
478478

479479
mcpServer := NewMCPServer()
480480

481-
// Only check for "sse" since stdio is the default
482-
if transport == "sse" {
483-
sseServer := server.NewSSEServer(mcpServer, server.WithBaseURL("http://localhost:8080"))
484-
log.Printf("SSE server listening on :8080")
485-
if err := sseServer.Start(":8080"); err != nil {
481+
// Only check for "http" since stdio is the default
482+
if transport == "http" {
483+
httpServer := server.NewStreamableHTTPServer(mcpServer)
484+
log.Printf("HTTP server listening on :8080/mcp")
485+
if err := httpServer.Start(":8080"); err != nil {
486486
log.Fatalf("Server error: %v", err)
487487
}
488488
} else {

examples/simple_client/main.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import (
1717
func main() {
1818
// Define command line flags
1919
stdioCmd := flag.String("stdio", "", "Command to execute for stdio transport (e.g. 'python server.py')")
20-
sseURL := flag.String("sse", "", "URL for SSE transport (e.g. 'http://localhost:8080/sse')")
20+
httpURL := flag.String("http", "", "URL for HTTP transport (e.g. 'http://localhost:8080/mcp')")
2121
flag.Parse()
2222

2323
// Validate flags
24-
if (*stdioCmd == "" && *sseURL == "") || (*stdioCmd != "" && *sseURL != "") {
25-
fmt.Println("Error: You must specify exactly one of --stdio or --sse")
24+
if (*stdioCmd == "" && *httpURL == "") || (*stdioCmd != "" && *httpURL != "") {
25+
fmt.Println("Error: You must specify exactly one of --stdio or --http")
2626
flag.Usage()
2727
os.Exit(1)
2828
}
@@ -51,11 +51,6 @@ func main() {
5151
// Create stdio transport with verbose logging
5252
stdioTransport := transport.NewStdio(command, nil, cmdArgs...)
5353

54-
// Start the transport
55-
if err := stdioTransport.Start(ctx); err != nil {
56-
log.Fatalf("Failed to start stdio transport: %v", err)
57-
}
58-
5954
// Create client with the transport
6055
c = client.NewClient(stdioTransport)
6156

@@ -78,20 +73,20 @@ func main() {
7873
}()
7974
}
8075
} else {
81-
fmt.Println("Initializing SSE client...")
82-
// Create SSE transport
83-
sseTransport, err := transport.NewSSE(*sseURL)
76+
fmt.Println("Initializing HTTP client...")
77+
// Create HTTP transport
78+
httpTransport, err := transport.NewStreamableHTTP(*httpURL)
8479
if err != nil {
85-
log.Fatalf("Failed to create SSE transport: %v", err)
86-
}
87-
88-
// Start the transport
89-
if err := sseTransport.Start(ctx); err != nil {
90-
log.Fatalf("Failed to start SSE transport: %v", err)
80+
log.Fatalf("Failed to create HTTP transport: %v", err)
9181
}
9282

9383
// Create client with the transport
94-
c = client.NewClient(sseTransport)
84+
c = client.NewClient(httpTransport)
85+
}
86+
87+
// Start the client
88+
if err := c.Start(ctx); err != nil {
89+
log.Fatalf("Failed to start client: %v", err)
9590
}
9691

9792
// Set up notification handler

0 commit comments

Comments
 (0)