Skip to content

fix: guard against null Prompt.arguments() in completion handlers#933

Closed
sainathreddyb wants to merge 1 commit intomodelcontextprotocol:mainfrom
sainathreddyb:fix/prompt-arguments-npe
Closed

fix: guard against null Prompt.arguments() in completion handlers#933
sainathreddyb wants to merge 1 commit intomodelcontextprotocol:mainfrom
sainathreddyb:fix/prompt-arguments-npe

Conversation

@sainathreddyb
Copy link
Copy Markdown


Fixes #932

The 2.0 forward-compat refactor (#972) changed Prompt constructors to stop coercing null arguments to an empty list, but the completion validation code in McpAsyncServer and McpStatelessAsyncServer still called .arguments().stream() without a null guard, causing a NullPointerException when a Prompt with null arguments receives a completion/complete request.

Additionally, MIGRATION-2.0.md references Prompt.withDefaults() as the migration path, but this factory method was never implemented.

This commit:

  • Adds a null check before .arguments().stream() in both McpAsyncServer and McpStatelessAsyncServer
  • Adds Prompt.withDefaults(name, description, arguments) factory that coerces null to an empty list, matching the 1.x behaviour

Motivation and Context

After #972 merged, any MCP server that registers a Prompt with null arguments and a completion handler will crash with a NullPointerException when a completion/complete request arrives. This is the SDK's own internal code failing to handle the null-semantics change it introduced — not a user error.

How Has This Been Tested?

  • Reproduced the bug with a standalone stdio MCP server that registers a prompt with null arguments and sends a completion/complete request — confirmed the NPE
  • Applied the fix and verified the server returns an empty completion result instead of crashing
  • All existing tests pass (./mvnw clean test)

Breaking Changes

None. This is a bugfix that makes the SDK more tolerant.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the [MCP Documentation](https://modelcontextprotocol.io)
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

The Prompt.withDefaults() factory was documented in MIGRATION-2.0.md as the recommended migration path but never implemented. This PR adds it so the migration guide is accurate.

The 2.0 forward-compat refactor (#972) changed Prompt constructors to
stop coercing null arguments to an empty list, but the completion
validation code in McpAsyncServer and McpStatelessAsyncServer still
called .arguments().stream() without a null guard, causing a
NullPointerException when a Prompt with null arguments receives a
completion/complete request.

Additionally, MIGRATION-2.0.md references Prompt.withDefaults() as the
migration path for callers that relied on the old null-to-empty-list
coercion, but this factory method was never implemented.

This commit:
- Adds a null check before .arguments().stream() in both
  McpAsyncServer and McpStatelessAsyncServer
- Adds Prompt.withDefaults(name, description, arguments) factory that
  coerces null to an empty list, matching the 1.x behaviour
@chemicL
Copy link
Copy Markdown
Member

chemicL commented Apr 24, 2026

Thanks for the proposal to fix this, however the fix here would result in actually executing the handler while the previous behavior was to return an empty response without calling the handler. I addressed this together with a test in #934

@chemicL chemicL closed this Apr 24, 2026
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.

NullPointerException in completion handler when Prompt has null arguments

2 participants