Disconnect clients and stop the background loop. Idempotent.
(self)
| 144 | ) |
| 145 | |
| 146 | def shutdown(self) -> None: |
| 147 | """Disconnect clients and stop the background loop. Idempotent.""" |
| 148 | loop = self._loop |
| 149 | if loop is None: |
| 150 | return |
| 151 | for name, client in list(self.clients.items()): |
| 152 | try: |
| 153 | self._run(client.close(), 5.0) |
| 154 | except Exception: # noqa: BLE001 |
| 155 | logger.debug("[mcp] close failed: %s", name, exc_info=True) |
| 156 | try: |
| 157 | loop.call_soon_threadsafe(loop.stop) |
| 158 | except Exception: # noqa: BLE001 |
| 159 | pass |
| 160 | self._loop = None |
| 161 | self.clients = {} |
| 162 | self.tools = [] |