Agent credentials
Agent credentials let an admin give one agent its own API key or token for a tool, instead of sharing a person's key or one key for every caller. The gateway keeps the secret encrypted and bound to that agent. When the agent calls a tool on an MCP server, the gateway sends the agent's own credential to the server for it. When the agent needs to call a SaaS API directly, it trades its sign-in token for the credential at the gateway's token endpoint. Admins can rotate a credential, revoke it at once, or suspend every credential of an agent with one switch, and every change and every hand-out is written to the audit log.
It needs a Spanway enterprise license that includes the agent_governance feature, a database, and a proxy admin to make changes. Admin viewers can read the list. Secrets are never shown again after they are saved, not in the Admin UI, not in the API and not in logs.
A real-world scenario
A support team runs a support-triage agent that reads tickets through a CRM MCP server and posts summaries to the CRM's REST API. Until now the MCP server used one shared API key, so the audit trail in the CRM showed every caller as the same integration user, and a leaked key would have meant rotating it for everyone. The admin creates a CRM service account just for the agent, stores its key in the gateway as the agent's mcp_server credential for the CRM server, and stores a second key as a saas credential named crm-api. The agent's tool calls now reach the CRM with its own key, the agent fetches crm-api with its identity provider token when it calls the REST API, and when the security team later spots the key in a log file, the admin revokes it in one click and rotates in a new one without touching any other caller.
How it works
There are two kinds of credential, and each agent can hold one of each kind per target.
| Kind | Where the secret goes | Who sees the secret |
|---|---|---|
mcp_server | The gateway sends it to one MCP server in place of the server's own secret, every time this agent calls a tool on that server. | Nobody. It never leaves the gateway except towards that MCP server. |
saas | The agent asks for it at POST /token with its identity provider token and the credential's name, and calls the SaaS API itself. | Only the agent, and only after proving who it is. |
An mcp_server credential works for MCP servers that take a fixed secret, which are servers whose sign-in is api_key, bearer_token, basic, authorization or token. The gateway formats the secret the same way it formats the server's own secret, so store the bare key, for example crm-live-..., not Bearer crm-live-.... It is used when the agent calls a tool. Listing the server's tools still uses the server's own connection.
Only an agent acting on its own uses its credentials. That means a virtual key that belongs to the agent, or the agent's own identity provider token with the agent bound to that application. Only a proxy admin can bind a virtual key to an agent (the key's agent_id), on /key/generate, /key/update and the other key routes, so a person cannot give their own key an agent's identity to borrow its credentials. When an agent acts on behalf of a signed-in person (the On behalf of a user execution mode), its credentials are not used, so the person's own connections apply and the agent can never do more than the person could.
When a credential cannot be used, the call is refused. The gateway never falls back to the server's shared secret for an agent whose credential is revoked, expired, suspended or unreadable, because quietly using a broader key would undo what the admin just did.
To keep tool calls fast, each gateway instance remembers for 30 seconds which MCP servers an agent holds credentials for, so an agent without a credential for a server does not wait on the database for every call. A change made on the same instance (add, rotate, revoke, delete, suspend) takes effect on the next call. Another instance can take up to 30 seconds to start using a newly added credential and keeps using the server's own secret until then, while revoking, suspending or rotating a credential always takes effect at once everywhere, because those calls still read the credential itself. If the database cannot be reached, an agent whose list is already known keeps working with servers it has no credential for, and calls that need a stored credential, or come from an agent the instance has not seen yet, are refused with agent_credential_unavailable.
Controls
| Control | What it does | When it helps | Default | Who should change it |
|---|---|---|---|---|
| Add credential | Stores a new key or token for this agent. Choose MCP server to have the gateway send it to one server, or SaaS API to let the agent fetch it. | Give an agent its own service account instead of a shared or personal key. | No credentials. | Proxy admins. |
| Expires | The time after which the gateway stops using the credential. The agent's calls are refused with agent_credential_expired until someone rotates it. | Match the expiry of the key at the vendor, so nothing keeps using a key the vendor already turned off. | No expiry. | Proxy admins who manage the vendor account. |
| Rotate | Replaces the secret. The old secret stops working at the gateway at once and the version number goes up by one. Rotating a revoked credential puts it back in service with the new secret. | Regular key rotation, or recovery after a revoke. | Not applicable. | Proxy admins. |
| Revoke | Stops the credential at once and erases its secret from the database. The record stays so the history is visible. | A key leaked or the agent should lose access to that tool now. | Not applicable. | Proxy admins, or security on call. |
| Delete | Removes the credential and its record. The agent's calls to that MCP server use the server's own secret again. | The agent no longer needs its own credential for that tool. | Not applicable. | Proxy admins. |
| Suspend all credentials | The agent wide kill switch for stored credentials. While it is on, every call that would use any of the agent's credentials is refused, and the agent cannot fetch any of them. Turning it off restores them exactly as they were. | Stop an agent from reaching every outside tool while you investigate, without deleting anything or disabling the agent's model access. | Off. | Proxy admins, or security on call. |
The agent kill switch is the bigger hammer. It stops the agent entirely, models included. Suspend all credentials only stops the agent's stored credentials.
Steps
-
Store a credential. In the Admin UI open Agents under Agentic, select the agent, and in the Stored Credentials section click Add credential. Enter a Name such as
crm-api, pick SaaS API or MCP server (and the server), paste the Secret, optionally add a Description and an Expires time, and click Save. The same through the API, keeping thecredential_idthe answer returns:curl -s -X POST "http://localhost:4000/v1/agents/$AGENT_ID/credentials" \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"name": "crm-api","kind": "saas","secret": "crm-live-0d2c6f1e9b7a4c3d9f3a","description": "CRM read access for the support agent","expires_at": "2027-01-31T00:00:00Z"}' | tee credential.jsonCREDENTIAL_ID=$(jq -r .credential_id credential.json)For an MCP server use
"kind": "mcp_server"and"mcp_server_id"with the server's id from MCP Servers orGET /v1/mcp/server. -
List the agent's credentials. The answer never contains a secret, only its last four characters when the secret is at least 12 characters long.
curl -s "http://localhost:4000/v1/agents/$AGENT_ID/credentials" -H "Authorization: Bearer $SPANWAY_API_KEY" -
Let the agent fetch a SaaS credential. The agent must be bound to an identity provider application in Autonomous mode, and needs a client id from
POST /register, which any MCP or OAuth client library does for you. It then sends its own token from the identity provider assubject_tokenand the credential's name asaudience:curl -s -X POST http://localhost:4000/token \-d grant_type=urn:ietf:params:oauth:grant-type:token-exchange \-d client_id="$CLIENT_ID" \-d subject_token="$AGENT_IDP_TOKEN" \-d subject_token_type=urn:ietf:params:oauth:token-type:jwt \-d audience=crm-api -
Rotate the secret when the vendor issues a new key. In the Admin UI click Rotate on the credential and paste the new secret. Through the API:
curl -s -X POST "http://localhost:4000/v1/agents/$AGENT_ID/credentials/$CREDENTIAL_ID/rotate" \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"secret": "crm-live-7a1b2c3d4e5f6a7b9f3a"}' -
Revoke it at once if it leaks. In the Admin UI click Revoke and give a reason. Through the API:
curl -s -X POST "http://localhost:4000/v1/agents/$AGENT_ID/credentials/$CREDENTIAL_ID/revoke" \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"reason": "key posted in a public channel"}' -
Suspend every credential of the agent while you investigate. In the Admin UI turn on Suspend all credentials and give a reason. Through the API, and again with
"suspended": falseto resume:curl -s -X PUT "http://localhost:4000/v1/agents/$AGENT_ID/credentials/suspension" \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"suspended": true, "reason": "agent is under review"}' -
Delete a credential the agent no longer needs with Delete, or
DELETE /v1/agents/$AGENT_ID/credentials/$CREDENTIAL_ID.
Expected result
Step 1 answers with the stored record, without the secret.
{
"credential_id": "b71e0c55-2f0a-4d3e-9a51-6c7d8e9f0a12",
"agent_id": "3f6c1a2e-8d4b-4c1e-9a7f-2b5d6e8c0a11",
"name": "crm-api",
"kind": "saas",
"status": "active",
"version": 1,
"secret_hint": "...9f3a",
"expires_at": "2027-01-31T00:00:00Z"
}
Step 3 answers with the credential as the access token. token_type is N_A because the gateway does not know how the SaaS API wants it sent, and expires_in is the time left until Expires, when one is set. No refresh token is issued, so the agent asks again when it needs the secret.
{
"access_token": "crm-live-0d2c6f1e9b7a4c3d9f3a",
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
"token_type": "N_A",
"expires_in": 5961600
}
After step 4 the list shows version: 2 and the new hint. After step 5 the credential shows status: revoked, and the agent's calls that need it answer 403 with agent_credential_revoked. After step 6 the list shows suspension.suspended: true, and every call that needs any of the agent's credentials answers 403 with agent_credentials_suspended. Last used in the Admin UI (last_used_at) shows when the gateway last used or handed out the credential, updated at most every five minutes.
Security
Secrets are encrypted with AES-256-GCM under the gateway's salt key (SPANWAY_SALT_KEY, or the master key when no salt key is set), and each ciphertext is bound to its agent and credential id, so a value copied into another row of the database cannot be opened. If neither key is set the gateway refuses to store a credential instead of keeping it in plain text. When the master key is rotated with /key/regenerate and no salt key is set, stored credentials are re-encrypted under the new key. Revoking a credential erases its secret. Secrets and request bodies with secrets are never written to logs, audit rows or error messages, and a request that fails validation answers without echoing the body.
| Method | Path | Summary |
|---|---|---|
| GET | /v1/agents/{agent_id}/credentials | List an agent's stored credentials |
| POST | /v1/agents/{agent_id}/credentials | Store a credential for an agent |
| PUT | /v1/agents/{agent_id}/credentials/suspension | Suspend or resume all of an agent's stored credentials |
| DELETE | /v1/agents/{agent_id}/credentials/{credential_id} | Delete an agent's stored credential |
| POST | /v1/agents/{agent_id}/credentials/{credential_id}/revoke | Revoke an agent's stored credential |
| POST | /v1/agents/{agent_id}/credentials/{credential_id}/rotate | Rotate an agent's stored credential |
Errors
| What you see | Why | Fix |
|---|---|---|
403 naming the agent_governance feature. | The license does not include it. | Ask for a license with agent_governance, see enterprise. |
403 Only proxy admins can change an agent's stored credentials | The caller is not a proxy admin. | Use a proxy admin key. |
403 Only proxy admins and admin viewers can list an agent's stored credentials | The caller is neither a proxy admin nor an admin viewer. | Use an admin key. |
404 Agent '<id>' not found | No agent with this id is stored in the database. Agents defined only in config.yaml cannot hold credentials. | Use the agent_id from the Agents page, or create the agent through the Admin UI or POST /v1/agents. |
404 No stored credential '<credential_id>' for agent '<agent_id>' | The credential id is wrong, belongs to another agent, or was deleted. | Take the id from the list in step 2. |
400 No MCP server '<id>' exists | The mcp_server_id names no MCP server on this gateway. | Copy the id from MCP Servers. |
400 MCP server '<id>' signs in with oauth2. Agent credentials only replace an api_key, bearer_token, basic, authorization or token secret | The server signs in some other way, so there is no fixed secret to replace. | Use a server with a fixed secret, or store a saas credential instead. |
409 Agent already has a stored credential named '<name>' or ... for MCP server '<id>' | Names are unique per agent, and so is one credential per MCP server. | Rotate the existing credential, or delete it first. |
422 with mcp_server_id is required when kind is mcp_server, secret must not contain line breaks or control characters, expires_at must be in the future or a name pattern message. | The request is not valid. Names are 1 to 64 lowercase letters, digits, dots, dashes or underscores. | Fix the field the message names. |
503 Set SPANWAY_SALT_KEY or a master key before storing agent credentials | The gateway has no key to encrypt with. | Set SPANWAY_SALT_KEY (recommended) or a master key and restart. |
500 No database connected | The gateway has no database. | Connect a database. |
403 with agent_credential_revoked, agent_credential_expired or agent_credentials_suspended on an MCP tool call. | The agent's credential for that server is revoked, past Expires, or suspended. | Rotate the credential, or turn off Suspend all credentials. |
403 with agent_credential_unreadable on an MCP tool call. | The gateway could not decrypt the credential, usually because the salt key changed. | Rotate the credential with the current secret. |
503 with agent_credential_unavailable on an MCP tool call. | The gateway could not read the agent's credentials from the database, so it refused rather than guess. | Check the database connection and retry. |
403 Only proxy admins can bind a key to an agent or change the agent of a key (`agent_id`). from a key route. | A non admin tried to give a key an agent's identity. | Ask a proxy admin to create the agent's key. |
400 invalid_target from POST /token saying the agent has no stored SaaS credential named <name>, or that audience must name one. | audience is missing, misspelled, names an mcp_server credential, or names a credential of another agent. | Send the exact name of one of this agent's saas credentials. |
400 invalid_target from POST /token with a revoked, expired or suspended message. | The credential cannot be handed out right now. | Rotate it, or resume the agent's credentials. |
400 invalid_request from POST /token saying only agents bound to an identity provider application can exchange their token. | The token carries an agent claim, but the agent is not bound to that application. | Bind the agent to the application whose token it sends. |
400 invalid_request from POST /token saying the agent token acts for a signed-in person. | The token is a delegated, on behalf of a user token. | Use the agent's own application token. |
503 temporarily_unavailable from POST /token | The gateway could not read the credentials, or has no database. | Retry, and check the database connection. |
Logs
| Level | Line | Meaning | Action |
|---|---|---|---|
| INFO | stored credential <name> for agent <agent_id> | An admin stored a credential. | None. |
| INFO | rotated credential <name> of agent <agent_id> to version <n> | An admin rotated a credential. | None. |
| INFO | deleted credential <name> of agent <agent_id> | An admin deleted a credential. | None. |
| WARNING | revoked credential <name> of agent <agent_id> | An admin revoked a credential. | Check that the agent's calls that need it are expected to fail. |
| WARNING | suspended every stored credential of agent <agent_id> or resumed every stored credential of agent <agent_id> | An admin used Suspend all credentials. | None, unless nobody expected it. |
| WARNING | refused agent <agent_id> a call to MCP server <server_id>: <code> | An agent's tool call was refused because its credential is revoked, expired, suspended or unreadable. | Rotate the credential or resume the agent's credentials if the call should work. |
| WARNING | refused agent <agent_id> the credential <name>: <code> | An agent's token exchange was refused for the same reasons. | Same as above. |
| ERROR | agent credential lookup failed for agent <agent_id>: <error type> or agent credential exchange failed for agent <agent_id>: <error type> | The database could not be read, so the call was refused. | Check the database connection. |
| WARNING | using the last known credential list of agent <agent_id>, refresh failed: <error type> | The database could not be read, so the gateway kept using the list of MCP servers the agent holds credentials for that it read earlier. Calls to servers without a credential continue, calls that need one are refused. | Check the database if it repeats. |
| WARNING | Failed to re-seal agent credentials: <error type> | A master key rotation could not re-encrypt the stored credentials. | Rotate the affected credentials with their secrets again. |
The Prometheus counter spanway_agent_credential_uses_total counts every time the gateway used or refused a stored credential, by kind (mcp_server or saas) and outcome (used, agent_credentials_suspended, agent_credential_revoked, agent_credential_expired or agent_credential_unreadable). It never carries an agent id.
With OpenTelemetry turned on, the same use or refusal adds a spanway.agent_credential event to the request's trace, the MCP tool call or the /token exchange, with spanway.agent_credential.kind and spanway.agent_credential.outcome. The event never carries the secret, the credential's name or its version. Credential use is not a source in Explore. Each token exchange writes a revealed row to the audit log, and admin changes write their own rows, see the next section, so filter the audit log by the Agent credentials table to see who used or changed what.
Audit
Every change writes a row on Spanway_AgentCredential with the record before and after, never the secret. object_id is <agent_id>/<credential_id>.
| Action | When |
|---|---|
created | A credential was stored. |
rotated | A credential got a new secret, with the version before and after. |
updated | A credential was revoked, with the status and the reason. |
deleted | A credential was deleted. |
blocked or unblocked | Suspend all credentials was turned on or off, with object_id <agent_id>/suspension and the reason. |
revealed | The agent fetched a saas credential through the token exchange. changed_by is agent:<agent_id> and the row names the credential and its version. |
Related
- Agent identity providers for binding an agent to Microsoft Entra ID or any OpenID Connect provider.
- Agent governance for limits, the agent kill switch and execution modes.
- MCP OAuth for
POST /registerandPOST /token. - Audit logs for reading the rows above.