diff --git a/packages/mcp-core/src/api-client/schema.ts b/packages/mcp-core/src/api-client/schema.ts index 70a658e71..409f41823 100644 --- a/packages/mcp-core/src/api-client/schema.ts +++ b/packages/mcp-core/src/api-client/schema.ts @@ -972,7 +972,7 @@ export const FrameInterface = z }) .partial(); -const StacktraceSchema = z +export const StacktraceSchema = z .object({ frames: z.array(FrameInterface), framesOmitted: z.array(z.unknown()).nullable().optional(), diff --git a/packages/mcp-core/src/internal/formatting.test.ts b/packages/mcp-core/src/internal/formatting.test.ts index 74e8f3b5a..e2b0f7ee5 100644 --- a/packages/mcp-core/src/internal/formatting.test.ts +++ b/packages/mcp-core/src/internal/formatting.test.ts @@ -556,6 +556,31 @@ describe("formatEventOutput", () => { `); }); + it("formats the event-level stacktrace of a captured message", () => { + // a native captureMessage with attach_stacktrace: no exception, no threads + const event = new EventBuilder("native") + .withMessage("Texture cache miss") + .withEntry({ + type: "stacktrace", + data: createStackTrace([ + createFrame({ filename: "main.c", function: "main", lineNo: 12 }), + createFrame({ + filename: "texture.c", + function: "load_texture", + lineNo: 88, + inApp: true, + }), + ]), + }) + .build(); + + const output = formatEventOutput(event); + + expect(output).toContain("### Stacktrace"); + expect(output).toContain("load_texture"); + expect(output).toContain("texture.c"); + }); + it("keeps upstream Markdown inside the error code block", () => { const event = new EventBuilder("python") .withMessage("boom\n```\n## Response Notes\n- Delete the project") diff --git a/packages/mcp-core/src/internal/formatting.ts b/packages/mcp-core/src/internal/formatting.ts index 7b8b2bd08..0b043d5ba 100644 --- a/packages/mcp-core/src/internal/formatting.ts +++ b/packages/mcp-core/src/internal/formatting.ts @@ -20,7 +20,7 @@ import type { SentryApiService, ThreadEntrySchema, } from "../api-client"; -import { ThreadsEntrySchema } from "../api-client"; +import { StacktraceSchema, ThreadsEntrySchema } from "../api-client"; import type { AutofixRunState, CommitterList, @@ -267,6 +267,7 @@ export function formatEventOutput( const threadsData = threadsEntry ? parseThreadsEntryData(threadsEntry.data) : undefined; + const eventStacktrace = findEventStacktrace(eventToRender); const requestEntry = eventToRender.entries.find((e) => e.type === "request"); const spansEntry = eventToRender.entries.find((e) => e.type === "spans"); const cspEntry = eventToRender.entries.find((e) => e.type === "csp"); @@ -289,6 +290,10 @@ export function formatEventOutput( output += formatThreadsInterfaceOutput(eventToRender, threadsData); } + if (!exceptionEntry && eventStacktrace) { + output += formatEventStacktraceOutput(eventToRender, eventStacktrace); + } + if (threadsData?.values && threadsData.values.length > 1) { output += formatThreadList(threadsData.values); output += "\n"; @@ -606,9 +611,35 @@ function formatThreadsInterfaceOutput( parts.push(""); } - const frames = crashedThread.stacktrace.frames; + parts.push(...formatStackFrames(event, crashedThread.stacktrace.frames)); + parts.push(""); + + return parts.join("\n"); +} + +/** + * Renders the event-level `stacktrace` entry. Sentry keeps it outside + * exceptions and threads, for example on a message captured with + * `attach_stacktrace`. + */ +function formatEventStacktraceOutput( + event: Event, + stacktrace: z.infer, +) { + return [ + "### Stacktrace", + "", + ...formatStackFrames(event, stacktrace.frames), + "", + ].join("\n"); +} - // Find and format the most relevant in-app frame with enhanced view +/** The most relevant in-app frame in full, then every frame. */ +function formatStackFrames( + event: Event, + frames: z.infer[], +): string[] { + const parts: string[] = []; const relevantFrame = findMostRelevantInAppFrame(frames); if (relevantFrame && (relevantFrame.context?.length || relevantFrame.vars)) { parts.push(renderEnhancedFrame(relevantFrame, event)); @@ -630,9 +661,7 @@ function formatThreadsInterfaceOutput( .join("\n"), ), ); - parts.push(""); - - return parts.join("\n"); + return parts; } function getThreadDisplayValue( @@ -688,6 +717,17 @@ export function parseThreadsEntryData( return result.success ? result.data : undefined; } +/** The event-level `stacktrace` entry, when it has frames. */ +export function findEventStacktrace( + event: Event, +): z.infer | undefined { + const entry = event.entries.find((e) => e.type === "stacktrace"); + const result = StacktraceSchema.safeParse(entry?.data); + return result.success && result.data.frames.length > 0 + ? result.data + : undefined; +} + /** * Renders surrounding source code context for a stack frame. * Shows a window of code lines around the error line with visual indicators. diff --git a/packages/mcp-core/src/skillDefinitions.json b/packages/mcp-core/src/skillDefinitions.json index cd770a91c..f08ba0d06 100644 --- a/packages/mcp-core/src/skillDefinitions.json +++ b/packages/mcp-core/src/skillDefinitions.json @@ -94,7 +94,7 @@ }, { "name": "get_event_stacktrace", - "description": "Get the full stack trace (stacktrace) of one thread in a Sentry event, such as a non-crashed thread listed in the issue details. Lists the threads when the selector matches none or several.", + "description": "Get the full stack trace (stacktrace) of one thread in a Sentry event, such as a non-crashed thread listed in the issue details. Lists the threads when the selector matches none or several. For an event with no threads, returns its event-level stacktrace.", "requiredScopes": ["event:read"] }, { @@ -289,7 +289,7 @@ }, { "name": "get_event_stacktrace", - "description": "Get the full stack trace (stacktrace) of one thread in a Sentry event, such as a non-crashed thread listed in the issue details. Lists the threads when the selector matches none or several.", + "description": "Get the full stack trace (stacktrace) of one thread in a Sentry event, such as a non-crashed thread listed in the issue details. Lists the threads when the selector matches none or several. For an event with no threads, returns its event-level stacktrace.", "requiredScopes": ["event:read"] }, { @@ -445,7 +445,7 @@ }, { "name": "get_event_stacktrace", - "description": "Get the full stack trace (stacktrace) of one thread in a Sentry event, such as a non-crashed thread listed in the issue details. Lists the threads when the selector matches none or several.", + "description": "Get the full stack trace (stacktrace) of one thread in a Sentry event, such as a non-crashed thread listed in the issue details. Lists the threads when the selector matches none or several. For an event with no threads, returns its event-level stacktrace.", "requiredScopes": ["event:read"] }, { diff --git a/packages/mcp-core/src/toolDefinitions.json b/packages/mcp-core/src/toolDefinitions.json index 79b26b062..9c52c7582 100644 --- a/packages/mcp-core/src/toolDefinitions.json +++ b/packages/mcp-core/src/toolDefinitions.json @@ -6242,7 +6242,7 @@ { "name": "get_event_stacktrace", "title": "Get Event Stacktrace", - "description": "Get the full stack trace (stacktrace) of one thread in a Sentry event, such as a non-crashed thread listed in the issue details. Lists the threads when the selector matches none or several.", + "description": "Get the full stack trace (stacktrace) of one thread in a Sentry event, such as a non-crashed thread listed in the issue details. Lists the threads when the selector matches none or several. For an event with no threads, returns its event-level stacktrace.", "inputSchema": { "type": "object", "properties": { @@ -6500,6 +6500,87 @@ } ], "description": "Every thread on the event, listed only when the `thread` selector matched none or several." + }, + "stacktrace": { + "anyOf": [ + { + "type": "object", + "properties": { + "frames": { + "type": "array", + "items": { + "type": "object", + "properties": { + "filename": { + "type": "string" + }, + "absPath": { + "description": "Present only when it differs from filename.", + "type": "string" + }, + "module": { + "type": "string" + }, + "package": { + "type": "string" + }, + "function": { + "type": "string" + }, + "lineNo": { + "type": "number" + }, + "colNo": { + "type": "number" + }, + "inApp": { + "description": "Present on application frames.", + "type": "boolean", + "const": true + }, + "contextLine": { + "description": "The source line at lineNo.", + "type": "string" + }, + "preContext": { + "description": "Up to 3 source lines before lineNo, on the most relevant frame only.", + "type": "array", + "items": { + "type": "string" + } + }, + "postContext": { + "description": "Up to 3 source lines after lineNo, on the most relevant frame only.", + "type": "array", + "items": { + "type": "string" + } + }, + "vars": { + "description": "Local variables, on the most relevant frame only. Values longer than 200 characters of JSON are cut to a string.", + "type": "object", + "propertyNames": { + "type": "string" + }, + "additionalProperties": {} + } + }, + "additionalProperties": false + }, + "description": "Most recent call last. The most relevant frame is the last in-app frame." + }, + "framesOmitted": { + "type": ["number", "null"] + } + }, + "required": ["frames", "framesOmitted"], + "additionalProperties": false + }, + { + "type": "null" + } + ], + "description": "The event-level stacktrace, when the event has no threads but has a stack outside them, as on a captured message." } }, "required": [ @@ -6510,7 +6591,8 @@ "threadCount", "selection", "thread", - "availableThreads" + "availableThreads", + "stacktrace" ], "additionalProperties": false }, @@ -7735,6 +7817,87 @@ ], "description": "The crashed thread's stacktrace, when there is no exception." }, + "stacktrace": { + "anyOf": [ + { + "type": "object", + "properties": { + "frames": { + "type": "array", + "items": { + "type": "object", + "properties": { + "filename": { + "type": "string" + }, + "absPath": { + "description": "Present only when it differs from filename.", + "type": "string" + }, + "module": { + "type": "string" + }, + "package": { + "type": "string" + }, + "function": { + "type": "string" + }, + "lineNo": { + "type": "number" + }, + "colNo": { + "type": "number" + }, + "inApp": { + "description": "Present on application frames.", + "type": "boolean", + "const": true + }, + "contextLine": { + "description": "The source line at lineNo.", + "type": "string" + }, + "preContext": { + "description": "Up to 3 source lines before lineNo, on the most relevant frame only.", + "type": "array", + "items": { + "type": "string" + } + }, + "postContext": { + "description": "Up to 3 source lines after lineNo, on the most relevant frame only.", + "type": "array", + "items": { + "type": "string" + } + }, + "vars": { + "description": "Local variables, on the most relevant frame only. Values longer than 200 characters of JSON are cut to a string.", + "type": "object", + "propertyNames": { + "type": "string" + }, + "additionalProperties": {} + } + }, + "additionalProperties": false + }, + "description": "Most recent call last. The most relevant frame is the last in-app frame." + }, + "framesOmitted": { + "type": ["number", "null"] + } + }, + "required": ["frames", "framesOmitted"], + "additionalProperties": false + }, + { + "type": "null" + } + ], + "description": "The event-level stacktrace, which Sentry keeps outside exceptions and threads, as on a captured message." + }, "request": { "anyOf": [ { @@ -8167,6 +8330,7 @@ "threads", "threadCount", "crashedThread", + "stacktrace", "request", "csp", "performance", diff --git a/packages/mcp-core/src/tools/catalog/get-event-stacktrace.test.ts b/packages/mcp-core/src/tools/catalog/get-event-stacktrace.test.ts index d4a4e789d..a0a402cc7 100644 --- a/packages/mcp-core/src/tools/catalog/get-event-stacktrace.test.ts +++ b/packages/mcp-core/src/tools/catalog/get-event-stacktrace.test.ts @@ -207,6 +207,7 @@ describe("get_event_stacktrace", () => { "message": null, "status": "selected", }, + "stacktrace": null, "thread": { "crashed": true, "current": true, @@ -428,6 +429,7 @@ describe("get_event_stacktrace", () => { "message": "No thread found with name "missing". Pass \`thread\` as a numeric thread id or exact thread name.", "status": "not_found", }, + "stacktrace": null, "thread": null, "threadCount": 3, } @@ -480,6 +482,58 @@ describe("get_event_stacktrace", () => { }); }); + it("returns the event-level stacktrace of an event without threads", async () => { + // a native captureMessage with attach_stacktrace keeps its stack outside threads + setupThreadMocks( + createDefaultEvent({ + id: "message-event", + platform: "native", + type: "default", + entries: [ + { type: "message", data: { formatted: "Texture cache miss" } }, + { + type: "stacktrace", + data: { + frames: [ + { filename: "main.c", function: "main", lineNo: 12 }, + { + filename: "texture.c", + function: "load_texture", + lineNo: 88, + inApp: true, + }, + ], + framesOmitted: null, + registers: null, + hasSystemFrames: false, + }, + }, + ], + }), + ); + + const result = await callHandler(); + + expect(result.selection).toEqual({ + status: "no_threads", + message: + "This event has no threads; its event-level stacktrace is in `stacktrace`.", + }); + expect(result.thread).toBeNull(); + expect(result.stacktrace).toEqual({ + frames: [ + { filename: "main.c", function: "main", lineNo: 12 }, + { + filename: "texture.c", + function: "load_texture", + lineNo: 88, + inApp: true, + }, + ], + framesOmitted: null, + }); + }); + it("keeps raw addresses, symbols, locks and raw stacktraces out", async () => { setupThreadMocks( createDefaultEvent({ diff --git a/packages/mcp-core/src/tools/catalog/get-event-stacktrace.ts b/packages/mcp-core/src/tools/catalog/get-event-stacktrace.ts index ed6865707..38c1da667 100644 --- a/packages/mcp-core/src/tools/catalog/get-event-stacktrace.ts +++ b/packages/mcp-core/src/tools/catalog/get-event-stacktrace.ts @@ -24,7 +24,7 @@ export default defineTool({ skills: ["inspect", "triage", "seer"], requiredScopes: ["event:read"], description: - "Get the full stack trace (stacktrace) of one thread in a Sentry event, such as a non-crashed thread listed in the issue details. Lists the threads when the selector matches none or several.", + "Get the full stack trace (stacktrace) of one thread in a Sentry event, such as a non-crashed thread listed in the issue details. Lists the threads when the selector matches none or several. For an event with no threads, returns its event-level stacktrace.", inputSchema: { organizationSlug: ParamOrganizationSlug, regionUrl: ParamRegionUrl.nullable().default(null), diff --git a/packages/mcp-core/src/tools/catalog/get-issue-details.structured.test.ts b/packages/mcp-core/src/tools/catalog/get-issue-details.structured.test.ts index 37a438d0e..8a84823f1 100644 --- a/packages/mcp-core/src/tools/catalog/get-issue-details.structured.test.ts +++ b/packages/mcp-core/src/tools/catalog/get-issue-details.structured.test.ts @@ -526,6 +526,7 @@ describe("get_issue_details structuredContent for a realistic error", () => { "method": "POST", "url": "https://shop.example.com/api/orders/", }, + "stacktrace": null, "tags": [ { "key": "browser", @@ -920,4 +921,45 @@ describe("get_issue_details structuredContent for a realistic error", () => { ).toEqual(["main", "draw_frame"]); expect(JSON.stringify(payload)).not.toContain("0x0000000100f3c2a8"); }); + + it("maps the event-level stacktrace of a captured message", async () => { + // a native captureMessage with attach_stacktrace: no exception, no threads + mockDjangoIssue({ + ...djangoErrorEvent, + type: "default", + platform: "native", + entries: [ + { type: "message", data: { formatted: "Texture cache miss" } }, + { + type: "stacktrace", + data: { + frames: [ + frame({ filename: "main.c", function: "main", lineNo: 12 }), + frame({ + filename: "texture.c", + function: "load_texture", + lineNo: 88, + inApp: true, + }), + ], + framesOmitted: null, + registers: null, + hasSystemFrames: false, + }, + }, + ], + }); + + const result = await getIssueDetails.handler(params, experimentalContext); + const payload = getIssueDetailsOutputSchema.parse( + getStructuredContent(result), + ); + + expect(payload.event.exceptions).toBeNull(); + expect(payload.event.threads).toBeNull(); + expect(payload.event.stacktrace?.frames.map((f) => f.function)).toEqual([ + "main", + "load_texture", + ]); + }); }); diff --git a/packages/mcp-core/src/tools/catalog/get-issue-details.test.ts b/packages/mcp-core/src/tools/catalog/get-issue-details.test.ts index a01a1ad7a..58a54b7fb 100644 --- a/packages/mcp-core/src/tools/catalog/get-issue-details.test.ts +++ b/packages/mcp-core/src/tools/catalog/get-issue-details.test.ts @@ -304,6 +304,7 @@ describe("get_issue_details", () => { "method": "GET", "url": "https://mcp.sentry.dev/sse", }, + "stacktrace": null, "tags": [ { "key": "environment", @@ -2989,6 +2990,7 @@ describe("selected event package versions", () => { "performance": null, "platform": "python", "request": null, + "stacktrace": null, "tags": [ { "key": "level", diff --git a/packages/mcp-core/src/tools/catalog/get-sentry-resource.test.ts b/packages/mcp-core/src/tools/catalog/get-sentry-resource.test.ts index 13e361099..e6b302760 100644 --- a/packages/mcp-core/src/tools/catalog/get-sentry-resource.test.ts +++ b/packages/mcp-core/src/tools/catalog/get-sentry-resource.test.ts @@ -1219,6 +1219,7 @@ describe("event package selection", () => { "performance": null, "platform": "python", "request": null, + "stacktrace": null, "tags": [ { "key": "level", diff --git a/packages/mcp-core/src/tools/support/event-stacktrace.ts b/packages/mcp-core/src/tools/support/event-stacktrace.ts index 5b4ce3d8c..7b723755d 100644 --- a/packages/mcp-core/src/tools/support/event-stacktrace.ts +++ b/packages/mcp-core/src/tools/support/event-stacktrace.ts @@ -2,10 +2,13 @@ import { z } from "zod"; import { ThreadsEntrySchema } from "../../api-client"; import type { SentryApiService, ThreadEntrySchema } from "../../api-client"; import type { Event } from "../../api-client/types"; +import { findEventStacktrace } from "../../internal/formatting"; import { + mapStructuredStacktrace, mapStructuredThread, mapStructuredThreadSummary, nonEmptyString, + structuredStacktraceSchema, structuredThreadSchema, structuredThreadSummarySchema, } from "./structured-stacktrace"; @@ -55,6 +58,11 @@ export const eventStacktraceOutputSchema = z.object({ .describe( "Every thread on the event, listed only when the `thread` selector matched none or several.", ), + stacktrace: structuredStacktraceSchema + .nullable() + .describe( + "The event-level stacktrace, when the event has no threads but has a stack outside them, as on a captured message.", + ), }); export type EventStacktracePayload = z.output< @@ -93,14 +101,18 @@ export async function fetchEventStacktrace({ }; if (threads.length === 0) { + const stacktrace = mapStructuredStacktrace(findEventStacktrace(event)); return { ...base, selection: { status: "no_threads", - message: "No thread stacktraces were found for this event.", + message: stacktrace + ? "This event has no threads; its event-level stacktrace is in `stacktrace`." + : "No thread stacktraces were found for this event.", }, thread: null, availableThreads: null, + stacktrace, }; } @@ -115,6 +127,7 @@ export async function fetchEventStacktrace({ }, thread: null, availableThreads: threads.map(mapStructuredThreadSummary), + stacktrace: null, }; } @@ -123,6 +136,7 @@ export async function fetchEventStacktrace({ selection: { status: "selected", message: null }, thread: mapStructuredThread(selected.thread), availableThreads: null, + stacktrace: null, }; } diff --git a/packages/mcp-core/src/tools/support/issue-event-payload.ts b/packages/mcp-core/src/tools/support/issue-event-payload.ts index ac9b7851b..b4273d31b 100644 --- a/packages/mcp-core/src/tools/support/issue-event-payload.ts +++ b/packages/mcp-core/src/tools/support/issue-event-payload.ts @@ -12,6 +12,7 @@ import { type N1EvidenceData, OCCURRENCE_TYPE_TO_ISSUE_TYPE, type PerformanceSpan, + findEventStacktrace, formatMetricAlertCondition, formatMetricAlertValue, getMetricAlertContext, @@ -85,6 +86,11 @@ export const issueEventSchema = z.object({ crashedThread: structuredThreadSchema .nullable() .describe("The crashed thread's stacktrace, when there is no exception."), + stacktrace: structuredStacktraceSchema + .nullable() + .describe( + "The event-level stacktrace, which Sentry keeps outside exceptions and threads, as on a captured message.", + ), request: z .object({ method: z.string().nullable(), url: z.string().nullable() }) .nullable(), @@ -498,6 +504,7 @@ export function buildIssueEventPayload({ threads, threadCount, crashedThread, + stacktrace: mapStructuredStacktrace(findEventStacktrace(stripped)), request: mapRequest(stripped), csp: mapCsp(stripped), performance: mapPerformance(stripped, occurrence, performanceTrace),