Skip to main content
Glama

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

TableJSON Schema
NameRequiredDescriptionDefault
nameYesRule name
evaluation_specYesJSON string defining rule conditions
execution_specYesJSON string defining rule actions
schedule_specNoJSON string defining rule schedule

Implementation Reference

  • 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 };
        }
      }
    );
  • 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"),
    },
  • 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);
  • 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}`;
    }

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv1.2.0

TDQS

B3.2/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden. It only says 'create,' implying a mutating operation, but does not disclose side effects, permission requirements, idempotency, or what happens on success/failure. This is a significant transparency gap for a creation tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single front-loaded sentence with no filler or redundancy. It efficiently communicates the core purpose.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

The tool has 4 parameters with complex behavioral meaning (evaluation, execution, schedule), no annotations, and no output schema. The one-sentence description does not cover prerequisites, return values, the format of the spec JSON strings, or any operational context, leaving the agent under-informed.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100% for all four parameters, so the baseline is 3. The description adds no additional parameter semantics beyond the schema's brief descriptions of the JSON spec fields.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action 'create' and the resource 'automated rule' with scope 'for the ad account,' which distinguishes it from sibling rule tools like list_rules, get_rule, update_rule, and delete_rule. It is specific and not a tautology.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is given on when to use this tool versus alternatives. It lacks context on prerequisites (e.g., needing an ad account ID or existing rule specs) and does not mention exclusions or cases where other rule tools would be more appropriate.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Deploy Server

Other Tools