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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.wrangler
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@sentry:registry=http://127.0.0.1:4873
@sentry-internal:registry=http://127.0.0.1:4873
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "cloudflare-mcp",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev --var \"E2E_TEST_DSN:$E2E_TEST_DSN\" --log-level=$(test $CI && echo 'none' || echo 'log')",
"build": "wrangler deploy --dry-run",
"test": "vitest --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.22.0",
"@sentry/cloudflare": "latest || *",
"agents": "^0.2.23",
"zod": "^3.25.76"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.8.19",
"@cloudflare/workers-types": "^4.20240725.0",
"@playwright/test": "~1.50.0",
"@sentry-internal/test-utils": "link:../../../test-utils",
"typescript": "^5.5.2",
"vitest": "~3.2.0",
"wrangler": "^4.23.0",
"ws": "^8.18.3"
},
"volta": {
"extends": "../../package.json"
},
"pnpm": {
"overrides": {
"strip-literal": "~2.0.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 = 38787;

const config = getPlaywrightConfig(
{
startCommand: `pnpm dev --port ${APP_PORT}`,
port: APP_PORT,
},
{
// This comes with the risk of tests leaking into each other but the tests run quite slow so we should parallelize
workers: '100%',
retries: 0,
},
);

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Generated by Wrangler on Mon Jul 29 2024 21:44:31 GMT-0400 (Eastern Daylight Time)
// by running `wrangler types`

interface Env {
E2E_TEST_DSN: '';
MY_DURABLE_OBJECT: DurableObjectNamespace;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `npm run dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `npm run deploy` to publish your worker
*
* Bind resources to your worker in `wrangler.toml`. After adding bindings, a type definition for the
* `Env` object can be regenerated with `npm run cf-typegen`.
*
* Learn more at https://developers.cloudflare.com/workers/
*/
import * as Sentry from '@sentry/cloudflare';
import { createMcpHandler } from 'agents/mcp';
import * as z from 'zod';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';

export default Sentry.withSentry(
(env: Env) => ({
dsn: env.E2E_TEST_DSN,
environment: 'qa', // dynamic sampling bias to keep transactions
tunnel: `http://localhost:3031/`, // proxy server
tracesSampleRate: 1.0,
sendDefaultPii: true,
debug: true,
transportOptions: {
// We are doing a lot of events at once in this test
bufferSize: 1000,
},
}),
{
async fetch(request, env, ctx) {
const server = new McpServer({
name: 'cloudflare-mcp',
version: '1.0.0',
});

const span = Sentry.getActiveSpan();

if (span) {
span.setAttribute('mcp.server.extra', ' /|\ ^._.^ /|\ ');
}

server.registerTool(
'my-tool',
{
title: 'My Tool',
description: 'My Tool Description',
inputSchema: {
message: z.string(),
},
},
async ({ message }) => {
const span = Sentry.getActiveSpan();

// simulate a long running tool
await new Promise(resolve => setTimeout(resolve, 500));

if (span) {
span.setAttribute('mcp.tool.name', 'my-tool');
span.setAttribute('mcp.tool.extra', 'ƸӜƷ');
span.setAttribute('mcp.tool.input', JSON.stringify({ message }));
}

return {
content: [
{
type: 'text' as const,
text: `Tool my-tool: ${message}`,
},
],
};
},
);

const handler = createMcpHandler(Sentry.wrapMcpServerWithSentry(server), {
route: '/mcp',
});

return handler(request, env, ctx);
},
} satisfies ExportedHandler<Env>,
);
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',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { expect, test } from '@playwright/test';
import { waitForRequest } from '@sentry-internal/test-utils';

test('sends spans for MCP tool calls', async ({ baseURL }) => {
const spanRequestWaiter = waitForRequest('cloudflare-mcp', event => {
const transaction = event.envelope[1][0][1];
return typeof transaction !== 'string' && 'transaction' in transaction && transaction.transaction === 'POST /mcp';
});

const spanMcpWaiter = waitForRequest('cloudflare-mcp', event => {
const transaction = event.envelope[1][0][1];
return (
typeof transaction !== 'string' &&
'transaction' in transaction &&
transaction.transaction === 'tools/call my-tool'
);
});

const response = await fetch(`${baseURL}/mcp`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json, text/event-stream',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'tools/call',
params: {
name: 'my-tool',
arguments: {
message: 'ʕっ•ᴥ•ʔっ',
},
},
}),
});

