|
| 1 | +# Setting Up Stytch OAuth for BioMCP |
| 2 | + |
| 3 | +This document outlines the steps required to set up Stytch OAuth authentication for the BioMCP application. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- A Stytch account (sign up at [Stytch](https://stytch.com/)) |
| 8 | +- Access to the BioMCP codebase |
| 9 | + |
| 10 | +## Setting Up Your Stytch Account |
| 11 | + |
| 12 | +1. **Create a Stytch Account** |
| 13 | + |
| 14 | + - Sign up at [Stytch](https://stytch.com/) |
| 15 | + - Select "Consumer Authentication" when prompted during the setup process |
| 16 | + |
| 17 | +2. **Configure the Stytch Project** |
| 18 | + |
| 19 | + - In the Stytch dashboard, navigate to "Frontend SDKs" and enable the frontend SDK |
| 20 | + - Navigate to "Connected Apps" and enable "Dynamic Client Registration" |
| 21 | + - This allows MCP clients to register themselves dynamically with Stytch |
| 22 | + |
| 23 | +3. **Retrieve Your Credentials** |
| 24 | + - Go to "Project Settings" in the Stytch dashboard |
| 25 | + - Note down the following credentials: |
| 26 | + - Project ID |
| 27 | + - Secret (API Key) |
| 28 | + - Public Token |
| 29 | + |
| 30 | +## Configuring BioMCP with Stytch |
| 31 | + |
| 32 | +1. **Update the Wrangler Configuration** |
| 33 | + |
| 34 | + - Open `wrangler.toml` in the BioMCP project |
| 35 | + - Update the following variables with your Stytch credentials: |
| 36 | + ```toml |
| 37 | + STYTCH_PROJECT_ID = "your-project-id" |
| 38 | + STYTCH_SECRET = "your-secret-key" |
| 39 | + STYTCH_PUBLIC_TOKEN = "your-public-token" |
| 40 | + ``` |
| 41 | + - For development, use the test environment: |
| 42 | + ```toml |
| 43 | + STYTCH_API_URL = "https://test.stytch.com/v1" |
| 44 | + ``` |
| 45 | + - For production, use: |
| 46 | + ```toml |
| 47 | + STYTCH_API_URL = "https://api.stytch.com/v1" |
| 48 | + ``` |
| 49 | + |
| 50 | +2. **Configure the OAuth KV Namespace** |
| 51 | + |
| 52 | + - Create a KV namespace in Cloudflare for storing OAuth tokens and state |
| 53 | + - Update the KV namespace ID in `wrangler.toml`: |
| 54 | + ```toml |
| 55 | + [[kv_namespaces]] |
| 56 | + binding = "OAUTH_KV" |
| 57 | + id = "your-kv-namespace-id" |
| 58 | + ``` |
| 59 | + |
| 60 | +3. **Configure JWT Secret** |
| 61 | + - Set a strong JWT secret for token signing: |
| 62 | + ```toml |
| 63 | + JWT_SECRET = "your-secure-jwt-secret" |
| 64 | + ``` |
| 65 | + |
| 66 | +## OAuth Flow Overview |
| 67 | + |
| 68 | +The BioMCP application uses the following OAuth flow: |
| 69 | + |
| 70 | +1. **Discovery**: MCP clients fetch OAuth metadata to locate Stytch authorization endpoints |
| 71 | +2. **Registration**: MCP clients dynamically register with Stytch |
| 72 | +3. **Authorization**: Users are redirected to Stytch for authentication and consent |
| 73 | +4. **Token Exchange**: After consent, authorization codes are exchanged for access tokens |
| 74 | +5. **MCP Connection**: MCP clients connect to the BioMCP server using OAuth access tokens |
| 75 | + |
| 76 | +## Endpoints |
| 77 | + |
| 78 | +The worker implements the following OAuth endpoints: |
| 79 | + |
| 80 | +- `/.well-known/oauth-authorization-server`: OAuth server metadata |
| 81 | +- `/authorize`: OAuth authorization endpoint |
| 82 | +- `/callback`: OAuth callback endpoint |
| 83 | +- `/token`: Token exchange endpoint |
| 84 | + |
| 85 | +## Testing |
| 86 | + |
| 87 | +To test the OAuth implementation: |
| 88 | + |
| 89 | +1. Deploy the worker to Cloudflare: |
| 90 | + |
| 91 | + ``` |
| 92 | + wrangler deploy |
| 93 | + ``` |
| 94 | +
|
| 95 | +2. Use the MCP Inspector or another OAuth client to test the flow: |
| 96 | + - Set the OAuth discovery URL to: `https://your-worker.workers.dev/.well-known/oauth-authorization-server` |
| 97 | + - The inspector will guide you through the OAuth flow |
| 98 | +
|
| 99 | +## Troubleshooting |
| 100 | +
|
| 101 | +- **JWT Validation Issues**: Ensure the JWKS endpoint is correctly configured and accessible |
| 102 | +- **Callback Errors**: Check that the redirect URIs are properly registered and match exactly |
| 103 | +- **Token Exchange Failures**: Verify that the authorization code is valid and not expired |
| 104 | +
|
| 105 | +## Security Considerations |
| 106 | +
|
| 107 | +- Always use HTTPS for all OAuth endpoints |
| 108 | +- Implement proper CORS headers for cross-origin requests |
| 109 | +- Regularly rotate the JWT secret |
| 110 | +- Use the production Stytch API URL for production environments |
0 commit comments