cortex_renew_user_key
Generate a new API key for a user, invalidating the previous key. Requires superadmin privileges.
Instructions
Generate a new API key for a user (invalidates the previous key). Requires superadmin API key.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| userId | Yes | The user login/ID to renew the key for |
Implementation Reference
- src/tools/users.ts:172-222 (handler)Tool handler for 'cortex_renew_user_key'. Checks superadmin availability, calls client.renewUserKey(userId), and returns the new API key in JSON response with a warning to store it securely. Handles errors gracefully.
server.tool( "cortex_renew_user_key", "Generate a new API key for a user (invalidates the previous key). Requires superadmin API key.", { userId: z.string().describe("The user login/ID to renew the key for"), }, async ({ userId }) => { try { if (!client.superadminAvailable) { return { content: [ { type: "text" as const, text: "User key management requires CORTEX_SUPERADMIN_KEY environment variable to be set.", }, ], isError: true, }; } const newKey = await client.renewUserKey(userId); return { content: [ { type: "text" as const, text: JSON.stringify( { userId, apiKey: newKey, message: `New API key generated for user "${userId}". The previous key is now invalid.`, warning: "Store this key securely. It will not be shown again.", }, null, 2, ), }, ], }; } catch (error) { return { content: [ { type: "text" as const, text: `Error renewing user key: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } }, ); - src/tools/users.ts:175-177 (schema)Input schema for cortex_renew_user_key: requires 'userId' (string) described as 'The user login/ID to renew the key for'.
{ userId: z.string().describe("The user login/ID to renew the key for"), }, - src/index.ts:44-44 (registration)Registration of the user tools module (which includes cortex_renew_user_key) via registerUserTools(server, client).
registerUserTools(server, client); - src/client.ts:423-429 (helper)Client helper method renewUserKey(userId) that sends POST to /user/{userId}/key/renew with superadmin auth and returns the plain text response (the new API key).
async renewUserKey(userId: string): Promise<string> { return this.requestText( `/user/${encodeURIComponent(userId)}/key/renew`, { method: "POST" }, true, ); } - src/client.ts:87-127 (helper)Private requestText utility used by renewUserKey to make an HTTP request and return the response body as plain text with superadmin bearer auth.
private async requestText( path: string, options: RequestInit = {}, useSuperadmin = false, ): Promise<string> { const url = `${this.baseUrl}${path}`; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), this.timeout); const authKey = useSuperadmin ? (this.config.superadminKey ?? this.config.apiKey) : this.config.apiKey; const headers: Record<string, string> = { Authorization: `Bearer ${authKey}`, }; if (options.body) { headers["Content-Type"] = "application/json"; } try { const response = await fetch(url, { ...options, headers: { ...headers, ...(options.headers as Record<string, string>) }, signal: controller.signal, }); if (!response.ok) { const body = await response.text().catch(() => ""); throw new Error(`Cortex API error: HTTP ${response.status}${body ? ` - ${body}` : ""}`); } return response.text(); } catch (error) { if (error instanceof Error && error.name === "AbortError") { throw new Error(`Cortex API timeout after ${this.timeout}ms`); } throw error; } finally { clearTimeout(timeoutId); } }