Skip to main content
Glama
prem-research

Telegram MCP Server

get_messages

Retrieve a specified count of messages from a Telegram entity by providing its ID, using the MCP server to facilitate communication with the Telegram platform.

Instructions

Get messages limited by a count from an entity

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
countNo
idYes

Implementation Reference

  • MCP tool handler for 'get_messages': registers and implements the tool by proxying HTTP requests to the backend API endpoint.
    @mcp.tool( name="get_messages", description="Get messages limited by a count from an entity" ) async def get_messages(id: int, count: int = 0) -> list[dict]: return get(f"{api_endpoint}get_messages/{id}", params={"count": count}).json()
  • Registration of the MCP tool named 'get_messages' using FastMCP @tool decorator.
    @mcp.tool( name="get_messages", description="Get messages limited by a count from an entity" )
  • Supporting HTTP API handler that fetches and formats messages from Telegram using Telethon client, called by the MCP tool.
    @app.get("/get_messages/{chat_id}") async def get_messages(chat_id: int, count: int = 0): entities = {} messages = [] e: Entity = await get_entity(entity=chat_id, raw=True) async for message in client.iter_messages(entity=e, reverse=False, limit=count): e_id: int = None if message.from_id is not None: e_id = message.from_id.user_id elif message.peer_id is not None: e_id = message.peer_id.user_id if e_id not in entities: entity = await client.get_entity(e_id) entities[e_id] = entity # Handle message text that might be binary message_text = message.message if isinstance(message_text, bytes): try: message_text = message_text.decode('utf-8', errors='replace') except Exception: message_text = '[Binary content]' messages += [ { "text": message_text, "date": message.date, "id": message.id, "from": await format_entity(entity=entities[e_id]), } ] await client.send_read_acknowledge(entity=chat_id, message=message) return messages

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/prem-research/telegram-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server