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
+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