Skip to content

Commit 565b510

Browse files
simplify legacy handlers again
1 parent 9ea3587 commit 565b510

1 file changed

Lines changed: 19 additions & 75 deletions

File tree

sentry_sdk/integrations/mcp.py

Lines changed: 19 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -377,33 +377,17 @@ async def _tool_handler_wrapper(
377377
if original_kwargs is None:
378378
original_kwargs = {}
379379

380-
# Detect v1 vs v2: MCP SDK v2 passes (ServerRequestContext, params) to handlers
381-
ctx: "Optional[Any]" = None
382-
params: "Optional[Any]" = None
383-
if (
384-
ServerRequestContext is not None
385-
and original_args
386-
and isinstance(original_args[0], ServerRequestContext)
387-
):
388-
ctx = original_args[0]
389-
if len(original_args) > 1:
390-
params = original_args[1]
391-
handler_name, arguments = _extract_handler_data_from_params("tool", params)
392-
else:
393-
handler_name = "unknown"
394-
arguments = {}
395-
else:
396-
handler_name, arguments = _extract_handler_data_from_args(
397-
"tool", original_args, original_kwargs
398-
)
380+
handler_name, arguments = _extract_handler_data_from_args(
381+
"tool", original_args, original_kwargs
382+
)
399383

400384
# Get request ID, session ID, and transport from context
401-
request_id, session_id, mcp_transport = _get_request_context_data(ctx=ctx)
385+
request_id, session_id, mcp_transport = _get_request_context_data()
402386

403387
span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
404388

405389
# Start span and execute
406-
with _with_active_http_scopes(ctx=ctx):
390+
with _with_active_http_scopes():
407391
span_mgr: "Union[Span, StreamedSpan]"
408392
if span_streaming:
409393
span_mgr = sentry_sdk.traces.start_span(
@@ -590,33 +574,17 @@ async def _prompt_handler_wrapper(
590574
if original_kwargs is None:
591575
original_kwargs = {}
592576

593-
# Detect v1 vs v2: MCP SDK v2 passes (ServerRequestContext, params) to handlers
594-
ctx: "Optional[Any]" = None
595-
if (
596-
ServerRequestContext is not None
597-
and original_args
598-
and isinstance(original_args[0], ServerRequestContext)
599-
):
600-
ctx = original_args[0]
601-
if len(original_args) > 1:
602-
handler_name, arguments = _extract_handler_data_from_params(
603-
"prompt", original_args[1]
604-
)
605-
else:
606-
handler_name = "unknown"
607-
arguments = {}
608-
else:
609-
handler_name, arguments = _extract_handler_data_from_args(
610-
"prompt", original_args, original_kwargs
611-
)
577+
handler_name, arguments = _extract_handler_data_from_args(
578+
"prompt", original_args, original_kwargs
579+
)
612580

613581
# Get request ID, session ID, and transport from context
614-
request_id, session_id, mcp_transport = _get_request_context_data(ctx=ctx)
582+
request_id, session_id, mcp_transport = _get_request_context_data()
615583

616584
span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
617585

618586
# Start span and execute
619-
with _with_active_http_scopes(ctx=ctx):
587+
with _with_active_http_scopes():
620588
span_mgr: "Union[Span, StreamedSpan]"
621589
if span_streaming:
622590
span_mgr = sentry_sdk.traces.start_span(
@@ -893,35 +861,17 @@ async def _resource_handler_wrapper(
893861
if original_kwargs is None:
894862
original_kwargs = {}
895863

896-
# Detect v1 vs v2: MCP SDK v2 passes (ServerRequestContext, params) to handlers
897-
ctx: "Optional[Any]" = None
898-
params: "Optional[Any]" = None
899-
if (
900-
ServerRequestContext is not None
901-
and original_args
902-
and isinstance(original_args[0], ServerRequestContext)
903-
):
904-
ctx = original_args[0]
905-
if len(original_args) > 1:
906-
params = original_args[1]
907-
handler_name, arguments = _extract_handler_data_from_params(
908-
"resource", params
909-
)
910-
else:
911-
handler_name = "unknown"
912-
arguments = {}
913-
else:
914-
handler_name, arguments = _extract_handler_data_from_args(
915-
"resource", original_args, original_kwargs
916-
)
864+
handler_name, arguments = _extract_handler_data_from_args(
865+
"resource", original_args, original_kwargs
866+
)
917867

918868
# Get request ID, session ID, and transport from context
919-
request_id, session_id, mcp_transport = _get_request_context_data(ctx=ctx)
869+
request_id, session_id, mcp_transport = _get_request_context_data()
920870

921871
span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
922872

923873
# Start span and execute
924-
with _with_active_http_scopes(ctx=ctx):
874+
with _with_active_http_scopes():
925875
span_mgr: "Union[Span, StreamedSpan]"
926876
if span_streaming:
927877
span_mgr = sentry_sdk.traces.start_span(
@@ -951,16 +901,10 @@ async def _resource_handler_wrapper(
951901
mcp_transport,
952902
)
953903

954-
uri = None
955-
if params is not None:
956-
uri = getattr(params, "uri", None)
957-
958-
# v1 scenario
959-
if ServerRequestContext is None:
960-
if original_args:
961-
uri = original_args[0]
962-
else:
963-
uri = original_kwargs.get("uri")
904+
if original_args:
905+
uri = original_args[0]
906+
else:
907+
uri = original_kwargs.get("uri")
964908

965909
protocol = None
966910
if uri is not None and hasattr(uri, "scheme"):

0 commit comments

Comments
 (0)