[{"data":1,"prerenderedAt":1430},["ShallowReactive",2],{"series-\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Freplacing-local-eval-with-isolated-cloudflare-workers":3,"series-context-\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers":1240},{"id":4,"title":5,"body":6,"contentId":1225,"date":1226,"description":1227,"extension":1228,"github":1229,"meetTheInstructor":1231,"meta":1232,"navigation":459,"part":219,"path":1233,"rawbody":1234,"robots":1231,"schemaOrg":1231,"seo":1235,"seriesSlug":1236,"sitemap":1237,"stem":1238,"subtitle":1231,"topics":1231,"__hash__":1239},"series\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Freplacing-local-eval-with-isolated-cloudflare-workers.md","Replacing the Evaluator with Isolated Cloudflare Workers",{"type":7,"value":8,"toc":1215},"minimark",[9,18,21,26,29,32,35,79,85,92,96,99,102,105,108,112,115,118,121,124,127,138,145,149,152,155,159,162,165,298,301,315,685,688,742,755,759,762,867,887,1067,1070,1074,1081,1084,1087,1190,1193,1197,1200,1208,1211],[10,11,12,13,17],"p",{},"In the previous parts, we implemented search and execute tools that used a trusted local evaluator based on the ",[14,15,16],"code",{},"Function"," constructor to run AI-generated code. That approach worked for development but raised serious security concerns.",[10,19,20],{},"In this part, we will replace that local evaluator with an isolated environment using Cloudflare Dynamic Workers. Isolation and explicit capabilities let us constrain what generated code can do instead of granting it the host's permissions.",[22,23,25],"h2",{"id":24},"user-input-cant-be-trusted","User input can't be trusted",[10,27,28],{},"One of the first things we learn when building websites with forms is that we cannot trust user input. It is not only about malicious users, but also mistakes and unexpected input.",[10,30,31],{},"Generated code is user input too. A model can make mistakes, and a malicious user can try to steer the model towards an unsafe action.",[10,33,34],{},"Imagine that, instead of writing a function to search the OpenAPI specification, the model wrote the following code:",[36,37,43],"pre",{"className":38,"code":39,"filename":40,"language":41,"meta":42,"style":42},"language-typescript shiki shiki-themes github-light github-light github-dark","async () => {\n  return process.env\n}\n","Example","typescript","",[14,44,45,64,73],{"__ignoreMap":42},[46,47,50,54,58,61],"span",{"class":48,"line":49},"line",1,[46,51,53],{"class":52},"s8jYJ","async",[46,55,57],{"class":56},"sxrX7"," () ",[46,59,60],{"class":52},"=>",[46,62,63],{"class":56}," {\n",[46,65,67,70],{"class":48,"line":66},2,[46,68,69],{"class":52},"  return",[46,71,72],{"class":56}," process.env\n",[46,74,76],{"class":48,"line":75},3,[46,77,78],{"class":56},"}\n",[10,80,81,82,84],{},"When generated code runs in the host process, it can access any global or capability the host exposes. Depending on the environment, that may include environment variables, filesystem access, process APIs, or unrestricted network access. The ",[14,83,16],{}," constructor is an evaluator. It was never designed to run untrusted code.",[10,86,87,88],{},"So we need a sandbox. ",[89,90,91],"em",{},"Never trust the user, or the generated code.",[22,93,95],{"id":94},"sandboxes-are-slow-and-expensive","Sandboxes are slow and expensive",[10,97,98],{},"A sandbox is an isolated environment where untrusted code cannot affect the rest of the system. In our case, it is the right way to run generated code.",[10,100,101],{},"Traditional sandboxes often use virtual machines or containers. Their startup time, memory footprint, and price vary by provider and configuration, but a full operating-system environment is much more than this task needs.",[10,103,104],{},"There is another problem. Many agents need environments in which to run code. If every short-lived tool execution starts a full operating-system environment, capacity and cost can add up quickly.",[10,106,107],{},"We need something lighter, faster, and more scalable. We are running small JavaScript functions. We need a constrained JavaScript runtime.",[22,109,111],{"id":110},"isolate-the-memory","Isolate the memory",[10,113,114],{},"Let us step back. Is there an existing solution that runs untrusted code in an isolated environment?",[10,116,117],{},"Browsers!",[10,119,120],{},"Browsers routinely run code from many different websites on the same machine. To keep that code separate, V8 uses isolates: independent JavaScript heaps that can share a process without sharing memory.",[10,122,123],{},"An isolate is much lighter than a virtual machine or a container. It gives each execution its own memory space, so code running in one isolate cannot directly inspect or modify another execution's objects.",[10,125,126],{},"Memory isolation alone is not enough, though. Untrusted code must also be given only the capabilities it needs: no filesystem access, no arbitrary secrets, and no unrestricted network access.",[10,128,129,130,137],{},"This is where Cloudflare Workers are particularly useful. ",[131,132,136],"a",{"href":133,"rel":134},"https:\u002F\u002Fblog.cloudflare.com\u002Fintroducing-cloudflare-workers\u002F#:~:text=We%20run%20JavaScript,top%20of%20this.",[135],"nofollow","They build on V8 isolates"," and a capability-based runtime: a Worker only receives the bindings and outbound access that we explicitly grant it.",[10,139,140,141,144],{},"Node.js also uses V8, but Node itself is not a secure sandbox. The ",[14,142,143],{},"node:vm"," module documentation explicitly says it should not be used to run untrusted code. For this use case, we need a runtime designed to enforce those boundaries.",[22,146,148],{"id":147},"dynamic-workers-to-the-rescue","Dynamic Workers to the rescue",[10,150,151],{},"Cloudflare Workers provide this kind of isolated runtime. With Dynamic Workers, we do not deploy a new Worker through an HTTP endpoint for each execution.",[10,153,154],{},"Instead, the parent Worker uses the Dynamic Worker Loader binding to create a fresh Worker at runtime, wait for its RPC response, and return that response to the MCP client. Cloudflare describes fresh isolates as starting in a few milliseconds and using a few megabytes of memory, which makes them suitable for short-lived code execution.",[22,156,158],{"id":157},"from-local-eval-to-dynamic-workers","From local eval to Dynamic Workers",[10,160,161],{},"So now we have the concept, let's implement it in our code.",[10,163,164],{},"Currently, we have:",[36,166,168],{"className":38,"code":167,"filename":40,"language":41,"meta":42,"style":42},"\u002F* eslint-disable no-new-func *\u002F\nlet searchFunction: unknown\ntry {\n  searchFunction = new Function(\n    'spec',\n    `\"use strict\"; return (${trimmedCode});`\n  )(searchSpec)\n}\ncatch (error) {\n  throw new Error(`Could not compile search code: ${errorMessage(error)}`)\n}\n",[14,169,170,176,191,198,217,227,239,245,250,259,293],{"__ignoreMap":42},[46,171,172],{"class":48,"line":49},[46,173,175],{"class":174},"sCsY4","\u002F* eslint-disable no-new-func *\u002F\n",[46,177,178,181,184,187],{"class":48,"line":66},[46,179,180],{"class":52},"let",[46,182,183],{"class":56}," searchFunction",[46,185,186],{"class":52},":",[46,188,190],{"class":189},"sBjJW"," unknown\n",[46,192,193,196],{"class":48,"line":75},[46,194,195],{"class":52},"try",[46,197,63],{"class":56},[46,199,201,204,207,210,214],{"class":48,"line":200},4,[46,202,203],{"class":56},"  searchFunction ",[46,205,206],{"class":52},"=",[46,208,209],{"class":52}," new",[46,211,213],{"class":212},"snPdu"," Function",[46,215,216],{"class":56},"(\n",[46,218,220,224],{"class":48,"line":219},5,[46,221,223],{"class":222},"sIIMD","    'spec'",[46,225,226],{"class":56},",\n",[46,228,230,233,236],{"class":48,"line":229},6,[46,231,232],{"class":222},"    `\"use strict\"; return (${",[46,234,235],{"class":56},"trimmedCode",[46,237,238],{"class":222},"});`\n",[46,240,242],{"class":48,"line":241},7,[46,243,244],{"class":56},"  )(searchSpec)\n",[46,246,248],{"class":48,"line":247},8,[46,249,78],{"class":56},[46,251,253,256],{"class":48,"line":252},9,[46,254,255],{"class":52},"catch",[46,257,258],{"class":56}," (error) {\n",[46,260,262,265,267,270,273,276,279,281,284,287,290],{"class":48,"line":261},10,[46,263,264],{"class":52},"  throw",[46,266,209],{"class":52},[46,268,269],{"class":212}," Error",[46,271,272],{"class":56},"(",[46,274,275],{"class":222},"`Could not compile search code: ${",[46,277,278],{"class":212},"errorMessage",[46,280,272],{"class":222},[46,282,283],{"class":56},"error",[46,285,286],{"class":222},")",[46,288,289],{"class":222},"}`",[46,291,292],{"class":56},")\n",[46,294,296],{"class":48,"line":295},11,[46,297,78],{"class":56},[10,299,300],{},"We will replace it with a call to a new worker that we create on the fly.",[302,303,306],"callout",{"color":304,"icon":305},"info","i-ph-info-duotone",[10,307,308,309,314],{},"This series assumes you are already familiar with ",[131,310,313],{"href":311,"rel":312},"https:\u002F\u002Fdevelopers.cloudflare.com\u002Fworkers\u002Fruntime-apis\u002Fbindings\u002F",[135],"Cloudflare Bindings",".",[36,316,318],{"className":38,"code":317,"filename":40,"language":41,"meta":42,"style":42},"async function runSearch(code: string): Promise\u003Cunknown> {\n  const worker = env.LOADER.get(`markethub-search-${crypto.randomUUID()}`, () => ({\n    compatibilityDate: '2026-07-11',\n    globalOutbound: null,\n    mainModule: 'worker.js',\n    modules: {\n      'worker.js': `\nimport { WorkerEntrypoint } from \"cloudflare:workers\";\n\nconst spec = ${JSON.stringify(searchSpec)};\n\nexport default class SearchExecutor extends WorkerEntrypoint {\n  async evaluate() {\n    try {\n      const result = await (${code})();\n      return { result, err: undefined };\n    } catch (error) {\n      return {\n        result: undefined,\n        err: error instanceof Error ? error.message : String(error)\n      };\n    }\n  }\n}\n      `\n    }\n  }))\n\n  const entrypoint = worker.getEntrypoint() as unknown as SearchExecutorEntrypoint\n  const response = await entrypoint.evaluate()\n\n  if (response.err)\n    throw new Error(response.err)\n  return response.result\n}\n",[14,319,320,356,404,414,424,434,439,450,455,461,484,488,494,500,506,517,523,529,535,541,547,553,559,565,570,576,581,587,592,623,645,650,659,672,680],{"__ignoreMap":42},[46,321,322,324,327,330,332,335,337,340,342,344,347,350,353],{"class":48,"line":49},[46,323,53],{"class":52},[46,325,326],{"class":52}," function",[46,328,329],{"class":212}," runSearch",[46,331,272],{"class":56},[46,333,14],{"class":334},"sP4rz",[46,336,186],{"class":52},[46,338,339],{"class":189}," string",[46,341,286],{"class":56},[46,343,186],{"class":52},[46,345,346],{"class":212}," Promise",[46,348,349],{"class":56},"\u003C",[46,351,352],{"class":189},"unknown",[46,354,355],{"class":56},"> {\n",[46,357,358,361,364,367,370,373,375,378,380,383,386,388,391,394,396,399,401],{"class":48,"line":66},[46,359,360],{"class":52},"  const",[46,362,363],{"class":189}," worker",[46,365,366],{"class":52}," =",[46,368,369],{"class":56}," env.",[46,371,372],{"class":189},"LOADER",[46,374,314],{"class":56},[46,376,377],{"class":212},"get",[46,379,272],{"class":56},[46,381,382],{"class":222},"`markethub-search-${",[46,384,385],{"class":56},"crypto",[46,387,314],{"class":222},[46,389,390],{"class":212},"randomUUID",[46,392,393],{"class":222},"()",[46,395,289],{"class":222},[46,397,398],{"class":56},", () ",[46,400,60],{"class":52},[46,402,403],{"class":56}," ({\n",[46,405,406,409,412],{"class":48,"line":75},[46,407,408],{"class":56},"    compatibilityDate: ",[46,410,411],{"class":222},"'2026-07-11'",[46,413,226],{"class":56},[46,415,416,419,422],{"class":48,"line":200},[46,417,418],{"class":56},"    globalOutbound: ",[46,420,421],{"class":189},"null",[46,423,226],{"class":56},[46,425,426,429,432],{"class":48,"line":219},[46,427,428],{"class":56},"    mainModule: ",[46,430,431],{"class":222},"'worker.js'",[46,433,226],{"class":56},[46,435,436],{"class":48,"line":229},[46,437,438],{"class":56},"    modules: {\n",[46,440,441,444,447],{"class":48,"line":241},[46,442,443],{"class":222},"      'worker.js'",[46,445,446],{"class":56},": ",[46,448,449],{"class":222},"`\n",[46,451,452],{"class":48,"line":247},[46,453,454],{"class":222},"import { WorkerEntrypoint } from \"cloudflare:workers\";\n",[46,456,457],{"class":48,"line":252},[46,458,460],{"emptyLinePlaceholder":459},true,"\n",[46,462,463,466,469,471,474,476,479,481],{"class":48,"line":261},[46,464,465],{"class":222},"const spec = ${",[46,467,468],{"class":189},"JSON",[46,470,314],{"class":222},[46,472,473],{"class":212},"stringify",[46,475,272],{"class":222},[46,477,478],{"class":56},"searchSpec",[46,480,286],{"class":222},[46,482,483],{"class":222},"};\n",[46,485,486],{"class":48,"line":295},[46,487,460],{"emptyLinePlaceholder":459},[46,489,491],{"class":48,"line":490},12,[46,492,493],{"class":222},"export default class SearchExecutor extends WorkerEntrypoint {\n",[46,495,497],{"class":48,"line":496},13,[46,498,499],{"class":222},"  async evaluate() {\n",[46,501,503],{"class":48,"line":502},14,[46,504,505],{"class":222},"    try {\n",[46,507,509,512,514],{"class":48,"line":508},15,[46,510,511],{"class":222},"      const result = await (${",[46,513,14],{"class":56},[46,515,516],{"class":222},"})();\n",[46,518,520],{"class":48,"line":519},16,[46,521,522],{"class":222},"      return { result, err: undefined };\n",[46,524,526],{"class":48,"line":525},17,[46,527,528],{"class":222},"    } catch (error) {\n",[46,530,532],{"class":48,"line":531},18,[46,533,534],{"class":222},"      return {\n",[46,536,538],{"class":48,"line":537},19,[46,539,540],{"class":222},"        result: undefined,\n",[46,542,544],{"class":48,"line":543},20,[46,545,546],{"class":222},"        err: error instanceof Error ? error.message : String(error)\n",[46,548,550],{"class":48,"line":549},21,[46,551,552],{"class":222},"      };\n",[46,554,556],{"class":48,"line":555},22,[46,557,558],{"class":222},"    }\n",[46,560,562],{"class":48,"line":561},23,[46,563,564],{"class":222},"  }\n",[46,566,568],{"class":48,"line":567},24,[46,569,78],{"class":222},[46,571,573],{"class":48,"line":572},25,[46,574,575],{"class":222},"      `\n",[46,577,579],{"class":48,"line":578},26,[46,580,558],{"class":56},[46,582,584],{"class":48,"line":583},27,[46,585,586],{"class":56},"  }))\n",[46,588,590],{"class":48,"line":589},28,[46,591,460],{"emptyLinePlaceholder":459},[46,593,595,597,600,602,605,608,611,614,617,620],{"class":48,"line":594},29,[46,596,360],{"class":52},[46,598,599],{"class":189}," entrypoint",[46,601,366],{"class":52},[46,603,604],{"class":56}," worker.",[46,606,607],{"class":212},"getEntrypoint",[46,609,610],{"class":56},"() ",[46,612,613],{"class":52},"as",[46,615,616],{"class":189}," unknown",[46,618,619],{"class":52}," as",[46,621,622],{"class":212}," SearchExecutorEntrypoint\n",[46,624,626,628,631,633,636,639,642],{"class":48,"line":625},30,[46,627,360],{"class":52},[46,629,630],{"class":189}," response",[46,632,366],{"class":52},[46,634,635],{"class":52}," await",[46,637,638],{"class":56}," entrypoint.",[46,640,641],{"class":212},"evaluate",[46,643,644],{"class":56},"()\n",[46,646,648],{"class":48,"line":647},31,[46,649,460],{"emptyLinePlaceholder":459},[46,651,653,656],{"class":48,"line":652},32,[46,654,655],{"class":52},"  if",[46,657,658],{"class":56}," (response.err)\n",[46,660,662,665,667,669],{"class":48,"line":661},33,[46,663,664],{"class":52},"    throw",[46,666,209],{"class":52},[46,668,269],{"class":212},[46,670,671],{"class":56},"(response.err)\n",[46,673,675,677],{"class":48,"line":674},34,[46,676,69],{"class":52},[46,678,679],{"class":56}," response.result\n",[46,681,683],{"class":48,"line":682},35,[46,684,78],{"class":56},[10,686,687],{},"That may sound complicated, but it is not.",[689,690,691,698,712,734],"ul",{},[692,693,694,697],"li",{},[14,695,696],{},"env.LOADER.get"," is a Cloudflare binding that allows us to create a new worker on the fly. We give it a unique name to create a new worker for each tool call to make sure each tool execution is isolated",[692,699,700,703,704,707,708,711],{},[14,701,702],{},"globalOutbound: null"," disables all outbound ",[14,705,706],{},"fetch()"," and ",[14,709,710],{},"connect()"," calls from the dynamic Worker. This is why the search executor can inspect its embedded specification but cannot access the network.",[692,713,714,717,718,721,722,725,726,729,730,733],{},[14,715,716],{},"mainModule"," is the entrypoint of our worker. It's like telling Vite to read ",[14,719,720],{},"index.ts"," or ",[14,723,724],{},"main.ts"," from the HTML file. In our case, we tell it to read ",[14,727,728],{},"worker.js"," that we define in the ",[14,731,732],{},"modules"," property.",[692,735,736,738,739,741],{},[14,737,732],{}," is a map of module names to their content, similarly to a virtual file system. By module, we mean ECMAScript modules. In our case, we define a single module called ",[14,740,728],{}," that contains the code to execute the AI's code.",[10,743,744,745,750,751,754],{},"Between our worker and the newly created one, Cloudflare uses RPC (",[131,746,749],{"href":747,"rel":748},"https:\u002F\u002Fcapnproto.org\u002F",[135],"Cap'n Proto","). That is why we can call ",[14,752,753],{},"entrypoint.evaluate()"," as if it were a local function. Under the hood, Cloudflare will serialize the request, send it to the new worker, execute it, and return the result.",[22,756,758],{"id":757},"give-execute-only-an-allowlisted-gateway","Give execute only an allowlisted gateway",[10,760,761],{},"Search needs no outbound access, but execute needs to call the local MarketHub API. The execute implementation changes only that capability boundary:",[36,763,765],{"className":38,"code":764,"filename":40,"language":41,"meta":42,"style":42},"const worker = env.LOADER.get(`markethub-execute-${crypto.randomUUID()}`, () => ({\n  compatibilityDate: '2026-07-11',\n  globalOutbound: exports.GlobalOutbound({}),\n  mainModule: 'worker.js',\n  modules: {\n    'worker.js': executeWorkerSource(code),\n  },\n}))\n",[14,766,767,805,814,830,839,844,857,862],{"__ignoreMap":42},[46,768,769,772,774,776,778,780,782,784,786,789,791,793,795,797,799,801,803],{"class":48,"line":49},[46,770,771],{"class":52},"const",[46,773,363],{"class":189},[46,775,366],{"class":52},[46,777,369],{"class":56},[46,779,372],{"class":189},[46,781,314],{"class":56},[46,783,377],{"class":212},[46,785,272],{"class":56},[46,787,788],{"class":222},"`markethub-execute-${",[46,790,385],{"class":56},[46,792,314],{"class":222},[46,794,390],{"class":212},[46,796,393],{"class":222},[46,798,289],{"class":222},[46,800,398],{"class":56},[46,802,60],{"class":52},[46,804,403],{"class":56},[46,806,807,810,812],{"class":48,"line":66},[46,808,809],{"class":56},"  compatibilityDate: ",[46,811,411],{"class":222},[46,813,226],{"class":56},[46,815,816,819,822,824,827],{"class":48,"line":75},[46,817,818],{"class":56},"  globalOutbound: ",[46,820,821],{"class":189},"exports",[46,823,314],{"class":56},[46,825,826],{"class":212},"GlobalOutbound",[46,828,829],{"class":56},"({}),\n",[46,831,832,835,837],{"class":48,"line":200},[46,833,834],{"class":56},"  mainModule: ",[46,836,431],{"class":222},[46,838,226],{"class":56},[46,840,841],{"class":48,"line":219},[46,842,843],{"class":56},"  modules: {\n",[46,845,846,849,851,854],{"class":48,"line":229},[46,847,848],{"class":222},"    'worker.js'",[46,850,446],{"class":56},[46,852,853],{"class":212},"executeWorkerSource",[46,855,856],{"class":56},"(code),\n",[46,858,859],{"class":48,"line":241},[46,860,861],{"class":56},"  },\n",[46,863,864],{"class":48,"line":247},[46,865,866],{"class":56},"}))\n",[10,868,869,870,872,873,877,878,881,882,707,884,886],{},"Passing ",[14,871,421],{}," means ",[874,875,876],"strong",{},"deny all outbound access",". Passing ",[14,879,880],{},"exports.GlobalOutbound({})"," instead routes every dynamic Worker ",[14,883,706],{},[14,885,710],{}," call through the parent Worker's gateway. The sandbox still has no direct network access; it receives only the behavior that the gateway permits.",[36,888,890],{"className":38,"code":889,"filename":40,"language":41,"meta":42,"style":42},"export class GlobalOutbound extends WorkerEntrypoint\u003CEnv> {\n  async fetch(request: Request): Promise\u003CResponse> {\n    const allowedOrigin = new URL(this.env.MARKETHUB_API_BASE).origin\n    const requestedOrigin = new URL(request.url).origin\n\n    if (requestedOrigin !== allowedOrigin) {\n      return new Response('Forbidden: outbound destination is not allowed', {\n        status: 403\n      })\n    }\n\n    return fetch(request)\n  }\n}\n",[14,891,892,916,947,976,992,996,1010,1028,1036,1041,1045,1049,1059,1063],{"__ignoreMap":42},[46,893,894,897,900,903,906,909,911,914],{"class":48,"line":49},[46,895,896],{"class":52},"export",[46,898,899],{"class":52}," class",[46,901,902],{"class":212}," GlobalOutbound",[46,904,905],{"class":52}," extends",[46,907,908],{"class":212}," WorkerEntrypoint",[46,910,349],{"class":56},[46,912,913],{"class":212},"Env",[46,915,355],{"class":56},[46,917,918,921,924,926,929,931,934,936,938,940,942,945],{"class":48,"line":66},[46,919,920],{"class":52},"  async",[46,922,923],{"class":212}," fetch",[46,925,272],{"class":56},[46,927,928],{"class":334},"request",[46,930,186],{"class":52},[46,932,933],{"class":212}," Request",[46,935,286],{"class":56},[46,937,186],{"class":52},[46,939,346],{"class":212},[46,941,349],{"class":56},[46,943,944],{"class":212},"Response",[46,946,355],{"class":56},[46,948,949,952,955,957,959,962,964,967,970,973],{"class":48,"line":75},[46,950,951],{"class":52},"    const",[46,953,954],{"class":189}," allowedOrigin",[46,956,366],{"class":52},[46,958,209],{"class":52},[46,960,961],{"class":212}," URL",[46,963,272],{"class":56},[46,965,966],{"class":189},"this",[46,968,969],{"class":56},".env.",[46,971,972],{"class":189},"MARKETHUB_API_BASE",[46,974,975],{"class":56},").origin\n",[46,977,978,980,983,985,987,989],{"class":48,"line":200},[46,979,951],{"class":52},[46,981,982],{"class":189}," requestedOrigin",[46,984,366],{"class":52},[46,986,209],{"class":52},[46,988,961],{"class":212},[46,990,991],{"class":56},"(request.url).origin\n",[46,993,994],{"class":48,"line":219},[46,995,460],{"emptyLinePlaceholder":459},[46,997,998,1001,1004,1007],{"class":48,"line":229},[46,999,1000],{"class":52},"    if",[46,1002,1003],{"class":56}," (requestedOrigin ",[46,1005,1006],{"class":52},"!==",[46,1008,1009],{"class":56}," allowedOrigin) {\n",[46,1011,1012,1015,1017,1020,1022,1025],{"class":48,"line":241},[46,1013,1014],{"class":52},"      return",[46,1016,209],{"class":52},[46,1018,1019],{"class":212}," Response",[46,1021,272],{"class":56},[46,1023,1024],{"class":222},"'Forbidden: outbound destination is not allowed'",[46,1026,1027],{"class":56},", {\n",[46,1029,1030,1033],{"class":48,"line":247},[46,1031,1032],{"class":56},"        status: ",[46,1034,1035],{"class":189},"403\n",[46,1037,1038],{"class":48,"line":252},[46,1039,1040],{"class":56},"      })\n",[46,1042,1043],{"class":48,"line":261},[46,1044,558],{"class":56},[46,1046,1047],{"class":48,"line":295},[46,1048,460],{"emptyLinePlaceholder":459},[46,1050,1051,1054,1056],{"class":48,"line":490},[46,1052,1053],{"class":52},"    return",[46,1055,923],{"class":212},[46,1057,1058],{"class":56},"(request)\n",[46,1060,1061],{"class":48,"line":496},[46,1062,564],{"class":56},[46,1064,1065],{"class":48,"line":502},[46,1066,78],{"class":56},[10,1068,1069],{},"The fixture is unauthenticated, so its gateway only allowlists the MarketHub origin. In production, the same gateway can inject a credential held by the parent Worker before forwarding the request. The generated code never sees that credential. This is the essential property.",[22,1071,1073],{"id":1072},"bound-every-execution","Bound every execution",[10,1075,1076,1077,1080],{},"Isolation controls ",[89,1078,1079],{},"what"," generated code can access. Limits control how much work it can do.",[10,1082,1083],{},"The companion implementation already rejects generated programs longer than 20,000 characters and truncates formatted results after 12,000 characters. Those bounds prevent a single model response or result from consuming the surrounding agent context indefinitely.",[10,1085,1086],{},"Dynamic Workers can also enforce per-invocation CPU and subrequest limits. For example, a production configuration could start with the following conservative budget and tune it from observed workloads:",[36,1088,1090],{"className":38,"code":1089,"filename":40,"language":41,"meta":42,"style":42},"const worker = env.LOADER.get(workerId, () => ({\n  compatibilityDate: '2026-07-11',\n  limits: {\n    cpuMs: 50,\n    subRequests: 10,\n  },\n  globalOutbound: exports.GlobalOutbound({}),\n  mainModule: 'worker.js',\n  modules: { 'worker.js': executeWorkerSource(code) },\n}))\n",[14,1091,1092,1115,1123,1128,1138,1148,1152,1164,1172,1186],{"__ignoreMap":42},[46,1093,1094,1096,1098,1100,1102,1104,1106,1108,1111,1113],{"class":48,"line":49},[46,1095,771],{"class":52},[46,1097,363],{"class":189},[46,1099,366],{"class":52},[46,1101,369],{"class":56},[46,1103,372],{"class":189},[46,1105,314],{"class":56},[46,1107,377],{"class":212},[46,1109,1110],{"class":56},"(workerId, () ",[46,1112,60],{"class":52},[46,1114,403],{"class":56},[46,1116,1117,1119,1121],{"class":48,"line":66},[46,1118,809],{"class":56},[46,1120,411],{"class":222},[46,1122,226],{"class":56},[46,1124,1125],{"class":48,"line":75},[46,1126,1127],{"class":56},"  limits: {\n",[46,1129,1130,1133,1136],{"class":48,"line":200},[46,1131,1132],{"class":56},"    cpuMs: ",[46,1134,1135],{"class":189},"50",[46,1137,226],{"class":56},[46,1139,1140,1143,1146],{"class":48,"line":219},[46,1141,1142],{"class":56},"    subRequests: ",[46,1144,1145],{"class":189},"10",[46,1147,226],{"class":56},[46,1149,1150],{"class":48,"line":229},[46,1151,861],{"class":56},[46,1153,1154,1156,1158,1160,1162],{"class":48,"line":241},[46,1155,818],{"class":56},[46,1157,821],{"class":189},[46,1159,314],{"class":56},[46,1161,826],{"class":212},[46,1163,829],{"class":56},[46,1165,1166,1168,1170],{"class":48,"line":247},[46,1167,834],{"class":56},[46,1169,431],{"class":222},[46,1171,226],{"class":56},[46,1173,1174,1177,1179,1181,1183],{"class":48,"line":252},[46,1175,1176],{"class":56},"  modules: { ",[46,1178,431],{"class":222},[46,1180,446],{"class":56},[46,1182,853],{"class":212},[46,1184,1185],{"class":56},"(code) },\n",[46,1187,1188],{"class":48,"line":261},[46,1189,866],{"class":56},[10,1191,1192],{},"If the dynamic Worker exceeds either limit, it throws an exception. The right values depend on the expected work: search may need very little CPU and no subrequests, while execute needs enough subrequests for its planned API calls.",[22,1194,1196],{"id":1195},"youre-now-ready","You're now ready",[10,1198,1199],{},"You now have an MCP server that uses Code Mode while keeping generated code inside a fresh isolated runtime. Search has no outbound access and execute has only an allowlisted gateway, and input, output, CPU, subrequest budgets prevent one execution from consuming unbounded resources.",[10,1201,1202,1203,1207],{},"I hope you've enjoyed this series and you learned something new. The companion repository, ",[1204,1205],"git-hub-link",{"repo":1206},"barbapapazes\u002Fcode-mode",", is available to access all the code we've discussed. Each article has a dedicated branch.",[10,1209,1210],{},"If you have any questions, feel free to reach out to me on Twitter or LinkedIn.",[1212,1213,1214],"style",{},"html pre.shiki code .s8jYJ, html code.shiki .s8jYJ{--shiki-light:#D73A49;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sxrX7, html code.shiki .sxrX7{--shiki-light:#24292E;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sCsY4, html code.shiki .sCsY4{--shiki-light:#6A737D;--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sBjJW, html code.shiki .sBjJW{--shiki-light:#005CC5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .snPdu, html code.shiki .snPdu{--shiki-light:#6F42C1;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sIIMD, html code.shiki .sIIMD{--shiki-light:#032F62;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sP4rz, html code.shiki .sP4rz{--shiki-light:#E36209;--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":42,"searchDepth":66,"depth":66,"links":1216},[1217,1218,1219,1220,1221,1222,1223,1224],{"id":24,"depth":66,"text":25},{"id":94,"depth":66,"text":95},{"id":110,"depth":66,"text":111},{"id":147,"depth":66,"text":148},{"id":157,"depth":66,"text":158},{"id":757,"depth":66,"text":758},{"id":1072,"depth":66,"text":1073},{"id":1195,"depth":66,"text":1196},"5e3a9c71-2b48-4d06-8f95-c7a1e0b6342d","2026-07-12","Replace a trusted local Function evaluator with isolated Cloudflare Dynamic Workers, controlled outbound access, and bounded execution results.","md",{"branch":1230},"part-5-dynamic-workers",null,{},"\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Freplacing-local-eval-with-isolated-cloudflare-workers","---\ncontentId: 5e3a9c71-2b48-4d06-8f95-c7a1e0b6342d\npart: 5\ntitle: Replacing the Evaluator with Isolated Cloudflare Workers\ndescription: Replace a trusted local Function evaluator with isolated Cloudflare Dynamic Workers, controlled outbound access, and bounded execution results.\ngithub:\n  branch: part-5-dynamic-workers\ndate: 2026-07-12\nseriesSlug: building-code-mode-for-apis-with-mcp-and-isolated-workers\n---\n\nIn the previous parts, we implemented search and execute tools that used a trusted local evaluator based on the `Function` constructor to run AI-generated code. That approach worked for development but raised serious security concerns.\n\nIn this part, we will replace that local evaluator with an isolated environment using Cloudflare Dynamic Workers. Isolation and explicit capabilities let us constrain what generated code can do instead of granting it the host's permissions.\n\n## User input can't be trusted\n\nOne of the first things we learn when building websites with forms is that we cannot trust user input. It is not only about malicious users, but also mistakes and unexpected input.\n\nGenerated code is user input too. A model can make mistakes, and a malicious user can try to steer the model towards an unsafe action.\n\nImagine that, instead of writing a function to search the OpenAPI specification, the model wrote the following code:\n\n```typescript [Example]\nasync () => {\n  return process.env\n}\n```\n\nWhen generated code runs in the host process, it can access any global or capability the host exposes. Depending on the environment, that may include environment variables, filesystem access, process APIs, or unrestricted network access. The `Function` constructor is an evaluator. It was never designed to run untrusted code.\n\nSo we need a sandbox. _Never trust the user, or the generated code._\n\n## Sandboxes are slow and expensive\n\nA sandbox is an isolated environment where untrusted code cannot affect the rest of the system. In our case, it is the right way to run generated code.\n\nTraditional sandboxes often use virtual machines or containers. Their startup time, memory footprint, and price vary by provider and configuration, but a full operating-system environment is much more than this task needs.\n\nThere is another problem. Many agents need environments in which to run code. If every short-lived tool execution starts a full operating-system environment, capacity and cost can add up quickly.\n\nWe need something lighter, faster, and more scalable. We are running small JavaScript functions. We need a constrained JavaScript runtime.\n\n## Isolate the memory\n\nLet us step back. Is there an existing solution that runs untrusted code in an isolated environment?\n\nBrowsers!\n\nBrowsers routinely run code from many different websites on the same machine. To keep that code separate, V8 uses isolates: independent JavaScript heaps that can share a process without sharing memory.\n\nAn isolate is much lighter than a virtual machine or a container. It gives each execution its own memory space, so code running in one isolate cannot directly inspect or modify another execution's objects.\n\nMemory isolation alone is not enough, though. Untrusted code must also be given only the capabilities it needs: no filesystem access, no arbitrary secrets, and no unrestricted network access.\n\nThis is where Cloudflare Workers are particularly useful. [They build on V8 isolates](https:\u002F\u002Fblog.cloudflare.com\u002Fintroducing-cloudflare-workers\u002F#:~:text=We%20run%20JavaScript,top%20of%20this.) and a capability-based runtime: a Worker only receives the bindings and outbound access that we explicitly grant it.\n\nNode.js also uses V8, but Node itself is not a secure sandbox. The `node:vm` module documentation explicitly says it should not be used to run untrusted code. For this use case, we need a runtime designed to enforce those boundaries.\n\n## Dynamic Workers to the rescue\n\nCloudflare Workers provide this kind of isolated runtime. With Dynamic Workers, we do not deploy a new Worker through an HTTP endpoint for each execution.\n\nInstead, the parent Worker uses the Dynamic Worker Loader binding to create a fresh Worker at runtime, wait for its RPC response, and return that response to the MCP client. Cloudflare describes fresh isolates as starting in a few milliseconds and using a few megabytes of memory, which makes them suitable for short-lived code execution.\n\n## From local eval to Dynamic Workers\n\nSo now we have the concept, let's implement it in our code.\n\nCurrently, we have:\n\n```typescript [Example]\n\u002F* eslint-disable no-new-func *\u002F\nlet searchFunction: unknown\ntry {\n  searchFunction = new Function(\n    'spec',\n    `\"use strict\"; return (${trimmedCode});`\n  )(searchSpec)\n}\ncatch (error) {\n  throw new Error(`Could not compile search code: ${errorMessage(error)}`)\n}\n```\n\nWe will replace it with a call to a new worker that we create on the fly.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nThis series assumes you are already familiar with [Cloudflare Bindings](https:\u002F\u002Fdevelopers.cloudflare.com\u002Fworkers\u002Fruntime-apis\u002Fbindings\u002F).\n::\n\n```typescript [Example]\nasync function runSearch(code: string): Promise\u003Cunknown> {\n  const worker = env.LOADER.get(`markethub-search-${crypto.randomUUID()}`, () => ({\n    compatibilityDate: '2026-07-11',\n    globalOutbound: null,\n    mainModule: 'worker.js',\n    modules: {\n      'worker.js': `\nimport { WorkerEntrypoint } from \"cloudflare:workers\";\n\nconst spec = ${JSON.stringify(searchSpec)};\n\nexport default class SearchExecutor extends WorkerEntrypoint {\n  async evaluate() {\n    try {\n      const result = await (${code})();\n      return { result, err: undefined };\n    } catch (error) {\n      return {\n        result: undefined,\n        err: error instanceof Error ? error.message : String(error)\n      };\n    }\n  }\n}\n      `\n    }\n  }))\n\n  const entrypoint = worker.getEntrypoint() as unknown as SearchExecutorEntrypoint\n  const response = await entrypoint.evaluate()\n\n  if (response.err)\n    throw new Error(response.err)\n  return response.result\n}\n```\n\nThat may sound complicated, but it is not.\n\n- `env.LOADER.get` is a Cloudflare binding that allows us to create a new worker on the fly. We give it a unique name to create a new worker for each tool call to make sure each tool execution is isolated\n- `globalOutbound: null` disables all outbound `fetch()` and `connect()` calls from the dynamic Worker. This is why the search executor can inspect its embedded specification but cannot access the network.\n- `mainModule` is the entrypoint of our worker. It's like telling Vite to read `index.ts` or `main.ts` from the HTML file. In our case, we tell it to read `worker.js` that we define in the `modules` property.\n- `modules` is a map of module names to their content, similarly to a virtual file system. By module, we mean ECMAScript modules. In our case, we define a single module called `worker.js` that contains the code to execute the AI's code.\n\nBetween our worker and the newly created one, Cloudflare uses RPC ([Cap'n Proto](https:\u002F\u002Fcapnproto.org\u002F)). That is why we can call `entrypoint.evaluate()` as if it were a local function. Under the hood, Cloudflare will serialize the request, send it to the new worker, execute it, and return the result.\n\n## Give execute only an allowlisted gateway\n\nSearch needs no outbound access, but execute needs to call the local MarketHub API. The execute implementation changes only that capability boundary:\n\n```typescript [Example]\nconst worker = env.LOADER.get(`markethub-execute-${crypto.randomUUID()}`, () => ({\n  compatibilityDate: '2026-07-11',\n  globalOutbound: exports.GlobalOutbound({}),\n  mainModule: 'worker.js',\n  modules: {\n    'worker.js': executeWorkerSource(code),\n  },\n}))\n```\n\nPassing `null` means **deny all outbound access**. Passing `exports.GlobalOutbound({})` instead routes every dynamic Worker `fetch()` and `connect()` call through the parent Worker's gateway. The sandbox still has no direct network access; it receives only the behavior that the gateway permits.\n\n```typescript [Example]\nexport class GlobalOutbound extends WorkerEntrypoint\u003CEnv> {\n  async fetch(request: Request): Promise\u003CResponse> {\n    const allowedOrigin = new URL(this.env.MARKETHUB_API_BASE).origin\n    const requestedOrigin = new URL(request.url).origin\n\n    if (requestedOrigin !== allowedOrigin) {\n      return new Response('Forbidden: outbound destination is not allowed', {\n        status: 403\n      })\n    }\n\n    return fetch(request)\n  }\n}\n```\n\nThe fixture is unauthenticated, so its gateway only allowlists the MarketHub origin. In production, the same gateway can inject a credential held by the parent Worker before forwarding the request. The generated code never sees that credential. This is the essential property.\n\n## Bound every execution\n\nIsolation controls *what* generated code can access. Limits control how much work it can do.\n\nThe companion implementation already rejects generated programs longer than 20,000 characters and truncates formatted results after 12,000 characters. Those bounds prevent a single model response or result from consuming the surrounding agent context indefinitely.\n\nDynamic Workers can also enforce per-invocation CPU and subrequest limits. For example, a production configuration could start with the following conservative budget and tune it from observed workloads:\n\n```typescript [Example]\nconst worker = env.LOADER.get(workerId, () => ({\n  compatibilityDate: '2026-07-11',\n  limits: {\n    cpuMs: 50,\n    subRequests: 10,\n  },\n  globalOutbound: exports.GlobalOutbound({}),\n  mainModule: 'worker.js',\n  modules: { 'worker.js': executeWorkerSource(code) },\n}))\n```\n\nIf the dynamic Worker exceeds either limit, it throws an exception. The right values depend on the expected work: search may need very little CPU and no subrequests, while execute needs enough subrequests for its planned API calls.\n\n## You're now ready\n\nYou now have an MCP server that uses Code Mode while keeping generated code inside a fresh isolated runtime. Search has no outbound access and execute has only an allowlisted gateway, and input, output, CPU, subrequest budgets prevent one execution from consuming unbounded resources.\n\nI hope you've enjoyed this series and you learned something new. The companion repository, \u003CGitHubLink repo=\"barbapapazes\u002Fcode-mode\" \u002F>, is available to access all the code we've discussed. Each article has a dedicated branch.\n\nIf you have any questions, feel free to reach out to me on Twitter or LinkedIn.\n",{"title":5,"description":1227},"building-code-mode-for-apis-with-mcp-and-isolated-workers",{"loc":1233},"series\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Freplacing-local-eval-with-isolated-cloudflare-workers","UV_byAEKaOKZaUKQhfhJewjQGnr01t-wos7HSmWoPjs",{"overview":1241,"chapters":1403},{"id":1242,"title":1243,"body":1244,"contentId":1383,"date":1226,"description":1384,"extension":1228,"github":1385,"meetTheInstructor":1387,"meta":1388,"navigation":459,"part":1231,"path":1389,"rawbody":1390,"robots":1231,"schemaOrg":1231,"seo":1391,"seriesSlug":1236,"sitemap":1392,"stem":1393,"subtitle":1394,"topics":1395,"__hash__":1402},"series\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers.md","Building Code Mode for APIs with MCP and Isolated Workers",{"type":7,"value":1245,"toc":1380},[1246,1249,1257,1263,1275,1290,1293,1296,1299,1356,1362,1370,1374],[10,1247,1248],{},"Hello and welcome! 👋",[10,1250,1251,1252,1256],{},"This series is a practical follow-up to ",[131,1253,1255],{"href":1254},"\u002Fposts\u002Fcode-mode-two-tools-and-an-mcp-can-save-your-llm-context","Code Mode, Two Tools, and an MCP Can Save Your LLM Context",". That article introduced the idea: instead of making a model orchestrate every dependent tool call, let it write the orchestration as code and return only the result needed for its next decision.",[10,1258,1259,1260,1262],{},"Here, we will build that idea step by step. We will start with a small local file transformation, move to an OpenAPI-described API, and then replace the trusted local evaluator based on the ",[14,1261,16],{}," constructor with an isolated runtime. The final result will be a locally runnable MCP server that can search an API specification and execute a narrow request against the API.",[10,1264,1265,1266,1270,1271,314],{},"MCP fundamentals are outside the scope of this series. If you need an introduction to the protocol or to the basic relationship between an AI agent and an MCP server, begin with ",[131,1267,1269],{"href":1268},"\u002Fseries\u002Fai-agents-and-mcp-server-teaming-up-for-the-agentic-web","AI Agents and MCP Server: Teaming Up for the Agentic Web",", especially ",[131,1272,1274],{"href":1273},"\u002Fseries\u002Fai-agents-and-mcp-server-teaming-up-for-the-agentic-web\u002Fmcp-to-provide-additional-capabilities-to-the-ai-agent","its MCP server article",[10,1276,1277,1278,1281,1282,1285,1286,1289],{},"Throughout the series, the ",[874,1279,1280],{},"model"," generates tool calls or code, the ",[874,1283,1284],{},"MCP client"," (or agent) runs the model loop, and the ",[874,1287,1288],{},"MCP server"," provides the capabilities. Keeping these roles separate is important: the host, not generated code, defines which capabilities are available.",[10,1291,1292],{},"The central idea is simple: with direct MCP tools, the model must call one tool, receive the complete intermediate result, and then use that result to decide how to call the next tool. With Code Mode, the model writes the orchestration as code, the code accesses the capabilities it needs inside the execution environment, and the model receives only the desired final result to continue.",[10,1294,1295],{},"Code Mode is not automatically the right choice. Direct MCP tools remain simpler for small, obvious actions. The extra runtime is worthwhile when a task contains dependent calls, requires local data processing, or needs to navigate a large and evolving API surface, like an OpenAPI specification that can be larger than the model's context window.",[10,1297,1298],{},"Throughout this journey, we will:",[1300,1301,1302,1312,1327,1337,1351],"ol",{},[692,1303,1304,1307,1308,1311],{},[874,1305,1306],{},"Beyond One Tool Call at a Time: Introducing Code Mode",": Build two direct ",[14,1309,1310],{},"Drive"," tools over a Markdown fixture and observe how the complete file content travels through the model's context before the next tool call.",[692,1313,1314,1317,1318,1320,1321,1323,1324,1326],{},[874,1315,1316],{},"Let the Model Orchestrate Tools with a Local Code Runtime",": Replace the two direct tools with one generic ",[14,1319,14],{}," tool, expose a typed ",[14,1322,1310],{}," facade, and introduce the executor interface that later parts will replace. The local evaluator based on the ",[14,1325,16],{}," constructor is intentionally a trusted development shortcut, not a production sandbox.",[692,1328,1329,1332,1333,1336],{},[874,1330,1331],{},"Searching OpenAPI Specifications Without Filling Context",": Keep the OpenAPI document inside the server, resolve its local ",[14,1334,1335],{},"$ref"," values, and let generated code search a compact type facade instead of returning the complete specification to the model.",[692,1338,1339,1342,1343,1346,1347,1350],{},[874,1340,1341],{},"Calling APIs with Code Mode and Returning Only What Matters",": Add a separate ",[14,1344,1345],{},"execute"," tool with a host-owned ",[14,1348,1349],{},"api.request()"," helper. We will validate request options, call the local MarketHub fixture, and return a narrow projection rather than the complete API response.",[692,1352,1353,1355],{},[874,1354,5],{},": Swap only the executor boundary for fresh Dynamic Workers. Search will run without outbound access, execute will use an allowlisted gateway, and credentials will remain outside generated code. We will also cover result bounds, resource limits, and host deadlines.",[10,1357,1358,1359,1361],{},"The companion repository, ",[1204,1360],{"repo":1206},", is an executable textbook for the series. Each article has a complete checkpoint branch, a small evaluation, and a focused diff from the previous step. I recommend that you check out a branch and inspect the implementation for a better understanding of the concepts, as the articles will not cover every detail.",[302,1363,1364],{"color":304,"icon":305},[10,1365,1366,1367,1369],{},"This series assumes familiarity with TypeScript, JavaScript promises, HTTP APIs, and basic MCP concepts. The local checkpoints contain no credentials. Parts 2 through 4 use a trusted local evaluator based on the ",[14,1368,16],{}," constructor for development, while Part 5 provides a Cloudflare deployment scaffold and structural security evaluation.",[22,1371,1373],{"id":1372},"chapters","Chapters",[1375,1376],"series-chapters-list",{"className":1377},[1378,1379],"mx-auto","max-w-5xl",{"title":42,"searchDepth":66,"depth":66,"links":1381},[1382],{"id":1372,"depth":66,"text":1373},"8f6d8d8d-9cc8-4ed3-9f2d-4e3d0b7a4d91","Build an MCP server for an OpenAPI-described API, then replace a trusted local evaluator with isolated Cloudflare Dynamic Workers.",{"name":1386,"repository":1206},"Code Mode","I created this series to turn the Code Mode idea into a practical implementation. We will start with two ordinary MCP tools, move the orchestration into generated code, and finish by replacing a trusted local evaluator based on the Function constructor with isolated Cloudflare Dynamic Workers.",{},"\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers","---\ncontentId: 8f6d8d8d-9cc8-4ed3-9f2d-4e3d0b7a4d91\ntitle: Building Code Mode for APIs with MCP and Isolated Workers\nsubtitle: Build a context-efficient MCP server for OpenAPI-described APIs\ndescription: Build an MCP server for an OpenAPI-described API, then replace a trusted local evaluator with isolated Cloudflare Dynamic Workers.\ndate: 2026-07-12\ntopics:\n  - ai\n  - ai-agents\n  - mcp\n  - code-mode\n  - openapi\n  - cloudflare\n  - typescript\ngithub:\n  name: Code Mode\n  repository: barbapapazes\u002Fcode-mode\nmeetTheInstructor: I created this series to turn the Code Mode idea into a practical implementation. We will start with two ordinary MCP tools, move the orchestration into generated code, and finish by replacing a trusted local evaluator based on the Function constructor with isolated Cloudflare Dynamic Workers.\nseriesSlug: building-code-mode-for-apis-with-mcp-and-isolated-workers\n---\n\nHello and welcome! 👋\n\nThis series is a practical follow-up to [Code Mode, Two Tools, and an MCP Can Save Your LLM Context](\u002Fposts\u002Fcode-mode-two-tools-and-an-mcp-can-save-your-llm-context). That article introduced the idea: instead of making a model orchestrate every dependent tool call, let it write the orchestration as code and return only the result needed for its next decision.\n\nHere, we will build that idea step by step. We will start with a small local file transformation, move to an OpenAPI-described API, and then replace the trusted local evaluator based on the `Function` constructor with an isolated runtime. The final result will be a locally runnable MCP server that can search an API specification and execute a narrow request against the API.\n\nMCP fundamentals are outside the scope of this series. If you need an introduction to the protocol or to the basic relationship between an AI agent and an MCP server, begin with [AI Agents and MCP Server: Teaming Up for the Agentic Web](\u002Fseries\u002Fai-agents-and-mcp-server-teaming-up-for-the-agentic-web), especially [its MCP server article](\u002Fseries\u002Fai-agents-and-mcp-server-teaming-up-for-the-agentic-web\u002Fmcp-to-provide-additional-capabilities-to-the-ai-agent).\n\nThroughout the series, the **model** generates tool calls or code, the **MCP client** (or agent) runs the model loop, and the **MCP server** provides the capabilities. Keeping these roles separate is important: the host, not generated code, defines which capabilities are available.\n\nThe central idea is simple: with direct MCP tools, the model must call one tool, receive the complete intermediate result, and then use that result to decide how to call the next tool. With Code Mode, the model writes the orchestration as code, the code accesses the capabilities it needs inside the execution environment, and the model receives only the desired final result to continue.\n\nCode Mode is not automatically the right choice. Direct MCP tools remain simpler for small, obvious actions. The extra runtime is worthwhile when a task contains dependent calls, requires local data processing, or needs to navigate a large and evolving API surface, like an OpenAPI specification that can be larger than the model's context window.\n\nThroughout this journey, we will:\n\n1. **Beyond One Tool Call at a Time: Introducing Code Mode**: Build two direct `Drive` tools over a Markdown fixture and observe how the complete file content travels through the model's context before the next tool call.\n2. **Let the Model Orchestrate Tools with a Local Code Runtime**: Replace the two direct tools with one generic `code` tool, expose a typed `Drive` facade, and introduce the executor interface that later parts will replace. The local evaluator based on the `Function` constructor is intentionally a trusted development shortcut, not a production sandbox.\n3. **Searching OpenAPI Specifications Without Filling Context**: Keep the OpenAPI document inside the server, resolve its local `$ref` values, and let generated code search a compact type facade instead of returning the complete specification to the model.\n4. **Calling APIs with Code Mode and Returning Only What Matters**: Add a separate `execute` tool with a host-owned `api.request()` helper. We will validate request options, call the local MarketHub fixture, and return a narrow projection rather than the complete API response.\n5. **Replacing the Evaluator with Isolated Cloudflare Workers**: Swap only the executor boundary for fresh Dynamic Workers. Search will run without outbound access, execute will use an allowlisted gateway, and credentials will remain outside generated code. We will also cover result bounds, resource limits, and host deadlines.\n\nThe companion repository, \u003CGitHubLink repo=\"barbapapazes\u002Fcode-mode\" \u002F>, is an executable textbook for the series. Each article has a complete checkpoint branch, a small evaluation, and a focused diff from the previous step. I recommend that you check out a branch and inspect the implementation for a better understanding of the concepts, as the articles will not cover every detail.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nThis series assumes familiarity with TypeScript, JavaScript promises, HTTP APIs, and basic MCP concepts. The local checkpoints contain no credentials. Parts 2 through 4 use a trusted local evaluator based on the `Function` constructor for development, while Part 5 provides a Cloudflare deployment scaffold and structural security evaluation.\n::\n\n## Chapters\n\n::series-chapters-list\n---\nclass: mx-auto max-w-5xl\n---\n::\n",{"title":1243,"description":1384},{"loc":1389},"series\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers","Build a context-efficient MCP server for OpenAPI-described APIs",[1396,1397,1398,1399,1400,1401,41],"ai","ai-agents","mcp","code-mode","openapi","cloudflare","WgDRYOu5CDIYY78O1GSEOv6BKbIsrTVwVf8cTOkdsOA",[1404,1410,1416,1422,1428],{"contentId":1405,"title":1306,"description":1406,"path":1407,"part":49,"github":1408,"topics":1231},"6f0c6f18-8d31-4b4d-aea6-9b2df779a0d1","See why dependent direct MCP tool calls make intermediate content part of model context, and how Code Mode moves orchestration into generated code.","\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Fbeyond-one-tool-call-at-a-time-introducing-code-mode",{"branch":1409},"part-1-direct-tools",{"contentId":1411,"title":1316,"description":1412,"path":1413,"part":66,"github":1414,"topics":1231},"2a9e7d4c-1f6b-4c83-9d52-7e0a6b3f4189","Build a minimal trusted local Code Mode runtime with a typed Drive facade and an executor seam ready for isolation.","\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Flet-the-model-orchestrate-tools-with-a-local-code-runtime",{"branch":1415},"part-2-generic-code-mode",{"contentId":1417,"title":1331,"description":1418,"path":1419,"part":75,"github":1420,"topics":1231},"8c4a1e72-bf39-4d65-a08e-2c7f91b6d543","Keep an OpenAPI specification server-side and search a compact typed facade without returning the complete document.","\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Fsearching-openapi-specification-without-filling-the-context",{"branch":1421},"part-3-search",{"contentId":1423,"title":1341,"description":1424,"path":1425,"part":200,"github":1426,"topics":1231},"1d7b5e93-6a24-4f80-bc19-3e8d0a572f16","Use a host-owned API request helper to validate and execute a narrow API call, then return only the useful projection.","\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Fcalling-apis-with-code-mode-and-returning-only-what-matters",{"branch":1427},"part-4-local-execute",{"contentId":1225,"title":5,"description":1227,"path":1233,"part":219,"github":1429,"topics":1231},{"branch":1230},1795016691634]