Skip to content

fix(gateway): guard nil session in mcp-add handler#527

Open
syf2211 wants to merge 1 commit into
docker:mainfrom
syf2211:fix/442-mcp-add-nil-session
Open

fix(gateway): guard nil session in mcp-add handler#527
syf2211 wants to merge 1 commit into
docker:mainfrom
syf2211:fix/442-mcp-add-nil-session

Conversation

@syf2211

@syf2211 syf2211 commented Jun 27, 2026

Copy link
Copy Markdown

Summary

Guard req.Session before calling InitializeParams() and Elicit() in the mcp-add tool handler so the gateway returns structured errors instead of panicking when session info is unavailable.

Fixes #442

Motivation

Some MCP transports/clients invoke mcp-add without populating req.Session. The handler dereferenced req.Session directly in the missing-config/secrets path and in remote OAuth handling, causing a nil pointer panic instead of a useful response.

Changes

  • Add nil checks before req.Session.InitializeParams() when determining client name for missing secrets/config handling
  • Allow remote OAuth handling to proceed when session is nil (delegate to getRemoteOAuthServerStatus, which already has safe fallbacks)
  • Guard elicitation flow in getRemoteOAuthServerStatus when session is nil
  • Add regression tests for nil-session paths

Tests

  • go test ./pkg/gateway/ -run 'TestAddServerHandlerNilSession|TestGetRemoteOAuthServerStatusNilSession' -v -count=1 — PASS
  • go test ./pkg/gateway/ -short -count=1 — PASS
  • go vet ./pkg/gateway/... — PASS
  • make lint — skipped (requires Docker in this environment)

Notes

  • Pre-PR review: composer-2.5 reviewed; OAuth path updated per feedback to still invoke getRemoteOAuthServerStatus when session is nil.

Fixes docker#442

Add nil checks before accessing req.Session in addServerHandler and
getRemoteOAuthServerStatus so mcp-add returns structured errors instead
of panicking when session info is unavailable.
@syf2211 syf2211 requested a review from a team as a code owner June 27, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic in mcp-add when req.Session is nil (nil pointer dereference)

1 participant