Skip to content

Commit eb20cd2

Browse files
dinasaur404RebeccaTamachiro
authored andcommitted
Add auth provider integrations, authentication context, and permission controls to MCP authz docs (#21440)
Include example integration with Stytch, Auth0, and WorkOS. Add a section on how to use authentication context in the MCP server and how to implement permission/role based access control within MCP tools.
1 parent 6704e0f commit eb20cd2

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

src/content/docs/agents/model-context-protocol/authorization.mdx

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,119 @@ Read the docs for the [Workers oAuth Provider Library](https://github.com/cloudf
124124

125125
### (3) Bring your own OAuth Provider
126126

127-
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).
127+
If your application already implements an Oauth Provider itself, or you use [Stytch](https://stytch.com/), [Auth0](https://auth0.com/), [WorkOS](https://workos.com/), 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).
128+
129+
You can use the auth provider to:
130+
- Allow users to authenticate to your MCP server through email, social logins, SSO (single sign-on), and MFA (multi-factor authentication).
131+
- Define scopes and permissions that directly map to your MCP tools.
132+
- Present users with a consent page corresponding with the requested permissions.
133+
- Enforce the permissions so that agents can only invoke permitted tools.
134+
135+
#### Stytch
136+
Get started with a [remote MCP server that uses Stytch](https://stytch.com/docs/guides/connected-apps/mcp-servers) to allow users to sign in with email, Google login or enterprise SSO and authorize their AI agent to view and manage their company’s OKRs on their behalf. Stytch will handle restricting the scopes granted to the AI agent based on the user’s role and permissions within their organization. When authorizing the MCP Client, each user will see a consent page that outlines the permissions that the agent is requesting that they are able to grant based on their role.
137+
138+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/ai/tree/main/demos/mcp-stytch-b2b-okr-manager)
139+
140+
For more consumer use cases, deploy a remote MCP server for a To Do app that uses Stytch for authentication and MCP client authorization. Users can sign in with email and immediately access the To Do lists associated with their account, and grant access to any AI assistant to help them manage their tasks.
141+
142+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/ai/tree/main/demos/mcp-stytch-consumer-todo-list)
143+
144+
#### Auth0
145+
Get started with a remote MCP server that uses Auth0 to authenticate users through email, social logins, or enterprise SSO to interact with their todos and personal data through AI agents. The MCP server securely connects to API endpoints on behalf of users, showing exactly which resources the agent will be able to access once it gets consent from the user. In this implementation, access tokens are automatically refreshed during long running interactions.
146+
147+
To set it up, first deploy the protected API endpoint:
148+
149+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/ai/tree/main/demos/remote-mcp-auth0/packages/todos-api)
150+
151+
Then, deploy the MCP server that handles authentication through Auth0 and securely connects AI agents to your API endpoint.
152+
153+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/ai/tree/main/demos/remote-mcp-auth0/packages/mcp-auth0-oidc)
154+
155+
#### WorkOS
156+
157+
Get started with a remote MCP server that uses WorkOS's AuthKit to authenticate users and manage the permissions granted to AI agents. In this example, the MCP server dynamically exposes tools based on the user's role and access rights. All authenticated users get access to the `add` tool, but only users who have been assigned the `image_generation` permission in WorkOS can grant the AI agent access to the image generation tool. This showcases how MCP servers can conditionally expose capabilities to AI agents based on the authenticated user's role and permission.
158+
159+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/ai/tree/main/demos/remote-mcp-authkit)
160+
161+
## Using Authentication Context in Your MCP Server
162+
163+
When a user authenticates to your MCP server through Cloudflare's OAuth Provider, their identity information and tokens are made available through the `props` parameter.
164+
165+
```js
166+
export class MyMCP extends McpAgent<Env, unknown, AuthContext> {
167+
async init() {
168+
this.server.tool("userInfo", "Get user information", {}, async () => ({
169+
content: [{ type: "text", text: `Hello, ${this.props.claims.name || "user"}!` }],
170+
}));
171+
}
172+
}
173+
```
174+
175+
The authentication context can be used for:
176+
177+
- Accessing user-specific data by using the user ID (this.props.claims.sub) as a key
178+
- Checking user permissions before performing operations
179+
- Customizing responses based on user preferences or attributes
180+
- Using authentication tokens to make requests to external services on behalf of the user
181+
- Ensuring consistency when users interact with your application through different interfaces (dashboard, API, MCP server)
182+
183+
## Implementing Permission-Based Access for MCP Tools
184+
185+
You can implement fine-grained authorization controls for your MCP tools based on user permissions. This allows you to restrict access to certain tools based on the user's role or specific permissions.
186+
187+
```js
188+
// Create a wrapper function to check permissions
189+
function requirePermission(permission, handler) {
190+
return async (request, context) => {
191+
// Check if user has the required permission
192+
const userPermissions = context.props.permissions || [];
193+
194+
if (!userPermissions.includes(permission)) {
195+
return {
196+
content: [{ type: "text", text: `Permission denied: requires ${permission}` }],
197+
status: 403
198+
};
199+
}
200+
201+
// If permission check passes, execute the handler
202+
return handler(request, context);
203+
};
204+
}
205+
206+
// Use the wrapper with your MCP tools
207+
async init() {
208+
// Basic tools available to all authenticated users
209+
this.server.tool("basicTool", "Available to all users", {}, async () => {
210+
// Implementation for all users
211+
});
212+
213+
// Protected tool using the permission wrapper
214+
this.server.tool(
215+
"adminAction",
216+
"Administrative action requiring special permission",
217+
{ /* parameters */ },
218+
requirePermission("admin", async (req) => {
219+
// Only executes if user has "admin" permission
220+
return {
221+
content: [{ type: "text", text: "Admin action completed" }]
222+
};
223+
})
224+
);
225+
226+
// Conditionally register tools based on user permissions
227+
if (this.props.permissions?.includes("special_feature")) {
228+
this.server.tool("specialTool", "Special feature", {}, async () => {
229+
// This tool only appears for users with the special_feature permission
230+
});
231+
}
232+
}
233+
```
234+
235+
Benefits:
236+
- Authorization check at the tool level ensures proper access control
237+
- Allows you to define permission checks once and reuse them across tools
238+
- Provides clear feedback to users when permission is denied
239+
- Can choose to only present tools that the agent is able to call
128240
129241
## Next steps
130242

0 commit comments

Comments
 (0)