Skip to content

Commit a278722

Browse files
authored
Fix: Ensure ASGI compliance in transport.py for POST message handling (#162)
- Modified mcpm.router.transport.py: - Refactored handle_post_message to correctly send a 202 Accepted response as a raw ASGI application before proceeding with internal message forwarding. It now implicitly returns None as expected. (Note: Complementary ASGI fixes for mcpm.router.router.py's handle_sse were already present in the base branch 076a6bf)
1 parent eb3feca commit a278722

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/mcpm/router/transport.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,16 @@ async def handle_post_message(self, scope: Scope, receive: Receive, send: Send):
220220
response = Response("Could not parse message", status_code=400)
221221
await response(scope, receive, send)
222222
try:
223-
await writer.send(err)
223+
await writer.send(SessionMessage(message=err))
224224
except (BrokenPipeError, ConnectionError, OSError) as pipe_err:
225225
logger.warning(f"Failed to send error due to pipe issue: {pipe_err}")
226226
return
227227

228-
logger.debug(f"Sending message to writer: {message}")
229-
response = Response("Accepted", status_code=202)
230-
await response(scope, receive, send)
228+
# Send the 202 Accepted response
229+
accepted_response = Response("Accepted", status_code=202)
230+
await accepted_response(scope, receive, send)
231231

232-
# add error handling, catch possible pipe errors
232+
# Attempt to send the message to the writer
233233
try:
234234
await writer.send(SessionMessage(message=message))
235235
except (BrokenPipeError, ConnectionError, OSError) as e:
@@ -240,7 +240,9 @@ async def handle_post_message(self, scope: Scope, receive: Receive, send: Send):
240240
logger.warning(f"Connection error when sending message to session {session_id}: {e}")
241241
self._read_stream_writers.pop(session_id, None)
242242
self._session_id_to_identifier.pop(session_id, None)
243-
return response
243+
244+
# Implicitly return None. The original 'return response' is removed.
245+
return
244246

245247
def _validate_api_key(self, scope: Scope, api_key: str | None) -> bool:
246248
# If api_key is explicitly set to None, disable API key validation

0 commit comments

Comments
 (0)