This project is a minimal TypeScript implementation demonstrating Anthropic's "Code Mode" or "Code execution with MCP".
Instead of overwhelming the language model's context window with dozens of tool definitions, the agent is given a single execution tool (execute_ts_code). At startup, the host application dynamically interrogates attached MCP servers (e.g., the local filesystem or a memory graph), and generates TypeScript stubs on your local disk in the .mcp-tools directory. The AI agent can then read these stubs using standard filesystem tools and write self-contained scripts to orchestrate the tools directly in a sandbox.
- Node.js environment
- TypeScript and TSX
- An Anthropic API Key
-
Install Dependencies If passing the project folder to a fresh machine, run:
npm install
-
Add Your API Key Create a
.envfile in the root of the project with your Anthropic Key, or simply export it in your terminal:export ANTHROPIC_API_KEY="sk-ant-api03-..."
-
Run the Demonstration
npm run start # or npx tsx src/index.ts
.mcp-tools/: Contains the auto-generated TypeScript stubs. Whenindex.tsruns it creates these physically on disk so the LLM can discover them.src/index.ts: The host. Connects the MCP Servers, spins up a local proxy, and then starts the agent.src/agent.ts: The Anthropic loop. It tells the agent about the.mcp-tools/directory and prompts it to write self-contained scripts.src/executor.ts: Usestsxto evaluate the TypeScript scripts generated by the agent.src/mcp-client.ts: Wraps the generic@modelcontextprotocol/sdkfor easy transport management.