Sponsored by Deepsite.site

MCP Telegram Server

Created By
leshchenko19798 months ago
Telegram MCP Server - now you can talk to your Telegram from Cursor
Content

MCP Telegram Server

A powerful MCP server implementation that provides Telegram functionality through a clean API interface, including message search, sending, and chat management capabilities.

Join our Telegram Discussion Group for support, updates, and community discussions.

Features

  • Message search with multiple modes:
    • Basic search by text
    • Advanced search with custom filters
    • Pattern-based search using regex
  • Chat management:
    • List available dialogs
    • Send messages with optional reply support
    • Generate message links
  • Analytics and data:
    • Chat statistics and analytics
    • Chat data export functionality
  • Robust error handling and logging
  • Built on MCP (Model Control Protocol) architecture

Prerequisites

  • Python 3.x
  • Telegram API credentials (API ID, API Hash)
  • MCP-compatible environment (like Cursor IDE)

Installation

  1. Clone the repository and navigate to the project directory:
git clone https://github.com/leshchenko1979/tg_mcp.git
cd tg_mcp
  1. Install dependencies:
pip install -r requirements.txt
  1. Create a .env file in the root directory with your Telegram credentials:
API_ID=your_api_id
API_HASH=your_api_hash
PHONE_NUMBER=+123456789
  1. Run the setup script to authenticate with Telegram:
python setup_telegram.py

This will create a session file (mcp_telegram_search.session) that stores your Telegram session data.

Cursor Configuration

To use this server with Cursor IDE:

  1. Create an mcp.json file in your .cursor directory with the following content:
{
  "mcpServers": {
    "mcp-telegram": {
      "command": "cmd /c set PYTHONPATH=%PYTHONPATH%;<path_to_server> && mcp run <path_to_server>/src/server.py",
      "description": "Telegram MCP server"
    }
  }
}

Note: Replace <path_to_server> with the absolute path to your installation directory.

  1. Ensure your .env file is properly configured as described in the Installation section.

  2. The server will automatically connect to Cursor when you open the project, making all Telegram tools available through the IDE.

Note: If you modify the server code, you'll need to reload the server in Cursor for changes to take effect.

Usage

The server can be run using MCP:

mcp run <path_to_server>/server.py

Available Tools

The server provides the following MCP tools:

  • search_messages(query: str, chat_id: str = None, limit: int = 20, min_date: str = None, max_date: str = None)

    • Search for messages in Telegram chats
    • Supports both global search and chat-specific search
    • Dates should be in ISO format
  • advanced_search(query: str, filters: str = None, date_range: str = None, chat_ids: str = None, message_types: str = None, limit: int = 20)

    • Advanced search with filtering options
    • filters, date_range should be JSON strings
    • chat_ids, message_types should be comma-separated strings
  • pattern_search(pattern: str, chat_ids: List[str] = None, pattern_type: str = None, limit: int = None)

    • Search messages using regex patterns
    • Supports multiple chat IDs
    • Optional pattern type specification
  • send_telegram_message(chat_id: str, message: str, reply_to_msg_id: int = None, parse_mode: str = None)

    • Send messages to Telegram chats
    • Supports replying to messages
    • Optional markdown/HTML parsing
  • get_dialogs(limit: int = 50)

    • List available Telegram dialogs
    • Returns chat IDs, names, types, and unread counts
  • get_statistics(chat_id: str)

    • Get statistics for a specific chat
  • generate_links(chat_id: str, message_ids: list[int])

    • Generate Telegram links for messages
  • export_data(chat_id: str, format: str = "json")

    • Export chat data in specified format

Project Structure

tg_mcp/
├── src/                # Source code directory
│   ├── client/        # Telegram client management
│   ├── config/        # Configuration settings
│   ├── monitoring/    # Monitoring and health checks
│   ├── tools/         # MCP tool implementations
│   ├── utils/         # Utility functions
│   ├── __init__.py    # Package initialization
│   └── server.py      # Main server implementation
├── tests/             # Test directory
├── logs/              # Log files directory
├── setup_telegram.py  # Telegram setup script
├── setup.py          # Package setup configuration
├── requirements.txt  # Project dependencies
├── .env             # Environment variables (create this)
├── .gitignore       # Git ignore patterns
└── LICENSE          # MIT License

Note: *.session and *.session-journal files will be created after authentication

Dependencies

The project relies on the following main packages:

loguru          # Logging
aiohttp         # Async HTTP
mcp[cli]        # Model Control Protocol
telethon>=1.34.0  # Telegram client
python-dotenv>=1.0.0  # Environment management

License

This project is licensed under the MIT License - see the LICENSE file for details.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
WindsurfThe new purpose-built IDE to harness magic
Amap Maps高德地图官方 MCP Server
CursorThe AI Code Editor
DeepChatYour AI Partner on Desktop
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
ChatWiseThe second fastest AI chatbot™
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Serper MCP ServerA Serper MCP Server
Playwright McpPlaywright MCP server
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Tavily Mcp
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.