Improve: Cover protocol and policy error codes in the MCP smoke test
The suite only exercised the happy paths, so a regression in the negative paths -- unknown method, unknown tool, a missing required argument, an oversized batch or a policy refusal -- would have gone unnoticed. Add those checks alongside a section that reads the switches from get_server_status, picks a feature that is actually turned off and asserts its tool answers -32006; with every switch on it skips rather than guessing, and with -AuditLog it also asserts the refusal shows up as CALL_DENIED. Document the full error-code table in section 12, including which codes mean "retry with different arguments" and which mean "this is a policy decision, retrying is pointless", and note that a batch call never fails as a whole for per-host errors. Verified against the Debug instance with McpKeylog temporarily set to zero: get_keylog refused with -32006 and the audit log gained the matching CALL_DENIED line, 34 checks passing; with the switch restored 32 pass, and 37 with a search path. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
7c5bf3e73d
commit
cc3e11d30d
2 files changed
+73
-1
No files matched your search
@@ -648,3 +648,25 @@ token 只从 `-Token` / env 读取,不落仓库、不打印;无在线主机
|
||||
- 过滤在分页之前:`total` 为命中条数、`totalAll` 为过滤前条数;摘要补一句「过滤前共 N 条(大小/修改时间/精简字段过滤在服务端进行,不减少客户端搜索耗时)」。
|
||||
- 过滤只看服务端已收到的条目,所以仍受 `max_results` / `timeout_ms` 截断:客户端没搜完时,过滤结果也是部分的(`truncated` / `finished` 字段照旧反映这一点)。
|
||||
- 验证(Debug 实例,根目录 `C:\Windows\Fonts`、`*.ttf`):基线 255 条;`min_size=1000000` → 39 条且 `totalAll=255`;`name_only=true` 每条只有 `path`/`name`;`modified_after=2026-01-01` → 165 条、`2026-06-03 00:00:00` → 130 条、`2027-01-01` → 0 条;`min_size=-5`、`min_size=10 & max_size=5`、`modified_after=2026/01/01`、`modified_after=2026-01-01 12:00` 均返回预期的 `-32602`。冒烟脚本 33/33 通过(新增 3 项:`min_size` 过滤后 `totalAll` 不变、`name_only` 精简字段、非法 `modified_after` 返回 `-32602`;后三项需带 `-SearchPath` 才执行)。
|
||||
|
||||
## 12. 错误码速查(E 批:冒烟与文档)
|
||||
|
||||
工具的失败分三类,AI 端应按 `code` 决定下一步,而不是读 `message` 文本。`message` 是给人看的英文说明,随实现变化,不作为契约。
|
||||
|
||||
| code | 含义 | 典型触发 | 建议动作 |
|
||||
|---|---|---|---|
|
||||
| `-32700` | Parse error | 请求体不是合法 JSON | 修正客户端序列化 |
|
||||
| `-32600` | Invalid Request | 缺 `jsonrpc` / `method` / 非法 `tools/call` 结构 | 修正请求 |
|
||||
| `-32601` | Method not found | `method` 不是 `initialize` / `tools/list` / `tools/call` 等 | 修正请求 |
|
||||
| `-32602` | Invalid params | 未知工具名、缺必填参数、参数类型/取值非法、批量 `ids` 超过 50 个、`search_file` 过滤参数非法、文本无法转 GBK | 改参数重试,重试同样的请求不会成功 |
|
||||
| `-32001` | Timeout | 等客户端就绪/等子连接/等搜索结果超时(`timeout_ms` 内没等到) | 可加大 `timeout_ms` 重试;持续出现看主机是否卡住 |
|
||||
| `-32002` | Host offline / not found | 主机 id 不存在、主连接已断、会话 id 不存在或已关闭 | 先 `list_online_hosts` 刷新再选主机 |
|
||||
| `-32003` | Busy | 该主机已有同类请求在飞(终端/截屏/文件/聊天会话),或同设备重复 `chat_open` | 等前一个结束或先 `*_close` |
|
||||
| `-32004` | Send failed | 命令写入主连接失败(连接刚断) | 重试一次;仍失败按 `-32002` 处理 |
|
||||
| `-32005` | Not supported on this host | 需要 Windows 的工具用在 LNX/MAC/APK 主机上 | 换 Windows 主机,或改用跨平台只读工具(`list_files` 等) |
|
||||
| `-32006` | Policy refused | 只读模式 / 对应 `Mcp*` 开关关闭 / 主机不支持该能力(如 V2 文件传输) | **不要重试**:这是配置决定,需管理员改服务端配置 |
|
||||
|
||||
- `-32006` 会额外在审计日志里记一条 `CALL_DENIED`(§10.8),其余错误只体现在 `CALL_FAIL` 行。
|
||||
- 批量调用(带 `ids[]`)**整体不报错**:每个 id 的结果单独带 `error`,顶层用 `succeeded` / `failed` / `skipped` 汇总;只有整批请求本身非法(超过 50 个 id、写操作传 `ids`)才返回顶层 `-32602`。
|
||||
- 冒烟脚本 `tools/mcp_smoke.ps1` 覆盖了上表的可复现项:`-32601`(未知 method)、`-32602`(未知工具/缺参数/51 个 id)、`-32002`(不存在的主机)、`-32006`(任一开关关闭时自动挑一个关着的功能验证,并断言审计里出现 `CALL_DENIED`;全部开启时跳过)。其余码由各批次的功能验证覆盖(`-32001`/`-32003` 等需要构造竞态或超时,不进冒烟)。
|
||||
- 本次验证(Debug 实例,`McpKeylog` 临时置 0):`get_keylog` 返回 `-32006`,`Bin\logs\mcp_audit.log` 末尾出现 `CALL_DENIED ... tool=get_keylog reason=Keyboard log is disabled (McpKeylog=0)`;此时冒烟 34/34 通过。恢复开关后 `-32006` 与审计两条转为跳过,32/32 通过;带 `-SearchPath` 时 37/37 通过。
|
||||
+51
-1
@@ -18,6 +18,8 @@
|
||||
-SearchPath 可选,跑 search_file 测试的搜索根目录(如 C:\Windows\Fonts)。
|
||||
不指定就跳过:客户端递归用无界 wsprintf 拼 MAX_PATH 缓冲,
|
||||
深目录会栈溢出(Debug 客户端弹 Run-Time Check Failure #2)。
|
||||
-AuditLog 可选,服务端审计日志路径(如 Bin\logs\mcp_audit.log)。给了才断言
|
||||
-32006 拒绝会写一条 CALL_DENIED。
|
||||
|
||||
退出码:0 全部通过;1 有失败项。token 不会被打印。
|
||||
#>
|
||||
@@ -26,7 +28,8 @@ param(
|
||||
[string]$Token = $env:YAMA_MCP_TOKEN,
|
||||
[string]$HostId = '',
|
||||
[switch]$SkipHost,
|
||||
[string]$SearchPath = ''
|
||||
[string]$SearchPath = '',
|
||||
[string]$AuditLog = ''
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
@@ -119,6 +122,24 @@ $ro = @($tools | Where-Object { $_.annotations.readOnlyHint -eq $true })
|
||||
Check '存在只读工具且无只读工具被标为破坏性' ($ro.Count -gt 0 -and
|
||||
(@($ro | Where-Object { $_.annotations.destructiveHint -eq $true }).Count -eq 0)) ("readOnly=" + $ro.Count)
|
||||
|
||||
# ===== 2b. 协议层错误码 =====
|
||||
Write-Host "`n[2b] 协议层错误码"
|
||||
$um = Rpc 'bogus/method' @{} $null $null
|
||||
Check '未知 method 返回 -32601' ([int]$um.error.code -eq -32601) ([string]$um.error.code)
|
||||
$ut = Rpc '' $null 'no_such_tool' @{}
|
||||
Check '未知工具返回 -32602' ([int]$ut.error.code -eq -32602) ([string]$ut.error.code)
|
||||
$mi = Rpc '' $null 'get_host_detail' @{}
|
||||
Check '缺少必填参数返回 -32602' ([int]$mi.error.code -eq -32602) ([string]$mi.error.message)
|
||||
|
||||
# ===== 2c. 批量入口上限 =====
|
||||
$ids51 = (1..51 | ForEach-Object { '"99999999999999' + $_ + '"' }) -join ','
|
||||
# Rpc 会把 ids 数组重新序列化,这里直接发原始请求体,避免 51 个 id 走两层转换
|
||||
$raw51 = '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"list_processes","arguments":{"ids":[' +
|
||||
$ids51 + ']}}}'
|
||||
$r51 = Send-Raw $raw51 $Token
|
||||
$j51 = $r51.Body | ConvertFrom-Json
|
||||
Check '批量 51 个 id 返回 -32602' ([int]$j51.error.code -eq -32602) ([string]$j51.error.message)
|
||||
|
||||
# ===== 3. 服务端本机只读工具 =====
|
||||
Write-Host "`n[3] 服务端本机工具"
|
||||
$online = Rpc '' $null 'list_online_hosts' @{ limit = 100 }
|
||||
@@ -229,6 +250,35 @@ if (-not $SkipHost) {
|
||||
Check '批量 results 覆盖全部 id' (@($bs.results).Count -eq 2 -and [int]$bs.succeeded -eq 1 -and [int]$bs.failed -eq 1)
|
||||
}
|
||||
|
||||
# ===== 5. 策略拒绝(-32006) =====
|
||||
# 只在某个开关确实关闭时才有意义,所以先读 get_server_status 的 switches 再挑一个关着的功能。
|
||||
# 全部开启时跳过(不算失败)——这条不是每次都能测,但测到时能证明拒绝路径与审计都还在。
|
||||
Write-Host "`n[5] 策略拒绝"
|
||||
$gateMap = @(
|
||||
@{ swName = 'keylog'; tool = 'get_keylog'; args = @{} }
|
||||
@{ swName = 'chat'; tool = 'chat_open'; args = @{} }
|
||||
@{ swName = 'terminal'; tool = 'terminal_open'; args = @{} }
|
||||
@{ swName = 'remote_control'; tool = 'get_screenshot'; args = @{} }
|
||||
)
|
||||
$offGate = $null
|
||||
foreach ($g in $gateMap) {
|
||||
if ($null -ne $st.switches -and $st.switches.PSObject.Properties.Name -contains $g.swName -and
|
||||
$st.switches.($g.swName) -eq $false) { $offGate = $g; break }
|
||||
}
|
||||
if ($offGate) {
|
||||
$args5 = @{}
|
||||
if ($HostId) { $args5['id'] = $HostId }
|
||||
foreach ($k in $offGate.args.Keys) { $args5[$k] = $offGate.args[$k] }
|
||||
$refused = Rpc '' $null $offGate.tool $args5
|
||||
Check ("关闭 " + $offGate.swName + " 后 " + $offGate.tool + " 返回 -32006") ([int]$refused.error.code -eq -32006) ([string]$refused.error.message)
|
||||
if ($AuditLog) {
|
||||
$tail = Get-Content -LiteralPath $AuditLog -Tail 20 -ErrorAction SilentlyContinue
|
||||
Check '拒绝记录进审计日志(CALL_DENIED)' (@($tail | Where-Object { $_ -match 'CALL_DENIED' }).Count -gt 0)
|
||||
}
|
||||
} else {
|
||||
Write-Host " 所有功能开关都开着,跳过 -32006 检查(关掉任一开关再跑一次可覆盖)。" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# ===== 汇总 =====
|
||||
Write-Host ("`n通过 " + $script:pass + " 项,失败 " + $script:fail + " 项。") -ForegroundColor $(if ($script:fail -eq 0) { 'Green' } else { 'Red' })
|
||||
if ($script:fail -gt 0) {
|
||||
|
||||
Reference in new issue
Block a user