# MCP-as-Code-API

> Turn MCP servers into typed code wrappers the agent imports in a sandbox — massive token savings as tool outputs flow between calls without ever hitting the context window.

- **Category**: Agentic AI
- **Subcategory**: Tool Use & Environment
- **Canonical URL**: https://designpattern.fyi/patterns/mcp_as_code_api/

---

## Description
**Intent**: Let large tool outputs flow through sandbox memory instead of the model's context window.

**Context**: Your agent is connected to many MCP servers (Google Drive, Slack, Postgres, GitHub) each exposing tens of tools with verbose JSON outputs. The agent already has a code-execution sandbox. You're burning context on intermediate results the model mostly doesn't need to see.

**Solution**: At connection time, walk each MCP server's tool list and emit one typed wrapper file per tool (e.g., `servers/gdrive/getDocument.ts`). Expose this tree to the agent as a readable filesystem. The agent explores via list/read primitives, writes a short script that imports wrappers, chains calls, transforms results in-memory, and prints only the final answer. Intermediate tool outputs stay in sandbox variables — only the printed result crosses back into model context.


## Use Cases
- Workflows chain many MCP tools and intermediate data is large (sheets, transcripts, binaries).
- A code-execution sandbox is already part of the agent stack.
- Token cost or latency is dominated by tool-output round-tripping.
- Tool surface is too large to fit all schemas in the prompt.





## Trade-offs


### Advantages

- Massive token reduction — Anthropic reports 98.7% on representative workflows.

- Large tool outputs never enter the context window.

- Composition becomes ordinary code: filters, joins, retries — not prompted loops.

- Tool discovery becomes filesystem navigation, reusing well-trained model behavior.




### Considerations & Drawbacks

- Requires a working code-execution sandbox with network egress controls.

- Model must be strong at code generation in the chosen runtime.

- Untrusted data flowing through code without LLM checkpoints widens the prompt-injection surface inside the sandbox.

- Wrapper generation must stay in sync with upstream MCP schema changes.







---
**Reference**: [Original Source](https://www.agentpatternscatalog.org/patterns/mcp-as-code-api/)

