Recommended as a deployment target for best isolation when running the code execution sandbox in production environments
Enables updating Salesforce records through MCP tool calls, such as updating Lead objects with data from other sources
code2mcp - Code Mode MCP Server
Execute TypeScript code to call MCP tools instead of using direct tool calls. Based on Cloudflare's revolutionary Code Mode pattern.
What is Code Mode?
Instead of exposing MCP tools directly to the LLM (which wastes tokens and struggles with complex tools), Code Mode:
Converts MCP tools into TypeScript APIs with full type definitions
Has the LLM write code that calls those APIs
Executes code in a secure sandbox with access only to specified MCP servers
Returns only the final results to the LLM, not intermediate data
Benefits
✅ 98% token reduction for complex multi-tool workflows
✅ Better tool understanding: LLMs are trained on millions of TypeScript examples
✅ Handle complex tools: Full APIs vs simplified tool schemas
✅ Secure execution: Sandboxed code, no network/filesystem access
✅ API keys hidden: Keys stored in orchestrator, never exposed to LLM
Installation
Note: build:full generates TypeScript API files from your MCP servers, which helps Claude understand parameter names and types. See IMPROVEMENTS.md for details.
Configuration
1. Pre-Configured MCP Servers ✅
Already configured and ready to use! Five MCP servers are pre-configured:
Context7 - Data storage and context management
Playwright - Browser automation and web scraping
Bright Data - Proxy network and geo-distributed scraping
Chrome DevTools - Chrome DevTools Protocol integration
Firecrawl - Advanced web crawling and content extraction
See CONFIGURED_SERVERS.md for details on each server.
To add more servers, edit src/index.ts and modify the MCP_SERVERS array:
2. Set Environment Variables
3. Register with Claude Code
Add to ~/.claude.json:
Usage
Example 1: Simple Tool Call
User: "What's the weather?"
Claude writes:
Output:
Example 2: Multi-Step Workflow (Token Savings!)
User: "Get my Google Doc and update Salesforce"
Claude writes:
Key Benefit: The 50,000-token document never enters Claude's context. Only the logs are returned!
Example 3: Complex Orchestration
This kind of complex orchestration would be impossible with standard MCP tool calling!
Architecture
Development
Security
The sandbox provides basic isolation:
✅ No network access (
fetch,XMLHttpRequest,WebSocketblocked)✅ No filesystem access (
fs,pathblocked)✅ No process access (
child_process,processblocked)✅ Only
__mcp_call()binding available✅ API keys stored in orchestrator, never in sandbox
✅ Timeout enforcement (default 30s)
Note: This uses Node.js built-in vm module, which provides basic isolation but is not as secure as isolated-vm or Deno. For production use, consider:
Using Deno with strict permissions
Using
isolated-vmwith older Node.js version (v20)Running in a containerized environment
Deploying to Cloudflare Workers (best isolation)
Token Usage Comparison
Standard MCP (Direct Tool Calling)
Code Mode
Token Reduction: 47% for simple workflows, 98% for complex workflows!
Documentation
See /DOCS folder for complete architecture documentation:
DOCS/Architecture/SYSTEM_MAP.md- Complete architecture overviewDOCS/Architecture/CODE_STRUCTURE.md- File organizationDOCS/Etc/CODE_MODE_IMPLEMENTATION_PLAN.md- Detailed implementation plan
References
License
ISC
Contributing
Contributions welcome! This is a reference implementation of the Code Mode pattern.
Roadmap
Configuration file support (vs hardcoded in src/index.ts)
Deno sandbox implementation for better security
TypeScript API browser/explorer
Support for HTTP/WebSocket MCP transports
Streaming execution logs
Code templates library
Performance optimizations
Comprehensive test suite
Built with ❤️ implementing Cloudflare's Code Mode pattern