Skip to content

Latest commit

 

History

History

bug-repro-server

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

MCP Java SDK Completion Handler NPE Bug Repro

Minimal stdio MCP server that reproduces a NullPointerException in the MCP Java SDK's completion handler.

The Bug

When a Prompt is registered with null arguments and a completion/complete request arrives for that prompt, the server crashes with a NullPointerException because McpAsyncServer.completionCompleteRequestHandler calls prompt.arguments().stream() without a null guard.

Location: McpAsyncServer.java, in the completionCompleteRequestHandler lambda — the line that does:

promptSpec.prompt().arguments().stream()
    .filter(arg -> arg.name().equals(argumentName))
    .findFirst()

When prompt.arguments() returns null, calling .stream() on it throws NullPointerException.

Build

cd bug-repro-server
mvn package

Requires the MCP Java SDK 2.0.0-SNAPSHOT installed in your local Maven repo (~/.m2).

Reproduce

Run the server:

java -jar target/bug-repro-server-1.0-SNAPSHOT.jar

Then send these three JSON-RPC messages to stdin (one per line):

1. Initialize:

{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{"roots":{"listChanged":true}},"clientInfo":{"name":"test","version":"1.0"}}}

2. Initialized notification:

{"jsonrpc":"2.0","method":"notifications/initialized"}

3. Completion request (triggers the bug):

{"jsonrpc":"2.0","id":2,"method":"completion/complete","params":{"ref":{"type":"ref/prompt","name":"summarize"},"argument":{"name":"query","value":"sum"}}}

One-liner test

echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{"roots":{"listChanged":true}},"clientInfo":{"name":"test","version":"1.0"}}}
{"jsonrpc":"2.0","method":"notifications/initialized"}
{"jsonrpc":"2.0","id":2,"method":"completion/complete","params":{"ref":{"type":"ref/prompt","name":"summarize"},"argument":{"name":"query","value":"sum"}}}' | java -jar target/bug-repro-server-1.0-SNAPSHOT.jar

Expected

A proper error response or empty completion result.

Actual

The server returns an internal error with the NPE:

{
  "jsonrpc": "2.0",
  "id": 2,
  "error": {
    "code": -32603,
    "message": "Cannot invoke \"java.util.List.stream()\" because the return value of \"io.modelcontextprotocol.spec.McpSchema$Prompt.arguments()\" is null",
    "data": "NullPointerException: Cannot invoke \"java.util.List.stream()\" because the return value of \"io.modelcontextprotocol.spec.McpSchema$Prompt.arguments()\" is null"
  }
}

Fix

The SDK should add a null guard before streaming prompt arguments. Something like:

List<PromptArgument> args = promptSpec.prompt().arguments();
if (args == null || args.stream().noneMatch(arg -> arg.name().equals(argumentName))) {
    logger.warn("Argument not found: {} in prompt: {}", argumentName, promptRef.name());
    return EMPTY_COMPLETION_RESULT;
}