You can secureModel Context Protocol (MCP) servers ↗ by using Cloudflare Access as an OAuth Single Sign-On (SSO) provider.
This guide walks through how to deploy a remoteMCP server onCloudflare Workers that requires Cloudflare Access for authentication. When users connect to the MCP server using anMCP client, they will be prompted to log in to youridentity provider and are only granted access if they pass yourAccess policies.
- Add anidentity provider to Cloudflare Zero Trust
- Installnpm ↗
- InstallNode.js ↗
To deploy ourexample MCP server ↗ on Workers:
Open a terminal and clone our example project:
Terminal window npmcreatecloudflare@latest--mcp-server-cf-access--template=cloudflare/ai/demos/remote-mcp-cf-accessWhen asked if you want to deploy to Cloudflare, selectNo.
Go to the project directory:
Terminal window cdmcp-server-cf-accessCreate aWorkers KV namespace to store the key. The binding name should be
OAUTH_KVif you want to run the example as written.Terminal window npxwranglerkvnamespacecreate"OAUTH_KV"The command will output the binding name and KV namespace ID:
{"kv_namespaces": [{"binding":"OAUTH_KV","id":"<YOUR_KV_NAMESPACE_ID>"}]}Open
wrangler.jsoncin an editor and insert yourOAUTH_KVnamespace ID:"kv_namespaces":[{"binding":"OAUTH_KV","id":"<YOUR_KV_NAMESPACE_ID>"}],You can now deploy the Worker to Cloudflare's global network:
Terminal window npxwranglerdeploy
The Worker will be deployed to your*.workers.dev subdomain atmcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev.
InCloudflare One ↗, go toAccess controls >Applications.
SelectAdd an application.
SelectSaaS.
InApplication, enter a custom name (for example,
MCP server) and select the textbox that appears below.SelectOIDC as the authentication protocol.
SelectAdd application.
InRedirect URLs, enter the authorization callback URL for your MCP server. The callback URL for ourexample MCP server is
txt https://mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev/callbackCopy the following values to input into our example MCP server. Other MCP servers may require different sets of input values.
- Client secret
- Client ID
- Token endpoint
- Authorization endpoint
- Key endpoint
(Optional) UnderAdvanced settings, turn onRefresh tokens if you want to reduce the number of times a user needs to log in to the identity provider.
ConfigureAccess policies to define the users who can access the MCP server.
Save the application.
Make a
POSTrequest to theAccess applications endpoint:
At least one of the followingtoken permissions is required:Required API token permissions
Access: Apps and Policies Write
Add an Access application curl"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/access/apps"\--requestPOST\--header"Authorization: Bearer$CLOUDFLARE_API_TOKEN"\--json'{"name": "MCP server","type": "saas","saas_app": {"auth_type": "oidc","redirect_uris": ["https://mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev/callback"],"grant_type": ["authorization_code","refresh_tokens"],"refresh_token_options": {"lifetime": "90d"}},"policies": ["f174e90a-fafe-4643-bbbc-4a0ed4fc8415"],"allowed_idps": []}'Copy the
client_idandclient_secretreturned in the response.To determine the OAuth endpoint URLs for the SaaS application, refer to thegeneric OIDC documentation.
Your MCP server needs to perform an OAuth 2.0 authorization flow to get anaccess_token from the SaaS app created inStep 1. When setting up the OAuth client on your MCP server, you will need to paste in the OAuth endpoints and credentials from the SaaS app.
To add OAuth endpoints and credentials to ourexample MCP server:
Create the followingWorkers secrets:
Terminal window wranglersecretputACCESS_CLIENT_IDwranglersecretputACCESS_CLIENT_SECRETwranglersecretputACCESS_TOKEN_URLwranglersecretputACCESS_AUTHORIZATION_URLwranglersecretputACCESS_JWKS_URLWhen prompted to enter a secret value, paste the corresponding values from your SaaS app:
Workers secret SaaS app field ACCESS_CLIENT_IDClient ID ACCESS_CLIENT_SECRETClient secret ACCESS_TOKEN_URLToken endpoint ACCESS_AUTHORIZATION_URLAuthorization endpoint ACCESS_JWKS_URLKey endpoint Configure a cookie encryption key:
a. Generate a random string:
Terminal window opensslrand-hex32b. Store the string in a Workers secret:
Terminal window wranglersecretputCOOKIE_ENCRYPTION_KEY
You should now be able to connect to your MCP server usingWorkers AI Playground ↗,MCP inspector ↗, orother MCP clients that support remote MCP servers. The demo MCP serversupports connections ↗ via eitherhttps://mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev/mcp orhttps://mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev/sse.
To test in Workers AI Playground:
Go toWorkers AI Playground ↗.
UnderMCP Servers, enter
https://mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev/mcpfor the MCP server URL.SelectConnect.
A popup window will appear requesting access to the MCP server. SelectApprove.
Follow the prompts to log in to your identity provider.
Workers AI Playground will show aConnected status. The MCP server should successfully obtain anaccess_token from Cloudflare Access.
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark