Skip to content

Repository files navigation

MCP Code Execution Agent Example

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.

Prerequisites

  • Node.js environment
  • TypeScript and TSX
  • An Anthropic API Key

Getting Started

  1. Install Dependencies If passing the project folder to a fresh machine, run:

    npm install
  2. Add Your API Key Create a .env file in the root of the project with your Anthropic Key, or simply export it in your terminal:

    export ANTHROPIC_API_KEY="sk-ant-api03-..."
  3. Run the Demonstration

    npm run start
    # or
    npx tsx src/index.ts

Architecture

  • .mcp-tools/: Contains the auto-generated TypeScript stubs. When index.ts runs 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: Uses tsx to evaluate the TypeScript scripts generated by the agent.
  • src/mcp-client.ts: Wraps the generic @modelcontextprotocol/sdk for easy transport management.

About

Minimal TypeScript implementation demonstrating Anthropic's "Code Mode" or "Code execution with MCP"

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages