Guides
MCP authentication by example
Every way a credential moves around an MCP call, as a worked example: who holds it, what you write, and what each side sees. The mechanisms behind them are in MCP servers and Security context. How you authenticate to the platform's own API is Authentication.
What is supported
Who is asking, whose agent asks for them, and whose MCP server answers. You are the developer deploying here; a third party runs its MCP server somewhere else.
| User | Agent provider | MCP provider | Supported? |
|---|---|---|---|
| Your user, in your app | You | You, in the same project | Yes. No credential (1), or as the user with their token (7) |
| Your user, in your app | You | A third party, with your key | Yes (5) |
| Your user, in your app | You | A third party, with the user's OAuth 2 sign-in | Yes, two ways. The platform runs the sign-in and keeps the tokens — connected accounts — or your app runs it and passes each run the access token (6) |
| Your user, in your app | You | A third party, signing the user in mid-conversation | Yes, with connected accounts: the run pauses with a sign-in link and carries on by itself once the user signs in. No token enters the conversation |
| Your user, in your app | You | A third party that wants a DPoP-bound token, a client certificate, plain HTTP or a port other than 443 | No. Bearer tokens, over HTTPS on 443 |
| Your user, in your app | You | You, in another project, or another tenant | No. The gateway admits only agents in the server's own project |
| Your user, in your app | You | You, and your server calls a third party as the user | Yours to build. The platform exchanges no tokens: keep each user's third-party token in your server's own database, keyed by the verified sub (3) |
| Someone outside | Their MCP app (Claude, ChatGPT, Cursor…) | You, with auth: server | Yes (3). They sign in with your provider |
| Someone outside | Their MCP app | You, with auth: platform | Not yet. The platform's sign-in issues tokens to machines only. An app that takes a custom header can use a minted token for an hour (2); otherwise use auth: server |
| Your backend or a script | None | You, public | Yes (2) |
| You, in the console | The Inspector | You, private or public | Yes. The server page's Inspector tab, as you |
| Anyone, anonymously | Anything | You, public, checking nobody | No, by design. A public server always has a caller check: the platform's, or your verifier |
Which one is yours
| # | Situation | The credential is | You write |
|---|---|---|---|
| 1 | Your agents call your MCP server | None. The platform knows your agent | server= on the binding |
| 2 | A script or backend calls your public server | A token the platform mints for that server | auth: platform and elan mcp token |
| 3 | People connect it from their MCP app and sign in (OAuth 2) | Your sign-in provider's token | auth: server and a verifier |
| 4 | Your MCP server calls an API with your key | Yours, in a secret | secrets: and egress: |
| 5 | Your agent calls another MCP server with your key | Yours, in a secret | credentials.own and ToolAuth |
| 6 | Your agent acts as your user, who signed in in your app (OAuth 2) | Your user's access token, per invocation | credentials.caller, ToolAuth and security_context |
| 7 | Your agent calls your own server as your user | Your user's token for that server | 6, with server= instead of a URL |
| 8 | A Python tool needs a key or a token | Your user's, yours, or one your code mints | auth_config or a credential service |
| 9 | Your agent acts as your user at a third party, and the platform signs them in (OAuth 2) | Your user's tokens, kept by the platform | connections: and credentials.connections |
- A server's
authdecides who can call it from outside (2 and 3). The agent'scredentials=decides what it sends when it calls out (5 to 8). The two are independent, and a server can be called both ways at once. - MCP App views add no sign-in of their own. See MCP App views.
1. Your agents call your MCP server
No credential at all. The gateway admits only this project's own agents and tells your server which run and which user the call is for.
from agentkit_core.manifest import ToolBinding
tools=[ToolBinding(kind="mcp", ref="tides", server="tide-tables",
tool_filter=["high_tide"])],from fastmcp import FastMCP
from fastmcp.server.dependencies import get_http_headers
MANIFEST = {"slug": "tide-tables", "version": "0.1.0", "auth": "platform"}
def build(ctx) -> FastMCP:
server = FastMCP("Tide Tables")
@server.tool()
def high_tide(port: str) -> str:
"""The next high tide at a port."""
sent = get_http_headers()
user = sent.get("x-agentkit-subject", "") # the run's user
run = sent.get("x-agentkit-invocation", "") # which run asked
...
return server- Works for any
expose, and is how a private server is called at all. - The subject is your agent's word, not a signature. Trust it as far as “an agent in this project says so”. To act as a user you have verified, use 7.
- Billed at the from your agents rates, never the public ones.
2. A script or backend calls your public server
With auth: platform (the default) and expose: public, the platform checks the caller, so your server verifies nothing. The caller sends a token minted for that one server:
elan mcp token tide-tables > token.txt # an hour, this server only
curl https://api.acme.example/mcp/acme/salmon/tide-tables \
-H "Authorization: Bearer $(cat token.txt)" \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-H "MCP-Protocol-Version: 2025-06-18" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'- Three ways to mint one: the server's Connect tab in the console,
elan mcp token, or, from a backend with a service user,POST /v1/tokenwithgrant_type=client_credentialsandresource=the server's URL. - The token stops at the gateway. Your server sees
X-Agentkit-Caller: externaland the token's holder inX-Agentkit-Subject, never the token. - It works for that one server and nothing else on the platform, so pasting it into an MCP client's config risks one server for an hour.
3. People connect it from their MCP app and sign in (OAuth 2)
For Claude, ChatGPT, Cursor, VS Code or any MCP client that signs its user in with OAuth 2. Your server is the resource server and verifies tokens from your sign-in provider:
from fastmcp import FastMCP
from fastmcp.server.auth import RemoteAuthProvider
from fastmcp.server.auth.providers.jwt import JWTVerifier
from fastmcp.server.dependencies import get_access_token
MANIFEST = {"slug": "notes", "version": "0.1.0", "auth": "server"}
ISSUER = "https://login.acme.example/"
def build(ctx) -> FastMCP:
verifier = JWTVerifier(
jwks_uri=ISSUER + ".well-known/jwks.json", # fetched on first use
issuer=ISSUER,
audience=ctx.public_url, # tokens issued for THIS server only
)
server = FastMCP("Notes", auth=RemoteAuthProvider(
token_verifier=verifier,
authorization_servers=[ISSUER],
base_url=ctx.public_origin, # FastMCP adds the server's path itself
))
@server.tool()
def my_notes() -> list[str]:
"""The signed-in user's notes."""
user = get_access_token().claims["sub"] # verified, never from the body
...
return servertargets:
notes:
expose: public- Somebody adds the server's URL to their MCP app.
- The first call is answered
401naming your server's metadata, which names your provider. - They sign in there. The app receives a token whose audience is your server's URL.
- Every call carries it. The gateway passes
Authorizationthrough untouched, and your verifier checks it.
- Your provider must issue a token for this URL, as its audience. Most call that the API identifier or the resource indicator.
- The app has to be a client of your provider: either your provider lets MCP apps register themselves, or you register each one.
- Refused before your code runs: a token this platform issued, and, at deploy, a public
auth: serverserver that configures no verifier. - The platform does not know who your callers are. Observability lists them as public and not identified; your server knows them by
sub. - The same server code serves your own agents too, through 7.
4. Your MCP server calls an API with your key
One key of yours, used for every caller. It reaches the server as an environment variable.
targets:
tide-tables:
egress:
- host: api.tides.example # this server's own allowlist
secrets:
- {name: tides-api-key, env: TIDES_API_KEY}elan secret set secrets tides-api-key # reads stdinimport os
import httpx
def build(ctx) -> FastMCP:
server = FastMCP("Tide Tables")
@server.tool()
async def high_tide(port: str) -> str:
"""The next high tide at a port."""
async with httpx.AsyncClient() as http: # uses the egress proxy
answer = await http.get(
"https://api.tides.example/v1/high", params={"port": port},
headers={"X-API-Key": os.environ["TIDES_API_KEY"]})
return answer.text
return server- Read it inside the tool.
build()also runs at deploy, offline, to list your tools. - The host is this server's alone. Your agents cannot reach it, and this server cannot reach theirs. HTTPS on 443 only.
- Use a client that honors the proxy.
httpxdoes by default; aiohttp needstrust_env=True. One that ignores it times out rather than failing. - Setting, rotating and removing the value: Secrets.
5. Your agent calls another MCP server with your key
A third-party server that takes one key, yours, for every user. Declare it as the agent's own credential and name it on the binding:
from agentkit_core.manifest import (
AgentManifest, Credentials, FromEnv, ToolAuth, ToolBinding,
)
MANIFEST = AgentManifest(
slug="triage",
version="0.1.0",
credentials=Credentials(own={
"tracker": FromEnv(env="TRACKER_KEY", kind="bearer",
resource="https://mcp.tracker.example/mcp"),
}),
tools=[
ToolBinding(kind="mcp", ref="tracker", url="https://mcp.tracker.example/mcp",
auth=ToolAuth(credential="tracker")),
],
...
)targets:
triage:
egress:
- host: mcp.tracker.example
secrets:
- {name: tracker-key, env: TRACKER_KEY} # fills credentials.own["tracker"]- Sent as
Authorization: Bearer. For a key in a header of its own, usekind="api_key", header="X-Tracker-Key". - The tracker sees your agent, not your user. Every user shares what this key can do.
- A caller cannot replace it: a credential named
trackerin an invocation is ignored. - The deploy refuses it if no secret fills
TRACKER_KEY.
6. Your agent acts as your user, who signed in in your app (OAuth 2)
Your user uses your agent, and your agent uses a third party's MCP server that signs people in with OAuth 2 — a tracker, a CRM, a calendar. Your user connects their account in your app, and your agent then works in it as them. The OAuth 2 client is your app; the platform only carries the resulting access token for one run.
Once, while you build: find its sign-in
- Read the server's Protected Resource Metadata. Its
authorization_serversnames the sign-in to use. A call with no token is answered401naming the same URL. - Read that sign-in's own metadata for its authorize, token and, if it has one, registration endpoints.
- Register your app there as a client: in the provider's developer settings, or through its registration endpoint. The redirect URI is your app's. The platform is not part of this flow.
curl https://mcp.tracker.example/.well-known/oauth-protected-resource/mcpFor each user: connect, then invoke
- A Connect tracker button in your app starts the authorization code flow with PKCE, asking for a token for
resource=https://mcp.tracker.example/mcpso it is issued for that server. - Your backend exchanges the code and keeps the refresh token, encrypted, per user.
- On each invoke it sends a fresh access token in the
security_context, and the agent attaches it to the one binding that names it.
MANIFEST = AgentManifest(
slug="triage",
version="0.1.0",
# The invoking user's, sent with each invocation.
credentials=Credentials(caller=["tracker"]),
tools=[
ToolBinding(kind="mcp", ref="tracker", url="https://mcp.tracker.example/mcp",
auth=ToolAuth(credential="tracker")),
],
...
)from elan_sdk import Elan
from elan_sdk.security import OAuth2, SecurityContext
elan = Elan.from_env()
# Your OAuth client, on your backend: it holds Ada's refresh token and
# returns a short-lived access token. The refresh token never leaves it.
access_token = tracker_tokens.access_token_for("ada")
accepted = elan.runtime.invoke(
"triage",
"What is assigned to me this week?",
project="salmon",
session_id="sess-ada-1",
end_user_id="ada",
security_context=SecurityContext(credentials=[
OAuth2(name="tracker", access_token=access_token,
scopes=["issues:read"], resource="https://mcp.tracker.example/mcp"),
]),
)- Only the access token reaches the platform. A
refresh_tokenorclient_secretis refused by name. - Set
resource, so not even a mistake in agent code can send the token anywhere else. - Nothing is stored. The token is sealed for the run and deleted when it ends. A run waiting for approval keeps it until
ttl_seconds; replace it withrefresh_security_contextbefore approving one that waited longer. - Ask before you invoke. Your app holds the tokens, so it knows whether this user has connected; show Connect tracker instead of starting a run that cannot work.
- A run that starts without one anyway ends with an
errorevent of typeCredentialNotSupplied, and one whose token runs out mid-run withCredentialExpired. The tracker is never called without it. To let the agent carry on without the tracker's tools instead, useToolAuth(credential="tracker", required=False). - The agent cannot sign the user in itself. It has no browser to show a consent screen in, and ADK's interactive sign-in writes the token into the stored conversation, so the platform ends a run that asks for it.
- The agent still needs the host under
egress:, as in 5. - To try it before your app exists, the Playground's run options take one test credential.
7. Your agent calls your own server as your user
Your own auth: server server from 3, called by your agent with the user's token from your provider. The binding names the server instead of a URL:
credentials=Credentials(caller=["notes"]),
tools=[
ToolBinding(kind="mcp", ref="notes", server="notes",
auth=ToolAuth(credential="notes")),
],from elan_sdk import Elan
from elan_sdk.security import Bearer, SecurityContext
elan = Elan.from_env()
accepted = elan.runtime.invoke(
"notes-assistant",
"What did I write about the launch?",
project="salmon",
session_id="sess-ada-1",
end_user_id="ada",
security_context=SecurityContext(credentials=[
# Issued by your sign-in provider, for the server's public URL.
Bearer(name="notes", token=ada_notes_token,
resource="https://api.acme.example/mcp/acme/salmon/notes"),
]),
)- Bind the token to the server's public URL,
https://<api host>/mcp/<tenant>/<project>/<server>. That is the audience its tokens are issued for, and what the reach rule checks, even though the call itself stays inside the cluster. - No egress rule. The call goes through the gateway, which passes
Authorizationthrough to anauth: serverserver untouched. - One server, two kinds of caller. The same verifier accepts your agents' calls here and people's calls from their MCP apps in 3, and acts as the token's
subin both. - The server does not need
expose: publicfor this; a private one is reached the same way.
8. A Python tool needs a key or a token
The same three sources reach a function tool through ADK's own credential API, answered by the platform's credential service:
| Whose | Declare | Read it with |
|---|---|---|
| Your user's, per invocation | credentials.caller | AuthenticatedFunctionTool(auth_config=auth_config(name)) |
| Yours, from a secret | credentials.own | security.headers(name, url=…, context=tool_context) |
| One your code mints, like a back-office token | credentials.service | tool_context.load_credential(…) |
Worked examples of all three are in In a function tool and Your own service.
MCP App views
- A view travels with its tool's call. Its
ui://resource is read over the same binding, so whatever credential the binding sends (1, 5, 6 or 7) is sent for the view too. There is nothing extra to declare. - A view cannot sign anybody in. In this console it runs in a frame allowed scripts only: no pop-ups, no cookies of its own, no access to the page around it. Sign-in happens in your app (6 and 7) or in the MCP app (3).
- A view shows what its tool returned, and the tool computed that as the verified user. Do per-user work in the tool, never in the view.
What your server sees
| The call comes from | Authorization | X-Agentkit-Caller | X-Agentkit-Subject |
|---|---|---|---|
| Your agent (1) | None | The workload the agent runs in | The run's user, as your agent states it |
| Your agent, as your user (7) | The user's token | The workload the agent runs in | The run's user, as your agent states it |
| A platform token (2) | None. It stops at the gateway | external | Whoever the token was minted for |
| An MCP app, signed in (3) | The caller's token | external | Not set. Read sub from the token |
| The console's Inspector | A pasted token, for auth: server only | inspector | The person signed in to the console |
- Every call also carries
X-Agentkit-TenantandX-Agentkit-Project, and an agent's call carriesX-Agentkit-Invocation, the run it came from. - Only the platform sets them. Any
X-Agentkit-*header a caller sends is removed first, and nothing but the gateway can reach your server. get_http_headers()leaves outAuthorization; read a verified token withget_access_token().