You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can secure <GlossaryTooltipterm="MCP server">Model Context Protocol (MCP) servers</GlossaryTooltip> by using Cloudflare Access as the Single Sign-On (SSO) provider. When users connect to the remote MCP server using an <GlossaryTooltipterm="MCP client">MCP client</GlossaryTooltip>, they will be prompted to log in to your [identity provider](/cloudflare-one/identity/idp-integration/) and are only granted access if they pass your [Access policies](/cloudflare-one/policies/access/#selectors).
11
+
You can secure <GlossaryTooltipterm="MCP server">Model Context Protocol (MCP) servers</GlossaryTooltip> by using Cloudflare Access as an OAuth Single Sign-On (SSO) provider.
12
+
13
+
This guide walks through how to deploy a remote MCP server on [Cloudflare Workers](/workers/) that requires Cloudflare Access for authentication. When users connect to the MCP server using an <GlossaryTooltipterm="MCP client">MCP client</GlossaryTooltip>, they will be prompted to log in to your [identity provider](/cloudflare-one/identity/idp-integration/) and are only granted access if they pass your [Access policies](/cloudflare-one/policies/access/#selectors).
12
14
13
15
## Prerequisites
14
16
15
-
- An [identity provider](/cloudflare-one/identity/idp-integration/) configured in Cloudflare Zero Trust
17
+
- Add an [identity provider](/cloudflare-one/identity/idp-integration/) to Cloudflare Zero Trust
When asked if you want to deploy to Cloudflare, select **No**.
32
+
33
+
2. Go to the project directory:
34
+
35
+
```sh
36
+
cd mcp-server-cf-access
37
+
```
38
+
39
+
3. Create a [Workers KV namespace](/kv/concepts/kv-namespaces/) to store the key. The binding name should be `OAUTH_KV` if you want to run the example as written.
40
+
41
+
```sh
42
+
npx wrangler kv namespace create "OAUTH_KV"
43
+
```
44
+
45
+
The command will output the binding name and KV namespace ID:
46
+
47
+
```sh output
48
+
{
49
+
"kv_namespaces": [
50
+
{
51
+
"binding": "OAUTH_KV",
52
+
"id": "<YOUR_KV_NAMESPACE_ID>"
53
+
}
54
+
]
55
+
}
56
+
```
57
+
58
+
4. Open `wrangler.jsonc` in an editor and insert your `OAUTH_KV` namespace ID:
59
+
60
+
```jsonc
61
+
"kv_namespaces": [
62
+
{
63
+
"binding":"OAUTH_KV",
64
+
"id":"<YOUR_KV_NAMESPACE_ID>"
65
+
}
66
+
],
67
+
```
68
+
69
+
70
+
5. You can now deploy the Worker to Cloudflare's global network:
71
+
72
+
```sh
73
+
npx wrangler deploy
74
+
```
16
75
17
-
## 1. Create an Access for SaaS app
76
+
The Worker will be deployed to your `*.workers.dev` subdomain at `mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev`.
77
+
78
+
## 2. Create an Access for SaaS app
18
79
19
80
1. In [Zero Trust](https://one.dash.cloudflare.com), go to **Access** > **Applications**.
20
81
2. Select **SaaS**.
21
82
3. In **Application**, enter a custom name (for example, `MCP server`) and select the textbox that appears below.
22
83
4. Select **OIDC** as the authentication protocol.
23
84
5. Select **Add application**.
24
-
6. In **Redirect URLs**, enter the authorization callback URL for your MCP server (for example, `https://<SERVER-NAME>.<SUBDOMAIN>.workers.dev/callback`). Refer to your MCP server documentation for instructions on obtaining this value.
25
-
7. Copy the following values to input into your MCP server's OAuth configuration. Different MCP servers may require different sets of input values.
26
-
-**Client secret**
27
-
-**Client ID**
28
-
-**Configuration endpoint**
29
-
-**Issuer**
30
-
-**Token endpoint**
31
-
-**Authorization endpoint**
32
-
-**Key endpoint**
33
-
-**Userinfo endpoint**
34
-
8. (Optional) Under **Advanced settings**, turn on [**Refresh tokens**](/cloudflare-one/applications/configure-apps/saas-apps/generic-oidc-saas/#advanced-settings) to reduce the number of times a user needs to log in to the identity provider.
35
-
9. Configure [Access policies](/cloudflare-one/policies/access/) to define the users can access the MCP server.
85
+
6. In **Redirect URLs**, enter the authorization callback URL for your MCP server. The callback URL for our [example MCP server](#1-deploy-an-example-mcp-server) is
7. Copy the following values to input into our example MCP server. Other MCP servers may require different sets of input values.
90
+
-**Client secret**
91
+
-**Client ID**
92
+
-**Token endpoint**
93
+
-**Authorization endpoint**
94
+
-**Key endpoint**
95
+
96
+
8. (Optional) Under **Advanced settings**, turn on [**Refresh tokens**](/cloudflare-one/applications/configure-apps/saas-apps/generic-oidc-saas/#advanced-settings) if you want to reduce the number of times a user needs to log in to the identity provider.
97
+
9. Configure [Access policies](/cloudflare-one/policies/access/) to define the users who can access the MCP server.
36
98
10. Save the application.
37
99
38
-
This SaaS application now acts as the SSO provider for your MCP server.
100
+
## 3. Configure your MCP server
101
+
102
+
Your MCP server needs to perform an OAuth 2.0 authorization flow to get an `access_token` from the SaaS app created in [Step 1](#1-create-an-access-for-saas-app). 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.
103
+
104
+
To add OAuth endpoints and credentials to our [example MCP server](#1-deploy-an-example-mcp-server):
105
+
106
+
1. Create the following [Workers secrets](/workers/configuration/secrets/):
107
+
108
+
```sh
109
+
wrangler secret put ACCESS_CLIENT_ID
110
+
wrangler secret put ACCESS_CLIENT_SECRET
111
+
wrangler secret put ACCESS_TOKEN_URL
112
+
wrangler secret put ACCESS_AUTHORIZATION_URL
113
+
wrangler secret put ACCESS_JWKS_URL
114
+
```
115
+
116
+
2. When prompted to enter a secret value, paste the corresponding values from your SaaS app:
You should now be able to connect to your MCP server running at `https://mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev/sse` using [Workers AI Playground](https://playground.ai.cloudflare.com/), [MCP inspector](https://github.com/modelcontextprotocol/inspector), or [other MCP clients](/agents/guides/remote-mcp-server/#connect-your-mcp-server-to-claude-and-other-mcp-clients) that support remote MCP servers.
143
+
144
+
To test in Workers AI Playground:
145
+
146
+
1. Go to [Workers AI Playground](https://playground.ai.cloudflare.com/).
39
147
40
-
## 2. Configure your MCP server
148
+
2.Under **MCP Servers**, enter `https://mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev/sse` for the MCP server URL.
41
149
42
-
Configure the OAuth client on your MCP server to use the OAuth endpoints and credentials from the SaaS app created in [Step 1](#1-create-an-access-for-saas-app). The MCP server should successfully obtain an `access_token` after the user authenticates to Access.
150
+
3. Select **Connect**.
43
151
44
-
For a practical example of how to configure the OAuth client, refer to [Deploy an example MCP server](#deploy-an-example-mcp-server).
152
+
4. A popup window will appear requesting access to the MCP server. Select **Approve**.
45
153
46
-
## Deploy an example MCP server
154
+
5. Follow the prompts to log in to your identity provider.
47
155
156
+
Workers AI Playground will show a **Connected** status. The MCP server should successfully obtain an `access_token` from Cloudflare Access.
0 commit comments