{"record":{"id":"93a7b1ababf7a74f","repo":"googleapis/mcp-toolbox","slug":"invalid-request-93a7b1","errorCode":"INVALID_REQUEST","errorMessage":"invalid mcp initialize request: %w","messagePattern":"invalid mcp initialize request: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/server/mcp/v20251125/method.go","lineNumber":73,"sourceCode":"\t\treturn promptsGetHandler(ctx, id, g, primitiveMgr, body)\n\tdefault:\n\t\terr := fmt.Errorf(\"invalid method %s\", method)\n\t\treturn jsonrpc.NewError(id, jsonrpc.METHOD_NOT_FOUND, err.Error(), nil), err\n\t}\n}\n\n// InitializeResponse runs capability negotiation and protocol version agreement.\n// This is the Initialization phase of the lifecycle for MCP client-server connections.\n// Always start with the latest protocol version supported.\nfunc initializeHandler(ctx context.Context, id jsonrpc.RequestId, body []byte) (any, error) {\n\tv, err := util.ToolboxVersionFromContext(ctx)\n\tif err != nil {\n\t\treturn jsonrpc.NewError(id, jsonrpc.INTERNAL_ERROR, err.Error(), nil), err\n\t}\n\n\tvar req InitializeRequest\n\tif err := json.Unmarshal(body, &req); err != nil {\n\t\terr = fmt.Errorf(\"invalid mcp initialize request: %w\", err)\n\t\treturn jsonrpc.NewError(id, jsonrpc.INVALID_REQUEST, err.Error(), nil), err\n\t}\n\n\ttoolsListChanged := false\n\tpromptsListChanged := false\n\tresult := InitializeResult{\n\t\tProtocolVersion: PROTOCOL_VERSION,\n\t\tCapabilities: ServerCapabilities{\n\t\t\tTools: &ListChanged{\n\t\t\t\tListChanged: &toolsListChanged,\n\t\t\t},\n\t\t\tPrompts: &ListChanged{\n\t\t\t\tListChanged: &promptsListChanged,\n\t\t\t},\n\t\t},\n\t\tServerInfo: Implementation{\n\t\t\tBaseMetadata: BaseMetadata{\n\t\t\t\tName: SERVER_NAME,","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/server/mcp/v20251125/method.go#L55-L91","documentation":"initializeHandler unmarshals the request body into InitializeRequest; if JSON unmarshaling fails this INVALID_REQUEST error is returned. The body was not a valid JSON object matching the MCP initialize request shape.","triggerScenarios":"tools/initialize requests whose body is malformed JSON, lacks required fields (protocolVersion, capabilities, clientInfo), or has wrong field types (e.g. clientInfo as a string instead of an object).","commonSituations":"Hand-rolled MCP clients sending incomplete initialize payloads; transport corruption or empty bodies; sending query parameters but no JSON body; using a pre-initialization schema from an older spec draft.","solutions":["Validate the request body is complete JSON matching InitializeRequest: protocolVersion, capabilities, clientInfo (name/version).","Inspect the wrapped cause in the error message to identify the exact unmarshal failure (missing field or type mismatch).","Ensure the HTTP request Content-Type is application/json and the body is sent correctly by the transport.","Use an official MCP client/SDK to generate the initialize request rather than hand-crafting it."],"exampleFix":"// before: missing clientInfo\n{\"protocolVersion\":\"2025-11-25\",\"capabilities\":{}}\n// after\n{\"protocolVersion\":\"2025-11-25\",\"capabilities\":{},\"clientInfo\":{\"name\":\"my-client\",\"version\":\"1.0.0\"}}","handlingStrategy":"validation","validationCode":"function isValidInitializeBody(body) {\n  return typeof body === 'object' && body !== null &&\n    typeof body.protocolVersion === 'string' &&\n    typeof body.capabilities === 'object' && body.capabilities !== null &&\n    typeof body.clientInfo === 'object' && body.clientInfo !== null &&\n    typeof body.clientInfo.name === 'string' &&\n    typeof body.clientInfo.version === 'string';\n}","typeGuard":"function isInitializeRequest(v) {\n  return typeof v === 'object' && v !== null && 'protocolVersion' in v && 'capabilities' in v && 'clientInfo' in v;\n}","tryCatchPattern":"try {\n  const res = await rpc.call('initialize', initParams);\n} catch (e) {\n  if (e.code === -32600 && /invalid mcp initialize request/.test(e.message)) {\n    // fix payload shape and retry once\n  } else throw e;\n}","preventionTips":["Use an official MCP SDK to construct initialize payloads.","Include protocolVersion, capabilities, and clientInfo in every initialize call.","Send bodies as application/json; never send empty bodies to the initialize endpoint."],"tags":["mcp","initialize","json-unmarshal","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}