Skip to content

Commit 8f5bf9b

Browse files
committed
Add CloudflareMCPServer wrapper to track metrics
1 parent 3dac16c commit 8f5bf9b

File tree

25 files changed

+5138
-5667
lines changed

25 files changed

+5138
-5667
lines changed

apps/docs-autorag/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"check:lint": "run-eslint-workers",
77
"check:types": "run-tsc",
8-
"deploy": "wrangler deploy",
8+
"deploy": "run-wrangler-deploy",
99
"dev": "wrangler dev",
1010
"start": "wrangler dev",
1111
"cf-typegen": "wrangler types",
@@ -16,6 +16,7 @@
1616
"@hono/zod-validator": "0.4.3",
1717
"@modelcontextprotocol/sdk": "1.9.0",
1818
"@repo/mcp-common": "workspace:*",
19+
"@repo/mcp-observability": "workspace:*",
1920
"agents": "0.0.62",
2021
"cloudflare": "4.2.0",
2122
"hono": "4.7.6",

apps/docs-autorag/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
21
import { McpAgent } from 'agents/mcp'
2+
import { env } from 'cloudflare:workers'
3+
4+
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
35

46
import { registerDocsTools } from './tools/docs'
57

@@ -9,9 +11,9 @@ export type Props = never
911
export type State = never
1012

