Overview

The AWS MCP sandbox looks like the boundary. It runs Python you do not control, it has no network, and every AWS call goes through one function, call_boto3. Giving an Agent Its Own AWS Identity ended close to that, with the role deciding what the agent can do and the sandbox looking hard to abuse. So is it hard to abuse?

In that post, I put a broker in front of the MCP server. It checks my agent’s API key, gets a token as the role and makes the MCP calls for the agent, which is why the role needs AWSMCPSignInOAuthAccessPolicy. But the sandbox runs as that same role. So what stops a script in the sandbox from asking for a token too?

Nothing, and the MCP server even hands the token back in the tool result. I left that part out of the previous post, and it matters as soon as an attacker can steer the agent with a prompt, or finds a token lying around somewhere. There are two ways to get one. One goes through the MCP server from inside the sandbox. The other goes straight to AWS with the role’s credentials and no MCP call in the path. CloudTrail records them differently, and the second one looks just like the broker doing its normal job.

How the Grant Works

Let’s start with the API itself. The grant is an ordinary AWS API call, not an MCP feature, and the reference is short.

POST /v1/token?x-amz-client-auth-method=iam
Content-type: application/json

{"grant_type": "client_credentials", "resource": "aws-mcp.amazonaws.com"}

The caller signs the request with SigV4, so Sign-in knows which identity is asking. The reply carries access_token, token_type and expires_in, and the expiry is the minimum of the caller’s session validity and one hour.

That gives us two facts about the token. It is scoped to aws-mcp.amazonaws.com as an OAuth resource, so the MCP endpoint is the only AWS API that accepts it, and through that endpoint it reaches whatever the role allows. And whoever signs the request decides the identity the token carries. The broker signs with the role’s credentials because it assumed the role, and AWSMCPSignInOAuthAccessPolicy on the role is what allows the call.

flowchart LR
  A[agent] -->|tools/call| M[aws-mcp server]
  M -->|forward access session| S[signin:CreateOAuth2TokenWithIAM]
  B[broker] -->|SigV4, direct| S
  S -->|bearer token| T[token]
  T -->|replayed| X[somewhere else]

Now look at the sandbox. It runs as the same role, so the API is one of the things a script can call. So a script can call the grant API too. Are you thinking what I am thinking?

The Roll Safe meme, a man tapping his head with a knowing look

Obtaining a Token From the Sandbox

I asked my agent to run this script through aws___run_script, the MCP tool that runs Python in the sandbox. The whole script is one call_boto3 call.

result = await call_boto3(
    service_name="signin",
    operation_name="CreateOAuth2TokenWithIAM",
    region_name="us-east-1",
    params={"grantType": "client_credentials", "resource": "aws-mcp.amazonaws.com"},
)
result

Before anything runs, my agent’s approval layer looks at which AWS operations the script calls. If it only reads, it runs right away. Anything else stops and waits for me to approve it, and so does a read that hands back a credential, like a Get call for a secret. CreateOAuth2TokenWithIAM is not a read, so the agent stopped and asked me. That request is the only sign of the attempt before AWS sees the call, and it looks like any other script the agent asks to run. Whoever approves it has to know what the operation name means.

Not every deployment asks. The approval layer has two unattended modes, HOTL (human on the loop) and HOOTL (human out of the loop), where a call that would wait runs without a person. HOTL announces the call as it runs, and HOOTL only records it. My build keeps aws___run_script on a list of tools that still wait for a person in both modes, so flipping to HOOTL changed nothing on its own. Take it off that list, or run a deployment that never had it, and the script runs unattended. With the three layers below off as well, a prompt that steers the agent is all an attacker needs.

When I approved it, the sandbox returned this, trimmed to the fields that matter.

{
  "status": "success",
  "stdout": "",
  "stderr": null,
  "return_value": {
    "accessToken": "ASOAeyJraW...",
    "tokenType": "Bearer",
    "expiresIn": 899
  },
  "api_calls": [
    { "service": "signin", "operation": "CreateOAuth2TokenWithIAM", "status": "success" }
  ]
}

