{"record":{"id":"0dfc15430bcc23c8","repo":"different-ai/openwork","slug":"create-mcp-connection-response-was-incomplete","errorCode":null,"errorMessage":"Create MCP connection response was incomplete.","messagePattern":"Create MCP connection response was incomplete\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ee/apps/den-web/app/(den)/dashboard/_components/mcp-connections-data.tsx","lineNumber":737,"sourceCode":"export function useCreateMcpConnection() {\n  const queryClient = useQueryClient();\n  const { orgId, runReauthableAction } = useOrgDashboard();\n\n  return useMutation({\n    mutationFn: async (input: CreateMcpConnectionInput): Promise<CreatedMcpConnection> => {\n      let created: CreatedMcpConnection | null = null;\n      await runReauthableAction(\"create-mcp-connection\", async () => {\n        const { response, payload } = await requestJson(\n          \"/v1/mcp-connections\",\n          { method: \"POST\", headers: getOrgScopeHeaders(requireOrgId(orgId)), body: JSON.stringify(input) },\n          20000,\n        );\n        if (!response.ok) {\n          throw getRequestError(payload, response, `Failed to add MCP connection (${response.status}).`);\n        }\n        created = payload as CreatedMcpConnection;\n      });\n      if (!created) throw new Error(\"Create MCP connection response was incomplete.\");\n      return created;\n    },\n    onSuccess: () => {\n      queryClient.invalidateQueries({ queryKey: mcpConnectionQueryKeys.all });\n    },\n  });\n}\n\nexport function useCreateNativeProviderConnection() {\n  const queryClient = useQueryClient();\n  const { orgId, runReauthableAction } = useOrgDashboard();\n\n  return useMutation({\n    mutationFn: async (input: CreateNativeProviderConnectionInput): Promise<CreatedMcpConnection> => {\n      let created: CreatedMcpConnection | null = null;\n      await runReauthableAction(\"create-mcp-connection\", async () => {\n        const { response, payload } = await requestJson(\n          \"/v1/mcp-connections\",","sourceCodeStart":719,"sourceCodeEnd":755,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/ee/apps/den-web/app/(den)/dashboard/_components/mcp-connections-data.tsx#L719-L755","documentation":"The add-MCP-connection mutation POSTs connection details and expects the created connection back; the body is cast to CreatedMcpConnection and if it is falsy after the request completes, this error is thrown. Because the value is assigned inside runReauthableAction's callback and only checked afterwards, the error also fires when the callback path never assigned a body (e.g. reauth interrupted the flow) — the client got no usable created-connection object.","triggerScenarios":"POST /connections returns ok with empty body; payload parsed to null/undefined; runReauthableAction completed a re-auth path without executing the request callback that assigns 'created'; response body missing required connection fields consumed downstream.","commonSituations":"Server returning 201 with empty body; TypeScript 'as CreatedMcpConnection' cast masking a malformed body that later fails on required fields; re-auth flow (token refresh) interleaving so the assignment never happens; dashboard/server version drift on the connection schema.","solutions":["Inspect the POST response body and confirm it is a full connection object","Validate the body with a real guard instead of the `payload as CreatedMcpConnection` cast so malformed bodies surface early","Check runReauthableAction: ensure that after a re-auth the request is retried and 'created' gets assigned","Fix the server route to return the created connection in the 2xx body"],"exampleFix":"// before\ncreated = payload as CreatedMcpConnection;\n// after\ncreated = isCreatedMcpConnection(payload) ? payload : null;\n// with\nfunction isCreatedMcpConnection(v: unknown): v is CreatedMcpConnection {\n  return isRecord(v) && typeof v.id === \"string\" && typeof v.name === \"string\";\n}","handlingStrategy":"type-guard","validationCode":"function isCreatedConnection(v: unknown): boolean {\n  return isRecord(v) && typeof v.id === \"string\" && typeof v.name === \"string\";\n}\n// call before returning from the mutation","typeGuard":"function isCreatedMcpConnection(v: unknown): v is CreatedMcpConnection {\n  return isRecord(v) && typeof v.id === \"string\" && typeof v.name === \"string\" && typeof v.url === \"string\";\n}","tryCatchPattern":"try {\n  const conn = await addMcpConnection(form);\n} catch (err) {\n  if (err.message === \"Create MCP connection response was incomplete.\") {\n    // refetch connection list to check whether it was actually created server-side\n    await queryClient.invalidateQueries(mcpConnectionQueryKeys.all);\n  }\n}","preventionTips":["Never use `as` casts on response bodies; validate with a type guard","Ensure re-auth wrappers retry-and-assign rather than resolving without the request result","Return the created resource in 2xx bodies, never an empty body"],"tags":["api","response-shape","mcp","react-query"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}