tgmcp

tgmcp

Enables AI agents to interact with Telegram accounts through MCP, supporting messaging, contacts, groups, media, and admin functions.

Category
访问服务器

README

TGMCP - Telegram Model Context Protocol

<div align="center"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Telegram_logo.svg/240px-Telegram_logo.svg.png" alt="Telegram Logo" width="120"/> <br> <b>Connect AI agents with Telegram using MCP standard</b> <br><br> <a href="https://pypi.org/project/tgmcp/"> <img src="https://img.shields.io/pypi/v/tgmcp.svg" alt="PyPI version"> </a> <a href="https://github.com/OEvortex/tgmcp/blob/main/LICENSE"> <img src="https://img.shields.io/github/license/OEvortex/tgmcp" alt="License"> </a> <a href="https://github.com/anthropics/anthropic-cookbook/tree/main/model_context_protocol"> <img src="https://img.shields.io/badge/protocol-MCP-blue" alt="MCP Protocol"> </a> <a href="https://deepwiki.com/OEvortex/tgmcp"> <img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki"> </a> <a href="https://mcpampel.com/repo/oevortex/tgmcp"> <img src="https://mcpampel.com/badge/oevortex/tgmcp.svg" alt="MCPAmpel Trust Score"> </a> </div>

📖 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

From PyPI (Recommended)

pip install tgmcp

From Source

# 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

# Optional: Tool Configuration
# Control which tool sets are enabled (default is true for all)
# TGMCP_ENABLE_CHAT_TOOLS=true
# TGMCP_ENABLE_CONTACT_TOOLS=true
# TGMCP_ENABLE_MESSAGE_TOOLS=true
# TGMCP_ENABLE_GROUP_TOOLS=true
# TGMCP_ENABLE_MEDIA_TOOLS=true
# TGMCP_ENABLE_PROFILE_TOOLS=true
# TGMCP_ENABLE_ADMIN_TOOLS=true

MCP Configuration

To use TGMCP with VS Code or other MCP-compatible applications, add it to your MCP configuration:

Option 1: Using Session String (Recommended)

{
  "mcp": {
    "servers": {
      "mcp-telegram": {
        "command": "tgmcp",
        "args": ["start"],
        "env": {
          "TELEGRAM_API_ID": "your_api_id",
          "TELEGRAM_API_HASH": "your_api_hash",
          "TELEGRAM_SESSION_STRING": "your_session_string",
          "MCP_NONINTERACTIVE": "true",
          
          "TGMCP_ENABLE_CHAT_TOOLS": "true",
          "TGMCP_ENABLE_CONTACT_TOOLS": "true",
          "TGMCP_ENABLE_MESSAGE_TOOLS": "true",
          "TGMCP_ENABLE_GROUP_TOOLS": "true",
          "TGMCP_ENABLE_MEDIA_TOOLS": "true",
          "TGMCP_ENABLE_PROFILE_TOOLS": "true",
          "TGMCP_ENABLE_ADMIN_TOOLS": "true"
        }
      }
    }
  }
}

IMPORTANT:

  1. Replace your_api_id, your_api_hash, and your_session_string with your actual Telegram API credentials.
  2. For most reliable operation, use the session string method (Option 1).
  3. The MCP_NONINTERACTIVE setting prevents interactive prompts during startup.

Option 2: Using Session Files (Basic Method)

{
  "mcp": {
    "servers": {
      "mcp-telegram": {
        "command": "tgmcp",
        "args": ["start"],
        "env": {
          "TELEGRAM_API_ID": "your_api_id",
          "TELEGRAM_API_HASH": "your_api_hash",
          "TELEGRAM_SESSION_NAME": "your_session_name",
          "MCP_NONINTERACTIVE": "true",
          
          "TGMCP_ENABLE_CHAT_TOOLS": "true",
          "TGMCP_ENABLE_CONTACT_TOOLS": "true",
          "TGMCP_ENABLE_MESSAGE_TOOLS": "true",
          "TGMCP_ENABLE_GROUP_TOOLS": "true",
          "TGMCP_ENABLE_MEDIA_TOOLS": "true",
          "TGMCP_ENABLE_PROFILE_TOOLS": "true",
          "TGMCP_ENABLE_ADMIN_TOOLS": "true"
        }
      }
    }
  }
}

Tool Configuration

TGMCP allows you to selectively enable or disable specific tool sets using environment variables. This can be useful for:

  • Reducing the number of available tools for security or simplicity
  • Limiting functionality for specific use cases
  • Improving performance by loading only necessary components

You can configure which tool sets are enabled by setting the following environment variables:

Environment Variable Description Default
TGMCP_ENABLE_CHAT_TOOLS Enable/disable chat-related tools true
TGMCP_ENABLE_CONTACT_TOOLS Enable/disable contact management tools true
TGMCP_ENABLE_MESSAGE_TOOLS Enable/disable message operation tools true
TGMCP_ENABLE_GROUP_TOOLS Enable/disable group administration tools true
TGMCP_ENABLE_MEDIA_TOOLS Enable/disable media handling tools true
TGMCP_ENABLE_PROFILE_TOOLS Enable/disable profile management tools true
TGMCP_ENABLE_ADMIN_TOOLS Enable/disable administrative function tools true

Set these variables to true or false to enable or disable the corresponding tool set. For example:

# Disable admin and group tools, enable everything else
TGMCP_ENABLE_ADMIN_TOOLS=false
TGMCP_ENABLE_GROUP_TOOLS=false

First-Time Authentication

Before using TGMCP, you need to authenticate with Telegram. You can do this in two ways:

Option 1: Generate a Session String (Recommended)

# Run the session string generator
python -m tgmcp.session_string_generator

