Modern `HttpServletMcpTransport` does not check `Mcp-Name` when `params.name` or `params.uri` is not a JSON string

Aperta Adatta ai principianti
#1,352 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@starryeye ci sta già lavorando.

Dal 25/11/2026.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
backend, security

Direzione di ricerca

Inizia dal metodo moderno HttpServletMcpTransport.validateHeaders, intorno al ritorno anticipato quando params.name o params.uri non è una stringa JSON, e confrontalo con il percorso esistente di mancata corrispondenza tra header e body, che restituisce HTTP 400 con -32020. Leggi McpServer.extractPrimitiveName per il comportamento correlato ai nomi null, quindi aggiungi test di regressione che coprano tools/call, prompts/get e resources/read con name/uri numerici e booleani, verificando che venga restituito un 400 e che nessun handler venga invocato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Bug description

The modern HttpServletMcpTransport (MCP 2026-07-28) checks Mcp-Name against the body only when params.name (or params.uri) is a JSON string. For any other value it returns early, so neither a mismatch nor a missing Mcp-Name header is detected:

// HttpServletMcpTransport.validateHeaders (v2.2.0, lines 464-466)
if (!(paramsMap.get(nameKey) instanceof String expectedName)) {
    return null;
}

The params are then decoded with Jackson, which coerces scalars to the String field. "name": 1 becomes the tool name "1", and ToolsFeature looks up and runs tool "1". prompts/get takes the same path, and resources/read does the same for a non-string uri.

So a request with Mcp-Name: searchProducts and "name": 1 is not rejected and runs tool 1. The same request with "name": "1" (a string) is correctly rejected with HTTP 400 and -32020.

The 2026-07-28 Streamable HTTP spec ("Server Validation") asks servers to reject header/body mismatches with HTTP 400 and -32020 (HeaderMismatch). The class javadoc also says that filters may authorize on Mcp-Method/Mcp-Name because mismatching requests are rejected before dispatch.

The practical impact is narrow. Only a primitive whose name is a number or boolean literal is reachable, and only a deployment that authorizes by Mcp-Name in a front filter is affected. Arrays, objects and null already end as -32602.

This was first reported privately; the maintainers asked for it to be tracked as a public hardening issue.

Environment

io.modelcontextprotocol.sdk:mcp-core 2.2.0 (also 2.2.0-M1 and 2.2.0-M2), Java 21. The code is unchanged on main (779893c).

Steps to reproduce

  1. Register two tools on a modern server: searchProducts and 1.
  2. Map HttpServletMcpTransport at /mcp, with a servlet filter in front that returns 403 for tools/call unless Mcp-Name is searchProducts.
  3. Send:
POST /mcp
Content-Type: application/json
Accept: application/json, text/event-stream
MCP-Protocol-Version: 2026-07-28
Mcp-Method: tools/call
Mcp-Name: searchProducts

{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":1,
 "_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}}}

Observed on 2.2.0-M1: HTTP 200, and tool 1 runs. The code path is unchanged in 2.2.0.

Expected behavior

For tools/call, prompts/get and resources/read, a name/uri that is present but not a JSON string is rejected before dispatch (HTTP 400 with -32020, or -32602), and no handler runs. For example:

Object bodyName = paramsMap.get(nameKey);
if (bodyName == null) {
    return null; // left to params decoding (-32602)
}
if (!(bodyName instanceof String expectedName)) {
    return "Request " + nameKey + " must be a string";
}

A related detail: McpServer.extractPrimitiveName also returns null for a non-string name, so rejecting these requests early also keeps primitiveName non-null for named methods.

Minimal Complete Reproducible example

The request in step 3 is enough to see it against any modern server that has a tool named 1. A regression test could cover each named method against HttpServletMcpTransport, with name/uri set to a number and to a boolean, expecting HTTP 400 and no handler call.

Lingua principale
Java
Stelle
3.8k
Fork
1.1k
Merge medio
1g 3h
PR unite (30g)
38

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelcontextprotocol/java-sdk

Tutte le issue di modelcontextprotocol/java-sdk

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.