Skip to content

Commit e38eb33

Browse files
committed
Misc updates
1 parent 0478d9b commit e38eb33

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const fetchDexTestAnalyzation = async ({
1414
timeEnd: string
1515
}) => {
1616
return await fetchCloudflareApi({
17-
endpoint: `/dex/test-results/by-quartile?from=${timeStart}&to=${timeEnd}&limit=5&testId=${dexTestId}`,
17+
endpoint: `/dex/test-results/by-quartile?from=${timeStart}&to=${timeEnd}&limit=4&testId=${dexTestId}`,
1818
accountId,
1919
apiToken: accessToken,
2020
options: {

apps/dex-analysis/src/index.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import OAuthProvider from '@cloudflare/workers-oauth-provider'
2-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
32
import { McpAgent } from 'agents/mcp'
43
import { env } from 'cloudflare:workers'
54

65
import {
76
createAuthHandlers,
87
handleTokenExchangeCallback,
98
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
9+
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
1010
import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
1111

12+
import { MetricsTracker } from '../../../packages/mcp-observability/src'
1213
import { registerDEXTools } from './tools/dex'
1314

1415
import type { AccountSchema, UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler'
1516

17+
const metrics = new MetricsTracker(env.MCP_METRICS, {
18+
name: env.MCP_SERVER_NAME,
19+
version: env.MCP_SERVER_VERSION,
20+
})
21+
1622
// Context from the auth process, encrypted & stored in the auth token
1723
// and provided to the DurableMCP as this.props
1824
export type Props = {
@@ -24,22 +30,41 @@ export type Props = {
2430
export type State = { activeAccountId: string | null }
2531

2632
export class CloudflareDEXMCP extends McpAgent<Env, State, Props> {
27-
server = new McpServer({
28-
name: 'Remote MCP Server with Cloudflare DEX Analysis',
29-
version: '1.0.0',
30-
})
33+
_server: CloudflareMCPServer | undefined
34+
set server(server: CloudflareMCPServer) {
35+
this._server = server
36+
}
3137

32-
initialState: State = {
33-
activeAccountId: null,
38+
get server(): CloudflareMCPServer {
39+
if (!this._server) {
40+
throw new Error('Tried to access server before it was initialized')
41+
}
42+
43+
return this._server
44+
}
45+
46+
constructor(ctx: DurableObjectState, env: Env) {
47+
super(ctx, env)
3448
}
3549

3650
async init() {
37-
registerAccountTools(this)
51+
this.server = new CloudflareMCPServer({
52+
userId: this.props.user.id,
53+
wae: this.env.MCP_METRICS,
54+
serverInfo: {
55+
name: this.env.MCP_SERVER_NAME,
56+
version: this.env.MCP_SERVER_VERSION,
57+
},
58+
})
3859

39-
// EXAMPLE TOOLS — register your own here
60+
registerAccountTools(this)
4061
registerDEXTools(this)
4162
}
4263

64+
initialState: State = {
65+
activeAccountId: null,
66+
}
67+
4368
getActiveAccountId() {
4469
// TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch
4570
try {
@@ -74,7 +99,7 @@ export default new OAuthProvider({
7499
// @ts-ignore
75100
apiHandler: CloudflareDEXMCP.mount('/sse'),
76101
// @ts-ignore
77-
defaultHandler: createAuthHandlers({ scopes: DexScopes }),
102+
defaultHandler: createAuthHandlers({ scopes: DexScopes, metrics }),
78103
authorizeEndpoint: '/oauth/authorize',
79104
tokenEndpoint: '/token',
80105
tokenExchangeCallback: (options) =>

apps/dex-analysis/wrangler.jsonc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
},
3939
"workers_dev": false,
4040
"preview_urls": false,
41+
"analytics_engine_datasets": [
42+
{
43+
"binding": "MCP_METRICS",
44+
"dataset": "mcp-metrics-dev"
45+
}
46+
],
4147
"env": {
4248
"staging": {
4349
"name": "mcp-cloudflare-workers-dex-staging",
@@ -59,7 +65,13 @@
5965
],
6066
"vars": {
6167
"ENVIRONMENT": "staging"
62-
}
68+
},
69+
"analytics_engine_datasets": [
70+
{
71+
"binding": "MCP_METRICS",
72+
"dataset": "mcp-metrics-staging"
73+
}
74+
],
6375
},
6476
"production": {
6577
"name": "mcp-cloudflare-workers-dex-production",
@@ -81,7 +93,13 @@
8193
],
8294
"vars": {
8395
"ENVIRONMENT": "production"
84-
}
96+
},
97+
"analytics_engine_datasets": [
98+
{
99+
"binding": "MCP_METRICS",
100+
"dataset": "mcp-metrics-production"
101+
}
102+
],
85103
}
86104
}
87105
}

0 commit comments

Comments
 (0)