This will generate a session string that you can use in your MCP configuration. This method is more reliable and portable, especially for cloud environments.

Option 2: Use File-Based Authentication

# Run the authentication script
python -m tgmcp.authenticate

This will create a .session file that TGMCP will use for authentication.

🔍 Usage

Run as MCP Server

python -m tgmcp

Use in Your Code

import asyncio
import os
from tgmcp import client, get_tool_config

async def main():
    # Optional: Configure which tools to enable
    # These should be set before importing any tools
    os.environ["TGMCP_ENABLE_ADMIN_TOOLS"] = "false"  # Disable admin tools
    
    # 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!')
    
    # Check which tool sets are enabled
    tool_config = get_tool_config()
    print("Enabled tool sets:", [k for k, v in tool_config.items() if v])
    
    # Disconnect when done
    await client.disconnect()

asyncio.run(main())

🛠️ Available MCP Tools

TGMCP provides a comprehensive set of tools that can be utilized through the MCP protocol:

Chat Tools

  • get_chats - List available chats with pagination
  • list_chats - Detailed metadata for users, groups, and channels
  • get_chat - Information about a specific chat
  • get_direct_chat_by_contact - Find a direct chat with a specific contact
  • send_message - Send text messages to any chat
  • archive_chat - Archive a chat
  • unarchive_chat - Unarchive a chat
  • mute_chat - Mute notifications for a chat
  • unmute_chat - Unmute notifications for a chat
  • get_invite_link - Get the invite link for a group or channel
  • export_chat_invite - Export a chat invite link
  • join_chat_by_link - Join a chat by invite link
  • import_chat_invite - Import a chat invite by hash

Message Operations

  • get_messages - Get paginated messages from a specific chat
  • list_messages - List messages with detailed information
  • get_message_context - Get context around a specific message
  • forward_message - Forward a message to another chat
  • edit_message - Edit a previously sent message
  • delete_message - Delete messages
  • pin_message - Pin a message in a chat
  • unpin_message - Unpin a message from a chat
  • mark_as_read - Mark messages as read
  • reply_to_message - Reply to a specific message
  • search_messages - Search for messages in a chat
  • get_pinned_messages - Get all pinned messages in a chat
  • get_history - Get chat history with customizable filters

Contact Management

  • list_contacts - View all contacts in your Telegram account
  • search_contacts - Find contacts by name, username, or phone
  • get_contact_ids - Get contact IDs for specific contacts
  • get_contact_chats - Get chats with specific contacts
  • get_last_interaction - Get last interaction with a contact
  • add_contact - Add new contacts to your Telegram account
  • delete_contact - Delete contacts from your account
  • block_user - Block users from contacting you
  • unblock_user - Unblock previously blocked users
  • import_contacts - Import multiple contacts at once
  • export_contacts - Export all contacts to a structured format
  • get_blocked_users - Get a list of blocked users
  • resolve_username - Resolve a username to a Telegram entity
  • search_public_chats - Search for public chats

Group Administration

  • create_group - Create new groups and add members
  • create_channel - Create a new broadcast channel
  • invite_to_group - Invite users to existing groups
  • leave_chat - Leave a group or channel
  • get_participants - List members of a group or channel
  • edit_chat_title - Change the title of a group or channel
  • edit_chat_photo - Change the photo of a group or channel
  • delete_chat_photo - Remove the photo from a group or channel
  • promote_admin - Give administrator privileges to users
  • demote_admin - Revoke administrator privileges
  • ban_user - Ban users from a group or channel
  • unban_user - Unban previously banned users
  • get_admins - List administrators of a group or channel
  • get_banned_users - List banned users
  • get_recent_actions - Get recent administrative actions

Media Tools

  • send_file - Send documents, photos, or videos
  • download_media - Save media from messages to your device
  • get_media_info - Get information about media in messages
  • send_voice - Send voice messages
  • send_sticker - Send stickers to chats
  • get_sticker_sets - Get available sticker sets
  • get_gif_search - Search for GIFs
  • send_gif - Send GIFs to chats

Profile Management

  • get_me - Get information about your own account
  • update_profile - Update profile information (name, bio)
  • set_profile_photo - Set a new profile photo
  • delete_profile_photo - Remove profile photos
  • get_privacy_settings - Get current privacy settings
  • set_privacy_settings - Update privacy settings
  • get_user_photos - Get a user's profile photos
  • get_user_status - Check a user's online status
  • get_bot_info - Get information about a bot
  • set_bot_commands - Configure bot commands

📚 Example

import asyncio
import os
from dotenv import load_dotenv
from tgmcp import client, get_tool_config

# Load environment variables
load_dotenv()

# Optional: Configure which tools to enable
# os.environ["TGMCP_ENABLE_ADMIN_TOOLS"] = "false"  # Uncomment to disable admin tools

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})")
    
    # Display which tool sets are enabled
    tool_config = get_tool_config()
    print("\nEnabled tool sets:")
    for tool_set, enabled in tool_config.items():
        status = "✅ Enabled" if enabled else "❌ Disabled"
        print(f"- {tool_set}: {status}")
    
    # 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

MIT License

📞 Support

For issues and questions, please open an issue on the GitHub repository.

📦 PyPI Package

This package is available on PyPI:

  • https://pypi.org/project/tgmcp/

You can install it using pip:

pip install tgmcp

<div align="center"> <p>Made with ❤️ for AI-assisted Telegram interaction</p> </div>

推荐服务器

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 多个工具。

官方
精选
本地
Kagi MCP Server

Kagi MCP Server

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

官方
精选
Python
graphlit-mcp-server

graphlit-mcp-server

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

官方
精选
TypeScript
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 模型以安全和受控的方式获取实时的网络信息。

官方
精选