{"record":{"id":"d2b7b91a7ac7a591","repo":"googleapis/mcp-toolbox","slug":"invalid-request-d2b7b9","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/v20250618/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/v20250618/method.go#L55-L91","documentation":"The initialize handler unmarshals the request body into InitializeRequest. If the JSON is malformed or does not match the expected shape (missing/invalid protocolVersion, capabilities, or clientInfo fields), the server rejects it with JSON-RPC INVALID_REQUEST and the message 'invalid mcp initialize request'. InitializeRequest embeds ProtocolVersion and ClientInfo which must be present per the MCP spec.","triggerScenarios":"POSTing a body to the initialize endpoint that fails json.Unmarshal against InitializeRequest: non-JSON body, wrong field types (e.g. protocolVersion as number instead of string), or missing required nested objects.","commonSituations":"Hand-rolled clients omitting clientInfo or capabilities; sending the MCP session body as form data instead of JSON; version skew where a client sends an initialize shape from an older spec; JSON with trailing commas or single quotes.","solutions":["Send a valid JSON body shaped like {\"protocolVersion\":\"2025-06-18\",\"capabilities\":{},\"clientInfo\":{\"name\":\"my-client\",\"version\":\"1.0.0\"}}","Validate the body with a JSON linter / JSON schema before calling initialize","Use an official MCP client SDK instead of hand-building the initialize payload","Capture the underlying %w error text in the response — it names the exact unmarshal failure (field and type)"],"exampleFix":"// before\n{\"protocolVersion\":\"2025-06-18\",\"clientInfo\":{\"name\":\"x\",\"version\":\"1\"}}\n// after\n{\"protocolVersion\":\"2025-06-18\",\"capabilities\":{},\"clientInfo\":{\"name\":\"x\",\"version\":\"1\"}}","handlingStrategy":"validation","validationCode":"const body = { protocolVersion: \"2025-06-18\", capabilities: {}, clientInfo: { name: \"my-client\", version: \"1.0.0\" } };\nif (typeof body.protocolVersion !== \"string\" || !body.clientInfo?.name) throw new Error(\"invalid initialize payload\");","typeGuard":"function isInitializeRequest(b: unknown): b is { protocolVersion: string; capabilities: object; clientInfo: { name: string; version: string } } {\n  const r = b as any;\n  return !!r && typeof r.protocolVersion === \"string\" && typeof r.clientInfo === \"object\" && r.clientInfo !== null && typeof r.capabilities === \"object\";\n}","tryCatchPattern":"try { const res = await initialize(body); } catch (e) { if (e.code === -32600) { console.error(\"initialize payload rejected:\", e.message); } else throw e; }","preventionTips":["Validate payloads against the MCP initialize JSON schema before sending","Always include protocolVersion, capabilities, and clientInfo","Test handshake with a known-good client first"],"tags":["mcp","json","validation","initialize"],"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"}