Sponsored by Deepsite.site

MCP Integration example with Telegram

Created By
arananeta year ago
A Python-based Model Context Protocol (MCP) client and server that enables LLMs to send notifications via Telegram and receive user responses.
Content

MCP Integration example with Telegram

A Python-based Model Context Protocol (MCP) client and server that enables LLMs to send notifications via Telegram and receive user responses.

Features

  • Send text alerts to a Telegram chat with customizable urgency levels
  • Check for user replies with configurable timeouts
  • Flexible template for integration with MCP-compatible LLM applications

MCP vs Function Calls or Agent tools

MCP Protocol (Model Context Protocol):

  • Separates the LLM’s reasoning (the “context”) from the execution of external actions. For example, the LLM decides what action to take and the MCP server handles how to execute that action.
  • This decoupling means you can update or swap out external tools without changing the core LLM model or its prompts.
  • It also allows for safer and more controlled execution of actions.

LLM Function Call or Agent:

  • Direct function calls or agent-based methods often mix decision-making and execution logic, which can make the system less modular.
  • Changes in how an action is performed may require reengineering the agent or modifying the LLM’s prompts, making maintenance harder.

Prerequisites

  • Python 3.11+
  • A Telegram bot token (from @BotFather)
  • Your Telegram chat ID

Installation

  1. Clone the repository:
    git git@github.com:arananet/mcp-telegram-poc.git
    cd mcp-telegram-poc
    
  2. Install dependencies:
    pip install -r requirements.txt
    
  3. Create the virtual environment:
    python3 -m venv .venv
    
  4. Copy .env.example to .env and fill in your Telegram credentials:
    cp .env.example .env
    nano .env
    

Configuration

Edit .env with:

  • TELEGRAM_BOT_TOKEN=your_bot_token_here
  • TELEGRAM_CHAT_ID=your_chat_id_here
  • OPENAI_API_KEY=your_openai_apikey

Getting a Telegram Bot Token

  1. Chat with @BotFather on Telegram.
  2. Send /newbot and provide a name (e.g. mcp_example) and then provide an user name (e.g. mcp_example_bot) after this telegram botfather will reply with the token.
  3. Copy the token provided.

Finding Your Chat ID

  1. Send a message to your bot (go to the created bot and send some example, e.g. hi).
  2. Visit https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates.
  3. Find the chat object's id field. You will see something like this (e.g. "chat":{"id":107184517,"first_name":"Edu","last_name":"Arana"...) the Chat ID is 107184517.

Usage

Running Standalone

python3 src/mcp_handler.py

Programmatic Usage

For testing sending and receiving an alert:

python3 examples/test_client.py

Agent integration

For testing the integration of the MCP using an agent with autogen 0.4 framework

python3 examples/test_autogen_agent.py

Testing Locally

Use the test script (Linux/macOS):

chmod +x test_server.sh
./test_server.sh

Available Tools

send_alert

  • Parameters:
    • message (str, required): Message to send (Markdown supported).
    • project (str, required): Project name.
    • urgency (str, optional): "low", "medium", "high" (default: "medium").
  • Description: Sends a Telegram alert and waits for a reply.

check_reply

  • Parameters:
    • message_id (int, required): Message ID to check.
    • timeout_seconds (int, optional): Wait time (default: 60).
  • Description: Polls for replies to a specific message.

Development

  • Install dependencies: pip install -r requirements.txt
  • Run server: python3 src/mcp_handler.py
  • Test client: python3 examples/test_client.py
  • Test Agent: python3 examples/test_autogen_agent.py

Examples

The examples folder contains:

  • test_server.py: Runs the server standalone.

  • test_client.py: Demonstrates client interaction.

  • test_autogen_agent.py: Demostrates the MCP integration between an AutoGen Agent

  • In the images folder you can find some screenshots of the testings.

Developer

  • Eduardo Arana
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
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.
ChatWiseThe second fastest AI chatbot™
Tavily Mcp
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
CursorThe AI Code Editor
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Serper MCP ServerA Serper MCP Server
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
DeepChatYour AI Partner on Desktop
Playwright McpPlaywright MCP server
Y GuiA web-based graphical interface for AI chat interactions with support for multiple AI models and MCP (Model Context Protocol) servers.
Amap Maps高德地图官方 MCP Server
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"
RedisA Model Context Protocol server that provides access to Redis databases. This server enables LLMs to interact with Redis key-value stores through a set of standardized tools.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
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.
WindsurfThe new purpose-built IDE to harness magic