Repository navigation
Commit 339a85d
committed
Accept any string as a request id
## Motivation and Context
`MCP::Server#handle` and `#handle_json` leave `JsonRpcHandler`'s default id check in place, which accepts
only string ids made of letters, digits, `_` and `-`, so a request whose id is `req.1`, `call:42` or a URN
is refused with `-32600`, and the error carries `"id": null` although the id was readable (issue #603).
The check came with the internalized `json_rpc_handler` gem, where it keeps markup out of ids that a server
might reflect into an HTML page; this server answers with JSON or SSE bodies that `to_json` escapes, so
it protects nothing here, while the MCP specification allows any string or integer id, as the TypeScript
and Python servers do.
The server now hands `JsonRpcHandler` no id pattern, so any string is accepted and echoed as sent, and an id
that is neither a string nor an integer, null aside, is still refused with `-32600`. `JsonRpcHandler` keeps
its default for other callers. Without the pattern a string that is not valid UTF-8 would pass the check and
the method would run, for an answer that cannot be encoded; `valid_id?` now refuses such a string whether or
not a pattern is given, where the pattern match raised against it before.
Fixes #603.
## How Has This Been Tested?
New tests in `test/mcp/server_test.rb`: ids with a dot, a colon, a URN, spaces, markup and the empty string
are answered under the same id, an error response echoes such an id, an id with a newline stays on one line
of the JSON, a fractional number, the booleans, an object and an array are still refused with a null id, and
an id that is not valid UTF-8 is refused without running the tool; `test/json_rpc_handler_test.rb` checks
the last with the default pattern. Against the previous library the first two fail with `-32600`
and the invalid UTF-8 id raises from the pattern match.
## Breaking Changes
None. Requests that earlier releases refused are now served.1 parent 0019b4d commit 339a85d
4 files changed
Lines changed: 72 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
176 | 176 | | |
177 | 177 | | |
178 | 178 | | |
179 | | - | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
180 | 182 | | |
181 | 183 | | |
182 | 184 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
285 | 285 | | |
286 | 286 | | |
287 | 287 | | |
288 | | - | |
| 288 | + | |
289 | 289 | | |
290 | 290 | | |
291 | 291 | | |
| |||
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
303 | | - | |
| 303 | + | |
304 | 304 | | |
305 | 305 | | |
306 | 306 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
137 | 152 | | |
138 | 153 | | |
139 | 154 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1915 | 1915 | | |
1916 | 1916 | | |
1917 | 1917 | | |
| 1918 | + | |
| 1919 | + | |
| 1920 | + | |
| 1921 | + | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
| 1926 | + | |
| 1927 | + | |
| 1928 | + | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
| 1938 | + | |
| 1939 | + | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
1918 | 1970 | | |
1919 | 1971 | | |
1920 | 1972 | | |
| |||
0 commit comments