Commit 1cf7903
authored
Refactor HttpClient-based transports to use Publisher instead of Subscriber (#1079)
HttpClient-based transports used to capture the enclosing sseSink in the subscriber, leading to HttpClient leaks when the client was closed. This PR addresses this, and adds many other improvements to HttpClient-based transports.
This has no public API change.
Improved transport reliability:
- Closed transports no longer keep their `HttpClient` alive, so selector threads and memory stop piling up
- `closeGracefully()` now releases open connections even when the session DELETE fails, e.g. when the server is down
- `connect()` on the legacy SSE transport no longer hangs when it gets the stream ends (error, stream closed,`closeGracefully()`, ...) before the first event or
- `sendMessage()` on Streamable HTTP no longer hangs when the SSE stream is closed without response or before the response arrives
- Responses the client never reads are always released (e.g. `DELETE`), so connections go back to the pool.
Errors surface immediately instead of as timeouts:
- On Streamable HTTP, a JSON response that can't be read (malformed, or over maxResponseSize) now fails the request immediately with the real cause, instead of a TimeoutException after requestTimeout`
- A server that answers a request with an empty JSON body now makes that request fail instead of silently timing out. An empty body in reply to a notification is still tolerated.
- Server-caused errors are now McpTransportException instead of a plain RuntimeException, and the message includes the response body the server sent.
- Errors that happen after connect() or sendMessage() has already completed now reach the transport's exception handler instead of Reactor "onErrorDropped" logs.
- A 404 or 400 invalidates the session only if the request that got it carried a session id. Fixes a race condition where are reconnect got a new session while another request was already in flight (with the old session). The old request could have ended up invalidating the new session.
Performance
- Large SSE responses, such as multi-MB tool results, are no longer slow to receive.
SSE parsing spec compliance
- Unknown fields such as retry: are ignored instead of failing the stream with "Invalid SSE response".
- The event type resets after each event, so a message that follows a named event is no longer misclassified and dropped.
- A data: line containing U+2028, U+2029 or U+0085 is no longer truncated.
- The legacy SSE transport skips empty "primer" events and unknown event types instead of failing.
- An empty id: clears the last event id.
Fixes #547
Fixes #620
Fixes #1042
Fixes #1047
Fixes #1147
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
Signed-off-by: Dariusz Jędrzejczyk <dariusz.jedrzejczyk@broadcom.com>1 parent c7fef64 commit 1cf7903
22 files changed
Lines changed: 2809 additions & 1456 deletions
File tree
- mcp-core/src
- main/java/io/modelcontextprotocol
- client/transport
- spec
- test/java/io/modelcontextprotocol
- client/transport
- spec
- mcp-test/src/test/java/io/modelcontextprotocol/client/transport
Lines changed: 89 additions & 61 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | 19 | | |
21 | 20 | | |
22 | 21 | | |
| |||
390 | 389 | | |
391 | 390 | | |
392 | 391 | | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
| 392 | + | |
| 393 | + | |
404 | 394 | | |
405 | | - | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
406 | 399 | | |
407 | 400 | | |
408 | | - | |
| 401 | + | |
409 | 402 | | |
410 | 403 | | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
429 | 426 | | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
| 427 | + | |
| 428 | + | |
435 | 429 | | |
436 | 430 | | |
437 | | - | |
438 | | - | |
| 431 | + | |
439 | 432 | | |
440 | 433 | | |
441 | | - | |
442 | | - | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
443 | 447 | | |
444 | 448 | | |
445 | | - | |
446 | | - | |
447 | | - | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
448 | 452 | | |
449 | | - | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
450 | 469 | | |
451 | 470 | | |
452 | 471 | | |
453 | | - | |
454 | 472 | | |
455 | 473 | | |
456 | 474 | | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
457 | 479 | | |
458 | 480 | | |
459 | 481 | | |
| |||
486 | 508 | | |
487 | 509 | | |
488 | 510 | | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
| 511 | + | |
500 | 512 | | |
501 | 513 | | |
502 | 514 | | |
| |||
517 | 529 | | |
518 | 530 | | |
519 | 531 | | |
520 | | - | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
521 | 542 | | |
522 | 543 | | |
523 | 544 | | |
| |||
529 | 550 | | |
530 | 551 | | |
531 | 552 | | |
532 | | - | |
533 | | - | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
534 | 562 | | |
535 | 563 | | |
536 | 564 | | |
| |||
0 commit comments