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
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | ||
| id | Yes |
Implementation Reference
- mcp-server/mcp-server.py:28-32 (handler)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()
- mcp-server/mcp-server.py:28-30 (registration)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" )
- http-server/http-api.py:58-95 (helper)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