The Two Fundamental Questions
Before writing any code, answer these two questions:| Question | What It Determines |
|---|---|
| Who is calling the Gateway? | The Gateway Token - identifies the caller to TrueFoundry |
| Whose credentials access the downstream service? | The MCP Server Auth - determines whose data/permissions are used |
Quick Start: Which Scenario Are You?
Scenario A: Agent Acts on YOUR Behalf (Developer/Service Mode)
Scenario A: Agent Acts on YOUR Behalf (Developer/Service Mode)
Your agent performs actions using your organization’s credentials - like a service account that does work on behalf of your company.Examples:
Key point: You pass ONE token (Virtual Account). The MCP server’s shared credentials are configured in the UI, not in your code.
- An internal support bot that queries your company’s knowledge base
- A code assistant that uses a shared GitHub service account
- A data analysis agent that accesses shared analytics databases
| Token Type | What to Use | Why |
|---|---|---|
| Gateway Token | Virtual Account token | Identifies your service/application |
| MCP Server Auth | Shared credentials (pre-configured) | All requests use the same service account |
Scenario B: Agent Acts on USER'S Behalf (Per-User Mode)
Scenario B: Agent Acts on USER'S Behalf (Per-User Mode)
Your agent performs actions using the end user’s credentials - the agent accesses each user’s own data and respects their permissions.Examples:
Key point: You pass the USER’s token. TrueFoundry looks up and injects the OAuth token for that specific user.
- A productivity agent that accesses the user’s own Gmail, Slack, or Calendar
- A development assistant that accesses the user’s GitHub repositories
- A CRM agent that sees only what the logged-in user can see
| Token Type | What to Use | Why |
|---|---|---|
| Gateway Token | User’s token (TFY PAT or IdP JWT) | Identifies which user is making the request |
| MCP Server Auth | Per-user OAuth (managed by TrueFoundry) | Each user’s own credentials |
Scenario C: Hybrid - Some Tools Shared, Some Per-User
Scenario C: Hybrid - Some Tools Shared, Some Per-User
Quick Decision Guide
Summary: Answering the Key Questions
Q1: My agent impersonates/acts on MY behalf (developer/service)
Q1: My agent impersonates/acts on MY behalf (developer/service)
Use:
- Gateway Token: Virtual Account token
- MCP Server Auth: Static headers (configured once in UI)
Q2: My agent acts on the USER's behalf
Q2: My agent acts on the USER's behalf
Use:
- Gateway Token: User’s TFY PAT or IdP JWT
- MCP Server Auth: OAuth2 (per-user)
Q3: Some MCP servers need OAuth, some need headers
Q3: Some MCP servers need OAuth, some need headers
Configure each MCP server with its appropriate auth model in the UI. Pass one Gateway token. The Gateway handles injecting the right credentials for each server automatically.You don’t need different code paths for different auth types.
Q4: Which token(s) do I pass?
Q4: Which token(s) do I pass?
You always pass ONE Gateway token in the
Authorization header.The MCP server auth is handled by the Gateway based on each server’s configuration:- OAuth servers: Gateway looks up stored tokens for the user
- Static Header servers: Gateway injects pre-configured credentials
- Passthrough servers: Gateway passes your JWT unchanged
x-tfy-mcp-headers if you want to override the default auth.Prerequisites
Before you begin, ensure you have:- Access to TrueFoundry: A TrueFoundry account with access to the AI Gateway
- MCP server registered: At least one MCP server registered in the gateway (see Getting Started)
-
Authentication token: One of the following:
- TrueFoundry Personal Access Token (PAT)
- Virtual Account token
- JWT from your Identity Provider (if using External Identity)
-
Python environment with the
fastmcppackage:
Connecting to an MCP Server
The MCP Gateway exposes each registered MCP server at a unique URL.MCP Server URL Format
<control-plane-url>: Your TrueFoundry control plane URL<integration-id>: The unique identifier for the MCP server (visible in the UI)
Basic Connection Example
Detailed Implementation Examples
Using TrueFoundry Personal Access Token (PAT)
For internal developers with TrueFoundry accounts.1
Generate a Personal Access Token
- Navigate to Settings > API Keys in the TrueFoundry UI
- Click Generate New API Key
- Store the token securely
2
Use the token in your code
Using Virtual Accounts (Service-to-Service)
For backend services, automated pipelines, or when individual user identity isn’t required.1
Create a Virtual Account
- Navigate to Settings > Virtual Accounts in the TrueFoundry UI
- Click Create Virtual Account
- Give it a descriptive name (e.g.,
my-agent-mcp-access) - Add permissions for the MCP servers your application needs
- Generate and securely store the token
2
Use the Virtual Account token
Handling OAuth-Protected MCP Servers
When an MCP server is configured with OAuth2 and the user hasn’t completed the OAuth flow, the Gateway returns an error with an authorization URL. Handling OAuth Errors in Code:OAuth errors occur during connection, not when calling individual tools. The Gateway checks for valid OAuth tokens during the initial handshake.
Token Passthrough for Custom MCP Servers
For MCP servers that validate JWTs from your Identity Provider directly.With Token Passthrough, the MCP server validates the JWT directly against your IdP’s configuration. The Gateway passes the JWT unchanged.
Mixing OAuth and Header-Based MCP Servers
A common question: “My agent uses Gmail (OAuth) and a web search API (header-based). Do I need to handle them differently?” Answer: No. The Gateway handles this for you.How It Works
-
Configure each MCP server with its auth model in the UI:
- Gmail MCP Server → OAuth2
- Web Search MCP Server → Static Header (API key)
- In your code, pass ONE Gateway token:
- The Gateway handles auth per server:
| MCP Server | Auth Model | What Gateway Does |
|---|---|---|
| Web Search | Static Header | Injects pre-configured API key |
| Gmail | OAuth2 | Looks up user’s OAuth token, injects it |
| Calculator | No Auth | Passes request as-is |
Overriding Authentication with Custom Headers
If you need to override the default auth for a specific MCP server, use thex-tfy-mcp-headers header.
For a Single MCP Server
For Virtual MCP Servers
When connecting to a Virtual MCP Server (which aggregates multiple MCP servers), use a nested structure:Complete Example: Building an MCP-Enabled Agent
Here’s a complete example with proper error handling for multiple MCP servers:Troubleshooting
Connection refused or timeout
Connection refused or timeout
- Verify the MCP server URL is correct (copy from TrueFoundry UI)
- Check that the MCP server is running and healthy
- Ensure your network can reach the TrueFoundry control plane
401 Unauthorized
401 Unauthorized
OAuth errors keep appearing
OAuth errors keep appearing
- The user needs to complete the OAuth consent flow
- Redirect them to the authorization URL provided in the error
- After consent, tokens are stored automatically—retry the request
Tool not found
Tool not found
- List available tools using
client.list_tools()to see what’s available - Tool names are case-sensitive
- The MCP server may have been updated—refresh the tool list
Can I use Virtual Account with OAuth MCP servers?
Can I use Virtual Account with OAuth MCP servers?
Not directly. Virtual Accounts don’t have per-user OAuth tokens.Options:
- Use TFY PAT or IdP JWT for per-user OAuth
- Configure the MCP server with Static Header auth instead (shared access)
Next Steps
- Learn about Authentication Architecture for the full technical details
- Explore the Agent API for using TrueFoundry’s built-in agent capabilities
- Create a Virtual MCP Server to combine tools from multiple servers
- Set up External Identity if your users don’t have TrueFoundry accounts