Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/mcp/streamable_example_test.go

raw symbols SCIP
1 // Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package mcp_test
6
7 import (
8 "bytes"
9 "fmt"
10 "io"
11 "log"
12 "net/http"
13 "net/http/httptest"
14 "strings"
15
16 "github.com/modelcontextprotocol/go-sdk/mcp"
17 )
18
19 // !+streamablehandler
20
21 // TODO: Until we have a way to clean up abandoned sessions, this test will leak goroutines (see #499)
22 func ExampleStreamableHTTPHandler() {
23 // Create a new streamable handler, using the same MCP server for every request.
24 //
25 // Here, we configure it to serves application/json responses rather than
26 // text/event-stream, just so the output below doesn't use random event ids.
27 server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.1.0"}, nil)
28 handler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server {
29 return server
30 }, &mcp.StreamableHTTPOptions{JSONResponse: true})
31 httpServer := httptest.NewServer(handler)
32 defer httpServer.Close()
33
34 // The SDK is currently permissive of some missing keys in "params".
35 resp := mustPostMessage(`{"jsonrpc": "2.0", "id": 1, "method":"initialize", "params": {}}`, httpServer.URL)
36 fmt.Println(resp)
37 // Output:
38 // {"jsonrpc":"2.0","id":1,"result":{"capabilities":{"logging":{}},"protocolVersion":"2025-11-25","serverInfo":{"name":"server","version":"v0.1.0"}}}
39 }
40
41 // !-streamablehandler
42
43 // !+httpmiddleware
44
45 func ExampleStreamableHTTPHandler_middleware() {
46 server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.1.0"}, nil)
47 handler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server {
48 return server
49 }, &mcp.StreamableHTTPOptions{Stateless: true})
50 loggingHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
51 // Example debugging; you could also capture the response.
52 body, err := io.ReadAll(req.Body)
53 if err != nil {
54 log.Fatal(err)
55 }
56 req.Body.Close() // ignore error
57 req.Body = io.NopCloser(bytes.NewBuffer(body))
58 fmt.Println(req.Method, string(body))
59 handler.ServeHTTP(w, req)
60 })
61 httpServer := httptest.NewServer(loggingHandler)
62 defer httpServer.Close()
63
64 // The SDK is currently permissive of some missing keys in "params".
65 mustPostMessage(`{"jsonrpc": "2.0", "id": 1, "method":"initialize", "params": {}}`, httpServer.URL)
66 // Output:
67 // POST {"jsonrpc": "2.0", "id": 1, "method":"initialize", "params": {}}
68 }
69
70 // !-httpmiddleware
71
72 func mustPostMessage(msg, url string) string {
73 req := orFatal(http.NewRequest("POST", url, strings.NewReader(msg)))
74 req.Header["Content-Type"] = []string{"application/json"}
75 req.Header["Accept"] = []string{"application/json", "text/event-stream"}
76 resp := orFatal(http.DefaultClient.Do(req))
77 defer resp.Body.Close()
78 body := orFatal(io.ReadAll(resp.Body))
79 return string(body)
80 }
81
82 func orFatal[T any](t T, err error) T {
83 if err != nil {
84 log.Fatal(err)
85 }
86 return t
87 }