checkRequest checks the given request against the provided method info, to ensure it is a valid MCP request. If valid, the relevant method info is returned. Otherwise, a non-nil error is returned describing why the request is invalid. This is extracted from request handling so that it can be calle
(req *jsonrpc.Request, infos map[string]methodInfo)
| 184 | // This is extracted from request handling so that it can be called in the |
| 185 | // transport layer to preemptively reject bad requests. |
| 186 | func checkRequest(req *jsonrpc.Request, infos map[string]methodInfo) (methodInfo, error) { |
| 187 | info, ok := infos[req.Method] |
| 188 | if !ok { |
| 189 | return methodInfo{}, fmt.Errorf("%w: %q unsupported", jsonrpc2.ErrNotHandled, req.Method) |
| 190 | } |
| 191 | if info.flags¬ification != 0 && req.IsCall() { |
| 192 | return methodInfo{}, fmt.Errorf("%w: unexpected id for %q", jsonrpc2.ErrInvalidRequest, req.Method) |
| 193 | } |
| 194 | if info.flags¬ification == 0 && !req.IsCall() { |
| 195 | return methodInfo{}, fmt.Errorf("%w: missing id for %q", jsonrpc2.ErrInvalidRequest, req.Method) |
| 196 | } |
| 197 | // missingParamsOK is checked here to catch the common case where "params" is |
| 198 | // missing entirely. |
| 199 | // |
| 200 | // However, it's checked again after unmarshalling to catch the rare but |
| 201 | // possible case where "params" is JSON null (see https://go.dev/issue/33835). |
| 202 | if info.flags&missingParamsOK == 0 && len(req.Params) == 0 { |
| 203 | return methodInfo{}, fmt.Errorf("%w: missing required \"params\"", jsonrpc2.ErrInvalidRequest) |
| 204 | } |
| 205 | return info, nil |
| 206 | } |
| 207 | |
| 208 | // methodInfo is information about sending and receiving a method. |
| 209 | type methodInfo struct { |
no test coverage detected
searching dependent graphs…