api_calls lists the signin call by name. The token is 3,424 characters, and expiresIn reads 899, which is shorter than usual. The broker’s own tokens come back with about an hour, and its log showed 3,598 to 3,600 seconds for every token it obtained over two days. Every token I asked for through the script came back with about fifteen minutes instead, even when the token that asked for it had less than that left.

With my usual settings, the token never showed up in the agent’s answer. Three layers hid it, and all three come from my own setup, not from AWS.

LayerWhere it runsWhat it did
Tool-result redactionagent, after every tool callaccessToken came back as [REDACTED]
Prompt ruleagent’s system promptnever put a secret or token in an answer
PII guardagentgateway, on the LLM routemasked JWT-shaped strings in tool output

The gateway’s guard caught the token because it is a JWT behind an ASOA prefix, and the guard masks anything shaped like eyJ... in the tool output my agent sends to its model. For the experiment I turned all three layers off. Taking the guard off that route left every request on it unguarded.

Then I re-ran the script through the agent. It suspended for approval, I approved it, and the token was in the answer.

The agent's reply, with the granted token printed in full

AWS grants the token, but every layer that hides it is a setting the agent’s owner controls.

Here is the token from the agent’s answer, decoded. The header says AWS signed the JWT with ES256.

{
  "kid": "a7498ba4-...",
  "typ": "JWT",
  "alg": "ES256"
}

The claims name the role, the audience and the sign-in session, and CloudTrail records that session on the MCP calls made with the token. These are all of them, with the account redacted and the 2,012-character session value cut short.

{
  "aud": "aws-mcp.amazonaws.com",
  "sub": "arn:aws:sts::111122223333:assumed-role/mcp-aws/mcp-aws",
  "nbf": 1795980405,
  "session": "ARAaGAoi...",
  "iss": "signin.amazonaws.com",
  "exp": 1795981305,
  "signin_session_arn": "arn:aws:signin:us-east-1:111122223333:session/021a81e2-...",
  "region": "us-east-1",
  "iat": 1795980405,
  "jti": "af48b6da-...",
  "client_id": "arn:aws:signin:::client-credentials/sigv4"
}

exp is exactly 900 seconds after iat, which is the fifteen minutes the sandbox gives every token.

Using the Token Out of the Sandbox

Does the token work outside the sandbox? Let’s test it out. The token is a bearer credential, so nothing proves the holder is the one who asked for it.

TOKEN=ASOAeyJraW...

SID=$(curl -s -D - -o /dev/null https://aws-mcp.us-east-1.api.aws/mcp \
  -H "authorization: Bearer $TOKEN" \
  -H 'content-type: application/json' \
  -H 'accept: application/json, text/event-stream' \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"replay","version":"0"}}}' \
  | awk -F': ' 'tolower($1)=="mcp-session-id"{print $2}' | tr -d '\r')

curl -s https://aws-mcp.us-east-1.api.aws/mcp \
  -H "authorization: Bearer $TOKEN" \
  -H 'content-type: application/json' \
  -H 'accept: application/json, text/event-stream' \
  -H "mcp-session-id: $SID" \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"aws___run_script","arguments":{"code":"result = await call_boto3(service_name=\"sts\", operation_name=\"GetCallerIdentity\")\nresult"}}}'

The first call is initialize, which answers 200 with an mcp-session-id header. Every later call repeats that header, and tools/call then works as it does for any other MCP client. I took the token the sandbox returned to a host outside it and asked for the caller identity. Trimmed to the HTTP status, the script status and the ARN, the reply was this.

http 200
status success
arn arn:aws:sts::111122223333:assumed-role/mcp-aws/mcp-aws

So yes, it works. The token is not tied to the sandbox that got it, and nothing checked where my host was calling from. Anything that can reach the MCP endpoint over HTTPS can use it.

