-
Notifications
You must be signed in to change notification settings - Fork 417
feat: HTTP Header Standardization for method and name #907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
guglielmo-san
merged 13 commits into
main
from
guglielmoc/SEP-2243_http_standardization
Apr 29, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f00d0f4
refactor: centralize MCP headers and add support for validating stand…
guglielmo-san d00288d
fix
guglielmo-san 57659c0
formatter
guglielmo-san 017e0fc
Merge branch 'main' into guglielmoc/SEP-2243_http_standardization
guglielmo-san 604f2d4
fix after merge
guglielmo-san 7df5ab6
refactor: decouple standard header population from setMCPHeaders in s…
guglielmo-san 4af1f8e
refactor: decouple MCP header utilities from http.Request by acceptin…
guglielmo-san bfda3b4
fix comments
guglielmo-san ec300b1
Merge branch 'main' into guglielmoc/SEP-2243_http_standardization
guglielmo-san 2d17fd6
Merge branch 'main' into guglielmoc/SEP-2243_http_standardization
guglielmo-san 34be9bf
fix comments
guglielmo-san 9255d63
refactor: remove redundant manual JSON-RPC error parsing in streamabl…
guglielmo-san 341ce99
refactor: simplify response body handling by removing redundant body …
guglielmo-san File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
guglielmo-san marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // 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" | ||
|
|
||
| internaljson "github.com/modelcontextprotocol/go-sdk/internal/json" | ||
| "github.com/modelcontextprotocol/go-sdk/jsonrpc" | ||
| ) | ||
|
|
||
| const ( | ||
| protocolVersionHeader = "Mcp-Protocol-Version" | ||
| sessionIDHeader = "Mcp-Session-Id" | ||
| lastEventIDHeader = "Last-Event-ID" | ||
| methodHeader = "Mcp-Method" | ||
| nameHeader = "Mcp-Name" | ||
| minVersionForStandardHeaders = protocolVersion20260630 | ||
| ) | ||
|
|
||
| func extractName(method string, params json.RawMessage) (string, bool) { | ||
| switch method { | ||
| case "tools/call": | ||
| var p CallToolParams | ||
| if err := internaljson.Unmarshal(params, &p); err == nil { | ||
| return p.Name, true | ||
| } | ||
| case "prompts/get": | ||
| var p GetPromptParams | ||
| if err := internaljson.Unmarshal(params, &p); err == nil { | ||
| return p.Name, true | ||
| } | ||
| case "resources/read": | ||
| var p ReadResourceParams | ||
| if err := internaljson.Unmarshal(params, &p); err == nil { | ||
| return p.URI, true | ||
| } | ||
| } | ||
|
|
||
| return "", false | ||
| } | ||
|
|
||
| // setStandardHeaders populates standard MCP headers. | ||
| // It requires the protocol version header to be set. | ||
| func setStandardHeaders(header http.Header, msg jsonrpc.Message) { | ||
|
guglielmo-san marked this conversation as resolved.
|
||
| 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) | ||
| } | ||
| nameInBody, ok := extractName(msg.Method, msg.Params) | ||
| if !ok { | ||
| return fmt.Errorf("failed to extract name from parameters for method %q", msg.Method) | ||
| } | ||
| if nameInHeader != nameInBody { | ||
| return fmt.Errorf("header mismatch: Mcp-Name header value '%s' does not match body value '%s'", nameInHeader, nameInBody) | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.