Connect MCP Clients Without A User With The Client Credentials Grant
On this page
This feature requires at least a Starter plan.
The MCP OAuth Client Credentials extension (io.modelcontextprotocol/oauth-client-credentials) lets a service call an MCPAn open protocol that connects AI applications to external tools and data. An MCP server can require OAuth 2.0 access tokens, with FusionAuth as the authorization server. server with no user present, such as a scheduled job or a CI pipeline. FusionAuth supports the extension through the Client Credentials grant between two EntitiesA FusionAuth object that represents something other than a user, such as a server or an organization.. The MCP server is the target Entity, and each MCP client is a recipient Entity with a Grant to the MCP server.
The extension allows two ways to authenticate: a client secret, or a JWT signed with the client's private key (private_key_jwt). FusionAuth supports only the client secret. In the MCP TypeScript SDK, use ClientCredentialsProvider, not PrivateKeyJwtProvider.
Prerequisites#
- FusionAuth with a Starter, Essentials, or Enterprise license. Entities require a paid plan.
- A FusionAuth instance that MCP clients can reach over HTTPS, such as
https://auth.example.com. The MCP TypeScript SDK refuses to send credentials to a token endpoint that doesn't use HTTPS. - An MCP server that validates FusionAuth access tokens, such as
https://mcp.example.com/mcp. To build one, see Control MCP Server Access.
How The Connection Works#
When an MCP client connects to the MCP server, the client:
- Calls the MCP server, gets a
401, and reads the server's protected resource metadata to find FusionAuth. - Reads the FusionAuth discovery document to find the token endpoint.
- Sends the client Id and client secret of the recipient Entity to the token endpoint, with the
target-entityscope from the server's metadata. - Receives an access token whose
audclaim contains the Id of the MCP server Entity, and sends the token on every MCP request.
An MCP client gets an access token from FusionAuth with the Client Credentials grant, then calls the MCP server.
FusionAuth sets aud from the target-entity scope. FusionAuth ignores the resource parameter that MCP clients send with this grant, so the token doesn't contain the MCP server URL. FusionAuth also drops any requested scope that doesn't start with target-entity:. A token requested with only other scopes has no aud or permissions claim, and the MCP server must reject the token.
Configure FusionAuth#
-
Find the Id of the Tenant for your Entities. Navigate to Tenants and copy the Id of the Tenant. This guide uses
<tenantId>for that value. -
Set the Tenant's Issuer to the URL MCP clients use to find FusionAuth. Edit the Tenant, and on the General tab, set Issuer to
https://auth.example.com/<tenantId>. FusionAuth serves the Tenant's discovery document at/.well-known/openid-configuration/<tenantId>, and MCP clients look there for an authorization serverThe server that authenticates users and issues tokens to applications in OAuth 2.0. FusionAuth is an authorization server. URL with a path.CautionChanging the Issuer changes the
issclaim of every token the Tenant issues. Applications that validateissagainst the old value reject new tokens until you update the applications. -
Create an Entity Type for MCP servers. Navigate to Entity Management -> Entity Types and click Add . Add a permission for each kind of access your MCP server checks, such as
readandwrite. -
Create an Entity for the MCP server with the new Entity Type. Navigate to Entity Management -> Entities and click . Copy the Id of the Entity. This guide uses
<mcpServerEntityId>for that value. -
Create an Entity for each MCP client. Copy the Client Id and Client secret of each client Entity, and store the secret in a secrets manager.
-
Grant each client Entity the permissions the client needs on the MCP server Entity. Manage the MCP server Entity, and add a Grant for the client Entity. To add Grants with the API instead, see Configure Entities.
-
Check that the client Entity can get a token:
$ curl -u '<clientId>:<clientSecret>' \ -d 'grant_type=client_credentials' \ -d 'scope=target-entity:<mcpServerEntityId>' \ https://auth.example.com/oauth2/token/<tenantId>FusionAuth returns an access token whose
audclaim contains<mcpServerEntityId>. Thepermissionsclaim lists the permissions from the Grant.
Configure The MCP Server#
-
Set
authorization_serversin the MCP server's protected resource metadata tohttps://auth.example.com/<tenantId>. -
Set
scopes_supportedin the protected resource metadata to["target-entity:<mcpServerEntityId>"]. The MCP TypeScript SDK requests the scopes inscopes_supportedbefore the scope set on the client. A scope with no permissions after the Entity Id requests every permission from the Grant. A scope that names several permissions fails when the client Entity lacks any one of them. -
Validate each access token. Check the signature and the expiration, as Control MCP Server Access does with the JWT Validate API. Then check the following claims:
issishttps://auth.example.com/<tenantId>.audcontains<mcpServerEntityId>.permissionscontains the permission each tool requires, under the<mcpServerEntityId>key, such as{"<mcpServerEntityId>": ["read"]}.
To check the signature locally with the keys at
/.well-known/jwks.json, sign the tokens with an RSA or EC key. FusionAuth doesn't publish HMAC keys in the JWKS. Set the key in the Access token signing key of the Entity Type, or of the Tenant. -
Advertise the extension in the server's
server/discoverresponse. The extension recommends this step, but clients connect without the step:{ "capabilities": { "extensions": { "io.modelcontextprotocol/oauth-client-credentials": {} } } }
Connect An MCP Client#
The MCP TypeScript SDK handles discovery, the token request, and token renewal. Pass the client Id and client secret of the client Entity to ClientCredentialsProvider, and set expectedIssuer to the Tenant's Issuer:
import {
Client,
ClientCredentialsProvider,
StreamableHTTPClientTransport,
} from '@modelcontextprotocol/client';
const provider = new ClientCredentialsProvider({
clientId: process.env.FUSIONAUTH_CLIENT_ID,
clientSecret: process.env.FUSIONAUTH_CLIENT_SECRET,
expectedIssuer: 'https://auth.example.com/<tenantId>',
});
const client = new Client({ name: 'nightly-report', version: '1.0.0' }, { capabilities: {} });
const transport = new StreamableHTTPClientTransport(new URL('https://mcp.example.com/mcp'), {
authProvider: provider,
});
await client.connect(transport);
const tools = await client.listTools();
console.log(tools.tools.map((tool) => tool.name));
expectedIssuer stops the client from sending the secret to any other authorization server that the MCP server's metadata names. Run the client. When the connection works, the client prints the names of the MCP server's tools.
A client secret works until you change the secret. To change a secret, see Client Secret Rotation.
Troubleshooting#
| Problem | Cause and fix |
|---|---|
The token has no aud or permissions claim. |
The client requested no target-entity scope. Set scopes_supported on the MCP server to ["target-entity:<mcpServerEntityId>"]. |
FusionAuth returns invalid_scope with does not have a grant to the target entity. |
The client Entity has no Grant on the MCP server Entity. Add the Grant. |
FusionAuth returns invalid_scope with The permission names [...] are invalid. |
The scope names a permission that the Grant doesn't include. Add the permission to the Grant, or remove the permission from the scope. |
| The SDK throws an issuer mismatch error. | expectedIssuer, authorization_servers, and the Tenant's Issuer must all be the same URL. |
Related
Learn how to get an access token for machine-to-machine requests with the OAuth Client Credentials grant.
Learn how to connect claude.ai and chatgpt.com to an MCP server protected by FusionAuth using Client ID Metadata Documents.
Learn how to protect Model Context Protocol (MCP) servers with OAuth authentication using FusionAuth.
Learn about the APIs for creating, retrieving, updating and deleting entities, as well as searching them.