-
Notifications
You must be signed in to change notification settings - Fork 10.3k
WIP: MCP #20860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
WIP: MCP #20860
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8980e2e
Scaffolding
irvinebroque 78b19b1
Scaffolding
irvinebroque d4fa6b6
Start to add and structure content
irvinebroque 32a4fc5
Reference MCP tools in RPC docs
irvinebroque 1ff6146
Basic tools docs
irvinebroque 87f5d4c
Add note to tools docs
irvinebroque 3956dc9
More details
irvinebroque 5d39540
Scaffolding out authorization docs
irvinebroque 1b68329
More updates
irvinebroque cb8010d
Scaffold out a few more things
irvinebroque 6579a2f
Move content to overview, link to workers-mcp instead
irvinebroque e8a1ab7
Start adding next steps after initial getting started
irvinebroque d5a94c1
Remove JSRPC tools, replace with standard sdk
irvinebroque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| title: Model Context Protocol (MCP) | ||
| pcx_content_type: navigation | ||
| sidebar: | ||
| order: 4 | ||
| group: | ||
| hideIndex: true | ||
| --- | ||
|
|
||
| import { DirectoryListing } from "~/components"; | ||
|
|
||
| <DirectoryListing /> |
14 changes: 14 additions & 0 deletions
14
src/content/docs/agents/model-context-protocol/mcp-client.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| pcx_content_type: concept | ||
| title: Agent as MCP Client | ||
| sidebar: | ||
| order: 4 | ||
| --- | ||
|
|
||
| import { Render } from "~/components"; | ||
|
|
||
| WIP | ||
|
|
||
| - Explain use cases for Agent as an MCP Client, and the connection to this and using MCP Servers to provide tools | ||
| - Show example of using the Agents SDK to call an MCP Server | ||
| - Explain how this gets translated into the format of tools that people are used to from ai-sdk and from agents-sdk |
8 changes: 8 additions & 0 deletions
8
src/content/docs/agents/model-context-protocol/mcp-server.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| pcx_content_type: concept | ||
| title: MCP Servers | ||
| sidebar: | ||
| order: 1 | ||
| --- | ||
|
|
||
| import { Render } from "~/components"; |
17 changes: 17 additions & 0 deletions
17
src/content/docs/agents/model-context-protocol/mcp-server/authentication/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --- | ||
| title: Authentication | ||
| sidebar: | ||
| order: 4 | ||
| group: | ||
| hideIndex: false | ||
| --- | ||
|
|
||
| import { DirectoryListing } from "~/components"; | ||
|
|
||
| WIP | ||
|
|
||
| Explain / reiterate difference between authN and authz | ||
|
|
||
| This section should have some examples of using other services / libraries for authentication. | ||
|
|
||
| It should show you how to rip out the mock auth from the example project, and replace it with your own. |
8 changes: 8 additions & 0 deletions
8
...nts/model-context-protocol/mcp-server/authorization/external-oauth-provider.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| pcx_content_type: concept | ||
| title: Use your own OAuth Provider | ||
| sidebar: | ||
| order: 4 | ||
| --- | ||
|
|
||
| import { Render } from "~/components"; |
146 changes: 146 additions & 0 deletions
146
src/content/docs/agents/model-context-protocol/mcp-server/authorization/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,146 @@ | ||||||
| --- | ||||||
| title: Authorization | ||||||
| sidebar: | ||||||
| order: 3 | ||||||
| group: | ||||||
| hideIndex: false | ||||||
| --- | ||||||
|
|
||||||
| import { DirectoryListing } from "~/components"; | ||||||
|
|
||||||
| 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). | ||||||
|
|
||||||
| <diagram> | ||||||
|
|
||||||
| </diagram> | ||||||
|
|
||||||
| 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. | ||||||
|
|
||||||
| Cloudflare provides an [OAuth Provider Library](https://github.com/geelen/mcp-remote-examples/tree/main/.vendor/workers-oauth-provider) that implements the provider side of the OAuth 2.1 protocol, allowing you to easily add authorization to your MCP server. | ||||||
|
|
||||||
| You can use the OAuth Provider Library in three ways: | ||||||
|
|
||||||
| 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/)) | ||||||
| 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/)) | ||||||
| 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/)) | ||||||
|
|
||||||
| The following sections describe each of these options and link to runnable code examples for each. | ||||||
|
|
||||||
| ## Authorization options | ||||||
|
|
||||||
| ### (1) Your MCP Server handles authorization itself | ||||||
|
|
||||||
| Your MCP Server, using the Cloudflare [MCP Server SDK](/agents/model-context-protocol/mcp-server/getting-started/) and [OAuth Provider Library](/agents/model-context-protocol/mcp-server/authorization/oauth-provider-api-reference/), can handle the complete OAuth authorization flow, without any third-party involvement. | ||||||
|
|
||||||
| The [Workers OAuth Provider Library](/agents/model-context-protocol/mcp-server/authorization/oauth-provider-api-reference/) 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. | ||||||
|
|
||||||
| {/* TODO: Update link */} | ||||||
| The OAuth Provider Library 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`: | ||||||
|
|
||||||
| {/* TODO: GithubCodeComponent */} | ||||||
|
|
||||||
| ```ts | ||||||
| import OAuthProvider from "workers-oauth-provider"; | ||||||
|
|
||||||
| // TODO: Bunch of naming decisions here | ||||||
| export default new OAuthProvider({ | ||||||
| apiRoute: "/mcp", | ||||||
| // Your MCP server: | ||||||
| apiHandler: MyMCPServer.Router, | ||||||
| // Your handler for authentication and authorization: | ||||||
| defaultHandler: OAuthProvider.defaultHandler, | ||||||
| // TODO: Should these have default values? | ||||||
| authorizeEndpoint: "/authorize", | ||||||
| tokenEndpoint: "/token", | ||||||
| clientRegistrationEndpoint: "/register", | ||||||
| }); | ||||||
| ``` | ||||||
|
|
||||||
| ```mermaid | ||||||
| sequenceDiagram | ||||||
| participant B as User-Agent (Browser) | ||||||
| participant C as MCP Client | ||||||
| participant M as MCP Server (your Worker) | ||||||
|
|
||||||
| C->>M: MCP Request | ||||||
| M->>C: HTTP 401 Unauthorized | ||||||
| Note over C: Generate code_verifier and code_challenge | ||||||
| C->>B: Open browser with authorization URL + code_challenge | ||||||
| B->>M: GET /authorize | ||||||
| Note over M: User logs in and authorizes | ||||||
| M->>B: Redirect to callback URL with auth code | ||||||
| B->>C: Callback with authorization code | ||||||
| C->>M: Token Request with code + code_verifier | ||||||
| M->>C: Access Token (+ Refresh Token) | ||||||
| C->>M: MCP Request with Access Token | ||||||
| Note over C,M: Begin standard MCP message exchange | ||||||
| ``` | ||||||
|
|
||||||
| 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. | ||||||
|
|
||||||
| 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/). | ||||||
|
|
||||||
| ### (2) Third-party OAuth Provider | ||||||
|
|
||||||
| The OAuth Provider Library 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). | ||||||
|
|
||||||
| 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. | ||||||
|
|
||||||
| ```ts | ||||||
| import OAuthProvider from "workers-oauth-provider"; | ||||||
| import MyAuthHandler from "./auth-handler"; | ||||||
|
|
||||||
| // TODO: Bunch of naming decisions here | ||||||
| export default new OAuthProvider({ | ||||||
| apiRoute: "/mcp", | ||||||
| // Your MCP server: | ||||||
| apiHandler: MyMCPServer.Router, | ||||||
| // Your handler for authentication and authorization with the third-party provider: | ||||||
| defaultHandler: MyAuthHandler, | ||||||
| // TODO: Should these have default values? | ||||||
| authorizeEndpoint: "/authorize", | ||||||
| tokenEndpoint: "/token", | ||||||
| clientRegistrationEndpoint: "/register", | ||||||
| }); | ||||||
| ``` | ||||||
|
|
||||||
| 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: | ||||||
|
|
||||||
| ```mermaid | ||||||
| sequenceDiagram | ||||||
| participant B as User-Agent (Browser) | ||||||
| participant C as MCP Client | ||||||
| participant M as MCP Server (your Worker) | ||||||
| participant T as Third-Party Auth Server | ||||||
|
|
||||||
| C->>M: Initial OAuth Request | ||||||
| M->>B: Redirect to Third-Party /authorize | ||||||
| B->>T: Authorization Request | ||||||
| Note over T: User authorizes | ||||||
| T->>B: Redirect to MCP Server callback | ||||||
| B->>M: Authorization code | ||||||
| M->>T: Exchange code for token | ||||||
| T->>M: Third-party access token | ||||||
| Note over M: Generate bound MCP token | ||||||
| M->>B: Redirect to MCP Client callback | ||||||
| B->>C: MCP authorization code | ||||||
| C->>M: Exchange code for token | ||||||
| M->>C: MCP access token | ||||||
| ``` | ||||||
|
|
||||||
| Read the docs for the [Workers oAuth Provider Library](/agents/model-context-protocol/mcp-server/authorization/oauth-provider-api-reference/) for more details. | ||||||
|
|
||||||
| ### (3) Bring your own OAuth Provider | ||||||
|
|
||||||
| 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 in (2). | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identified issues
Proposed fix
Suggested change
I corrected the capitalization of 'Oauth' to 'OAuth' as per the style guide recommendation. This change does not affect any code references or URLs. |
||||||
|
|
||||||
| The following examples show how to use the OAuth Provider Library with an external OAuth provider: | ||||||
|
|
||||||
| - [Stytch](/agents/model-context-protocol/mcp-server/examples/stytch/) | ||||||
| - [Auth0](/agents/model-context-protocol/mcp-server/examples/auth0/) | ||||||
|
|
||||||
| ## Next steps | ||||||
|
|
||||||
| - [Learn how to use the OAuth Provider SDK](/agents/model-context-protocol/mcp-server/authorization/oauth-provider-api-reference/) | ||||||
| - [Learn how to use a third-party OAuth provider](/agents/model-context-protocol/mcp-server/examples/third-party-oauth-provider/) | ||||||
| - [Learn how to bring your own OAuth provider](/agents/model-context-protocol/mcp-server/examples/external-oauth-provider/) | ||||||
11 changes: 11 additions & 0 deletions
11
...odel-context-protocol/mcp-server/authorization/oauth-provider-api-reference.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| pcx_content_type: navigation | ||
| title: OAuth Provider Library | ||
| # TODO: Update link to published library | ||
| external_link: https://github.com/geelen/mcp-remote-examples/tree/main/.vendor/workers-oauth-provider | ||
| sidebar: | ||
| order: 5 | ||
| head: [] | ||
| --- | ||
|
|
||
| {/* TODO: this page (or the linked docs) need to be really clear about what the defaultHandler must implement, and provide a really clear reference example */} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identified issues
Proposed fix
The term 'oAuth' should be capitalized as 'OAuth' to comply with the style guide. This change is outside of any code references or URLs, so it's appropriate to make this correction.