When a token is stolen, it goes around everything I put in front of the endpoint. My agent’s calls have to pass the approval layer and then the broker, which checks the API key. Whoever holds the stolen token skips both and calls the endpoint directly, so the only limits left are what the role is allowed to do and when the token expires.

Does the fifteen-minute expiry save us? Not really. A stolen token can call aws___run_script again and get a new one with a fresh fifteen minutes. I chained three that way from outside the sandbox, and every one came back with fifteen minutes. So fifteen minutes is how long one token lives, not how long the access lasts, at least until the deny below stops the next grant.

The Direct Grant

The sandbox is not the only way to get a token. The broker gets its own straight from AWS with the role’s credentials, with no MCP server in between. Anyone holding those credentials can do the same, and the AWS CLI makes the call like this.

aws signin create-oauth2-token-with-iam \
  --grant-type client_credentials \
  --resource aws-mcp.amazonaws.com \
  --region us-east-1

The exact call I ran was the Python one, with the role’s credentials in the environment.

import boto3

signin = boto3.Session().client("signin", region_name="us-east-1")
reply = signin.create_oauth2_token_with_iam(
    grantType="client_credentials",
    resource="aws-mcp.amazonaws.com",
)
print(reply["accessToken"], reply["expiresIn"])

It printed a token 3,318 characters long with expiresIn 3599, the full hour. It is the same API and the same role as in the sandbox, just without the MCP server in the middle.

The broker keeps its token in memory and only asks for a new one when the cached one is close to expiring, so its grants come at least an hour apart. Someone else with the role’s credentials can grant whenever they like. Mine came seconds apart, which the broker never does on a normal refresh.

Reading the CloudTrail Records

So what did CloudTrail keep? Both paths leave a CreateOAuth2Token event, without the WithIAM, and invokedBy tells them apart.

PathEventinvokedByuserAgent
Broker or direct callerCreateOAuth2TokenabsentBoto3/1.43.126
SandboxCreateOAuth2Tokenaws-mcp.amazonaws.comaws-mcp.amazonaws.com

Why does only the sandbox grant get invokedBy? It goes through a forward access session, where the MCP server makes the call on the role’s behalf, so AWS writes the server down as the one that invoked it. A direct grant has nothing in the middle, so the field is simply missing. sourceIPAddress and userAgent split the same way, and on the sandbox grant both read aws-mcp.amazonaws.com instead of a real address or client.

The tool calls are AwsMcpEvent records from aws-mcp.amazonaws.com, and the payload is hidden. Look at requestParameters first. It holds the JSON-RPC call, with the tool name in params.name and the script in params.arguments, which AWS replaces with [HIDDEN_DUE_TO_SECURITY_REASONS]. responseElements.result is hidden the same way. userAgent reads python-httpx/0.28.1, the broker’s client, and sourceIPAddress is the address the broker called from.

{
  "eventName": "CallReadWriteTool",
  "eventSource": "aws-mcp.amazonaws.com",
  "userIdentity": {
    "type": "AssumedRole",
    "arn": "arn:aws:sts::111122223333:assumed-role/mcp-aws/mcp-aws",
    "sessionContext": {
      "signInSessionArn": "arn:aws:signin:us-east-1:111122223333:session/36278c46-..."
    }
  },
  "sourceIPAddress": "203.0.113.10",
  "userAgent": "python-httpx/0.28.1",
  "requestParameters": {
    "jsonrpc": "2.0",
    "method": "tools/call",
    "params": {
      "name": "aws___run_script",
      "arguments": "[HIDDEN_DUE_TO_SECURITY_REASONS]"
    }
  },
  "responseElements": {
    "result": "[HIDDEN_DUE_TO_SECURITY_REASONS]"
  },
  "additionalEventData": {
    "downstreamRequests": [
      { "apiName": "signin:CreateOAuth2TokenWithIAM", "awsRegion": "us-east-1", "requestId": "0b819208-..." }
    ]
  }
}

