Skip to content

Commit 46958b0

Browse files
committed
fix: change tags in error in favor of mechanism
1 parent 7c0a8db commit 46958b0

File tree

1 file changed

+29
-6
lines changed
  • develop-docs/sdk/expected-features/mcp-instrumentation

1 file changed

+29
-6
lines changed

develop-docs/sdk/expected-features/mcp-instrumentation/errors.mdx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ MCP Server error instrumentation ensures that all errors occurring within the Mo
1010
- **Comprehensive context:** Errors are always associated with the active Sentry span, so you get full context (method, tool, arguments, etc.) in Sentry.
1111
- **Categorized errors:** Errors are tagged by type (e.g., `validation`, `timeout`, `tool_execution`, `resource_operation`, `prompt_execution`, `transport`, etc.) for easy filtering and analysis in Sentry.
1212
- **Handler wrapping:** All MCP server handlers (`tool`, `resource`, `prompt`) are wrapped to ensure errors are captured and correlated with the correct request span.
13-
13+
- **Span status tracking:** When errors occur, the active span status is automatically set to error for better trace correlation.
1414

1515
## Safe Error Capture
1616

@@ -19,14 +19,31 @@ The core utility is an error capture function:
1919
```ts
2020
import { getClient } from '../../currentScopes';
2121
import { captureException } from '../../exports';
22+
import { SPAN_STATUS_ERROR } from '../../tracing';
23+
import { getActiveSpan } from '../../utils/spanUtils';
24+
import type { McpErrorType } from './types';
2225

23-
export function captureError(error: Error, errorType?: string): void {
26+
export function captureError(error: Error, errorType?: McpErrorType, extraData?: Record<string, unknown>): void {
2427
try {
2528
const client = getClient();
2629
if (!client) return;
30+
31+
const activeSpan = getActiveSpan();
32+
if (activeSpan?.isRecording()) {
33+
activeSpan.setStatus({
34+
code: SPAN_STATUS_ERROR,
35+
message: 'internal_error',
36+
});
37+
}
38+
2739
captureException(error, {
28-
tags: {
29-
mcp_error_type: errorType || 'handler_execution',
40+
mechanism: {
41+
type: 'mcp_server',
42+
handled: false,
43+
data: {
44+
error_type: errorType || 'handler_execution',
45+
...extraData,
46+
},
3047
},
3148
});
3249
} catch {
@@ -36,18 +53,20 @@ export function captureError(error: Error, errorType?: string): void {
3653
```
3754

3855
- **Never throws an exception:** All error capture is wrapped in a try/catch and will never throw an exception.
39-
- **Tags errors:** [WIP] will be removed as tags are intended for users.
56+
- **Mechanism-based categorization:** Error metadata is attached using the `mechanism` object.
57+
- **Flexible context:** Additional context can be passed via `extraData` and will be included in the mechanism data.
4058

4159
## Handler Wrapping for Error Capture
4260

4361
All MCP server method handlers (`tool`, `resource`, `prompt`) are wrapped to:
4462
- Correlate handler execution with the correct Sentry span (using request/session data)
4563
- Capture both synchronous and asynchronous errors
4664
- Categorize errors by handler type and error nature
65+
- Set span status to error for failed operations
4766

4867
### Error Categorization
4968

50-
Errors are categorized and tagged according to the handler and type of error:
69+
Errors are categorized using the mechanism's data field according to the handler and type of error:
5170

5271
- **Tool handler errors:**
5372
- `validation` (e.g., protocol/validation errors)
@@ -62,6 +81,10 @@ Errors are categorized and tagged according to the handler and type of error:
6281
- **Protocol errors:**
6382
- `protocol`
6483

84+
The mechanism approach ensures that error classification information is available in the Sentry UI for debugging and analysis.
85+
86+
**Note**: this mechanism data is not indexed so it is not searchable.
87+
6588
## Span Correlation
6689

6790
All errors are captured within the context of the active Sentry span, so you can:

0 commit comments

Comments
 (0)