Skip to content

MCP handler 型別強制 + CLI parity: max_messages string bypass + CLI date behavior 不一致 #8

@kiki830621

Description

@kiki830621

Problem

Finding A1: max_messages: "0" (string) 繞過 <=0 檢查

MCP SDK 的 Value.intValue 只對 .int(...) case 回傳 Int,對 .string(...) 回傳 nil。所以呼叫端送 JSON {"max_messages": "0"}(string 不是 int)會:

  1. args["max_messages"]?.intValue 回傳 nil
  2. <= 0 檢查被跳過
  3. fallback 到 default(get_chat_history: limit; dump_chat_to_markdown: 5000)
  4. 使用者無感知

From verification of #5, #6 by devils-advocate:
「實測 MCP SDK Value.intValue.string(...)nil,使用者誤送 JSON string "0" → nil → fallback 到 default 5000,完全繞過 <= 0 檢查」

Finding A2: CLI parseCLIDate + --until 跟 MCP 不同步

CLI 的 parseCLIDate(TelegramAllCLI.swift:148)仍有 #5 修好的 bug:

  • 沒 regex pre-check——CLI 會 silently accept 2026/04/17
  • --until 沒 end-of-day 語意——CLI 的 --until 2026-04-17 還是 parse 到 00:00:00,漏掉當日訊息

From verification of #5, #6 by regression + devils-advocate:
「CLI history --dump-markdown 和 MCP dump_chat_to_markdown 行為分歧:同一個 2026-04-17 在兩邊的上界語意不一樣」

Type

bug

Expected

  • max_messages 若是 string-encoded number(例 "5")要能正確 parse 或拒絕
  • CLI 的 --until 行為跟 MCP until_date 一致(包含整天)
  • CLI 的 --since/--until 拒絕非 YYYY-MM-DD 格式

Actual

  • MCP handler 對 type-mismatched 輸入靜默忽略
  • CLI 對 2026/04/17 等格式 silent accept
  • CLI --until 遺漏當日訊息

Strategy

  • MCP handler: 當 args["max_messages"] 是 string 時,先 Int(stringValue) parse,parse 失敗回 errorResult → done in parseMaxMessages helper (commit a918b11, refined to use SDK Int(value, strict:false) in f0203ac)
  • CLI: 讓 parseCLIDateparseUntilDate 共用 CheTelegramAllMCPCore 的 helpers,或下推到 TelegramAllLib → done by moving DateParsing to TelegramAllLib (commit b7e7899)

Code Reference

  • MCP 層:Sources/CheTelegramAllMCPCore/Server.swift:364-375, 484-494
  • CLI 層:Sources/telegram-all/TelegramAllCLI.swift:148-158
  • MCP SDK 行為:.build/checkouts/swift-sdk/Sources/MCP/Base/Value.swift:42-45

Related: #5, #6

Implementation history


Current Status

Phase: closed
Last updated: 2026-04-27 by idd-close

Key Decisions

Scope Changes

Blocking

(none)

Test counts

  • 150 → 163 (+13)
  • Codex 獨立驗證 swift test 通過 (34 + 14 = 48 隔離 filter tests)

Commits

Subsumed

Follow-up

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions