{"record":{"id":"b41b9133ea0ea8a8","repo":"paperclipai/paperclip","slug":"failed-to-create-agent-token","errorCode":null,"errorMessage":"Failed to create agent token","messagePattern":"Failed to create agent token","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli/src/commands/client/connect.ts","lineNumber":132,"sourceCode":"      profile: profileName,\n      persona: \"board\",\n      apiBase,\n      companyId: company?.id ?? null,\n      key: publicKeyResult(key),\n      exports: buildExports({ apiBase, companyId: company?.id, agentId: undefined, envName: apiKeyEnvVarName, token: key.token }),\n    };\n  }\n\n  const company = await chooseCompany(companies, opts.companyId ?? resolvedProfile.profile.companyId, {\n    optional: false,\n  });\n  if (!company) throw new Error(\"Company is required for agent profiles\");\n  const agents = (await boardApi.get<Agent[]>(apiPath`/api/companies/${company.id}/agents`)) ?? [];\n  if (agents.length === 0) throw new Error(`Company '${company.name}' has no agents to connect.`);\n  const agent = await chooseAgent(agents, resolvedProfile.profile.agentId);\n  const tokenName = opts.tokenName?.trim() || `cli-agent-${new Date().toISOString()}`;\n  const key = await boardApi.post<CreatedAgentKey>(apiPath`/api/agents/${agent.id}/keys`, createAgentKeySchema.parse({ name: tokenName }));\n  if (!key) throw new Error(\"Failed to create agent token\");\n  upsertProfile(profileName, {\n    apiBase,\n    companyId: company.id,\n    persona: \"agent\",\n    agentId: agent.id,\n    agentName: agent.name,\n    apiKeyEnvVarName,\n    tokenName: key.name,\n    tokenId: key.id,\n    tokenCreatedAt: key.createdAt,\n  }, opts.context);\n  setCurrentProfile(profileName, opts.context);\n  p.outro(pc.green(`Connected profile '${profileName}' as ${agent.name}.`));\n  return {\n    ok: true,\n    profile: profileName,\n    persona: \"agent\",\n    apiBase,","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/paperclipai/paperclip/blob/67001ec6eb96ae601aa27bc91d9b2415d665334a/cli/src/commands/client/connect.ts#L114-L150","documentation":"Thrown when POST /api/agents/{agent.id}/keys returns a falsy value. The API client's post<T> unwraps to undefined when the server response body is empty or unparseable, so the new key cannot be persisted to the profile.","triggerScenarios":"createAgentKeySchema.parse({name}) is POSTed to /api/agents/{id}/keys and the client resolves to undefined/null.","commonSituations":"Proxy or middleware stripping the response body; server-side error returning 2xx with no body; transient network/auth issue that the client silently swallowed.","solutions":["Check the server logs for the /api/agents/{id}/keys request and confirm a CreatedAgentKey body is returned.","Retry connect; if it persists, hit the endpoint with curl using the board token to inspect the raw response.","Verify apiBase points at a healthy Paperclip API instance."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"const isCreatedAgentKey = (v: unknown): v is CreatedAgentKey =>\n  !!v && typeof v === 'object' && typeof (v as any).token === 'string' && typeof (v as any).id === 'string';","tryCatchPattern":"try {\n  const key = await boardApi.post<CreatedAgentKey>(url, body);\n  if (!isCreatedAgentKey(key)) throw new Error('Failed to create agent token');\n} catch (err) {\n  // Surface server error, then retry once or fall back to manual key creation.\n  console.error('Agent key creation failed:', err instanceof Error ? err.message : err);\n  throw err;\n}","preventionTips":["Confirm the API server is healthy before connect.","Inspect /api/agents/{id}/keys with curl to verify the response shape."],"tags":["cli","connect","api","token"],"backgroundTag":null,"analyzedSha":"67001ec6eb96ae601aa27bc91d9b2415d665334a","analyzedAt":"2026-08-12T12:05:45.408Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}