create_dns_record
Add a new DNS record to Cloudflare domains by specifying type, name, content, TTL, priority, and proxy settings. Simplifies DNS management for automated setups.
Instructions
Create a new DNS record
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | DNS record content | |
| name | Yes | DNS record name | |
| priority | No | Priority (for MX records) | |
| proxied | No | Whether the record should be proxied through Cloudflare | |
| ttl | No | Time to live (TTL) in seconds (default: 1 for auto) | |
| type | Yes | DNS record type |
Implementation Reference
- src/index.ts:268-311 (handler)MCP tool handler for create_dns_record: handles tool call dispatch, input processing, API configuration check, calls CloudflareApi.createDnsRecord, and returns formatted response.const handleCreateDnsRecord = async (args: { type: string; name: string; content: string; ttl?: number; priority?: number; proxied?: boolean }) => { try { if (!configureApiIfNeeded()) { return { content: [{ type: "text", text: "❌ Configuration incomplete. Please configure Cloudflare API Token and Zone ID first." }], }; } const recordData: any = { type: DnsRecordType.parse(args.type), name: args.name, content: args.content, }; if (args.ttl !== undefined) recordData.ttl = args.ttl; if (args.priority !== undefined) recordData.priority = args.priority; if (args.proxied !== undefined) recordData.proxied = args.proxied; const record = await CloudflareApi.createDnsRecord(recordData); return { content: [{ type: "text", text: `✅ DNS record created successfully! 🔹 Name: ${record.name} 🔹 Type: ${record.type} 🔹 Content: ${record.content} 🔹 ID: ${record.id} ${record.proxied ? '🟠 Proxied through Cloudflare' : ''}` }], }; } catch (error) { return { content: [{ type: "text", text: `❌ Error creating DNS record: ${error instanceof Error ? error.message : 'Unknown error'}` }], }; } };
- src/api.ts:152-166 (helper)Core implementation: validates input with Zod schema, performs POST request to Cloudflare /dns_records endpoint, parses and returns the created DnsRecord.createDnsRecord: async (record: CreateDnsRecord): Promise<DnsRecord> => { const validatedRecord = CreateDnsRecordRequest.parse(record); const response = await api('dns_records', 'POST', validatedRecord); const data = CloudflareApiResponse.parse(await response.json()); if (!data.success) { throw new Error(`API Error: ${data.errors.map(e => e.message).join(', ')}`); } if (!data.result || Array.isArray(data.result)) { throw new Error('Failed to create DNS record'); } return data.result; },
- src/types.ts:50-57 (schema)Zod schema defining input shape and validation for create_dns_record parameters.export const CreateDnsRecordRequest = z.object({ type: DnsRecordType, name: z.string(), content: z.string(), ttl: z.number().optional().default(1), priority: z.number().optional(), proxied: z.boolean().optional(), });
- src/index.ts:81-116 (registration)Tool registration in ListTools response: defines name, description, and JSON inputSchema matching the Zod schema.{ name: "create_dns_record", description: "Create a new DNS record", inputSchema: { type: "object", properties: { type: { type: "string", enum: ["A", "AAAA", "CNAME", "MX", "TXT", "NS", "SRV", "CAA", "PTR"], description: "DNS record type", }, name: { type: "string", description: "DNS record name", }, content: { type: "string", description: "DNS record content", }, ttl: { type: "number", description: "Time to live (TTL) in seconds (default: 1 for auto)", minimum: 1, }, priority: { type: "number", description: "Priority (for MX records)", }, proxied: { type: "boolean", description: "Whether the record should be proxied through Cloudflare", }, }, required: ["type", "name", "content"], }, },
- src/types.ts:4-6 (schema)Zod enum schema for valid DNS record types, used in CreateDnsRecordRequest.export const DnsRecordType = z.enum([ "A", "AAAA", "CNAME", "MX", "TXT", "NS", "SRV", "CAA", "PTR" ]);