tgsearch-mcp
Provides read-only Telegram search using a user's Telegram account over MTProto. It can find chats and public directory results, inspect chat metadata and forum topics, search messages within a chat or globally via Telegram server-side search, retrieve message history and context, and optionally build a local FTS5 index for faster repeated or offline search. It does not send, edit, delete, join, or leave chats.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@tgsearch-mcp search my Telegram chats for messages about the invoice"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
tgsearch-mcp
Read-only Telegram search for MCP hosts (Claude, VS Code, Cursor, …), built on your own user account over MTProto — find a chat, then find the message, fast.
Most Telegram MCP servers are download wrappers: to "search" they export history
and filter it locally. This server uses Telegram's server-side search
(messages.search, messages.searchGlobal), so a query over a 100k-message
channel returns in well under a second instead of minutes.
Why not tdl/tdl-mcp
|
| |
Search messages | export history + local filter |
|
Text messages | media-only unless | everything |
Public chat directory | no |
|
Connection | new | one persistent MTProto client |
Local index | no | optional SQLite FTS5 cache |
tdl remains a good companion for media downloads; this server is for search.
Related MCP server: Telegram Assistant MCP
Install
pipx install tgsearch-mcp # or: uv tool install tgsearch-mcpRequires Python ≥ 3.10 and a Python build with SQLite FTS5 (default on CPython from python.org, Homebrew and distro packages).
1. Create a Telegram app (one time)
Go to https://my.telegram.org → API development tools → create an app.
Note the api_id and api_hash.
Check them without logging in
From a source checkout (pipx users can skip this and pass --api-id/--api-hash
to login, or set TG_API_ID/TG_API_HASH):
cp .env.example .env # then fill TG_API_ID / TG_API_HASH
python scripts/check_api.pyscripts/check_api.py looks for the pair in CLI flags → environment → .env
→ config.json, connects to Telegram and proves the pair is accepted by
requesting a pre-login QR token (it expires unused: no code, message or
notification is sent). --save stores a validated pair in config.json so
tgsearch-mcp login and serve pick it up, --json is for scripts. Exit
codes: 0 ok, 1 rejected or network error, 2 not configured.
Fill in .env like this:
TG_API_ID=12345678
TG_API_HASH=0123456789abcdef0123456789abcdef2. Log in (one time)
tgsearch-mcp login # QR: Settings → Devices → Link Desktop Device
tgsearch-mcp login --phone +79991234567 # phone + code (+ 2FA password)You can also pass --api-id/--api-hash, set TG_API_ID/TG_API_HASH, or
run python scripts/check_api.py --save first to persist validated ones.
The session string is stored in $XDG_CONFIG_HOME/tgsearch-mcp/config.json
(~/.config/tgsearch-mcp/config.json) with mode 0600.
The account shows up in Telegram → Settings → Devices; revoke it there or with
tgsearch-mcp logout.
3. Register the server
Claude Code / Claude Desktop / VS Code / any mcpServers host:
{
"mcpServers": {
"telegram": {
"command": "tgsearch-mcp",
"args": ["serve"]
}
}
}serve is the default, so "args": [] works too. Use python -m tgsearch_mcp
if you installed the package into a specific interpreter.
Verify it works
No Telegram account needed:
pytest -q # unit tests: parsing, FTS5 index, tool catalog, docs sync
python scripts/smoke.py # offline: boots the server over stdio without credentials
tgsearch-mcp status # JSON: configured / logged_in / index
python scripts/verify.py # live: boots the server like a real hostscripts/smoke.py is what CI runs: empty temp directory, no TG_* variables,
no network — it checks the 9-tool catalog and that tg_status degrades softly.
scripts/verify.py prints the same catalog and calls tg_status; when the
session is authorized it also runs find → search → context. Exit codes:
0 ok, 1 protocol failure, 2 not logged in yet.
With a real account:
tgsearch-mcp login # QR or --phone
python scripts/verify.py --find quant --query "sharpe ratio"
python scripts/verify.py --chat @durov --query test # any public chatTo watch the raw JSON-RPC exchange, run tgsearch-mcp serve and pipe
newline-delimited initialize / tools/list / tools/call messages into it —
that is exactly what the script does.
Tools
Tool | What it does |
| Config, login, account and index health. Never errors. Start here. |
| Search your dialogs and the public directory. Returns |
| About, members, pinned message, forum flag, link, restrictions. |
| Forum topics of a supergroup (with |
| Server-side search inside one chat: query, kind, dates, sender, topic. |
| Search across every chat the account can see. |
| History by |
| Build/refresh the optional local FTS5 index for one chat. |
| Instant local search over indexed chats (works offline). |
chat accepts @username, https://t.me/... links, and numeric ids
(-100… works).
Typical agent flow:
tg_find_chats("quant trading") → ref: @quant_channel
tg_search_messages("@quant_channel", "backtest Sharpe")
tg_get_messages("@quant_channel", around_id=1234, limit=20) → contextEverything is read-only: there is no code path that sends, edits, deletes, joins or leaves a chat.
Local index (optional)
Server-side search is exact but capped by Telegram's own tokenizer. For large histories you query repeatedly, or for offline search:
tg_index_chat("@big_channel", limit=20000) # incremental on later runs
tg_search_index("шарп коэффициент") # prefix match, Russian-friendlyThe index lives in $XDG_DATA_HOME/tgsearch-mcp/index.sqlite3
(~/.local/share/tgsearch-mcp/index.sqlite3) and stores message text.
Delete it (or set TGSEARCH_INDEX) to keep text out of the default location.
Server-side search remains the primary path — the index is a cache, not a
mirror, and only covers chats you explicitly indexed.
Files
Follows the XDG Base Directory spec (XDG_* values must be absolute; relative
values are ignored):
Path | Contents |
|
|
| optional local FTS5 index ( |
TGSEARCH_HOME replaces both directories with a single one (handy for tests or
a portable setup), and TGSEARCH_INDEX overrides just the index file.
Configuration
Variable | Default | Meaning |
| — | Telegram app credentials |
| — | Session string (overrides |
|
| Config base ( |
|
| Data base ( |
| — | Fixed dir for both config and index |
|
| Local index path |
|
| Per-request timeout, seconds |
|
| Max dialogs fetched per listing |
|
| Include archived dialogs |
|
| Dialog cache TTL, seconds |
|
| Max chars stored per message |
Limitations
Private chats are searchable only if the account is a member (or has access via a folder/link). Private invite links are not auto-joined.
Secret chats (E2E) are not accessible through MTProto sessions.
contacts.searchmatches chat titles/usernames, not message content.searchGlobalcovers chats the account can reach — not arbitrary public channels you never opened. Indexing a public channel requires reading it.Telegram applies rate limits; the server surfaces
FloodWaitasretry_afterinstead of hanging, and caps concurrency.Automated crawling of many channels is against the spirit of Telegram's terms and can get an account limited. Keep ranges modest and prefer server-side search over mass indexing.
Development
python -m venv .venv && .venv/bin/pip install -e ".[dev]"
.venv/bin/pytest
.venv/bin/python scripts/smoke.py
.venv/bin/tgsearch-mcp status
.venv/bin/python -m buildTests cover query building, formatting/parsing, the FTS5 index, the MCP tool catalog and README/AGENTS.md sync; they never touch the network.
Agent rules live in AGENTS.md, which OpenCode and VS Code
Copilot read natively; there are no vendor-specific rule files.
Releases are tagged vX.Y.Z on main; .github/workflows/release.yml verifies
the tag and publishes to PyPI via trusted publishing.
Security
The session string is a full credential for your Telegram account — anyone
who has it can act as you. It is stored with mode 0600 under
$XDG_CONFIG_HOME/tgsearch-mcp, never logged, and never sent anywhere except
Telegram itself. Protect it like an SSH key.
License
MIT.
Available Tools
9 toolstg_find_chatsA
Find chats and return the exact ref to use in other tools.
Searches your dialogs by title/username/id and, for a non-empty query, the public Telegram directory. Public results include chats you have not joined; nothing is joined automatically.
Inputs: limit, query, scope, refresh. Returns: object
tg_get_chatA
Get one chat's details: about, member count, pinned message, forum flag, public link and access restrictions.
Inputs: chat (required). Returns: object
tg_get_messagesA
Read history or fetch specific messages.
Use ids for exact messages, around_id for context around a search hit,
before_id/after_id for pagination, and no cursor for the latest messages.
Results are oldest first and include next cursors.
Inputs: chat (required), ids, limit, after_id, +6 more. Returns: object
tg_index_chatA
Build or update the local SQLite FTS5 index for one chat.
Incremental by default: only messages newer than the indexed maximum are
fetched. Use it for large histories you query repeatedly, then search it
instantly with tg_search_index. On FloodWait the response carries
stopped.retry_after; re-run later to continue.
Inputs: chat (required), limit, reset, since_id. Returns: object
tg_list_topicsA
List forum topics of a supergroup; use the returned topic_id as
topic_id in tg_search_messages / tg_get_messages.
Inputs: chat (required), limit, query, offset_topic. Returns: object
tg_search_globalA
Search across all chats your account has access to.
Public channels you have not joined are NOT included: open such a channel with tg_search_messages instead.
Inputs: query (required), kind, limit, max_date, +6 more. Returns: object
tg_search_indexA
Search the local FTS5 index built by tg_index_chat.
Works without network when chat is a numeric id or a known username.
Returns highlighted snippets and links. This is a cache, not a substitute
for server-side search over unindexed chats.
Inputs: query (required), chat, mode, limit. Returns: object
tg_search_messagesA
Server-side full-text search inside one chat - the fast path for
"find X in this channel". Results are newest first, with snippet, link and
a next_offset_id cursor. For context around a hit use tg_get_messages
with around_id.
Inputs: chat (required), kind, limit, query, +6 more. Returns: object
tg_statusA
Check Telegram configuration, login state, account and local index.
Call this first. Never raises: when configuration or the session is missing it returns {ok:true, logged_in:false, hint}.
Inputs: no named parameters. Returns: object
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 9 tools
The three search tools (tg_search_messages, tg_search_global, tg_search_index) could initially look interchangeable, but the descriptions clearly scope each: one chat server-side, account-wide server-side, and local FTS5 cache. tg_get_messages vs tg_search_messages and tg_find_chats vs tg_get_chat are also cleanly differentiated by intent (discover vs read).
Every tool shares the tg_ prefix followed by a predictable verb_noun pattern (get_chat, find_chats, list_topics, search_messages, get_messages, index_chat, search_index). tg_status is the single noun-only exception but is idiomatic for a health/config probe.
Nine tools is well-scoped for a search/read-focused Telegram server, with each tool covering a distinct capability (status, lookup, topic listing, three search modes, history read, indexing). No obvious filler tools.
The search-and-read lifecycle is well covered: discover chats, inspect them, list topics, search per-chat/global/local-index, and paginate history. Minor gaps exist for write/side-effect actions (join a discovered public channel, send/reply, download media, list all dialogs without a query), but the stated search purpose is largely served.
Maintenance
Related MCP Connectors
Search your AI chat history (ChatGPT, Claude, Codex) from any MCP client. Remote, private, read-only
Search and inspect source-pinned agent skills, plugins and tools through a free public MCP.
Search MCP servers, agent skills and AI tools by task. Read-only, no API key.
Search MCP servers, MCP clients and AI agents, and retrieve listing details. Free, read-only access.
Related MCP Servers
- FlicenseNot gradedqualityBmaintenanceRead-only MCP access to a personal Telegram account. Allows querying chats, reading and searching messages via Streamable HTTP.-
- AlicenseNot gradedqualityBmaintenanceEnables read-only searching of a Telegram user account's personal chats and groups through four bounded, paginated MCP tools, with OAuth token validation and private session authentication.4Apache 2.0
- AlicenseNot gradedqualityCmaintenanceLets any MCP client list, read, and search chats in the user's own Telegram account through a read-only server, without sending, editing, or deleting messages.1MIT
- AlicenseNot gradedqualityBmaintenanceA read-only server that lets any MCP client or model list, search, and read your own Telegram chats, including chat details, forum topics, message history, and surrounding context. It never sends, edits, or deletes anything, and stores your Telegram credentials encrypted at rest.MIT