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

# MCP server (e2e mcp)

> Reference for e2e mcp, the MCP server that lets coding agents like Claude Code and Cursor inspect your app, try locators, and run test lines.

`e2e mcp` lets coding agents such as Claude Code and Cursor inspect your app,
try locators, run test lines, and take masked screenshots while they write tests. It runs over
stdio and uses the project's configured engine. For setup and what to ask
the agent, see [Let the agent use the app](https://e2e.com/docs/coding-agents#let-the-agent-use-the-app).

```json title=".mcp.json"
{
  "mcpServers": {
    "e2e": { "command": "npx", "args": ["e2e", "mcp"] }
  }
}
```

The app tool catalog reloads from the config whenever a session opens.

## Reference

### Flags

<CodeGroup>
```bash npm
npx e2e mcp [--config <path>] [--target <name>] [--headed] [--max-sessions <n>]
```

```bash pnpm
pnpm exec e2e mcp [--config <path>] [--target <name>] [--headed] [--max-sessions <n>]
```

```bash bun
bunx e2e mcp [--config <path>] [--target <name>] [--headed] [--max-sessions <n>]
```
</CodeGroup>

| Flag | Default |
| --- | --- |
| [`--config <path>`](#config) | discovered like `e2e run` |
| [`--target <name>`](#target) | the only target |
| [`--headed`](#headed) | headless |
| [`--max-sessions <n>`](#max-sessions) | `4`, `1` on Bun |

#### `--config`

The config every session loads unless `open_session` names one. Each
session loads it afresh ([Several sessions](#several-sessions)) with the
server's environment: `process.env` in the config reads the variables the
MCP client started `e2e mcp` with, not the agent's shell.

#### `--target`

The target a session opens on when `open_session` names none.

- Without it and without a name, the only target is used.
- Several targets and no name is `TARGET_REQUIRED`.

#### `--headed`

Shows the browser or simulator, when the engine supports it, in every
session whose `open_session` does not set `headed`.

#### `--max-sessions`

How many sessions may be open at once, 1 through 16. On
[Bun](https://e2e.com/docs/reference/cli#bun), one. See [Several sessions](#several-sessions).

```bash
e2e mcp --max-sessions 8 # default: 4
```

### Tools

| Tool | Description |
| --- | --- |
| [`open_session`](#open_session) | Opens a session |
| [`tools`](#tools-2) | Lists the catalog or one tool |
| [`call`](#call) | Runs one catalog tool |
| [`close_session`](#close_session) | Ends the session and its engine |

`session` may be left out while exactly one session is open. See
[Several sessions](#several-sessions).

#### `open_session`

Without `attach`, starts a new attempt using the config, engine, and app
command for the target. The app opens at its URL or starts fresh on a
device from the [pinned app](https://e2e.com/docs/mobile#point-at-your-app). It installs
no build, so a local device target needs `app.bundleId`. Returns the session
id, the catalog, and one line that names the first screen: its revision, path,
and node count. [`observe`](#observe) lists the nodes, and action results
report changes against that screen. The catalog in the reply gives the single
actions by name and arguments only; [`tools`](#tools-2) describes each. The summary
tells the agent to read the `writing-tests` guide (`e2e://guide/writing-tests`) before it
writes or edits a test.

The summary names the app's URL, with the port a free-port `url` was
assigned, and the app command: its executable, readiness URL, `env` and
`inheritEnv` names, and log file. It prints no `args`, no `env` values, and
no URL userinfo or query. e2e starts no other app process, so a database the
app uses runs outside it.

```text wrap
App: http://127.0.0.1:57162/ (port 57162, assigned for the declared port 0).
Process: app.command "node" (ready at http://127.0.0.1:57162/; env PORT, DATABASE_URL; log .e2e/app.log). e2e starts no other app process for this session; a database or other service the app uses runs outside e2e.
```

```txt
open_session {target: "web", headed: true}
```

| Input | Type | Required | Does |
| --- | --- | --- | --- |
| `target` | string | no | The target to open. Falls back to `--target`, then the sole target. |
| `config` | string (path) | no | The config for this session, absolute or relative to the server's working directory (`cwd` in the client's MCP config). Falls back to `--config`, then normal `e2e run` discovery. |
| `headed` | boolean | no | `true` shows this session's browser or simulator, `false` hides it. Without it the session follows `--headed`. |
| `path` | string | no | Where on the app URL to open instead of its root, such as `/settings/members`. The session opens on that page. Not with `test`. |
| `test` | string | no | One test to run first, named like an `e2e run` positional. The session stops where the test stopped. See [Running a test](#running-a-test). |
| `engine` | string or object | no | The session's engine in a directory with no config file, as a package name or `{package, export, options}`. See [Without a config](#without-a-config). |
| `app` | object | no | The app under test for an `engine` session, written as a target's [`app`](https://e2e.com/docs/reference/config#the-app-under-test). |
| `attach` | string | no | ID from the pause header of a [held test](#attaching-to-a-held-test). Pass only this field. |

#### `tools`

Returns the catalog, one line per tool.

```txt
tools {tool: "locate"}
```

| Input | Type | Required | Does |
| --- | --- | --- | --- |
| `tool` | string | no | A catalog tool. Returns its full description and argument schema. |
| `session` | string | while several are open | The session id. |

#### `call`

Runs one catalog tool.

```txt
call {tool: "tap", args: {target: "n42"}, session: "<id>"}
```

| Input | Type | Required | Does |
| --- | --- | --- | --- |
| `tool` | string | yes | A catalog tool name. |
| `args` | object | no | The tool's arguments. |
| `session` | string | while several are open | The session id. |

- The tool's arguments can also sit beside `tool`, without `args`:
  `call {tool: "tap", target: "n42"}`. Passing both is `INVALID_ARGUMENT`.
- Arguments are validated against the tool's closed schema first. A wrong
  type, or an argument a tool that declares arguments does not declare, is
  `INVALID_ARGUMENT`, and nothing runs. `back` and `dismiss_keyboard` take no
  arguments; they drop any they are given and run.
- An action that fails is an error result that still carries the screen it
  re-observed. It leads with the tool, its target, and the code:
  `navigate file:///etc/passwd failed: POLICY_DENIED: forbidden URL scheme: file:`.
- Project tools honor their AI SDK `toModelOutput` renderer, including when
  `execute` returns a string. Rendered `error-text` and `error-json` outputs
  are MCP error results (`isError: true`).

#### `close_session`

Saves a recording still running, ends the attempt, and disposes the engine.
It stops app processes and services when the session is their last user.
Use it on an attachment to detach and leave the test held.

```txt
close_session {session: "<id>"}
```

| Input | Type | Required | Does |
| --- | --- | --- | --- |
| `session` | string | while several are open | The session id. |

### The catalog

What `call` can run depends on the session's target.

| Catalog tool | Description |
| --- | --- |
| [`observe`](#observe) | Returns a fresh observation |
| [Actions](#actions) | Act on an observed node |
| [`dismiss_keyboard`](#dismiss_keyboard) | Hides the on-screen keyboard |
| [`locate`](#locate) | Tries a semantic locator |
| [`run_code`](#run_code) | Runs JavaScript on the test API |
| [`test_lines`](#test_lines) | Returns the session's test lines |
| [`screenshot`](#screenshot) | Returns the masked viewport, optionally saved to a file |
| [Point tools](#point-tools) | Act at a screenshot point |
| [`start_recording`, `stop_recording`](#recording) | Records a video of the app |
| [Project tools](#project-tools) | Your `defineTool` tools |

#### `observe`

A fresh observation, one node per line as `#id role "name" ...`, with the
current path and revision. Takes no arguments.

- A link's `href` is its path when it is on the app URL's origin, else
  origin and path.
- `mailto:`, `tel:`, and `blob:` targets keep their scheme.
- `…` marks what is left out: `?…` a query, `#…` a fragment, `data:…` or
  `javascript:…` an inline payload, a trailing `…` a target cut at 256
  characters.

#### Actions

`tap`, `double_tap`, `long_press`, `right_click`, `hover`, `type`, `press`,
`select`, `check`, `scroll`, `scroll_to`, `drag`, `upload`, `navigate`,
`back`, `resize_viewport`, `switch_tab`, `close_tab`, `type_secret`. Each returns the action's summary, the
[test line](#test_lines) that repeats it or why there is none, and what
changed on screen.

| Input | Does |
| --- | --- |
| A node id | From the newest observation. An id only an earlier observation listed is found again on the newest one when exactly one node there reads the same; otherwise the action fails with `LOCATOR_NOT_FOUND`. |
| The value, key, `checked` state, direction, drop target, project-relative or `sample:` `files`, URL, `width` and `height`, or secret name | What the verb needs. |
| `modifiers` (optional) | On `tap`, `double_tap`, and `right_click`, when the engine declares `tapModifiers`: a list of `Shift`, `Control`, `Alt`, `Meta`, `ControlOrMeta`, held for the click. |

- A verb the engine does not declare is not in the catalog.
- `upload` refuses a path outside the project, a hidden file, or a file
  under a hidden directory with `POLICY_DENIED`. A `sample:` file has no
  test line: save a file in the project for one.
- `type_secret` is listed when the config declares `credentials` or
  `secrets` and the engine declares the `fill` action. The plaintext never
  reaches the agent as text; see
  [screenshots after a secret fill](https://e2e.com/docs/reference/security#screenshots-after-a-secret-fill).

#### `dismiss_keyboard`

Hides the on-screen keyboard. Takes no arguments. Listed when the engine's
keyboard declares `dismiss`, as the device engines do.

#### `locate`

Returns how many nodes match a semantic locator, which ones, and the
`screen.*` call to write.

| Input | Does |
| --- | --- |
| `role` (with optional `name`), `text`, `label`, `placeholder`, `testId` | Exactly one of them. |
| `exact` | Exact matching; `false` for substring, case-insensitive. |

```text wrap
0 nodes match getByLabel("Email").
A test using this locator would fail with LOCATOR_NOT_FOUND. ...

1 node matches getByRole("button", name: "Sign in").
Use: screen.getByRole("button", "Sign in")
```

- Zero or several matches name the error a test would get.
- A `name` matches the accessible name only. `observe` shows a `status`,
  `alert`, or `listitem` by its text, so one of those with a name that
  matches nothing also points at `getByRole(role).filter({ hasText })` and
  `getByText`.
- What it shows of a node is redacted, as in `observe`: a filled secret
  reads `<secret:name>`.

#### `run_code`

Runs JavaScript on the live session and returns what the code returned, what
it logged, each line it ran on the app, and what changed on screen.

```txt
call {tool: "run_code", args: {code: "const rows = screen.getByRole('listitem');\nconst prices = (await rows.allTextContents()).map((text) => Number(text.match(/\\$([\\d.]+)/)[1]));\nawait expect(rows).toHaveCount(12);\nreturn prices;"}}
```

| Input | Does |
| --- | --- |
| `code` | The body of an async function. `screen`, `app`, `expect`, `credentials`, `secrets`, `services`, `console`, and the engine's fixture (`browser`, `device`, `desktop`, or `electron`) are in scope, in a session attached to a held test too. |
| `timeout` | Optional. How long each action or matcher on `screen` waits by default, in ms, 1 through 120000. |

```txt
Returned: [12.5,18,24]
Test lines: 2, 1 read not listed.
1. await expect(screen.getByRole('listitem')).toHaveCount(12);
```

The result marks a line that fails:

```text wrap
Failed: LOCATOR_NOT_FOUND: locator matched no nodes within getByRole("button", name: "Save")
Test lines: 2, 1 failed.
1. await screen.getByLabel('Full name').fill('Grace Hopper');
2. failed: await screen.getByRole('button', 'Save').tap();
   LOCATOR_NOT_FOUND: locator matched no nodes within getByRole("button", name: "Save")
```

- The code runs in the server's Node process with the objects a test body
  gets. A call waits, matches, and fails as it does in a test, and a
  function you pass, such as a `browser.route` handler, is called like a
  test's.
- `return` a JSON value to get it back. `console.log` lines come back too.
- A line that fails throws the error a test gets, with its `code`. Uncaught,
  it stops the code and the result is an error result. The app stays where
  the passing lines left it.
- `await` each line. Lines without `await` don't run in order, so a read
  can finish before the tap above it. A failure nothing awaited, a promise
  the code left to reject, or an error a `setTimeout` callback throws fails
  the call. An error a handler leaves after its call ended is a warning in
  the server's log.
- Code still running after the call returns, such as an async function the
  code started and didn't await, keeps acting on the app. Those actions are
  not in the result or the draft.
- `expect.soft` keeps its failures for the end of the call, which then fails
  with them.
- The result lists the lines that ran. A read shows its value there when the
  code returns nothing and logs nothing. Otherwise `return` the value.
- The lines that pass, except reads, join the [`test_lines`](#test_lines)
  draft in the order they ran. A read is a call on `screen` or a locator
  that resolves with a value. A call on `app` or the engine's fixture is
  drafted whatever it returns. An object a call
  resolves with, such as a new tab, gets a `const` line that names it. A
  line that passes a function the server calls, such as a route handler, is
  drafted with the function's text and a comment: declare the variables it
  uses in the test. A passing `expect(value)` leaves no line.
- Each action or matcher on `screen` waits `timeout` by default. A `timeout`
  in a line's own options replaces it. Without `timeout`, each waits 5000 ms,
  or the config's [`actionTimeout`](https://e2e.com/docs/reference/config#actiontimeout) or
  [`assertionTimeout`](https://e2e.com/docs/reference/config#assertiontimeout) when that is
  shorter. `app` and the engine's fixture wait as they do in a test.
- `await import()` takes a Node built-in or a file's absolute path. A
  relative path resolves from e2e's own install, not the project.
- The whole call may run 5 minutes, the lines it left running included. Code that never yields, such as
  `while (true) {}`, blocks the server.
- The result shows `<secret:name>` where a configured secret value is.

The code is not confined. It has the authority of a test file: it can
import modules, read files, and read the values the app shows, a secret
included. `run_code` is on by default: every `e2e mcp` server offers it, no
flag turns it off, and a client that approves `call` approves `run_code`
with it. See [Security](https://e2e.com/docs/reference/security).

| Throws | When |
| --- | --- |
| `INVALID_ARGUMENT` | Code that doesn't parse. The message gives the line, and nothing runs. |
| `ASSERTION_FAILED` | An `expect` that doesn't hold. |
| `POLICY_DENIED` | A read of a secure field, such as a password input. |

An error that is not the runner's, such as a `TypeError`, comes back by its
name with the line it was thrown on.

#### `test_lines`

Returns, in order, the test lines from session actions and passing
[`run_code`](#run_code) statements. Takes no arguments.

After `open_session {test}`, the lines continue that test from where it
stopped. After `open_session {attach}`, they belong at the held test's
pause point.

Each action's result carries its line after the summary:

```txt
Tapped #n31.
Test line: await screen.getByRole("button", "Sign in").tap();
```

- The locator comes from the target as the newest observation listed it,
  before the action runs. It is the first of role and name, label,
  placeholder, text, and test id that matches the target and nothing else
  on the live screen, as [`locate`](#locate) would report it.
- A locator that also matches other nodes is narrowed: to nodes that render
  (`{ visible: true }`), then under a container
  (`getByRole("listitem").filter({ hasText: "Offer B" })`), then by position
  (`.nth(1)`, with a comment saying so).
- A control with no name, label, placeholder, text, or test id is found by
  its role (`getByRole("spinbutton")`, with a comment), narrowed the same
  way when it is not the only one.
- When nothing reaches the target alone, the result says `No test line:`
  and why. A line that would fail is never printed.
- A failed action prints and records no line. The draft shows an action
  without a line as a `//` comment.

| Action | Line |
| --- | --- |
| Opening the session, without [`test`](#running-a-test) | `app.open()` |
| `tap`, `double_tap`, `long_press`, `right_click`, `hover` | `tap()`, `doubleTap()`, `longPress()`, `secondaryTap()`, `hover()`, with any `modifiers` |
| `type` into a node | `fill(value)`; a credential's username as `credentials.user(name).username` |
| `type_secret` | `fill(credentials.user(name).password)` or `fill(secrets.get(name))`, never the value |
| `press` on a node, once | `press(key)` |
| `select`, `check` | `selectOption(label)`, `check()` or `uncheck()` |
| `drag`, `upload` | `dragTo(locator)`, `setInputFiles(files)` |
| `scroll_to` a node | `scrollIntoView()` |
| `navigate`, `back` | `app.open(url)`, with a URL on the app's origin as its path; `app.back()` |
| `tap_at` where the engine takes a bare point | `screen.tapAt({ x, y })` in the viewport's CSS pixels, with a comment that a point holds only while the layout does |
| `tap_at`, `hover_at`, `type_at`, `press_at`, `select_at` on the listed control under the point | The control's line, as for `tap`, `hover`, `type`, `press`, and `select` by id |
| `drag_at` between two listed controls | `dragTo(locator)` |

No line: `type` or `press` with no target, `press` with `times` over 1,
`scroll`, `scroll_to` a text, `dismiss_keyboard`, `resize_viewport` (set the
size with the engine's built-in fixture, `browser.setViewport` on web), `switch_tab`
and `close_tab` (`browser.tabs()[tab - 1].activate()` and `.close()` on
web), a `type` or `type_at` the
node took through the keyboard, `hover_at` and `drag_at` at bare points
(no `screen` call repeats them), `type_at` and `press_at` on a point
where nothing is listed, and a [point tool](#point-tools) called before the
first screenshot.

#### `screenshot`

The masked viewport as an image.

| Argument | Type | Description |
| --- | --- | --- |
| `path` | `string` | Optional. Also saves the screenshot as a PNG file, resolved against the project root, and returns its absolute path. A path outside the project, a hidden file, or one under a hidden directory is `POLICY_DENIED`. |

```json
{ "tool": "screenshot", "args": { "path": "screenshots/checkout.png" } }
```

- From then on every action result carries a fresh screenshot.
- It masks secure fields only, after a secret fill too.
- The file is a second, full-resolution capture, masked as `app.screenshot`
  masks it. The attached image may be scaled down.

#### Point tools

Each takes `x` and `y` in the latest screenshot's pixels. `drag_at` takes its
drop point as `to_x` and `to_y`.

| Tool | Inputs | Does | Listed when the engine declares |
| --- | --- | --- | --- |
| `tap_at` | `x`, `y` | Taps the bare point through the engine's `performAt`. Without `performAt` the point must land on a listed control, which is tapped by id. | `tap` or `performAt` |
| `hover_at` | `x`, `y` | Hovers the same way: the bare point through `performAt`'s `hover` kind, otherwise a listed control under it by id. | the `hover` action or pointer kind |
| `drag_at` | `x`, `y`, `to_x`, `to_y` | Drags from the first point to the second through `performAt`'s `dragTo` kind. Without that kind the points must land on two different listed controls, which are dragged by id, or the call fails with `UNSUPPORTED_CAPABILITY`. | the `dragTo` action or pointer kind |
| `type_at` | `x`, `y`, `value`; `replace` on an engine with a keyboard | Fills a listed input under the point by id. With a keyboard, anything else is tapped to focus it and typed into, at the caret unless `replace` is set. Without one, a point on nothing listed fails. | `type` or a keyboard |
| `press_at` | `x`, `y`, `key` | Sends the key to a listed control under the point by id. With a keyboard, anything else is tapped to focus it and the key goes through the keyboard. | `press` or a keyboard |
| `select_at` | `x`, `y`, `value` (the option's visible label) | Picks the option in the listed select-like control under the point. Nothing listed there fails. | `select` |

<Note>
Before the session's first `screenshot`, each point tool does nothing and
answers with the line that says to take one.
</Note>

#### Recording

`start_recording` and `stop_recording` record a video of the app. Both are
listed when the engine records video.

```txt
call {tool: "start_recording", args: {name: "checkout-flow"}}
call {tool: "stop_recording"}
```

| Tool | Input | Does |
| --- | --- | --- |
| `start_recording` | `name` (optional): up to 64 letters, digits, `-`, and `_`, starting with a letter or digit | One recording at a time. A second start reports the one running. |
| `stop_recording` | none | Saves the video and returns each file's absolute path, or the URL where a provider keeps it. |

- A session records video only between `start_recording` and
  `stop_recording`, as many times as needed. The configured
  [`video`](https://e2e.com/docs/reference/config#video) mode applies to runs, not sessions, and
  a session writes no [trace](https://e2e.com/docs/reference/config#trace).
- Each recording is saved to `<output>/videos/<session>/<n>[-<name>].webm`
  (`.mp4` on mobile, and [`output`](https://e2e.com/docs/reference/config#output) is `.e2e` by
  default), where `n` counts the session's recordings.
- A web recording that spans a restart or a state reset has one file per
  page, `-part2`, `-part3`, and so on, in play order.
- A browser or device provider that records the lease itself records the
  session instead. [Kernel](https://e2e.com/docs/integrations/kernel) downloads its replay, saved
  here as `<n>[-<name>].mp4` like any other file. A provider that keeps its
  recordings is listed by URL instead of a file.
- `close_session`, the idle timeout, and the session TTL save a recording
  still running, and the close result lists its files.

<Warning>
Recordings are not masked: they are files for a person to watch, and no
tool returns their pixels. A secret typed into a plain input while
recording is readable in the video.
</Warning>

#### Project tools

Project tools in `agents.default.tools` follow in the catalog under their
own names, scoped to the target's platform. A mobile project that passes
`mobileTools(...)` also gets `open_app`, `swipe`, and `alert`, and one that
passes `foldTools(...)` gets `fold` for a foldable device.

| Throws | When |
| --- | --- |
| `INVALID_CONFIG` | A project tool named like a built-in, at config load. |
| `UNKNOWN_TOOL` | A name not in the catalog. The message lists the names. |

## Setup

The server ships with `e2e`. `e2e init` offers to register it in
`.mcp.json` (Claude Code), `.cursor/mcp.json` (Cursor), and
`.codex/config.toml` (Codex). Install dependencies and start the MCP client
in the project directory so the command uses the installed e2e version. Init
writes your package manager's command: `npx e2e mcp`, `pnpm exec e2e mcp`,
`yarn exec e2e mcp` (`yarn --silent exec -- e2e mcp` on Yarn 1), or
`bunx e2e mcp`. By hand:

<Tabs>
  <Tab title="Claude Code">
    <CodeGroup>
    ```bash npm
    claude mcp add --scope project e2e -- npx e2e mcp
    ```

    ```bash pnpm
    claude mcp add --scope project e2e -- pnpm exec e2e mcp
    ```

    ```bash bun
    claude mcp add --scope project e2e -- bunx e2e mcp
    ```
    </CodeGroup>
  </Tab>
  <Tab title="Codex">
    Run `e2e init --yes --skills none --mcp .codex/config.toml` with the
    installed CLI, or add the entry below. Codex reads the project's
    `.codex/config.toml` once the project is trusted. Start a new session
    after registering it. `codex mcp list`, run in the project, shows the
    registration.

    ```toml title=".codex/config.toml"
    [mcp_servers.e2e]
    command = "npx"
    args = ["e2e", "mcp"]
    ```
  </Tab>
  <Tab title="Cursor">
    Add the server to `.cursor/mcp.json`. Other clients that read an
    `mcpServers` map take the same block.

    ```json title=".cursor/mcp.json"
    {
      "mcpServers": {
        "e2e": { "command": "npx", "args": ["e2e", "mcp"] }
      }
    }
    ```
  </Tab>
  <Tab title="OpenCode">
    Add the server to `mcp` in the project's `opencode.json`.
    `opencode mcp list`, run in the project, shows it as connected.

    ```json title="opencode.json"
    {
      "$schema": "https://opencode.ai/config.json",
      "mcp": {
        "e2e": { "type": "local", "command": ["npx", "e2e", "mcp"] }
      }
    }
    ```
  </Tab>
</Tabs>

- **The plugin.** The [e2e plugin](https://e2e.com/docs/coding-agents#install-the-skill) for
  Codex and Cursor includes the server as `npx -y e2e@<version> mcp`, which
  runs the plugin's e2e release even when the project has another version
  installed. In Claude Code, or without the plugin, register the server in
  the project.
- **`Connection closed`.** When the client reports it and the command fails
  in a terminal with `EBADDEVENGINES`, the project's `devEngines` pins
  another package manager. Register that manager's command, such as
  `pnpm exec e2e mcp`.
- **Services.** The first session on a config file starts its
  [services](https://e2e.com/docs/services), and the last one to close stops them.

The server is also listed in the
[MCP Registry](https://registry.modelcontextprotocol.io) as
`io.github.e2e-inc/e2e`. A client that installs it from there runs the
listed release with `npx` and must start it in the project directory. The
project still needs e2e and its engine installed, since the config imports
them. A listed release on another version than the project's e2e can reject
config keys it does not know; the registration above always matches.

The server needs no model: a project without the optional `ai` package
opens sessions and drives the app with the deterministic tools. A test a
session [runs](#running-a-test) calls the configured model for an agent step
the replay cache cannot serve.

## Without a config

In a directory with no config file, `open_session` needs an `engine`. The
session has one target on that engine. Every other setting is at its
default, so the session has no credentials, secrets, or project tools.

```txt
open_session {engine: "@e2e-dev/web", app: {url: "http://localhost:3000"}}
open_session {engine: {package: "@e2e-dev/mobile", options: {platform: "ios"}}, app: {bundleId: "com.example.app"}}
```

To pass options, or to name an export, write `engine` as an object:

| Field | Type | Does |
| --- | --- | --- |
| `package` | string | The package to load. It can carry a subpath, such as `@e2e-dev/testmu/web`. |
| `export` | string | The export to call. Default: the default export. |
| `options` | object | The options the factory takes, as in an `e2e.config.ts`. |

- **A package name alone** calls the package's default export with no
  options. `@e2e-dev/web`, `@e2e-dev/mobile`, `@e2e-dev/desktop`, and
  `@e2e-dev/electron` export their factory this way. [Your own engine](https://e2e.com/docs/writing-an-engine#open-it-by-name) can do the same.
- **An option can be another factory call.** The server reads an option
  value as a call when it has `package` and no key other than `export` and
  `options`. It makes that call first and passes the result as the option.
  Use it to give an engine a device or browser provider:

  ```txt
  open_session {
    engine: {package: "@e2e-dev/mobile", options: {
      platform: "ios",
      device: {package: "@e2e-dev/limrun", export: "limrun", options: {region: "eu"}}
    }},
    app: {bundleId: "com.example.app"}
  }
  ```

  The call above builds
  `mobile({ platform: 'ios', device: limrun({ region: 'eu' }) })`. The
  provider reads its API key from the server's environment.
- **Lookup.** The server looks for each package in the project, then beside
  the running `e2e`, then in `~/.e2e/engines`. When it is in none of them,
  the error names the command that installs it for every project:
  `npm install --prefix ~/.e2e/engines @e2e-dev/web`.
- **`app`** takes every key a target's
  [`app`](https://e2e.com/docs/reference/config#the-app-under-test) does, such as
  [`launchArguments`](https://e2e.com/docs/reference/config#launcharguments),
  [`permissions`](https://e2e.com/docs/reference/config#permissions), and
  [`command`](https://e2e.com/docs/reference/config#command), and is checked the same way.
  `@e2e-dev/web` needs `url`. `@e2e-dev/mobile` and `@e2e-dev/desktop` need
  `bundleId` or `appPath`. `@e2e-dev/electron` needs `appPath`, or none for a
  packaged app named by `options: {executablePath}`.
- **Files.** The project root is the server's working directory, and the
  session writes `.e2e/` there.
- **Tests.** `e2e run` and `open_session {test}` need a config. To keep
  the session's test lines as a test, write a config with
  [`e2e init`](https://e2e.com/docs/reference/cli#e2e-init).
- **Several sessions.** Sessions opened with `engine` run side by side. A
  session on a config doesn't open beside them and fails with
  `CONFIG_IN_USE`.

| Throws | When |
| --- | --- |
| `CONFIG_NOT_FOUND` | `open_session` without `engine`, and the directory has no config. |
| `ENGINE_NOT_INSTALLED` | A package that `engine` names is in none of the three lookup places. |
| `INVALID_ARGUMENT` | `package` is not a package name, `export` is not an export name, `engine` comes with `target`, `config`, or `test`, or the directory has a config. Also `app` without `engine`. |
| `INVALID_CONFIG` | The named export is not a function, a factory refused its `options`, the engine's factory returned no engine, or the engine refused `app`. For an export that is not a function, the message lists the package's functions. |

## Output

- Operator output goes to stderr, prefixed `e2e mcp:`. Stdout carries only
  the protocol.
- What your code writes through `console`, `process.stdout`, or
  `process.stderr` (the config's top-level code, project tools, a test a
  session runs) goes to stderr with secret values redacted.
- Output printed while a config loads appears once it has loaded. If the
  load fails, that output is withheld, since the config's secrets are
  unknown.
- A promise a session's test leaves rejected fails that test, as in a run.
  One that rejects after the test's body ended is an `UNHANDLED_REJECTION`
  line in `close_session`'s result, and a warning on stderr once the session
  has closed. Any other error nobody caught, a rejection before any session
  has run a test included, is logged redacted and shuts the server down.

<Note>
A child process that inherits the server's stdio writes past this.
</Note>

## Sessions

A session is one attempt on one target. A `call` before any session is open
fails with `NO_SESSION`.

A session closes:

- on `close_session`,
- after 30 idle minutes,
- after 4 hours,
- when the client disconnects,
- on SIGINT or SIGTERM.

The idle and lifetime limits apply to sessions that open their own attempt.

The client disconnects when it closes the server's stdin or dies. Then every
session closes, the app commands the sessions started stop, and the server
exits.

Sessions enforce what tests enforce:

- Node ids are resolved against the newest observation, as
  [Actions](#actions) describes. A secret fill checks the node it found, as
  for any fill.
- The single actions fill a secret only through `type_secret`.
  [`run_code`](#run_code) fills one as a test does.
- Once the config has loaded, its secret values are redacted from every text
  a tool returns, failures and cleanup lines included, and from the server's
  own log lines.
- Nothing a session does is recorded as a test result. Only a test the
  session runs reads or writes the replay cache
  ([Running a test](#running-a-test)). Its artifacts land under
  `<output>/results/mcp/<target>-<attemptId>/`, its videos under
  `<output>/videos/`. A later `e2e run` clears `<output>/results`; the videos
  stay.
- A queued call cancelled by the client does not execute when its turn
  arrives.

## Attaching to a held test

Use the ID in the pause header. It stays the same across holds of one
attempt. Start MCP from the run's directory with the same e2e version.

```txt
open_session {attach: "k3f9"}
```

Pass only `attach`. An attached session has the same tools as a normal
session on that target. Use `start_recording` and `stop_recording` to save
a video under `<output>/videos/<session>/`. If the run already records
the attempt, `start_recording` returns an error with the run's video
directory.

Use `close_session` to detach and leave the test held. The reply includes
the path of any recording saved on detach.

Press Enter or run `npx e2e run --step next` to resume the test and end the
attachment right away. Any recording still running saves in the background.
The runner logs the saved path or the error. The MCP server logs the same
result if it's still connected.

[`run_code`](#run_code) gets the engine's fixture over the held test's app,
as a normal session's code does, so a desktop window's `activate()` there
changes the window the test reads after it resumes.

The report and trace record the test's own steps. App logs and screen
updates from an attached call can appear in the trace if they arrive after
the call ends.

## Running a test

`open_session {test}` runs one test of the project on the session's attempt,
then hands the session over on the screen where the test stopped.

```txt
open_session {test: "tests/checkout.e2e.ts:12"}
```

A failed test opens the session with its failure:

```text wrap
Test "a member checks out" (tests/checkout.e2e.ts:12) failed after 30.25s; 8 steps ran.
LOCATOR_NOT_FOUND: locator matched no nodes within getByLabel("Name")
At tests/checkout.e2e.ts:20:3
On screen, closest to what the locator asked for:
  #n32 textbox "Full name"
```

- **Selection.** `test` takes what `e2e run` takes as a positional: a file
  with `:line` for the test declared on that line, a file, a directory, a
  glob, or a name. It must select exactly one test on the session's target.
  The line is the one the `test(` call is on; a line inside the test
  selects nothing.
- **What runs.** The test's `beforeAll` hooks, its `test.extend()` fixtures,
  its `beforeEach` hooks, and its body, with the same fixtures, locators,
  `expect`, `timeout`, `actionTimeout`, and `assertionTimeout` as in a run.
  The session does not open the app URL or launch the pinned app first; the
  test opens the app itself.
- **Where it stops.** At the first failure, or at the end of the body. Nothing
  is torn down. The opening text leads with the outcome, as above, with the
  last 15 steps of a failed test. The session summary, the catalog, and the
  line that names the screen follow.
- **Test lines.** The test's own steps print no [test line](#test_lines).
  The session's lines start with the agent's first action, and `test_lines`
  returns them as the lines that continue the test.
- **Teardown.** `afterEach` hooks, fixture teardowns, and `afterAll` hooks
  run at `close_session`, or when the session closes on its own. Their
  failures show as `Cleanup (afterEach)` and `Cleanup (afterAll)` lines.
- **Rerun.** `close_session`, then `open_session {test}` again. Each session
  loads the config and the test files fresh, files they import included.
- **Replay cache.** The test reads and writes the cache as a run's first
  attempt: steps replay, and a new recording is saved once a later check
  passes. A failure evicts the entries no check confirmed. The cache settles
  when the body ends, so an `afterEach` check confirms nothing.
  [`cache.strict`](https://e2e.com/docs/reference/config#cache) holds as in a run.
- **Not part of it.** No test result, report, or trace page. Retries, and the
  test's `trace` and `video` modes, don't apply. The failure screenshot and
  `screen-at-failure.txt` land in the session's artifacts directory.
- **Screenshots.** Each session counts as a run of its own for
  `toHaveScreenshot`: a baseline one session writes is compared against in
  the next.
- **Several sessions.** Test code in two sessions takes turns: one session
  runs its test or its teardown at a time. An open cancelled while its test
  waits for its turn runs none of
  the test's code.

| Throws | When |
| --- | --- |
| `NO_TESTS` | `test` selects no runnable test. The message names the cause, as for `e2e run`. |
| `TEST_AMBIGUOUS` | `test` selects several tests. The message lists them as `file:line`. |
| `SESSION_TEST_UNSUPPORTED` | The test is in a serial group, starts from a setup test's `session`, or runs as several agents. |
| `COLLECTION_ERROR` | A test file fails to collect, as for `e2e run`. |

## Running tests beside a session

A session can stay open while `e2e run` runs the suite, so the agent can go
to a failing screen afterward. On web, whether the two can run at once
depends on the target's `app`:

| Target's `app` | `e2e run` while a session is open |
| --- | --- |
| No `command` | Both share the server you started yourself. |
| `url` on port [`0`](https://e2e.com/docs/web#let-the-runner-pick-the-port) | Each starts its own server. Servers like `next dev` don't allow two instances in the same directory. |
| `command` with [`reuseExisting: true`](https://e2e.com/docs/web#starting-your-app) | The run reuses the session's server. Closing the session stops that server. |
| `command` on a fixed port without `reuseExisting` | Fails because the port is already in use. |

- In CI, e2e ignores `reuseExisting`, so a fixed port behaves like the last
  row.
- Close the session before a run that would collide with it on the port or
  on a mobile device.

## Several sessions

Several sessions can be open at once, up to `--max-sessions`, each on its
own attempt and its own browser or device. Parallel agents, such as a
coding agent's subagents, share one server this way: each opens a session
and passes its id to every `tools`, `call`, and `close_session`.

| Throws | When |
| --- | --- |
| `SESSION_REQUIRED` | A call without `session` while several are open. The message lists the open sessions. |
| `NO_SESSION` | A `session` id that is not open. For a session that is closing or ended, the message says why. |
| `SESSION_OPEN` | An `open_session` past the limit, or while the server shuts down. |
| `CONFIG_IN_USE` | An `open_session` on another config while sessions are open, or on a config beside sessions opened with `engine`. |
| `ENGINE_IN_USE` | A second session on an engine created inside a package. |
| `CANCELLED` | An `open_session` the client cancelled or disconnected from, or the server shut down during, once what it started has stopped. |

- A session holds its slot from `open_session` until its attempt has closed,
  so sessions still booting or still closing count toward the limit.
- Sessions open at once share one config.
- Each session loads the config afresh, down to the files it imports by
  path, such as a base config it spreads, so each gets engine instances of
  its own. A session that runs a test loads the test files the same way. An engine created inside a package is one instance every session
  shares.
- Sessions on one app command share its process: the first session starts
  it, and it stops when the last of them closes. A URL declared with port
  `0` gets its own port in each session, and so its own process.
- On mobile, each session needs its own simulator or emulator: declare one
  target per device, each naming its `device`, and open each session on its
  own target.

## Resources

| URI | Content |
| --- | --- |
| `e2e://guide` | The agent skill's overview, the text `e2e guide` prints. |
| `e2e://guide/{topic}` | One topic: `setup`, `writing-tests`, `agent`, `running`, `explore`, `debugging`, `mcp`, `bug-bash`. |

## Exit codes

| Code | When |
| --- | --- |
| `0` | The client disconnects, with or without a session open, or a signal ends it. |
| `1` | An uncaught error shuts the server down. |
| `2` | The command line is invalid. |
| The error's [category code](https://e2e.com/docs/reference/cli#exit-codes) | A startup failure. |

Config and engine failures come back as tool errors with the same codes the
CLI prints.
