Feature(Go): MCP endpoint with bearer auth and tools/list
Add a stateless POST /mcp endpoint to the Go server, mounted by the web server and built from an environment-driven Config in cmd. The endpoint gates on YAMA_MCP_TOKEN: without it the route is not registered and /mcp 404s. Requests use a constant-time bearer compare, and transport failures stay transport failures (401 with a WWW-Authenticate challenge, 405 for anything but POST, 413 above 8 MiB, 204 for notifications). Everything after a parsed JSON-RPC request is HTTP 200, including -32600 for batch arrays and -32601 for unknown methods or tools. tools/list returns the nine minimal tools verbatim from an embedded copy of the canonical schemas. tools/mcp_tools/gen_go_tools.py extracts them from mcp/tools.json, preserving order and only the four published fields, and its --check mode detects drift. Readonly mode hides and refuses terminal_exec/terminal_close (-32006); tools that are listed but not implemented yet answer -32000 until their milestone lands. Also sync design §3.2 and the M2/M7/M8 plan text to tools.json, which is the sole schema contract: files not entries, line-based read_file, no since, camelCase capabilities, max_width [64,1920] defaulting to 1024, and outputSchema included in the M8 subset comparison. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
5f124fcb3a
commit
3f1957a9c9
11 files changed
+2022
-60
No files matched your search
@@ -200,15 +200,17 @@ curl -s -X POST localhost:18090/mcp -H "Authorization: Bearer $YAMA_MCP_TOKEN" \
|
||||
- `get_host_detail`:`id` 或 `ids`(≤50,批量走 `{tool,total,succeeded,failed,skipped,results[]}`);
|
||||
`capabilities{screenPreview,screenRegion,windowInfo,fileTransferV2,c2cPaths,registry,remoteControl,platform}`
|
||||
(`protocol.SupportsCap` 逐位)——**不含 `v2`**(恒真,列出来误导)。
|
||||
- `get_server_status`:`{version,commit,uptime_s,devices_online,devices_offline_known,
|
||||
mcp:{readonly,tools}}`;`version`/`commit` 取 `buildinfo`(§1.2),`uptime` 用 `mcp.Config.StartedAt`。
|
||||
- `get_server_status`:按 `outputSchema` 的字段名组装,Go 只填能如实取到的
|
||||
`version / uptime_sec / hosts / sessions / port / bind`(其余 MFC 专有字段不给);
|
||||
`version` 取 `buildinfo`(§1.2),`uptime_sec` 用 `mcp.Config.StartedAt`,`commit`
|
||||
只进日志,不塞进 schema 未定义的返回字段。
|
||||
- `get_audit_log`:`auditquery.Scan`;`type` 映射事件过滤;无 `since`。
|
||||
3. `buildinfo` 包 + `build.ps1` / `Makefile` 的 `-X` + winres 重建(§1.2)。
|
||||
|
||||
**测试**(`mcp/tools_local_test.go`)
|
||||
|
||||
- `devicehistory.New(t.TempDir()+"/devices.json", 10, log)` + `hub.Register` 造 3 台在线、2 台历史
|
||||
离线;断言过滤/分页/`total` 与 `returned` 分离。
|
||||
离线;断言过滤/分页,以及 `total`(命中数)、`totalAll`(过滤前)、`hasMore` 三者分离。
|
||||
- `get_host_detail` 的能力位:给一个 capability 串(含/不含 `0x0004`)两种。
|
||||
- `get_audit_log`:用 `logger.Audit` 写临时文件后查询(`auditquery` 的用例在搬包时一并迁)。
|
||||
|
||||
@@ -325,9 +327,10 @@ curl -s -X POST localhost:18090/mcp -H "Authorization: Bearer $YAMA_MCP_TOKEN" \
|
||||
(不要先落盘再报错)。
|
||||
4. **路径校验**(安全,必须单测):`filepath.Clean(filepath.Join(targetDir, FromSlash(name)))`
|
||||
+ `filepath.Rel` 判越界;另拒 NUL / 换行;`name` 允许客户端回填为相对路径。
|
||||
5. SHA-256 校验(完成包 32 字节)→ 读窗口(负 `offset` 从尾部倒数)→ 编码判定
|
||||
(`utf8.Valid` 优先,失败回退 `simplifiedchinese.GBK`,`latin1` 逐字节)→ 返回
|
||||
`{path,size,bytes_read,truncated,encoding,text,sha256}`。
|
||||
5. SHA-256 校验(完成包 32 字节)→ 编码判定(BOM → `utf8.Valid` → 回退
|
||||
`simplifiedchinese.GBK`,`latin1` 逐字节)→ 按行切分并取
|
||||
`offset`/`limit`/`max_bytes` 窗口 → 返回
|
||||
`{path,size,encoding,eol,lines_total,offset,lines_returned,truncated,text,sha256}`。
|
||||
6. 超时/断流:`-32001`;工具描述写明"超时可能是不支持 V2 的旧客户端"(设计 R3)。
|
||||
|
||||
**测试**:路径穿越用例集(`../x`、`..\\..\\x`、绝对路径、UNC、混合分隔符、`a/../../b`);
|
||||
@@ -336,9 +339,9 @@ curl -s -X POST localhost:18090/mcp -H "Authorization: Bearer $YAMA_MCP_TOKEN" \
|
||||
### M8 校验工具与文档(设计 §10)
|
||||
|
||||
1. `verify_tools.py --subset <tools.json> [token] [port] [--readonly]`:
|
||||
抓 Go 服务端 `tools/list`,与 `tools.json` 同名工具逐字段比 `description` / `inputSchema`;
|
||||
**忽略 `outputSchema`**(Go 不做);名字集合必须恰好等于期望的 9(或 7);
|
||||
多一个少一个都失败。
|
||||
抓 Go 服务端 `tools/list`,与 `tools.json` 同名工具逐字段比 `description` / `inputSchema` /
|
||||
`outputSchema`(与 `verify_tools.py` 的 `FIELDS` 口径一致);名字集合必须恰好等于
|
||||
期望的 9(或 7),多一个少一个都失败。
|
||||
2. 同批更新:`deploy/yama-server.env.example` 加 `YAMA_MCP_TOKEN` / `YAMA_MCP_READONLY` /
|
||||
`YAMA_MCP_TERM_IDLE` / `YAMA_MCP_MAX_CONCURRENCY` / `YAMA_MCP_READ_MAX`;
|
||||
设计文档状态改"已实施"。
|
||||
|
||||
Reference in new issue
Block a user