{"record":{"id":"3d9f9b1f2ca96070","repo":"PrefectHQ/fastmcp","slug":"unexpected-asgi-message-type-message-type","errorCode":null,"errorMessage":"Unexpected ASGI message type: {message['type']}","messagePattern":"Unexpected ASGI message type: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/utilities/asgi_transport.py","lineNumber":181,"sourceCode":"                request_delivered = True\n                return {\n                    \"type\": \"http.request\",\n                    \"body\": request_body,\n                    \"more_body\": False,\n                }\n            await client_disconnected.wait()\n            return {\"type\": \"http.disconnect\"}\n\n        async def send_response(message: Message) -> None:\n            nonlocal response_status, response_headers, start_received\n            if message[\"type\"] == \"http.response.start\":\n                start_received = True\n                response_status = message[\"status\"]\n                response_headers = list(message.get(\"headers\", []))\n                response_started.set()\n                return\n            if message[\"type\"] != \"http.response.body\":\n                raise RuntimeError(f\"Unexpected ASGI message type: {message['type']}\")\n            body: bytes = message.get(\"body\", b\"\")\n            if body:\n                await chunk_writer.send(body)\n            if not message.get(\"more_body\", False):\n                await chunk_writer.aclose()\n\n        async def run_application() -> None:\n            nonlocal application_error\n            try:\n                await self._app(scope, receive_request, send_response)\n            except Exception as exc:\n                # The bridge is the application's outermost boundary: a crash must fail the\n                # originating request (or show up in the already-started response), never\n                # tear down the task group shared with every other in-flight request.\n                application_error = exc\n            finally:\n                response_started.set()\n                await chunk_writer.aclose()","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/utilities/asgi_transport.py#L163-L199","documentation":"During ASGI response streaming, the transport's send_response handler accepts only 'http.response.start' and 'http.response.body' messages. Any other ASGI message type (e.g. lifecycle or websocket messages on an HTTP response path) triggers this RuntimeError.","triggerScenarios":"The ASGI app sends a message type other than http.response.start/body while the transport is reading the HTTP response, such as 'http.disconnect' misuse or websocket-scope messages.","commonSituations":"ASGI apps with buggy custom middleware sending non-HTTP messages; using the transport against a websocket or lifespan scope; framework upgrades changing message emission.","solutions":["Inspect the ASGI app/middleware for messages emitted outside the HTTP response contract","Ensure the request scope is a plain HTTP request when using this transport","Log the offending message['type'] to identify which component misbehaves"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"# Ensure scope type is http before sending requests through the transport\nassert scope[\"type\"] == \"http\"","typeGuard":null,"tryCatchPattern":"try:\n    response = await client.send(request)\nexcept RuntimeError as e:\n    if \"Unexpected ASGI message type\" in str(e):\n        logger.error(\"ASGI app emitted non-HTTP message: %s\", e)\n        # inspect middleware / app message emission\n    else:\n        raise","preventionTips":["Audit custom ASGI middleware for message-type handling","Test apps against the standard ASGI spec (asgiref compliance)","Only use this transport for plain HTTP scopes"],"tags":["python","asgi","http","protocol"],"backgroundTag":"unexpected-protocol-message","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}