async def shutdown(signal, loop):
  """Cleanup tasks tied to the service's shutdown."""
  print(f"\nReceived exit signal {signal.name}...")

  # Get all running tasks
  tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]

  # Cancel all tasks
  for task in tasks:
    task.cancel()

  print(f"Cancelling {len(tasks)} outstanding tasks")
  await asyncio.gather(*tasks, return_exceptions=True)

  # Stop the loop
  loop.stop()
  print("Shutdown complete!")
Cleanup tasks tied to the service's shutdown.
contributing/samples/mcp_sse_agent/filesystem_server.py