AWS SigV4 Gateway AuthenticationΒΆ
OverviewΒΆ
Some upstream MCP servers sit behind AWS IAM authentication. Examples are a Lambda function URL with AWS_IAM auth and an API Gateway endpoint with IAM authorization. These endpoints reject bearer tokens and static headers. They accept only requests signed with AWS Signature Version 4.
ContextForge supports this with auth_type: "sigv4" on a gateway. The gateway stores AWS credentials and signs every request it sends to the upstream server.
Gateway URL
- Direct installs (
uvx, pip, ordocker run):http://localhost:4444 - Docker Compose (nginx proxy):
http://localhost:8080 - Dev server (
make dev):http://localhost:8000
How it worksΒΆ
A SigV4 signature covers the HTTP method, path, query string, selected headers, and a SHA-256 hash of the exact request body. It also carries a timestamp, and AWS accepts a signature for only about 15 minutes. A signature therefore cannot live in a stored header the way a bearer token does.
ContextForge attaches a signer to the HTTP client that talks to the upstream server. The signer runs once for every outgoing request and computes a new signature each time. This covers:
- the
initializecall and tool, resource, and prompt discovery at registration - tool calls, resource reads, prompt fetches, and completions
- requests on pooled upstream sessions that stay open for a long time
- gateway health checks
A long-lived session never reuses an expired signature.
PrerequisitesΒΆ
- An upstream MCP server behind AWS IAM authentication, reachable over
SSEorSTREAMABLEHTTP. - An IAM identity with permission to call that endpoint. Use a dedicated identity with the minimum policy.
- The access key ID, secret access key, AWS region, and AWS service name of the endpoint.
- A ContextForge API token with permission to create gateways.
AUTH_ENCRYPTION_SECRETset on the gateway. It encrypts the stored secret. SigV4 adds no other environment variable.
Register a SigV4 gatewayΒΆ
Create an API token:
export GW=http://127.0.0.1:8000
export TOKEN=$(python -m mcpgateway.utils.create_jwt_token \
--username admin@example.com --exp 10080 --secret "$JWT_SECRET_KEY")
$JWT_SECRET_KEY must match the value the running gateway uses.
Register the gateway with POST /gateways:
curl -sS -X POST "$GW/gateways" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "aws-lambda-sigv4",
"url": "https://<id>.lambda-url.<region>.on.aws/mcp",
"transport": "STREAMABLEHTTP",
"visibility": "public",
"auth_type": "sigv4",
"sigv4_config": {
"access_key_id": "<access-key-id>",
"secret_access_key": "<secret-access-key>",
"session_token": "<optional, temporary credentials only>",
"region": "<region>",
"service": "<service, for example lambda or execute-api>"
}
}'
Keep secrets out of shell history
Do not paste a real secret access key into a command line. Read it with read -rs and build the JSON with jq:
read -rs "AWS_SK?Secret access key: "; echo
jq -n --arg url "$FN_URL" --arg ak "$AWS_AK" --arg sk "$AWS_SK" --arg region "$AWS_REGION" '{
name: "aws-lambda-sigv4", url: $url, transport: "STREAMABLEHTTP", visibility: "public", auth_type: "sigv4",
sigv4_config: {access_key_id: $ak, secret_access_key: $sk, region: $region, service: "lambda"}
}' | curl -sS -X POST "$GW/gateways" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d @-
unset AWS_SK
Use the API to register a SigV4 gateway and to change its credentials. The Admin UI edit form keeps the stored SigV4 settings when you save, but it cannot change them.
sigv4_config fieldsΒΆ
| Field | Required | Rule |
|---|---|---|
access_key_id | yes | 16 to 128 letters or digits. |
secret_access_key | yes | Encrypted at rest. Masked in API responses. |
session_token | no | Use only with temporary STS credentials. Encrypted at rest. Masked in API responses. |
region | yes | Lowercase letters, digits, and hyphens. Example: eu-north-1. |
service | yes | Lowercase letters, digits, and hyphens. Examples: lambda, execute-api. |
sigv4_config rejects any other key. It is valid only when auth_type is sigv4.
transport is STREAMABLEHTTP or SSE.
What registration doesΒΆ
- ContextForge validates the request and encrypts
secret_access_keyandsession_token. - It signs the
initializecall and tool discovery, and sends them to the upstream server. - If the upstream server accepts the signature, ContextForge saves the gateway and its tools.
- If the upstream server answers 401 or 403, registration fails with a message that the upstream server rejected the SigV4 credentials. ContextForge saves no gateway.
| Result | Meaning |
|---|---|
200 or 201 | The gateway is registered. The response shows the secret and token masked. |
422 | The sigv4_config shape is wrong, for example a missing field or a bad region. |
| Credential error from the upstream server | The access key, secret, region, or service does not match the endpoint. |
401 or 403 from ContextForge | The API token lacks permission to create gateways. |
Check the gatewayΒΆ
List the tools that discovery found:
Confirm that reads never return the secret:
The expected output is 0.
Rotate credentialsΒΆ
Send a new sigv4_config with PUT /gateways/{id}:
curl -sS -X PUT "$GW/gateways/<gateway-id>" \
-H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{
"auth_type": "sigv4",
"sigv4_config": {
"access_key_id": "<access-key-id>",
"secret_access_key": "<new-secret-access-key>",
"region": "<region>",
"service": "<service>"
}
}'
- A change to
sigv4_configcloses the pooled upstream sessions of that gateway. The next call uses the new credentials. - Send the masked value as
secret_access_keyorsession_tokento keep the stored value. A client can send back a gateway it just read without overwriting the secret. - Change
auth_typeto another value to drop SigV4. ContextForge then clearssigv4_config.
Sign REST tool callsΒΆ
A REST tool can send SigV4-signed requests too. Two forms exist.
- Standalone tool. Set
auth_typetosigv4and add asigv4_configobject toPOST /tools. - Tool under a SigV4 gateway. Set
auth_typetosigv4andgateway_idto the gateway. Omitsigv4_config. The tool signs with the gateway credential.
curl -sS -X POST "$GW/tools" \
-H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{
"tool": {
"name": "orders-api",
"url": "https://<id>.execute-api.<region>.amazonaws.com/orders/{id}",
"integration_type": "REST",
"request_type": "POST",
"auth_type": "sigv4",
"sigv4_config": {
"access_key_id": "<access-key-id>",
"secret_access_key": "<secret-access-key>",
"region": "<region>",
"service": "execute-api"
}
}
}'
How the signing works:
- ContextForge signs the final request. The body hash covers the exact bytes sent. URL template values are substituted before signing.
- The tool
sigv4_configtakes precedence. Without it, the tool uses the credential of its gateway when that gateway usessigv4. Without either, the call fails and sends no request. - A tool that has a
sigv4_configuses the same fields as the gateway, plus one optional field:unsigned_payload. A gatewaysigv4_configrejectsunsigned_payloadwith HTTP 422, because MCP transport signing always hashes the body. unsigned_payload: truesendsUNSIGNED-PAYLOADin place of the body hash. Use it only for services that accept it, such as S3. The default is a signed body hash.- After an HTTP 401, ContextForge signs the request again with a fresh timestamp and sends it once more. It never retries after an HTTP 403.
- Each call uses its own HTTP client. The signing object never moves between calls.
- A client
Authorizationheader orX-Upstream-Authorizationheader never replaces the signature. - ContextForge never stores credentials in
auth_value. A read of the tool returnssigv4_configwith the secret fields masked. - Send the masked value as
secret_access_keyorsession_tokenonPUT /tools/{id}to keep the stored secret. Changeauth_typeto another value to drop SigV4 and clearsigv4_config. - Only REST tools accept
sigv4. MCP tools from a SigV4 gateway are signed through the gateway. - A tool signs only when its
auth_typeissigv4. A tool with anotherauth_typeunder a SigV4 gateway keeps its own authentication and is never signed with the gateway credential. - To make a tool under a SigV4 gateway inherit its credential, the gateway must use
sigv4. The caller must see the gateway under the usual visibility rules, including the team scope of the caller's token. The caller must also own the gateway, hold the team owner role for it, or be a platform admin. A public gateway does not grant the credential to other callers. Otherwise the request fails with HTTP 403. - ContextForge repeats this check when an update changes the
urlor theauthof a tool that inherits a gateway credential. An update that sends the storedurlunchanged skips the check. - A request without a caller identity fails the check. An unknown gateway ID and a hidden gateway return the same error, so a caller cannot probe for gateway IDs.
- Bulk registration applies the same check to each tool that inherits a gateway credential. The tool is rejected and counted as failed, and the rest of the batch continues.
- Switching a tool to
sigv4clears any storedauth_value. - A masked
secret_access_keyis valid only when the tool already stores a secret to keep. Otherwise the request fails with HTTP 400. - A tool without
sigv4keeps presigned URLs working. A URL that already carriesX-Amz-Signatureis sent as is and gets no second signature. - A tool with
auth_type: "sigv4"rejects a URL that carriesX-Amz-Signature. A query signature and anAuthorizationsignature cannot share one request.
Security propertiesΒΆ
- The secret access key and session token are encrypted at rest with
AUTH_ENCRYPTION_SECRET. API reads return them masked. No API or log output shows the plaintext. - Logs, traces, and SIEM exports redact
secret_access_key,session_token,X-Amz-Security-Token,X-Amz-Signature, andX-Amz-Credential. This covers header values, query parameters, JSON fields, and the signature inside a SigV4Authorizationheader value in free text. SIEM field names match without regard to letter case. A failed upstream call logs no signature or credential. - An
Authorizationheader sent by a client never replaces the signature. SigV4 gateways ignore it. - Export skips SigV4 gateways and SigV4 REST tools, so signing credentials never reach an export file.
- Two SigV4 gateways collide only when
access_key_id,region, andserviceall match. A SigV4 gateway never collides with a gateway of another auth type. - A SigV4 request that returns HTTP 401 is signed again once. Both sends share the tool timeout.
- Registration and rotation of a SigV4 credential write an audit entry for the gateway or tool. The entry holds
sigv4_key_fingerprint, the first 12 hex characters of the SHA-256 of the access key ID. A rotation addssigv4_credential_rotatedandsigv4_previous_key_fingerprint. A removal, such as a switch to another auth type, addssigv4_credential_removedandsigv4_previous_key_fingerprint. A change to only the gateway name or description writes none of these fields. The entry never holds the access key ID, the secret, or the session token. - A bulk import writes one
bulk_create_toolsorbulk_update_toolsentry per team. The entry listssigv4_key_fingerprintsfor the team. It also listssigv4_toolswhen a tool registers, rotates, or removes a credential. Each item holdstool_id,name, and the samesigv4_*fields as a single create or update. - ContextForge does not audit each signed request or health check. The gateway or tool ID in the audit entry identifies the holder of the credential.
- The
developerrole keepsgateways.create,gateways.update,tools.create, andtools.update, so a developer can register and rotate SigV4 credentials. Visibility and ownership rules still apply. A caller outside the owning team, a non-owner, and a public-only token cannot update the gateway. - A tool under a SigV4 gateway receives the gateway credential only through the ownership rules in Sign REST tool calls.
- A stored secret that ContextForge cannot decrypt fails closed. The call returns an error and sends no unsigned request.
LimitsΒΆ
- ContextForge stores static credentials. It does not refresh an expired STS session token. Update
sigv4_configbefore the token expires. - The Admin UI cannot change SigV4 credentials. Saving the edit form keeps the stored settings. Use the API to change them.
- The modern listener skips SigV4 gateways, because a standing stream cannot sign each request. SigV4 gateways get no list-changed push events.
- SigV4 applies to
SSEandSTREAMABLEHTTPgateways, and to REST tools. - The Admin UI tool forms have no SigV4 option. Create and change SigV4 REST tools through the API.
- Configuration import does not carry SigV4 tool credentials, because export skips them. Register those tools through the API.
- The downgrade of migration
f6b8d3e1a5c2dropstools.sigv4_config. It first disables each tool withauth_type = 'sigv4'and clears itsauth_type. Set another auth type, then re-enable the tool.
TroubleshootingΒΆ
| Symptom | Likely cause |
|---|---|
| Registration fails with "upstream rejected the SigV4 credentials" | Wrong access key, secret, region, or service. The region and service must match the endpoint. An expired session token also causes it. |
422 on registration | Missing field, unknown key, or malformed region, service, or access key ID. |
| Registration works, then health checks fail | The IAM identity lost its permission, or the key was rotated or disabled in AWS. |
| Calls fail after the session token expires | STS tokens expire. Update sigv4_config with a new token. |
no such column: sigv4_config | The database lacks migration e5a7c2d9b4f1 (gateways) or f6b8d3e1a5c2 (tools). Restart the gateway or run alembic upgrade head. |
422 on POST /tools with sigv4 | A standalone tool needs sigv4_config, or a gateway_id. Only REST tools accept sigv4. |
| A REST tool call fails with a SigV4 error and sends nothing | The tool and its gateway both have no usable SigV4 credential, or the stored secret cannot be decrypted. |
An unsigned curl to the upstream URL returns 200 | The endpoint is public. Set its auth type to AWS_IAM or the equivalent. |