Skip to content

how to handle mcp server down or reconnect?Β #1022

Open
@amd-maheshs3

Description

@amd-maheshs3

Question

elif connection_type == "http":
            try:
                transport = await self.exit_stack.enter_async_context(
                    streamablehttp_client(
                        url=self.config["url"],
                        headers=self.config.get("headers", {}),
                        timeout=self.config.get("timeout", 600),
                    )
                )
                read_stream, write_stream, _ = transport
                print(read_stream, write_stream)
                session = await self.exit_stack.enter_async_context(
                    ClientSession(read_stream, write_stream)
                )
                await session.initialize()
                self.session = session
            except Exception as e:
                logging.error(f"Error initializing HTTP server {self.name}: {e}")
                await self.cleanup()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\maheshs3\OneDrive - Advanced Micro Devices Inc\Desktop\mcp\test_chat.py", line 479, in
asyncio.run(main())
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\base_events.py", line 654, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\Users\maheshs3\OneDrive - Advanced Micro Devices Inc\Desktop\mcp\test_chat.py", line 474, in main
await chat_session.start()
File "C:\Users\maheshs3\OneDrive - Advanced Micro Devices Inc\Desktop\mcp\test_chat.py", line 387, in start
await server.initialize()
File "C:\Users\maheshs3\OneDrive - Advanced Micro Devices Inc\Desktop\mcp\test_chat.py", line 130, in initialize
await session.initialize()
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\site-packages\mcp\client\session.py", line 123, in initialize
result = await self.send_request(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\site-packages\mcp\shared\session.py", line 272, in send_request
response_or_error = await response_stream_reader.receive()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\site-packages\anyio\streams\memory.py", line 119, in receive
await receive_event.wait()
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\locks.py", line 213, in wait
await fut
asyncio.exceptions.CancelledError: Cancelled by cancel scope 18b317f29d0
Exception ignored in: <function BaseSubprocessTransport.del at 0x0000018B2E0E1A80>
Traceback (most recent call last):
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\base_subprocess.py", line 125, in del
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\base_subprocess.py", line 78, in repr
info.append(f'stdout={stdout.pipe}')
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\proactor_events.py", line 80, in repr
info.append(f'fd={self._sock.fileno()}')
^^^^^^^^^^^^^^^^^^^
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\windows_utils.py", line 102, in fileno
raise ValueError("I/O operation on closed pipe")
ValueError: I/O operation on closed pipe
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x0000018B2E0E31A0>
Traceback (most recent call last):
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\proactor_events.py", line 116, in del
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\proactor_events.py", line 80, in repr
info.append(f'fd={self._sock.fileno()}')
^^^^^^^^^^^^^^^^^^^
File "C:\Users\maheshs3\AppData\Local\anaconda3\envs\project_dummy\Lib\asyncio\windows_utils.py", line 102, in fileno
raise ValueError("I/O operation on closed pipe")
ValueError: I/O operation on closed pipe

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions