|
| 1 | +--- |
| 2 | +pcx_content_type: how-to |
| 3 | +title: Secure MCP servers with Access for SaaS |
| 4 | +sidebar: |
| 5 | + order: 1 |
| 6 | + label: Secure MCP servers with Access for SaaS |
| 7 | +--- |
| 8 | + |
| 9 | +import { Render, GlossaryTooltip, Tabs, TabItem, APIRequest } from "~/components" |
| 10 | + |
| 11 | +You can secure <GlossaryTooltip term="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 <GlossaryTooltip term="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). |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- Add an [identity provider](/cloudflare-one/identity/idp-integration/) to Cloudflare Zero Trust |
| 18 | +- Install [npm](https://docs.npmjs.com/getting-started) |
| 19 | +- Install [Node.js](https://nodejs.org/en/) |
| 20 | + |
| 21 | +## 1. Deploy an example MCP server |
| 22 | + |
| 23 | +To deploy our [example MCP server](https://github.com/cloudflare/ai/tree/main/demos/remote-mcp-cf-access) on Workers: |
| 24 | + |
| 25 | +1. Open a terminal and clone our example project: |
| 26 | + |
| 27 | + ```sh |
| 28 | + npm create cloudflare@latest -- mcp-server-cf-access --template=cloudflare/ai/demos/remote-mcp-cf-access |
| 29 | + ``` |
| 30 | + |
| 31 | + 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 | + ``` |
| 75 | + |
| 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 |
| 79 | + |
| 80 | +<Tabs syncKey="dashPlusAPI"> |
| 81 | +<TabItem label="Dashboard"> |
| 82 | + |
| 83 | +1. In [Zero Trust](https://one.dash.cloudflare.com), go to **Access** > **Applications**. |
| 84 | +2. Select **SaaS**. |
| 85 | +3. In **Application**, enter a custom name (for example, `MCP server`) and select the textbox that appears below. |
| 86 | +4. Select **OIDC** as the authentication protocol. |
| 87 | +5. Select **Add application**. |
| 88 | +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 |
| 89 | + ```txt |
| 90 | + https://mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev/callback |
| 91 | + ``` |
| 92 | +7. Copy the following values to input into our example MCP server. Other MCP servers may require different sets of input values. |
| 93 | + - **Client secret** |
| 94 | + - **Client ID** |
| 95 | + - **Token endpoint** |
| 96 | + - **Authorization endpoint** |
| 97 | + - **Key endpoint** |
| 98 | + |
| 99 | +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. |
| 100 | +9. Configure [Access policies](/cloudflare-one/policies/access/) to define the users who can access the MCP server. |
| 101 | +10. Save the application. |
| 102 | + |
| 103 | +</TabItem> |
| 104 | +<TabItem label="API"> |
| 105 | + |
| 106 | +1. Make a `POST` request to the [Access applications](/api/resources/zero_trust/subresources/access/subresources/applications/methods/create/) endpoint: |
| 107 | + |
| 108 | + <APIRequest |
| 109 | + path="/accounts/{account_id}/access/apps" |
| 110 | + method="POST" |
| 111 | + json={{ |
| 112 | + name: "MCP server", |
| 113 | + type: "saas", |
| 114 | + saas_app: { |
| 115 | + auth_type: "oidc", |
| 116 | + redirect_uris: [ |
| 117 | + "https://mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev/callback" |
| 118 | + ], |
| 119 | + grant_type: [ |
| 120 | + "authorization_code", |
| 121 | + "refresh_tokens" |
| 122 | + ], |
| 123 | + refresh_token_options: { |
| 124 | + lifetime: "90d" |
| 125 | + } |
| 126 | + }, |
| 127 | + policies: [ |
| 128 | + "f174e90a-fafe-4643-bbbc-4a0ed4fc8415" |
| 129 | + ], |
| 130 | + allowed_idps: [] |
| 131 | + }} |
| 132 | + /> |
| 133 | + |
| 134 | +2. Copy the `client_id` and `client_secret` returned in the response. |
| 135 | +3. To determine the OAuth endpoint URLs for the SaaS application, refer to the [generic OIDC documentation](/cloudflare-one/applications/configure-apps/saas-apps/generic-oidc-saas/#2-add-your-application-to-access). |
| 136 | + |
| 137 | +</TabItem> |
| 138 | +</Tabs> |
| 139 | + |
| 140 | +## 3. Configure your MCP server |
| 141 | + |
| 142 | +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. |
| 143 | + |
| 144 | +To add OAuth endpoints and credentials to our [example MCP server](#1-deploy-an-example-mcp-server): |
| 145 | + |
| 146 | +1. Create the following [Workers secrets](/workers/configuration/secrets/): |
| 147 | + |
| 148 | + ```sh |
| 149 | + wrangler secret put ACCESS_CLIENT_ID |
| 150 | + wrangler secret put ACCESS_CLIENT_SECRET |
| 151 | + wrangler secret put ACCESS_TOKEN_URL |
| 152 | + wrangler secret put ACCESS_AUTHORIZATION_URL |
| 153 | + wrangler secret put ACCESS_JWKS_URL |
| 154 | + ``` |
| 155 | + |
| 156 | +2. When prompted to enter a secret value, paste the corresponding values from your SaaS app: |
| 157 | + |
| 158 | + | Workers secret | SaaS app field | |
| 159 | + | ------------- | -------------- | |
| 160 | + | `ACCESS_CLIENT_ID`| Client ID | |
| 161 | + | `ACCESS_CLIENT_SECRET` | Client secret | |
| 162 | + | `ACCESS_TOKEN_URL` | Token endpoint | |
| 163 | + | `ACCESS_AUTHORIZATION_URL` | Authorization endpoint | |
| 164 | + | `ACCESS_JWKS_URL` | Key endpoint | |
| 165 | + |
| 166 | +3. Configure a cookie encryption key: |
| 167 | + |
| 168 | + a. Generate a random string: |
| 169 | + |
| 170 | + ```sh |
| 171 | + openssl rand -hex 32 |
| 172 | + ``` |
| 173 | + |
| 174 | + b. Store the string in a Workers secret: |
| 175 | + |
| 176 | + ```sh |
| 177 | + wrangler secret put COOKIE_ENCRYPTION_KEY |
| 178 | + ``` |
| 179 | + |
| 180 | +## 4. Test the connection |
| 181 | + |
| 182 | +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. |
| 183 | + |
| 184 | +To test in Workers AI Playground: |
| 185 | + |
| 186 | +1. Go to [Workers AI Playground](https://playground.ai.cloudflare.com/). |
| 187 | + |
| 188 | +2. Under **MCP Servers**, enter `https://mcp-server-cf-access.<YOUR_SUBDOMAIN>.workers.dev/sse` for the MCP server URL. |
| 189 | + |
| 190 | +3. Select **Connect**. |
| 191 | + |
| 192 | +4. A popup window will appear requesting access to the MCP server. Select **Approve**. |
| 193 | + |
| 194 | +5. Follow the prompts to log in to your identity provider. |
| 195 | + |
| 196 | +Workers AI Playground will show a **Connected** status. The MCP server should successfully obtain an `access_token` from Cloudflare Access. |
0 commit comments