CF-Incidents Agents, Workers - @cloudflare/codemode v0.2.1: MCP barrel export, zero-dependency main entry point, and custom sandbox modules

Cloudflare Service Informational Feeds

ENF News Bot

Automated News Bot
Joined
Apr 15, 2022
Messages
2,591
The latest releases of @cloudflare/codemode ↗︎ add a new MCP barrel export, remove ai and zod as required peer dependencies from the main entry point, and give you more control over the sandbox.

New @cloudflare/codemode/mcp export​

https://developers.cloudflare.com/changelog/rss/#new-cloudflarecodemodemcp-export

A new @cloudflare/codemode/mcp entry point provides two functions that wrap MCP servers with Code Mode:

  • codeMcpServer({ server, executor }) — wraps an existing MCP server with a single code tool where each upstream tool becomes a typed codemode.* method.
  • openApiMcpServer({ spec, executor, request }) — creates search and execute MCP tools from an OpenAPI spec with host-side request proxying and automatic $ref resolution.

Code:
import { codeMcpServer } from "@cloudflare/codemode/mcp";
import { DynamicWorkerExecutor } from "@cloudflare/codemode";

const executor = new DynamicWorkerExecutor({ loader: env.LOADER });

// Wrap an existing MCP server — all its tools become
// typed methods the LLM can call from generated code
const server = await codeMcpServer({ server: upstreamMcp, executor });

Code:
import { codeMcpServer } from "@cloudflare/codemode/mcp";
import { DynamicWorkerExecutor } from "@cloudflare/codemode";

const executor = new DynamicWorkerExecutor({ loader: env.LOADER });

// Wrap an existing MCP server — all its tools become
// typed methods the LLM can call from generated code
const server = await codeMcpServer({ server: upstreamMcp, executor });

Zero-dependency main entry point​

https://developers.cloudflare.com/changelog/rss/#zero-dependency-main-entry-point

Breaking change in v0.2.0: generateTypes and the ToolDescriptor / ToolDescriptors types have moved to @cloudflare/codemode/ai:


Code:
// Before
import { generateTypes } from "@cloudflare/codemode";

// After
import { generateTypes } from "@cloudflare/codemode/ai";

Code:
// Before
import { generateTypes } from "@cloudflare/codemode";

// After
import { generateTypes } from "@cloudflare/codemode/ai";

The main entry point (@cloudflare/codemode) no longer requires the ai or zod peer dependencies. It now exports:

ExportDescription
sanitizeToolNameSanitize tool names into valid JS identifiers
normalizeCodeNormalize LLM-generated code into async arrow functions
generateTypesFromJsonSchemaGenerate TypeScript type definitions from plain JSON Schema
jsonSchemaToTypeConvert a single JSON Schema to a TypeScript type string
DynamicWorkerExecutorSandboxed code execution via Dynamic Worker Loader
ToolDispatcherRPC target for dispatching tool calls from sandbox to host

The ai and zod peer dependencies are now optional — only required when importing from @cloudflare/codemode/ai.

Custom sandbox modules​

https://developers.cloudflare.com/changelog/rss/#custom-sandbox-modules

DynamicWorkerExecutor now accepts an optional modules option to inject custom ES modules into the sandbox:


Code:
const executor = new DynamicWorkerExecutor({
	loader: env.LOADER,
	modules: {
		"utils.js": `export function add(a, b) { return a + b; }`,
	},
});

// Sandbox code can then: import { add } from "utils.js"

Code:
const executor = new DynamicWorkerExecutor({
	loader: env.LOADER,
	modules: {
		"utils.js": `export function add(a, b) { return a + b; }`,
	},
});

// Sandbox code can then: import { add } from "utils.js"

Internal normalization and sanitization​

https://developers.cloudflare.com/changelog/rss/#internal-normalization-and-sanitization

DynamicWorkerExecutor now normalizes code and sanitizes tool names internally. You no longer need to call normalizeCode() or sanitizeToolName() before passing code and functions to execute().

Upgrade​

https://developers.cloudflare.com/changelog/rss/#upgrade

npm i @cloudflare/codemode@latest

See the Code Mode documentation for the full API reference.

Continue reading...
 
Back
Top