telegram-mcp-server 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 [Your Name]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ include README.md
2
+ include LICENSE
3
+ include INSTALL.md
4
+ include USAGE.md
5
+ include requirements.txt
6
+ recursive-include telegram_mcp_server *.py
@@ -0,0 +1,248 @@
1
+ Metadata-Version: 2.4
2
+ Name: telegram-mcp-server
3
+ Version: 0.1.0
4
+ Summary: Remote control Claude Code via Telegram - MCP Server
5
+ Author-email: Ray Volcy <volycu@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/batianVolyc/telegram-mcp-server
8
+ Project-URL: Documentation, https://github.com/batianVolyc/telegram-mcp-server#readme
9
+ Project-URL: Repository, https://github.com/batianVolyc/telegram-mcp-server
10
+ Project-URL: Issues, https://github.com/batianVolyc/telegram-mcp-server/issues
11
+ Keywords: mcp,telegram,claude,ai,automation
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Communications :: Chat
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Requires-Python: >=3.10
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: mcp>=1.0.0
25
+ Requires-Dist: python-telegram-bot>=21.0
26
+ Requires-Dist: httpx>=0.24.0
27
+ Requires-Dist: toml>=0.10.0
28
+ Provides-Extra: dev
29
+ Requires-Dist: pytest>=7.0; extra == "dev"
30
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
31
+ Requires-Dist: black>=23.0; extra == "dev"
32
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
33
+ Dynamic: license-file
34
+
35
+ # Telegram MCP Server
36
+
37
+ > 通过 Telegram 远程控制 AI 编程助手(Claude Code / Codex)
38
+
39
+ [![PyPI](https://img.shields.io/pypi/v/telegram-mcp-server)](https://pypi.org/project/telegram-mcp-server/)
40
+ [![Python](https://img.shields.io/pypi/pyversions/telegram-mcp-server)](https://pypi.org/project/telegram-mcp-server/)
41
+ [![License](https://img.shields.io/github/license/batianVolyc/telegram-mcp-server)](LICENSE)
42
+
43
+ ## 特性
44
+
45
+ - 🌙 **真正的无人值守** - 最长等待 7 天,智能渐进式轮询
46
+ - 📱 **远程控制** - 通过 Telegram 随时随地控制 AI 助手
47
+ - 🔄 **双向通信** - 发送通知,接收回复,持续对话
48
+ - 📁 **文件操作** - 查看、下载项目文件
49
+ - 🎯 **多会话管理** - 同时管理多个项目
50
+ - 🤖 **通用支持** - 同时支持 Claude Code 和 Codex
51
+
52
+ ## 快速开始
53
+
54
+ ### 1. 安装
55
+
56
+ ```bash
57
+ # 推荐:使用 uv
58
+ uv tool install telegram-mcp-server
59
+
60
+ # 或使用 pip
61
+ pip install telegram-mcp-server
62
+ ```
63
+
64
+ ### 2. 配置
65
+
66
+ ```bash
67
+ telegram-mcp-server --setup
68
+ ```
69
+
70
+ 交互式向导会帮你:
71
+ - 创建 Telegram Bot
72
+ - 获取凭据
73
+ - 自动配置 AI 助手
74
+
75
+ ### 3. 使用
76
+
77
+ ```bash
78
+ # 启动 AI 助手
79
+ claude # 或 codex
80
+
81
+ # 在 AI 助手中
82
+ > 进入无人值守模式。任务:分析项目结构
83
+ ```
84
+
85
+ 在 Telegram 查看结果并继续对话!
86
+
87
+ ## 工作原理
88
+
89
+ ```
90
+ AI 助手 (Claude Code/Codex)
91
+ ↓ MCP 协议
92
+ MCP 服务器 (telegram-mcp-server)
93
+ ├─ 8 个工具(通知、等待、文件等)
94
+ └─ Telegram Bot(后台运行)
95
+ ↓ Telegram API
96
+ 你的 Telegram 客户端
97
+ ```
98
+
99
+ ## 核心功能
100
+
101
+ ### MCP 工具
102
+
103
+ - `telegram_notify` - 发送结构化通知
104
+ - `telegram_wait_reply` - 等待用户回复
105
+ - `telegram_unattended_mode` - 无人值守模式
106
+ - `telegram_send_file` - 发送文件
107
+ - `telegram_send_code` - 发送代码(带语法高亮)
108
+ - 更多...
109
+
110
+ ### Telegram 命令
111
+
112
+ - `/sessions` - 列出所有会话
113
+ - `/status <id>` - 查看会话状态
114
+ - `/to <id> <msg>` - 发送消息到会话
115
+ - `/file <id> <path>` - 查看文件
116
+ - `/delete <id>` - 删除会话
117
+ - `/help` - 显示帮助
118
+
119
+ ### 智能轮询
120
+
121
+ 渐进式轮询策略,最长等待 7 天:
122
+
123
+ | 等待时长 | 检查频率 | 响应延迟 |
124
+ |---------|---------|---------|
125
+ | 0-30 分钟 | 每 30 秒 | 最多 30 秒 |
126
+ | 30-60 分钟 | 每 60 秒 | 最多 60 秒 |
127
+ | 1 小时以上 | 每 120 秒 | 最多 120 秒 |
128
+
129
+ ## 使用场景
130
+
131
+ ### 场景 1: 过夜任务
132
+
133
+ ```bash
134
+ # 晚上 10 点
135
+ > 进入无人值守模式。任务:运行完整测试套件,修复所有错误
136
+
137
+ # 早上 8 点在 Telegram 查看结果
138
+ ```
139
+
140
+ ### 场景 2: 远程工作
141
+
142
+ ```bash
143
+ # 在办公室启动任务
144
+ > 进入无人值守模式。任务:重构数据库访问层
145
+
146
+ # 在路上通过 Telegram 查看进度和控制
147
+ ```
148
+
149
+ ### 场景 3: 多项目管理
150
+
151
+ ```bash
152
+ # 终端 1
153
+ cd project-a
154
+ TELEGRAM_SESSION="proj-a" claude
155
+
156
+ # 终端 2
157
+ cd project-b
158
+ TELEGRAM_SESSION="proj-b" codex
159
+
160
+ # 在 Telegram 同时管理两个项目
161
+ ```
162
+
163
+ ## 配置
164
+
165
+ ### Claude Code
166
+
167
+ 支持三种配置范围:
168
+
169
+ - **User scope**: `~/.claude/mcp.json` - 全局配置
170
+ - **Project scope**: `.mcp.json` - 团队共享
171
+ - **Local scope**: `.claude/mcp.json` - 项目特定
172
+
173
+ ### Codex
174
+
175
+ 全局配置:`~/.codex/config.toml`
176
+
177
+ 自动包含 `tool_timeout_sec = 604800`(7 天超时)
178
+
179
+ ## 环境变量
180
+
181
+ ```bash
182
+ # 自定义会话名
183
+ TELEGRAM_SESSION="my-task" claude
184
+
185
+ # 自定义最长等待时间
186
+ TELEGRAM_MAX_WAIT=86400 claude # 24 小时
187
+
188
+ # 自定义轮询间隔
189
+ TELEGRAM_POLL_INTERVAL="10,30,60" claude
190
+ ```
191
+
192
+ ## 故障排查
193
+
194
+ ### 问题:Telegram Bot 无响应
195
+
196
+ ```bash
197
+ # 查看日志
198
+ tail -f /tmp/telegram-mcp-server.log
199
+
200
+ # 快速修复
201
+ cd telegram-mcp-server
202
+ ./quick_fix.sh
203
+ ```
204
+
205
+ ### 问题:Codex 60 秒超时
206
+
207
+ ```bash
208
+ # 自动修复
209
+ ./fix_codex_timeout.sh
210
+ ```
211
+
212
+ ### 问题:会话未注册
213
+
214
+ ```bash
215
+ # 重新配置
216
+ telegram-mcp-server --setup
217
+ ```
218
+
219
+ ## 文档
220
+
221
+ - [配置指南](docs/CONFIGURATION_GUIDE.md) - 详细配置说明
222
+ - [轮询机制](docs/POLLING_MECHANISM.md) - 智能轮询原理
223
+ - [故障排查](docs/TROUBLESHOOTING.md) - 常见问题解决
224
+ - [MCP 工作原理](docs/HOW_MCP_WORKS.md) - 技术架构
225
+
226
+ ## 系统要求
227
+
228
+ - Python 3.10+
229
+ - Claude Code 或 Codex
230
+ - Telegram 账号
231
+
232
+ ## 贡献
233
+
234
+ 欢迎贡献!请查看 [CONTRIBUTING.md](CONTRIBUTING.md)
235
+
236
+ ## 许可证
237
+
238
+ MIT License - 详见 [LICENSE](LICENSE)
239
+
240
+ ## 支持
241
+
242
+ - 🐛 [报告问题](https://github.com/batianVolyc/telegram-mcp-server/issues)
243
+ - 💬 [讨论](https://github.com/batianVolyc/telegram-mcp-server/discussions)
244
+ - ⭐ 如果觉得有用,请给个 Star!
245
+
246
+ ---
247
+
248
+ **让 AI 编程助手为你工作,而不是你守着它工作** 🚀
@@ -0,0 +1,214 @@
1
+ # Telegram MCP Server
2
+
3
+ > 通过 Telegram 远程控制 AI 编程助手(Claude Code / Codex)
4
+
5
+ [![PyPI](https://img.shields.io/pypi/v/telegram-mcp-server)](https://pypi.org/project/telegram-mcp-server/)
6
+ [![Python](https://img.shields.io/pypi/pyversions/telegram-mcp-server)](https://pypi.org/project/telegram-mcp-server/)
7
+ [![License](https://img.shields.io/github/license/batianVolyc/telegram-mcp-server)](LICENSE)
8
+
9
+ ## 特性
10
+
11
+ - 🌙 **真正的无人值守** - 最长等待 7 天,智能渐进式轮询
12
+ - 📱 **远程控制** - 通过 Telegram 随时随地控制 AI 助手
13
+ - 🔄 **双向通信** - 发送通知,接收回复,持续对话
14
+ - 📁 **文件操作** - 查看、下载项目文件
15
+ - 🎯 **多会话管理** - 同时管理多个项目
16
+ - 🤖 **通用支持** - 同时支持 Claude Code 和 Codex
17
+
18
+ ## 快速开始
19
+
20
+ ### 1. 安装
21
+
22
+ ```bash
23
+ # 推荐:使用 uv
24
+ uv tool install telegram-mcp-server
25
+
26
+ # 或使用 pip
27
+ pip install telegram-mcp-server
28
+ ```
29
+
30
+ ### 2. 配置
31
+
32
+ ```bash
33
+ telegram-mcp-server --setup
34
+ ```
35
+
36
+ 交互式向导会帮你:
37
+ - 创建 Telegram Bot
38
+ - 获取凭据
39
+ - 自动配置 AI 助手
40
+
41
+ ### 3. 使用
42
+
43
+ ```bash
44
+ # 启动 AI 助手
45
+ claude # 或 codex
46
+
47
+ # 在 AI 助手中
48
+ > 进入无人值守模式。任务:分析项目结构
49
+ ```
50
+
51
+ 在 Telegram 查看结果并继续对话!
52
+
53
+ ## 工作原理
54
+
55
+ ```
56
+ AI 助手 (Claude Code/Codex)
57
+ ↓ MCP 协议
58
+ MCP 服务器 (telegram-mcp-server)
59
+ ├─ 8 个工具(通知、等待、文件等)
60
+ └─ Telegram Bot(后台运行)
61
+ ↓ Telegram API
62
+ 你的 Telegram 客户端
63
+ ```
64
+
65
+ ## 核心功能
66
+
67
+ ### MCP 工具
68
+
69
+ - `telegram_notify` - 发送结构化通知
70
+ - `telegram_wait_reply` - 等待用户回复
71
+ - `telegram_unattended_mode` - 无人值守模式
72
+ - `telegram_send_file` - 发送文件
73
+ - `telegram_send_code` - 发送代码(带语法高亮)
74
+ - 更多...
75
+
76
+ ### Telegram 命令
77
+
78
+ - `/sessions` - 列出所有会话
79
+ - `/status <id>` - 查看会话状态
80
+ - `/to <id> <msg>` - 发送消息到会话
81
+ - `/file <id> <path>` - 查看文件
82
+ - `/delete <id>` - 删除会话
83
+ - `/help` - 显示帮助
84
+
85
+ ### 智能轮询
86
+
87
+ 渐进式轮询策略,最长等待 7 天:
88
+
89
+ | 等待时长 | 检查频率 | 响应延迟 |
90
+ |---------|---------|---------|
91
+ | 0-30 分钟 | 每 30 秒 | 最多 30 秒 |
92
+ | 30-60 分钟 | 每 60 秒 | 最多 60 秒 |
93
+ | 1 小时以上 | 每 120 秒 | 最多 120 秒 |
94
+
95
+ ## 使用场景
96
+
97
+ ### 场景 1: 过夜任务
98
+
99
+ ```bash
100
+ # 晚上 10 点
101
+ > 进入无人值守模式。任务:运行完整测试套件,修复所有错误
102
+
103
+ # 早上 8 点在 Telegram 查看结果
104
+ ```
105
+
106
+ ### 场景 2: 远程工作
107
+
108
+ ```bash
109
+ # 在办公室启动任务
110
+ > 进入无人值守模式。任务:重构数据库访问层
111
+
112
+ # 在路上通过 Telegram 查看进度和控制
113
+ ```
114
+
115
+ ### 场景 3: 多项目管理
116
+
117
+ ```bash
118
+ # 终端 1
119
+ cd project-a
120
+ TELEGRAM_SESSION="proj-a" claude
121
+
122
+ # 终端 2
123
+ cd project-b
124
+ TELEGRAM_SESSION="proj-b" codex
125
+
126
+ # 在 Telegram 同时管理两个项目
127
+ ```
128
+
129
+ ## 配置
130
+
131
+ ### Claude Code
132
+
133
+ 支持三种配置范围:
134
+
135
+ - **User scope**: `~/.claude/mcp.json` - 全局配置
136
+ - **Project scope**: `.mcp.json` - 团队共享
137
+ - **Local scope**: `.claude/mcp.json` - 项目特定
138
+
139
+ ### Codex
140
+
141
+ 全局配置:`~/.codex/config.toml`
142
+
143
+ 自动包含 `tool_timeout_sec = 604800`(7 天超时)
144
+
145
+ ## 环境变量
146
+
147
+ ```bash
148
+ # 自定义会话名
149
+ TELEGRAM_SESSION="my-task" claude
150
+
151
+ # 自定义最长等待时间
152
+ TELEGRAM_MAX_WAIT=86400 claude # 24 小时
153
+
154
+ # 自定义轮询间隔
155
+ TELEGRAM_POLL_INTERVAL="10,30,60" claude
156
+ ```
157
+
158
+ ## 故障排查
159
+
160
+ ### 问题:Telegram Bot 无响应
161
+
162
+ ```bash
163
+ # 查看日志
164
+ tail -f /tmp/telegram-mcp-server.log
165
+
166
+ # 快速修复
167
+ cd telegram-mcp-server
168
+ ./quick_fix.sh
169
+ ```
170
+
171
+ ### 问题:Codex 60 秒超时
172
+
173
+ ```bash
174
+ # 自动修复
175
+ ./fix_codex_timeout.sh
176
+ ```
177
+
178
+ ### 问题:会话未注册
179
+
180
+ ```bash
181
+ # 重新配置
182
+ telegram-mcp-server --setup
183
+ ```
184
+
185
+ ## 文档
186
+
187
+ - [配置指南](docs/CONFIGURATION_GUIDE.md) - 详细配置说明
188
+ - [轮询机制](docs/POLLING_MECHANISM.md) - 智能轮询原理
189
+ - [故障排查](docs/TROUBLESHOOTING.md) - 常见问题解决
190
+ - [MCP 工作原理](docs/HOW_MCP_WORKS.md) - 技术架构
191
+
192
+ ## 系统要求
193
+
194
+ - Python 3.10+
195
+ - Claude Code 或 Codex
196
+ - Telegram 账号
197
+
198
+ ## 贡献
199
+
200
+ 欢迎贡献!请查看 [CONTRIBUTING.md](CONTRIBUTING.md)
201
+
202
+ ## 许可证
203
+
204
+ MIT License - 详见 [LICENSE](LICENSE)
205
+
206
+ ## 支持
207
+
208
+ - 🐛 [报告问题](https://github.com/batianVolyc/telegram-mcp-server/issues)
209
+ - 💬 [讨论](https://github.com/batianVolyc/telegram-mcp-server/discussions)
210
+ - ⭐ 如果觉得有用,请给个 Star!
211
+
212
+ ---
213
+
214
+ **让 AI 编程助手为你工作,而不是你守着它工作** 🚀
@@ -0,0 +1,64 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "telegram-mcp-server"
7
+ version = "0.1.0"
8
+ description = "Remote control Claude Code via Telegram - MCP Server"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = {text = "MIT"}
12
+ authors = [
13
+ {name = "Ray Volcy", email = "volycu@gmail.com"}
14
+ ]
15
+ keywords = ["mcp", "telegram", "claude", "ai", "automation"]
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Developers",
19
+ "License :: OSI Approved :: MIT License",
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3.10",
22
+ "Programming Language :: Python :: 3.11",
23
+ "Programming Language :: Python :: 3.12",
24
+ "Topic :: Communications :: Chat",
25
+ "Topic :: Software Development :: Libraries :: Python Modules",
26
+ ]
27
+
28
+ dependencies = [
29
+ "mcp>=1.0.0",
30
+ "python-telegram-bot>=21.0",
31
+ "httpx>=0.24.0",
32
+ "toml>=0.10.0",
33
+ ]
34
+
35
+ [project.optional-dependencies]
36
+ dev = [
37
+ "pytest>=7.0",
38
+ "pytest-asyncio>=0.21.0",
39
+ "black>=23.0",
40
+ "ruff>=0.1.0",
41
+ ]
42
+
43
+ [project.urls]
44
+ Homepage = "https://github.com/batianVolyc/telegram-mcp-server"
45
+ Documentation = "https://github.com/batianVolyc/telegram-mcp-server#readme"
46
+ Repository = "https://github.com/batianVolyc/telegram-mcp-server"
47
+ Issues = "https://github.com/batianVolyc/telegram-mcp-server/issues"
48
+
49
+ [project.scripts]
50
+ telegram-mcp-server = "telegram_mcp_server.cli:main"
51
+
52
+ [tool.setuptools]
53
+ packages = ["telegram_mcp_server"]
54
+
55
+ [tool.setuptools.package-data]
56
+ telegram_mcp_server = ["*.md"]
57
+
58
+ [tool.black]
59
+ line-length = 100
60
+ target-version = ['py310']
61
+
62
+ [tool.ruff]
63
+ line-length = 100
64
+ target-version = "py310"
@@ -0,0 +1,3 @@
1
+ mcp>=1.0.0
2
+ python-telegram-bot>=21.0
3
+ aiofiles>=23.0.0
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,9 @@
1
+ """Telegram MCP Server - Remote control Claude Code via Telegram"""
2
+
3
+ __version__ = "0.1.0"
4
+ __author__ = "Your Name"
5
+ __license__ = "MIT"
6
+
7
+ from . import server, bot, config, session, message_queue
8
+
9
+ __all__ = ["server", "bot", "config", "session", "message_queue"]
@@ -0,0 +1,123 @@
1
+ """Main entry point for Telegram MCP Server"""
2
+ import sys
3
+ import os
4
+ import logging
5
+ import asyncio
6
+ from mcp.server.stdio import stdio_server
7
+
8
+ from . import config
9
+ from .server import server
10
+ from .bot import setup_bot
11
+
12
+ # Setup logging
13
+ logging.basicConfig(
14
+ level=logging.INFO,
15
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
16
+ handlers=[
17
+ logging.FileHandler('/tmp/telegram-mcp-server.log'),
18
+ logging.StreamHandler(sys.stderr)
19
+ ]
20
+ )
21
+
22
+ logger = logging.getLogger(__name__)
23
+
24
+
25
+ async def run_telegram_bot():
26
+ """Run Telegram bot asynchronously (only if not already running)"""
27
+ try:
28
+ config.validate_config()
29
+ except ValueError as e:
30
+ logger.error(f"Configuration error: {e}")
31
+ logger.error("Telegram bot will not start. MCP server will continue without Telegram integration.")
32
+ return
33
+
34
+ # Check if another bot instance is already running
35
+ import socket
36
+ lock_file = "/tmp/telegram-mcp-bot.lock"
37
+
38
+ try:
39
+ # Try to create lock file
40
+ if os.path.exists(lock_file):
41
+ # Check if the process is still running
42
+ with open(lock_file, 'r') as f:
43
+ old_pid = f.read().strip()
44
+
45
+ # Check if process exists
46
+ try:
47
+ os.kill(int(old_pid), 0) # Check if process exists
48
+ logger.info(f"Telegram bot already running in another process (PID: {old_pid})")
49
+ logger.info("This MCP server instance will use shared storage without starting bot")
50
+ return
51
+ except OSError:
52
+ # Process doesn't exist, remove stale lock
53
+ os.remove(lock_file)
54
+
55
+ # Write our PID to lock file
56
+ with open(lock_file, 'w') as f:
57
+ f.write(str(os.getpid()))
58
+
59
+ logger.info("Starting Telegram bot (first instance)...")
60
+
61
+ # Setup bot
62
+ bot_app = setup_bot(config.TELEGRAM_BOT_TOKEN)
63
+
64
+ # Set global telegram_bot reference
65
+ from . import server as server_module
66
+ server_module.telegram_bot = bot_app
67
+
68
+ # Initialize and start bot
69
+ try:
70
+ async with bot_app:
71
+ await bot_app.initialize()
72
+ await bot_app.start()
73
+ await bot_app.updater.start_polling(allowed_updates=None)
74
+ # Keep bot running
75
+ while True:
76
+ await asyncio.sleep(1)
77
+ except Exception as e:
78
+ logger.error(f"Telegram bot error: {e}", exc_info=True)
79
+ finally:
80
+ # Remove lock file on exit
81
+ if os.path.exists(lock_file):
82
+ os.remove(lock_file)
83
+
84
+ except Exception as e:
85
+ logger.error(f"Bot startup error: {e}", exc_info=True)
86
+
87
+
88
+ async def main():
89
+ """Main entry point"""
90
+ logger.info("Starting Telegram MCP Server...")
91
+
92
+ # Create task for Telegram bot
93
+ bot_task = asyncio.create_task(run_telegram_bot())
94
+
95
+ # Give bot time to start
96
+ await asyncio.sleep(2)
97
+
98
+ # Run MCP server (stdio)
99
+ logger.info("Starting MCP stdio server...")
100
+ try:
101
+ async with stdio_server() as (read_stream, write_stream):
102
+ await server.run(
103
+ read_stream,
104
+ write_stream,
105
+ server.create_initialization_options()
106
+ )
107
+ finally:
108
+ # Cancel bot task on exit
109
+ bot_task.cancel()
110
+ try:
111
+ await bot_task
112
+ except asyncio.CancelledError:
113
+ pass
114
+
115
+
116
+ if __name__ == "__main__":
117
+ try:
118
+ asyncio.run(main())
119
+ except KeyboardInterrupt:
120
+ logger.info("Shutting down...")
121
+ except Exception as e:
122
+ logger.error(f"Fatal error: {e}", exc_info=True)
123
+ sys.exit(1)