Skip to content

Commit 28344e0

Browse files
authored
Merge pull request #188 from artgas1/fix/surface-mtproto-schema-drift
fix: report MTProto schema drift instead of a generic error code
2 parents a7bdd4d + 7b19b95 commit 28344e0

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)