> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mcp-use.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Mode

> Let agents write code to call MCP tools

Code mode exposes `execute_code` and `search_tools` so agents run JavaScript with access to connected MCP tools instead of making individual tool calls. Node.js only.

## Enable

```typescript theme={null}
import { MCPClient } from "@mcp-use/client";

const client = new MCPClient(
  {
    mcpServers: {
      fs: {
        command: "npx",
        args: ["-y", "@modelcontextprotocol/server-filesystem", "./data"],
      },
    },
  },
  { codeMode: true },
);

const result = await client.executeCode(`
  const files = await fs.list_directory({ path: "." });
  return files.length;
`);

console.log(result.result, result.logs, result.error);
await client.close();
```

## Executors

| Executor                             | Use case                                                           |
| ------------------------------------ | ------------------------------------------------------------------ |
| `"vm"` (default)                     | Local Node.js VM — fast, trusted environments                      |
| `"e2b"`                              | Cloud sandbox — untrusted code (`@e2b/code-interpreter` + API key) |
| Custom function / `BaseCodeExecutor` | Your own runtime                                                   |

```typescript theme={null}
{ codeMode: {
    enabled: true,
    executor: "e2b",
    executorOptions: { apiKey: process.env.E2B_API_KEY!, timeoutMs: 300_000 },
}}
```

## Agent integration

Use with `@mcp-use/agent` and `PROMPTS.CODE_MODE`:

```typescript theme={null}
import { MCPAgent, PROMPTS } from "@mcp-use/agent";

const agent = new MCPAgent({ llm, client, systemPrompt: PROMPTS.CODE_MODE });
await agent.run("List all files and count them");
```

## Reference

[Client API reference](https://mcp-use-typescript-api-reference.vercel.app/modules/_mcp-use_client.index.html) — config types, `searchTools`, and `ExecutionResult`.

[Anthropic: Code execution with MCP](https://www.anthropic.com/engineering/code-execution-with-mcp)
