|
| 1 | +import OAuthProvider from '@cloudflare/workers-oauth-provider' |
| 2 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' |
| 3 | +import { McpAgent } from 'agents/mcp' |
| 4 | +import { env } from 'cloudflare:workers' |
| 5 | + |
| 6 | +import { |
| 7 | + createAuthHandlers, |
| 8 | + handleTokenExchangeCallback, |
| 9 | +} from '@repo/mcp-common/src/cloudflare-oauth-handler' |
| 10 | +import { registerAccountTools } from '@repo/mcp-common/src/tools/account' |
| 11 | + |
| 12 | +import { registerDEXTools } from './tools/dex' |
| 13 | + |
| 14 | +import type { AccountSchema, UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler' |
| 15 | + |
| 16 | +// Context from the auth process, encrypted & stored in the auth token |
| 17 | +// and provided to the DurableMCP as this.props |
| 18 | +export type Props = { |
| 19 | + accessToken: string |
| 20 | + user: UserSchema['result'] |
| 21 | + accounts: AccountSchema['result'] |
| 22 | +} |
| 23 | + |
| 24 | +export type State = { activeAccountId: string | null } |
| 25 | + |
| 26 | +export class MyMCP extends McpAgent<Env, State, Props> { |
| 27 | + server = new McpServer({ |
| 28 | + name: 'Remote MCP Server with Cloudflare DEX Analysis', |
| 29 | + version: '1.0.0', |
| 30 | + }) |
| 31 | + |
| 32 | + initialState: State = { |
| 33 | + activeAccountId: null, |
| 34 | + } |
| 35 | + |
| 36 | + async init() { |
| 37 | + registerAccountTools(this) |
| 38 | + |
| 39 | + // EXAMPLE TOOLS — register your own here |
| 40 | + registerDEXTools(this) |
| 41 | + } |
| 42 | + |
| 43 | + getActiveAccountId() { |
| 44 | + // TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch |
| 45 | + try { |
| 46 | + return this.state.activeAccountId ?? null |
| 47 | + } catch (e) { |
| 48 | + return null |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + setActiveAccountId(accountId: string) { |
| 53 | + // TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch |
| 54 | + try { |
| 55 | + this.setState({ |
| 56 | + ...this.state, |
| 57 | + activeAccountId: accountId, |
| 58 | + }) |
| 59 | + } catch (e) { |
| 60 | + return null |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +const DexScopes = { |
| 66 | + 'account:read': 'See your account info such as account details, analytics, and memberships.', |
| 67 | + 'user:read': 'See your user info such as name, email address, and account memberships.', |
| 68 | + 'teams:read': 'See Cloudflare Cloudflare Zero Trust data for your account', // TODO: Finalize configuration for this. |
| 69 | + offline_access: 'Grants refresh tokens for long-lived access.', |
| 70 | +} as const |
| 71 | + |
| 72 | +export default new OAuthProvider({ |
| 73 | + apiRoute: '/sse', |
| 74 | + // @ts-ignore |
| 75 | + apiHandler: MyMCP.mount('/sse'), |
| 76 | + // @ts-ignore |
| 77 | + defaultHandler: createAuthHandlers({ scopes: DexScopes }), |
| 78 | + authorizeEndpoint: '/oauth/authorize', |
| 79 | + tokenEndpoint: '/token', |
| 80 | + tokenExchangeCallback: (options) => |
| 81 | + handleTokenExchangeCallback(options, env.CLOUDFLARE_CLIENT_ID, env.CLOUDFLARE_CLIENT_SECRET), |
| 82 | + // Cloudflare access token TTL |
| 83 | + accessTokenTTL: 3600, |
| 84 | + clientRegistrationEndpoint: '/register', |
| 85 | +}) |
0 commit comments