cloudflare-dns-mcp_create_redirect
Create and configure a single URL redirect (Page Rule) for a specified zone, including source and target URLs, redirect type, query string preservation, priority, and status.
Instructions
Create a single URL redirect (Page Rule) for the given zone
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| preserve_query_string | No | ||
| priority | No | ||
| redirect_type | No | ||
| source_url | Yes | ||
| status | No | active | |
| target_url | Yes | ||
| zone_name | Yes |
Implementation Reference
- src/tools/redirects.ts:54-92 (handler)The async handler function that implements the tool logic: parses input, fetches zone ID, constructs page rule payload, posts to Cloudflare API to create redirect, and returns result.handler: async (params: unknown) => { const { zone_name, source_url, target_url, redirect_type, preserve_query_string, priority, status } = CreateRedirectInputSchema.parse(params); // Lookup 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; // Build pagerule payload const pageruleBody = { targets: [ { target: 'url', constraint: { operator: 'matches', value: source_url }, }, ], actions: [ { id: 'forwarding_url', value: { url: target_url + (preserve_query_string ? '?$1' : ''), status_code: redirect_type, }, }, ], priority: priority ?? 1, status, }; const created = await client.post(`/zones/${zoneId}/pagerules`, pageruleBody); return { content: [ { type: "text", text: JSON.stringify(created, null, 2) } ] }; },
- src/tools/redirects.ts:9-19 (schema)Zod input schema defining parameters for creating a redirect: zone_name, source_url, target_url, redirect_type (301/302), preserve_query_string, priority, status.const CreateRedirectInputSchema = z.object({ zone_name: z.string(), source_url: z.string().min(1), target_url: z.string().min(1), redirect_type: z.number().optional().default(301).refine(v => v === 301 || v === 302, { message: 'redirect_type must be 301 or 302', }), preserve_query_string: z.boolean().optional().default(true), priority: z.number().optional(), status: z.enum(['active', 'disabled']).optional().default('active'), });
- src/tools/redirects.ts:126-130 (registration)Registration of the create_redirect tool within the getRedirectTools function's return object.return { tools: { 'cloudflare-dns-mcp/list_page_rules': listPageRulesTool, 'cloudflare-dns-mcp/create_redirect': createRedirectTool, 'cloudflare-dns-mcp/delete_page_rule': deletePageRuleTool, } };
- src/index.ts:48-52 (registration)Global tool registration in index.ts where tool names are sanitized (slashes replaced with underscores, e.g., 'cloudflare-dns-mcp/create_redirect' becomes 'cloudflare-dns-mcp_create_redirect') and added to toolsMap for the MCP server.const toolsMap: Record<string, any> = {}; for (const tool of Object.values(allTools)) { const safeName = tool.name.replace(/[^a-zA-Z0-9_-]/g, '_'); toolsMap[safeName] = tool; }
- src/index.ts:25-31 (registration)Merging of redirectTools (including create_redirect) into the allTools object in main server setup.const allTools = { ...dnsTools.tools, ...securityTools.tools, ...sslCertTools.tools, ...echoTools.tools, ...redirectTools.tools, ...zoneTools.tools,