Skip to main content

MCP lazy authentication

Some MCP servers act as the person using them. The gateway holds an OAuth token or a bring your own key secret per user for those servers, and until a user has connected one, the gateway has nothing to send upstream for them. When a client connects to one server by name, the gateway answers with a sign-in challenge straight away. On the aggregate endpoint at /mcp and on a toolset endpoint at /toolset/{toolset_name}/mcp that is not possible, because one unauthenticated server must not block the others. Lazy authentication is what happens there instead: the servers the user has connected list their tools as usual, and every server that still needs the user's own credential lists one synthetic tool named {server}-connect. Calling that tool returns the link to the gateway's connect page, the user signs in there once, and the next tools/list shows the real tools.

Which servers get a connect tool​

A server contributes a connect tool when the gateway can supply the missing credential through its own connect page, which covers two kinds of server.

ServerConnect tool when
auth_type: oauth2 with the authorization code flow and gateway-managed custody (delegate_auth_to_upstream off)The caller's user has no vaulted token for the server, or the upstream rejected the stored token.
is_byok: true with an API key or bearer token schemeThe caller's user has not stored a key and the client did not send one in a per-server header, or the upstream rejected the stored key.

Servers that use a shared credential, machine to machine OAuth (client_credentials), token exchange, or a token the client itself forwards never show a connect tool. Their listing outcome still reports auth_required when the upstream rejects them, as before.

If the server's tool catalogue is pinned, the pinned tools are listed next to the connect tool, filtered by the server's allowed tools and by the caller's key, team and toolset permissions exactly as a live listing would be. The agent can see what it will be able to do once the user has connected, and a toolset never widens because the upstream was skipped.

A server built from an OpenAPI specification keeps its whole catalogue in the gateway, so it needs no upstream call to list tools. Such a server lists the connect tool first and its normal tools after it, under the same filters, while the user's key is still missing. Only a tool call needs the key.

Scenario​

A support team has a toolset support-agent with tools from a ticketing server and a knowledge base server. The ticketing server is OAuth per user, the knowledge base uses a shared key. A new engineer points a desktop assistant at http://localhost:4000/toolset/support-agent/mcp with their virtual key. The assistant lists tools and sees the knowledge base tools plus jira-connect. When the engineer asks for an open ticket, the assistant calls jira-connect, gets the connect link back, and shows it. The engineer opens the link, signs in to the gateway, clicks Connect on the ticketing server, and the next listing in the assistant shows the ticket tools.

Steps​

  1. In the Admin UI, open MCP Servers under AI Gateway (or go to /ui/mcp-servers) and register the server with auth_type set to oauth2 and the authorization code flow, or pick the API Key or Bearer Token auth type and turn on BYOK (Bring Your Own Key) for a server where each user brings their own key. The switch is on the add and edit forms for HTTP, SSE and OpenAPI servers. Grant it to the key or team, directly or through a toolset.

  2. Connect an MCP client to http://localhost:4000/mcp or http://localhost:4000/toolset/{toolset_name}/mcp with a virtual key that belongs to a user, and list tools.

    curl -s http://localhost:4000/mcp \
    -H "Authorization: Bearer $SPANWAY_API_KEY" \
    -H "Accept: application/json, text/event-stream" \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}'
  3. Call the connect tool to get the link.

    curl -s http://localhost:4000/mcp \
    -H "Authorization: Bearer $SPANWAY_API_KEY" \
    -H "Accept: application/json, text/event-stream" \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"jira-connect","arguments":{}}}'
  4. Open the link in a browser, sign in to the gateway, and click Connect on the server. An OAuth server sends you to the upstream sign-in and back. A bring your own key server opens a dialog that asks for your API key and stores it for your account. The same page is reachable at any time at /ui/connect.

  5. List tools again. The connect tool is gone and the server's tools are listed.

Expected result​

The first tools/list contains the connected servers' tools and one connect tool for each server still waiting for the user. The per-server outcome in the result's _meta names the gateway connect page.

{
"tools": [
{"name": "confluence-search_pages", "description": "Search the knowledge base", "inputSchema": {"type": "object"}},
{
"name": "jira-connect",
"title": "Connect jira",
"description": "jira needs you to sign in before its tools can be used. Call this tool to get the sign-in link, open the link in a browser, finish signing in, then list the tools again.",
"inputSchema": {"type": "object", "properties": {}, "additionalProperties": false},
"_meta": {"spanway.ai/connect_url": "https://gateway.example.com/ui/connect"}
}
],
"_meta": {
"spanway.ai/server_outcomes": {
"confluence": {"status": "ok", "tool_count": 1},
"jira": {"status": "auth_required", "http_status": 401, "connect_url": "https://gateway.example.com/ui/connect"}
}
}
}

The tools/call on jira-connect returns a text block and structured content that both carry the link.

{
"content": [{"type": "text", "text": "Open https://gateway.example.com/ui/connect in a browser, connect jira, then list the tools again."}],
"structuredContent": {"connect_url": "https://gateway.example.com/ui/connect", "server": "jira"}
}

The link is built from the PROXY_BASE_URL environment variable when it is set, otherwise from the request the client made. X-Forwarded-* headers are only honoured when general_settings.use_x_forwarded_for is on and the request comes from an address inside general_settings.trusted_proxy_ranges, see the settings reference. The REST listing at GET /mcp-rest/tools/list never shows connect tools or a connect_url, because it lists each server directly and reports a server that still needs the user's credential as auth_required. Calling the connect tool through POST /mcp-rest/tools/call works, and when PROXY_BASE_URL is not set that call receives the relative path /ui/connect, because there is no MCP session to take the host from.

Errors​

SymptomMeaningFix
A tools/call on {server}-connect returns a tool error saying the upstream has no tool named connect.The user has already connected the server, so the call was routed upstream like any other tool.List tools again. The connect tool disappears once a credential exists.
The server outcome says auth_required but there is no connect_url and no connect tool.The server's credential is not one the gateway can collect on its connect page, for example a shared key or a client-forwarded token that the upstream rejected.Fix the server's stored credential in MCP Servers under AI Gateway, or have the client send a valid upstream token.
The connect link is /ui/connect without a host.The connect tool was called through POST /mcp-rest/tools/call, outside an MCP session, and PROXY_BASE_URL is not set.Set PROXY_BASE_URL to the gateway's public URL, or prefix the path with the gateway host.

Logs​

LevelLineMeaning
INFOMCP list_tools: {server} has no per-user credential for this caller; serving its connect toolNormal. A user listed tools before connecting the server. No action needed.
INFOMCP list_tools: {server} has no per-user credential for this caller; serving its connect tool ahead of its local catalogueNormal. A user listed an OpenAPI server before storing their key, so the connect tool was placed ahead of the server's tools. No action needed.
INFOMCP list_tools: upstream {server} rejected the stored per-user credential (HTTP {status}); serving its connect toolThe user's stored token or key no longer works upstream and they have been asked to connect again. If many users hit this at once, check the server's OAuth client or key settings.
INFOMCP tools/call: connect tool for {server} called; returning the connect URLNormal. The client asked for the sign-in link.