Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions mcp/mcp_http_headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by the license
// that can be found in the LICENSE file.

package mcp

import (
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/modelcontextprotocol/go-sdk/jsonrpc"
)

const (
ProtocolVersionHeader = "Mcp-Protocol-Version"
Comment thread
guglielmo-san marked this conversation as resolved.
Outdated
SessionIDHeader = "Mcp-Session-Id"
LastEventIDHeader = "Last-Event-ID"
MethodHeader = "Mcp-Method"
NameHeader = "Mcp-Name"
MinVersionForStandardHeaders = "2026-06-XX"
Comment thread
guglielmo-san marked this conversation as resolved.
Outdated
)

func extractName(method string, params json.RawMessage) (string, bool) {
switch method {
case "tools/call":
var p CallToolParams
if err := json.Unmarshal(params, &p); err == nil {
Comment thread
guglielmo-san marked this conversation as resolved.
Outdated
return p.Name, true
}
case "prompts/get":
var p GetPromptParams
if err := json.Unmarshal(params, &p); err == nil {
return p.Name, true
}
case "resources/read":
var p ReadResourceParams
if err := json.Unmarshal(params, &p); err == nil {
return p.URI, true
}
}

return "", false
}

func setStandardHeaders(header http.Header, msg jsonrpc.Message) {
if msg == nil {
return
}
if header.Get(ProtocolVersionHeader) == "" || header.Get(ProtocolVersionHeader) < MinVersionForStandardHeaders {
return
}

switch msg := msg.(type) {
case *jsonrpc.Request:
header.Set(MethodHeader, msg.Method)
if name, ok := extractName(msg.Method, msg.Params); ok {
header.Set(NameHeader, name)
}
}
}

func validateMcpHeaders(header http.Header, msg jsonrpc.Message) error {
protocolVersion := header.Get(ProtocolVersionHeader)
if protocolVersion == "" || protocolVersion < MinVersionForStandardHeaders {
return nil
}

switch msg := msg.(type) {
case *jsonrpc.Request:
methodInHeader := header.Get(MethodHeader)
if methodInHeader == "" {
return errors.New("missing required Mcp-Method header")
}
if methodInHeader != msg.Method {
return fmt.Errorf("header mismatch: Mcp-Method header value '%s' does not match body value '%s'", methodInHeader, msg.Method)
}

if msg.Method == "tools/call" || msg.Method == "resources/read" || msg.Method == "prompts/get" {
nameInHeader := header.Get(NameHeader)
if nameInHeader == "" {
return fmt.Errorf("missing required Mcp-Name header for method %q", msg.Method)
}
if nameInBody, ok := extractName(msg.Method, msg.Params); ok {
Comment thread
guglielmo-san marked this conversation as resolved.
Outdated
if nameInHeader != nameInBody {
return fmt.Errorf("header mismatch: Mcp-Name header value '%s' does not match body value '%s'", nameInHeader, nameInBody)
}
}
}
}
return nil
}
Loading
Loading