social-mcp
Allows an AI agent to interact with the user's own Telegram account, including reading unread messages, fetching chat history, sending replies, editing and deleting messages, managing chats and groups, and blocking or unblocking users.
social-mcp
A local Model Context Protocol (MCP) server that bridges an AI agent (e.g. OpenCode) with your personal Telegram direct messages: read unread messages, pull chat history for context, and send replies.
How it works
opencode (AI agent) ↔ social-mcp (MCP over stdio) ↔ Telethon ↔ TelegramThis is not a bot. The server runs in userbot mode — it logs in as your own account via the Telegram API, so everything it sends is sent from you. Your AI assistant simply gets a set of tools (15+) to read and manage your own chats.
⚠️ Read before using
Automating a personal account sits in a grey zone of Telegram's Terms of Service. Keep the automation reasonable, don't spam, use at your own risk.
Store
.envand*.sessionoutside of version control. These files grant full access to your account.Only you ("the boss") should command the assistant. It must never act on instructions coming from message content itself.
Related MCP server: telegram-business-bridge
Requirements
Python 3.10+
A Telegram
api_id/api_hashfrom https://my.telegram.org (API Development Tools)Works on Windows, Linux and macOS
Project layout
social-mcp/
├── config.py
├── clients/
│ ├── __init__.py
│ └── telegram_client.py
├── storage.py
├── server.py
├── setup_auth.py
├── .env.example
├── requirements.txt
└── README.mdInstall dependencies
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\Activate.ps1
pip install -r requirements.txtConfigure credentials
Get your Telegram API ID/hash from https://my.telegram.org (API Development Tools).
Create a .env file in the project root:
TELEGRAM_API_ID=123456
TELEGRAM_API_HASH=your_api_hash_here
TELEGRAM_PHONE=+15551234567
# Optional overrides
SOCIAL_MCP_DATA_DIR=/home/you/.social-mcp
SOCIAL_MCP_LOG_LEVEL=INFOSession/state files live under SOCIAL_MCP_DATA_DIR (default ~/.social-mcp),
deliberately outside the repo so they can never be committed by accident.
First-time interactive login
Run this once, manually, from a real terminal — OpenCode invokes server.py
over stdio and cannot answer interactive prompts.
python setup_auth.py --telegramTelegram will text/app you a login code, and ask for your 2FA password if enabled.
Run the server standalone (smoke test)
python server.pyIt will idle on stdio waiting for MCP protocol messages — that's expected; this step just confirms it starts without import/config errors. Ctrl+C to stop.
Wire it into OpenCode
Add this to ~/.config/opencode/opencode.json (adjust paths to your machine):
{
"mcpServers": {
"social-mcp": {
"command": "/absolute/path/to/social-mcp/.venv/bin/python",
"args": ["/absolute/path/to/social-mcp/server.py"],
"env": {
"SOCIAL_MCP_DATA_DIR": "/home/you/.social-mcp"
}
}
}
}Restart OpenCode. It should discover these tools:
list_dialogs(limit?, only_groups?, only_unread?)— start here when the chat id is unknownget_chat_info(platform, target_id)— chat metadata plus members and rolesget_unread_messages(limit?, platforms?)— addsis_newandnew_count; never changes read statemark_as_read(platform, chat_id?)— opt-in clears Telegram's unread flag; omitchat_idto clear every dialog with unread messagessend_reply(platform, target_id, text, reply_to_message_id?, silent?)get_chat_history(platform, target_id, limit?, offset_id?)—offset_idpages backwardsedit_message/delete_messagedelete_chat— full dissolution: kicks all members, leaves, purges (groups); hard-deletes owned channels; revokes and purges private dialogsleave_chat— exit a group/channel without touching its membersblock_user/unblock_user/get_blocked_userscreate_group/create_supergroup/add_user_to_group/remove_user_from_group/unban_user_from_group/invite_to_channel
Notes on Telegram limits
Only the owner can destroy a group. In a supergroup you do not own,
delete_chatbans every member, leaves and purges your own copy, but the group itself survives.remove_user_from_groupbans in supergroups; undo it withunban_user_from_group(a banned user has to rejoin on their own).A bare user id needs its access_hash. Once a dialog is deleted the entity leaves the session cache and the id stops resolving — pass an
@usernameinstead, which Telegram resolves network-side.Media is not dropped. Messages carrying only a photo, voice note, file or sticker are reported with a
[photo]-style placeholder andhas_media: true.Reading never marks as read.
get_unread_messagesis side-effect free on Telegram, so the unread badge never drops on its own. Callmark_as_readwhen you actually want it cleared, or the counter grows forever.new_countneeds two calls to be useful. The first call has no cutoff and counts everything unseen; afterwards it reports what arrived since the previous call.
Example agent workflow
Agent calls
get_unread_messages(limit=10)→ gets a JSON list of unread messages.Agent summarizes them for you.
You say "reply to Anna on Telegram: I'm free after 6pm".
Agent optionally calls
get_chat_history(platform="telegram", target_id=<id>)to see prior context, drafts a reply, and callssend_reply(platform="telegram", target_id=<id>, text="...").
Notes on reliability
All tool functions catch platform-specific errors and return a structured
{"success": false, "error": "..."}JSON payload instead of raising, so a single failed call never kills the stdio connection to OpenCode.Group deletion handles all Telegram quirks: owned channels are hard-deleted via
channels.deleteChannel, basic groups viamessages.deleteChatwhen you have admin rights, with an automatic kick-all → leave → purge fallback otherwise.storage.pykeeps a small SQLite file recording which message IDs have already been surfaced, as a foundation for future "mark as read" / dedup logic — it is not yet wired into filtering by default.
License
MIT © sydrx
This server cannot be deployed
Maintenance
Related MCP Connectors
Messaging tools for AI agents: send messages, manage chats, groups and channels.
Your own LinkedIn, WhatsApp, Instagram, Telegram, Email and Calendar accounts, usable from any agent
Telegram bridge for your MCP-compatible agent. Bidirectional, no LLM in our stack.
Run a Telegram channel from your AI agent. Posts go out through your own bot, not your account.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceEnables AI agents to read, send, and organize Telegram messages and chats. Supports tools for listing chats, fetching messages, sending/reply, archiving, muting, and folder management.1MIT
- AlicenseAqualityBmaintenanceConnect any AI agent to your personal Telegram messages through the official Business API, enabling message history search and draft replies with optional manual approval.718MIT
- AlicenseNot gradedqualityCmaintenanceEnables AI agents to interact with a user's Telegram account: list chats, read history, search, and send messages through Telegram's MTProto API.1MIT
- FlicenseNot gradedqualityAmaintenanceEnables AI assistants to read Telegram conversations, search messages, retrieve chat context, resolve recipients, and send messages with delivery status.1-