tg-mcp-spy
Caches Telegram channel posts locally in SQLite and exposes them via MCP tools for querying and management.
README
tg-mcp-spy
A Python MCP (Model Context Protocol) server that caches Telegram channel posts in a local SQLite database and exposes them via MCP tools. It connects to Telegram through a user session (Telethon) and mirrors the user's broadcast-channel subscriptions into a queryable local cache.
Quick start
1. Install dependencies
uv sync --all-extras
2. Set environment variables
export TELEGRAM_API_ID=your_api_id
export TELEGRAM_API_HASH=your_api_hash
export TELEGRAM_SESSION_STRING=your_session_string
The session string must be generated externally (e.g. via Telethon's interactive login). Optional variables:
| Variable | Default | Description |
|---|---|---|
TGMCPSPY_DB_PATH |
tgmcpspy.db |
Path to the SQLite database |
TGMCPSPY_POST_TTL_DAYS |
90 |
Days to retain cached posts |
3. Run the server
python -m package_tgmcpspy.server
The server binds to 127.0.0.1:8000 by default.
MCP Tools
| Tool | Description |
|---|---|
list_tracked_channels |
List all locally tracked channels |
add_channel(channel) |
Add a channel to the local tracked list |
remove_channel(channel) |
Remove a channel from the local tracked list |
sync_dialogs |
Sync tracked channels with your Telegram subscriptions |
update_channel(channel) |
Fetch latest posts for a single channel |
update_all_channels |
Fetch latest posts for all tracked channels |
get_post(channel, post_id) |
Get a specific cached post |
list_channel_posts(channel, start_date, end_date) |
List posts from one channel by date range |
list_all_posts(start_date, end_date) |
List posts from all tracked channels by date range |
Channel identifiers accept a Telegram username or numeric id (including -100... format). Dates accept YYYY-MM-DD or ISO timestamps, interpreted as UTC.
MCP Resources
| URI | Description |
|---|---|
channel://list |
Tracked channels (placeholder; use the tool for live data) |
post://{channel}/{post_id} |
Single cached post (placeholder; use the tool for live data) |
MCP Prompt
| URI | Description |
|---|---|
channel_digest://{channel} |
Summarize recent posts from a channel (default: last 7 days) |
Development
make format # Format code with ruff
make lint # Run ruff linter
make typecheck # Run mypy
make test # Run pytest
make check # Run format-check + lint + typecheck + test
Architecture
src/package_tgmcpspy/
models.py — domain dataclasses, exceptions, identifier normalization
config.py — environment-based configuration loading
db.py — SQLAlchemy Core schema + async repository
telegram.py — Telethon wrapper with FloodWait retry
server.py — FastMCP application, lifespan, tools, resources, prompts
All MCP tool calls are processed sequentially. Cached posts are immutable — edits and deletions on Telegram are ignored. Posts older than the configured TTL are purged automatically.
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.