@@ -567,15 +567,18 @@ async def _handler_wrapper(
567567 # Start span and execute
568568 with isolation_scope_context :
569569 with current_scope_context :
570- span_mgr : "Union[Span, StreamedSpan]"
570+ span_mgr : "Union[Span, StreamedSpan, ContextManager[None] ]"
571571 if span_streaming :
572- span_mgr = sentry_sdk .traces .start_span (
573- name = span_name ,
574- attributes = {
575- "sentry.op" : OP .MCP_SERVER ,
576- "sentry.origin" : MCPIntegration .origin ,
577- },
578- )
572+ if sentry_sdk .traces .get_current_span () is not None :
573+ span_mgr = sentry_sdk .traces .start_span (
574+ name = span_name ,
575+ attributes = {
576+ "sentry.op" : OP .MCP_SERVER ,
577+ "sentry.origin" : MCPIntegration .origin ,
578+ },
579+ )
580+ else :
581+ span_mgr = nullcontext ()
579582 else :
580583 span_mgr = get_start_span_function ()(
581584 op = OP .MCP_SERVER ,
@@ -584,40 +587,41 @@ async def _handler_wrapper(
584587 )
585588
586589 with span_mgr as span :
587- # Set input span data
588- _set_span_input_data (
589- span ,
590- handler_name ,
591- span_data_key ,
592- mcp_method_name ,
593- arguments ,
594- request_id ,
595- session_id ,
596- mcp_transport ,
597- )
590+ if span is not None :
591+ # Set input span data
592+ _set_span_input_data (
593+ span ,
594+ handler_name ,
595+ span_data_key ,
596+ mcp_method_name ,
597+ arguments ,
598+ request_id ,
599+ session_id ,
600+ mcp_transport ,
601+ )
598602
599- # For resources, extract and set protocol
600- if handler_type == "resource" :
601- uri = None
602- if params is not None :
603- uri = getattr (params , "uri" , None )
604-
605- # v1 scenario
606- if ServerRequestContext is None :
607- if original_args :
608- uri = original_args [0 ]
609- else :
610- uri = original_kwargs .get ("uri" )
611-
612- protocol = None
613- if uri is not None and hasattr (uri , "scheme" ):
614- protocol = uri .scheme
615- elif handler_name and "://" in handler_name :
616- protocol = handler_name .split ("://" )[0 ]
617- if protocol :
618- _set_span_data_attribute (
619- span , SPANDATA .MCP_RESOURCE_PROTOCOL , protocol
620- )
603+ # For resources, extract and set protocol
604+ if handler_type == "resource" :
605+ uri = None
606+ if params is not None :
607+ uri = getattr (params , "uri" , None )
608+
609+ # v1 scenario
610+ if ServerRequestContext is None :
611+ if original_args :
612+ uri = original_args [0 ]
613+ else :
614+ uri = original_kwargs .get ("uri" )
615+
616+ protocol = None
617+ if uri is not None and hasattr (uri , "scheme" ):
618+ protocol = uri .scheme
619+ elif handler_name and "://" in handler_name :
620+ protocol = handler_name .split ("://" )[0 ]
621+ if protocol :
622+ _set_span_data_attribute (
623+ span , SPANDATA .MCP_RESOURCE_PROTOCOL , protocol
624+ )
621625
622626 try :
623627 # Execute the async handler
@@ -630,14 +634,15 @@ async def _handler_wrapper(
630634
631635 except Exception as e :
632636 # Set error flag for tools
633- if handler_type == "tool" :
637+ if span is not None and handler_type == "tool" :
634638 _set_span_data_attribute (
635639 span , SPANDATA .MCP_TOOL_RESULT_IS_ERROR , True
636640 )
637641 sentry_sdk .capture_exception (e )
638642 raise
639643
640- _set_span_output_data (span , result , result_data_key , handler_type )
644+ if span is not None :
645+ _set_span_output_data (span , result , result_data_key , handler_type )
641646
642647 return result
643648
0 commit comments