Skip to content

[code-review] MRTR 路径 resolveMRTSampling 跳过 ValidateSamplingParams:畸形 sampling params 绕过结构验证直打 LLM provider(与交互路径不对称) #2514

Description

@topcheer

文件和行号

  • internal/mcp/mrtr.go:213-231(resolveMRTSampling)
  • 对照:internal/mcp/client.go:2578-2588(handleSampling 交互路径)

问题描述

MRTR(input_required 重试协议)的 deferred sampling/createMessage 输入请求解析只跑 ParseSamplingParams(裸 json.Unmarshal,零结构校验),不跑 ValidateSamplingParams。四路径对照:

路径 结构验证
交互式 sampling(client.go:2586) 有 → -32602
交互式 elicitation(client.go:2618)
MRTR elicitation(mrtr.go:196)
MRTR sampling(mrtr.go:220)

mrtr.go:214 注释自称 "Mirrors handleSampling"——同文件 elicitation 孪生函数正确镜像了验证,唯独 sampling 被漏,实现遗漏非有意取舍。

触发场景

任意用户已配置的 MCP 服务器对任何一次 tools/call(client.go:639 callWithMRTR 挂载)返回 input_required,其 inputRequests 内嵌带畸形 params 的 sampling/createMessage(如 tool_result 缺配对/混 text 的 user 消息/空 name tool 定义)→ resolveMRTSampling 未验证 → 直达 agentruntime/mcp_sampling.go 的 handler(消息循环原样转 provider blocks,无 id/配对/纯度检查)→ p.Chat 畸形请求打 LLM API(400 或未定义行为)。同样内容在交互路径被本地 -32602 干净拒绝,且失去 -32602 语义的服务器自我纠错通道。

预期行为 vs 实际行为

  • 预期:MRTR 路径与交互路径同构——验证不过返回 "invalid sampling params" 错误(成为 MRTR input resolution 错误)
  • 实际:畸形 params 原样直达 provider

修复建议

mrtr.go:223 ParseSamplingParams 成功后补一行:if err := ValidateSamplingParams(params); err != nil { return nil, fmt.Errorf("invalid sampling params: %w", err) }(与 resolveMRTElicitation 既有错误形态一致)

严重程度

medium(独立复核 sa-8 确认:同信任边界内防线旁路+buggy 服务器触发难排查 provider 400;下游仅成本类防线不覆盖结构校验)

Co-Authored-By: ggcode noreply@ggcode.dev

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions