Skip to content

Commit 7b19b95

Browse files
artgas1claude
andcommitted
fix: report MTProto schema drift instead of a generic error code
When the server sends an object whose constructor the installed Telethon schema does not know, Telethon raises TypeNotFoundError. Today that is swallowed by log_and_format_error and surfaces as An error occurred (code: CHAT-ERR-418). Check mcp_errors.log for details. which is indistinguishable from "no such user/chat". That confusion is expensive, because schema drift does not fail cleanly: an unknown constructor desynchronises the whole read buffer, so only some calls break while their neighbours keep working. In our case list_chats, get_common_chats, resolve_username and get_full_user were failing while get_chats, search_contacts, list_messages and send_message were fine — which reads exactly like "that user does not exist". The reported constructor id is often garbage from the desynchronised stream and appears in no schema at all, so searching for it leads nowhere too. This names the condition, states that the data did arrive and only parsing failed, and points at the fix. Every other error keeps the existing format. Tested: both branches covered; the new test was run against a sabotaged implementation first and fails there, so it can actually catch a regression. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent a7bdd4d commit 7b19b95

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎telegram_mcp/runtime.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,31 @@ def log_and_format_error(
732732
if user_message:
733733
return user_message
734734

735+
# MTProto schema drift must not hide behind the generic code. Telethon releases lag
736+
# behind production Telegram, and when the server sends an object whose constructor
737+
# the installed schema does not know, the read buffer desynchronises: some tools fail
738+
# while their neighbours keep working. Reported as a generic error, that pattern is
739+
# indistinguishable from "no such user/chat" and sends debugging the wrong way.
740+
if _is_schema_drift(error):
741+
return (
742+
f"MTProto schema mismatch: the installed Telethon does not know an object the "
743+
f"server sent ({error}). This is NOT a missing user or chat — the data arrived, "
744+
f"parsing it failed. Upgrade Telethon; if it is already the latest release, its "
745+
f"schema is behind the current layer (code: {error_code})."
746+
)
747+
735748
return f"An error occurred (code: {error_code}). Check mcp_errors.log for details."
736749

737750

751+
def _is_schema_drift(error: Exception) -> bool:
752+
"""True for TypeNotFoundError — the installed TL schema is older than what the server sends."""
753+
try:
754+
from telethon.errors.common import TypeNotFoundError
755+
except Exception: # telethon missing or moved — not this helper's problem
756+
return False
757+
return isinstance(error, TypeNotFoundError)
758+
759+
738760
def validate_id(*param_names_to_validate):
739761
"""
740762
Decorator to validate chat_id and user_id parameters, including lists of IDs.

‎tests/test_schema_drift.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Schema drift must be reported as such, not as a generic error code.
2+
3+
A `TypeNotFoundError` means the installed TL schema is older than what the server
4+
sends. Hidden behind the generic message, it reads like "no such user/chat" and
5+
costs hours of debugging in the wrong direction.
6+
"""
7+
8+
from telegram_mcp.runtime import log_and_format_error
9+
from telethon.errors.common import TypeNotFoundError
10+
11+
12+
def test_schema_drift_is_named_and_actionable():
13+
msg = log_and_format_error("list_chats", TypeNotFoundError(0xD58A08C6, b"\x00"))
14+
assert "MTProto schema mismatch" in msg
15+
assert "NOT a missing user or chat" in msg
16+
17+
18+
def test_ordinary_error_keeps_the_generic_format():
19+
msg = log_and_format_error("get_chat", ValueError("boom"))
20+
assert "code:" in msg
21+
assert "MTProto schema mismatch" not in msg

0 commit comments

Comments
 (0)