WorkOS
The WorkOS plugin provides authentication for your mcp server using WorkOS AuthKit.
For the complete documentation index, see llms.txt. Markdown variants of every page are available by appending .md to the URL.Installation
Install the WorkOS plugin:
pnpm i @xmcp-dev/workosWorkOS Setup
Before getting started, we need to configure our WorkOS application:
- Go to your WorkOS Dashboard.
- In the Overview page, under Quickstart, find and save your
WORKOS_API_KEYandWORKOS_CLIENT_ID. - Go to Domains and save the AuthKit domain, it looks like this
https://xxx.authkit.app. - Navigate to Connect and go to Configuration and enable the following options inside MCP Auth settings: Client ID Metadata Document (CIMD) and Dynamic Client Registration (DCR).
Environment Variables
Create a .env file in the root of your project and configure the following environment variables:
For production, the BASE_URL should be replaced with your deployed server URL.
Set up the Provider
Create a middleware.ts and import the provider from the package:
Configuration Options
apiKey: WorkOS API key from your dashboard.clientId: WorkOS client ID for OAuth.authkitDomain: Your AuthKit domain.baseURL: Base URL of your app for OAuth callbacks.docsURL: (Optional) URL for your MCP server documentation.
Get a user session
Access the authenticated user in your xmcp tools using getSession.
Example: Greet the user with their WorkOS identity
Get user details
Access the authenticated user in your xmcp tools using getUser.
Example: Get user details
Access the client
The getClient() function gives you access to the full WorkOS Node SDK, allowing you to leverage all WorkOS features in your MCP tools.
Example: Get organization memberships
Troubleshooting
Token Expired Errors
Access tokens are short-lived. If you see token_expired errors:
- MCP clients should automatically refresh tokens.
- If errors persist, the client may have a bug or the refresh token expired.
- Users can disconnect and reconnect to get fresh tokens.
Redirect URI Not Registered
If you see this error during OAuth:
- Copy the redirect URI from the error message.
- Go to WorkOS Dashboard → Connect → Applications
- Add the redirect URI to the application.
Finding a Client's Redirect URI
If you need to manually register a client:
- Check the error message: OAuth errors include the redirect URI that needs to be registered.
- Check client documentation: Each MCP client documents its callback URL.
- Check your server logs: Look for the
redirect_uriparameter in failed OAuth requests.
Session Not Initialized
If you see Session not initialized or can only be used within the workos-context-session context errors:
- Ensure
getSessionis called within a tool or handler that runs through the middleware pipeline. - Verify the
workosProvidermiddleware is properly configured in yourmiddleware.ts. - Make sure the route is under the
/mcppath. - Check that the request includes a valid bearer token.
Wrong Redirect URI
If you see that the redirect URI is not registered or not working, you can manually register it in the WorkOS Dashboard:
- Check the error message: OAuth errors usually include the redirect URI.
- Check client documentation: Each client documents its callback URL.
- Check server logs: Look for the
redirect_uriparameter in OAuth requests.