{"record":{"id":"00e6c4eb76e7f80c","repo":"rohitg00/ai-engineering-from-scratch","slug":"32600","errorCode":"-32600","errorMessage":"Invalid Request","messagePattern":"Invalid Request","errorType":"error_code","errorClass":"RpcProblem","httpStatus":null,"severity":"error","filePath":"phases/13-tools-and-protocols/07-building-an-mcp-server/code/main.ts","lineNumber":160,"sourceCode":"  if (data !== undefined) error.data = data;\n  return { jsonrpc: \"2.0\", id, error };\n}\n\nfunction complete(\n  payload: JsonObject,\n  cache?: { ttlMs: number; cacheScope: \"private\" | \"public\" },\n): JsonObject {\n  return {\n    resultType: \"complete\",\n    ...payload,\n    ...(cache ?? {}),\n    _meta: { [SERVER_INFO_KEY]: { ...SERVER_INFO } },\n  };\n}\n\nfunction validateRequest(message: JsonRpcRequest): void {\n  if (message.jsonrpc !== \"2.0\" || typeof message.method !== \"string\") {\n    throw new RpcProblem(-32600, \"Invalid Request\");\n  }\n  const requestId: unknown = message.id;\n  if (requestId !== undefined && !isValidRequestId(requestId)) {\n    throw new RpcProblem(-32600, \"id must be a string or integer\");\n  }\n  const params = message.params;\n  if (!params || typeof params !== \"object\" || Array.isArray(params)) {\n    throw new RpcProblem(-32602, \"params must be an object\");\n  }\n  const meta = params._meta;\n  if (!meta || typeof meta !== \"object\" || Array.isArray(meta)) {\n    throw new RpcProblem(-32602, \"params._meta is required\");\n  }\n  const requested = meta[VERSION_KEY];\n  if (typeof requested !== \"string\") {\n    throw new RpcProblem(-32602, `${VERSION_KEY} is required`);\n  }\n  if (!SUPPORTED_VERSIONS.includes(requested)) {","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/13-tools-and-protocols/07-building-an-mcp-server/code/main.ts#L142-L178","documentation":"The server rejects an incoming JSON-RPC message whose envelope is not a valid 2.0 request: jsonrpc must be the exact string '2.0' and method must be a string. This is the JSON-RPC 2.0 spec's Invalid Request code (-32600), thrown by validateRequest before any handler runs.","triggerScenarios":"dispatch() receives a message with jsonrpc:'1.0' or missing, a numeric method like method:42, or a notification-shaped payload with a non-string method field.","commonSituations":"Hand-rolled stdio clients forgetting the jsonrpc:'2.0' field, JSON pipelines that mangle keys, or tests reusing JSON-RPC 1.0 style payloads ({id, method, params} without the version field).","solutions":["Set jsonrpc:'2.0' and a string method on every request sent to dispatch","Log the raw message before dispatch to see exactly which field is wrong","If writing a client, centralize envelope construction in one helper so the field can never be missed"],"exampleFix":"// before\ndispatch({ id: 1, method: 'initialize', params: { _meta: {} } });\n// after\ndispatch({ jsonrpc: '2.0', id: 1, method: 'initialize', params: { _meta: {} } });","handlingStrategy":"validation","validationCode":"function isValidEnvelope(m: unknown): boolean { \n  return typeof m === 'object' && m !== null \n    && (m as any).jsonrpc === '2.0' \n    && typeof (m as any).method === 'string'; \n} \nif (!isValidEnvelope(msg)) throw new Error('bad envelope');","typeGuard":"function isJsonRpcRequest(m: unknown): m is { jsonrpc: '2.0'; method: string } { \n  return typeof m === 'object' && m !== null && (m as Record<string, unknown>).jsonrpc === '2.0' \n    && typeof (m as Record<string, unknown>).method === 'string'; \n}","tryCatchPattern":"try { dispatch(msg); } catch (e) { if (e instanceof RpcProblem && e.code === -32600) fixEnvelopeAndRetry(); else throw e; }","preventionTips":["Build every request through one typed helper that hardcodes jsonrpc:'2.0'","Assert the envelope in unit tests of the client"],"tags":["json-rpc","typescript","mcp","validation"],"backgroundTag":"invalid-json-rpc-request","analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}