MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestStreamableMcpHeaderVersionGating

Function TestStreamableMcpHeaderVersionGating

mcp/streamable_test.go:2122–2171  ·  view source on GitHub ↗

TestStreamableMcpHeaderVersionGating verifies that header validation is skipped for protocol versions older than minVersionForStandardHeaders.

(t *testing.T)

Source from the content-addressed store, hash-verified

2120// TestStreamableMcpHeaderVersionGating verifies that header validation
2121// is skipped for protocol versions older than minVersionForStandardHeaders.
2122func TestStreamableMcpHeaderVersionGating(t *testing.T) {
2123 server := NewServer(&Implementation{Name: "testServer", Version: "v1.0.0"}, nil)
2124 server.AddTool(
2125 &Tool{Name: "my-tool", InputSchema: &jsonschema.Schema{Type: "object"}},
2126 func(ctx context.Context, req *CallToolRequest) (*CallToolResult, error) {
2127 return &CallToolResult{}, nil
2128 })
2129
2130 handler := NewStreamableHTTPHandler(func(req *http.Request) *Server { return server }, nil)
2131 defer handler.closeAll()
2132
2133 initReq := req(1, methodInitialize, &InitializeParams{ProtocolVersion: protocolVersion20251125})
2134 initResp := resp(1, &InitializeResult{
2135 Capabilities: &ServerCapabilities{
2136 Logging: &LoggingCapabilities{},
2137 Tools: &ToolCapabilities{ListChanged: true},
2138 },
2139 ProtocolVersion: protocolVersion20251125,
2140 ServerInfo: &Implementation{Name: "testServer", Version: "v1.0.0"},
2141 }, nil)
2142
2143 testStreamableHandler(t, handler, []streamableRequest{
2144 {
2145 method: "POST",
2146 messages: []jsonrpc.Message{initReq},
2147 wantStatusCode: http.StatusOK,
2148 wantMessages: []jsonrpc.Message{initResp},
2149 wantSessionID: true,
2150 },
2151 {
2152 method: "POST",
2153 headers: http.Header{protocolVersionHeader: {protocolVersion20251125}},
2154 messages: []jsonrpc.Message{req(0, notificationInitialized, &InitializedParams{})},
2155 wantStatusCode: http.StatusAccepted,
2156 },
2157 // Requests with deliberately wrong MCP headers should still succeed
2158 // because the protocol version is too old for validation.
2159 {
2160 method: "POST",
2161 headers: http.Header{
2162 protocolVersionHeader: {protocolVersion20251125},
2163 methodHeader: {"wrong-method"},
2164 nameHeader: {"wrong-name"},
2165 },
2166 messages: []jsonrpc.Message{req(2, "tools/call", &CallToolParams{Name: "my-tool"})},
2167 wantStatusCode: http.StatusOK,
2168 wantMessages: []jsonrpc.Message{resp(2, &CallToolResult{Content: []Content{}}, nil)},
2169 },
2170 })
2171}
2172
2173// TestStreamable405AllowHeader verifies RFC 9110 §15.5.6 compliance:
2174// 405 Method Not Allowed responses MUST include an Allow header.

Callers

nothing calls this directly

Calls 7

AddToolMethod · 0.95
closeAllMethod · 0.95
NewServerFunction · 0.85
NewStreamableHTTPHandlerFunction · 0.85
reqFunction · 0.85
respFunction · 0.85
testStreamableHandlerFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…