Repository navigation
Commit 507eb28
committed
Fix non-compliant JSON-RPC error responses in servlet transports
The servlet transports returned error bodies that did not conform to the
MCP JSON-RPC response format: they omitted the required "jsonrpc" field,
used "jsonRpcError" instead of "error", and did not include the request ID
even where it was available.
These malformed responses resulted from serializing McpError directly
rather than constructing a JSONRPCResponse error envelope. Because
McpError extends RuntimeException, this also caused information disclosure:
Jackson included exception internals such as stack traces in the response.
Before (wrong; illustrative response with an abbreviated stack trace):
```json
{
"jsonRpcError": {
"code": -123,
"message": "foo"
},
"message": "foo",
"localizedMessage": "foo",
"cause": null,
"suppressed": [],
"stackTrace": [
{
"className": "io.modelcontextprotocol.spec.McpError$Builder",
"methodName": "build",
"fileName": "McpError.java",
"lineNumber": 81,
"nativeMethod": false
}
]
}
```
After (correct; no request ID is available in this example):
```json
{
"jsonrpc": "2.0",
"error": {
"code": -123,
"message": "foo"
}
}
```
Write typed JSONRPCResponse envelopes across the SSE, streamable, and
stateless servlet transports. Allow error responses without an ID while
continuing to require IDs on successful responses, and propagate request
IDs where already parsed. Preserve the public McpError response writer
as a wrapper that unwraps the protocol error.
Add regression coverage for HTTP error envelopes, request IDs, and
exclusion of exception properties, including causes and suppressed
exceptions. Update tests that expected a top-level exception message.1 parent 29a1c76 commit 507eb28
8 files changed
Lines changed: 308 additions & 125 deletions
File tree
- mcp-core/src/main/java/io/modelcontextprotocol
- server/transport
- spec
- mcp-test/src/test/java/io/modelcontextprotocol
- server
- transport
- spec
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
| 10 | + | |
| 11 | + | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
| |||
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
23 | 40 | | |
24 | 41 | | |
25 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | 21 | | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
375 | 375 | | |
376 | 376 | | |
377 | 377 | | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
382 | 381 | | |
383 | 382 | | |
384 | 383 | | |
385 | 384 | | |
386 | 385 | | |
387 | 386 | | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
| 387 | + | |
| 388 | + | |
397 | 389 | | |
398 | 390 | | |
399 | 391 | | |
400 | 392 | | |
401 | 393 | | |
402 | 394 | | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
| 395 | + | |
| 396 | + | |
412 | 397 | | |
413 | 398 | | |
414 | 399 | | |
| 400 | + | |
415 | 401 | | |
416 | 402 | | |
417 | 403 | | |
418 | 404 | | |
419 | 405 | | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
420 | 409 | | |
421 | 410 | | |
422 | 411 | | |
| |||
430 | 419 | | |
431 | 420 | | |
432 | 421 | | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
| 422 | + | |
| 423 | + | |
443 | 424 | | |
444 | 425 | | |
445 | 426 | | |
| |||
486 | 467 | | |
487 | 468 | | |
488 | 469 | | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
497 | 474 | | |
498 | 475 | | |
499 | 476 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | 20 | | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
176 | 175 | | |
177 | 176 | | |
178 | 177 | | |
179 | 178 | | |
180 | 179 | | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
185 | 183 | | |
186 | 184 | | |
187 | 185 | | |
188 | 186 | | |
189 | 187 | | |
| 188 | + | |
190 | 189 | | |
191 | 190 | | |
192 | 191 | | |
193 | 192 | | |
194 | 193 | | |
195 | 194 | | |
| 195 | + | |
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
217 | 216 | | |
218 | 217 | | |
219 | 218 | | |
| |||
225 | 224 | | |
226 | 225 | | |
227 | 226 | | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
| 227 | + | |
| 228 | + | |
232 | 229 | | |
233 | 230 | | |
234 | 231 | | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
| 232 | + | |
| 233 | + | |
239 | 234 | | |
240 | 235 | | |
241 | 236 | | |
242 | 237 | | |
243 | 238 | | |
244 | 239 | | |
245 | 240 | | |
246 | | - | |
247 | | - | |
| 241 | + | |
| 242 | + | |
248 | 243 | | |
249 | 244 | | |
250 | 245 | | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
| 246 | + | |
| 247 | + | |
255 | 248 | | |
256 | 249 | | |
257 | 250 | | |
258 | 251 | | |
259 | 252 | | |
260 | 253 | | |
261 | 254 | | |
262 | | - | |
| 255 | + | |
| 256 | + | |
263 | 257 | | |
264 | 258 | | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
273 | 263 | | |
274 | 264 | | |
275 | 265 | | |
| |||
0 commit comments