cd /news/ai-agents/temporal-ai-durable-mcp-gateway-with… · home › topics › ai-agents › article
[ARTICLE · art-175570] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

Temporal & AI: Durable MCP Gateway with Code Mode

Maple Xu of Temporal's AI Foundations team built an MCP gateway on Temporal that cuts agent token use by running tool calls through a single run_script tool in code mode, and makes long tool-call jobs durable. In a demo, scanning 30 Notion pages cost 142K tokens with plain tool calls versus 68K in code mode, and an interrupted 300-page scan resumed from Temporal's Event History instead of restarting. The repo cautions the gateway is not yet safe for write tools because a tool call can execute more than once.

recommendation

Route agent tool calls through a code-mode script that filters results before they hit context, and make each call a durable step so interrupted jobs resume; avoid write tools until calls are idempotent.

more in playbook: Designing agent systems
by read1 min views1 publishedNov 24, 2026

When an agent calls MCP tools directly, every result lands in its context window. Ask it which of 30 Notion pages mention AI, and it reads all 30 pages into context to answer a yes or no question about each one. And when a long job of hundreds of tool calls dies partway through, from a rate limit, a dropped connection or a restarted MCP server, the agent starts over and pays for every call again.

In this episode of Temporal & AI, Maple Xu from Temporal's AI Foundations team builds an MCP gateway on Temporal that fixes both.

Code mode: the agent gets one tool, run_script, and writes a short Python script against the upstream tools. The script fetches, filters and returns only what the agent needs, so the page bodies never reach the model. In the video, 30 pages took 142K tokens with plain tool calls and 68K in code mode.

Durable steps: every tool call inside the script is a durable step. A failed call retries with backoff, up to 8 attempts.

Same scan, two outcomes: Maple interrupts a 300 page scan partway through. Without Temporal, every fetch is lost and the agent runs the script again from the start. With the gateway, the Workflow s, finished fetches stay in Event History, and the agent is still on the same tool call and doesn't notice.

One caveat from the repo: it isn't safe for write tools as is, since a tool call can run more than once.

🎥 Watch the episode: [https://youtu.be/emAjjoT_liY](https://youtu.be/emAjjoT_liY)

🧪 GitHub repo, run it yourself: [https://github.com/temporal-community/temporal-ai-mcp-gateway](https://github.com/temporal-community/temporal-ai-mcp-gateway)
── more in #ai-agents 4 stories · sorted by recency
── more on @temporal 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/temporal-ai-durable-…] indexed:0 read:1min 2026-11-24 · —