expect(response.status).toBe(200);

const requestData = await spanRequestWaiter;
const mcpData = await spanMcpWaiter;

const requestEvent = requestData.envelope[1][0][1];
const mcpEvent = mcpData.envelope[1][0][1];

// Check that the events have contexts
// this is for TypeScript type safety
if (
typeof mcpEvent === 'string' ||
!('contexts' in mcpEvent) ||
typeof requestEvent === 'string' ||
!('contexts' in requestEvent)
) {
throw new Error("Events don't have contexts");
}

expect(mcpEvent.contexts?.trace?.trace_id).toBe((mcpData.envelope[0].trace as any).trace_id);
expect(requestData.envelope[0].event_id).not.toBe(mcpData.envelope[0].event_id);

expect(requestEvent.contexts?.trace).toEqual({
span_id: expect.any(String),
trace_id: expect.any(String),
data: expect.objectContaining({
'sentry.origin': 'auto.http.cloudflare',
'sentry.op': 'http.server',
'sentry.source': 'url',
'sentry.sample_rate': 1,
'http.request.method': 'POST',
'url.path': '/mcp',
'url.full': 'http://localhost:38787/mcp',
'url.port': '38787',
'url.scheme': 'http:',
'server.address': 'localhost',
'http.request.body.size': 120,
'user_agent.original': 'node',
'http.request.header.content_type': 'application/json',
'network.protocol.name': 'HTTP/1.1',
'mcp.server.extra': ' /|\ ^._.^ /|\ ',
'http.response.status_code': 200,
}),
op: 'http.server',
status: 'ok',
origin: 'auto.http.cloudflare',
});

expect(mcpEvent.contexts?.trace).toEqual({
trace_id: expect.any(String),
parent_span_id: requestEvent.contexts?.trace?.span_id,
span_id: expect.any(String),
op: 'mcp.server',
origin: 'auto.function.mcp_server',
data: {
'sentry.origin': 'auto.function.mcp_server',
'sentry.op': 'mcp.server',
'sentry.source': 'route',
'mcp.transport': 'WorkerTransport',
'network.transport': 'unknown',
'network.protocol.version': '2.0',
'mcp.method.name': 'tools/call',
'mcp.request.id': '1',
'mcp.tool.name': 'my-tool',
'mcp.request.argument.message': '"ʕっ•ᴥ•ʔっ"',
'mcp.tool.extra': 'ƸӜƷ',
'mcp.tool.input': '{"message":"ʕっ•ᴥ•ʔっ"}',
'mcp.tool.result.content_count': 1,
'mcp.tool.result.content_type': 'text',
'mcp.tool.result.content': 'Tool my-tool: ʕっ•ᴥ•ʔっ',
},
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["@cloudflare/vitest-pool-workers"]
},
"include": ["./**/*.ts", "../worker-configuration.d.ts"],
"exclude": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "es2021",
/* Specify a set of bundled library declaration files that describe the target runtime environment. */
"lib": ["es2021"],
/* Specify what JSX code is generated. */
"jsx": "react-jsx",

/* Specify what module code is generated. */
"module": "es2022",
/* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "Bundler",
/* Enable importing .json files */
"resolveJsonModule": true,

/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
"allowJs": true,
/* Enable error reporting in type-checked JavaScript files. */
"checkJs": false,

/* Disable emitting files from a compilation. */
"noEmit": true,

/* Ensure that each file can be safely transpiled without relying on other imports. */
"isolatedModules": true,
/* Allow 'import x from y' when a module doesn't have a default export. */
"allowSyntheticDefaultImports": true,
/* Ensure that casing is correct in imports. */
"forceConsistentCasingInFileNames": true,

/* Enable all strict type-checking options. */
"strict": true,

/* Skip type checking all .d.ts files. */
"skipLibCheck": true,
"types": ["@cloudflare/workers-types/experimental"]
},
"exclude": ["test"],
"include": ["src/**/*.ts"]
}
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' },
},
},
},
});
Loading