Repository navigation
Commit 0270b24
committed
Refuse ids and method names that cannot be encoded
## Motivation and Context
A request id or method name holding bytes that are not valid UTF-8 comes out of the JSON parser as sent and
reaches a response. The Streamable HTTP transport echoes the id in the refusals it builds itself:
the `-32020` header mismatch, the `-32022` unsupported version, the session id sent in the modern lifecycle,
the duplicate `initialize`, the legacy envelope and the over-size `initialize` errors. `JsonRpcHandler` echoes
an unknown method name in the `data` of Method not found. With such bytes `to_json` raises: the transport's rescue
answers 500 with the error reported instead of the intended 400 or 413, and on the legacy path a Method not
found for such a name cannot be generated and is answered `-32603` Internal error. Any client can send either.
Those refusals now carry the id only when it is an Integer or a String that is valid UTF-8, and otherwise
what they carry for an id that could not be read; `JsonRpcHandler` refuses a method name that is not
valid UTF-8 with `-32600` Invalid Request before dispatch, as it refuses one that is not a string.
## How Has This Been Tested?
`test/json_rpc_handler_test.rb` (such a method name answers `-32600` without calling the handler registered under it)
and four tests in `test/mcp/server/transports/streamable_http_transport_test.rb` (such an id on the modern `-32022`
and `-32020` refusals and on a legacy duplicate `initialize` answers 400 with no readable id and no exception report;
a refusal keeps an empty or a large integer id and drops a boolean, a fraction or an object; such a method name on
a legacy session answers `-32600` in the SSE body). Against the previous library the invalid-byte id tests get a 500,
the id-type test sees the boolean echoed, the handler test sees its handler run, and the legacy method-name test gets `-32603`.
## Breaking Changes
The transport refusals named above answer a request whose id is not an Integer or a valid UTF-8 String as for
an id they could not read, where they echoed an id of another type as sent; the `subscriptions/listen` refusals are unchanged.1 parent d518025 commit 0270b24
4 files changed
Lines changed: 130 additions & 7 deletions
File tree
- lib
- mcp/server/transports
- test
- mcp/server/transports
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
230 | | - | |
| 230 | + | |
231 | 231 | | |
232 | 232 | | |
233 | 233 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
907 | 907 | | |
908 | 908 | | |
909 | 909 | | |
910 | | - | |
| 910 | + | |
911 | 911 | | |
912 | 912 | | |
913 | 913 | | |
| |||
916 | 916 | | |
917 | 917 | | |
918 | 918 | | |
919 | | - | |
| 919 | + | |
920 | 920 | | |
921 | 921 | | |
922 | 922 | | |
| |||
1135 | 1135 | | |
1136 | 1136 | | |
1137 | 1137 | | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
1138 | 1146 | | |
1139 | 1147 | | |
1140 | 1148 | | |
| |||
1576 | 1584 | | |
1577 | 1585 | | |
1578 | 1586 | | |
1579 | | - | |
| 1587 | + | |
1580 | 1588 | | |
1581 | 1589 | | |
1582 | 1590 | | |
| |||
1653 | 1661 | | |
1654 | 1662 | | |
1655 | 1663 | | |
1656 | | - | |
| 1664 | + | |
1657 | 1665 | | |
1658 | 1666 | | |
1659 | 1667 | | |
| |||
1699 | 1707 | | |
1700 | 1708 | | |
1701 | 1709 | | |
1702 | | - | |
| 1710 | + | |
1703 | 1711 | | |
1704 | 1712 | | |
1705 | 1713 | | |
| |||
1966 | 1974 | | |
1967 | 1975 | | |
1968 | 1976 | | |
1969 | | - | |
| 1977 | + | |
1970 | 1978 | | |
1971 | 1979 | | |
1972 | 1980 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
76 | 91 | | |
77 | 92 | | |
78 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
398 | 398 | | |
399 | 399 | | |
400 | 400 | | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
401 | 458 | | |
402 | 459 | | |
403 | 460 | | |
| |||
6300 | 6357 | | |
6301 | 6358 | | |
6302 | 6359 | | |
| 6360 | + | |
| 6361 | + | |
| 6362 | + | |
| 6363 | + | |
| 6364 | + | |
| 6365 | + | |
| 6366 | + | |
| 6367 | + | |
| 6368 | + | |
| 6369 | + | |
| 6370 | + | |
| 6371 | + | |
| 6372 | + | |
| 6373 | + | |
| 6374 | + | |
| 6375 | + | |
| 6376 | + | |
| 6377 | + | |
| 6378 | + | |
| 6379 | + | |
| 6380 | + | |
| 6381 | + | |
| 6382 | + | |
| 6383 | + | |
| 6384 | + | |
| 6385 | + | |
| 6386 | + | |
| 6387 | + | |
| 6388 | + | |
| 6389 | + | |
| 6390 | + | |
| 6391 | + | |
| 6392 | + | |
| 6393 | + | |
| 6394 | + | |
| 6395 | + | |
| 6396 | + | |
| 6397 | + | |
| 6398 | + | |
| 6399 | + | |
| 6400 | + | |
| 6401 | + | |
| 6402 | + | |
6303 | 6403 | | |
6304 | 6404 | | |
6305 | 6405 | | |
| |||
0 commit comments