pyrogram-mcp
Provides a persistent OpenAI Codex app-server bridge, allowing Telegram commands to start and manage stateful Codex sessions with configurable model, reasoning effort, service tier, and durable conversation threads.
Provides Telegram userbot operations through MCP, including chat management, reading message history, sending media, search, contacts, moderation, and dynamic raw MTProto calls using a shared Kurigram session.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@pyrogram-mcpsend 'deploy complete' to my Saved Messages on Telegram"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
β‘ pyrogram-mcp
High-Performance Telegram Userbot & Model Context Protocol (MCP) Server
Built with Kurigram, dispyro, and FastMCP with an integrated, persistent OpenAI Codex App-Server Bridge.
pyrogram-mcp bridges native Telegram operations directly into LLM agent workflows (such as OpenAI Codex, Claude Code, and Cursor) via the Model Context Protocol (MCP). It runs as both an interactive Telegram userbot and an MCP server over multiple Kurigram sessions in one asyncio loop, cleanly separating Telegram update dispatching, core business operations, and MCP tool adapters into isolated layers.
π Key Highlights
π Unified Multi-Session Runtime: Shares several active MTProto sessions and one
asyncioevent loop across userbot handlers, background workers, and MCP tool executions β without duplicate processes.π οΈ Dual-Layer MCP Tooling:
High-Level Tools: Clean abstractions for managing chats, reading message history, sending media, search, contacts, and moderation.
Dynamic Reflection: Runtime introspection into installed Kurigram MTProto methods (
raw_search,raw_describe,raw_call) and high-level client methods (client_search,client_describe,client_call).
π€ Persistent Codex App-Server Bridge: Issue commands directly inside Telegram using
Π°ΠΌ <Π·Π°ΠΏΡΠΎΡ>to trigger a long-running, stateful OpenAI Codex session powered bygpt-5.6-luna.π Flexible MCP Transports: Supports Streamable HTTP (
http://127.0.0.1:8000/mcp) for long-lived daemon connections and stdio for direct process execution.π‘οΈ Owner-Only Security: All Telegram commands enforce strict
filters.mechecks; MCP filesystem access is sandboxed via configurableMEDIA_ROOTS.
Related MCP server: DreamAgent MCP Server
ποΈ Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β MCP Clients / Codex β
ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β (HTTP / stdio)
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β core/mcp/tools FastMCP Schemas & Adapters β
ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β core/service Telegram Operations & Policy β
ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β core/telegram Peer Resolution & Raw MTProto β
ββββββββββββββββ¬ββββββββββββββββββββββββββββββββ¬βββββββββββββββ
β β
βΌ βΌ
βββββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββββ
β Kurigram Client β β dispyro Dispatcher β
β (Shared MTProto Engine) β β (Telegram Update Handlers) β
βββββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββββDetailed architectural diagrams and implementation notes are available in IMPLEMENTATION_PLAN.md.
Userbot command behavior and conventions are specified in COMMANDS.md.
π Requirements
Requirement | Minimum Version | Notes |
Python |
| Modern |
Telegram API | β |
|
Poetry | Latest | Dependency management & environment isolation |
Codex CLI |
| Required for Codex bridge ( |
MySQL / Redis | Optional | Only needed if persistence / caching is enabled |
π Quickstart
1. Clone & Install
git clone <repository-url>
cd pyrogram-mcp
poetry install --no-root2. Configure Environment
cp settings.env.example settings.envOpen settings.env and supply your Telegram credentials:
API_ID=1234567
API_HASH=your_telegram_api_hash
PHONE_NUMBER=+1234567890
# Minimal standalone configuration (disable optional databases)
MYSQL_ENABLED=false
REDIS_ENABLED=false
# MCP & Codex Bridge
MCP_TRANSPORT=streamable-http
MCP_HOST=127.0.0.1
MCP_PORT=80003. Launch
poetry run python3 run.pyTelegram authorization is performed from the TUI. Each session file is saved securely tocore/sessions/ (which is excluded by .gitignore).
The TUI offers:
ΠΠ°ΠΏΡΡΡΠΈΡΡ β starts every saved Telegram session in one asyncio loop; this option is disabled until at least one session exists.
Π‘ΠΎΠ·Π΄Π°ΡΡ ΡΠ΅ΡΡΠΈΡ Telegram β asks for a session name, phone, Telegram code and optional 2FA password.
The session list at the bottom shows five accounts at a time. Use β/β or
j/k to scroll; PageUp/PageDown moves by a page. Session files are stored in
core/sessions/, with non-secret names and phone numbers in the ignored
core/data/sessions.json metadata file.
π MCP Transport & Configuration
Streamable HTTP (Recommended)
When Codex or an external MCP client connects to an existing running instance of pyrogram-mcp, use Streamable HTTP:
MCP_TRANSPORT=streamable-http
MCP_HOST=127.0.0.1
MCP_PORT=8000MCP Endpoint:
http://127.0.0.1:8000/mcpRequired for the interactive Telegram
Π°ΠΌbridge.
stdio
When an MCP client manages the server lifecycle directly as a subprocess, configure:
MCP_TRANSPORT=stdioπ§ OpenAI Codex Bridge
The Codex bridge manages a single, persistent codex app-server process with durable conversational threads saved to core/data/codex_thread.json.
Environment & Authentication Isolation
By default, native Codex state is isolated in its own workspace:
CODEX_HOME=~/.local/share/pyrogram-mcp/codexThis prevents project rollouts and threads from polluting your main user or VS Code Codex environment.
Reusing existing auth: If
~/.codex/auth.jsonis present, the bridge automatically creates a symlink so credentials are not duplicated.Manual login: If no existing credentials exist:
CODEX_HOME=~/.local/share/pyrogram-mcp/codex codex login
Bridge Tuning
CODEX_BIN=codex
CODEX_MODEL=gpt-5.6-luna
CODEX_REASONING_EFFORT=xhigh
CODEX_SERVICE_TIER=default
CODEX_TIMEOUT=0π¬ Telegram Commands
CODEX_TIMEOUT=0 lets long exports finish without a total turn deadline.
A positive value sets a total timeout in seconds. Requests remain serialized;
while a request runs, another Π°ΠΌ reports that the previous request is busy.
All userbot commands are strictlyowner-only (filters.me) and case-insensitive.
Command | Description | Example |
| Verifies userbot responsiveness |
|
| Lists available Codex models and reasoning levels |
|
| Switches active Codex model |
|
| Shows current model and active reasoning effort |
|
| Displays current Codex service tier |
|
| Switches service tier ( |
|
| Sends prompt to persistent Codex agent |
|
For complete usage guides and command behavior, see COMMANDS.md.
Codex response rules and Telegram HTML formatting live in
core/prompts/codex.md.
Codex replies are normalized from Markdown to Telegram HTML before delivery.
Message search supports server-side from_user filtering and exclusive
min_id/max_id pagination. History offset_id is an exclusive older-page
cursor. Batch known message IDs instead of fetching them individually.
For multi-account work, call list_sessions first and pass its name as the
session argument to the selected MCP tool. Omitting it uses the first/default
session. This applies to named message/entity/media tools, client_call and
raw_call.
π Dynamic Kurigram Raw API
pyrogram-mcp introspects the installed Kurigram package dynamically at runtime rather than relying on static, hardcoded MTProto classes:
raw_search: Search for MTProto functions and types by pattern.raw_describe: Inspect method signatures, parameter types, and docstrings.raw_call: Execute any MTProto function dynamically with JSON arguments.
Example: Calling Raw MTProto
{
"peer": {
"_": "types.InputPeerSelf"
},
"limit": 50
}Peer Resolver Helper
To resolve usernames or channel IDs seamlessly without manual conversion, use the __resolve_peer__ token:
{
"_": "__resolve_peer__",
"value": "@channel_name"
}π Security & Repository Hygiene
π« Do Not Commit Secrets: Never commit
settings.env,.sessionfiles incore/sessions/,auth.json, or runtime logs.π Sandboxed File Access: Restrict
MEDIA_ROOTSstrictly to directories intended for MCP file access (e.g./tmp/pyrogram-mcp-media).π Restricted Command Scope: Userbot commands are protected with
filters.me; destructive write actions are clearly annotated in the tool schema.
π§ͺ Development & Quality Checks
Run local checks prior to committing changes:
# Verify syntax and bytecode compilation
poetry run python3 -m compileall -q .
# Verify total registered MCP tools
poetry run python3 -c 'from core.mcp.server import mcp; print(f"Registered MCP tools: {len(mcp._tool_manager._tools)}")'π License
Distributed under the terms appropriate for your deployment. Refer to the repository license or contact the maintainers before redistribution.
This server cannot be deployed
Maintenance
Related MCP Connectors
Telegram bridge for your MCP-compatible agent. Bidirectional, no LLM in our stack.
Multi-tenant Telegram gateway for AI agents β HTTP+stdio, 8 tools, MTProto User API
Connect any AI agent to 1,000+ apps and 27,000+ actions through one remote MCP server (OAuth).
A paid remote MCP for OpenAI Codex agent coordination MCP, built to return verdicts, receipts, usage
Related MCP Servers
- AlicenseAqualityAmaintenanceEnables AI agents to delegate scoped tasks across multiple providers like Codex, Claude Code, and Antigravity through a single local MCP server, with quota monitoring, notifications, and remote control via messaging apps.132Apache 2.0
- FlicenseNot gradedqualityBmaintenanceEnables ChatGPT to operate a DreamAgent account via MCP, allowing creation and management of projects (Telegram/Discord bots, websites, schedulers) and conversational build/edit/debug through DreamAgent's AI agent.-
- AlicenseAqualityBmaintenanceEnables MCP-capable agents to create and invoke OpenServerless endpoints, manage secrets, and configure integrations such as S3, PostgreSQL, Redis, Milvus, and MongoDB.13Apache 2.0
- AlicenseNot gradedqualityBmaintenanceEnables LLM agents to remotely perform Termux/Android operations through a hosted MCP endpoint, including device status checks, shell command execution, and package management.15 npmMIT