Code Mode 标志

Code Mode

组织热门
universal-tool-calling-protocol

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

发布者universal-tool-calling-protocol
代码仓库code-mode
语言TypeScript
复刻数
108
星标数
1.6K
可用工具
0
传输类型stdio
许可协议MPL-2.0
链接
  • 将工具接入 AI 工作流

    Code Mode 提供 MCP 功能,可供兼容的 AI 客户端和智能体使用。

  • 0 个可用工具

    浏览下方可调用的操作;如果服务器提供了名称和描述,也会一并显示。

  • 可直接复制的配置

    使用安装配置片段,在您首选的 MCP 客户端中配置此服务器。

  • 开源社区热度

    关联的代码仓库有 1.6K 个 Star 和 108 个 Fork。

npm

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

typescript
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

typescript
// 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

typescript
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

typescript
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

bash
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:

bash
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:

bash
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 MCP server. (If your agent has a shell, prefer the utcp CLI above.)

json
{
  "mcpServers": {
    "code-mode": {
      "command": "npx",
      "args": ["@utcp/code-mode-mcp"],
      "env": {
        "UTCP_CONFIG_FILE": "/path/to/your/.utcp_config.json"
      }
    }
  }
}

That's it! No installation, no Node.js knowledge required. The Code Mode MCP Server automatically:

  • Downloads and runs the latest version via npx
  • Loads your tool configurations from JSON
  • Provides code execution capabilities to Claude Desktop
  • Gives you call_tool_chain as an MCP tool for TypeScript execution

Perfect for non-developers who want Code Mode power in Claude Desktop!

Direct TypeScript Usage

1. MCP Server Integration

Connect to any Model Context Protocol server:

Each call_template_type is a separate plugin package. Install and import the package once at startup so it can register itself with the client; the plugin registers as a side effect of being imported. For mcp, that's @utcp/mcp. The same pattern applies to other transports: @utcp/http, @utcp/text, etc.

bash
npm install @utcp/mcp
typescript
import '@utcp/mcp';                      // registers the 'mcp' call template
import { CodeModeUtcpClient } from '@utcp/code-mode';

const client = await CodeModeUtcpClient.create();

// Connect to GitHub MCP server
await client.registerManual({
  name: 'github',
  call_template_type: 'mcp',
  config: {
    mcpServers: {
      github: {
        transport: 'stdio',              // required by @utcp/mcp
        command: 'docker',
        args: ['run', '-i', '--rm', '-e', 'GITHUB_PERSONAL_ACCESS_TOKEN', 'mcp/github'],
        env: { GITHUB_PERSONAL_ACCESS_TOKEN: process.env.GITHUB_TOKEN }
      }
    }
  }
});

2. Execute Multi-Step Workflows

Replace 15+ tool calls with a single code execution:

typescript
const { result, logs } = await client.callToolChain(`
  // Traditional: 4 separate API round trips → Code Mode: 1 execution
  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 });
  const files = await github.get_pull_request_files({ owner: 'microsoft', repo: 'vscode', pull_number: 1234 });
  
  // Process data in-sandbox (no token overhead)
  const summary = {
    title: pr.title,
    state: pr.state,
    author: pr.user.login,
    stats: {
      comments: comments.length,
      reviews: reviews.length, 
      filesChanged: files.length,
      approvals: reviews.filter(r => r.state === 'APPROVED').length
    },
    topDiscussion: comments.slice(0, 3).map(c => ({
      author: c.user.login,
      preview: c.body.substring(0, 100) + '...'
    }))
  };
  
  console.log(\`PR "\${pr.title}" analysis complete\`);
  return summary;
`);

console.log('Analysis Result:', result);
// console output: 'PR "Fix memory leak in hooks" analysis complete'

Advanced Features

Multi-Protocol Tool Chains

Mix and match different tool ecosystems in a single execution:

