@@ -91,7 +91,7 @@ async def main():
91
91
from mcp .shared .exceptions import McpError
92
92
from mcp .shared .message import ServerMessageMetadata , SessionMessage
93
93
from mcp .shared .session import RequestResponder
94
- from mcp .types import RequestId
94
+ from mcp .types import Operation , RequestId
95
95
96
96
logger = logging .getLogger (__name__ )
97
97
@@ -478,6 +478,10 @@ async def handler(req: types.CallToolRequest):
478
478
)
479
479
logger .debug (f"Created async operation with token: { operation .token } " )
480
480
481
+ ctx = self .request_context
482
+ ctx .operation_token = operation .token
483
+ request_ctx .set (ctx )
484
+
481
485
# Start async execution in background
482
486
async def execute_async ():
483
487
try :
@@ -560,6 +564,9 @@ def _process_tool_result(
560
564
content = list (unstructured_content ),
561
565
structuredContent = maybe_structured_content ,
562
566
isError = False ,
567
+ _operation = Operation (token = self .request_context .operation_token )
568
+ if self .request_context and self .request_context .operation_token
569
+ else None ,
563
570
)
564
571
565
572
def _should_execute_async (self , tool : types .Tool ) -> bool :
@@ -720,9 +727,7 @@ def send_request_for_operation(self, token: str, request: types.ServerRequest) -
720
727
# Add operation token to request
721
728
if hasattr (request .root , "params" ) and request .root .params is not None :
722
729
if not hasattr (request .root .params , "operation" ) or request .root .params .operation is None :
723
- # Create operation field if it doesn't exist
724
- operation_data = types .RequestParams .Operation (token = token )
725
- request .root .params .operation = operation_data
730
+ request .root .params .operation = Operation (token = token )
726
731
logger .debug (f"Marked operation { token } as input_required and added to request" )
727
732
728
733
def send_notification_for_operation (self , token : str , notification : types .ServerNotification ) -> None :
@@ -732,9 +737,7 @@ def send_notification_for_operation(self, token: str, notification: types.Server
732
737
# Add operation token to notification
733
738
if hasattr (notification .root , "params" ) and notification .root .params is not None :
734
739
if not hasattr (notification .root .params , "operation" ) or notification .root .params .operation is None :
735
- # Create operation field if it doesn't exist
736
- operation_data = types .NotificationParams .Operation (token = token )
737
- notification .root .params .operation = operation_data
740
+ notification .root .params .operation = Operation (token = token )
738
741
logger .debug (f"Marked operation { token } as input_required and added to notification" )
739
742
740
743
def complete_request_for_operation (self , token : str ) -> None :
@@ -833,25 +836,16 @@ async def _handle_request(
833
836
# app.get_request_context()
834
837
context_token = request_ctx .set (
835
838
RequestContext (
836
- message .request_id ,
837
- message .request_meta ,
838
- session ,
839
- lifespan_context ,
839
+ request_id = message .request_id ,
840
+ operation_token = message .operation .token if message .operation else None ,
841
+ meta = message .request_meta ,
842
+ session = session ,
843
+ lifespan_context = lifespan_context ,
840
844
request = request_data ,
841
845
)
842
846
)
843
847
response = await handler (req )
844
848
845
- # Handle operation token in response (for input_required operations)
846
- if (
847
- hasattr (req , "params" )
848
- and req .params is not None
849
- and hasattr (req .params , "operation" )
850
- and req .params .operation is not None
851
- ):
852
- operation_token = req .params .operation .token
853
- self .complete_request_for_operation (operation_token )
854
-
855
849
# Track async operations for cancellation
856
850
if isinstance (req , types .CallToolRequest ):
857
851
result = response .root
0 commit comments