Telegram Model Context Protocol integration
Project description
TGMCP - Telegram Model Context Protocol
Connect AI agents with Telegram using MCP standard
📖 Overview
TGMCP is a Python package that implements the Model Context Protocol (MCP) for Telegram. It allows AI agents to seamlessly interact with Telegram accounts, providing access to messaging, contacts, groups, media sharing, and more.
This package acts as a bridge between AI assistants and Telegram, enabling them to:
- Read and send messages
- Manage contacts and groups
- Handle media (images, documents, stickers, GIFs)
- Perform administrative functions
- Update profile information
All data is handled locally and securely through the Telegram API.
✨ Key Features
- Chat Operations: List chats, retrieve messages, send messages
- Contact Management: Add, delete, block/unblock, search contacts
- Group Administration: Create groups, add members, manage permissions
- Media Handling: Send/receive files, stickers, GIFs, voice messages
- Profile Management: Update profile info, privacy settings
- Message Operations: Forward, edit, delete, pin messages
- Administrative Functions: Promote/demote admins, ban users
🚀 Installation
Prerequisites
- Python 3.8 or higher
- Telegram API credentials (obtain here)
- UV package installer (recommended)
Install with UV (Recommended)
# Clone the repository
git clone https://github.com/OEvortex/tgmcp.git
cd tgmcp
# Install with UV
uv pip install -e .
Traditional Install
# Clone the repository
git clone https://github.com/OEvortex/tgmcp.git
cd tgmcp
# Install with pip
pip install -e .
⚙️ Configuration
Environment Variables
Create a .env file in your project directory with your Telegram API credentials:
TELEGRAM_API_ID=your_api_id
TELEGRAM_API_HASH=your_api_hash
TELEGRAM_SESSION_NAME=your_session_name
# Optional: Use string session instead of file session
# TELEGRAM_SESSION_STRING=your_session_string
MCP Configuration
To use TGMCP with other MCP-compatible applications, add it to your MCP configuration file:
{
"mcpServers": {
"mcp-telegram": {
"command": "tgmcp", /* use full path if client can't find the command (e.g. "/usr/local/bin/tgmcp"). See IMPORTANT section below for full path instructions. */
"args": ["start"],
"env": {
"TELEGRAM_API_ID": "<your_api_id>",
"TELEGRAM_API_HASH": "<your_api_hash>"
}
}
}
}
IMPORTANT: To find the full path to the tgmcp command:
- Windows:
where tgmcpin Command Prompt orGet-Command tgmcpin PowerShell - macOS/Linux:
which tgmcpin Terminal
🔍 Usage
Run as MCP Server
python -m tgmcp
Use in Your Code
import asyncio
from tgmcp import client
async def main():
# Connect to Telegram
await client.start()
# Get user info
me = await client.get_me()
print(f"Logged in as: {me.first_name} {getattr(me, 'last_name', '')}")
# Send a message
await client.send_message('username', 'Hello from TGMCP!')
# Disconnect when done
await client.disconnect()
asyncio.run(main())
🛠️ Tools Available
Chat Tools
get_chats- Get a paginated list of all your Telegram chatslist_chats- List available chats with detailed metadata (users, groups, channels)get_chat- Get detailed information about a specific chat by IDget_direct_chat_by_contact- Find direct chats with a specific contact by name/username/phonesend_message- Send a text message to any chatarchive_chat- Archive a chat to reduce clutterunarchive_chat- Unarchive a previously archived chatmute_chat- Mute notifications for a specific chatunmute_chat- Unmute previously silenced chat notificationsget_invite_link- Get an invite link for a group or channelexport_chat_invite- Create and export a new chat invite linkjoin_chat_by_link- Join a chat using an invite linkimport_chat_invite- Import a chat invite by hash code
Contact Tools
list_contacts- List all contacts in your Telegram accountsearch_contacts- Search for contacts by name, username, or phone numberget_contact_ids- Get all contact IDs in your accountget_contact_chats- List all chats involving a specific contactget_last_interaction- Get the most recent messages with a contactadd_contact- Add a new contact to your Telegram accountdelete_contact- Delete a contact from your address bookblock_user- Block a user by their user IDunblock_user- Unblock a previously blocked userimport_contacts- Import a list of contacts in batchexport_contacts- Export all contacts as a JSON stringget_blocked_users- Get a list of all users you've blockedresolve_username- Resolve a username to a user or chat IDsearch_public_chats- Search for public chats, channels, or bots
Message Tools
get_messages- Get paginated messages from a specific chatlist_messages- Retrieve messages with optional filters (search, date)get_message_context- See messages before and after a specific messageforward_message- Forward a message from one chat to anotheredit_message- Edit a message you previously sentdelete_message- Delete a message by IDpin_message- Pin an important message in a chatunpin_message- Unpin a previously pinned messagemark_as_read- Mark all messages as read in a chatreply_to_message- Reply to a specific message in a chatget_history- Get full chat history up to a limitsearch_messages- Search for messages in a chat by textget_pinned_messages- Get all pinned messages in a chat
Group & Channel Tools
create_group- Create a new group and add initial memberscreate_channel- Create a new channel or supergroupinvite_to_group- Invite users to a group or channelleave_chat- Leave a group or channelget_participants- List all participants in a group or channeledit_chat_title- Change the title of a chat, group, or channeledit_chat_photo- Change the photo of a chat, group, or channeldelete_chat_photo- Remove the photo from a chatpromote_admin- Promote a user to admin with custom rightsdemote_admin- Remove admin status from a userban_user- Ban a user from a group or channelunban_user- Unban a previously banned userget_admins- Get all admins in a group or channelget_banned_users- Get a list of all banned usersget_recent_actions- View recent admin actions in a group
Media Tools
send_file- Send any file (document, photo, video) to a chatdownload_media- Download media from a message to local storageget_media_info- Get detailed information about media in a messagesend_voice- Send a voice message (OGG/OPUS format)send_sticker- Send a sticker to a chatget_sticker_sets- Get a list of available sticker setsget_gif_search- Search for GIFs by query termsend_gif- Send a GIF to a chat by its document ID
Profile Tools
get_me- Get information about your own Telegram userupdate_profile- Update your profile information (name, bio)set_profile_photo- Set a new profile photodelete_profile_photo- Delete your current profile photoget_privacy_settings- View your privacy settingsset_privacy_settings- Update privacy settings (last seen, phone, etc.)get_user_photos- Get profile photos of any userget_user_status- Check a user's online statusget_bot_info- Get information about a botset_bot_commands- Set commands for bots you own
Admin Tools
promote_admin- Promote a user to admin in a group/channeldemote_admin- Demote a user from admin statusban_user- Ban a user from a group or channelunban_user- Unban a previously banned userget_admins- Get all admins in a group or channelget_banned_users- Get all banned users in a group or channelleave_chat- Leave a group or channelexport_chat_invite- Generate a new invite linkimport_chat_invite- Join a chat using an invite hashget_recent_actions- View recent administrative actions
📚 Example
import asyncio
import os
from dotenv import load_dotenv
from tgmcp import client
# Load environment variables
load_dotenv()
async def example():
# Start the client
await client.start()
# Get recent chats
dialogs = await client.get_dialogs(limit=5)
print("\nRecent chats:")
for dialog in dialogs:
chat_name = getattr(dialog.entity, "title", None) or getattr(dialog.entity, "first_name", "Unknown")
print(f"- {chat_name} (ID: {dialog.entity.id})")
# Disconnect when done
await client.disconnect()
if __name__ == "__main__":
asyncio.run(example())
🔒 Security & Privacy
- All data remains on your local machine
- Authentication with Telegram is handled securely
- No data is sent to third parties
- Session files should be kept secure
🤝 Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
📄 License
📞 Support
For issues and questions, please open an issue on GitHub.
Made with ❤️ for AI-assisted Telegram interaction
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tgmcp-0.1.0.tar.gz.
File metadata
- Download URL: tgmcp-0.1.0.tar.gz
- Upload date:
- Size: 28.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69c95a605d5e3cff8be84e8ac3f37a328d8f9316d804e6323430af93eb68c940
|
|
| MD5 |
b8fe41a6b8d01c8d2d802a83aebbdb72
|
|
| BLAKE2b-256 |
2412fa88cc9fdf70627741282ca5273548f37c6b785229978b5e16426a895fff
|
File details
Details for the file tgmcp-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tgmcp-0.1.0-py3-none-any.whl
- Upload date:
- Size: 29.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28977ecc3f5023cc09ddf5b6352ce52c8f6e653129a58f0129df798d9f4f72ad
|
|
| MD5 |
92f73127f026b672ef4ceeebbd0afad2
|
|
| BLAKE2b-256 |
a5dce5aa501336d97ba0e8fb5ca8b1ae426fb5a8e16fca72f10e26c0a9b604a6
|