Feature: Add instructions, capability reporting and filters to MCP

P10-A is the first batch of the next MCP phase: the server now tells a model
how it expects to be driven instead of leaving the rules to be inferred from
per-tool descriptions.

initialize carries the instructions field that MCP 2025-06-18 defines, in
five sections: where host ids come from, which switch hides which tool, what
each error code implies for the next action, typical orchestrations, and
safety. Every statement was checked against the implementation rather than
derived from the tool descriptions, and four did not survive: the host_*
family only supports name/pid filters in part (named per tool now), read-only
mode does not hide host_set_remark and host_set_watch_level (they touch only
the local host store and are refused with -32006 when called), -32003 is one
lock keyed by device id rather than one per capability, and there is no
"screen stream" tool to pick instead of get_screenshot. The string is built
from u8 literals because the project compiles with /execution-charset:.936.

get_host_detail returns a capabilities object (platform, screenPreview,
remoteControl, fileTransferV2, c2cPaths, registry), each flag tied to a gate
that already exists in the dispatcher, so a model no longer discovers a
host's limits by calling and reading -32005. remoteControl mirrors the two
conditions remote_* actually applies (Windows or Linux, and not
multi-monitor), not the preview bit. Making the flags honest exposed
list_registry rejecting only LNX and MAC, so an APK host passed and then
waited out the timeout; it now uses the same is-Windows predicate the bit is
computed from. That predicate existed as identical local copies in
McpMachine.cpp and McpChat.cpp and now lives in McpInternal.

list_processes and list_windows take an optional name and pid, filtered on
the server before the page window is applied, with total counting matches
and totalAll still reporting the unfiltered count. The two tools get their
own schema builder so list_services does not advertise a filter it would
silently ignore, and the pid descriptions differ because a pid means the
process itself for one tool and its owner for the other.

get_keylog gains set_offline and clear, the two commands the client already
implements on the keyboard sub-connection but nothing on the MCP side ever
sent, plus offlineRecord from the ready packet's byte 1. Both writes are
gated on McpReadonly=0 and validated before COMMAND_KEYBOARD goes out, so a
refused call cannot leave a running keyboard session behind; the tool's
annotations change from read-only to writable and destructive, and the
server instructions gain the matching sentence.

Four capabilities are exposed twice through two different sub-connection
chains, so each pair's descriptions now say which one to reach for first and
when the other is the only option: the host_* trio carries more fields and
filters on the server, the list_* trio is the McpMachine=0 fallback, and
operate_window is the only one that can close a window. No tool was merged:
the split is what lets McpMachine be switched off on its own. search_file's
description drops the stale MAX_PATH-overflow warning (fixed in 8f718d9) for
the behaviour that remains - the client silently skips any entry whose full
path does not fit, so a drive-root scan misses deep results and reaches the
10000-entry cap sooner.

Verified per commit by building the Debug x64 server, starting it on a spare
port so the running Release instance could not answer instead, and reading
the fields back out of real responses as UTF-8; tools/list shows 54 tools
with the edited descriptions present. Bin/settings.ini was restored
byte-exact and the Release instance was left alone. The smoke test gained
assertions for all four additions: instructions has to parse out of
initialize, capabilities is compared against the reported platform (registry
true exactly for the Windows client types, remoteControl false on macOS and
Android), the filters query a real entry back by name and by pid while
totalAll stays put, and offlineRecord accepts both a boolean and its absence
for old clients. That script ran 28 checks clean against this build, with
the host-scoped assertions exercised against synthetic responses on a
separate harness (12 of 12, including the negative cases).

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-28 21:25:44 +01:00
1 parent 4a3cded174
commit bb55f52b29
6 files changed
+331 -44

No files matched your search

+69 -5
View File
@@ -16,8 +16,9 @@
-HostId 指定在线主机 id;缺省取 list_online_hosts 的第一台
-SkipHost 只测服务端本机工具,不触达远程主机(无在线主机时用)
-SearchPath 可选,跑 search_file 测试的搜索根目录(如 C:\Windows\Fonts)。
不指定就跳过:客户端递归用无界 wsprintf 拼 MAX_PATH 缓冲,
深目录会栈溢出(Debug 客户端弹 Run-Time Check Failure #2)。
不指定就跳过:没有安全的默认根目录,从盘符根扫描很慢,且路径超过
MAX_PATH 的条目会被客户端静默跳过(8f718d9 起不再栈溢出),通常先撞
10000 条上限,测出来的是「部分结果」而不是功能是否可用。
-AuditLog 可选,服务端审计日志路径(如 Bin\logs\mcp_audit.log)。给了才断言
-32006 拒绝会写一条 CALL_DENIED。
@@ -118,6 +119,8 @@ Check '请求体超过 2 MB 返回 413' ($o.Status -eq 413) ("status=" + $o.Stat
Write-Host "`n[2] 协议层"
$init = Rpc 'initialize' @{ protocolVersion = '2025-06-18'; capabilities = @{}; clientInfo = @{ name = 'smoke'; version = '1' } } $null $null
Check 'initialize 返回 serverInfo' ($null -ne $init.result.serverInfo)
# initialize.instructions 是模型唯一的「隐性知识」入口(主机 id 形态、开关、错误码、编排)。
Check 'initialize 带非空 instructions' (([string]$init.result.instructions).Length -gt 100)
$tl = Rpc 'tools/list' @{} $null $null
$tools = @($tl.result.tools)
@@ -210,12 +213,53 @@ if (-not $SkipHost) {
Check 'get_host_detail id 回显正确' (([string]$h.id) -eq $HostId)
Check 'get_host_detail 含 os/version/online' ($h.os -and $h.version -and ($null -ne $h.online))
# capabilities:这台主机本身能做什么(服务端开关在 get_server_status.switches)。
# 逐项都要能对上真实门控,所以除了「齐全」还要断言 platform 与 registry 的一致性。
$caps = $h.capabilities
Check 'get_host_detail 带 capabilities.platform' ($null -ne $caps -and [string]$caps.platform -ne '')
$capFlags = @('screenPreview', 'remoteControl', 'fileTransferV2', 'c2cPaths', 'registry')
$missing = @($capFlags | Where-Object {
$null -eq $caps -or $null -eq $caps.$_ -or $caps.$_.GetType() -ne [bool]
})
Check 'capabilities 五个布尔位齐全' ($missing.Count -eq 0) ("missing=" + ($missing -join ','))
$isWin = @('DLL', 'EXE', 'MEXE', 'SC', 'MDLL') -contains [string]$caps.platform
Check 'capabilities.registry 与平台一致(仅 Windows)' ($caps.registry -eq $isWin) `
("platform=" + $caps.platform + " registry=" + $caps.registry)
if (@('MAC', 'APK') -contains [string]$caps.platform) {
Check 'capabilities.remoteControl 对 MAC/APK 为 false' ($caps.remoteControl -eq $false) `
("platform=" + $caps.platform)
}
$procs = Rpc '' $null 'list_processes' @{ id = $HostId; limit = 3 }
$ps = $procs.result.structuredContent
Check 'list_processes 分页字段' (HasPaging $ps)
Check 'list_processes 条数不超过 limit' (@($ps.processes).Count -le 3)
Check 'list_processes 总数为正' ([int]$ps.total -gt 0)
# 服务端过滤(P10-A §5.3):过滤在分页之前生效——total 是命中数,totalAll 仍是全量。
$firstProc = @($ps.processes)[0]
if ($firstProc -and [string]$firstProc.name) {
$pname = [string]$firstProc.name
$byName = Rpc '' $null 'list_processes' @{ id = $HostId; name = $pname; limit = 100 }
$bn = $byName.result.structuredContent
Check 'list_processes name 过滤命中且 totalAll 不变' `
($byName.error -eq $null -and [int]$bn.totalAll -eq [int]$ps.totalAll -and
[int]$bn.total -gt 0 -and [int]$bn.total -le [int]$bn.totalAll) `
("total=" + $bn.total + " totalAll=" + $bn.totalAll + " baseAll=" + $ps.totalAll)
$lower = $pname.ToLowerInvariant()
Check 'list_processes name 过滤结果都含该名字' `
(@($bn.processes | Where-Object { -not ([string]$_.name).ToLowerInvariant().Contains($lower) }).Count -eq 0)
}
if ($firstProc -and $null -ne $firstProc.pid) {
$ppid = [int]$firstProc.pid
$byPid = Rpc '' $null 'list_processes' @{ id = $HostId; pid = $ppid; limit = 100 }
$bp = $byPid.result.structuredContent
Check 'list_processes pid 过滤只返回该进程' `
($byPid.error -eq $null -and [int]$bp.total -ge 1 -and
@($bp.processes | Where-Object { [int]$_.pid -ne $ppid }).Count -eq 0) `
("pid=" + $ppid + " total=" + $bp.total)
}
$wins = Rpc '' $null 'list_windows' @{ id = $HostId; limit = 3 }
Check 'list_windows 正常返回' ($wins.error -eq $null -and (HasPaging $wins.result.structuredContent))
@@ -229,8 +273,8 @@ if (-not $SkipHost) {
$dir = Rpc '' $null 'list_files' @{ id = $HostId; path = $drive; limit = 5 }
Check ("list_files 目录列举 (" + $drive + ")") ($dir.error -eq $null -and (HasPaging $dir.result.structuredContent))
# search_file:客户端递归用无界 wsprintf 拼 MAX_PATH 缓冲,深目录会栈溢出
# (Debug 客户端弹 Run-Time Check Failure #2 并挂住搜索线程)。所以默认不跑,
# search_file:没有安全的默认搜索根。从盘符根扫描很慢,客户端会静默跳过超过
# MAX_PATH 的路径(8f718d9 起不再栈溢出),通常先撞 10000 条上限。所以默认不跑,
# 需要时用 -SearchPath 指定一个中浅层目录(如 C:\Windows\Fonts)。
if ($SearchPath) {
$search = Rpc '' $null 'search_file' @{ id = $HostId; path = $SearchPath; pattern = '*.exe'; limit = 3; timeout_ms = 5000 }
@@ -259,7 +303,7 @@ if (-not $SkipHost) {
($bad.error -and [int]$bad.error.code -eq -32602)
}
} else {
Write-Host " 未指定 -SearchPath,跳过 search_file 测试(深目录会触发客户端已知的 MAX_PATH 栈溢出)。" -ForegroundColor Yellow
Write-Host " 未指定 -SearchPath,跳过 search_file 测试(默认根目录扫得慢且结果不全)。" -ForegroundColor Yellow
}
}
@@ -267,6 +311,26 @@ if (-not $SkipHost) {
$batch = Rpc '' $null 'list_processes' @{ ids = @($HostId, '999999999999999'); limit = 1 }
$bs = $batch.result.structuredContent
Check '批量 results 覆盖全部 id' (@($bs.results).Count -eq 2 -and [int]$bs.succeeded -eq 1 -and [int]$bs.failed -eq 1)
# get_keylog.offlineRecord(P10-A):客户端就绪包里带「离线记录」标志,老客户端只发 1 字节,
# 此时服务端按设计省略该字段——缺省与布尔都算通过,但要把是哪种情况说出来。
if (@($tools | Where-Object { $_.name -eq 'get_keylog' }).Count -gt 0) {
$kl = Rpc '' $null 'get_keylog' @{ id = $HostId; duration_ms = 1000; max_bytes = 4096 }
if ($kl.error) {
Check 'get_keylog 正常返回' $false ([string]$kl.error.message)
} else {
$ks = $kl.result.structuredContent
$hasFlag = $ks.PSObject.Properties.Name -contains 'offlineRecord'
Check 'get_keylog.offlineRecord 为布尔(老客户端可缺省)' `
((-not $hasFlag) -or $ks.offlineRecord.GetType() -eq [bool]) `
("offlineRecord=" + $ks.offlineRecord)
if (-not $hasFlag) {
Write-Host " 主机未上报离线记录标志(老客户端),offlineRecord 缺省属预期。" -ForegroundColor Yellow
}
}
} else {
Write-Host " get_keylog 未开启(McpKeylog),跳过键盘记录读取。" -ForegroundColor Yellow
}
}
# ===== 4b. 结构化失败(新客户端:TOKEN_*_ERROR → -32009/-32010) =====