Skip to content

Commit 7f61335

Browse files
authored
mcp: cover all branches on nil checks for json response (#2275)
**Description** Fix the a nil check on MCP session to capture all branches. **Related Issues/PRs (if applicable)** N/A **Special notes for reviewers (if applicable)** N/A Signed-off-by: Ignasi Barrera <ignasi@tetrate.io>
1 parent 8766e8c commit 7f61335

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

internal/mcpproxy/handlers.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,17 @@ func (m *mcpRequestContext) servePOST(w http.ResponseWriter, r *http.Request) {
302302

303303
switch msg := rawMsg.(type) {
304304
case *jsonrpc.Response:
305+
// We do require a Session ID. If it is not present, a 400 Bad Request response should be returned:
306+
// https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#session-management
307+
if s == nil {
308+
errType = metrics.MCPErrorInvalidSessionID
309+
onErrorResponse(w, http.StatusBadRequest, "missing session ID")
310+
return
311+
}
305312
if doNotForwardResponseToBackends(msg) {
306313
w.Header().Set(sessionIDHeader, string(s.clientGatewaySessionID()))
307314
w.WriteHeader(http.StatusAccepted)
308315
} else {
309-
// We do require a Session ID. If it is not present, a 400 Bad Request response should be returned:
310-
// https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#session-management
311-
if s == nil {
312-
errType = metrics.MCPErrorInvalidSessionID
313-
onErrorResponse(w, http.StatusBadRequest, "missing session ID")
314-
return
315-
}
316316
m.l.Debug("Decoded MCP response", slog.Any("response", msg))
317317
result, err = m.handleClientToServerResponse(ctx, s, w, msg)
318318
}

0 commit comments

Comments
 (0)