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

+20
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
@@ -580,6 +581,8 @@ func listFilesResult(args listFilesArgs, drives []driveEntry, files []fileEntry,
var structured map[string]any
var text string
var header []string
var rows [][]string
if drives != nil {
total := len(drives)
begin := min(offset, total)
@@ -593,6 +596,14 @@ func listFilesResult(args listFilesArgs, drives []driveEntry, files []fileEntry,
"hasMore": end < total,
}
text = pageSummary(total, limit, offset, "个驱动器")
header = []string{"drive", "typeName", "fileSystem", "totalMB", "freeMB"}
rows = make([][]string, 0, end-begin)
for _, d := range drives[begin:end] {
rows = append(rows, []string{
d.Drive, d.TypeName, d.FileSystem,
strconv.FormatUint(d.TotalMB, 10), strconv.FormatUint(d.FreeMB, 10),
})
}
} else {
total := len(files)
begin := min(offset, total)
@@ -606,9 +617,18 @@ func listFilesResult(args listFilesArgs, drives []driveEntry, files []fileEntry,
"hasMore": end < total,
}
text = pageSummary(total, limit, offset, "个条目")
header = []string{"name", "isDir", "size", "mtime"}
rows = make([][]string, 0, end-begin)
for _, f := range files[begin:end] {
rows = append(rows, []string{
f.Name, strconv.FormatBool(f.IsDir),
strconv.FormatUint(f.Size, 10), strconv.FormatInt(f.Mtime, 10),
})
}
}
if truncated {
text += fmt.Sprintf("目录条目超过 %d 条,只解析了前 %d 条。", fileListMaxEntries, fileListMaxEntries)
}
text = appendRows(text, header, rows)
return newToolResult(text, structured)
}