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
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | ||
| name | Yes | ||
| port | No | ||
| priority | No | ||
| proxied | No | ||
| target | No | ||
| ttl | No | ||
| type | Yes | ||
| weight | No | ||
| zone_name | Yes |
Implementation Reference
- src/tools/dns-records.ts:275-325 (handler)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) } ] }; },
- src/tools/dns-records.ts:257-268 (schema)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), });
- src/tools/dns-records.ts:333-333 (registration)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,
- src/tools/dns-records.ts:159-173 (helper)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; };