|
| 1 | +import OAuthProvider from '@cloudflare/workers-oauth-provider' |
| 2 | +import { McpAgent } from 'agents/mcp' |
| 3 | + |
| 4 | +import { |
| 5 | + createAuthHandlers, |
| 6 | + handleTokenExchangeCallback, |
| 7 | +} from '@repo/mcp-common/src/cloudflare-oauth-handler' |
| 8 | +import { handleDevMode } from '@repo/mcp-common/src/dev-mode' |
| 9 | +import { getUserDetails, UserDetails } from '@repo/mcp-common/src/durable-objects/user_details.do' |
| 10 | +import { getEnv } from '@repo/mcp-common/src/env' |
| 11 | +import { RequiredScopes } from '@repo/mcp-common/src/scopes' |
| 12 | +import { initSentryWithUser } from '@repo/mcp-common/src/sentry' |
| 13 | +import { CloudflareMCPServer } from '@repo/mcp-common/src/server' |
| 14 | +import { registerAccountTools } from '@repo/mcp-common/src/tools/account.tools' |
| 15 | +import { registerZoneTools } from '@repo/mcp-common/src/tools/zone.tools' |
| 16 | +import { MetricsTracker } from '@repo/mcp-observability' |
| 17 | + |
| 18 | +import { registerGraphQLTools } from './tools/graphql.tools' |
| 19 | + |
| 20 | +import type { AuthProps } from '@repo/mcp-common/src/cloudflare-oauth-handler' |
| 21 | +import type { Env } from './graphql.context' |
| 22 | + |
| 23 | +export { UserDetails } |
| 24 | + |
| 25 | +const env = getEnv<Env>() |
| 26 | + |
| 27 | +const metrics = new MetricsTracker(env.MCP_METRICS, { |
| 28 | + name: env.MCP_SERVER_NAME, |
| 29 | + version: env.MCP_SERVER_VERSION, |
| 30 | +}) |
| 31 | + |
| 32 | +// Context from the auth process, encrypted & stored in the auth token |
| 33 | +// and provided to the DurableMCP as this.props |
| 34 | +type Props = AuthProps |
| 35 | +type State = { activeAccountId: string | null } |
| 36 | + |
| 37 | +export class GraphQLMCP extends McpAgent<Env, State, Props> { |
| 38 | + _server: CloudflareMCPServer | undefined |
| 39 | + set server(server: CloudflareMCPServer) { |
| 40 | + this._server = server |
| 41 | + } |
| 42 | + |
| 43 | + get server(): CloudflareMCPServer { |
| 44 | + if (!this._server) { |
| 45 | + throw new Error('Tried to access server before it was initialized') |
| 46 | + } |
| 47 | + |
| 48 | + return this._server |
| 49 | + } |
| 50 | + |
| 51 | + constructor(ctx: DurableObjectState, env: Env) { |
| 52 | + super(ctx, env) |
| 53 | + } |
| 54 | + |
| 55 | + async init() { |
| 56 | + this.server = new CloudflareMCPServer({ |
| 57 | + userId: this.props.user.id, |
| 58 | + wae: this.env.MCP_METRICS, |
| 59 | + serverInfo: { |
| 60 | + name: this.env.MCP_SERVER_NAME, |
| 61 | + version: this.env.MCP_SERVER_VERSION, |
| 62 | + }, |
| 63 | + sentry: initSentryWithUser(env, this.ctx, this.props.user.id), |
| 64 | + }) |
| 65 | + |
| 66 | + // Register account tools |
| 67 | + registerAccountTools(this) |
| 68 | + |
| 69 | + // Register zone tools |
| 70 | + registerZoneTools(this) |
| 71 | + |
| 72 | + // Register GraphQL tools |
| 73 | + registerGraphQLTools(this) |
| 74 | + } |
| 75 | + |
| 76 | + async getActiveAccountId() { |
| 77 | + try { |
| 78 | + // Get UserDetails Durable Object based off the userId and retrieve the activeAccountId from it |
| 79 | + // we do this so we can persist activeAccountId across sessions |
| 80 | + const userDetails = getUserDetails(env, this.props.user.id) |
| 81 | + return await userDetails.getActiveAccountId() |
| 82 | + } catch (e) { |
| 83 | + this.server.recordError(e) |
| 84 | + return null |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + async setActiveAccountId(accountId: string) { |
| 89 | + try { |
| 90 | + const userDetails = getUserDetails(env, this.props.user.id) |
| 91 | + await userDetails.setActiveAccountId(accountId) |
| 92 | + } catch (e) { |
| 93 | + this.server.recordError(e) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +const GraphQLScopes = { |
| 99 | + ...RequiredScopes, |
| 100 | + 'account:read': 'See your account info such as account details, analytics, and memberships.', |
| 101 | + 'zone:read': 'See zone data such as settings, analytics, and DNS records.', |
| 102 | +} as const |
| 103 | + |
| 104 | +export default { |
| 105 | + fetch: async (req: Request, env: Env, ctx: ExecutionContext) => { |
| 106 | + if (env.ENVIRONMENT === 'development' && env.DEV_DISABLE_OAUTH === 'true') { |
| 107 | + return await handleDevMode(GraphQLMCP, req, env, ctx) |
| 108 | + } |
| 109 | + |
| 110 | + return new OAuthProvider({ |
| 111 | + apiHandlers: { |
| 112 | + '/mcp': GraphQLMCP.serve('/mcp'), |
| 113 | + '/sse': GraphQLMCP.serveSSE('/sse'), |
| 114 | + }, |
| 115 | + // @ts-ignore |
| 116 | + defaultHandler: createAuthHandlers({ scopes: GraphQLScopes, metrics }), |
| 117 | + authorizeEndpoint: '/oauth/authorize', |
| 118 | + tokenEndpoint: '/token', |
| 119 | + tokenExchangeCallback: (options) => |
| 120 | + handleTokenExchangeCallback( |
| 121 | + options, |
| 122 | + env.CLOUDFLARE_CLIENT_ID, |
| 123 | + env.CLOUDFLARE_CLIENT_SECRET |
| 124 | + ), |
| 125 | + // Cloudflare access token TTL |
| 126 | + accessTokenTTL: 3600, |
| 127 | + clientRegistrationEndpoint: '/register', |
| 128 | + }).fetch(req, env, ctx) |
| 129 | + }, |
| 130 | +} |
0 commit comments