逐日AI

面试题库

共 328 题,当前筛选 1 题。

7 天 MCP:把工具接进任何 Agent

D3 resources 与 prompts:URI 模板、变更通知、进度与日志、分页,以及客户端能力

  • MCP 的分页游标为什么必须是不透明的?如果客户端去解析它,会出什么问题?Why must MCP pagination cursors be opaque, and what breaks if a client parses them?
    国内高频海外高频进阶#pagination#api-design

    分析过程 · 先想清楚再作答

    1. 这题表面考规范条文,实际考「有没有做过带分页的对外接口」。只背出「规范说不透明」拿不到分,要能说出解析之后具体哪一步会崩。
    2. 拆法:先问游标里到底装的是什么。服务端可以装偏移量、主键、时间戳、甚至一段加密状态,而且**换实现时它随时会变**。客户端一旦按某种格式解析,服务端从偏移量换成主键那天,所有客户端一起挂——这是把服务端的内部实现变成了公开契约。
    3. 第二个坑是伪造。客户端自己造一个 offset:9999 递给服务端,等于绕过了服务端对翻页范围的控制;如果游标里编了权限或过滤条件,伪造它就是一次越权。
    4. 第三个坑最阴:把空字符串当成结束。规范写死了只有 nextCursor **缺失**才代表没有下一页,空串是完全合法的游标。判错的表现是最后一页数据被静默丢掉,而且不报错,测试也很难发现。
    5. 结论:客户端对游标只允许做一个判断——nextCursor 在不在。页大小同理不得假设固定值,服务端随时可以改。非法游标服务端应当回 -32602,而不是静默返回第一页,否则客户端会陷进死循环。
    6. 可预期的追问:那服务端这边有什么坑?偏移量式游标要求列表顺序稳定,中途插入一条会让后面全部错位,所以要么先排序、要么把游标编成上一条的主键。

    How to reason about it · think before answering

    1. It looks like a spec-recitation question but really tests whether you have shipped a paginated public API. Quoting the rule earns nothing; naming the concrete failure does.
    2. Start from what a cursor holds. A server may encode an offset, a primary key, a timestamp, or encrypted state, and it may change that at any time. A client that parses one format breaks everywhere the day the server switches, because parsing turned an internal detail into a public contract.
    3. Second failure is forgery. A client that fabricates offset:9999 bypasses the server's control over paging range, and if the cursor encodes filters or permissions, forging it is a privilege escalation.
    4. Third and nastiest: treating an empty string as the end. The spec is explicit that only a missing nextCursor ends the sequence; an empty string is a valid cursor. Getting this wrong silently drops the last page with no error, which tests rarely catch.
    5. Conclusion: a client may make exactly one judgment about a cursor — whether nextCursor is present. Page size likewise must not be assumed fixed. Servers should reject invalid cursors with -32602 rather than silently returning page one, which would loop the client forever.
    6. Likely follow-up: what bites the server side? Offset cursors require a stable ordering, since an insertion shifts everything after it, so either sort first or encode the last item's key instead.

    答题要点

    • 游标内容是服务端的内部实现,解析它等于把实现细节变成公开契约,服务端换实现时客户端全挂
    • 伪造游标可以绕过服务端对翻页范围的控制,游标里若编了过滤或权限条件就是越权
    • 只有 nextCursor 缺失才代表结束,空字符串是合法游标,判错会静默丢掉最后一页
    • 页大小由服务端决定不得假设固定,非法游标服务端应回 -32602 而不是静默回第一页

    Key points

    • Cursor contents are server internals; parsing them turns an implementation detail into a public contract that breaks on any change
    • Forged cursors bypass server-side paging control, and become privilege escalation if the cursor encodes filters or permissions
    • Only a missing nextCursor ends the sequence — an empty string is valid, and getting it wrong silently drops the last page
    • Page size is server-decided and must not be assumed fixed; invalid cursors should return -32602 rather than silently resetting