1
1
import OAuthProvider from '@cloudflare/workers-oauth-provider'
2
- import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
3
2
import { McpAgent } from 'agents/mcp'
4
3
import { env } from 'cloudflare:workers'
5
4
6
5
import {
7
6
createAuthHandlers ,
8
7
handleTokenExchangeCallback ,
9
8
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
9
+ import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
10
10
import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
11
11
12
+ import { MetricsTracker } from '../../../packages/mcp-observability/src'
12
13
import { registerDEXTools } from './tools/dex'
13
14
14
15
import type { AccountSchema , UserSchema } from '@repo/mcp-common/src/cloudflare-oauth-handler'
15
16
17
+ const metrics = new MetricsTracker ( env . MCP_METRICS , {
18
+ name : env . MCP_SERVER_NAME ,
19
+ version : env . MCP_SERVER_VERSION ,
20
+ } )
21
+
16
22
// Context from the auth process, encrypted & stored in the auth token
17
23
// and provided to the DurableMCP as this.props
18
24
export type Props = {
@@ -24,22 +30,41 @@ export type Props = {
24
30
export type State = { activeAccountId : string | null }
25
31
26
32
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
+ }
31
37
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 )
34
48
}
35
49
36
50
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
+ } )
38
59
39
- // EXAMPLE TOOLS — register your own here
60
+ registerAccountTools ( this )
40
61
registerDEXTools ( this )
41
62
}
42
63
64
+ initialState : State = {
65
+ activeAccountId : null ,
66
+ }
67
+
43
68
getActiveAccountId ( ) {
44
69
// TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch
45
70
try {
@@ -74,7 +99,7 @@ export default new OAuthProvider({
74
99
// @ts -ignore
75
100
apiHandler : CloudflareDEXMCP . mount ( '/sse' ) ,
76
101
// @ts -ignore
77
- defaultHandler : createAuthHandlers ( { scopes : DexScopes } ) ,
102
+ defaultHandler : createAuthHandlers ( { scopes : DexScopes , metrics } ) ,
78
103
authorizeEndpoint : '/oauth/authorize' ,
79
104
tokenEndpoint : '/token' ,
80
105
tokenExchangeCallback : ( options ) =>
0 commit comments