The Model Context Protocol
The Model Context Protocol (MCP) is a specification for how AI agents can communicate with, and discover the capabilities of, servers which implement specific functionality. Whereas an LLM may not natively have the capability to perform certain actions, such as sending email, if it can communicate with an MCP server that advertises this capability it is empowered to delegate the implementation of email sending to that server. Naturally, this type of interaction can extend beyond email to whatever services implementers wish to create. For the purposes of implementing authorization with Stytch, MCP Clients are a type of Connected App. MCP Clients request access to a member’s account through theauthorization_code grant.
In this guide, we’ll walk through the creation of a Remote (MCP) server which manages authorization using Stytch. We’ll discuss all the steps that go into a complete MCP Authorization flow. These steps are performed between a variety of actors - some are performed by the MCP Client, some by your MCP Server, and some by Stytch directly. At each step, we’ll call out which actor is responsible for what. For steps that need to be implemented by you, we’ll show code snippets in a variety of languages.
To illustrate, we’ll follow along with the Stytch MCP Server, which uses Stytch under the hood.
This guide is for implementing MCP Authorization with the Stytch product. For a more general purpose guide on the role of OAuth within the MCP ecosystem, check out our Blog.
Pre-requisites
At a high level, your MCP Server is responsible for the following:- Validating Stytch-issued access tokens and returning a specific error format on failure
- Serving a JSON document instructing MCP Clients to talk to Stytch
<IdentityProvider />React Component for Consumer applications<B2BIdentityProvider />React Component for B2B applications
The Complete MCP Authorization Flow
Access Token Validation
This step is performed by your MCP Server.
401 Unauthorized status code, and a specially formatted WWW-Authenticate header. This header will direct the client to look up the Protected Resource Metadata (PRM) document as detailed in RFC 9728.Let’s try with the Stytch MCP Server, which uses Stytch under the hood.WWW-Authenticate header with a resource_metadata attribute.- Express
- NextJS
- FastMCP
- Hono
Node projects should use the Stytch backend SDK to perform token validation using the Introspect Access Token API method.
Protected Resource Metadata
This step is performed by your MCP Server.
resource_metadata field from the WWW-Authenticate header returned and issue a GET request to that URL to retrieve the Protected Resource Metadata (PRM) Document. The PRM Document is hosted by your MCP Server. Your MCP Server will return a JSON document containing information about how the client should request an access token.| Field Name | Meaning |
|---|---|
| resource | The resource identifier - a https:// URL. This should be the location of your MCP Server. |
| authorization_servers | A JSON array containing a list of OAuth authorization server issuer URLs. This should be your Stytch PROJECT_DOMAIN. |
| scopes_supported | A JSON array of OAuth scopes that can be requested from your Stytch project. Some MCP clients will use the scopes returned in the scopes_supported array as the initial set of permissions to request. |
- Express
- NextJS
- FastMCP
- Hono
Authorization Server Metadata
This step is performed by Stytch.
authorization_servers field from the PRM document and issue a GET request to the first authorization server’s Authorization Server Metadata (ASM) endpoint, as defined by RFC 8414.The ASM endpoint is hosted by Stytch, not by your server. However, the ASM endpoint needs to know the location where you host the<IdentityProvider />React Component for Consumer applications<B2BIdentityProvider />React Component for B2B applications
https://example.com - we’ll come back to it later.<B2BIdentityProvider /> or <IdentityProvider /> React component is hosted at https://stytch.com/oauth/authorize, which is communicated to the MCP Client as the authorization_endpoint.| Field Name | Meaning |
|---|---|
| authorization_endpoint | URL of your Stytch Project’s authorization endpoint, hosted by you and configured in the Stytch Dashboard. |
| registration_endpoint | URL of your Stytch Project’s OAuth 2.0 Dynamic Client Registration endpoint, hosted by Stytch. |
| token_endpoint | URL of your Stytch Project’s token endpoint, hosted by Stytch. |
Dynamic Client Registration
This step is performed by Stytch.
client_id by Stytch, in order to uniquely identify itself. There are two ways for this to happen:- Pre-registration: if the MCP Client already has a
client_id, it may use thatclient_idand skip this step. Some MCP Clients will let users specify their ownclient_id. This is especially common in Enterprise environments where there are more limitations on what parties have access to sensitive data. - Dynamic registration: the MCP Client can send a Dynamic Client Registration (DCR) request to your Stytch Project’s DCR Endpoint( for B2B, for Consumer) and be granted a unique
client_idon the fly.
client_id. All clients created through DCR will be Third Party Public clients within Stytch.Request Consent
This step is performed by the MCP Client.
authorization_endpoint previously returned in the ASM document. The MCP client will pass in various query parameters to tell us who it is and what data it is trying to access. The full set of parameters that may be passed are detailed in the OAuth 2.1 Authorization Request specification.Grant Consent
This step is performed by your main application.
authorization_endpoint is the location of a web page hosting the Stytch <B2BIdentityProvider /> or <IdentityProvider /> React component. This component will parse the query parameters passed by the MCP Client in step 5, validate the authorization request, and prompt the member for consent. If the member consents to share their data with the MCP Client, the member will be redirected back to the redirect_uri owned by the client with a code.Exchange Authorization Code
This step is performed by Stytch.
<B2BIdentityProvider /> or <IdentityProvider /> React component will redirect the member/user back to the client with an authorization code that the client exchanges for tokens. The client will call the token_endpoint listed in the ASM response - an endpoint hosted by Stytch.Complete
Finally, the MCP Client can make requests to your MCP Server using the access token embedded in the Authorization header:The middleware we implemented in Step 1 will validate this token and process the request. Your MCP Server can now use the authenticated MCP Client’s information inside tool calls.
What’s Next
You should now have a working overview of all the steps that go in to setting up authorization for MCP servers.- Follow along with our Cloudflare MCP Guide and demo app
- Learn more about OAuth Scopes in the Stytch platform