Skip to content

Commit ecd8f7f

Browse files
committed
Add sentry client to CloudflareMCPServer
1 parent 99836e5 commit ecd8f7f

File tree

17 files changed

+293
-52
lines changed

17 files changed

+293
-52
lines changed

apps/docs-autorag/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ export type Props = never
1111
export type State = never
1212

1313
export class CloudflareDocumentationMCP extends McpAgent<Env, State, Props> {
14-
server = new CloudflareMCPServer(undefined, env.MCP_METRICS, {
15-
name: env.MCP_SERVER_NAME,
16-
version: env.MCP_SERVER_VERSION,
14+
server = new CloudflareMCPServer({
15+
wae: env.MCP_METRICS,
16+
serverInfo: {
17+
name: env.MCP_SERVER_NAME,
18+
version: env.MCP_SERVER_VERSION,
19+
},
1720
})
1821

1922
constructor(

apps/sandbox-container/server/containerMcp.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ export class ContainerMcpAgent extends McpAgent<Env, {}, Props> {
5151

5252
async init() {
5353
this.props.user.id
54-
this.server = new CloudflareMCPServer(
55-
this.props.user.id,
56-
this.env.MCP_METRICS,
57-
{
54+
this.server = new CloudflareMCPServer({
55+
userId: this.props.user.id,
56+
wae: this.env.MCP_METRICS,
57+
serverInfo: {
5858
name: this.env.MCP_SERVER_NAME,
5959
version: this.env.MCP_SERVER_VERSION,
6060
},
61-
{ instructions: BASE_INSTRUCTIONS }
62-
)
61+
options: { instructions: BASE_INSTRUCTIONS },
62+
})
6363

6464
this.server.tool(
6565
'container_initialize',

apps/workers-bindings/src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ export class WorkersBindingsMCP extends McpAgent<Env, WorkersBindingsMCPState, P
5454
}
5555

5656
async init() {
57-
this.server = new CloudflareMCPServer(this.props.user.id, this.env.MCP_METRICS, {
58-
name: this.env.MCP_SERVER_NAME,
59-
version: this.env.MCP_SERVER_VERSION,
57+
this.server = new CloudflareMCPServer({
58+
userId: this.props.user.id,
59+
wae: this.env.MCP_METRICS,
60+
serverInfo: {
61+
name: this.env.MCP_SERVER_NAME,
62+
version: this.env.MCP_SERVER_VERSION,
63+
},
6064
})
6165

6266
registerAccountTools(this)

apps/workers-observability/src/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
createAuthHandlers,
77
handleTokenExchangeCallback,
88
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
9+
import { initSentry } from '@repo/mcp-common/src/sentry'
910
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
1011
import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
1112
import { registerWorkersTools } from '@repo/mcp-common/src/tools/worker'
@@ -35,7 +36,6 @@ export class ObservabilityMCP extends McpAgent<Env, State, Props> {
3536
set server(server: CloudflareMCPServer) {
3637
this._server = server
3738
}
38-
3939
get server(): CloudflareMCPServer {
4040
if (!this._server) {
4141
throw new Error('Tried to access server before it was initialized')
@@ -44,18 +44,19 @@ export class ObservabilityMCP extends McpAgent<Env, State, Props> {
4444
return this._server
4545
}
4646

47-
initialState: State = {
48-
activeAccountId: null,
49-
}
50-
5147
constructor(ctx: DurableObjectState, env: Env) {
5248
super(ctx, env)
5349
}
5450

5551
async init() {
56-
this.server = new CloudflareMCPServer(this.props.user.id, this.env.MCP_METRICS, {
57-
name: this.env.MCP_SERVER_NAME,
58-
version: this.env.MCP_SERVER_VERSION,
52+
this.server = new CloudflareMCPServer({
53+
userId: this.props.user.id,
54+
wae: this.env.MCP_METRICS,
55+
serverInfo: {
56+
name: this.env.MCP_SERVER_NAME,
57+
version: this.env.MCP_SERVER_VERSION,
58+
},
59+
sentry: initSentry(env, this.ctx, this.props.user.id),
5960
})
6061

6162
registerAccountTools(this)
@@ -72,6 +73,7 @@ export class ObservabilityMCP extends McpAgent<Env, State, Props> {
7273
try {
7374
return this.state.activeAccountId ?? null
7475
} catch (e) {
76+
this.server.recordError(e)
7577
return null
7678
}
7779
}
@@ -84,6 +86,7 @@ export class ObservabilityMCP extends McpAgent<Env, State, Props> {
8486
activeAccountId: accountId,
8587
})
8688
} catch (e) {
89+
this.server.recordError(e)
8790
return null
8891
}
8992
}

apps/workers-observability/src/tools/logs.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,14 @@ export function registerLogsTools(agent: ObservabilityMCP) {
161161
},
162162
],
163163
}
164-
} catch (error) {
164+
} catch (e) {
165+
agent.server.recordError(e)
165166
return {
166167
content: [
167168
{
168169
type: 'text',
169170
text: JSON.stringify({
170-
error: `Error analyzing worker logs: ${error instanceof Error && error.message}`,
171+
error: `Error analyzing worker logs: ${e instanceof Error && e.message}`,
171172
}),
172173
},
173174
],
@@ -225,13 +226,14 @@ export function registerLogsTools(agent: ObservabilityMCP) {
225226
},
226227
],
227228
}
228-
} catch (error) {
229+
} catch (e) {
230+
agent.server.recordError(e)
229231
return {
230232
content: [
231233
{
232234
type: 'text',
233235
text: JSON.stringify({
234-
error: `Error analyzing logs by Ray ID: ${error instanceof Error && error.message}`,
236+
error: `Error analyzing logs by Ray ID: ${e instanceof Error && e.message}`,
235237
}),
236238
},
237239
],
@@ -290,13 +292,14 @@ export function registerLogsTools(agent: ObservabilityMCP) {
290292
},
291293
],
292294
}
293-
} catch (error) {
295+
} catch (e) {
296+
agent.server.recordError(e)
294297
return {
295298
content: [
296299
{
297300
type: 'text',
298301
text: JSON.stringify({
299-
error: `Error retrieving worker telemetry keys: ${error instanceof Error && error.message}`,
302+
error: `Error retrieving worker telemetry keys: ${e instanceof Error && e.message}`,
300303
}),
301304
},
302305
],

apps/workers-observability/worker-configuration.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ declare namespace Cloudflare {
55
interface Env {
66
OAUTH_KV: KVNamespace;
77
ENVIRONMENT: "development" | "staging" | "production";
8+
GIT_HASH: "OVERRIDEN_DURING_DEPLOYMENT";
9+
SENTRY_DSN: "PLACEHOLDER";
810
MCP_SERVER_NAME: "PLACEHOLDER";
911
MCP_SERVER_VERSION: "PLACEHOLDER";
1012
CLOUDFLARE_CLIENT_ID: "PLACEHOLDER";
1113
CLOUDFLARE_CLIENT_SECRET: "PLACEHOLDER";
14+
SENTRY_ACCESS_CLIENT_ID: "PLACEHOLDER";
15+
SENTRY_ACCESS_CLIENT_SECRET: "PLACEHOLDER";
1216
MCP_OBJECT: DurableObjectNamespace<import("./src/index").ObservabilityMCP>;
1317
MCP_METRICS: AnalyticsEngineDataset;
1418
}

apps/workers-observability/wrangler.jsonc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@
6969
}
7070
],
7171
"vars": {
72-
"ENVIRONMENT": "staging"
72+
"ENVIRONMENT": "staging",
73+
"GIT_HASH": "OVERRIDEN_DURING_DEPLOYMENT",
74+
"SENTRY_DSN": "https://[email protected]/1764",
75+
"MCP_SERVER_NAME": "workers-staging-observability",
76+
"MCP_SERVER_VERSION": "1.0.0"
7377
},
7478
"analytics_engine_datasets": [
7579
{
@@ -97,7 +101,11 @@
97101
}
98102
],
99103
"vars": {
100-
"ENVIRONMENT": "production"
104+
"ENVIRONMENT": "production",
105+
"GIT_HASH": "OVERRIDEN_DURING_DEPLOYMENT",
106+
"SENTRY_DSN": "https://[email protected]/1764",
107+
"MCP_SERVER_NAME": "workers-observability",
108+
"MCP_SERVER_VERSION": "1.0.0"
101109
},
102110
"analytics_engine_datasets": [
103111
{

packages/mcp-common/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"agents": "0.0.62",
1919
"cloudflare": "4.2.0",
2020
"hono": "4.7.6",
21+
"toucan-js": "^4.1.1",
2122
"zod": "3.24.2"
2223
},
2324
"devDependencies": {
@@ -27,6 +28,7 @@
2728
"@repo/eslint-config": "workspace:*",
2829
"@repo/tools": "workspace:*",
2930
"@repo/typescript-config": "workspace:*",
31+
"@sentry/types": "8.9.2",
3032
"@vitest/ui": "3.0.9",
3133
"vitest": "3.0.9",
3234
"@types/node": "^22.14.1"

packages/mcp-common/src/cloudflare-auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export async function getAuthToken({
154154

155155
if (!resp.ok) {
156156
console.log(await resp.text())
157-
throw new McpError('Failed to get OAuth token', 500)
157+
throw new McpError('Failed to get OAuth token', 500, { reportToSentry: true })
158158
}
159159

160160
return AuthorizationToken.parse(await resp.json())
@@ -185,7 +185,7 @@ export async function refreshAuthToken({
185185
})
186186
if (!resp.ok) {
187187
console.log(await resp.text())
188-
throw new McpError('Failed to get OAuth token', 500)
188+
throw new McpError('Failed to get OAuth token', 500, { reportToSentry: true })
189189
}
190190

191191
return AuthorizationToken.parse(await resp.json())

packages/mcp-common/src/cloudflare-oauth-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ async function getTokenAndUser(
9494

9595
if (!userResponse.ok) {
9696
console.log(await userResponse.text())
97-
throw new McpError('Failed to fetch user', 500)
97+
throw new McpError('Failed to fetch user', 500, { reportToSentry: true })
9898
}
9999
if (!accountsResponse.ok) {
100100
console.log(await accountsResponse.text())
101-
throw new McpError('Failed to fetch accounts', 500)
101+
throw new McpError('Failed to fetch accounts', 500, { reportToSentry: true })
102102
}
103103

104104
// Fetch the user & accounts info from Cloudflare

0 commit comments

Comments
 (0)