-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
test(cloudflare): Add e2e test for MCPAgent with DurableObject instrumentation #20601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .wrangler |
32 changes: 32 additions & 0 deletions
32
dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| { | ||
| "name": "cloudflare-mcp-agent", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "deploy": "wrangler deploy", | ||
| "dev": "wrangler dev --var \"E2E_TEST_DSN:$E2E_TEST_DSN\"", | ||
| "build": "wrangler deploy --dry-run", | ||
| "typecheck": "tsc --noEmit", | ||
| "cf-typegen": "wrangler types", | ||
| "test:build": "pnpm install && pnpm build", | ||
| "test:assert": "pnpm typecheck && pnpm test:dev && pnpm test:prod", | ||
| "test:prod": "TEST_ENV=production playwright test", | ||
| "test:dev": "TEST_ENV=development playwright test" | ||
| }, | ||
| "dependencies": { | ||
| "@modelcontextprotocol/sdk": "^1.29.0", | ||
| "@sentry/cloudflare": "file:../../packed/sentry-cloudflare-packed.tgz", | ||
| "agents": "0.11.9", | ||
| "zod": "^4.3.6" | ||
| }, | ||
| "devDependencies": { | ||
| "@cloudflare/workers-types": "^4.20260426.0", | ||
| "@playwright/test": "~1.56.0", | ||
| "@sentry-internal/test-utils": "link:../../../test-utils", | ||
| "typescript": "^6.0.3", | ||
| "wrangler": "^4.86.0" | ||
| }, | ||
| "volta": { | ||
| "extends": "../../package.json" | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/playwright.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
| const testEnv = process.env.TEST_ENV; | ||
|
|
||
| if (!testEnv) { | ||
| throw new Error('No test env defined'); | ||
| } | ||
|
|
||
| const APP_PORT = 38788; | ||
|
|
||
| const config = getPlaywrightConfig({ | ||
| startCommand: `pnpm dev --port ${APP_PORT}`, | ||
| port: APP_PORT, | ||
| }); | ||
|
|
||
| export default config; |
4 changes: 4 additions & 0 deletions
4
dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/src/env.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| interface Env { | ||
| E2E_TEST_DSN: string; | ||
| MCP_AGENT: DurableObjectNamespace; | ||
| } |
78 changes: 78 additions & 0 deletions
78
dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/src/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
| import { McpAgent } from 'agents/mcp'; | ||
| import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; | ||
| import * as z from 'zod'; | ||
|
|
||
| class MyMCPAgentBase extends McpAgent<Env, unknown, Record<string, unknown>> { | ||
| #mcpServer = new McpServer({ | ||
| name: 'cloudflare-mcp-agent', | ||
| version: '1.0.0', | ||
| }); | ||
|
|
||
| get server() { | ||
| return Sentry.wrapMcpServerWithSentry(this.#mcpServer); | ||
| } | ||
|
|
||
| async init(): Promise<void> { | ||
| this.#mcpServer.registerTool( | ||
| 'my-tool', | ||
| { | ||
| title: 'My Tool', | ||
| description: 'My Tool Description', | ||
| inputSchema: { | ||
| message: z.string(), | ||
| }, | ||
| }, | ||
| async ({ message }) => { | ||
| const span = Sentry.getActiveSpan(); | ||
|
|
||
| await new Promise(resolve => setTimeout(resolve, 500)); | ||
|
|
||
| if (span) { | ||
| span.setAttribute('mcp.tool.name', 'my-tool'); | ||
| span.setAttribute('mcp.tool.extra', 'from-mcpagent'); | ||
| span.setAttribute('mcp.tool.input', JSON.stringify({ message })); | ||
| } | ||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text' as const, | ||
| text: `Tool my-tool: ${message}`, | ||
| }, | ||
| ], | ||
| }; | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export const MyMCPAgent = Sentry.instrumentDurableObjectWithSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.E2E_TEST_DSN, | ||
| environment: 'qa', | ||
| tunnel: `http://localhost:3031/`, | ||
| tracesSampleRate: 1.0, | ||
| sendDefaultPii: true, | ||
| debug: true, | ||
| transportOptions: { | ||
| bufferSize: 1000, | ||
| }, | ||
| }), | ||
| MyMCPAgentBase, | ||
| ); | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.E2E_TEST_DSN, | ||
| environment: 'qa', | ||
| tunnel: `http://localhost:3031/`, | ||
| tracesSampleRate: 1.0, | ||
| sendDefaultPii: true, | ||
| debug: true, | ||
| transportOptions: { | ||
| bufferSize: 1000, | ||
| }, | ||
| }), | ||
| MyMCPAgent.serve('/mcp', { binding: 'MCP_AGENT' }), | ||
| ); |
6 changes: 6 additions & 0 deletions
6
dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/start-event-proxy.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { startEventProxyServer } from '@sentry-internal/test-utils'; | ||
|
|
||
| startEventProxyServer({ | ||
| port: 3031, | ||
| proxyServerName: 'cloudflare-mcp-agent', | ||
| }); |
96 changes: 96 additions & 0 deletions
96
dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/tests/index.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForRequest } from '@sentry-internal/test-utils'; | ||
|
|
||
| test('sends spans for MCP tool calls via MCPAgent (DurableObject)', async ({ baseURL }) => { | ||
| const mcpToolWaiter = waitForRequest('cloudflare-mcp-agent', event => { | ||
| const transaction = event.envelope[1][0][1]; | ||
| return ( | ||
| typeof transaction !== 'string' && | ||
| 'transaction' in transaction && | ||
| transaction.transaction === 'tools/call my-tool' | ||
| ); | ||
| }); | ||
|
|
||
| // Step 1: Initialize the MCP session | ||
| const initResponse = await fetch(`${baseURL}/mcp`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json, text/event-stream', | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: 0, | ||
| method: 'initialize', | ||
| params: { | ||
| protocolVersion: '2024-11-05', | ||
| capabilities: {}, | ||
| clientInfo: { | ||
| name: 'test-client', | ||
| version: '1.0.0', | ||
| }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| expect(initResponse.status).toBe(200); | ||
| const sessionId = initResponse.headers.get('Mcp-Session-Id'); | ||
| expect(sessionId).toBeTruthy(); | ||
|
|
||
| // Step 2: Send initialized notification | ||
| await fetch(`${baseURL}/mcp`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json, text/event-stream', | ||
| 'Mcp-Session-Id': sessionId!, | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| method: 'notifications/initialized', | ||
| }), | ||
| }); | ||
|
|
||
| // Step 3: Call the tool with the session ID | ||
| const response = await fetch(`${baseURL}/mcp`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json, text/event-stream', | ||
| 'Mcp-Session-Id': sessionId!, | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method: 'tools/call', | ||
| params: { | ||
| name: 'my-tool', | ||
| arguments: { | ||
| message: 'hello from MCPAgent test', | ||
| }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| expect(response.status).toBe(200); | ||
|
|
||
| const mcpData = await mcpToolWaiter; | ||
| const mcpEvent = mcpData.envelope[1][0][1]; | ||
|
|
||
| expect(mcpEvent.contexts?.trace?.trace_id).toBe(mcpData.envelope[0].trace.trace_id); | ||
| expect(mcpEvent.contexts?.trace).toEqual({ | ||
| trace_id: expect.any(String), | ||
| parent_span_id: expect.any(String), | ||
| span_id: expect.any(String), | ||
| op: 'mcp.server', | ||
| origin: 'auto.function.mcp_server', | ||
| data: expect.objectContaining({ | ||
| 'sentry.origin': 'auto.function.mcp_server', | ||
| 'sentry.op': 'mcp.server', | ||
| 'mcp.method.name': 'tools/call', | ||
| 'mcp.tool.name': 'my-tool', | ||
| 'mcp.tool.extra': 'from-mcpagent', | ||
| 'mcp.tool.input': '{"message":"hello from MCPAgent test"}', | ||
| }), | ||
| }); | ||
| }); |
21 changes: 21 additions & 0 deletions
21
dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "es2021", | ||
| "lib": ["es2021"], | ||
| "jsx": "react-jsx", | ||
| "module": "es2022", | ||
| "moduleResolution": "Bundler", | ||
| "resolveJsonModule": true, | ||
| "allowJs": true, | ||
| "checkJs": false, | ||
| "noEmit": true, | ||
| "isolatedModules": true, | ||
| "allowSyntheticDefaultImports": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| "types": ["@cloudflare/workers-types/experimental"] | ||
| }, | ||
| "exclude": ["test"], | ||
| "include": ["src/**/*.ts"] | ||
| } |
11 changes: 11 additions & 0 deletions
11
dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/vitest.config.mts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'; | ||
|
|
||
| export default defineWorkersConfig({ | ||
| test: { | ||
| poolOptions: { | ||
| workers: { | ||
| wrangler: { configPath: './wrangler.toml' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
21 changes: 21 additions & 0 deletions
21
dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/wrangler.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "$schema": "node_modules/wrangler/config-schema.json", | ||
| "name": "cloudflare-mcp-agent", | ||
| "main": "src/index.ts", | ||
| "compatibility_date": "2025-03-21", | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| "durable_objects": { | ||
| "bindings": [ | ||
| { | ||
| "name": "MCP_AGENT", | ||
| "class_name": "MyMCPAgent", | ||
| }, | ||
| ], | ||
| }, | ||
| "migrations": [ | ||
| { | ||
| "tag": "v1", | ||
| "new_sqlite_classes": ["MyMCPAgent"], | ||
| }, | ||
| ], | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.