Commit 60b6751
committed
Handle the SEP-2575 Modern Request Envelope in the Server Core
## Motivation and Context
Second step of the stateless lifecycle (SEP-2575, modelcontextprotocol/modelcontextprotocol#2575)
for the 2026-07-28 MCP spec release, building on the `MCP::RequestEnvelope` foundations.
`Server#handle_request` now lifts the per-request `_meta` envelope before dispatch: a request
whose `_meta` carries the full required triple is validated (raising `-32022` with
`data: { supported:, requested: }` for unsupported versions) and its envelope is threaded into
`tools/call`, `prompts/get`, `completion/complete`, `resources/read`, subscribe/unsubscribe,
and custom method handlers via `MCP::ServerContext`. A partial triple keeps flowing through
the legacy path untouched, so existing `_meta` usage (`progressToken`, trace context) is unaffected.
`MCP::ServerContext` gains per-request readers and a capability guard:
- `modern?`, `protocol_version`, `client_info`, and `client_capabilities` read from the envelope first
and fall back to session state stored by `initialize` on legacy requests. The envelope always wins
because servers MUST NOT infer client state from prior requests; nothing is written to the session
on the modern path.
- `require_client_capability!(*path)` raises `Server::MissingRequiredClientCapabilityError`
(`-32021` with `data: { requiredCapabilities: }`) when the request did not declare the capability.
- `notify_log_message` honors the per-request `io.modelcontextprotocol/logLevel`: on modern requests without it,
the server MUST NOT send `notifications/message`, and an insufficient level drops the message the same way.
`MCP::ServerSession` gains the connection-era lock of the dual-era serving model: `era` is `nil`
until the first era-distinctive message succeeds, a successful `initialize` locks `:legacy` as
a side effect of `mark_initialized!`, `lock_era!` refuses to flip an established era, and transports can construct
per-request sessions with `era: :modern`. On a modern-locked session, `initialize` is rejected with `-32022`
(the modern lifecycle has no handshake) and the envelope triple becomes required for every other request
except `server/discover`.
The existing plain `RuntimeError` raises in `ServerSession#list_roots` and friends are intentionally unchanged:
converting them to `RequestHandlerError` subclasses would break callers rescuing `RuntimeError`.
Typed `-32021` errors are scoped to the new envelope-based guard.
Refs modelcontextprotocol#389.
## How Has This Been Tested?
New tests in `test/mcp/server_test.rb` cover the wire behavior through `Server#handle`: envelope data exposure
without session mutation, `-32022` for unsupported envelope versions, partial triples staying legacy,
the envelope requirement and `initialize` rejection on modern-locked sessions, the `server/discover` exemption,
`require_client_capability!` returning `-32021` with the `requiredCapabilities` data shape,
and the `ServerSession` era-lock transitions (including preset `era: :modern` and invalid values).
New tests in `test/mcp/server_context_test.rb` cover the per-request logLevel gate (absent, insufficient,
and sufficient levels), the envelope-first/session-fallback readers, and string-keyed capability matching
in `require_client_capability!`.
## Breaking Changes
None. All new keyword arguments default to `nil`, legacy requests take exactly the same code path as before,
and the era lock only constrains sequences that were previously impossible (modern-era traffic).1 parent 75fffab commit 60b6751
5 files changed
Lines changed: 379 additions & 16 deletions
File tree
- lib/mcp
- test/mcp
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
560 | 560 | | |
561 | 561 | | |
562 | 562 | | |
| 563 | + | |
563 | 564 | | |
564 | 565 | | |
565 | 566 | | |
566 | 567 | | |
567 | | - | |
| 568 | + | |
568 | 569 | | |
569 | 570 | | |
570 | | - | |
| 571 | + | |
571 | 572 | | |
572 | 573 | | |
573 | | - | |
| 574 | + | |
574 | 575 | | |
575 | | - | |
| 576 | + | |
576 | 577 | | |
577 | | - | |
| 578 | + | |
578 | 579 | | |
579 | 580 | | |
580 | 581 | | |
581 | | - | |
| 582 | + | |
582 | 583 | | |
583 | 584 | | |
584 | 585 | | |
| |||
609 | 610 | | |
610 | 611 | | |
611 | 612 | | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
612 | 641 | | |
613 | 642 | | |
614 | 643 | | |
| |||
748 | 777 | | |
749 | 778 | | |
750 | 779 | | |
751 | | - | |
| 780 | + | |
752 | 781 | | |
753 | 782 | | |
754 | 783 | | |
| |||
781 | 810 | | |
782 | 811 | | |
783 | 812 | | |
784 | | - | |
| 813 | + | |
785 | 814 | | |
786 | 815 | | |
787 | 816 | | |
| |||
808 | 837 | | |
809 | 838 | | |
810 | 839 | | |
811 | | - | |
| 840 | + | |
812 | 841 | | |
813 | 842 | | |
814 | 843 | | |
| |||
826 | 855 | | |
827 | 856 | | |
828 | 857 | | |
| 858 | + | |
829 | 859 | | |
830 | 860 | | |
831 | 861 | | |
| |||
921 | 951 | | |
922 | 952 | | |
923 | 953 | | |
924 | | - | |
| 954 | + | |
925 | 955 | | |
926 | 956 | | |
927 | 957 | | |
| |||
930 | 960 | | |
931 | 961 | | |
932 | 962 | | |
| 963 | + | |
933 | 964 | | |
934 | 965 | | |
935 | 966 | | |
| |||
938 | 969 | | |
939 | 970 | | |
940 | 971 | | |
941 | | - | |
| 972 | + | |
942 | 973 | | |
943 | 974 | | |
944 | 975 | | |
945 | 976 | | |
946 | 977 | | |
947 | 978 | | |
| 979 | + | |
948 | 980 | | |
949 | 981 | | |
950 | 982 | | |
951 | 983 | | |
952 | 984 | | |
953 | 985 | | |
954 | 986 | | |
955 | | - | |
| 987 | + | |
956 | 988 | | |
957 | 989 | | |
958 | 990 | | |
959 | 991 | | |
960 | 992 | | |
961 | 993 | | |
962 | 994 | | |
| 995 | + | |
963 | 996 | | |
964 | 997 | | |
965 | 998 | | |
| |||
984 | 1017 | | |
985 | 1018 | | |
986 | 1019 | | |
987 | | - | |
| 1020 | + | |
988 | 1021 | | |
989 | 1022 | | |
990 | 1023 | | |
| |||
994 | 1027 | | |
995 | 1028 | | |
996 | 1029 | | |
| 1030 | + | |
997 | 1031 | | |
998 | 1032 | | |
999 | 1033 | | |
| |||
1053 | 1087 | | |
1054 | 1088 | | |
1055 | 1089 | | |
1056 | | - | |
| 1090 | + | |
1057 | 1091 | | |
1058 | 1092 | | |
1059 | 1093 | | |
| |||
1068 | 1102 | | |
1069 | 1103 | | |
1070 | 1104 | | |
| 1105 | + | |
1071 | 1106 | | |
1072 | 1107 | | |
1073 | 1108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
11 | 15 | | |
12 | 16 | | |
| 17 | + | |
13 | 18 | | |
14 | 19 | | |
15 | 20 | | |
| |||
20 | 25 | | |
21 | 26 | | |
22 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
23 | 74 | | |
24 | 75 | | |
25 | 76 | | |
| |||
41 | 92 | | |
42 | 93 | | |
43 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
44 | 103 | | |
45 | 104 | | |
46 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
13 | 22 | | |
14 | 23 | | |
15 | 24 | | |
| 25 | + | |
16 | 26 | | |
17 | 27 | | |
18 | 28 | | |
| |||
31 | 41 | | |
32 | 42 | | |
33 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
34 | 57 | | |
35 | 58 | | |
36 | 59 | | |
| |||
236 | 259 | | |
237 | 260 | | |
238 | 261 | | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
239 | 266 | | |
240 | 267 | | |
241 | 268 | | |
| |||
0 commit comments