Skip to content

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

Open
wucm667 wants to merge 10 commits 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 10 commits 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.

jba
jba previously approved these changes Apr 30, 2026
Comment thread mcp/server.go
@wucm667
Copy link
Copy Markdown
Author

wucm667 commented May 5, 2026

@guglielmo-san Thanks for catching that! Added nil checks for both InputSchema and OutputSchema after the type assertion to handle the typed nil case. Pushed the fix.

@guglielmo-san
Copy link
Copy Markdown
Contributor

@wucm667 I think you forgot to push the the fix :)

Add nil checks for *jsonschema.Schema after type assertion to catch
the case where a typed nil pointer is assigned to the interface field,
which would otherwise cause a SIGSEGV when accessing schema properties.

Suggested-by: guglielmo-san
Signed-off-by: wucm667 <stevenwucongmin@gmail.com>
@wucm667
Copy link
Copy Markdown
Author

wucm667 commented May 5, 2026

@guglielmo-san The fix is pushed now. Let me know if anything else needs adjusting.

@guglielmo-san
Copy link
Copy Markdown
Contributor

@wucm667 can you add tests to mcp/server_test.go for the changes you introduced?

Cover the typed nil InputSchema and OutputSchema cases that were
added to prevent SIGSEGV when a typed nil *jsonschema.Schema is
passed as a schema argument.
@wucm667
Copy link
Copy Markdown
Author

wucm667 commented May 5, 2026

@guglielmo-san Added tests in mcp/server_test.go covering both typed nil InputSchema and OutputSchema cases. All tests pass locally.

Comment thread mcp/server_test.go Outdated
Comment thread mcp/server_test.go Outdated
wucm667 and others added 5 commits May 5, 2026 19:16
Co-authored-by: Guglielmo Colombo <guglielmoc@google.com>
Co-authored-by: Guglielmo Colombo <guglielmoc@google.com>
The previous GitHub suggested change introduced a syntax error by
omitting the closing brace for TestAddToolNilSchema, causing lint
and test failures in CI.

Signed-off-by: wucm667 <stevenwucongmin@gmail.com>
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

3 participants