Airsc is free for everyone. If it helps you, support it with $2–$20.

Support
MCP serverSafeAgent workflow & memorySome setup

code-mode

universal-tool-calling-protocol · universal-tool-calling-protocol/code-mode

This plug-and-play library enables AI agents to call MCP and UTCP tools more efficiently via code execution, reducing costs and iterations.

🔌 Plug-and-play library to enable agents to call MCP and UTCP tools via code execution.

1.6k stars108License: MPL-2.0Updated 2 months ago0 opens

Preview

▍

What you can do with it

  • Improve the speed and cost efficiency of AI agents performing multi-step tasks.
  • Enable agents to batch multiple API calls into a single code execution block.
  • Streamline complex tool orchestrations by letting the LLM generate execution code directly.

Safety check

Safe

No risky patterns found in the files we scanned.

Automated scan of the repo files. Always review code you run.

From the README

🤖 Code-Mode Library: First library for tool calls via code execution

Transform your AI agents from clunky tool callers into efficient code executors — in just 3 lines.

Why This Changes Everything

LLMs excel at writing code but struggle with tool calls. Instead of exposing hundreds of tools directly, give them ONE tool that executes TypeScript code with access to your entire toolkit.

Apple, Cloudflare, and Anthropic say that Code-Mode is a more efficient way to approach tool calling compared to the traditional dump function information and then extract a JSON for function calling.

Benchmarks

Independent Python benchmark study validates the performance claims with $9,536/year cost savings at 1,000 scenarios/day:

Scenario ComplexityTraditionalCode ModeImprovement
Simple (2-3 tools)3 iterations1 execution67% faster
Medium (4-7 tools)8 iterations1 execution75% faster
Complex (8+ tools)16 iterations1 execution88% faster

Why Code Mode Dominates:

Batching Advantage - Single code block replaces multiple API calls
Cognitive Efficiency - LLMs excel at code generation vs. tool orchestration
Computational Efficiency - No context re-processing between operations

Getting Started

Get Started in 3 Lines

import { CodeModeUtcpClient } from '@utcp/code-mode';

const client = await CodeModeUtcpClient.create();                    // 1. Initialize
await client.registerManual({ name: 'github', /* MCP config */ });  // 2. Add tools  
const { result } = await client.callToolChain(`/* TypeScript */`);   // 3. Execute code

That's it. Your AI agent can now execute complex workflows in a single request instead of dozens.

What You Get

Progressive Tool Discovery

// Agent discovers tools dynamically, loads only what it needs
const tools = await client.searchTools('github pull request');
// Instead of 500 tool definitions → 3 relevant tools

Natural Code Execution

const { result, logs } = await client.callToolChain(`
  // Chain multiple operations in one request
  const pr = await github.get_pull_request({ owner: 'microsoft', repo: 'vscode', pull_number: 1234 });
  const comments = await github.get_pull_request_comments({ owner: 'microsoft', repo: 'vscode', pull_number: 1234 });
  const reviews = await github.get_pull_request_reviews({ owner: 'microsoft', repo: 'vscode', pull_number: 1234 });
  
  // Process data efficiently in-sandbox
  return {
    title: pr.title,
    commentCount: comments.length,
    approvals: reviews.filter(r => r.state === 'APPROVED').length
  };
`);
// Single API call replaces 15+ traditional tool calls

Auto-Generated TypeScript Interfaces

namespace github {
  interface get_pull_requestInput {
    /** Repository owner */
    owner: string;
    /** Repository name */ 
    repo: string;
    /** Pull request number */
    pull_number: number;
  }
}

Enterprise-Ready

  • Secure VM Sandboxing – Node.js isolates prevent unauthorized access
  • Timeout Protection – Configurable execution limits prevent runaway code
  • Complete Observability – Full console output capture and error handling
  • Zero External Dependencies – Tools only accessible through registered UTCP/MCP servers
  • Runtime Introspection – Dynamic interface discovery for adaptive workflows

If you're working at an enterprise, and need support, book a consultation here.

Universal Protocol Support

Works with any tool ecosystem:

ProtocolDescriptionUsage
MCPModel Context Protocol serverscall_template_type: 'mcp'
HTTPREST APIs with auto-discoverycall_template_type: 'http'
FileLocal JSON/YAML configurationscall_template_type: 'file'
CLICommand-line tool executioncall_template_type: 'cli'

Installation

npm install @utcp/code-mode

Recommended for shell agents: the utcp CLI

If your agent can run shell commands (Claude Code, Cursor, Codex, Claude Cowork, etc.), the utcp CLI is the preferred way to use Code Mode — no MCP server, no client config, no env vars. The agent self-configures by writing a .utcp_config.json and drives everything from the shell.

Just point the agent at the built-in guide:

npx -y @utcp/code-mode-cli prompt

Hand it a description of the API to use — a UTCP call template, an OpenAPI spec, or a plain-English description — and it writes the config, discovers tools, runs tool-chains, and even completes interactive OAuth logins (e.g. Notion), all from the shell:

npx -y @utcp/code-mode-cli search "<task>"           # discover tools + TS interfaces
npx -y @utcp/code-mode-cli run <<'EOF'               # run a tool-chain
const r = await openlibrary.read_search_json_search_json_get({ q: "tolkien", limit: 3 });
return r.docs.map(b => b.title);
EOF
npx -y @utcp/code-mode-cli login <manual>            # interactive OAuth, writes token to .env

CLI vs MCP: prefer the CLI whenever the agent has shell access (most coding agents) — it's simpler and self-configuring. Use the MCP server (below) only for MCP-only clients like Claude Desktop. Both wrap the same @utcp/code-mode engine.

See code-mode-cli/ for full docs.

Ready-to-Use MCP Server

On an MCP-only client (e.g. Claude Desktop)? Use our plug-and-play

View source on GitHub