MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / checkRequest

Function checkRequest

mcp/shared.go:186–206  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

184// This is extracted from request handling so that it can be called in the
185// transport layer to preemptively reject bad requests.
186func 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&notification != 0 && req.IsCall() {
192 return methodInfo{}, fmt.Errorf("%w: unexpected id for %q", jsonrpc2.ErrInvalidRequest, req.Method)
193 }
194 if info.flags&notification == 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.
209type methodInfo struct {

Callers 3

servePOSTMethod · 0.85
handleReceiveFunction · 0.85
ServeHTTPMethod · 0.85

Calls 1

IsCallMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…