#1930·tushare

[Bug] MCP 工具 schema:freq/topic 参数顶层 enum 缺少 type,导致严格校验的 LLM API 整个请求被拒(400)

Author: UncleFan1014Created Sep 28, 2026Updated Sep 28, 2026

问题

api.tushare.pro/mcp 的 17 个工具共 25 个参数(freq、topic),JSON schema 为「顶层 enum + 顶层 anyOf、但无顶层 type」:

json
"freq": {
  "enum": ["1min", "5min", "15min", "60min", "30min"],
  "description": "分钟频度",
  "anyOf": [{"type": "string"}, {"type": "integer"}]
}

OpenAI 兼容 API 生态的通行惯例是:enum 必须依附于显式 type。Moonshot(Kimi)API 自 2026-09-28 起对 tools[].function.parameters 启用严格校验(错误信息称为 "moonshot flavored json schema"),规则实测为:属性顶层出现 enum 而无 type,整个请求直接 400,与消息内容、模型无关。

受影响工具(实测扫描自 MCP 下发的工具定义,2026-09-28):

参数 工具
freq etf_mins ft_mins hk_mins idx_mins opt_mins stk_mins sw_mins rt_etf_min rt_etf_min_daily rt_fut_min rt_fut_min_daily rt_idx_min rt_idx_min_daily rt_min rt_min_daily
topic rt_etf_min rt_etf_min_daily rt_fut_min rt_fut_min_daily rt_hk_k rt_idx_min rt_idx_min_daily rt_min rt_min_daily rt_sw_k

复现

Kimi Code(v2.0.2,macOS)挂载 tushareMcp 后,任一模型(k3、kimi-for-coding)发起任意请求:

400 tools.function.parameters is not a valid moonshot flavored json schema,
details: <At path 'properties.freq': type is not defined>
type: invalid_request_error

最小复现(单工具、单参数,OpenAI 兼容 chat/completions):

bash
curl -s https://api.kimi.com/coding/v1/chat/completions \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{
    "model": "k3",
    "messages": [{"role": "user", "content": "ok"}],
    "max_tokens": 16,
    "tools": [{"type": "function", "function": {
      "name": "t_min",
      "parameters": {"type": "object", "properties": {
        "p": {"enum": ["1min", "5min"], "description": "分钟频度",
              "anyOf": [{"type": "string"}, {"type": "integer"}]}
      }, "required": [], "additionalProperties": false}
    }}]
  }'
# -> 400 {"error":{"message":"tools.function.parameters is not a valid moonshot
#    flavored json schema, details: <At path 'properties.p': type is not defined>","type":"invalid_request_error"}}

对照实验(同一请求仅替换 p 的 schema,均实测):

schema 变体 结果
顶层 enum + anyOf、无 type(freq 现状) 400
纯 enum 无 type 400
补顶层 "type": "string"(其余不变) 200
enum 移入 anyOf 分支内 200
纯 anyOf(无 enum,其余 236 个 tushare 工具的参数风格) 200

我们已做的处置

  1. 通过 wire 日志(traceId c1817df798dcbbf88dfd42bce4b6b016)与上述对照实验定位到根因;
  2. 临时止血:在客户端配置 tushareMcp.disabledTools 禁用上述 17 个工具,模型恢复可用,代价是分钟线/实时行情类接口全部不可用。

不修复的隐患

  1. 所有走严格校验网关的 agent 用户会整体瘫痪:症状是「模型 400 完全不可用」,而非某个工具不可用——普通用户无从定位到 MCP schema,只会得出「tushareMcp 与某模型不兼容」的结论而整体弃用;
  2. 校验严格化是服务端可随时收紧的行为(本次 Moonshot 于 2026-09-23 至 09-28 之间收紧,本地工具集 5 天未变、前后行为翻转),其他 OpenAI 兼容服务端随时可能跟进同样校验,届时影响面扩大到其他客户端;
  3. 用户被迫手工维护 disabledTools 黑名单(本例 17 条),MCP 工具更新后黑名单还需跟着维护,采用成本持续上升。

修复建议(两条路径均已实测通过,任选其一)

  • 方案 A(最小改动):给这 25 个参数补顶层 "type": "string",其余字段(enum/anyOf/description/default)全部保留;
  • 方案 B:把 enum 移入 anyOf 的 string 分支内:{"anyOf": [{"type": "string", "enum": [...]}, {"type": "integer"}], "description": ...}。

环境

  • 客户端:Kimi Code CLI v2.0.2(macOS),MCP 接入方式 https://api.tushare.pro/mcp/?token=***
  • 服务端错误样本时间:2026-09-28 14:06 CST
  • 注册 ID:332294