Examples
AI Web Client
Configure client-side Code Mode through IoAiWeb.WebConfig.codeMode. AI Web registers connected non-UI MCP tools with the session and advertises the client Code Mode capability to MCP servers.
import { IoAiWebFactory } from "@interopio/ai-web";
import { IoCodeModeFactory } from "@interopio/code-mode/browser";
const aiWeb = await IoAiWebFactory(io, {
agentServer: { baseUrl: "https://agent.example.com" },
codeMode: {
factory: IoCodeModeFactory,
config: { wasmLocation: "/quickjs/emscripten-module.wasm" },
},
});
MCP Core Server
Configure server-side Code Mode through IoIntelMCPCore.Config.server.codeMode. MCP Core exposes meta-tools only to clients that do not advertise their own Code Mode capability.
import { IoCodeModeFactory } from "@interopio/code-mode/node";
import { IoIntelMCPCoreFactory } from "@interopio/mcp-core";
const mcpCore = await IoIntelMCPCoreFactory(io, {
licenseKey,
transport: { type: "http" },
server: {
name: "io-intelligence-mcp",
codeMode: {
factory: IoCodeModeFactory,
config: { wasmLocation: quickJsWasmPath },
},
},
});
AI Web supports one active Code Mode provider per session. Multiple server-side providers can expose conflicting getTools, getToolDefinition, and executeCode tools. See duplicate-provider troubleshooting for capability negotiation and conflict diagnosis.
Standalone Host
Register IoCodeMode.RuntimeTool[] with Session.setTools(). The host model adapter must inject getSystemInstruction(), expose the tools returned by getLlmTools(), and route model tool calls to their execute methods.
import { IoCodeModeFactory } from "@interopio/code-mode/browser";
import type { IoCodeMode } from "@interopio/code-mode";
const hostTools: IoCodeMode.RuntimeTool[] = [
{
name: "search_orders",
description: "Search orders by query.",
inputSchema: {
type: "object",
properties: { query: { type: "string" } },
required: ["query"],
},
outputSchema: {
type: "object",
properties: { matches: { type: "array", items: { type: "string" } } },
required: ["matches"],
},
execute: async (args) => {
const result = { matches: [`Results for ${String(args.query)}`] };
return {
content: [{ type: "text", text: JSON.stringify(result) }],
structuredContent: result,
};
},
},
];
const codeMode = await IoCodeModeFactory({
wasmLocation: "/quickjs/emscripten-module.wasm",
});
const session = codeMode.createSession();
session.setTools(hostTools);
const systemInstruction = codeMode.getSystemInstruction();
const toolsForModel = session.getLlmTools();
Keep the session alive through the model run. To cancel an executeCode call, pass the run's signal to its execute(args, { signal }) method. Remove the session with codeMode.removeSession(session.id) when the run ends.
const executeCode = toolsForModel.find((tool) => tool.name === "executeCode")!;
const controller = new AbortController();
const result = await executeCode.execute(
{ code: "const result = await tool_search_orders({ query: 'orders' }); return result.matches;", explanation: "Search for orders." },
{ signal: controller.signal }
);
The registered search_orders tool returns both JSON text in content and an object in structuredContent. The sandbox call resolves to the structuredContent object, so the code reads result.matches directly. Without structuredContent, a tool that declares outputSchema has its text parsed as JSON; without either field, JSON text remains a string and must be parsed in sandbox code. The tool is exposed as tool_search_orders; use the exact binding returned by getTools for your host's tool catalog. A cancelled run returns an error result. Create a new controller for each independently cancellable execution.