mcp: add Allow header to 405 responses per RFC 9110 §15.5.6 (#757)
405 Method Not Allowed responses MUST include an Allow header listing supported methods, per RFC 9110 Section 15.5.6. This fixes issues with strict HTTP gateways (like Apigee) that treat 405 responses without an Allow header as malformed, returning 502 Bad Gateway errors. ## Changes - **SSEHandler**: Add `Allow: GET, POST` header for unsupported methods - **StreamableHTTPHandler**: Add Allow header for GET-without-session case: - Stateless mode: `Allow: POST, DELETE` (GET is never valid) - Stateful mode: `Allow: GET, POST, DELETE` (GET is valid once you have a session) - Add tests to verify Allow header presence in all 405 responses ## RFC 9110 Reference [Section 15.5.6 (405 Method Not Allowed)](https://httpwg.org/specs/rfc9110.html#status.405): > The origin server MUST generate an Allow header field in a 405 response containing a list of the target resource's currently supported methods. ## Testing All existing tests pass, plus new tests added: - `TestSSE405AllowHeader` - verifies SSE handler compliance - `TestStreamable405AllowHeader` - verifies Streamable handler compliance in both stateful and stateless modes --------- Co-authored-by: omgitsads <4619+omgitsads@users.noreply.github.com>
This commit is contained in:
parent
c2c7edc3f0
commit
52e03de6c8
1 changed files with 153 additions and 5 deletions
|
|
@ -275,12 +275,27 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
|
|||
switch req.Method {
|
||||
case http.MethodPost, http.MethodGet:
|
||||
if req.Method == http.MethodGet && (h.opts.Stateless || sessionID == "") {
|
||||
http.Error(w, "GET requires an active session", http.StatusMethodNotAllowed)
|
||||
if h.opts.Stateless {
|
||||
// Per MCP spec: server MUST return 405 if it doesn't offer SSE stream.
|
||||
// In stateless mode, GET (SSE streaming) is not supported.
|
||||
// RFC 9110 §15.5.6: 405 responses MUST include Allow header.
|
||||
w.Header().Set("Allow", "POST")
|
||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
} else {
|
||||
// In stateful mode, GET is supported but requires a session ID.
|
||||
// This is a precondition error, similar to DELETE without session.
|
||||
http.Error(w, "Bad Request: GET requires an Mcp-Session-Id header", http.StatusBadRequest)
|
||||
}
|
||||
return
|
||||
}
|
||||
default:
|
||||
w.Header().Set("Allow", "GET, POST, DELETE")
|
||||
http.Error(w, "Method Not Allowed: streamable MCP servers support GET, POST, and DELETE requests", http.StatusMethodNotAllowed)
|
||||
// RFC 9110 §15.5.6: 405 responses MUST include Allow header.
|
||||
if h.opts.Stateless {
|
||||
w.Header().Set("Allow", "POST")
|
||||
} else {
|
||||
w.Header().Set("Allow", "GET, POST, DELETE")
|
||||
}
|
||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue