Fix(mcp): put listing rows and host ids into text blocks

MCP clients that surface only content[].text saw just count summaries from
list_hosts and get_audit_log; the 64-bit host id lived only in
structuredContent. Without the id, read_file, get_host_detail and
terminal_exec cannot be addressed, so the tools were unusable from those
clients.

Append a tab-separated table after the summary for every list-shaped local
tool: list_hosts (id first, then name/ip/group/os/online/clientType/
last_seen), get_audit_log (time/type/msg), list_files (drives and directory
entries) and get_host_detail (compact host JSON; id/ok/detail rows for a
batch call). Only the returned page is rendered, cells are flattened to one
line, and the truncation note stays on its own line before the table.

structuredContent is unchanged and remains the authoritative copy. Tests
cover row contents, paging, escaping and truncation ordering; a live run
against a Debug client confirmed the id is now visible in text. The MFC
server needs no equivalent change: BuildResult already appends the full
structuredContent as a second text item, so text-only clients there already
receive host ids. Go's envelope emits a single text item, which is why the
rows go into it.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-13 11:25:57 +01:00
1 parent 70556391dd
commit 3d18b2119a
5 files changed
+197 -4

No files matched your search

+36 -3
View File
@@ -201,6 +201,18 @@ func wantIDs(t *testing.T, hosts []map[string]any, want ...string) {
}
}
func TestAppendRowsSanitizesCells(t *testing.T) {
got := appendRows("head", []string{"a", "b"},
[][]string{{"x\ty", "line\nbreak"}, {"", ""}})
want := "head\na\tb\nx y\tline break\n-\t-"
if got != want {
t.Fatalf("appendRows = %q, want %q", got, want)
}
if got := appendRows("head", []string{"a"}, nil); got != "head" {
t.Fatalf("empty rows = %q, want unchanged summary", got)
}
}
func TestListHostsPoolsAndProjection(t *testing.T) {
f := newLocalFixture(t)
@@ -222,6 +234,14 @@ func TestListHostsPoolsAndProjection(t *testing.T) {
if !strings.Contains(text, "共 3 台主机") {
t.Fatalf("text = %q, want host count", text)
}
// Text-only MCP clients never see structuredContent, so the table in the
// text block must carry the id every follow-up call is addressed by.
if !strings.Contains(text, "id\tname\tip\tgroup\tos\tonline\tclientType\tlast_seen") {
t.Fatalf("text = %q, want host table header", text)
}
if !strings.Contains(text, "1001\tALPHA\t10.0.0.1\tdefault\tWindows 10\ttrue\tEXE\t-") {
t.Fatalf("text = %q, want ALPHA row with id first", text)
}
alpha := hosts[0]
if got := strField(t, alpha, "name"); got != "ALPHA" {
@@ -384,7 +404,11 @@ func TestListHostsPaging(t *testing.T) {
func TestGetHostDetailOnlineAndOffline(t *testing.T) {
f := newLocalFixture(t)
_, sc := mustCallLocal(t, f.srv, "get_host_detail", `{"id":"1001"}`)
text, sc := mustCallLocal(t, f.srv, "get_host_detail", `{"id":"1001"}`)
if !strings.Contains(text, "\n") || !strings.Contains(text, `"id":"1001"`) ||
!strings.Contains(text, `"name":"ALPHA"`) {
t.Fatalf("text = %q, want summary plus host JSON", text)
}
host, ok := sc["host"].(map[string]any)
if !ok {
t.Fatalf("host = %v", sc["host"])
@@ -457,7 +481,12 @@ func TestGetHostDetailOnlineAndOffline(t *testing.T) {
func TestGetHostDetailBatch(t *testing.T) {
f := newLocalFixture(t)
_, sc := mustCallLocal(t, f.srv, "get_host_detail", `{"ids":["1001","9999","2001"]}`)
text, sc := mustCallLocal(t, f.srv, "get_host_detail", `{"ids":["1001","9999","2001"]}`)
if !strings.Contains(text, "id\tok\tdetail") ||
!strings.Contains(text, "1001\ttrue\t{") ||
!strings.Contains(text, "9999\tfalse\t") {
t.Fatalf("text = %q, want one row per id", text)
}
if got := strField(t, sc, "tool"); got != "get_host_detail" {
t.Fatalf("tool = %q", got)
}
@@ -573,7 +602,11 @@ func TestGetAuditLog(t *testing.T) {
)
s := New(Config{Token: "test-token", AuditPath: path})
_, sc := mustCallLocal(t, s, "get_audit_log", `{}`)
text, sc := mustCallLocal(t, s, "get_audit_log", `{}`)
if !strings.Contains(text, "time\ttype\tmsg") ||
!strings.Contains(text, "2026-11-12T10:03:00Z\tdevice.online\t") {
t.Fatalf("text = %q, want audit rows", text)
}
entries, ok := sc["entries"].([]any)
if !ok || len(entries) != 4 {
t.Fatalf("entries = %v, want 4", sc["entries"])