-
Notifications
You must be signed in to change notification settings - Fork 239
Panic in mcp-add when req.Session is nil (nil pointer dereference) #442
Description
Panic in mcp-add when req.Session is nil (nil pointer dereference)
Summary
The Docker MCP Gateway panics with a nil pointer dereference when the mcp-add tool is invoked to add a server from a custom catalog (e.g. --additional-catalog webhook-mcp). The crash occurs in pkg/gateway/mcpadd.go when req.Session is nil and code calls req.Session.InitializeParams() without a nil check.
Environment
- Docker MCP Gateway:
docker mcp gateway run --additional-catalog webhook-mcp - OS: macOS (darwin)
- Custom catalog: Local YAML catalog for a container-based MCP server (
webhook-mcp)
Steps to Reproduce
- Create a custom MCP catalog YAML that defines a server (e.g.
webhook-mcp) withimageorcommandand add it via--additional-catalog. - Start the gateway:
docker mcp gateway run --additional-catalog webhook-mcp - Connect a client (e.g. Cursor, Claude Desktop) to the gateway.
- Invoke the
mcp-addtool with the server name (e.g.{"name": "webhook-mcp"}). - The gateway panics.
Expected Behavior
The gateway should either:
- Successfully add the server and return a result, or
- Return a structured error (e.g. "missing session info") instead of panicking.
Actual Behavior
The gateway panics with a nil pointer dereference:
panic: runtime error: invalid memory address or nil pointer dereference
[stack trace pointing to pkg/gateway/mcpadd.go:222]
Root Cause
In mcpadd.go, req.Session is accessed without a nil check in several places:
-
Line ~177 (missing secrets/config handling):
if init := req.Session.InitializeParams(); init != nil && init.ClientInfo != nil { clientName = init.ClientInfo.Name }
If
req.Sessionis nil,req.Session.InitializeParams()panics. -
Line ~294–295 (OAuth handling in
getRemoteOAuthServerStatus):init := req.Session.InitializeParams()
Same issue when
req.Sessionis nil.
For some transports or client connection types, req.Session can be nil. The code should guard these accesses.
Suggested Fix
Add nil checks before accessing req.Session:
// Example for the first case (~line 177):
var clientName string
if req.Session != nil {
if init := req.Session.InitializeParams(); init != nil && init.ClientInfo != nil {
clientName = init.ClientInfo.Name
}
}
// Example for OAuth handling (~line 294):
var init *mcp.InitializeParams
if req.Session != nil {
init = req.Session.InitializeParams()
}
// Then use init with nil checks where neededAdditional Context
- The custom catalog server (
webhook-mcp) is a local container-based MCP server with tools for querying webhook data. - The panic occurs regardless of whether the server is remote/OAuth or local; the crash happens before that logic when
req.Sessionis nil. - This blocks use of
mcp-addfor dynamically adding servers from custom catalogs when the session is not populated.