typescript
// Register multiple tool sources
await client.registerManual({ name: 'github', call_template_type: 'mcp', /* config */ });
await client.registerManual({ name: 'slack', call_template_type: 'http', /* config */ });
await client.registerManual({ name: 'db', call_template_type: 'file', file_path: './db-tools.json' }); // This loads a UTCP manual from a json file

const result = await client.callToolChain(`
  // Fetch PR data from GitHub (MCP)
  const pr = await github.get_pull_request({ owner: 'company', repo: 'api', pull_number: 42 });
  
  // Query deployment status from database (File)
  const deployment = await db.get_deployment_status({ pr_id: pr.id });
  
  // Send notification to Slack (HTTP)
  await slack.post_message({
    channel: '#releases',
    text: \`PR #42 "\${pr.title}" deployed to \${deployment.environment}\`
  });
  
  return { pr: pr.title, environment: deployment.environment };
`);

Runtime Interface Introspection

Tools can dynamically discover and adapt to available interfaces:

typescript
const result = await client.callToolChain(`
  // Discover available tools at runtime
  console.log('Available interfaces:', __interfaces);
  
  // Get specific tool interface for validation
  const prInterface = __getToolInterface('github.get_pull_request');
  console.log('PR tool expects:', prInterface);
  
  // Use interface info for dynamic workflows
  const hasSlackTools = __interfaces.includes('namespace slack');
  if (hasSlackTools) {
    await slack.post_message({ channel: '#dev', text: 'Analysis complete' });
  }
  
  return { toolsAvailable: hasSlackTools };
`);

Context-Efficient Data Processing

Process large datasets without bloating the model's context:

typescript
const result = await client.callToolChain(`
  // Fetch large dataset
  const allIssues = await github.list_repository_issues({ owner: 'facebook', repo: 'react' });
  console.log('Fetched', allIssues.length, 'total issues');
  
  // Process efficiently in-sandbox
  const criticalBugs = allIssues
    .filter(issue => issue.labels.some(l => l.name === 'bug'))
    .filter(issue => issue.labels.some(l => l.name === 'high priority'))
    .map(issue => ({
      number: issue.number,
      title: issue.title,
      author: issue.user.login,
      daysOld: Math.floor((Date.now() - new Date(issue.created_at)) / (1000 * 60 * 60 * 24))
    }))
    .sort((a, b) => b.daysOld - a.daysOld);
  
  // Only return processed summary (not 10,000 raw issues)
  return {
    totalIssues: allIssues.length,
    criticalBugs: criticalBugs.slice(0, 10), // Top 10 oldest critical bugs
    summary: \`Found \${criticalBugs.length} critical bugs, oldest is \${criticalBugs[0]?.daysOld} days old\`
  };
`);

Error Handling & Observability

Built-in error handling with complete execution transparency:

typescript
const { result, logs } = await client.callToolChain(`
  try {
    console.log('Starting multi-step workflow...');
    
    const data = await external_api.fetch_data({ id: 'user-123' });
    console.log('Data fetched successfully');
    
    const processed = await data_processor.transform(data);
    console.warn('Processing completed with', processed.warnings.length, 'warnings');
    
    return processed;
  } catch (error) {
    console.error('Workflow failed:', error.message);
    throw error; // Propagates to outer error handling
  }
`, 30000); // 30-second timeout

// Complete observability
console.log('Result:', result);
console.log('Execution logs:', logs);
// ['Starting multi-step workflow...', 'Data fetched successfully', '[WARN] Processing completed with 2 warnings']

Custom Timeouts

Configure execution limits for different workload types:

typescript
// Quick operations (5 seconds)
const quickResult = await client.callToolChain(`return await ping.check();`, 5000);

// Heavy data processing (2 minutes) 
const heavyResult = await client.callToolChain(`
  const bigData = await database.export_full_dataset();
  return await analytics.process_dataset(bigData);
`, 120000);

AI Agent Integration

Plug-and-play with any AI framework. The built-in prompt template handles all the complexity:

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

const systemPrompt = `
You are an AI assistant with access to tools via UTCP CodeMode.
${CodeModeUtcpClient.AGENT_PROMPT_TEMPLATE}
Additional instructions...
`;

