{"record":{"id":"cea7fd0240b84a9c","repo":"googleapis/mcp-toolbox","slug":"invalid-request-cea7fd","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/v20241105/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/v20241105/method.go#L55-L91","documentation":"initializeHandler unmarshals the request body into InitializeRequest; if the body is not valid JSON or lacks the expected shape, it wraps the unmarshal error as \"invalid mcp initialize request: %w\" and returns JSON-RPC INVALID_REQUEST. It fires during the MCP initialization handshake.","triggerScenarios":"POSTing an initialize request whose body is malformed JSON, empty, or missing required fields (protocolVersion/capabilities/clientInfo shape mismatch) to the MCP endpoint.","commonSituations":"Hand-crafted curl tests with quoting/escaping errors; clients sending initialize with wrong field names or nesting; proxies or gateways altering the body; sending the initialize payload to the wrong endpoint or with the wrong content type.","solutions":["Read the wrapped cause in the message to see which JSON field failed to decode.","Ensure the body is valid JSON matching InitializeRequest: {\"jsonrpc\":\"2.0\",\"id\":...,\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"...\",\"capabilities\":{},\"clientInfo\":{\"name\":\"...\",\"version\":\"...\"}}}.","Send with Content-Type: application/json to the correct MCP endpoint path.","Use an official MCP client SDK to construct the initialize request instead of hand-writing it."],"exampleFix":"// before: missing params -> unmarshal failure\n{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\"}\n// after: complete initialize request\n{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2025-11-25\",\"capabilities\":{},\"clientInfo\":{\"name\":\"my-client\",\"version\":\"1.0.0\"}}}","handlingStrategy":"validation","validationCode":"// Validate the initialize payload before sending\nfunction buildInitializeRequest(id, version) {\n  const params = {\n    protocolVersion: version,\n    capabilities: {},\n    clientInfo: { name: \"my-client\", version: \"1.0.0\" }\n  };\n  const body = JSON.stringify({ jsonrpc: \"2.0\", id, method: \"initialize\", params });\n  JSON.parse(body); // throw early on serialization bugs\n  return body;\n}","typeGuard":"function isInitializeRequest(r) {\n  return r.jsonrpc === \"2.0\" && r.method === \"initialize\" &&\n    typeof r.params?.protocolVersion === \"string\" &&\n    typeof r.params?.clientInfo?.name === \"string\";\n}","tryCatchPattern":"if (res.error && res.error.code === -32600 && /invalid mcp initialize request/.test(res.error.message)) {\n  console.error(\"Initialize body rejected:\", res.error.message); // cause is embedded\n}","preventionTips":["Use an official MCP SDK to build the initialize handshake","Always send Content-Type: application/json and a complete params object","Log the exact request body when debugging handshakes so wrapped JSON errors are actionable"],"tags":["mcp","initialize","json","handshake","request-validation"],"backgroundTag":"malformed-json-request","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"}