Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions packages/worker/src/mcp/observability.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { expect, test, vi } from 'vitest'

const sentryMock = vi.hoisted(() => ({
isInitialized: vi.fn(() => true),
getClient: vi.fn(() => ({ getOptions: () => ({ dsn: 'https://example' }) })),
withScope: vi.fn((callback: (scope: ScopeStub) => void) => {
callback(sentryMock.scope)
}),
captureException: vi.fn(),
captureMessage: vi.fn(),
scope: {
setLevel: vi.fn(),
setTag: vi.fn(),
setContext: vi.fn(),
setUser: vi.fn(),
},
}))

type ScopeStub = typeof sentryMock.scope

vi.mock('@sentry/cloudflare', () => ({
isInitialized: (...args: Array<unknown>) => sentryMock.isInitialized(...args),
getClient: (...args: Array<unknown>) => sentryMock.getClient(...args),
withScope: (...args: Array<unknown>) => sentryMock.withScope(...args),
captureException: (...args: Array<unknown>) =>
sentryMock.captureException(...args),
captureMessage: (...args: Array<unknown>) =>
sentryMock.captureMessage(...args),
}))

const { logMcpEvent } = await import('./observability.ts')

test('logMcpEvent keeps sandbox failures on mcp-event logs and skips Sentry', () => {
sentryMock.captureException.mockClear()
sentryMock.captureMessage.mockClear()
sentryMock.withScope.mockClear()
sentryMock.scope.setLevel.mockClear()

const originalInfo = console.info
const payloads: Array<string> = []
console.info = ((tag: unknown, json?: unknown) => {
if (tag === 'mcp-event' && typeof json === 'string') {
payloads.push(json)
}
}) as typeof console.info
try {
logMcpEvent({
category: 'mcp',
tool: 'execute',
toolName: 'execute',
outcome: 'failure',
durationMs: 12,
baseUrl: 'https://example.com',
hasUser: true,
userId: 'user-1',
sandboxError: true,
errorName: 'Unknown',
errorMessage:
'Notion API /data_sources/39977ef0-f2db-81c6-9147-000bd579e312/query failed: validation_error',
cause: 'Notion API /data_sources/.../query failed: validation_error',
})

logMcpEvent({
category: 'mcp',
tool: 'capability',
capabilityName: 'value_get',
capabilitySource: 'builtin',
outcome: 'failure',
durationMs: 3,
baseUrl: 'https://example.com',
hasUser: true,
userId: 'user-1',
sandboxError: false,
failurePhase: 'handler',
errorName: 'Error',
errorMessage: 'platform handler blew up',
cause: new Error('platform handler blew up'),
})
} finally {
console.info = originalInfo
}

expect(payloads).toHaveLength(2)
expect(JSON.parse(payloads[0]!)).toMatchObject({
tool: 'execute',
outcome: 'failure',
sandboxError: true,
})
expect(JSON.parse(payloads[1]!)).toMatchObject({
tool: 'capability',
outcome: 'failure',
sandboxError: false,
})

expect(sentryMock.captureMessage).not.toHaveBeenCalled()
expect(sentryMock.captureException).toHaveBeenCalledTimes(1)
expect(sentryMock.captureException).toHaveBeenCalledWith(
expect.objectContaining({ message: 'platform handler blew up' }),
)
expect(sentryMock.scope.setLevel).toHaveBeenCalledWith('error')
expect(sentryMock.scope.setUser).toHaveBeenCalledWith({ id: 'user-1' })
})
9 changes: 6 additions & 3 deletions packages/worker/src/mcp/observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ function reportMcpFailureToSentry(
cause?: unknown,
) {
try {
// Sandbox failures are caller/user-module errors (bad Notion filters,
// syntax errors, thrown strings). They stay on the structured
// `mcp-event` log line; sending them to Sentry creates warning issues
// that look like platform bugs and trip triage automation.
if (payload.sandboxError) return
if (!Sentry.isInitialized()) return
const client = Sentry.getClient()
if (!client?.getOptions().dsn) return

Sentry.withScope((scope) => {
const level = payload.sandboxError ? 'warning' : 'error'
scope.setLevel(level)
scope.setLevel('error')

// Id only: sendDefaultPii is false and emails stay out of Sentry.
if (payload.userId) scope.setUser({ id: payload.userId })
Expand All @@ -81,7 +85,6 @@ function reportMcpFailureToSentry(
if (payload.failurePhase) {
scope.setTag('mcp.failure_phase', payload.failurePhase)
}
if (payload.sandboxError) scope.setTag('mcp.sandbox_error', 'true')
scope.setContext('mcp', {
baseUrl: payload.baseUrl,
hasUser: payload.hasUser,
Expand Down
Loading