The code and the result are hidden, but downstreamRequests is the useful part. It lists every API the script called, with a requestId that joins to the real API event. In this run the single downstream call was the grant. readOnly is false on the tool event, even when the script only reads. The downstream API event carries its own value.

The same value appears one second later as requestID in the grant itself.

{
  "eventName": "CreateOAuth2Token",
  "eventSource": "signin.amazonaws.com",
  "userIdentity": {
    "type": "AssumedRole",
    "arn": "arn:aws:sts::111122223333:assumed-role/mcp-aws/mcp-aws",
    "sessionContext": {
      "signInSessionArn": "arn:aws:signin:us-east-1:111122223333:session/36278c46-..."
    },
    "invokedBy": "aws-mcp.amazonaws.com"
  },
  "sourceIPAddress": "aws-mcp.amazonaws.com",
  "userAgent": "aws-mcp.amazonaws.com",
  "requestParameters": {
    "resource": "aws-mcp.amazonaws.com",
    "client_id": "arn:aws:signin:::client-credentials/sigv4"
  },
  "additionalEventData": {
    "signInSessionArn": "arn:aws:signin:us-east-1:111122223333:session/021a81e2-...",
    "grant_type": "client_credentials",
    "success": "true"
  },
  "requestID": "0b819208-..."
}

Two session ARNs sit in that record. The first is the caller, the broker’s session. The second is the session the grant created, and it is the one the token’s signin_session_arn claim names. The record ties the call, the new token and every later use of it together. requestParameters.resource names the audience, client_id is the implicit client for the SigV4 flow, and grant_type confirms client credentials. readOnly is true on the grant, even though the call creates a session.

And the replay? It shows up four minutes after the grant as another CallReadWriteTool, from a different address and a different user agent, with sts:GetCallerIdentity in downstreamRequests. Its signInSessionArn is the session the grant created, not the broker’s, and that is what ties the replay back to the token.

The MCP session leaves a record too when it ends. DestroySession has aws-mcp.amazonaws.com as its event source, carries readOnly: false, and holds nothing but its method, so there are no downstream requests to follow.

{
  "eventName": "DestroySession",
  "eventSource": "aws-mcp.amazonaws.com",
  "userIdentity": {
    "type": "AssumedRole",
    "arn": "arn:aws:sts::111122223333:assumed-role/mcp-aws/mcp-aws",
    "sessionContext": {
      "signInSessionArn": "arn:aws:signin:us-east-1:111122223333:session/...04bc7a86a60b"
    }
  },
  "requestParameters": { "jsonrpc": "2.0", "method": "session/destroy" }
}

Its signInSessionArn matches the calls it follows, which is how the destroy groups with them. One sign-in session in my account had three destroys, one of them between two tool calls, and the last one 22 minutes after the previous call. That suggests MCP sessions follow connections rather than runs, and a destroy can arrive long after a client has gone quiet. A destroy tears down the MCP session and nothing else. The second of those two tool calls still ran under the same sign-in session, so the token stayed valid and the sign-in session stayed open.

NOTE

Neither the grants nor the AwsMcpEvent records showed up in event history within seconds. On my last check both were there within five minutes, and on an earlier one the MCP records took about ten. A detection that waits for event history runs that far behind the grant.

Spotting the Grant

So how do we spot it? The two rules below cover the sandbox grant and the replay, and both work without the hidden payload. They do not catch the direct grant, though, because in CloudTrail it can look exactly like the broker’s own. They also miss an attacker on the broker itself, who can read the token from its memory and send it with the broker’s own client without asking for a new grant.

A Grant Invoked by the Server

Any CreateOAuth2Token whose invokedBy is aws-mcp.amazonaws.com came from inside the sandbox, because the broker’s direct grant never carries that field. The query below lists them.

