@@ -119,26 +119,41 @@ async def _run_server(self, server_params: StdioServerParameters) -> None:
119119 try :
120120 while True :
121121 # Get message from local server
122- session_message = await self ._read_stream .receive ()
123- message = session_message .message
124- # For responses, determine which request_id to use
125- if self ._is_response (message ):
126- message_id = self ._get_message_id (message )
127- if message_id and message_id in self ._active_requests :
128- # Use the stored request_id for this response
129- request_id = self ._active_requests [message_id ]
130- # Send with the matched request_id
131- await self ._send_message (message , request_id )
132- # Clean up the mapping after use
133- del self ._active_requests [message_id ]
122+ try :
123+ session_message = await self ._read_stream .receive ()
124+ message = session_message .message
125+ # For responses, determine which request_id to use
126+ if self ._is_response (message ):
127+ message_id = self ._get_message_id (message )
128+ if message_id and message_id in self ._active_requests :
129+ # Use the stored request_id for this response
130+ request_id = self ._active_requests [message_id ]
131+ # Send with the matched request_id
132+ await self ._send_message (message , request_id )
133+ # Clean up the mapping after use
134+ del self ._active_requests [message_id ]
135+ else :
136+ # If no mapping found, use the last known request_id
137+ await self ._send_message (
138+ message , self ._last_request_id
139+ )
134140 else :
135- # If no mapping found, use the last known request_id
136- await self ._send_message (
137- message , self ._last_request_id
138- )
139- else :
140- # For non-responses, use the last known request_id
141- await self ._send_message (message , self ._last_request_id )
141+ # For non-responses, use the last known request_id
142+ await self ._send_message (message , self ._last_request_id )
143+ except Exception as e :
144+ if session_message :
145+ logger .info (session_message )
146+ logger .error (
147+ f"Error processing message for session { self ._session_id } : { e } " ,
148+ exc_info = True ,
149+ )
150+ await self ._send_message (
151+ JSONRPCError (
152+ code = - 32000 ,
153+ message = f"Error processing message: { session_message } { e } " ,
154+ ),
155+ self ._last_request_id ,
156+ )
142157 finally :
143158 # Cancel the consumer when we exit the loop
144159 consumer_task .cancel ()
@@ -158,6 +173,7 @@ async def _run_server(self, server_params: StdioServerParameters) -> None:
158173 self ._server_stderr_output = stderr_temp .read ().decode (
159174 "utf-8" , errors = "replace"
160175 )
176+ logger .error (self ._server_stderr_output )
161177 # The context managers will handle cleanup of resources
162178
163179 def _run_server_callback (self , task ):
0 commit comments