create_or_update_credential
Create or update a credential for a consumer in the APISIX-MCP server, specifying username, credential ID, and configuration details.
Instructions
Create or update a credential for a consumer
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| credential | Yes | credential configuration object | |
| id | Yes | credential id | |
| username | Yes | consumer username |
Implementation Reference
- src/tools/consumer.ts:22-24 (registration)Registers the 'create_or_update_credential' tool with its inline handler function that performs a PUT request to create or update a consumer credential via the admin API.server.tool("create_or_update_credential", "Create or update a credential for a consumer", CreateCredentialSchema.shape, async (args) => { return await makeAdminAPIRequest(`/consumers/${args.username}/credentials/${args.id}`, "PUT", args); });
- src/tools/consumer.ts:22-24 (handler)The inline handler function for the tool that executes the logic: makes a PUT request to the APISIX admin API endpoint for the consumer's credential.server.tool("create_or_update_credential", "Create or update a credential for a consumer", CreateCredentialSchema.shape, async (args) => { return await makeAdminAPIRequest(`/consumers/${args.username}/credentials/${args.id}`, "PUT", args); });
- src/schemas/consumer.ts:44-48 (schema)Zod schema defining the input parameters for the create_or_update_credential tool: username, id, and credential object.export const CreateCredentialSchema = z.object({ username: ConsumerSchema.shape.username, id: z.string().describe("credential id"), credential: CredentialSchema, });
- src/utils/adminAPI.ts:11-80 (helper)Shared helper function called by the tool handler to perform HTTP requests to the APISIX admin API, handling success and error responses in MCP format.export async function makeAdminAPIRequest( path: string, method: string = "GET", data?: object ): Promise<CallToolResult> { const baseUrl = `${APISIX_SERVER_HOST}:${APISIX_ADMIN_API_PORT}${APISIX_ADMIN_API_PREFIX}`; const url = `${baseUrl}${path}`; try { const response = await axios({ method, url, data, headers: { "X-API-KEY": APISIX_ADMIN_KEY, "Content-Type": "application/json", }, }); return { content: [ { type: "text", text: JSON.stringify(response.data, null, 2), }, ], }; } catch (error) { if (axios.isAxiosError(error)) { console.error(`Request failed: ${method} ${url}`); console.error( `Status: ${error.response?.status}, Error: ${error.message}` ); if (error.response?.data) { try { const stringifiedData = JSON.stringify(error.response.data); console.error(`Response data: ${stringifiedData}`); } catch { console.error(`Response data: [Cannot parse as JSON]`); } } return { isError: true, content: [ { type: "text", text: JSON.stringify( `Status: ${error.response?.status}\nMessage: ${error.message} Data:\n${JSON.stringify(error.response?.data || {}, null, 2)}`, null, 2 ), }, ], }; } else { return { isError: true, content: [ { type: "text", text: JSON.stringify(error, null, 2), }, ], }; } } }