aws cloudtrail lookup-events --region us-east-1 \
  --lookup-attributes AttributeKey=EventName,AttributeValue=CreateOAuth2Token \
  --output json \
  | jq -r '.Events[].CloudTrailEvent | fromjson
      | select(.userIdentity.invokedBy == "aws-mcp.amazonaws.com")
      | "\(.eventTime) \(.userIdentity.arn)"'

A Foreign Client on the Token

In my setup the broker is the only thing that should be calling the MCP endpoint. Its calls carry the python-httpx user agent and always leave from the same address. It also never uses a token from the sandbox, so a call carrying a session that a sandbox grant created did not come from the broker. My replay was exactly that, a new session from a different address and a different user agent.

aws cloudtrail lookup-events --region us-east-1 \
  --lookup-attributes AttributeKey=EventSource,AttributeValue=aws-mcp.amazonaws.com \
  --output json \
  | jq -r '.Events[].CloudTrailEvent | fromjson
      | "\(.eventTime) \(.eventName) \(.sourceIPAddress) \(.userAgent) \(.userIdentity.sessionContext.signInSessionArn)"'

My replay came from an IPv6 address while the broker had only ever used IPv4, so in my account the address family alone set the replay apart. Before you build a rule on the address, check whether the broker’s host can reach AWS over both families, and put both in the baseline if it can.

Denying the Sandbox Grant

The sandbox and the broker share one identity, so IAM cannot separate them by principal. They differ on the condition keys the MCP server adds, though. aws:ViaAWSMCPService is true for calls that come through the AWS-managed MCP server’s forward access session, and the broker’s direct grant is not one of them. That gives a deny that blocks the sandbox without breaking the broker.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Deny",
      "Action": ["signin:CreateOAuth2Token*", "signin:AuthorizeOAuth2Access"],
      "Resource": "*",
      "Condition": { "Bool": { "aws:ViaAWSMCPService": "true" } }
    }
  ]
}

I put it on the role as an inline policy, then obtained a token both ways again. The direct grant still worked, 3,318 characters with expiresIn 3599, the same numbers as the run above. The sandbox grant came back denied. This time I sent the script straight to the endpoint with a bearer token, the same way as the replay, instead of through the agent.

{
  "status": "error",
  "error": "ClientError: An error occurred (AccessDeniedException) when calling the CreateOAuth2TokenWithIAM operation: Unauthorized, principal does not have permission for action: signin:CreateOAuth2Token",
  "api_calls": [
    { "service": "signin", "operation": "CreateOAuth2TokenWithIAM", "status": "error" }
  ]
}

I removed the deny once both results were in. The broker still needs the signin permission to obtain its own token, so the deny goes on top of that permission, not in place of it.

Two smaller steps help. If your approval layer lets reads through by verb, make it hold any operation whose name mentions a token, the way mine holds names with Token, Secret or Credential in them even when they start with Get. And keep the signin permission off any role that does not need the MCP at all. It is a permission to create a token, even though CloudTrail marks the grant readOnly.

The Role Is the Boundary

The sandbox has no network access, but the token it returns does not need any. It worked from another host, and CloudTrail tied that replay back to the grant through the session the grant created. A script in the sandbox still runs as the role, so unless a condition on the MCP path says otherwise, it can do whatever the role can, including obtaining the next token.

flowchart LR
  A[agent] -->|aws___run_script| S[sandbox, no network]
  S -->|call_boto3| R[role]
  B[broker] --> R
  R -->|everything it allows| X[signin, s3, ec2, ...]

That makes the role, not the server, the boundary. AWS makes the same point in Secure AI agent access patterns to AWS resources using Model Context Protocol, which says to assume the agent can use any permission it holds. For a role like mine, that means the deny above, because it is what stops a stolen token from getting the next one. If the deny is missing, the two rules above are what you have left, one for the sandbox grant and one for the replay. As for the three layers that hid the token, I turned them all back on after the test, including the gateway’s PII guard, the last thing a credential passes before it reaches the model.