python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

How to close the client connection correctly?

Open freedba opened this issue 8 months ago • 0 comments

INFO: 127.0.0.1:52574 - "DELETE /mcp/ HTTP/1.1" 200 OK [05/28/25 12:07:55] INFO HTTP Request: POST http://127.0.0.1:10003/mcp "HTTP/1.1 307 Temporary Redirect" _client.py:1740 INFO HTTP Request: POST http://127.0.0.1:10003/mcp "HTTP/1.1 307 Temporary Redirect" _client.py:1740 INFO HTTP Request: POST http://127.0.0.1:10003/mcp/ "HTTP/1.1 200 OK" _client.py:1740 INFO Received session ID: cfd7fba8487740b3b3e7f87a780633c7 streamable_http.py:140 ERROR Error parsing SSE message: streamable_http.py:173 ERROR Error reading SSE stream: streamable_http.py:321 ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────── Traceback (most recent call last) ───────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ D:\ProgramData\sourcecode\agent.venv\Lib\site-packages\mcp\client\streamable_http.py:162 in _handle_sse_event │
│ │
│ 159 │ │ │ │ │ message.root.id = original_request_id │
│ 160 │ │ │ │ │
│ 161 │ │ │ │ session_message = SessionMessage(message) │
│ ❱ 162 │ │ │ │ await read_stream_writer.send(session_message) │
│ 163 │ │ │ │ │
│ 164 │ │ │ │ # Call resumption token callback if we have an ID │
│ 165 │ │ │ │ if sse.id and resumption_callback: │
│ │
│ D:\ProgramData\sourcecode\agent.venv\Lib\site-packages\anyio\streams\memory.py:242 in send │
│ │
│ 239 │ │ """ │
│ 240 │ │ await checkpoint() │
│ 241 │ │ try: │
│ ❱ 242 │ │ │ self.send_nowait(item) │
│ 243 │ │ except WouldBlock: │
│ 244 │ │ │ # Wait until there's someone on the receiving end │
│ 245 │ │ │ send_event = Event() │
│ │
│ D:\ProgramData\sourcecode\agent.venv\Lib\site-packages\anyio\streams\memory.py:213 in send_nowait │
│ │
│ 210 │ │ if self._closed: │
│ 211 │ │ │ raise ClosedResourceError │
│ 212 │ │ if not self._state.open_receive_channels: │
│ ❱ 213 │ │ │ raise BrokenResourceError │
│ 214 │ │ │
│ 215 │ │ while self._state.waiting_receivers: │
│ 216 │ │ │ receive_event, receiver = self._state.waiting_receivers.popitem(last=False) │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
BrokenResourceError

                         During handling of the above exception, another exception occurred:                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                  
                         ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────── Traceback (most recent call last) ───────────────────────────────────────────────────────────────────────────────────────────────────────────────╮                       
                         │ D:\ProgramData\sourcecode\agent\.venv\Lib\site-packages\mcp\client\streamable_http.py:307 in _handle_sse_response                                                                                                                                          │                       
                         │                                                                                                                                                                                                                                                                │                       
                         │   304 │   │   try:                                                                                                                                                                                                                                             │                       
                         │   305 │   │   │   event_source = EventSource(response)                                                                                                                                                                                                         │                       
                         │   306 │   │   │   async for sse in event_source.aiter_sse():                                                                                                                                                                                                   │                       
                         │ ❱ 307 │   │   │   │   is_complete = await self._handle_sse_event(                                                                                                                                                                                              │                       
                         │   308 │   │   │   │   │   sse,                                                                                                                                                                                                                                 │                       
                         │   309 │   │   │   │   │   ctx.read_stream_writer,                                                                                                                                                                                                              │                       
                         │   310 │   │   │   │   │   resumption_callback=(                                                                                                                                                                                                                │                       
                         │                                                                                                                                                                                                                                                                │                       
                         │ D:\ProgramData\sourcecode\agent\.venv\Lib\site-packages\mcp\client\streamable_http.py:174 in _handle_sse_event                                                                                                                                             │                       
                         │                                                                                                                                                                                                                                                                │                       
                         │   171 │   │   │                                                                                                                                                                                                                                                │                       
                         │   172 │   │   │   except Exception as exc:                                                                                                                                                                                                                     │                       
                         │   173 │   │   │   │   logger.error(f"Error parsing SSE message: {exc}")                                                                                                                                                                                        │                       
                         │ ❱ 174 │   │   │   │   await read_stream_writer.send(exc)                                                                                                                                                                                                       │                       
                         │   175 │   │   │   │   return False                                                                                                                                                                                                                             │                       
                         │   176 │   │   else:                                                                                                                                                                                                                                            │                       
                         │   177 │   │   │   logger.warning(f"Unknown SSE event: {sse.event}")                                                                                                                                                                                            │                       
                         │                                                                                                                                                                                                                                                                │                       
                         │ D:\ProgramData\sourcecode\agent\.venv\Lib\site-packages\anyio\streams\memory.py:242 in send                                                                                                                                                                │                       
                         │                                                                                                                                                                                                                                                                │                       
                         │   239 │   │   """                                                                                                                                                                                                                                              │                       
                         │   240 │   │   await checkpoint()                                                                                                                                                                                                                               │                       
                         │   241 │   │   try:                                                                                                                                                                                                                                             │                       
                         │ ❱ 242 │   │   │   self.send_nowait(item)                                                                                                                                                                                                                       │                       
                         │   243 │   │   except WouldBlock:                                                                                                                                                                                                                               │                       
                         │   244 │   │   │   # Wait until there's someone on the receiving end                                                                                                                                                                                            │                       
                         │   245 │   │   │   send_event = Event()                                                                                                                                                                                                                         │                       
                         │                                                                                                                                                                                                                                                                │                       
                         │ D:\ProgramData\sourcecode\agent\.venv\Lib\site-packages\anyio\streams\memory.py:213 in send_nowait                                                                                                                                                         │                       
                         │                                                                                                                                                                                                                                                                │                       
                         │   210 │   │   if self._closed:                                                                                                                                                                                                                                 │                       
                         │   211 │   │   │   raise ClosedResourceError                                                                                                                                                                                                                    │                       
                         │   212 │   │   if not self._state.open_receive_channels:                                                                                                                                                                                                        │                       
                         │ ❱ 213 │   │   │   raise BrokenResourceError                                                                                                                                                                                                                    │                       
                         │   214 │   │                                                                                                                                                                                                                                                    │                       
                         │   215 │   │   while self._state.waiting_receivers:                                                                                                                                                                                                             │                       
                         │   216 │   │   │   receive_event, receiver = self._state.waiting_receivers.popitem(last=False)                                                                                                                                                                  │                       
                         ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯                       
                         BrokenResourceError                                                                                                                                                                                                                                                                      

---------------------------------error INFO HTTP Request: POST http://127.0.0.1:10003/mcp/ "HTTP/1.1 400 Bad Request" _client.py:1740 ERROR Error in post_writer: Client error '400 Bad Request' for url 'http://127.0.0.1:10003/mcp/' streamable_http.py:404 For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400

client running call_toos, waiting the response, and the client closes the connection, but it fails. How to close the connection correctly? mcp version is 1.9.1 thanks!

freedba avatar May 28 '25 04:05 freedba