mcp: avoid panicking when a resource handler returns nil - #1176
Merged
guglielmo-san merged 1 commit intoAug 17, 2026
Merged
Conversation
readResource dereferenced the handler's result before the nil check that was meant to guard it, so a ResourceHandler returning (nil, nil) crashed the server with a nil pointer panic instead of returning an error. Request handlers run in their own goroutine with no recover, so that panic terminates the whole process and every concurrent session, not just the offending request. The sibling handlers callTool and getPrompt already guard their result with `if err == nil && res != nil` before touching it. Move the nil check in readResource ahead of handleMultiRoundTripResult and setDefaultCacheableValues so a nil result returns the existing descriptive error, and add a regression test.
guglielmo-san
approved these changes
Aug 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
readResourcedereferenced the handler's result before theif res == nilcheck that was meant to guard it. A
ResourceHandlerreturning(nil, nil)therefore panicked instead of returning an error:
handleMultiRoundTripResultcallsres.setResultTypeon thenil result, and
res.setDefaultCacheableValues()runs on the nil result.Either way the
if res == nilguard a few lines below was dead code.Why it matters
Request handlers run in their own goroutine and nothing recovers, so the panic
does not just fail the one request. It terminates the whole server process and
every concurrent session.
The sibling handlers already avoid this:
callToolandgetPromptboth guardtheir result with
if err == nil && res != nilbefore touching it, so a(nil, nil)return flows up and becomes a normal error response.Change
Move the nil check in
readResourceahead ofhandleMultiRoundTripResultandsetDefaultCacheableValues, so a nil result returns the existing descriptiveerror (
read handler returned nil information) instead of panicking. Added aregression test that drives a resource handler returning
(nil, nil)throughthe server and asserts a clean error.