Debian Codegraph @

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

raw symbols SCIP
1 // Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2 // Use of this source code is governed by the license
3 // that can be found in the LICENSE file.
4
5 package mcp
6
7 import (
8 "encoding/json"
9 "errors"
10 "fmt"
11 "net/http"
12
13 internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
14 "github.com/modelcontextprotocol/go-sdk/jsonrpc"
15 )
16
17 const (
18 protocolVersionHeader = "Mcp-Protocol-Version"
19 sessionIDHeader = "Mcp-Session-Id"
20 lastEventIDHeader = "Last-Event-ID"
21 methodHeader = "Mcp-Method"
22 nameHeader = "Mcp-Name"
23 minVersionForStandardHeaders = protocolVersion20260630
24 )
25
26 func extractName(method string, params json.RawMessage) (string, bool) {
27 switch method {
28 case "tools/call":
29 var p CallToolParams
30 if err := internaljson.Unmarshal(params, &p); err == nil {
31 return p.Name, true
32 }
33 case "prompts/get":
34 var p GetPromptParams
35 if err := internaljson.Unmarshal(params, &p); err == nil {
36 return p.Name, true
37 }
38 case "resources/read":
39 var p ReadResourceParams
40 if err := internaljson.Unmarshal(params, &p); err == nil {
41 return p.URI, true
42 }
43 }
44
45 return "", false
46 }
47
48 // setStandardHeaders populates standard MCP headers.
49 // It requires the protocol version header to be set.
50 func setStandardHeaders(header http.Header, msg jsonrpc.Message) {
51 if msg == nil {
52 return
53 }
54 if header.Get(protocolVersionHeader) == "" || header.Get(protocolVersionHeader) < minVersionForStandardHeaders {
55 return
56 }
57
58 switch msg := msg.(type) {
59 case *jsonrpc.Request:
60 header.Set(methodHeader, msg.Method)
61 if name, ok := extractName(msg.Method, msg.Params); ok {
62 header.Set(nameHeader, name)
63 }
64 }
65 }
66
67 func validateMcpHeaders(header http.Header, msg jsonrpc.Message) error {
68 protocolVersion := header.Get(protocolVersionHeader)
69 if protocolVersion == "" || protocolVersion < minVersionForStandardHeaders {
70 return nil
71 }
72
73 switch msg := msg.(type) {
74 case *jsonrpc.Request:
75 methodInHeader := header.Get(methodHeader)
76 if methodInHeader == "" {
77 return errors.New("missing required Mcp-Method header")
78 }
79 if methodInHeader != msg.Method {
80 return fmt.Errorf("header mismatch: Mcp-Method header value '%s' does not match body value '%s'", methodInHeader, msg.Method)
81 }
82
83 if msg.Method == "tools/call" || msg.Method == "resources/read" || msg.Method == "prompts/get" {
84 nameInHeader := header.Get(nameHeader)
85 if nameInHeader == "" {
86 return fmt.Errorf("missing required Mcp-Name header for method %q", msg.Method)
87 }
88 nameInBody, ok := extractName(msg.Method, msg.Params)
89 if !ok {
90 return fmt.Errorf("failed to extract name from parameters for method %q", msg.Method)
91 }
92 if nameInHeader != nameInBody {
93 return fmt.Errorf("header mismatch: Mcp-Name header value '%s' does not match body value '%s'", nameInHeader, nameInBody)
94 }
95 }
96 }
97 return nil
98 }