@xmcp-dev/sentry 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -2
- package/dist/index.d.ts +8 -0
- package/dist/index.js +83 -0
- package/package.json +36 -3
package/README.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @xmcp-dev/sentry
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Trace tool calls, prompt retrieval, and resource reads through one MCP middleware. `@xmcp-dev/sentry` uses your initialized Sentry Node SDK and isolates tags between concurrent operations.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add @xmcp-dev/sentry @sentry/node
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import * as Sentry from "@sentry/node";
|
|
11
|
+
import { sentryMiddleware } from "@xmcp-dev/sentry";
|
|
12
|
+
|
|
13
|
+
Sentry.init({
|
|
14
|
+
dsn: process.env.SENTRY_DSN,
|
|
15
|
+
tracesSampleRate: 0.1,
|
|
16
|
+
defaultIntegrations: false,
|
|
17
|
+
});
|
|
18
|
+
export const mcp = sentryMiddleware({ sentry: Sentry });
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
This is manual instrumentation and works in xmcp's bundled HTTP and STDIO servers. If your application already initializes Sentry, pass that same SDK instance and retain its configuration. For automatic instrumentation of other libraries, follow [Sentry's initialization guidance](https://docs.sentry.io/platforms/javascript/guides/node/) before starting the server; do not also instrument these MCP operations with Sentry's MCP wrapper, which would produce duplicate spans.
|
|
22
|
+
|
|
23
|
+
Each execution round produces a `mcp.server` span with the MCP method and `xmcp.outcome`: success, error, cancellation, or input required. Returned `isError` results create a generic error event; thrown exceptions are captured with their original stack. Cancellation and intermediate input-required results do not create error events. Discovery bypasses instrumentation. Register Sentry first in the `mcp` array to include application middleware denials.
|
|
24
|
+
|
|
25
|
+
The middleware adds no arguments, results, request headers, user identity, or resource URIs. Original exceptions can contain sensitive messages or stack data: configure Sentry's `beforeSend` and application error handling to scrub those where needed. Other Sentry integrations and settings can collect additional data independently.
|
|
26
|
+
|
|
27
|
+
Telemetry failures preserve the original operation result or exception and never execute the handler again. Delivery uses the application's Sentry SDK buffer. On serverless hosts, arrange for `Sentry.flush()` through the platform's request-lifetime hook; on shutdown flush before exiting. The middleware does not flush the shared buffer per tool call.
|
|
28
|
+
|
|
29
|
+
See the [runnable Sentry example](https://github.com/basementstudio/xmcp/tree/main/examples/sentry-http).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type * as Sentry from "@sentry/node";
|
|
2
|
+
import type { McpMiddleware } from "xmcp";
|
|
3
|
+
export interface SentryMiddlewareOptions {
|
|
4
|
+
/** Pass the same initialized SDK namespace used by the application. */
|
|
5
|
+
sentry: Pick<typeof Sentry, "withIsolationScope" | "startSpan" | "captureException" | "captureMessage">;
|
|
6
|
+
}
|
|
7
|
+
/** Trace execution without attaching tool inputs, outputs, headers or resource URIs. */
|
|
8
|
+
export declare function sentryMiddleware({ sentry, }: SentryMiddlewareOptions): McpMiddleware;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
function safely(action) {
|
|
2
|
+
try {
|
|
3
|
+
action();
|
|
4
|
+
}
|
|
5
|
+
catch {
|
|
6
|
+
/* Telemetry must not replace an operation result. */
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/** Trace execution without attaching tool inputs, outputs, headers or resource URIs. */
|
|
10
|
+
export function sentryMiddleware({ sentry, }) {
|
|
11
|
+
return async (context, next) => {
|
|
12
|
+
if (context.method !== "tools/call" &&
|
|
13
|
+
context.method !== "prompts/get" &&
|
|
14
|
+
context.method !== "resources/read")
|
|
15
|
+
return next();
|
|
16
|
+
const component = context.method === "resources/read" ? undefined : context.params.name;
|
|
17
|
+
let execution;
|
|
18
|
+
const execute = (span) => (execution ??= (async () => {
|
|
19
|
+
try {
|
|
20
|
+
const result = await next();
|
|
21
|
+
let outcome = "unknown";
|
|
22
|
+
safely(() => {
|
|
23
|
+
outcome = context.signal.aborted
|
|
24
|
+
? "cancelled"
|
|
25
|
+
: result.resultType === "input_required"
|
|
26
|
+
? "input_required"
|
|
27
|
+
: result.isError === true
|
|
28
|
+
? "error"
|
|
29
|
+
: "success";
|
|
30
|
+
});
|
|
31
|
+
safely(() => {
|
|
32
|
+
span?.setAttribute("xmcp.outcome", outcome);
|
|
33
|
+
span?.setStatus(outcome === "error"
|
|
34
|
+
? { code: 2, message: "tool_error" }
|
|
35
|
+
: outcome === "cancelled"
|
|
36
|
+
? { code: 2, message: "cancelled" }
|
|
37
|
+
: outcome === "input_required"
|
|
38
|
+
? { code: 0 }
|
|
39
|
+
: { code: 1 });
|
|
40
|
+
});
|
|
41
|
+
if (outcome === "error")
|
|
42
|
+
safely(() => {
|
|
43
|
+
sentry.captureMessage("MCP tool returned an error", "error");
|
|
44
|
+
});
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
safely(() => {
|
|
49
|
+
span?.setAttribute("xmcp.outcome", context.signal.aborted ? "cancelled" : "error");
|
|
50
|
+
span?.setStatus({
|
|
51
|
+
code: 2,
|
|
52
|
+
message: context.signal.aborted ? "cancelled" : "internal_error",
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
if (!context.signal.aborted)
|
|
56
|
+
safely(() => {
|
|
57
|
+
sentry.captureException(error);
|
|
58
|
+
});
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
})());
|
|
62
|
+
try {
|
|
63
|
+
return await sentry.withIsolationScope((scope) => {
|
|
64
|
+
scope.setTag("mcp.method", context.method);
|
|
65
|
+
if (component)
|
|
66
|
+
scope.setTag("mcp.component", component);
|
|
67
|
+
return sentry.startSpan({
|
|
68
|
+
name: component ? `${context.method} ${component}` : context.method,
|
|
69
|
+
op: "mcp.server",
|
|
70
|
+
attributes: {
|
|
71
|
+
"mcp.method.name": context.method,
|
|
72
|
+
"network.transport": context.http ? "http" : "stdio",
|
|
73
|
+
},
|
|
74
|
+
}, (span) => execute(span));
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// The SDK can fail before or after invoking its callback. Reuse the
|
|
79
|
+
// original promise so telemetry failures can never run the tool twice.
|
|
80
|
+
return execution ?? execute();
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xmcp-dev/sentry",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Sentry integration for xmcp",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@sentry/node": "^11.4.0",
|
|
21
|
+
"xmcp": "^1.6.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@sentry/node": "^11.4.0",
|
|
25
|
+
"@types/node": "^22.19.2",
|
|
26
|
+
"typescript": "^5.9.3",
|
|
27
|
+
"tsx": "^4.21.0",
|
|
28
|
+
"xmcp": "1.7.1"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/basementstudio/xmcp",
|
|
33
|
+
"directory": "packages/plugins/sentry"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "rm -rf dist && tsc",
|
|
37
|
+
"test": "node --import tsx --test test/*.test.ts"
|
|
38
|
+
}
|
|
6
39
|
}
|