create_rewrite
Create email rewrite rules by specifying local part patterns, destination addresses, and optional domain and order number to redirect incoming messages.
Instructions
Create rewrite rule(s). List of dicts with: name, local_part_rule (pattern), destinations, domain (optional), order_num (optional).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| items | Yes |
Implementation Reference
- migadu_mcp/tools/rewrite_tools.py:33-48 (handler)The MCP tool handler for 'create_rewrite'. Decorated with @migadu_bulk_tool, accepts a RewriteCreateRequest (validated via Pydantic), resolves domain, converts destinations to strings, and delegates to the RewriteService.create_rewrite method. Returns {'rewrite': result, 'success': True}.
@migadu_bulk_tool(mcp, RewriteCreateRequest, entity="rewrite", idempotent=False) async def create_rewrite( item: RewriteCreateRequest, ctx: Context ) -> dict[str, Any]: """Create rewrite rule(s). List of dicts with: name, local_part_rule (pattern), destinations, domain (optional), order_num (optional).""" domain = item.domain or resolve_domain(None) destinations = [str(d) for d in item.destinations] await ctx.info(f"📋 Creating rewrite {item.name} on {domain}") result = ( await get_service_factory() .rewrite_service() .create_rewrite( domain, item.name, item.local_part_rule, destinations, item.order_num ) ) return {"rewrite": result, "success": True} - The service-level implementation of create_rewrite. Constructs request data (name, local_part_rule, destinations as CSV, optional order_num) and POSTs to /domains/{domain}/rewrites via the MigaduClient.
async def create_rewrite( self, domain: str, name: str, local_part_rule: str, destinations: list[str], order_num: int | None = None, ) -> dict[str, Any]: data: dict[str, Any] = { "name": name, "local_part_rule": local_part_rule, "destinations": ",".join(destinations), } if order_num is not None: data["order_num"] = order_num return await self.client.post(f"/domains/{domain}/rewrites", json=data) - migadu_mcp/utils/schemas.py:187-202 (schema)Pydantic schema RewriteCreateRequest used to validate input to the create_rewrite tool. Fields: name, local_part_rule, destinations (coerced from CSV string to list[EmailStr]), domain (optional), order_num (optional).
class RewriteCreateRequest(BaseModel): name: str = Field(..., description="Unique identifier/slug for the rule") local_part_rule: str = Field( ..., description="Pattern to match (e.g., 'demo-*', 'support-*')" ) destinations: list[EmailStr] domain: str | None = None order_num: int | None = None @field_validator("destinations", mode="before") @classmethod def _coerce(cls, v: Union[list[str], str]) -> list[str]: coerced = _coerce_destinations(v) if not coerced: raise ValueError("destinations must be a non-empty list or CSV string") return coerced - migadu_mcp/main.py:17-17 (registration)Import of register_rewrite_tools from rewrite_tools module.
from migadu_mcp.tools.rewrite_tools import register_rewrite_tools - migadu_mcp/main.py:56-56 (registration)Registration call: register_rewrite_tools(mcp) which registers all rewrite tools including create_rewrite.
register_rewrite_tools(mcp)