create_rule
Create automated rules for Facebook ad accounts to adjust ads based on performance conditions and scheduled actions.
Instructions
Create a new automated rule for the ad account.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Rule name | |
| evaluation_spec | Yes | JSON string defining rule conditions | |
| execution_spec | Yes | JSON string defining rule actions | |
| schedule_spec | No | JSON string defining rule schedule |
Implementation Reference
- src/tools/rules.ts:44-61 (handler)The create_rule tool handler. Defined via server.tool('create_rule', ...) with schema (name, evaluation_spec, execution_spec, schedule_spec). Calls client.post to POST to the Meta Ads API endpoint 'adrules_library'.
server.tool( "create_rule", "Create a new automated rule for the ad account.", { name: z.string().describe("Rule name"), evaluation_spec: z.string().describe("JSON string defining rule conditions"), execution_spec: z.string().describe("JSON string defining rule actions"), schedule_spec: z.string().optional().describe("JSON string defining rule schedule"), }, async (params) => { try { const { data, rateLimit } = await client.post(`${client.accountPath}/adrules_library`, { ...params }); return { content: [{ type: "text" as const, text: JSON.stringify({ ...data as object, _rateLimit: rateLimit }, null, 2) }] }; } catch (error) { return { content: [{ type: "text" as const, text: `Failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } ); - src/tools/rules.ts:47-52 (schema)Input schema for create_rule using Zod: name (string), evaluation_spec (string JSON), execution_spec (string JSON), schedule_spec (optional string JSON).
{ name: z.string().describe("Rule name"), evaluation_spec: z.string().describe("JSON string defining rule conditions"), execution_spec: z.string().describe("JSON string defining rule actions"), schedule_spec: z.string().optional().describe("JSON string defining rule schedule"), }, - src/tools/rules.ts:5-101 (registration)Registration function registerRuleTools that registers create_rule (and other rule tools) on the MCP server.
export function registerRuleTools(server: McpServer, client: AdsClient): void { // ─── list_rules ──────────────────────────────────────────── server.tool( "list_rules", "List automated rules for the ad account.", { fields: z.string().optional().describe("Comma-separated fields to return"), limit: z.number().optional().default(25).describe("Number of results to return"), after: z.string().optional().describe("Pagination cursor for next page"), }, async (params) => { try { const { data, rateLimit } = await client.get(`${client.accountPath}/adrules_library`, { ...params }); return { content: [{ type: "text" as const, text: JSON.stringify({ ...data as object, _rateLimit: rateLimit }, null, 2) }] }; } catch (error) { return { content: [{ type: "text" as const, text: `Failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } ); // ─── get_rule ────────────────────────────────────────────── server.tool( "get_rule", "Get details of a specific automated rule.", { rule_id: z.string().describe("Rule ID"), fields: z.string().optional().describe("Comma-separated fields to return"), }, async ({ rule_id, ...params }) => { try { const { data, rateLimit } = await client.get(`/${rule_id}`, { ...params }); return { content: [{ type: "text" as const, text: JSON.stringify({ ...data as object, _rateLimit: rateLimit }, null, 2) }] }; } catch (error) { return { content: [{ type: "text" as const, text: `Failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } ); // ─── create_rule ─────────────────────────────────────────── server.tool( "create_rule", "Create a new automated rule for the ad account.", { name: z.string().describe("Rule name"), evaluation_spec: z.string().describe("JSON string defining rule conditions"), execution_spec: z.string().describe("JSON string defining rule actions"), schedule_spec: z.string().optional().describe("JSON string defining rule schedule"), }, async (params) => { try { const { data, rateLimit } = await client.post(`${client.accountPath}/adrules_library`, { ...params }); return { content: [{ type: "text" as const, text: JSON.stringify({ ...data as object, _rateLimit: rateLimit }, null, 2) }] }; } catch (error) { return { content: [{ type: "text" as const, text: `Failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } ); // ─── update_rule ─────────────────────────────────────────── server.tool( "update_rule", "Update an existing automated rule.", { rule_id: z.string().describe("Rule ID"), name: z.string().optional().describe("New rule name"), evaluation_spec: z.string().optional().describe("JSON string defining updated rule conditions"), execution_spec: z.string().optional().describe("JSON string defining updated rule actions"), schedule_spec: z.string().optional().describe("JSON string defining updated rule schedule"), status: z.string().optional().describe("Rule status: ENABLED, DISABLED"), }, async ({ rule_id, ...params }) => { try { const { data, rateLimit } = await client.post(`/${rule_id}`, { ...params }); return { content: [{ type: "text" as const, text: JSON.stringify({ ...data as object, _rateLimit: rateLimit }, null, 2) }] }; } catch (error) { return { content: [{ type: "text" as const, text: `Failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } ); // ─── delete_rule ─────────────────────────────────────────── server.tool( "delete_rule", "Delete an automated rule.", { rule_id: z.string().describe("Rule ID"), }, async ({ rule_id }) => { try { const { data, rateLimit } = await client.delete(`/${rule_id}`); return { content: [{ type: "text" as const, text: JSON.stringify({ ...data as object, _rateLimit: rateLimit }, null, 2) }] }; } catch (error) { return { content: [{ type: "text" as const, text: `Failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } ); } - src/index.ts:75-75 (registration)Call site in main server where registerRuleTools is invoked with the live server and client.
registerRuleTools(server, client); - src/services/ads-client.ts:187-215 (helper)The AdsClient.post method used by create_rule to POST to the Meta Ads API.
async post( path: string, params?: Record<string, unknown> ): Promise<ClientResponse> { return this.request("POST", path, params); } async delete( path: string, params?: Record<string, unknown> ): Promise<ClientResponse> { return this.request("DELETE", path, params); } // --- Upload (URL-based) --- async upload( path: string, fileUrl: string, params?: Record<string, unknown> ): Promise<ClientResponse> { return this.post(path, { ...params, url: fileUrl }); } // --- Account helpers --- get accountPath(): string { return `/act_${this.accountId}`; }