Skip to content

fix(mcp): prevent SIGSEGV in AddTool when schema is nil#918

Open
wucm667 wants to merge 1 commit intomodelcontextprotocol:mainfrom
wucm667:fix/go-sdk-add-tool-nil-schema
Open

fix(mcp): prevent SIGSEGV in AddTool when schema is nil#918
wucm667 wants to merge 1 commit intomodelcontextprotocol:mainfrom
wucm667:fix/go-sdk-add-tool-nil-schema

Conversation

@wucm667
Copy link
Copy Markdown

@wucm667 wucm667 commented Apr 30, 2026

Bug

mcp.AddTool crashes the server with SIGSEGV when input/output struct tags trigger a nil schema.

Root Cause

In mcp/server.go, the function setSchema calls internalSchema.Resolve(...) without checking if internalSchema is nil. If the schema inference (e.g., via jsonschema.ForType) or a provided schema results in a nil value, this dereference causes a panic.

Fix

Added nil checks for internalSchema before calling Resolve in two locations within setSchema:

  1. After schema generation via jsonschema.ForType.
  2. After processing a provided schema (which might be nil or fail to unmarshal).

If internalSchema is nil, the function now returns a descriptive error instead of crashing.

Verification

  • go test ./mcp/... passes.
  • The fix prevents the crash by handling the nil case gracefully.

Closes #916.

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.

mcp.AddTool crashes the server with SIGSEGV when input/output struct tags trigger a nil schema from jsonschema-go — missing nil-guard in setSchema

2 participants