Skip to main content
Glama
jeffgolden

Cloudflare MCP Server

by jeffgolden

cloudflare-dns-mcp_create_dns_record

Automate DNS record creation in a specified Cloudflare zone by defining parameters like record type, name, content, TTL, and optional proxy settings.

Instructions

Create a new DNS record in a given zone

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYes
nameYes
portNo
priorityNo
proxiedNo
targetNo
ttlNo
typeYes
weightNo
zone_nameYes

Implementation Reference

  • Handler function that implements the logic to create a new DNS record in a Cloudflare zone. Finds the zone, validates inputs, handles special record types (TXT, CAA, MX, SRV), calls the Cloudflare POST API, and returns the result in MCP format.
    handler: async (params: z.infer<typeof CreateDnsRecordInputSchema>) => {
      const { zone_name, ...rest } = CreateDnsRecordInputSchema.parse(params);
      // Find zone id
      const zones = await client.get<Array<{ id: string; name: string }>>('/zones', { name: zone_name });
      if (zones.length === 0) throw new Error(`Zone ${zone_name} not found`);
      const zoneId = zones[0].id;
    
      const quotedContent = rest.type === 'TXT' ? ensureTxtQuotes(rest.content) : rest.content;
      // validate edge-cases
      if (rest.type === 'MX' && rest.priority === undefined) {
        throw new Error('MX record requires "priority"');
      }
      if (rest.type === 'SRV') {
        const required = ['priority', 'weight', 'port', 'target'];
        for (const f of required) {
          if ((rest as any)[f] === undefined) throw new Error(`SRV record requires "${f}"`);
        }
      }
    
      let body: any;
      if (rest.type === 'CAA') {
        const caaData = parseCAAContent(quotedContent);
        body = {
          type: rest.type,
          name: rest.name,
          data: caaData,
          ttl: rest.ttl,
        };
      } else {
        body = {
          type: rest.type,
          name: rest.name,
          content: quotedContent,
          ttl: rest.ttl,
          priority: rest.priority,
          proxied: rest.proxied,
          ...(rest.weight !== undefined && { weight: rest.weight }),
          ...(rest.port !== undefined && { port: rest.port }),
          ...(rest.target !== undefined && { target: rest.target }),
        };
      }
      const record = await client.post<typeof DNSRecordSchema['_type']>(`/zones/${zoneId}/dns_records`, body);
      return {
        content: [
          {
            type: "text",
            text: JSON.stringify({ ...record, zone_name }, null, 2)
          }
        ]
      };
    },
  • Zod input schema for the create_dns_record tool defining parameters like zone_name, type, name, content, ttl, etc.
    const CreateDnsRecordInputSchema = z.object({
      zone_name: z.string(),
      type: z.string(),
      name: z.string(),
      content: z.string(),
      ttl: z.number().optional().default(1),
      priority: z.number().optional(),
      weight: z.number().optional(),
      port: z.number().optional(),
      target: z.string().optional(),
      proxied: z.boolean().optional().default(false),
    });
  • Local registration of the create_dns_record tool in the getDnsTools() function's return object.
    'cloudflare-dns-mcp/create_dns_record': createDnsRecordTool,
  • src/index.ts:5-26 (registration)
    Global registration: imports getDnsTools, calls it to get tools including create_dns_record, and spreads into allTools. Note: tool names are later sanitized by replacing '/' with '_'.
    import { getDnsTools } from './tools/dns-records.js';
    import { getSecurityTools } from './tools/security.js';
    import { getSslCertTools } from './tools/ssl-certs.js';
    import { getZoneManagementTools } from './tools/zone-management.js';
    import { getEchoTools } from './tools/echo.js';
    import { getRedirectTools } from './tools/redirects.js';
    import { CloudflareClient } from './cloudflare-client.js';
    
    dotenv.config();
    
    async function main() {
      const cfClient = new CloudflareClient();
    
      const dnsTools = getDnsTools(cfClient);
      const securityTools = getSecurityTools(cfClient);
      const sslCertTools = getSslCertTools(cfClient);
      const zoneTools = getZoneManagementTools(cfClient);
      const echoTools = getEchoTools();
      const redirectTools = getRedirectTools(cfClient);
    
      const allTools = {
        ...dnsTools.tools,
  • Helper functions ensureTxtQuotes (for TXT records) and parseCAAContent (for CAA records) used in the handler.
    // Ensure TXT record value is wrapped in double quotes; Cloudflare permits full string.
      const ensureTxtQuotes = (val: string) => (val.startsWith('"') ? val : `"${val}"`);
    
      // Parse a CAA record string of the form "<flags> <tag> <value>" into the object Cloudflare expects.
      const parseCAAContent = (content: string) => {
        const match = content.match(/^(\d+)\s+(issue|issuewild|iodef)\s+(.+)$/);
        if (!match) {
          throw new Error('CAA content must be in the format: "flags tag value" (e.g., "0 issue cloudflare.com")');
        }
        return {
          flags: Number(match[1]),
          tag: match[2],
          value: match[3].replace(/^"(.*)"$/, '$1'), // strip surrounding quotes if user provided them
        } as const;
      };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jeffgolden/cloudflare_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server