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
Copy file name to clipboardExpand all lines: src/content/docs/agents/model-context-protocol/mcp-server/authorization/index.mdx
+109-7Lines changed: 109 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,37 +8,139 @@ sidebar:
8
8
9
9
import { DirectoryListing } from"~/components";
10
10
11
-
When building an MCP (Model Context Protocol) server, you need both a way to allow users to login (authentication) and allow them to grant the MCP client access to resources on their account (authorization).
11
+
When building a [Model Context Protocol (MCP)](https://modelcontextprotocol.io) server, you need both a way to allow users to login (authentication) and allow them to grant the MCP client access to resources on their account (authorization).
12
12
13
13
<diagram>
14
14
15
15
</diagram>
16
16
17
17
The Model Context Protocol uses [a subset of OAuth 2.1 for authorization](https://spec.modelcontextprotocol.io/specification/draft/basic/authorization/). OAuth allows your users to grant limited access to resources, without them having to share API keys or other credentials.
18
18
19
-
Cloudflare provides an oAuth SDK that implements the provider side of the OAuth 2.1 protocol, allowing you to easily add authorization to your MCP server. You can also use your own OAuth provider with the MCP Server SDK, including Stytch, Auth0, and other authorization providers.
19
+
Cloudflare provides an OAuth SDK that implements the provider side of the OAuth 2.1 protocol, allowing you to easily add authorization to your MCP server.
20
20
21
-
### Workers OAuth Provider SDK
21
+
You can use the OAuth SDK in three ways:
22
+
23
+
1.**Your Worker handles authorization itself.** Your MCP server, running on Cloudflare, handles the complete OAuth flow. ([Example](/agents/model-context-protocol/mcp-server/getting-started/))
24
+
2.**Integrate with a third-party OAuth provider**, such as GitHub or Google. ([Example](/agents/model-context-protocol/mcp-server/examples/third-party-oauth-provider/))
25
+
3.**Integrate with your own OAuth provider**, including authorization-as-a-service providers such as Stytch and Auth0. ([Example](/agents/model-context-protocol/mcp-server/examples/external-oauth-provider/))
26
+
27
+
The following sections describe each of these options and link to runnable code examples for each.
28
+
29
+
## Authorization options
30
+
31
+
### (1) Your MCP Server handles authorization itself
32
+
33
+
Your MCP Server, using the Cloudflare MCP Server and OAuth Provider SDKs, can handle the complete OAuth authorization flow, without any third-party involvement.
34
+
35
+
The [Workers OAuth Provider SDK](/agents/model-context-protocol/mcp-server/authorization/oauth-sdk/) is a Cloudflare Worker that implements a [`fetch()` handler](/workers/runtime-apis/handlers/fetch/), and handles incoming requests to your MCP server. You provide your own handlers for your MCP Server's API, and autentication and authorization logic, and URI paths for the OAuth endpoints, and the SDK handles the rest.
36
+
37
+
{/* TODO: Update link */}
38
+
The OAuth Provider SDK comes with an [example handler implementation](https://github.com/geelen/mcp-remote-examples/tree/main/02-user-password/src/routes) for autentication and authorization, referenced below as `defaultHandler`:
39
+
40
+
{/* TODO: GithubCodeComponent */}
41
+
42
+
```ts
43
+
importOAuthProviderfrom"workers-oauth-provider";
44
+
45
+
// TODO: Bunch of naming decisions here
46
+
exportdefaultnewOAuthProvider({
47
+
apiRoute: "/mcp",
48
+
// Your MCP server:
49
+
apiHandler: MyMCPServer.Router,
50
+
// Your handler for authentication and authorization:
51
+
defaultHandler: OAuthProvider.defaultHandler,
52
+
// TODO: Should these have default values?
53
+
authorizeEndpoint: "/authorize",
54
+
tokenEndpoint: "/token",
55
+
clientRegistrationEndpoint: "/register",
56
+
});
57
+
```
58
+
59
+
```mermaid
60
+
sequenceDiagram
61
+
participant B as User-Agent (Browser)
62
+
participant C as MCP Client
63
+
participant M as MCP Server (your Worker)
64
+
65
+
C->>M: MCP Request
66
+
M->>C: HTTP 401 Unauthorized
67
+
Note over C: Generate code_verifier and code_challenge
68
+
C->>B: Open browser with authorization URL + code_challenge
69
+
B->>M: GET /authorize
70
+
Note over M: User logs in and authorizes
71
+
M->>B: Redirect to callback URL with auth code
72
+
B->>C: Callback with authorization code
73
+
C->>M: Token Request with code + code_verifier
74
+
M->>C: Access Token (+ Refresh Token)
75
+
C->>M: MCP Request with Access Token
76
+
Note over C,M: Begin standard MCP message exchange
77
+
```
78
+
79
+
Remember — [authentication is different from authorization](https://www.cloudflare.com/learning/access-management/authn-vs-authz/). Your MCP Server can handle authorization itself, while still relying on an external authentication service to first authenticate users. The [example](/agents/model-context-protocol/mcp-server/) in getting started provides a mock authentdcation flow. You will need to implement your own authentication handler — either handling authentication yourself, or using an external authentication service such as Clerk, Stytch, Auth0 or others.
80
+
81
+
For a step-by-step example, refer to the [Worker as OAuth Provider](/agents/model-context-protocol/mcp-server/authorization/worker-as-oauth-provider/) section., and refer to the [API reference docs for the OAuth Provider SDK](/agents/model-context-protocol/mcp-server/authorization/oauth-sdk/).
82
+
83
+
### (2) Third-party OAuth Provider
84
+
85
+
The OAuth Provider SDK can be configured to use a third-party OAuth provider, such as [GitHub](https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app) or [Google](https://developers.google.com/identity/protocols/oauth2).
86
+
87
+
When you use a third-party OAuth provider, you must provide a handler to the `OAuthProvider` that implements the OAuth flow for the third-party provider.
22
88
23
89
```ts
24
90
importOAuthProviderfrom"workers-oauth-provider";
91
+
importMyAuthHandlerfrom"./auth-handler";
25
92
93
+
// TODO: Bunch of naming decisions here
26
94
exportdefaultnewOAuthProvider({
27
95
apiRoute: "/mcp",
96
+
// Your MCP server:
28
97
apiHandler: MyMCPServer.Router,
29
-
defaultHandler: MyMCPServer.defaultHandler,
98
+
// Your handler for authentication and authorization with the third-party provider:
99
+
defaultHandler: MyAuthHandler,
100
+
// TODO: Should these have default values?
30
101
authorizeEndpoint: "/authorize",
31
102
tokenEndpoint: "/token",
32
103
clientRegistrationEndpoint: "/register",
33
104
});
34
105
```
35
106
107
+
Note that as [defined in the Model Context Protocol specification](https://spec.modelcontextprotocol.io/specification/draft/basic/authorization/#292-flow-description) when you use a third-party OAuth provider, the MCP Server (your Worker) generates and issues its own token to the MCP client:
108
+
109
+
```mermaid
110
+
sequenceDiagram
111
+
participant B as User-Agent (Browser)
112
+
participant C as MCP Client
113
+
participant M as MCP Server (your Worker)
114
+
participant T as Third-Party Auth Server
115
+
116
+
C->>M: Initial OAuth Request
117
+
M->>B: Redirect to Third-Party /authorize
118
+
B->>T: Authorization Request
119
+
Note over T: User authorizes
120
+
T->>B: Redirect to MCP Server callback
121
+
B->>M: Authorization code
122
+
M->>T: Exchange code for token
123
+
T->>M: Third-party access token
124
+
Note over M: Generate bound MCP token
125
+
M->>B: Redirect to MCP Client callback
126
+
B->>C: MCP authorization code
127
+
C->>M: Exchange code for token
128
+
M->>C: MCP access token
129
+
```
130
+
36
131
Read the docs for the [Workers oAuth Provider SDK](/agents/model-context-protocol/mcp-server/authorization/oauth-sdk/) for more details.
37
132
38
-
### External OAuth Providers
133
+
### (3) Bring your own OAuth Provider
39
134
40
-
You can also use an external OAuth provider with the MCP Server SDK.
135
+
If your application already implements an Oauth Provider itself, or you use Stytch, Auth0, or authorization-as-a-service provider, you can use this in the same way that you would use a third-party OAuth provider, described above.
136
+
137
+
The following examples show how to use the OAuth Provider SDK with an external OAuth provider:
0 commit comments