// Works with any AI library
const response = await openai.chat.completions.create({
  model: 'gpt-4',
  messages: [
    { role: 'system', content: systemPrompt },
    { role: 'user', content: 'Analyze the latest PR in microsoft/vscode' }
  ]
});

The template provides comprehensive guidance on:

  • Tool discovery workflow (searchTools → __interfaces → callToolChain)
  • Hierarchical access patterns (manual.tool() syntax)
  • Interface introspection (__getToolInterface())
  • Error handling and best practices

API Reference

Core Methods

callToolChain(code: string, timeout?: number)

Execute TypeScript code with full tool access and observability.

  • Returns: {result: any, logs: string[]} with execution result and captured console output
  • Default timeout: 30 seconds

getAllToolsTypeScriptInterfaces()

Generate complete TypeScript interfaces for IDE integration.

  • Returns: String containing all interface definitions with namespaces

searchTools(query: string) (from UtcpClient)

Discover tools using natural language queries.

  • Returns: Array of relevant tools with descriptions and interfaces

Static Methods

CodeModeUtcpClient.create(root_dir?, config?)

Create a new client instance with optional configuration.

CodeModeUtcpClient.AGENT_PROMPT_TEMPLATE

Production-ready prompt template for AI agents.


Security & Performance

Secure by Design

  • Node.js VM sandboxing – Isolated execution context
  • No filesystem access – Tools only through registered servers
  • Timeout protection – Configurable execution limits
  • Zero network access – No external dependencies or API keys exposed

Performance Optimized

  • Minimal memory footprint – VM contexts are lightweight
  • Efficient tool caching – TypeScript interfaces cached automatically
  • Streaming console output – Real-time log capture without buffering
  • Identifier sanitization – Handles invalid TypeScript identifiers gracefully

Development Experience

IDE Integration

Generate TypeScript definitions for full IntelliSense support:

bash
# Generate tool interfaces  
const interfaces = await client.getAllToolsTypeScriptInterfaces();
await fs.writeFile('generated-tools.d.ts', interfaces);

# Add to tsconfig.json
{
  "compilerOptions": {
    "typeRoots": ["./generated-tools.d.ts"]
  }
}

Debug & Monitor

Built-in observability for production deployments:

typescript
const { result, logs } = await client.callToolChain(userCode);

// Ship logs to your monitoring system
logs.forEach(log => {
  if (log.startsWith('[ERROR]')) monitoring.error(log);
  if (log.startsWith('[WARN]')) monitoring.warn(log);
});

Benchmark Methodology

The comprehensive Python study tested 16 realistic scenarios across:

  • Financial workflows (invoicing, expense tracking)
  • DevOps operations (deployments, monitoring)
  • Data processing (analysis, reporting)
  • Business automation (CRM, notifications)

Models tested: Claude Haiku, Gemini Flash
Pricing basis: $0.25/1M input, $1.25/1M output tokens
Scale: 1,000 scenarios/day = $9,536/year savings with Code Mode

Learn More

License

MPL-2.0 – Open source with commercial-friendly terms.

将 Code Mode MCP 与多个 AI 模型配合使用

TypingMind 在工作区级别连接 MCP 工具,因此连接 Code Mode 后,您就可以在 TypingMind 中搭配不同的 AI 模型使用,无需为每个模型单独设置。 此 MCP 通过您设备上的 TypingMind MCP 连接器在本地运行。

本地连接器设置指南

如果 MCP 服务器需要访问您电脑上的本地文件、应用或私有资源,请选择此方式。

1

打开 MCP 设置

在 TypingMind 中,依次前往“设置”、“高级设置”和“模型上下文协议”,然后选择“设置连接器”。

  1. 在您的浏览器中打开 TypingMind。
  2. 点击“设置”图标。
  3. 进入“高级设置”。
  4. 打开“模型上下文协议”部分。
  5. 点击“设置连接器”,然后选择“此设备”。
