MCP server portals·续4
Enable Gateway routing
You can route every server in a portal through Gateway or turn on routing for individual servers. If Gateway routing is enabled on the portal, it applies to every server in that portal regardless of the server-level setting.
Route every server in a portal
To route traffic from every MCP server in a portal through Gateway:
- In the Cloudflare dashboard ↗︎, go to Zero Trust > Access controls > MCP Portals.
- Find the portal you want to configure, then select the three dots > Edit.
- Under Basic information, turn on Route traffic through Cloudflare Gateway.
- Select Save.
Route an individual server
To route traffic from one MCP server through Gateway without enabling Gateway routing for the entire portal:
- In the Cloudflare dashboard ↗︎, go to Zero Trust > Access controls > AI controls.
- Go to the MCP servers tab.
- Find the server you want to configure, then select the three dots > Edit.
- Under Basic information, turn on Route traffic through Cloudflare Gateway.
- Select Save.
The API field for the server-level setting is secure_web_gateway. It defaults to false.
Portal traffic will now appear in your Gateway HTTP logs. To apply DLP scanning, create a Gateway HTTP policy.
Example Gateway policy
To scan traffic for sensitive data, create a Gateway HTTP policy that matches both the MCP server and a predefined or custom DLP profile.
Gateway HTTP policies for MCP portal traffic must explicitly target the upstream MCP server. Ensure that your policy matches the upstream MCP server hostname (for example, example-mcp-server.example.workers.dev) rather than the portal URL (<subdomain>.<domain>).
For example, the following policy blocks traffic that contains credentials and secrets or financial information:
| Selector | Operator | Value | Logic | Action |
|---|---|---|---|---|
| Host | in | example-mcp-server.example.workers.dev |
And | Block |
| DLP Profile | in | Credentials and Secrets, Financial Information |
What happens when a request is blocked
When a tool call matches a Block DLP policy, Gateway blocks it and the portal surfaces the block to the MCP client as an error rather than completing the tool call. This applies in both directions:
- Tool call requests: If the data the agent sends to a tool matches a DLP profile, Gateway blocks the outbound request and the agent receives an error indicating the request was blocked.
- Tool call responses: If the data the upstream server returns matches a DLP profile, Gateway blocks the response and the portal returns an error instead of the matched content.
The agent can retry the request, but it will continue to be blocked until the content no longer matches the policy.
Limitations
- DLP AI prompt profiles do not apply to MCP server portal traffic. AI prompt profiles are designed for specific web client API paths and do not match the MCP protocol format. Use standard DLP profiles instead.
- SSE transport is not supported through Gateway. If your upstream MCP server only supports SSE, Gateway routing will not work for that server.
- Portal-level Gateway routing does not apply to DCR or background synchronization. To route these operations through Gateway, turn on Gateway routing for the individual server.
Connect to a portal
Users can connect to your MCP server running at https://<subdomain>.<domain>/mcp using Workers AI Playground ↗︎, MCP inspector ↗︎, or other MCP clients that support remote MCP servers.
To test in Workers AI Playground:
- Go to Workers AI Playground ↗︎.
- Under MCP Servers, enter
https://<subdomain>.<domain>/mcpfor the portal URL. - Select Connect.
- In the popup window, log in to your Cloudflare Access identity provider.
- The popup window will list the MCP servers in the portal that require authentication. For each of these MCP servers, select Connect and follow the login prompts.
- Select Done to complete the portal authentication process.
Workers AI Playground will show a Connected status and list the available tools. You can now ask the AI model to complete a task using an available tool. Requests made to an MCP server will appear in your portal logs.
For MCP clients with server configuration files, add the portal as a remote HTTP server. The exact URL field depends on your client. For example:
MCP client configuration for MCP portalsjson
{
"mcpServers": {
"example-mcp-server": {
"url": "https://<subdomain>.<domain>/mcp"
}
}
}
Portal homepage
When users visit the portal domain (https://<subdomain>.<domain>/) in a browser, the portal displays a homepage with connection details and setup instructions.
Note
Do not visit the MCP endpoint URL (https://<subdomain>.<domain>/mcp) directly in a browser. The /mcp path is intended for MCP clients only and will return an invalid token error if accessed in a browser.
The homepage shows:
- The portal name and your organization branding (if configured in Cloudflare Access)
- The MCP endpoint URL with a copy button
- Per-client connection instructions for Claude Desktop, Workers AI Playground, OpenCode, Windsurf, and other MCP clients with OS-specific file paths
Authenticated users see their email address and a Sign out button in the session bar. Users who are not authenticated can still view the homepage and connection instructions.
Sign out of a portal
To end a portal session, select Sign out from the portal homepage (https://<subdomain>.<domain>/). The sign-out flow:
- Revokes all portal-level OAuth grants for your user.
- Deletes all upstream MCP server OAuth states associated with your session.
- Redirects through Cloudflare Access logout.
After sign-out, the portal displays a confirmation page with a summary of the revoked sessions. To reconnect, visit the portal homepage and authenticate again.
Connect with a service token
You can connect to an MCP portal using an Access service token for machine-to-machine access. Service tokens bypass the browser-based OAuth flow and authenticate using the CF-Access-Client-Id and CF-Access-Client-Secret headers.
A service token session is authorized twice: once at the portal URL, and once for each upstream MCP server it tries to reach through the portal. Both checks need a matching Service Auth policy.
Required configuration
| Where | Policy action | Include rule | Purpose |
|---|---|---|---|
| Portal Access application | Service Auth | Your service token | Lets the bot connect to the portal URL. |
| Each linked MCP server Access app | Service Auth | Your service token | Lets the bot see and call that server's tools through the portal. |
| Server's portal mapping | n/a | n/a | Require user auth must be off so the portal uses the admin credential. |
Note
Require user auth in the dashboard maps to the on_behalf field on the portal-server mapping in the API and Terraform. For each linked server you want a service token to reach, set on_behalf to false. Servers with on_behalf: true are excluded from service token sessions because they require a per-user OAuth grant that a service token cannot provide.
If a linked MCP server does not have a Service Auth policy matching the token, that server is hidden from the bot's tool list.
Set up a service token connection
- Create a service token in your Zero Trust account.
- Open the portal's Access application and add a Service Auth policy that includes the service token.
- For each upstream MCP server you want the bot to reach:
- Open the server's Access application and add a Service Auth policy that includes the same service token.
- Open the portal and edit the server. Turn Require user auth off so the portal uses the admin credential for that server.
- Connect from your MCP client with the service token headers.
To configure this flow with the API, set servers[].on_behalf to false in the MCP portal request. Create a non_identity Access policy with a service_token include rule. Attach the policy to the portal and each linked server Access application. For a complete Terraform example, refer to Configure via Terraform.
To verify the connection, send an MCP initialize request with the service token headers:
curl https://<subdomain>.<domain>/mcp \
-H "CF-Access-Client-Id: <CLIENT_ID>" \
-H "CF-Access-Client-Secret: <CLIENT_SECRET>" \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
--data '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"service-token-test","version":"1.0.0"}}}'
For mcp-remote, pass the credentials through environment variables instead of storing them in the MCP client configuration:
MCP client configuration for service token connectionsjson
{
"mcpServers": {
"example-portal": {
"command": "npx",
"args": [
"-y",
"mcp-remote@latest",
"https://<subdomain>.<domain>/mcp",
"--header",
"CF-Access-Client-Id:${ACCESS_CLIENT_ID}",
"--header",
"CF-Access-Client-Secret:${ACCESS_CLIENT_SECRET}"
],
"env": {
"ACCESS_CLIENT_ID": "<CLIENT_ID>",
"ACCESS_CLIENT_SECRET": "<CLIENT_SECRET>"
}
}
}
}
Note
Service tokens do not support per-user OAuth with upstream MCP servers. The portal uses the admin credential for every upstream request made by a service token session. Servers with Require user auth turned on are excluded from service token sessions because they require a per-user OAuth grant that a service token cannot provide.
Device authentication
MCP server portals require a browser-based authentication flow. Device authentication (picking up identity from the Cloudflare One Client without a browser redirect) is not currently supported for MCP portals. Users must complete the Access login flow in a browser when first connecting.
Optimize context
MCP server portals support context optimization options that reduce how many tokens tool definitions consume in the model's context window. These options are useful when a portal aggregates many MCP servers or servers that expose a large number of tools.
To use context optimization, append the optimize_context query parameter to your portal URL when connecting from an MCP client.
Minimize tools
The minimize_tools option strips tool descriptions and input schemas from all upstream tools, leaving only their names. The portal exposes a special query tool that agents use to search and retrieve full tool definitions on demand. Agents can discover tools without loading all definitions upfront.
This option provides up to 5x savings in token usage, though querying tool definitions before use adds a small amount of overhead.
To connect with minimize_tools, use the following portal URL:
https://<subdomain>.<domain>/mcp?optimize_context=minimize_tools
For MCP clients with server configuration files:
MCP client configuration with minimize_toolsjson
{
"mcpServers": {
"example-portal": {
"url": "https://<subdomain>.<domain>/mcp?optimize_context=minimize_tools"
}
}
}
Search and execute
The search_and_execute option hides all upstream tools and exposes only two tools to the agent: query and execute. The query tool searches and retrieves tool definitions. The execute tool runs the upstream tools. The generated code runs in an isolated Dynamic Worker environment, which keeps authentication credentials and environment variables out of the model context.
This option reduces the initial token cost of portal tools to a small constant, regardless of how many tools are available. However, the agent becomes fully reliant on query to discover tools before it can call them.
To connect with search_and_execute, use the following portal URL:
https://<subdomain>.<domain>/mcp?optimize_context=search_and_execute
For MCP clients with server configuration files:
MCP client configuration with search_and_executejson
{
"mcpServers": {
"example-portal": {
"url": "https://<subdomain>.<domain>/mcp?optimize_context=search_and_execute"
}
}
}
For more information on the Code Mode pattern behind search_and_execute, refer to Code Mode.
Manage portal sessions
Once connected to a portal, users can manage their upstream MCP server sessions without leaving their MCP client. The portal uses MCP elicitations ↗︎ to provide a server selection page where you can enable or disable servers, log out of individual servers, and reauthenticate.
Return to the server selection page
To manage your server connections during an active session, ask your AI agent to take you back to the server selection page. For example, prompt your agent with:
Take me back to the server selection page.
The portal returns an authorization URL. Open this URL in your web browser to access the server selection page:
https://<subdomain>.<domain>/authorize?elicitationId=<ELICITATION_ID>
From this page you can:
- Enable or disable servers — Toggle individual upstream MCP servers on or off. Disabling a server removes its tools from the active session, which reduces context window usage.
- Log out and reauthenticate — Log out of a server and log back in if you need to change which data the server has access to. For example, you may need to reauthenticate with different permissions.
