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