Repository navigation
Commit 5f82b63
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 54885b2 commit 5f82b63
4 files changed
Lines changed: 72 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
188 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
189 | 191 | | |
190 | 192 | | |
191 | 193 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
288 | 288 | | |
289 | 289 | | |
290 | 290 | | |
291 | | - | |
| 291 | + | |
292 | 292 | | |
293 | 293 | | |
294 | 294 | | |
| |||
303 | 303 | | |
304 | 304 | | |
305 | 305 | | |
306 | | - | |
| 306 | + | |
307 | 307 | | |
308 | 308 | | |
309 | 309 | | |
| |||
| 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 | |
|---|---|---|---|
| |||
1960 | 1960 | | |
1961 | 1961 | | |
1962 | 1962 | | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| 2001 | + | |
| 2002 | + | |
| 2003 | + | |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
| 2007 | + | |
| 2008 | + | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
| 2013 | + | |
| 2014 | + | |
1963 | 2015 | | |
1964 | 2016 | | |
1965 | 2017 | | |
| |||
0 commit comments