|
| 1 | +import OAuthProvider from '@cloudflare/workers-oauth-provider' |
| 2 | +import { McpAgent } from 'agents/mcp' |
| 3 | +import { env } from 'cloudflare:workers' |
| 4 | + |
| 5 | +import { |
| 6 | + createAuthHandlers, |
| 7 | + handleTokenExchangeCallback, |
| 8 | +} from '@repo/mcp-common/src/cloudflare-oauth-handler' |
| 9 | +import { CloudflareMCPServer } from '@repo/mcp-common/src/server' |
| 10 | +import { registerAccountTools } from '@repo/mcp-common/src/tools/account' |
| 11 | + |
| 12 | +import { MetricsTracker } from '../../../packages/mcp-observability/src' |
| 13 | +import { registerIntegrationsTools } from './tools/integrations' |
| 14 | + |
| 15 | +import type { AccountSchema, UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler' |
| 16 | +import type { CloudflareMcpAgent } from '@repo/mcp-common/src/types/cloudflare-mcp-agent' |
| 17 | + |
| 18 | +const metrics = new MetricsTracker(env.MCP_METRICS, { |
| 19 | + name: env.MCP_SERVER_NAME, |
| 20 | + version: env.MCP_SERVER_VERSION, |
| 21 | +}) |
| 22 | + |
| 23 | +export type Props = { |
| 24 | + accessToken: string |
| 25 | + user: UserSchema['result'] |
| 26 | + accounts: AccountSchema['result'] |
| 27 | +} |
| 28 | + |
| 29 | +export type State = { activeAccountId: string | null } |
| 30 | +export class CASBMCP extends McpAgent<Env, State, Props> { |
| 31 | + _server: CloudflareMCPServer | undefined |
| 32 | + set server(server: CloudflareMCPServer) { |
| 33 | + this._server = server |
| 34 | + } |
| 35 | + |
| 36 | + get server(): CloudflareMCPServer { |
| 37 | + if (!this._server) { |
| 38 | + throw new Error('Tried to access server before it was initialized') |
| 39 | + } |
| 40 | + |
| 41 | + return this._server |
| 42 | + } |
| 43 | + |
| 44 | + constructor(ctx: DurableObjectState, env: Env) { |
| 45 | + super(ctx, env) |
| 46 | + } |
| 47 | + |
| 48 | + async init() { |
| 49 | + this.server = new CloudflareMCPServer(this.props.user.id, this.env.MCP_METRICS, { |
| 50 | + name: this.env.MCP_SERVER_NAME, |
| 51 | + version: this.env.MCP_SERVER_VERSION, |
| 52 | + }) |
| 53 | + |
| 54 | + registerAccountTools(this) |
| 55 | + registerIntegrationsTools(this) |
| 56 | + } |
| 57 | + |
| 58 | + getActiveAccountId() { |
| 59 | + try { |
| 60 | + return this.state.activeAccountId ?? null |
| 61 | + } catch (e) { |
| 62 | + console.error('getActiveAccountId failured: ', e) |
| 63 | + return null |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + setActiveAccountId(accountId: string) { |
| 68 | + try { |
| 69 | + this.setState({ |
| 70 | + ...this.state, |
| 71 | + activeAccountId: accountId, |
| 72 | + }) |
| 73 | + } catch (e) { |
| 74 | + return null |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +const CloudflareOneCasbScopes = { |
| 79 | + 'account:read': 'See your account info such as account details, analytics, and memberships.', |
| 80 | + 'user:read': 'See your user info such as name, email address, and account memberships.', |
| 81 | + 'teams:read': 'See Cloudflare One Resources', |
| 82 | + offline_access: 'Grants refresh tokens for long-lived access.', |
| 83 | +} as const |
| 84 | + |
| 85 | +export default new OAuthProvider({ |
| 86 | + apiRoute: '/sse', |
| 87 | + // @ts-ignore |
| 88 | + apiHandler: CASBMCP.mount('/sse'), |
| 89 | + // @ts-ignore |
| 90 | + defaultHandler: createAuthHandlers({ scopes: CloudflareOneCasbScopes, metrics }), |
| 91 | + authorizeEndpoint: '/oauth/authorize', |
| 92 | + tokenEndpoint: '/token', |
| 93 | + tokenExchangeCallback: (options) => |
| 94 | + handleTokenExchangeCallback(options, env.CLOUDFLARE_CLIENT_ID, env.CLOUDFLARE_CLIENT_SECRET), |
| 95 | + // Cloudflare access token TTL |
| 96 | + accessTokenTTL: 3600, |
| 97 | + clientRegistrationEndpoint: '/register', |
| 98 | +}) |
0 commit comments