> ## 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

> Code execution types and methods for MCPClient

Node.js only. Enable on `MCPClient` to let agents execute JavaScript with access to connected MCP tools.

```ts theme={null}
import { BaseCodeExecutor, MCPClient } from "@mcp-use/client";
import type {
  CodeModeConfig,
  ExecutionResult,
  ToolSearchResponse,
  VMExecutorOptions,
  E2BExecutorOptions,
} from "@mcp-use/client";
```

## Enable

```ts theme={null}
// Shorthand
new MCPClient(config, { codeMode: true });

// Full config
new MCPClient(config, {
  codeMode: {
    enabled: true,
    executor: "vm" | "e2b" | customFn | BaseCodeExecutor,
    executorOptions: { timeoutMs?: number, apiKey?: string, memoryLimitMb?: number },
  },
});
```

## Methods

### executeCode(code, timeout?)

Runs code with tool namespaces exposed as `serverName.toolName(args)`.

Returns `ExecutionResult`:

| Field | Type |
| - | - |
| `result` | `unknown` |
| `logs` | `string[]` |
| `error` | `string \| null` |
| `execution_time` | `number` (seconds) |

### searchTools(query?, detailLevel?)

Search tools across active sessions. `detailLevel`: `"names" | "descriptions" | "full"`.

Returns `ToolSearchResponse` with `results` and `meta` (counts, namespaces).

## Executors

| Type | Class / value | Notes |
| - | - | - |
| VM | `"vm"` (default) | `VMCodeExecutor`, `isVMAvailable()` |
| E2B | `"e2b"` | `E2BCodeExecutor`, requires `@e2b/code-interpreter` peer |
| Custom | `BaseCodeExecutor` subclass or async function | Implement `execute()` + `cleanup()` |

`CodeModeConnector` exposes internal `execute_code` and `search_tools` MCP tools.

## Types

```ts theme={null}
interface CodeModeConfig {
  enabled: boolean;
  executor?: "vm" | "e2b" | CodeExecutorFunction | BaseCodeExecutor;
  executorOptions?: VMExecutorOptions | E2BExecutorOptions;
}

interface VMExecutorOptions {
  timeoutMs?: number;      // default 30000
  memoryLimitMb?: number;
}

interface E2BExecutorOptions {
  apiKey: string;
  timeoutMs?: number;      // default 300000
}
```

## Related

* [Code mode guide](https://docs.mcp-use.com/v1/typescript/client/code-mode.md)
* [`MCPClient`](https://docs.mcp-use.com/v1/typescript/api-reference/client/mcp-client)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.