mcp: HTTP Header Standardization for x-mcp-header (#915)
## Description Implements [SEP-2243](https://modelcontextprotocol.io/seps/2243-http-standardization) (HTTP Header Standardization) for x-mcp-param custom header. Fixes https://github.com/modelcontextprotocol/go-sdk/issues/905 --------- Co-authored-by: Maciej Kisiel <mkisiel@google.com>
This commit is contained in:
parent
d10c315008
commit
909372d8d1
1 changed files with 1512 additions and 15 deletions
|
|
@ -491,6 +491,7 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
|
|||
http.Error(w, "failed connection", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
transport.connection.toolLookup = server.getServerTool
|
||||
// Capture the user ID from the token info to enable session hijacking
|
||||
// prevention on subsequent requests.
|
||||
var userID string
|
||||
|
|
@ -669,6 +670,8 @@ type streamableServerConn struct {
|
|||
|
||||
logger *slog.Logger
|
||||
|
||||
toolLookup func(name string) (*serverTool, bool)
|
||||
|
||||
incoming chan jsonrpc.Message // messages from the client to the server
|
||||
|
||||
mu sync.Mutex // guards all fields below
|
||||
|
|
@ -1202,9 +1205,9 @@ func (c *streamableServerConn) servePOST(w http.ResponseWriter, req *http.Reques
|
|||
}
|
||||
}
|
||||
|
||||
// Validate MCP standard headers (Mcp-Method, Mcp-Name)
|
||||
// Validate MCP standard headers (Mcp-Method, Mcp-Name, Mcp-Param-*)
|
||||
if !isBatch && len(incoming) == 1 {
|
||||
if err := validateMcpHeaders(req.Header, incoming[0]); err != nil {
|
||||
if err := validateMcpHeaders(req.Header, incoming[0], c.toolLookup); err != nil {
|
||||
resp := &jsonrpc.Response{
|
||||
Error: jsonrpc2.NewError(CodeHeaderMismatch, err.Error()),
|
||||
}
|
||||
|
|
@ -1829,7 +1832,7 @@ func (c *streamableClientConn) Write(ctx context.Context, msg jsonrpc.Message) e
|
|||
}
|
||||
// Keep this after the setMCPHeaders call to ensure that the
|
||||
// protocol version header is set.
|
||||
setStandardHeaders(req.Header, msg)
|
||||
setStandardHeaders(ctx, req.Header, msg)
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
// Any error from client.Do means the request didn't reach the server.
|
||||
|
|
|
|||
Loading…
Reference in a new issue