MCP per-user credentials
Some MCP servers must act as the person making the request rather than as a shared service account, so that the server's own permissions apply. The gateway supports three kinds of per-user credential. A person can store their own API key for a server marked as bring your own key, the gateway can hold the OAuth token a person obtained through the authorization code flow, and a server can declare environment variables that each person fills in with their own values. In every case the credential is stored encrypted against the user id, never shown again, and injected when that person's session calls the server.
A real-world scenario
An internal search portal issues personal tokens, and search results depend on who is asking. The admin registers the portal's MCP server as bring your own key with two lines of instructions and a link to the token page. Each engineer clicks Connect on the server card, pastes their token once, and from then on their assistant searches as them. When an engineer moves teams, the admin opens the server's User Credentials tab and revokes that one credential.
Bring your own key
-
Register the server with
is_byok: true. In the Admin UI, the server form has a BYOK (Bring Your Own Key) switch with Access Description lines and an API Key Help URL. In the API orconfig.yaml:mcp_servers:internal-search:url: https://search.internal.example.com/mcptransport: httpauth_type: api_keyis_byok: truebyok_description:- "Create a personal token on the search portal"- "Paste it here, the gateway stores it for your account only"byok_api_key_help_url: https://search.internal.example.com/tokens -
Each user connects. On MCP Servers the card shows a BYOK credential row with Connect. The dialog shows the description and help link, then Provide API Key. Connect & Authorize stores the key. The same from a terminal, with the user's own virtual key:
curl -X POST http://localhost:4000/v1/mcp/server/8f1c2d3e-4b5a-4c6d-9e7f-0a1b2c3d4e5f/user-credential \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"credential": "srch_live_9f2a6c1e", "save": true}'Expected result:
{"server_id": "8f1c2d3e-4b5a-4c6d-9e7f-0a1b2c3d4e5f", "has_credential": true} -
The card now reads Connected with an Update link. Until a user has stored a key, the aggregate endpoint
/mcpand toolset endpoints list one{server}-connecttool for that server instead of its tools, and calling it returns the link to the connect page, as lazy authentication explains. A client that connects to the server by name gets a sign-in challenge instead. To remove a stored key, open Connect in the Admin UI (or go to/ui/connect) and click Disconnect on the server, or sendDELETE /v1/mcp/server/{server_id}/user-credential. With audit logging on, storing, replacing and removing a key each write an audit row that names who made the change and never contains the key. An admin sees who has connected on the server's User Credentials tab, or withGET /v1/mcp/server/{server_id}/user-credentials, which lists users and timestamps and never the secrets.
MCP clients that cannot show a form go through a minimal OAuth 2.1 authorization code flow instead. When a client connects to a bring your own key server with no stored credential, the gateway answers 401 byok_auth_required with a WWW-Authenticate header that points at /.well-known/oauth-authorization-server. The client opens GET /v1/mcp/oauth/authorize in the browser, a page that asks for the API key. The person must be signed in to the gateway, and the client's redirect address must be a loopback address such as http://127.0.0.1:<port>/callback. Submitting the page checks that the server exists, takes personal keys and is one the person may use, then hands the client a short code. The key is stored for that person only when the client exchanges the code at POST /v1/mcp/oauth/token, which also returns a bearer token for one hour. If the key cannot be saved, for example because the gateway has no database, no token is issued and the client gets server_error.
Authorization codes are PKCE bound, expire after five minutes and live in the memory of the gateway worker that issued them, so with several replicas the browser page and the token exchange must reach the same replica, for example through sticky sessions. The connect page in the Admin UI does not have this limit.
Per-user OAuth tokens
For auth_type: oauth2 servers using the authorization code flow, each user authorises once through the Admin UI, the Chat UI integrations page, or the gateway's own connect page. The gateway stores the access and refresh tokens for that user and refreshes them before they expire.
-
On MCP Servers, click Connect on the server card and sign in to the provider in the browser. The card reads Connected.
-
Check the stored token without exposing it:
curl http://localhost:4000/v1/mcp/server/8f1c2d3e-4b5a-4c6d-9e7f-0a1b2c3d4e5f/oauth-user-credential/status \-H "Authorization: Bearer $SPANWAY_API_KEY"Expected result:
{"server_id": "8f1c2d3e-4b5a-4c6d-9e7f-0a1b2c3d4e5f", "has_credential": true, "expires_at": "2026-11-21T18:04:12Z", "is_expired": false, "connected_at": "2026-11-21T17:04:12Z"} -
GET /v1/mcp/user-credentialslists every server the calling user has connected.DELETE .../oauth-user-credentialremoves the token, and a proxy admin may passuser_idto revoke another user's token, which is what Revoke on the User Credentials tab does.POST .../oauth-user-credentialstores a token the client obtained itself, withaccess_token, optionalrefresh_token,expires_inandscopes.
Per-user environment variables
A server can list env_vars that differ per user, such as a workspace id or a personal token for a stdio server. In the server form these are Variables with the scope Per-user.
-
On MCP Servers, a card whose per-user variables you have not filled in has a red border and reads "N user fields missing". Click Set, fill in the values in Set your credentials and click Save Credentials. Submitted values are merged over stored ones, so only changed fields need to be sent, and saved values are never shown back.
-
The same from a terminal:
curl -X POST http://localhost:4000/v1/mcp/server/8f1c2d3e-4b5a-4c6d-9e7f-0a1b2c3d4e5f/user-env-vars \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"values": {"WORKSPACE_ID": "acme-eu"}}'Expected result:
{"server_id": "8f1c2d3e-4b5a-4c6d-9e7f-0a1b2c3d4e5f", "server_name": "workspace_tools", "required": [{"name": "WORKSPACE_ID", "is_set": true}], "missing_count": 0} -
GET /v1/mcp/server/{server_id}/user-env-varsshows which variables are set and which are missing,DELETEclears them, andGET /v1/mcp/user-env-vars/statusdoes the same across every server the user can access, which the dashboard uses to flag servers that still need input. A call to a server with missing values fails with status 412 until they are set.
When an admin changes a server's URL, the gateway deletes every user's stored environment variable values and bring your own key for that server before it saves the new URL, so nothing a user entered for the old upstream is ever sent to the new one. Users see the server as needing input again and enter their values afresh. Per-user OAuth tokens are cleared the same way whenever a field that decides which token gets minted changes.
Routes
{{ops:^/v1/mcp/server/{server_id}/(user-credential|user-credentials|oauth-user-credential|oauth-user-credential/status|user-env-vars)$|^/v1/mcp/(user-credentials|user-env-vars/status)$}}
Errors
| Status | Where | Meaning | Fix |
|---|---|---|---|
401 byok_auth_required "No stored credential found for this BYOK server. Complete the OAuth authorization flow to provide your API key." | A tool call to a bring your own key server reached by name | The user has not stored a key for this server. The answer carries a WWW-Authenticate header that starts the connect flow. On /mcp and toolset endpoints the user sees the {server}-connect tool instead. | The user clicks Connect on the server card, or lets the client run the flow. |
401 byok_auth_required "User identity is required for BYOK servers" | The same | The key belongs to no user, so there is nobody whose key to use. | Call with a key that belongs to a user. |
503 byok_auth_unavailable "BYOK credential check requires a database connection." | The same | The gateway cannot read stored keys without its database, so it refuses the call instead of using another credential. | Check the database. |
| Upstream 401 or 403 relayed as a tool error | A tool call to a per-user OAuth server | The user's stored token expired and could not be refreshed, or the upstream refused it. | The user clicks Connect or Update on the server card. |
400 This MCP server does not support BYOK credentials or 403 User does not have permission to access mcp server with id ... | Submitting the BYOK connect page | The server does not take personal keys, does not exist, or the person may not use it. | Connect to a server the person has access to. |
401 login_required | BYOK connect page | The person is not signed in to the gateway. | Sign in to the Admin UI, then open the page again. |
400 Only S256 code_challenge_method is supported | BYOK connect page | The client sent a weaker PKCE method. | Use a client that sends S256. |
503 Too many pending authorization flows | BYOK connect page | Too many connect flows were started and not finished. | Wait a few minutes and try again. |
invalid_grant, unsupported_grant_type or server_error | POST /v1/mcp/oauth/token | The code expired, was used, came from another replica or failed the PKCE check, the grant type is wrong, or the key could not be saved. | Start the connect flow again. For server_error, check the database. |
412 missing_user_env_vars | Tool calls, POST /mcp-rest/tools/call | The server declares per-user variables the user has not filled in. The message names them. | Click Set on the card, or post the values to .../user-env-vars. |
| "Please enter your API key" | BYOK connect dialog | The key field was left empty. | Paste the key. |
400 This MCP server does not support BYOK credentials | POST .../user-credential | The server is not marked as bring your own key, so it takes no personal keys. | Turn on BYOK (Bring Your Own Key) on the server, or use the server's shared credential. |
400 User ID not found in token | POST and DELETE .../user-credential | The virtual key belongs to no user, so there is nobody to store the key for. | Use a key that belongs to a user, or sign in to the Admin UI and connect from there. |
403 Proxy admin access required to revoke another user's MCP credential. | DELETE with user_id on .../user-credential, .../oauth-user-credential | Only proxy admins may revoke another user's credential. | Use an admin key, or omit user_id to revoke your own. |
403 User does not have permission to access mcp server with id ... | POST .../user-credential | The caller is not a proxy admin and the server is not one their key, team or toolsets grant. | Ask an admin to grant the server, or check the server id. |
404 MCP Server ... not found | The per-user routes .../user-credential, .../oauth-user-credential and .../user-env-vars, as a proxy admin | No server in the database or in config.yaml has that id. A caller who is not an admin gets the 403 above instead, so an id they cannot use and one that does not exist look the same. | Copy the id from the server card. |
500 Database not connected. Connect a database to your proxy | POST and DELETE .../user-credential | The gateway runs without a database, so it cannot store personal keys. | Set DATABASE_URL and restart. |
| Card still reads "N user fields missing" after a URL change | MCP Servers | The admin changed the server's URL, which cleared every user's values. | Enter the values again. |
DELETE .../user-credential answers 200 with has_credential false even when no key was stored, so removing a key twice is safe. It never removes an OAuth token stored for the same server, use DELETE .../oauth-user-credential for that.
Storing, replacing and removing a personal key, an OAuth token or your per-user variables is audited, including an admin revoking someone else's credential. The rows name the server and the user, and for variables only their names, never a value, see audit logs.
Logs
| Level | Line | Meaning |
|---|---|---|
| ERROR | byok_token: failed to store user credential for user=... server=...: ... | The OAuth connect flow could not save the key, usually because the database is unreachable. The client gets server_error. Check the database connection. |
| INFO | MCP server <id>: URL changed, cleared <n> stored per-user env var or BYOK row(s) | An admin changed the server's URL, so the gateway removed the values users had stored for the old address. Users enter them again. |
The tool listing lines for servers that still need a key are in the lazy authentication Logs table.
Related
- MCP lazy authentication for the connect tool that
/mcpand toolset endpoints offer until a user has connected. - Audit logs for the rows written when a personal key is stored or removed.
- MCP OAuth for the interactive flow and the gateway hosted authorization server.
- MCP access control for which servers a user may reach at all.
- OpenAPI specs as MCP tools for APIs where each user has their own key.
- Chat UI for the Integrations and Credentials pages users see.
- The MCP Servers page for the User Credentials tab.