File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
738760def validate_id (* param_names_to_validate ):
739761 """
740762 Decorator to validate chat_id and user_id parameters, including lists of IDs.
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments