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 or directly on the 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.
How it works
You create a session whose
environment_idis your project's self-hosted ID.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.
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
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.On Amazon EC2, keep containers away from the instance's role. See On Amazon EC2.
What you need
The session role and the caller policy from Set up permissions and prerequisites. 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/hascma-tool-runner-linux-arm64andcma-tool-runner-linux-x86_64, for Linux with glibc 2.28 or newer, andcma-tool-runner-macos, a universal binary for Apple silicon and Intel Macs. The macOS build is not notarized. After a download, check thatshasum -a 256 bin/cma-tool-runner-macosmatches the value inbin/BUILD-INFO, then runxattr -d com.apple.quarantine bin/cma-tool-runner-macoson that file only.A role for the tool runner's connection. The CloudFormation template creates one,
CmaSelfHostedToolRunnerRole, when you deploy it withCreateSelfHostedRole=true. To make it by hand, create a role whose trust policy lets your account assume it, and give it only this policy:
{ "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:
# ~/.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
Find the project the session will be in. Get your agent (
GET /agents/{id}) and drop the last two segments of itsresource_arn. What remains is the project ARN, such asarn:aws:bedrock:us-west-2:111122223333:project/default. The last segment is the project ID.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" }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:
{ "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, from Step 4 (Watch the session) onward.
Run it in a container
Build the image
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:
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:
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
The image's entrypoint starts the AgentCore mode, so override it to start the self-hosted mode, which reads the activation from stdin:
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 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:
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:
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
Run the build from bin/ for your platform, with the activation on stdin. On a Mac:
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.
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
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
What you see | Cause |
|---|---|
The tool runner prints its usage and exits | It was started without |
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 |
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 |
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. |