

# Run the tool runner in a self-hosted environment

A session's tools can run on a machine you own instead of an AgentCore Runtime. You start the tool runner yourself, give it the session's activation, and it connects out to the service. It doesn't listen on any port, so nothing else on the machine or the network can drive it.

You can [run it in a container](#claude-managed-agents-bedrock-self-hosted-run-it-in-a-container) or [directly on the machine](#claude-managed-agents-bedrock-self-hosted-run-it-directly-on-your-machine). Use the container unless you have a reason not to.

S3 folders and files need an AgentCore Runtime. A self-hosted session that names one is refused when it is created. See [Use S3 folders and files](claude-managed-agents-bedrock-s3-folders-and-files.md).

**How it works**

1. You create a session whose `environment_id` is your project's self-hosted ID.

1. You start a tool runner with the session's activation. It signs its connection with a role in your account and connects out to the service.

1. The agent loop runs in the service. For each tool call, the service sends the call to your tool runner, which runs it on your machine and returns the result.

## What the model's commands can reach
<a name="claude-managed-agents-bedrock-self-hosted-what-the-model-s-commands-can-reach"></a>

The tool runner runs each command the model asks for as its own user, with no sandbox around it. The command gets that user's files, network, and credentials. On your own machine that can include your `~/.aws` directory, your SSO cache, your SSH keys, and anything else your user can read. The tool runner removes the AWS credential variables from each command's environment, but that is not a boundary. A command can still read the files those variables would have pointed to.

So give the tool runner nothing you would not give the model:
+ **Run it where your files are out of reach.** A container that mounts nothing from your home directory does that. So does a separate OS user with its own home directory.
+ **Sign its connection with a role that may only connect.** A command can read whatever credentials you give the tool runner, so give it a role with only `bedrock:CreateAnthropicSessionConnection`, as shown in [What you need](#claude-managed-agents-bedrock-self-hosted-what-you-need).
+ **On Amazon EC2, keep containers away from the instance's role.** See [On Amazon EC2](#claude-managed-agents-bedrock-self-hosted-on-amazon-ec2).

## What you need
<a name="claude-managed-agents-bedrock-self-hosted-what-you-need"></a>
+ The session role and the caller policy from [Set up permissions and prerequisites](claude-managed-agents-bedrock-account-setup.md). A self-hosted session still uses the session role for its model calls. It needs no repository and no AgentCore Runtime.
+ The tool runner bundle. `bin/` has `cma-tool-runner-linux-arm64` and `cma-tool-runner-linux-x86_64`, for Linux with glibc 2.28 or newer, and `cma-tool-runner-macos`, a universal binary for Apple silicon and Intel Macs. The macOS build is not notarized. After a download, check that `shasum -a 256 bin/cma-tool-runner-macos` matches the value in `bin/BUILD-INFO`, then run `xattr -d com.apple.quarantine bin/cma-tool-runner-macos` on that file only.
+ A role for the tool runner's connection. The CloudFormation template creates one, `CmaSelfHostedToolRunnerRole`, when you deploy it with `CreateSelfHostedRole=true`. To make it by hand, create a role whose trust policy lets your account assume it, and give it only this policy:

```json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": "bedrock:CreateAnthropicSessionConnection",
    "Resource": "arn:aws:bedrock:us-west-2:111122223333:project/*/anthropic-session/*"
  }]
}
```

To let one tool runner connect to one session only, name that session in `Resource` instead of `*`, or pass a session policy that does when you assume the role.

Add a profile for the connection role:

```bash
# ~/.aws/config
[profile cma-runner]
role_arn = arn:aws:iam::111122223333:role/CmaSelfHostedToolRunnerRole
source_profile = <the profile you normally use>
region = us-west-2
```

## Create the session and its activation
<a name="claude-managed-agents-bedrock-self-hosted-create-the-session-and-its-activation"></a>

1. Find the project the session will be in. Get your agent (`GET /agents/{id}`) and drop the last two segments of its `resource_arn`. What remains is the project ARN, such as `arn:aws:bedrock:us-west-2:111122223333:project/default`. The last segment is the project ID.

1. Create the session with the project's self-hosted environment ID, which is the project ARN followed by `/anthropic-self-hosted`:

   ```
   POST /sessions
   {
     "agent": "agent_...",
     "environment_id": "arn:aws:bedrock:us-west-2:111122223333:project/default/anthropic-self-hosted"
   }
   ```

1. Ask for the tool runner's activation and save the answer to `activation.json`:

   ```
   POST /sessions/{session_id}/bedrock/attachment
   { "workspace_directory": "/mnt/workspace" }
   ```

`workspace_directory` is where the file tools work and where `bash` starts. Leave it out, and the activation names the `working_directory` that you gave the session when you created it, which is the directory the model is told it works in, or `/workspace` if you gave none. In a container, pass `"workspace_directory": "/mnt/workspace"`, the directory the model is told it works in by default. The image has both `/mnt/workspace` and `/workspace`, and if you leave both fields out, the tools start in `/workspace` while the model is told `/mnt/workspace`. On your machine, give the session a `working_directory` that you made for it, so the model is told the same directory that the tool runner works in. A session created with `working_directory` set to `/mnt/workspace`, the default, is read as one that set none, so name `/mnt/workspace` in `workspace_directory` here if that's where it should work. The answer is the whole activation:

```json
{
  "action": "activate",
  "protocol_version": 1,
  "environment_id": "arn:aws:bedrock:us-west-2:111122223333:project/default/anthropic-self-hosted",
  "session_id": "sesn_...",
  "attachment_generation": 1,
  "endpoint": "https://bedrock.us-west-2.amazonaws.com/anthropic",
  "region": "us-west-2",
  "service": "bedrock",
  "workspace": { "directory": "/workspace", "capability_directories": [] },
  "project_id": "default",
  "attachment_token": "cmat_..."
}
```

`attachment_token` is what lets this tool runner, and no other, connect to the session. The service shows it only in this answer, so keep the file where the model's commands cannot read it, and delete it when the session ends. The call needs `bedrock:CreateAnthropicSessionAttachment` on the session.

Each call attaches a new tool runner and makes any earlier activation stale. Call it again to replace a tool runner, for example one whose credentials expired. The earlier tool runner gets no more tool calls from then on, and calls wait for the new one to connect. When it next hears from the service, it stops and exits with 1.

Start one tool runner per activation. If a second one is started with the same activation, the two can trade the session for a while, and a call in flight at a switch can run on both. One of them stops and exits with 1 once the service sees both connected to it.

Start the tool runner before you send the session its first message, so it is connected when the first tool call arrives. One tool runner serves one session. Once it is running, send messages and read the results as in [Run your first agent](claude-managed-agents-bedrock-first-agent.md), from Step 4 (Watch the session) onward.

## Run it in a container
<a name="claude-managed-agents-bedrock-self-hosted-run-it-in-a-container"></a>

### Build the image
<a name="claude-managed-agents-bedrock-self-hosted-build-the-image"></a>

The image is the same one AgentCore runs, built for your machine's architecture. Run the block for your machine from the tool runner bundle's folder. On Apple silicon or ARM64 Linux:

```bash
mkdir build && cd build
cp ../LICENSE ../NOTICE ../THIRD-PARTY-NOTICES ../image/Dockerfile .
cp ../bin/cma-tool-runner-linux-arm64 cma-tool-runner
chmod 755 cma-tool-runner
mkdir workspace
docker build --pull -t cma-tool-runner-local .
```

On an Intel Mac or x86-64 Linux, which also changes the base image's platform:

```bash
mkdir build && cd build
cp ../LICENSE ../NOTICE ../THIRD-PARTY-NOTICES .
cp ../bin/cma-tool-runner-linux-x86_64 cma-tool-runner
chmod 755 cma-tool-runner
mkdir workspace
sed 's|--platform=linux/arm64|--platform=linux/amd64|' ../image/Dockerfile > Dockerfile
docker build --pull -t cma-tool-runner-local .
```

`--pull` fetches the latest Amazon Linux 2023 image, whose CA bundle the `Dockerfile` copies. Add any tools your agents need, such as `git` or `python3`, to the `Dockerfile` before you build. If you install them with `apt-get`, put that `RUN` line before the `COPY --from` line: apt can install Debian's ca-certificates with them, which replaces the copied bundle.

### Start it
<a name="claude-managed-agents-bedrock-self-hosted-start-it"></a>

The image's entrypoint starts the AgentCore mode, so override it to start the self-hosted mode, which reads the activation from stdin:

```bash
eval "$(aws configure export-credentials --profile cma-runner --format env)"

docker run -i --rm \
  -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN \
  -e AWS_REGION=us-west-2 \
  --entrypoint /opt/cma/bin/cma-tool-runner \
  cma-tool-runner-local self-hosted < activation.json
```

This passes the connection role's temporary credentials and nothing else. Never mount `~/.aws` or your home directory into the container. The container runs as an unprivileged user, and the model's files stay in the container and go away with it.

The credentials last as long as the role session, which is one hour when your own profile is an assumed role. The tool runner signs its connection with them, so run sessions that end before they expire, or start a new tool runner for a new session. The log goes to the terminal. `RUST_LOG=debug` (passed with `-e`) shows more.

### On Amazon EC2
<a name="claude-managed-agents-bedrock-self-hosted-on-amazon-ec2"></a>

A container can reach the instance metadata service and get the credentials of the instance's role, unless the instance prevents it. Require IMDSv2 with a hop limit of 1, which keeps containers on Docker's default network from getting a token:

```bash
aws ec2 modify-instance-metadata-options --instance-id i-... \
  --http-tokens required --http-put-response-hop-limit 1
```

Check it from the container before you rely on it:

```bash
docker run --rm --entrypoint bash cma-tool-runner-local -c \
  'exec 3<>/dev/tcp/169.254.169.254/80 && printf "PUT /latest/api/token HTTP/1.1\r\nHost: 169.254.169.254\r\nX-aws-ec2-metadata-token-ttl-seconds: 60\r\n\r\n" >&3 && timeout 3 cat <&3'
```

It should print nothing or fail. If it prints a token, the container can reach the instance's role. Containers also have unrestricted outbound network access by default. If your agents should not reach other hosts on your network, start the container on a Docker network with egress rules that allow only the service's endpoint.

## Run it directly on your machine
<a name="claude-managed-agents-bedrock-self-hosted-run-it-directly-on-your-machine"></a>

Run the build from `bin/` for your platform, with the activation on stdin. On a Mac:

```bash
mkdir -p ~/cma-workspace/sesn_...
AWS_PROFILE=cma-runner AWS_REGION=us-west-2 \
  ./bin/cma-tool-runner-macos self-hosted < activation.json
```

On Linux, use `cma-tool-runner-linux-x86_64` or `cma-tool-runner-linux-arm64` instead.

Create the session with `working_directory` set to the directory you made, as an absolute path such as `/Users/you/cma-workspace/sesn_...`. The activation then names it, so you don't need to edit `activation.json`. The tool runner reads standard AWS profiles, including SSO and `credential_process` profiles.

**Note**  
This is the quickest way to try a self-hosted session, and it has no boundary. Every command the model runs can read every file your user can, including the source profile behind `cma-runner`. For anything beyond a trial, run it as a separate OS user whose home directory holds only the `cma-runner` profile and its own credentials, or use the container.

## Stop it
<a name="claude-managed-agents-bedrock-self-hosted-stop-it"></a>

Delete the session (`DELETE /sessions/{id}`), then stop the tool runner with Ctrl\+C or by stopping the container. A tool runner whose session is deleted stops getting tool calls but keeps running until you stop it.

## If the tool calls fail
<a name="claude-managed-agents-bedrock-self-hosted-if-the-tool-calls-fail"></a>


| What you see | Cause | 
| --- | --- | 
| The tool runner prints its usage and exits | It was started without `self-hosted`. | 
| It exits at once after reading the activation | The activation is malformed or missing a field. The log names it. | 
| It never logs that it connected, or logs 403 | Its role may not `bedrock:CreateAnthropicSessionConnection` on the session, or its credentials are for another account. | 
| It logs 403 and says its credentials have expired | The credentials it started with ran out. Refresh them and start it again. | 
| It logs 404 | The session ID or project is wrong, the session was deleted, or the activation's `attachment_token` is missing or not the session's. | 
| It logs 409, or that it was replaced, and exits | The activation is stale: another call to the attachment route replaced it, or another tool runner was started with the same activation. Ask for a new one. | 
| It logs that the account is dialing faster than its connect quota allows | Your account's tool runners are connecting faster than its quota allows. It waits and connects again by itself. | 
| The session's tool calls time out | The tool runner was not running, or not yet connected, when the turn started. | 