Codex MCP Telegram

Codex MCP Telegram

Enables remote execution of Codex CLI commands and provides an MCP tool for AI agents to escalate questions to humans via Telegram, allowing for human-in-the-loop workflows when away from the machine.

Category
访问服务器

README

Codex MCP Telegram

An MCP (Model Context Protocol) server that provides a Telegram escalation tool for human-in-the-loop guidance.

Features

  • 🔧 MCP Server: Exposes Telegram escalation as an MCP tool
  • 📱 Telegram Escalation Tool: MCP tool to ask humans for input over Telegram
  • 🔒 Security: User authentication via allowed user IDs
  • Async Execution: Non-blocking command execution
  • 🧭 Two-step Flow: Prompt a human and poll for the answer
  • 💬 Agentic Escalation: Codex explicitly calls an MCP tool to request human guidance

Prerequisites

  1. Python 3.10+

  2. Telegram Bot (for remote access)

    • Create a bot via @BotFather on Telegram
    • Save the bot token

Installation

  1. Clone or navigate to the project directory

  2. Install dependencies:

    pip install -r requirements.txt
    

    Or install as a package:

    pip install -e .
    

Configuration

The server uses environment variables for configuration:

Required (for Telegram escalation tool)

  • TELEGRAM_BOT_TOKEN: Your Telegram bot token from BotFather
  • TELEGRAM_CHAT_ID: Chat ID where the escalation prompts should be sent
  • TELEGRAM_ALLOWED_USER_IDS: Comma-separated list of Telegram user IDs allowed to reply

Example Configuration

Create a .env file or export environment variables:

export TELEGRAM_BOT_TOKEN="your_bot_token_here"
export TELEGRAM_CHAT_ID="123456789"
export TELEGRAM_ALLOWED_USER_IDS="123456789,987654321"

Usage

As MCP Server (stdio)

Run the server:

python -m codex_mcp_server.server

Or use the installed script:

codex-mcp-telegram
# or (backward compatibility alias)
codex-mcp-server

The server communicates via stdio following the MCP protocol.

Telegram MCP Escalation Tools

The MCP tool telegram_prompt sends a message to the configured chat and returns a correlation_id. Use telegram_poll to check for a reply using that ID.

Reply format: #<correlation_id> <answer>

Example message:

❓ MCP Escalation

Should we proceed with the migration?

Reply with #<id> <answer>

Getting Your Telegram User ID

  1. Start a chat with your bot
  2. Send /start
  3. The bot will show your User ID if you're not authorized
  4. Add this ID to TELEGRAM_ALLOWED_USER_IDS or use TELEGRAM_CHAT_ID

MCP Tools

The server exposes the following MCP tools:

telegram_prompt

Send a Telegram message and return a correlation ID.

Parameters:

  • question (required): Question to send
  • context (optional): Additional context to include

Response: Returns JSON with correlation_id.

telegram_poll

Check for a human response associated with a correlation ID.

Parameters:

  • correlation_id (required): ID returned from telegram_prompt

Response: Returns JSON with:

  • status: pending, answered, expired, or unknown
  • answer: Present when status is answered

Security Considerations

  1. Authentication: Always configure TELEGRAM_ALLOWED_USER_IDS to prevent unauthorized access.

  2. Command Injection: The server validates command length and uses proper subprocess execution. However, be cautious with prompts that may contain sensitive information.

  3. Network: The Telegram bot requires network access. Ensure your firewall allows outbound connections to api.telegram.org.

  4. Tokens: Never commit your bot token to version control. Use environment variables or secure configuration files.

Troubleshooting

"Telegram bot not starting"

  • Check that TELEGRAM_BOT_TOKEN is set correctly
  • Verify network connectivity to Telegram API
  • Check logs for detailed error messages

"Unauthorized" errors

  • Verify your User ID is in TELEGRAM_ALLOWED_USER_IDS or TELEGRAM_CHAT_ID matches

Telegram polling and timeouts

  • Ensure TELEGRAM_CHAT_ID matches the chat where the bot should post escalation messages
  • Replies must include the correlation ID: #<id> <answer>
  • If timeouts persist, confirm the bot has permission to read messages in the chat

MCP Client Configuration (Codex CLI)

Add the MCP server in your Codex CLI configuration so it can call telegram_prompt and telegram_poll:

{
  "mcpServers": {
    "codex-mcp-telegram": {
      "command": "codex-mcp-telegram",
      "args": []
    }
  }
}

Or add it via the Codex CLI:

codex mcp add telegram -- python -m codex_mcp_server.server
Added global MCP server 'telegram'.
codex mcp list

Development

# Install in development mode
pip install -e .

# Run tests (if available)
pytest

# Run with debug logging
PYTHONPATH=. python -m codex_mcp_server.server

Manual Verification Checklist

If automated tests are not available, verify the following manually:

  • Start the MCP server.
  • Call telegram_prompt and confirm the Telegram message includes the correlation ID and reply instructions.
  • Reply with #<id> <answer> from an allowed user ID.
  • Call telegram_poll and confirm the tool returns status=answered and the answer.
  • Reply without #<id> and confirm nothing happens.
  • Reply from an unallowed user ID and confirm nothing happens.
  • Wait for the request to expire and confirm the tool returns status=expired.

License

MIT License

Contributing

Contributions welcome! Please ensure code follows PEP 8 style guidelines and includes appropriate error handling.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选