1113
export class CloudflareDocumentationMCP extends McpAgent<Env, State, Props> {
12-
server = new McpServer({
13-
name: 'Remote MCP Server with Cloudflare Documentation',
14-
version: '1.0.0',
14+
server = new CloudflareMCPServer(undefined, env.MCP_METRICS, {
15+
name: env.MCP_SERVER_NAME,
16+
version: env.MCP_SERVER_VERSION,
1517
})
1618

1719
constructor(

apps/docs-autorag/worker-configuration.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/* eslint-disable */
2-
// Generated by Wrangler by running `wrangler types` (hash: f47f9f62d3a1ddb9823a379fdafce5ce)
3-
// Runtime types generated with workerd@1.20250417.0 2025-03-10 nodejs_compat
2+
// Generated by Wrangler by running `wrangler types` (hash: 23bfeb1ecdb06845b72c16f50728901d)
3+
// Runtime types generated with workerd@1.20250422.0 2025-03-10 nodejs_compat
44
declare namespace Cloudflare {
55
interface Env {
66
ENVIRONMENT: "development" | "staging" | "production";
77
AUTORAG_NAME: "cloudflare-docs-autorag";
8+
MCP_SERVER_NAME: "PLACEHOLDER";
9+
MCP_SERVER_VERSION: "PLACEHOLDER";
810
MCP_OBJECT: DurableObjectNamespace<import("./src/index").CloudflareDocumentationMCP>;
11+
MCP_METRICS: AnalyticsEngineDataset;
912
AI: Ai;
1013
}
1114
}

apps/docs-autorag/wrangler.jsonc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@
3030
},
3131
"vars": {
3232
"ENVIRONMENT": "development",
33-
"AUTORAG_NAME": "cloudflare-docs-autorag"
33+
"AUTORAG_NAME": "cloudflare-docs-autorag",
34+
"MCP_SERVER_NAME": "PLACEHOLDER",
35+
"MCP_SERVER_VERSION": "PLACEHOLDER"
3436
},
3537
"dev": {
3638
"port": 8976
3739
},
3840
"workers_dev": false,
3941
"preview_urls": false,
42+
"analytics_engine_datasets": [
43+
{
44+
"binding": "MCP_METRICS",
45+
"dataset": "mcp-metrics-dev"
46+
}
47+
],
4048
"env": {
4149
"staging": {
4250
"name": "mcp-cloudflare-docs-autorag-staging",

apps/sandbox-container/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@n8n/json-schema-to-zod": "^1.1.0",
2727
"@repo/eval-tools": "workspace:*",
2828
"@repo/mcp-common": "workspace:*",
29+
"@repo/mcp-observability": "workspace:*",
2930
"@types/node": "^22.13.10",
3031
"agents": "^0.0.62",
3132
"cron-schedule": "^5.0.4",

apps/sandbox-container/server/containerMcp.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
21
import { McpAgent } from 'agents/mcp'
32

43
import { OPEN_CONTAINER_PORT } from '../shared/consts'
@@ -9,23 +8,27 @@ import { BASE_INSTRUCTIONS } from './prompts'
98
import { fileToBase64, stripProtocolFromFilePath } from './utils'
109

1110
import type { FileList } from '../shared/schema'
12-
import type { Env, Props } from '.'
11+
import type { Props } from '.'
12+
import { CloudflareMCPServer } from "@repo/mcp-common/src/server"
1313

14-
export class ContainerMcpAgent extends McpAgent<Env, Props> {
15-
server = new McpServer(
16-
{
17-
name: 'Container MCP Agent',
18-
version: '1.0.0',
19-
},
20-
{ instructions: BASE_INSTRUCTIONS }
21-
)
14+
export class ContainerMcpAgent extends McpAgent<Env, {}, Props> {
15+
server: CloudflareMCPServer
2216

2317
constructor(
2418
public ctx: DurableObjectState,
2519
public env: Env
2620
) {
2721
console.log('creating container DO')
2822
super(ctx, env)
23+
this.server = new CloudflareMCPServer(
24+
this.props.user.id,
25+
this.env.MCP_METRICS,
26+
{
27+
name: this.env.MCP_SERVER_NAME,
28+
version: this.env.MCP_SERVER_VERSION,
29+
},
30+
{ instructions: BASE_INSTRUCTIONS }
31+
)
2932
}
3033

3134
async destroyContainer(): Promise<void> {

apps/sandbox-container/server/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ import type { AccountSchema, UserSchema } from '@repo/mcp-common/src/cloudflare-
1414

1515
export { ContainerManager, ContainerMcpAgent }
1616

17-
export type Env = {
18-
CONTAINER_MCP_AGENT: DurableObjectNamespace<ContainerMcpAgent>
19-
CONTAINER_MANAGER: DurableObjectNamespace<ContainerManager>
20-
ENVIRONMENT: 'dev' | 'prod' | 'test'
21-
CLOUDFLARE_CLIENT_ID: string
22-
CLOUDFLARE_CLIENT_SECRET: string
23-
}
17+
import { MetricsTracker } from "@repo/mcp-observability"
18+
19+
const metrics = new MetricsTracker(env.MCP_METRICS, {
20+
name: env.MCP_SERVER_NAME,
21+
version: env.MCP_SERVER_VERSION
22+
})
2423

2524
// Context from the auth process, encrypted & stored in the auth token
2625
// and provided to the DurableMCP as this.props
@@ -41,6 +40,7 @@ const ContainerScopes = {
4140

4241
export default {
4342
fetch: (req: Request, env: Env, ctx: ExecutionContext) => {
43+
// @ts-ignore
4444
if (env.ENVIRONMENT === 'test') {
4545
ctx.props = {}
4646
return ContainerMcpAgent.mount('/sse', { binding: 'CONTAINER_MCP_AGENT' }).fetch(
@@ -55,7 +55,7 @@ export default {
5555
// @ts-ignore
5656
apiHandler: ContainerMcpAgent.mount('/sse', { binding: 'CONTAINER_MCP_AGENT' }),
5757
// @ts-ignore
58-
defaultHandler: createAuthHandlers({ scopes: ContainerScopes }),
58+
defaultHandler: createAuthHandlers({ scopes: ContainerScopes, metrics }),
5959
authorizeEndpoint: '/oauth/authorize',
6060
tokenEndpoint: '/token',
6161
tokenExchangeCallback: (options) =>

0 commit comments

Comments
 (0)