go-sdk/mcp/streamable_example_test.go
Robert Findley ac2f175214
docs: add feature and troubleshooting documentation (#463)
Add a framework for feature documentation, and start populating it with
our SDK documentation.

This framework is as follows:
- internal/docs/**.src.md is the markdown source for the docs/
  directory.
- The x/example/internal/cmd/weave tool is used to compile these docs to
  the top-level docs, supporting both linked code samples and generated
  tables of contents.
- The readme-check workflow is updated to check these docs as well.
- The structure of these docs follows the MCP spec.
- Wherever possible, example code is linked from actual Go documentation
  examples, so that it is testable.

Some minor modifications to the weave tool were made to support this
framework.

Additionally, partially fill out this documentation with content on
base protocol and client features, as well as troubleshooting help.

Along the way, a bug was encountered that our LoggingTransport was not
concurrency safe. This is fixed with a mutex.

Fixes https://github.com/modelcontextprotocol/go-sdk/issues/466
Fixes https://github.com/modelcontextprotocol/go-sdk/issues/409
Updates https://github.com/modelcontextprotocol/go-sdk/issues/442
2025-09-16 11:13:40 -04:00

86 lines
2.7 KiB
Go

// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package mcp_test
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"strings"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
// !+streamablehandler
func ExampleStreamableHTTPHandler() {
// Create a new streamable handler, using the same MCP server for every request.
//
// Here, we configure it to serves application/json responses rather than
// text/event-stream, just so the output below doesn't use random event ids.
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.1.0"}, nil)
handler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server {
return server
}, &mcp.StreamableHTTPOptions{JSONResponse: true})
httpServer := httptest.NewServer(handler)
defer httpServer.Close()
// The SDK is currently permissive of some missing keys in "params".
resp := mustPostMessage(`{"jsonrpc": "2.0", "id": 1, "method":"initialize", "params": {}}`, httpServer.URL)
fmt.Println(resp)
// Output:
// {"jsonrpc":"2.0","id":1,"result":{"capabilities":{"logging":{}},"protocolVersion":"2025-06-18","serverInfo":{"name":"server","version":"v0.1.0"}}}
}
// !-streamablehandler
// !+httpmiddleware
func ExampleStreamableHTTPHandler_middleware() {
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.1.0"}, nil)
handler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server {
return server
}, nil)
loggingHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// Example debugging; you could also capture the response.
body, err := io.ReadAll(req.Body)
if err != nil {
log.Fatal(err)
}
req.Body.Close() // ignore error
req.Body = io.NopCloser(bytes.NewBuffer(body))
fmt.Println(req.Method, string(body))
handler.ServeHTTP(w, req)
})
httpServer := httptest.NewServer(loggingHandler)
defer httpServer.Close()
// The SDK is currently permissive of some missing keys in "params".
mustPostMessage(`{"jsonrpc": "2.0", "id": 1, "method":"initialize", "params": {}}`, httpServer.URL)
// Output:
// POST {"jsonrpc": "2.0", "id": 1, "method":"initialize", "params": {}}
}
// !-httpmiddleware
func mustPostMessage(msg, url string) string {
req := orFatal(http.NewRequest("POST", url, strings.NewReader(msg)))
req.Header["Content-Type"] = []string{"application/json"}
req.Header["Accept"] = []string{"application/json", "text/event-stream"}
resp := orFatal(http.DefaultClient.Do(req))
defer resp.Body.Close()
body := orFatal(io.ReadAll(resp.Body))
return string(body)
}
func orFatal[T any](t T, err error) T {
if err != nil {
log.Fatal(err)
}
return t
}