Skip to content

Commit b97cd26

Browse files
committed
Minor Updates
1 parent 223255e commit b97cd26

File tree

8 files changed

+330
-573
lines changed

8 files changed

+330
-573
lines changed

apps/dex-analysis/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"deploy": "wrangler deploy",
99
"dev": "wrangler dev",
1010
"start": "wrangler dev",
11+
"types": "wrangler types --include-env=false",
1112
"cf-typegen": "wrangler types",
1213
"test": "vitest run"
1314
},
@@ -16,6 +17,7 @@
1617
"@hono/zod-validator": "0.4.3",
1718
"@modelcontextprotocol/sdk": "1.10.2",
1819
"@repo/mcp-common": "workspace:*",
20+
"@repo/mcp-observability": "workspace:*",
1921
"agents": "0.0.67",
2022
"cloudflare": "4.2.0",
2123
"hono": "4.7.6",

apps/dex-analysis/src/context.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { UserDetails } from '@repo/mcp-common/src/durable-objects/user_details'
2+
import type { CloudflareDEXMCP } from './index'
3+
4+
export interface Env {
5+
OAUTH_KV: KVNamespace
6+
ENVIRONMENT: 'development' | 'staging' | 'production'
7+
MCP_SERVER_NAME: string
8+
MCP_SERVER_VERSION: string
9+
CLOUDFLARE_CLIENT_ID: string
10+
CLOUDFLARE_CLIENT_SECRET: string
11+
MCP_OBJECT: DurableObjectNamespace<CloudflareDEXMCP>
12+
USER_DETAILS: DurableObjectNamespace<UserDetails>
13+
MCP_METRICS: AnalyticsEngineDataset
14+
}

apps/dex-analysis/src/index.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import OAuthProvider from '@cloudflare/workers-oauth-provider'
22
import { McpAgent } from 'agents/mcp'
3-
import { env } from 'cloudflare:workers'
43

54
import {
65
createAuthHandlers,
76
handleTokenExchangeCallback,
87
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
8+
import { getUserDetails, UserDetails } from '@repo/mcp-common/src/durable-objects/user_details'
9+
import { getEnv } from '@repo/mcp-common/src/env'
910
import { RequiredScopes } from '@repo/mcp-common/src/scopes'
1011
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
1112
import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
@@ -14,6 +15,11 @@ import { MetricsTracker } from '@repo/mcp-observability'
1415
import { registerDEXTools } from './tools/dex'
1516

1617
import type { AccountSchema, UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler'
18+
import type { Env } from './context'
19+
20+
export { UserDetails }
21+
22+
const env = getEnv<Env>()
1723

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

65-
initialState: State = {
66-
activeAccountId: null,
67-
}
68-
69-
getActiveAccountId() {
70-
// TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch
71+
async getActiveAccountId() {
7172
try {
72-
return this.state.activeAccountId ?? null
73+
// Get UserDetails Durable Object based off the userId and retrieve the activeAccountId from it
74+
// we do this so we can persist activeAccountId across sessions
75+
const userDetails = getUserDetails(env, this.props.user.id)
76+
return await userDetails.getActiveAccountId()
7377
} catch (e) {
78+
this.server.recordError(e)
7479
return null
7580
}
7681
}
7782

78-
setActiveAccountId(accountId: string) {
79-
// TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch
83+
async setActiveAccountId(accountId: string) {
8084
try {
81-
this.setState({
82-
...this.state,
83-
activeAccountId: accountId,
84-
})
85+
const userDetails = getUserDetails(env, this.props.user.id)
86+
await userDetails.setActiveAccountId(accountId)
8587
} catch (e) {
86-
return null
88+
this.server.recordError(e)
8789
}
8890
}
8991
}
9092

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

9799
export default new OAuthProvider({

apps/dex-analysis/src/tools/dex.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function registerDEXTools(agent: CloudflareDEXMCP) {
2828
timeEnd: dexTestTimeEnd,
2929
},
3030
async (params) => {
31-
const accountId = agent.getActiveAccountId()
31+
const accountId = await agent.getActiveAccountId()
3232
if (!accountId) {
3333
return {
3434
content: [
@@ -82,7 +82,7 @@ export function registerDEXTools(agent: CloudflareDEXMCP) {
8282
'Retrieve a list of all Cloudflare DEX Tests configured.',
8383
{},
8484
async () => {
85-
const accountId = agent.getActiveAccountId()
85+
const accountId = await agent.getActiveAccountId()
8686
if (!accountId) {
8787
return {
8888
content: [

apps/dex-analysis/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"extends": "@repo/typescript-config/workers.json"
3-
}
2+
"extends": "@repo/typescript-config/workers.json",
3+
"include": ["*/**.ts"]
4+
}

apps/dex-analysis/worker-configuration.d.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
// Generated by Wrangler by running `wrangler types` (hash: 085ce38c6421603ed1a73fa9678ae0a7)
21
// Runtime types generated with [email protected] 2025-03-10 nodejs_compat
3-
declare namespace Cloudflare {
4-
interface Env {
5-
OAUTH_KV: KVNamespace;
6-
CLOUDFLARE_CLIENT_ID: string;
7-
CLOUDFLARE_CLIENT_SECRET: string;
8-
ENVIRONMENT: string;
9-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
10-
MCP_OBJECT: DurableObjectNamespace<import("./src/index").CloudflareDEXMCP>;
11-
}
12-
}
13-
interface Env extends Cloudflare.Env {}
14-
152
// Begin runtime types
163
/*! *****************************************************************************
174
Copyright (c) Cloudflare. All rights reserved.

apps/dex-analysis/wrangler.jsonc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
{
2222
"class_name": "CloudflareDEXMCP",
2323
"name": "MCP_OBJECT"
24+
},
25+
{
26+
"class_name": "UserDetails",
27+
"name": "USER_DETAILS"
2428
}
2529
]
2630
},
@@ -54,6 +58,11 @@
5458
{
5559
"class_name": "CloudflareDEXMCP",
5660
"name": "MCP_OBJECT"
61+
},
62+
{
63+
"class_name": "UserDetails",
64+
"name": "USER_DETAILS",
65+
"script_name": "mcp-cloudflare-workers-observability-staging"
5766
}
5867
]
5968
},
@@ -82,6 +91,11 @@
8291
{
8392
"class_name": "CloudflareDEXMCP",
8493
"name": "MCP_OBJECT"
94+
},
95+
{
96+
"class_name": "UserDetails",
97+
"name": "USER_DETAILS",
98+
"script_name": "mcp-cloudflare-workers-observability-production"
8599
}
86100
]
87101
},

0 commit comments

Comments
 (0)