Skip to content

DEX Minor Updates #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/dex-analysis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev",
"types": "wrangler types --include-env=false",
"cf-typegen": "wrangler types",
"test": "vitest run"
},
Expand All @@ -16,6 +17,7 @@
"@hono/zod-validator": "0.4.3",
"@modelcontextprotocol/sdk": "1.10.2",
"@repo/mcp-common": "workspace:*",
"@repo/mcp-observability": "workspace:*",
"agents": "0.0.67",
"cloudflare": "4.2.0",
"hono": "4.7.6",
Expand Down
14 changes: 14 additions & 0 deletions apps/dex-analysis/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { UserDetails } from '@repo/mcp-common/src/durable-objects/user_details'
import type { CloudflareDEXMCP } from './index'

export interface Env {
OAUTH_KV: KVNamespace
ENVIRONMENT: 'development' | 'staging' | 'production'
MCP_SERVER_NAME: string
MCP_SERVER_VERSION: string
CLOUDFLARE_CLIENT_ID: string
CLOUDFLARE_CLIENT_SECRET: string
MCP_OBJECT: DurableObjectNamespace<CloudflareDEXMCP>
USER_DETAILS: DurableObjectNamespace<UserDetails>
MCP_METRICS: AnalyticsEngineDataset
}
34 changes: 18 additions & 16 deletions apps/dex-analysis/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import OAuthProvider from '@cloudflare/workers-oauth-provider'
import { McpAgent } from 'agents/mcp'
import { env } from 'cloudflare:workers'

import {
createAuthHandlers,
handleTokenExchangeCallback,
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
import { getUserDetails, UserDetails } from '@repo/mcp-common/src/durable-objects/user_details'
import { getEnv } from '@repo/mcp-common/src/env'
import { RequiredScopes } from '@repo/mcp-common/src/scopes'
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
Expand All @@ -14,6 +15,11 @@ import { MetricsTracker } from '@repo/mcp-observability'
import { registerDEXTools } from './tools/dex'

import type { AccountSchema, UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler'
import type { Env } from './context'

export { UserDetails }

const env = getEnv<Env>()

const metrics = new MetricsTracker(env.MCP_METRICS, {
name: env.MCP_SERVER_NAME,
Expand Down Expand Up @@ -62,36 +68,32 @@ export class CloudflareDEXMCP extends McpAgent<Env, State, Props> {
registerDEXTools(this)
}

initialState: State = {
activeAccountId: null,
}

getActiveAccountId() {
// TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch
async getActiveAccountId() {
try {
return this.state.activeAccountId ?? null
// Get UserDetails Durable Object based off the userId and retrieve the activeAccountId from it
// we do this so we can persist activeAccountId across sessions
const userDetails = getUserDetails(env, this.props.user.id)
return await userDetails.getActiveAccountId()
} catch (e) {
this.server.recordError(e)
return null
}
}

setActiveAccountId(accountId: string) {
// TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch
async setActiveAccountId(accountId: string) {
try {
this.setState({
...this.state,
activeAccountId: accountId,
})
const userDetails = getUserDetails(env, this.props.user.id)
await userDetails.setActiveAccountId(accountId)
} catch (e) {
return null
this.server.recordError(e)
}
}
}

const DexScopes = {
...RequiredScopes,
'account:read': 'See your account info such as account details, analytics, and memberships.',
'dex:read': 'See Cloudflare Cloudflare DEX data for your account',
offline_access: 'Grants refresh tokens for long-lived access.',
} as const

export default new OAuthProvider({
Expand Down
4 changes: 2 additions & 2 deletions apps/dex-analysis/src/tools/dex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function registerDEXTools(agent: CloudflareDEXMCP) {
timeEnd: dexTestTimeEnd,
},
async (params) => {
const accountId = agent.getActiveAccountId()
const accountId = await agent.getActiveAccountId()
if (!accountId) {
return {
content: [
Expand Down Expand Up @@ -82,7 +82,7 @@ export function registerDEXTools(agent: CloudflareDEXMCP) {
'Retrieve a list of all Cloudflare DEX Tests configured.',
{},
async () => {
const accountId = agent.getActiveAccountId()
const accountId = await agent.getActiveAccountId()
if (!accountId) {
return {
content: [
Expand Down
5 changes: 3 additions & 2 deletions apps/dex-analysis/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"extends": "@repo/typescript-config/workers.json"
}
"extends": "@repo/typescript-config/workers.json",
"include": ["*/**.ts"]
}
13 changes: 0 additions & 13 deletions apps/dex-analysis/worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
// Generated by Wrangler by running `wrangler types` (hash: 085ce38c6421603ed1a73fa9678ae0a7)
// Runtime types generated with [email protected] 2025-03-10 nodejs_compat
declare namespace Cloudflare {
interface Env {
OAUTH_KV: KVNamespace;
CLOUDFLARE_CLIENT_ID: string;
CLOUDFLARE_CLIENT_SECRET: string;
ENVIRONMENT: string;
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
MCP_OBJECT: DurableObjectNamespace<import("./src/index").CloudflareDEXMCP>;
}
}
interface Env extends Cloudflare.Env {}

// Begin runtime types
/*! *****************************************************************************
Copyright (c) Cloudflare. All rights reserved.
Expand Down
14 changes: 14 additions & 0 deletions apps/dex-analysis/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
{
"class_name": "CloudflareDEXMCP",
"name": "MCP_OBJECT"
},
{
"class_name": "UserDetails",
"name": "USER_DETAILS"
}
]
},
Expand Down Expand Up @@ -54,6 +58,11 @@
{
"class_name": "CloudflareDEXMCP",
"name": "MCP_OBJECT"
},
{
"class_name": "UserDetails",
"name": "USER_DETAILS",
"script_name": "mcp-cloudflare-workers-observability-staging"
}
]
},
Expand Down Expand Up @@ -82,6 +91,11 @@
{
"class_name": "CloudflareDEXMCP",
"name": "MCP_OBJECT"
},
{
"class_name": "UserDetails",
"name": "USER_DETAILS",
"script_name": "mcp-cloudflare-workers-observability-production"
}
]
},
Expand Down
Loading
Loading