Provides a tool for sending escalation messages to Telegram chats and waiting for human responses, enabling AI agents to request input from users via Telegram when they need guidance or approval.
Codex MCP Telegram
An MCP (Model Context Protocol) server that wraps Codex CLI commands and provides remote access via Telegram Bot API. Execute Codex CLI commands remotely when you're away from your machine.
Features
š§ MCP Server: Exposes Codex CLI commands as MCP tools
š± 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
š Multiple Commands: Support for exec, review, and status checks
š¬ Agentic Escalation: Codex explicitly calls an MCP tool to request human guidance
Prerequisites
Codex CLI installed and available in PATH
# Verify installation codex --versionPython 3.10+
Telegram Bot (for remote access)
Create a bot via @BotFather on Telegram
Save the bot token
Installation
Clone or navigate to the project directory
Install dependencies:
pip install -r requirements.txtOr 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 BotFatherTELEGRAM_CHAT_ID: Chat ID where the escalation prompts should be sentTELEGRAM_ALLOWED_USER_IDS: Comma-separated list of Telegram user IDs allowed to reply
Optional
MAX_COMMAND_LENGTH: Maximum command length (default: 1000)COMMAND_TIMEOUT: Command execution timeout in seconds (default: 300)CODEX_DEFAULT_MODEL: Default model to use for Codex commands (e.g., "o1", "o3")
Example Configuration
Create a .env file or export environment variables:
Usage
As MCP Server (stdio)
Run the server:
Or use the installed script:
The server communicates via stdio following the MCP protocol.
Telegram MCP Escalation Tool
The MCP tool telegram_notify_and_wait sends a message to the configured chat and waits for a reply.
Reply format: #<correlation_id> <answer>
Example message:
Getting Your Telegram User ID
Start a chat with your bot
Send
/startThe bot will show your User ID if you're not authorized
Add this ID to
TELEGRAM_ALLOWED_USER_IDSor useTELEGRAM_CHAT_ID
MCP Tools
The server exposes the following MCP tools:
codex_exec
Execute a Codex CLI command.
Parameters:
prompt(required): The prompt/command to executemodel(optional): Model to use (e.g., "o1", "o3")
codex_review
Run a code review on specified files or directories.
Parameters:
target(required): File or directory path to reviewprompt(optional): Specific review instructions
codex_status
Check Codex CLI availability and version.
Parameters: None
telegram_notify_and_wait
Send a Telegram message and wait for a human response.
Parameters:
question(required): Question to sendtimeout_sec(optional): Seconds to wait (default: 1800)context(optional): Additional context to include
Response:
Returns JSON with answer and correlation_id. On timeout, answer is null and an error field is included.
Security Considerations
Authentication: Always configure
TELEGRAM_ALLOWED_USER_IDSto prevent unauthorized access.Command Injection: The server validates command length and uses proper subprocess execution. However, be cautious with prompts that may contain sensitive information.
Network: The Telegram bot requires network access. Ensure your firewall allows outbound connections to
api.telegram.org.Tokens: Never commit your bot token to version control. Use environment variables or secure configuration files.
Troubleshooting
"Codex CLI not found"
Ensure Codex CLI is installed and in your PATH
Verify with:
which codex
"Telegram bridge not starting"
Check that
TELEGRAM_BOT_TOKENis set correctlyVerify network connectivity to Telegram API
Check logs for detailed error messages
"Unauthorized" errors
Verify your User ID is in
TELEGRAM_ALLOWED_USER_IDSorTELEGRAM_CHAT_IDmatches
Timeout errors
Increase
COMMAND_TIMEOUTif commands take longerCheck Codex CLI logs for underlying issues
Telegram polling and timeouts
Ensure
TELEGRAM_CHAT_IDmatches the chat where the bot should post escalation messagesReplies 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_notify_and_wait:
Development
Local Telegram Tool Test
Manual Verification Checklist
If automated tests are not available, verify the following manually:
Start the MCP server and call
telegram_notify_and_wait.Confirm the Telegram message includes the correlation ID and reply instructions.
Reply with
#<id> <answer>from an allowed user ID and confirm the tool returns the answer.Reply without
#<id>and confirm nothing happens.Reply from an unallowed user ID and confirm nothing happens.
Let the call time out and confirm the tool returns a clear timeout error.
License
MIT License
Contributing
Contributions welcome! Please ensure code follows PEP 8 style guidelines and includes appropriate error handling.