Commit c1cde20
authored
refactor: improve MCP server resilience and error handling (#579)
- Replace McpError with appropriate standard exceptions (IllegalArgumentException, IllegalStateException) for validation errors
- Change tool/prompt registration to replace existing items instead of rejecting duplicates with warningsi
- Change addTool behavior to replace existing tools instead of throwing errors
- Change addPrompt() to allow replacing existing prompts with warning instead of throwing error
- Make removal operations idempotent - log warnings instead of throwing errors for non-existent items
- Change removePrompt() to log warning instead of throwing error for non-existent prompts
- Change removeTool() to gracefully handle non-existent tools with warnings instead of errors
- Add listTools() and listPrompts() methods to all server variants (async, sync, stateless)
- Add listTools() method to all server classes for tool enumeration
- Add listPrompts() method to all server implementations for retrieving registered prompts
- Improve error construction using McpError.builder() pattern for protocol-specific errors
- Update error handling in session classes to properly propagate McpError JSON-RPC errors
- Use proper error codes (INVALID_PARAMS) for prompt not found scenarios
- Update tests to reflect new lenient behavior for duplicate registrations and removals
- Add aggregateExceptionMessages() utility method utility to aggregate the exception chains
- Update McpServerSession and McpStreamableServerSession to use the aggregated errors in the json-rpc-error data section
This change makes the MCP server APIs more resilient and user-friendly by using appropriate exception types,
supporting item replacement, and providing listing capabilities while maintaining backward compatibility.
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>1 parent ff98e29 commit c1cde20
File tree
13 files changed
+305
-196
lines changed- mcp-core/src
- main/java/io/modelcontextprotocol
- server
- spec
- test/java/io/modelcontextprotocol/server
- mcp-test/src/main/java/io/modelcontextprotocol
- server
13 files changed
+305
-196
lines changedLines changed: 57 additions & 37 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
322 | 322 | | |
323 | 323 | | |
324 | 324 | | |
325 | | - | |
| 325 | + | |
326 | 326 | | |
327 | 327 | | |
328 | | - | |
| 328 | + | |
329 | 329 | | |
330 | 330 | | |
331 | | - | |
| 331 | + | |
332 | 332 | | |
333 | 333 | | |
334 | | - | |
| 334 | + | |
335 | 335 | | |
336 | 336 | | |
337 | 337 | | |
338 | 338 | | |
339 | 339 | | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
344 | 343 | | |
345 | 344 | | |
346 | 345 | | |
| |||
464 | 463 | | |
465 | 464 | | |
466 | 465 | | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
467 | 474 | | |
468 | 475 | | |
469 | 476 | | |
470 | 477 | | |
471 | 478 | | |
472 | 479 | | |
473 | 480 | | |
474 | | - | |
| 481 | + | |
475 | 482 | | |
476 | 483 | | |
477 | | - | |
| 484 | + | |
478 | 485 | | |
479 | 486 | | |
480 | 487 | | |
481 | | - | |
482 | | - | |
483 | | - | |
| 488 | + | |
| 489 | + | |
484 | 490 | | |
485 | 491 | | |
486 | 492 | | |
487 | 493 | | |
488 | | - | |
489 | 494 | | |
490 | | - | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
491 | 500 | | |
492 | 501 | | |
493 | 502 | | |
| |||
518 | 527 | | |
519 | 528 | | |
520 | 529 | | |
521 | | - | |
522 | | - | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
523 | 534 | | |
524 | 535 | | |
525 | 536 | | |
| |||
747 | 758 | | |
748 | 759 | | |
749 | 760 | | |
750 | | - | |
| 761 | + | |
751 | 762 | | |
752 | 763 | | |
753 | | - | |
| 764 | + | |
754 | 765 | | |
755 | 766 | | |
756 | 767 | | |
757 | | - | |
758 | | - | |
759 | | - | |
760 | | - | |
761 | | - | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
762 | 774 | | |
763 | | - | |
764 | | - | |
765 | | - | |
766 | | - | |
767 | | - | |
768 | | - | |
769 | 775 | | |
770 | | - | |
| 776 | + | |
771 | 777 | | |
| 778 | + | |
772 | 779 | | |
773 | 780 | | |
774 | 781 | | |
775 | 782 | | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
776 | 791 | | |
777 | 792 | | |
778 | 793 | | |
779 | 794 | | |
780 | 795 | | |
781 | 796 | | |
782 | 797 | | |
783 | | - | |
| 798 | + | |
784 | 799 | | |
785 | 800 | | |
786 | | - | |
| 801 | + | |
787 | 802 | | |
788 | 803 | | |
789 | 804 | | |
790 | 805 | | |
791 | 806 | | |
792 | 807 | | |
793 | 808 | | |
794 | | - | |
795 | | - | |
796 | 809 | | |
797 | 810 | | |
798 | 811 | | |
799 | 812 | | |
800 | 813 | | |
801 | | - | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
802 | 818 | | |
803 | 819 | | |
804 | 820 | | |
| |||
834 | 850 | | |
835 | 851 | | |
836 | 852 | | |
| 853 | + | |
837 | 854 | | |
838 | | - | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
839 | 859 | | |
840 | 860 | | |
841 | 861 | | |
| |||
Lines changed: 56 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
319 | 319 | | |
320 | 320 | | |
321 | 321 | | |
322 | | - | |
| 322 | + | |
323 | 323 | | |
324 | 324 | | |
325 | | - | |
| 325 | + | |
326 | 326 | | |
327 | 327 | | |
328 | | - | |
| 328 | + | |
329 | 329 | | |
330 | 330 | | |
331 | | - | |
| 331 | + | |
332 | 332 | | |
333 | 333 | | |
334 | 334 | | |
335 | 335 | | |
336 | 336 | | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
341 | 340 | | |
342 | 341 | | |
343 | 342 | | |
| |||
347 | 346 | | |
348 | 347 | | |
349 | 348 | | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
350 | 357 | | |
351 | 358 | | |
352 | 359 | | |
353 | 360 | | |
354 | 361 | | |
355 | 362 | | |
356 | 363 | | |
357 | | - | |
| 364 | + | |
358 | 365 | | |
359 | 366 | | |
360 | | - | |
| 367 | + | |
361 | 368 | | |
362 | 369 | | |
363 | 370 | | |
364 | | - | |
365 | | - | |
366 | | - | |
| 371 | + | |
| 372 | + | |
367 | 373 | | |
368 | | - | |
369 | 374 | | |
370 | | - | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
371 | 380 | | |
372 | 381 | | |
373 | 382 | | |
| |||
391 | 400 | | |
392 | 401 | | |
393 | 402 | | |
394 | | - | |
395 | | - | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
396 | 407 | | |
397 | 408 | | |
398 | 409 | | |
| |||
593 | 604 | | |
594 | 605 | | |
595 | 606 | | |
596 | | - | |
| 607 | + | |
597 | 608 | | |
598 | 609 | | |
599 | | - | |
| 610 | + | |
600 | 611 | | |
601 | 612 | | |
602 | 613 | | |
603 | | - | |
604 | | - | |
605 | | - | |
606 | | - | |
607 | | - | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
608 | 620 | | |
609 | | - | |
610 | | - | |
611 | 621 | | |
612 | 622 | | |
613 | 623 | | |
614 | 624 | | |
615 | 625 | | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
616 | 635 | | |
617 | 636 | | |
618 | 637 | | |
619 | 638 | | |
620 | 639 | | |
621 | 640 | | |
622 | 641 | | |
623 | | - | |
| 642 | + | |
624 | 643 | | |
625 | 644 | | |
626 | | - | |
| 645 | + | |
627 | 646 | | |
628 | 647 | | |
629 | 648 | | |
| |||
633 | 652 | | |
634 | 653 | | |
635 | 654 | | |
636 | | - | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
637 | 660 | | |
638 | 661 | | |
639 | 662 | | |
| |||
662 | 685 | | |
663 | 686 | | |
664 | 687 | | |
665 | | - | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
666 | 692 | | |
667 | 693 | | |
668 | 694 | | |
| |||
Lines changed: 16 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
77 | 85 | | |
78 | 86 | | |
79 | 87 | | |
| |||
148 | 156 | | |
149 | 157 | | |
150 | 158 | | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
151 | 167 | | |
152 | 168 | | |
153 | 169 | | |
| |||
0 commit comments