# MCP

Covers: `poema-mcp`, the MCP server that lets Claude Code or another MCP client work in instances: its install, its tools
and what it never does.

For: developers who give a coding agent instances to work in, and agents that read their own tool list.

`poema-mcp` is an MCP server that lets Claude Code, or another MCP client, create and work in instances: it makes them,
runs commands and edits files in them, suspends them when idle and forks them to try several approaches. It is how an
agent on your machine drives poema cloud, and not the MCP servers an agent running in an instance uses, which are set on
New instance ([Deploy from git](https://client-test.poema.dev/docs/git.md)).

## Set it up

The Python SDK installs `poema-mcp` ([Quickstart](https://client-test.poema.dev/docs.md)). It reads the same variables as the SDK.

With an API key:

For Claude Code:

```
claude mcp add poema -e POEMA_URL=https://api.client-test.poema.dev -e POEMA_API_KEY=poema_ns_... -- poema-mcp
```

Any other MCP client runs the same command, over stdio, with the same two variables.

With poema login:

With the CLI installed too, sign in once instead, and give it no variables: it reads the key `poema login` keeps.

```
poema login --url https://client-test.poema.dev
claude mcp add poema -- poema-mcp
```

It reads that key when it starts: after the next `poema login`, restart the MCP server.

Claude Code has to find `poema-mcp`: put `~/.venvs/poema/bin` on its `PATH`, or give the full path.

## Tools

The tools, grouped by what they do. Each answers a failure with its error, for the client to act on.

### Make and manage instances

- `sandbox_create` makes a new instance and waits until it runs, up to 2 minutes: a name, and optionally an `image` (poema's base image
  unless given), `workspaces`, a `gateway`, a `model`, `env` (its environment variables, names to values), `labels` (on
  its Task), `timeout_seconds` (it is suspended that many seconds after it starts, as `spec.timeoutSeconds`, at most your plan's
  session length, which also applies when none is given), a `host`
  of poema's fleet to run on, which it waits for and is never moved from, and `allowed_tools`, the tools it may call on
  its workspaces' MCP servers by server name (the workspaces that declare one are copied for it, and deleted with it).
  It refuses a name an instance already has. Without a gateway an instance has no network; `default` reaches package
  registries and git hosts. One that cannot start within 2 minutes is an error saying why; the instance stays and starts
  by itself once it can, so `sandbox_delete` it if you no longer want it.
- `sandbox_suspend` suspends an instance and waits until it is: it stops using compute, and its memory and files are kept.
- `sandbox_resume` wakes it and waits until it runs.
- `sandbox_fork` forks an instance into new ones, 1 to 100 names, each running on a copy of its `/workspace` (with
  `memory`, of its memory too), and waits until they run. The instance is suspended first and stays so until resumed.
  Forks count toward the instances you may run at once.
- `sandbox_set_timeout` suspends it `seconds` from now (from when it next starts, if it is not running), at most your
  plan's session length; 0 sets that length.
- `sandbox_delete` deletes an instance and its workspace.
- `task_start` makes a new instance that runs its own command, an agent such as `claude -p "..."`, with what
  `sandbox_create` takes. It needs no timeout: your quota caps what it spends. When its command ends it is suspended, and
  `sandbox_status` and `sandbox_logs` give its exit code and the last 16 KiB of its output. Such an instance is never
  forked, and once its command has ended `sandbox_resume`, `sandbox_run` and the file tools refuse it, as a resume may
  run its command again. One that cannot start within 2 minutes is an error saying why; the instance stays and starts
  by itself once it can, so `sandbox_delete` it if you no longer want it.

### Commands and files

- `sandbox_run` runs a shell command (`bash -lc`) to its end, in `/workspace` unless given a `cwd`, with no stdin, and
  returns its exit code and output. One still running after its timeout, 120 seconds unless given, is killed and
  returns its output so far.
- The command and file tools need an instance made with Access from your machine (`debug: true`); those
  `sandbox_create` and `task_start` make always have it.
- `sandbox_read_file` and `sandbox_write_file` read and write a file, the write making its directories.
- `sandbox_list_files` lists the files and directories under a `path`, `/workspace` unless given, to a `depth` of
  levels, 1 unless given: a line each, a directory's path ending in `/` and a file's followed by its size in bytes.

### Read what is there

- `sandbox_list` lists the instances and their phases.
- `sandbox_status` says an instance's phase and why (its conditions' messages), its image, gateway and model, when its
  timeout suspends it, a line each, then its last 10 events.
- `sandbox_logs` returns the last `tail` lines its own command printed, 200 unless given.
- `workspace_list`, `gateway_list` and `model_list` list your organization's, with no key, no user in a URL, no server arguments
  and no environment values. `quota_get` shows your limits and what is used, and `usage_get` your spend (optionally from a
  `start` day to an `end` day).
- `mcp_server_tools` lists the names of the tools of an MCP server that one of your workspaces declares, so that you can
  choose `allowed_tools`; never the server's own descriptions.

### Annotations

Each tool carries MCP's tool annotations for the client. `sandbox_list`, `sandbox_status`, `sandbox_logs`,
`sandbox_read_file`, `sandbox_list_files` and the views above are read-only. `sandbox_run`, `sandbox_write_file` and
`sandbox_delete` are destructive, as they can overwrite or remove files. Of the tools that change something, all but
`sandbox_run`, `sandbox_set_timeout` and `task_start` are idempotent. `task_start` is marked destructive, not idempotent and open-world: its command can
reach whatever its Gateway allows, so a client asks you first. `mcp_server_tools` is open-world too, as it asks an MCP
server outside poema. The other tools reach poema's instances only.

## What it never does

> **Note:** An agent reads text that can steer it, so `poema-mcp` offers less than the CLI.

It works on your organization's instances, including those made on the dashboard, and can read anything inside one
through `sandbox_run`. The agent itself chooses `allowed_tools` for the instances it makes. It never makes or shows a key, never changes who has access, a plan, a limit or money, and never
widens which sites an instance may reach: a person does those on the dashboard or in the CLI ([Team and access](https://client-test.poema.dev/docs/team.md),
[Network rules](https://client-test.poema.dev/docs/network.md)). It opens no terminal, port forward or SSH.

Last checked: unreleased checkout.
