{"record":{"id":"e648a81fefc68c0b","repo":"vercel/ai","slug":"server-sent-invalid-initialize-result","errorCode":null,"errorMessage":"Server sent invalid initialize result","messagePattern":"Server sent invalid initialize result","errorType":"exception","errorClass":"MCPClientError","httpStatus":null,"severity":"error","filePath":"packages/mcp/src/tool/mcp-client.ts","lineNumber":568,"sourceCode":"      this.protocolEra = 'legacy';\n      this.protocolVersion = LATEST_LEGACY_PROTOCOL_VERSION;\n      this.setTransportProtocolVersion(this.protocolVersion);\n\n      const result = await this.request({\n        request: {\n          method: 'initialize',\n          params: {\n            protocolVersion: LATEST_LEGACY_PROTOCOL_VERSION,\n            capabilities: this.clientCapabilities,\n            clientInfo: this.clientInfo,\n          },\n        },\n        resultSchema: InitializeResultSchema,\n        options: { signal },\n      });\n\n      if (result === undefined) {\n        throw new MCPClientError({\n          message: 'Server sent invalid initialize result',\n        });\n      }\n\n      this.applyInitializeResult(result);\n\n      // Complete initialization handshake:\n      await this.notification(\n        {\n          method: 'notifications/initialized',\n        },\n        { signal },\n      );\n\n      return this;\n    } catch (error) {\n      try {\n        await waitForAbort(this.transport.close({ signal }), signal);","sourceCodeStart":550,"sourceCodeEnd":586,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/mcp/src/tool/mcp-client.ts#L550-L586","documentation":"Thrown during MCPClient.init when the 'initialize' request resolves with undefined — i.e. the server's response failed to validate against InitializeResultSchema or the request produced no result. This indicates a malformed or non-compliant server response to the MCP handshake.","triggerScenarios":"The server replies to the initialize request with a payload missing required fields (protocolVersion, capabilities, serverInfo) or with invalid shapes, causing schema validation to fail and result to be undefined.","commonSituations":"Connecting to a non-MCP or partially-implemented HTTP/SSE endpoint; a proxy returning HTML error pages; protocol version drift where the server speaks an incompatible response format; server bugs after upgrades.","solutions":["Inspect the raw initialize response from the server (curl the endpoint) to see what is actually returned.","Update the MCP server to a spec-compliant version that returns protocolVersion, capabilities, and serverInfo.","Verify you are pointing at the correct MCP endpoint URL and transport, not an HTML page or wrong route.","Update @ai-sdk/mcp in case the schema expectations changed."],"exampleFix":"// before (server response missing fields)\n{ jsonrpc: '2.0', id: 1, result: { serverInfo: { name: 'x' } } }\n// after (spec-compliant)\n{ jsonrpc: '2.0', id: 1, result: { protocolVersion: '2025-06-18', capabilities: {}, serverInfo: { name: 'x', version: '1.0' } } }","handlingStrategy":"try-catch","validationCode":"const res = await fetch(mcpUrl, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'initialize', params: { protocolVersion: '2025-06-18', capabilities: {}, clientInfo: { name: 'probe', version: '0' } } }) });\nconst body = await res.json();\nif (!body?.result?.protocolVersion || !body?.result?.serverInfo) throw new Error('Endpoint is not a compliant MCP server');","typeGuard":"function isInitializeResult(r: unknown): r is { protocolVersion: string; capabilities: object; serverInfo: { name: string; version: string } } {\n  return !!r && typeof r === 'object' && 'protocolVersion' in r && 'capabilities' in r && 'serverInfo' in r;\n}","tryCatchPattern":"try {\n  const client = await createMCPClient({ transport });\n} catch (e) {\n  if (MCPClientError.isInstance(e) && e.message === 'Server sent invalid initialize result') {\n    // inspect raw server response / verify endpoint URL\n  } else throw e;\n}","preventionTips":["Probe the MCP endpoint with a raw initialize request before wiring the client","Confirm the URL points at the MCP route, not an HTML page","Keep server and client MCP SDK versions aligned"],"tags":["mcp","initialization","handshake","protocol"],"backgroundTag":"invalid-initialize-response","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}