TypingMind MCP 连接器设置界面,已选择“此设备”
2

运行连接器命令

选择“此设备”,从 TypingMind 复制命令并在终端中运行。使用 MCP 时,请保持该进程运行。

  1. 复制 TypingMind 显示的设置命令。
  2. 在 macOS 上打开“终端”,或在 Windows 上打开 Windows Terminal。
  3. 粘贴并运行命令。
  4. 如果终端询问您是否继续,请确认安装软件包。
  5. 使用 MCP 工具时,请保持终端窗口及其中的进程运行。
3

将 Code Mode 添加为服务器

当连接器状态为“就绪”时,点击“编辑服务器”,然后粘贴 MCP 服务器配置。

  1. 等待连接器状态显示为“就绪”。
  2. 点击“编辑服务器”。
  3. 粘贴 Code Mode MCP 服务器配置。
  4. 保存服务器列表。
  5. 如需确认连接器是否仍处于就绪状态,请刷新。
TypingMind MCP 设置界面,显示正在运行的服务器和“编辑服务器”按钮
{
  "mcpServers": {
    "code-mode": {
      "command": "npx",
      "args": [
        "-y",
        "<mcp-server-package>"
      ]
    }
  }
}
4

在不同模型中使用同一连接

保存服务器列表,打开“插件”,启用 Code Mode MCP 工具,然后在 TypingMind 中选择任意受支持的 AI 模型,即可在聊天中使用这些工具,或将其分配给智能体。

  1. 在 TypingMind 中打开“插件”页面。
  2. 启用 Code Mode MCP 工具。
  3. 开始聊天并选择您想使用的 AI 模型。
  4. 在聊天中使用 MCP 工具,或将其分配给 AI 智能体。
  5. 根据需要随时切换到其他 AI 模型,无需重新连接 MCP。
在 TypingMind 聊天中使用已启用的 MCP 工具和选定的 AI 模型
您能使用 Code Mode 帮我完成这项任务吗?
Code Mode
当然。我已阅读。
以下是我使用 Code Mode 得到的结果。

常见问题

Code Mode MCP 服务器有什么用途?

Code Mode 是一个 MCP 服务器,可让兼容的 AI 客户端连接外部工具并获取上下文。在 TypingMind 中,只需添加一次此 MCP 服务器,即可在您的 AI 工作区中使用其工具。

可以在 TypingMind 中让多个 AI 模型使用 Code Mode MCP 吗?

可以。TypingMind 在工作区层面连接 MCP 工具,因此您可以让 Claude、ChatGPT、Gemini 等不同 AI 模型,或您在 TypingMind 中配置的其他模型使用 Code Mode,而无需为每个模型单独配置 MCP 服务器。

为什么在 TypingMind 中使用 Code Mode MCP?

TypingMind 将多个 AI 模型、提示词、插件、AI 智能体、API 密钥和 MCP 工具整合到同一个工作区中,是优秀的大语言模型聊天前端之一。连接 Code Mode 后,您可以在偏好的不同模型中使用其 MCP 工具,同时在 TypingMind 中井然有序地管理聊天工作流程。

如何将 Code Mode MCP 连接到 TypingMind?

Code Mode 通过 TypingMind 本地 MCP 连接器运行。当 MCP 服务器需要访问您计算机上的本地文件、桌面应用、命令行工具或私有资源时,这种方式最合适。

Code Mode MCP 在 TypingMind 中提供哪些工具?

Code Mode 提供 MCP 功能,您可以在 TypingMind 的插件页面启用这些功能,然后在聊天中使用,或将其分配给 AI 智能体。

使用 Code Mode MCP 需要向 TypingMind 提供我的 API 密钥吗?

不需要。TypingMind 采用本地优先的设计,让您自行掌控模型提供商、API 密钥、提示词和 MCP 配置。如果 Code Mode 需要身份验证,请在创建连接时,为该 MCP 服务器添加所需的请求头、OAuth 设置或本地配置。

相关 MCP 服务器

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