GitHub 3.6K
Tool Use & Environment established

Code Mode MCP Tool Interface Improvement Pattern

LLMs generate TypeScript code to orchestrate MCP tools in ephemeral V8 isolates, eliminating token-heavy round-trips and enabling efficient multi-step workflows with 10x+ token savings.

By Nikola Balic (@nibzard)
Add to Pack
or

Saved locally in this browser for now.

Cite This Pattern
APA
Nikola Balic (@nibzard) (2026). Code Mode MCP Tool Interface Improvement Pattern. In *Awesome Agentic Patterns*. Retrieved March 11, 2026, from https://agentic-patterns.com/patterns/code-first-tool-interface-pattern
BibTeX
@misc{agentic_patterns_code-first-tool-interface-pattern,
  title = {Code Mode MCP Tool Interface Improvement Pattern},
  author = {Nikola Balic (@nibzard)},
  year = {2026},
  howpublished = {\url{https://agentic-patterns.com/patterns/code-first-tool-interface-pattern}},
  note = {Awesome Agentic Patterns}
}
01

Problem

Traditional Model Context Protocol (MCP) approaches of directly exposing tools to Large Language Models create significant token waste and complexity issues. We've moved from telling LLMs what to do, to teaching them to write instructions for themselves—it's turtles writing code all the way down[^1] for all domains.

02

Solution

Code Mode complements (not replaces) MCP servers by adding an ephemeral execution layer that eliminates token-heavy round-trips:

03

How to use it

  1. Design Tool APIs: Create TypeScript interfaces for your tools that are intuitive for code generation
  2. Implement Bindings: Develop secure bindings that control access to external resources
  3. Sandbox Setup: Configure V8 isolates with appropriate security constraints
  4. Code Execution Flow:
  • LLM generates TypeScript code using the provided APIs
    • Code runs in isolated V8 environment
    • Bindings provide controlled access to tools
    • Results return to the agent for further processing
04

Trade-offs

Pros:

  • Dramatic token savings on multi-step workflows (10-100x reduction; Anthropic reports 75x on 10K-row spreadsheets: 150K → 2K tokens)
  • Dramatic fan-out efficiency - for loops over 100+ entries vs 100+ tool calls (speed + reliability at scale)
  • Faster execution through elimination of round-trips
  • Enhanced security - credentials stay in MCP servers, never in LLM
  • Complex orchestration - LLMs excel at writing orchestration code
  • CaMeL-style self-debugging - agents can debug their own homework with error handling and retry logic
  • Typed verification and semantic caching - compile-time validation and workflow reuse opportunities
  • Maintained MCP benefits - existing servers work without modification
  • Natural idempotency patterns - checkpoint/resume capabilities with state stores

Cons/Considerations:

  • Infrastructure complexity - requires V8 isolate runtime infrastructure
  • Code quality dependency - execution success depends on LLM's code generation
  • Poor fit for dynamic research loops - struggles when next steps are decided dynamically at each stage
  • Intelligence-in-the-middle challenge - cases requiring LLM calls mid-execution defeat the purpose
  • Debugging challenges - runtime errors in generated code need handling
  • API design overhead - need intuitive TypeScript interfaces for code generation
  • Partial failure complexity - requires careful design of state management and recovery patterns
06

References

[^1]: Phrase coined by Rafal Wilinski in his Code Mode analysis