-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathindex.ts
More file actions
83 lines (74 loc) · 2.36 KB
/
index.ts
File metadata and controls
83 lines (74 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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>,
);