Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/mcp_telegram/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ async def app_lifespan(server: FastMCP) -> AsyncIterator[None]:

This will connect to Telegram on startup and disconnect on shutdown.
"""
client_connected = False
try:
tg.create_client()
await tg.client.connect()
client_connected = True
yield
finally:
await tg.client.disconnect() # type: ignore
# Only disconnect if we successfully connected
if client_connected and tg._client is not None:
try:
await tg.client.disconnect()
except Exception:
# Ignore disconnect errors during cleanup
pass


tg = Telegram()
Expand Down