Skip to content

feat: admin tools 按身份授权——API key 须属于 admin 用户(#104 后续) - #105

Merged
Svtter merged 4 commits into
mainfrom
feat/mcp-server
Nov 19, 2026
Merged

Svtter merged 4 commits into
mainfrom
feat/mcp-server

Conversation

@Svtter

@Svtter Svtter commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #103 / #104:管理员工具的授权从「启动开关」改为「调用者身份」。

语义(与 #104 的差异)

#104 用 --allow-admin-tools 在启动时开关 admin 工具——开关开着时任何 agent 都能用,不区分身份。本 PR 改为身份驱动:

  • admin 角色进数据模型:users.role 列(空=普通成员,admin=管理员;老库自动迁移,数据无损)
  • MCP 启动时解析身份:配置的 --api-key 本地经 VerifyAPIKey+IsAdmin 校验,或经 --server 的 GET /api/me(新增端点,Bearer key / 登录 cookie 均可)
  • admin → 工具常驻可用(不需要重启加 flag);member / 无效 key / 无 key → 工具不注册,tools/list 不可见(fail-closed)
  • --allow-admin-tools=true|false 降级为强制覆盖(操作员无 key 的场景可 =true)
  • user_add 支持 admin=true(管理员铸造管理员);user_list 输出 role
  • CLI:sth user add <name> [--admin]、sth user promote|demote <name>

验证

  • 真实 stdio 冒烟:无 key → 12 tools;admin key → 16 tools("authenticated as root ... exposed");member key → 12 tools("hidden (admin only)")
  • 老库迁移测试:v2.6.0 schema(无 role 列)→ 打开后 role 往返正常、数据无损
  • go test -race ./internal/... 5 包全绿(新增身份门控/远程 /api/me/强制覆盖/迁移测试)

Closes #101(全部收尾)。

…admin

Replace the deployment-time --allow-admin-tools switch with identity
authorization: the admin-class tools (user_add / user_issue_key /
user_revoke_key / audit) are registered only when the configured API key
resolves to a user holding the admin role. A member key, an invalid key,
or no key leaves them unregistered and invisible in tools/list. The flag
survives as a forced override (true/false) for operator setups.

- session: users.role column (fresh schema + migration for existing
  DBs), SetUserRole / IsAdmin, User.Role on all lookups
- server: GET /api/me returns the authenticated identity (Bearer key or
  login cookie) with role/isAdmin
- cli: sth user add [--admin], promote/demote, user list shows role
- mcp: resolveAdminTools verifies the key locally or via /api/me at
  startup; user_add accepts admin=true; user_list reports roles
- verified over real stdio: no key -> 12 tools, admin key -> 16,
  member key -> 12; legacy-DB migration test included

@Svtter Svtter left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自动代码评审 (review-server-neo)

⚠️ CONDITIONAL MERGE

评审详情

整体方向合理(身份化门控 + 迁移 + 文档同步),但 env 覆盖的 fail-open 与 /api/me 探测失败即致命这两点需要修,否则加固过的部署/老服务端会被打回不可用或静默放开。

Blocking

  • internal/cli/mcp.go:76-84:env 只识别 true/1,STH_ALLOW_ADMIN_TOOLS=false|0 被当成"未设置",于是 admin key 会重新暴露管理员工具——相对旧行为(resolveBool 让 false/0 明确为 false)是 fail-open。同时与 CHANGELOG/README 声称的"STH_ALLOW_ADMIN_TOOLS forces the outcome regardless of identity"直接矛盾。改法:与 resolveBool / popBoolFlag 一致地接受 true|1|false|0,false/0 置 adminOverrideSet=true, adminOverrideValue=false,无法识别的值打 warning(或直接报错)。
  • internal/cli/mcp.go:118-126 + runMCP(internal/cli/mcp.go:178):GET /api/me 任何失败都会让 runMCP 直接返回错误,MCP server 起不来。命中场景很常见:key 失效/过期、老服务端没有 /api/me(doJSONRequest 对 404 报错)、--server 指向未启用 auth 的实例(handleMe 在 auth 关闭时返回 401)、瞬时网络错误;并且 resolveAPIKey 会回落到 STH_API_KEY,环境里带这个变量的 CI/开发机会无故起不来。这与本地分支的处理不一致(internal/cli/mcp.go:132-137 对不匹配的 key 只打 note 继续)。改法:fail-closed 应当是"打 note + allowAdminTools=false"继续启动;若确实想硬失败,请在 README/CHANGELOG 里明确写出并说明与服务端版本的关系。

Should-fix

  • internal/cli/mcp.go:117-133:配了 --server 时,管理员判定来自远端身份,但被门控的 user_add / user_issue_key / user_revoke_key / audit 操作的是本地 store(--db)。远端 admin 在本地毫无权限也能铸造本地用户/key;反过来本地 admin 会被隐藏。建议对本地工具同时校验本地 store,或在文档中显式声明"远端 admin 即授予本地 store 管理员能力"。
  • internal/cli/mcp.go:110-116:只给 --api-key 不给 --server(README 里没有 --server 的最简用法)时,key 是对着默认远端 127.0.0.1:3939 用的,却拿本地 DB 去解析身份,通常解析不到 → 管理员工具静默隐藏。建议此时同样探测 server.DefaultServerURL,或给出"请显式传 --server"的 note。
  • 测试缺口:没有覆盖 /api/me 失败路径(401 / 非 JSON / 老服务端)、没有 STH_ALLOW_ADMIN_TOOLS=false 的用例(正是上面 Blocking 那条)、internal/server/auth.go:301 handleMe 完全没有服务端测试(200 字段形状、auth 关闭时 401、用户已删除时 401)、sth user promote|demote(internal/cli/user.go:88 userSetRole)也没有测试。这些是我在 internal/**/*_test.go 里 grep promote|demote|userSetRole|IsAdmin\(|STH_ALLOW_ADMIN_TOOLS 后确认缺失的(只命中 mcp_test.go 的 env=true 用例与 migrate_role_test.go)。
  • internal/session/migrate_role_test.go:20-38:注释声称"without losing users or keys",但迁移后从未调用 VerifyAPIKey 验证 key 仍可用;补一行等价性断言,否则"keeps keys"只是注释。
  • internal/cli/mcp_test.go:1012-1016:forced off must hide user_add even for an admin key 的配置却传的是 memberKey,断言与注释不符;newIdentityTestStore(internal/cli/mcp_test.go:943)返回的 *session.Store 在调用处用 _ = store 丢弃,且 store.FindUserByUsername("root"), _ 忽略错误 —— 查找失败时会拿空 ID 去 IssueAPIKey,测试可能假通过。请改成 adminKey 路径并检查错误。

Nits

  • internal/cli/user.go:56:sth user add alice --admin true(空格形式)会把 true 当成多余 positional 静默丢弃;userAdd 只取 positionals[0],建议对多余 positional 报错(同类问题在其它子命令也存在)。
  • internal/session/migrate_role_test.go:44:ALTER TABLE ... DROP COLUMN 需要 SQLite ≥ 3.35,若 CI 链接的 sqlite3 更老,这个测试会以"schema 不兼容"而非迁移 bug 失败,值得在失败信息里提示。
  • internal/cli/user.go:110:roleLabel 返回 (member),输出成 role: (member),读起来像双重括号,建议成员输出 member 或直接留空。
  • internal/server/auth.go:314:500 分支直接回 err.Error(),与周边处理保持一致可接受,但会外泄内部错误文本。

由 review-server-neo 自动生成。新 push 会重新评审。

Details

Comment thread internal/cli/mcp.go
dbPath: flags["db"],
serverURL: flags["server"],
apiKey: resolveAPIKey(flags),
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] internal/cli/mcp.go:76-84:env 只识别 true/1,STH_ALLOW_ADMIN_TOOLS=false|0 被当成"未设置",于是 admin key 会重新暴露管理员工具——相对旧行为(resolveBool 让 false/0 明确为 false)是 fail-open。同时与 CHANGELOG/README 声称的"STH_ALLOW_ADMIN_TOOLS forces the outcome regardless of identity"直接矛盾。改法:与 resolveBool / popBoolFlag …

Comment thread internal/cli/mcp.go Outdated
return nil
}

admin := false

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] internal/cli/mcp.go:118-126 + runMCP(internal/cli/mcp.go:178):GET /api/me 任何失败都会让 runMCP 直接返回错误,MCP server 起不来。命中场景很常见:key 失效/过期、老服务端没有 /api/me(doJSONRequest 对 404 报错)、--server 指向未启用 auth 的实例(handleMe 在 auth 关闭时返回 401)、瞬时网络错误;并且 resolveAPIKey 会回落到 STH_API_KEY,环境里带这个变量的 CI/开发机会无…

Comment thread internal/cli/mcp.go
c.allowAdminTools = false
return nil
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should-fix] internal/cli/mcp.go:117-133:配了 --server 时,管理员判定来自远端身份,但被门控的 user_add / user_issue_key / user_revoke_key / audit 操作的是本地 store(--db)。远端 admin 在本地毫无权限也能铸造本地用户/key;反过来本地 admin 会被隐藏。建议对本地工具同时校验本地 store,或在文档中显式声明"远端 admin 即授予本地 store 管理员能力"。

Comment thread internal/cli/mcp.go
}
c.allowAdminTools = c.adminOverrideValue
return nil
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should-fix] internal/cli/mcp.go:110-116:只给 --api-key 不给 --server(README 里没有 --server 的最简用法)时,key 是对着默认远端 127.0.0.1:3939 用的,却拿本地 DB 去解析身份,通常解析不到 → 管理员工具静默隐藏。建议此时同样探测 server.DefaultServerURL,或给出"请显式传 --server"的 note。

// database created before the column existed.
store, err := NewSQLiteStore(dbPath)
if err != nil {
t.Fatal(err)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should-fix] internal/session/migrate_role_test.go:20-38:注释声称"without losing users or keys",但迁移后从未调用 VerifyAPIKey 验证 key 仍可用;补一行等价性断言,否则"keeps keys"只是注释。

Comment thread internal/cli/mcp_test.go Outdated
t.Fatal("invalid key must not expose user_add")
}

// Forced override wins over identity.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should-fix] internal/cli/mcp_test.go:1012-1016:forced off must hide user_add even for an admin key 的配置却传的是 memberKey,断言与注释不符;newIdentityTestStore(internal/cli/mcp_test.go:943)返回的 *session.Store 在调用处用 _ = store 丢弃,且 store.FindUserByUsername("root"), _ 忽略错误 —— 查找失败时会拿空 ID 去 IssueAPIKey,测试可能假通过。请…

Rework the admin gating semantics per review + product decision: the
admin-class tools (user_add / user_issue_key / user_revoke_key / audit)
are registered unconditionally — always available to admins — and every
call enforces the caller identity instead:

- no API key configured: local-operator mode, calls allowed (startup
  notes it)
- with an API key: each call resolves the identity (local store or
  GET /api/me on --server); member/invalid keys get an actionable
  'admin role required' error; a key revoked mid-session stops working
  on the very next call
- --allow-admin-tools=true|false (flag or STH_ALLOW_ADMIN_TOOLS) forces
  the outcome; env accepts true|1|false|0 with explicit-false honored
  and unparseable values rejected (no silent fail-open)

Also from the #105 review: startup no longer probes /api/me (a broken or
old server cannot prevent the MCP server from starting — failures only
affect the individual admin call), the local-key denial message points
at --server for remote keys, and tests now cover promote/demote,
handleMe (admin/member/401/auth-disabled), env false|0 + invalid values,
/migration keeping keys verifiable, and call-time denial for every path.

@Svtter Svtter left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自动代码评审 (review-server-neo)

⚠️ CONDITIONAL MERGE

评审详情

第二轮复审(head 71bf137,设计已从"注册期门控"改为"注册常开 + 调用期 requireAdmin")。上一轮的两条 Blocking 都已修好;剩余为姿态决策与一个未同步字段。

上一轮已修复

  • env 覆盖:false|0 现在被当成显式拒绝、无法识别的值直接报错(internal/cli/mcp.go:78-91),并有用例(internal/cli/mcp_test.go:869-890)。
  • 启动不再探身份:/api/me 改为调用期校验(internal/cli/mcp.go:97-124),老服务端 404 / 失效 key / 网络故障只让管理员调用失败,brokenServer 用例验证了非管理员工具仍可用。
  • GET /api/me 有测试(internal/server/me_test.go);sth user promote|demote 有测试(internal/cli/mcp_test.go TestUserPromoteDemote);迁移测试真的验证了 key 存活(internal/session/migrate_role_test.go:47-49);newIdentityTestStore 不再吞错;注释/断言不一致已修。

Should-fix

  • internal/cli/mcp.go:100-107(requireAdmin):无 key 时直接 return nil,默认姿态从 fail-closed 变成 fail-open。最常见的 sth mcp --db ... 用法下,被 prompt 注入的 agent 现在可以 user_add / user_issue_key / user_revoke_key / audit(含 prune_days 抹日志)——这与 issue #104 写明的验收标准("关闭时 agent 在 tools/list 里根本看不到这些工具")冲突,也是相对 2.6.0 的安全默认值变化。建议:无 key 也 fail-closed(默认拒绝/隐藏,--allow-admin-tools=true 才放行),或至少取得 issue owner 明确同意;同时在 CHANGELOG 补一句升级动作("保持旧姿态请设 STH_ALLOW_ADMIN_TOOLS=false"),目前没有。
  • internal/cli/mcp.go:45-47,136,159:c.identity 在工具调用中写入、在 identitySuffix(:166-172)中被读,完全没有同步;go-sdk 是否并发分发工具请求我无法在本 checkout 确认(SDK 源码不在仓库内),一旦并发就是 data race(现有测试串行,-race 抓不到)。而且该缓存是多余的:callerIsAdmin 已把 username 返回给 requireAdmin。直接删掉 identity 字段即可,零成本消除风险。
  • internal/cli/mcp.go:126-138 + 各 tool* 里的 c.openStore():远端 /api/me 的 admin 结论被用来放行本地 store 的 user_add/user_issue_key/user_revoke_key/audit,新测试 TestMCPAdminGatingRemoteIdentity 把"远端 admin 在本地 DB 里建用户"固化成了期望行为。要么本地工具再校验本地 store,要么在 README/CHANGELOG 明说这个跨域授权。
  • 每次管理员调用会开两次 store 连接(callerIsAdmin 一次 + tool 里一次),每次 open 都会重跑 init/ensureColumns 的 DDL 检查;建议把 *session.Store 复用起来。

Nits

  • internal/cli/mcp_test.go:142:dbPath: t.TempDir() 传的是目录而非 DB 文件,任何工具调用都会失败,用 newMCPTestDB 或直接留空。
  • internal/cli/mcp.go:53-58、internal/cli/mcp_test.go:34,838:注释里仍写着已改名的 resolveAdminTools。
  • internal/cli/mcp.go:86-88:STH_ALLOW_ADMIN_TOOLS=<垃圾值> 现在会让 sth mcp 直接起不来;方向正确(loud failure),但文档未提,建议 CHANGELOG 补一句。
  • internal/cli/user.go:110:roleLabel 仍输出 role: (member)(新测试还断言了这个字符串),建议成员输出 member。
  • internal/cli/user.go:56:sth user add alice --admin true(空格形式)仍把 true 当多余 positional 静默丢弃。
  • internal/server/auth.go:314:500 分支仍回显 err.Error()。

minor nits, OK to ship after —— 先处理「无 key 的 fail-open 姿态」与 c.identity 未同步两项,其余为文档与测试小项。


由 review-server-neo 自动生成。新 push 会重新评审。

Details

Comment thread internal/cli/mcp.go Outdated
// requireAdmin enforces the caller identity on every admin-class tool call.
// An explicit --allow-admin-tools=false denies unconditionally, =true allows
// unconditionally. With no API key configured the process runs in
// local-operator mode (the process can write the store directly anyway), so

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should-fix] internal/cli/mcp.go:100-107(requireAdmin):无 key 时直接 return nil,默认姿态从 fail-closed 变成 fail-open。最常见的 sth mcp --db ... 用法下,被 prompt 注入的 agent 现在可以 user_add / user_issue_key / user_revoke_key / audit(含 prune_days 抹日志)——这与 issue #104 写明的验收标准("关闭时 agent 在 tools/list 里根本看不到这些工具")冲突,也是相对 2…

Comment thread internal/cli/mcp.go Outdated
adminOverrideSet bool
adminOverrideValue bool

// identity caches the username the API key resolved to, for error

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should-fix] internal/cli/mcp.go:45-47,136,159:c.identity 在工具调用中写入、在 identitySuffix(:166-172)中被读,完全没有同步;go-sdk 是否并发分发工具请求我无法在本 checkout 确认(SDK 源码不在仓库内),一旦并发就是 data race(现有测试串行,-race 抓不到)。而且该缓存是多余的:callerIsAdmin 已把 username 返回给 requireAdmin。直接删掉 identity 字段即可,零成本消除风险。

Comment thread internal/cli/mcp.go
}

// callerIsAdmin resolves the configured API key to a user and reports whether
// that user holds the admin role. An unknown or revoked key is denied with an

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should-fix] internal/cli/mcp.go:126-138 + 各 tool* 里的 c.openStore():远端 /api/me 的 admin 结论被用来放行本地 store 的 user_add/user_issue_key/user_revoke_key/audit,新测试 TestMCPAdminGatingRemoteIdentity 把"远端 admin 在本地 DB 里建用户"固化成了期望行为。要么本地工具再校验本地 store,要么在 README/CHANGELOG 明说这个跨域授权。

Address the second-round rs review on #105:

- no API key configured: admin-class calls are now denied (no identity
  is not admin) instead of silently allowed; the startup note and the
  denial both point at an admin key or --allow-admin-tools=true.
  Upgrade action documented in the CHANGELOG
- remove the unsynchronized c.identity cache written during tool calls
  (data race if the SDK dispatches concurrently); callerIsAdmin returns
  the username to the error message instead
- cache the *session.Store for the process lifetime: admin calls used to
  open the database twice (identity check + operation), re-running
  schema DDL each time; per-call Close removed accordingly
- document the cross-domain authorization explicitly: with --server the
  admin check uses the remote identity while admin tools operate on the
  local --db store
- nits: userAdd rejects stray positionals ('sth user add alice --admin
  true' no longer silently drops 'true'), roleLabel prints 'member'
  instead of '(member)', legacy-schema migration test skips with a
  SQLite >= 3.35 hint, stale resolveAdminTools comment references
  updated, TestMCPToolsListed uses a real DB file

@Svtter Svtter left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自动代码评审 (review-server-neo)

⚠️ CONDITIONAL MERGE

评审详情

第三轮复审(head 9bc49aa)。上一轮的三条 Should-fix 全部处理到位,本轮只剩两处"新行为没同步到对外契约"的小项。

上一轮已修复

  • 无 key 现在 fail-closed:internal/cli/mcp.go:129-148 直接报 "no API key configured…",并有 TestMCPAdminToolsCallTimeEnforcement 的 no-key 用例。
  • 删掉了 racy 的 identity 缓存:callerIsAdmin 改为返回 (admin, username, err),requireAdmin 直接用返回值(internal/cli/mcp.go:150-190),并发写入问题消失。
  • 双次开库消除:openStore 变成进程级缓存 + sync.Mutex(internal/cli/mcp.go:50-72);各 tool 里的 defer store.Close() 已同步删掉,没有遗漏(grep store.Close() 在 mcp.go 中已无残留调用点)。
  • 文档补齐:CHANGELOG 加了 Upgrade action(无 key 被拒、设 STH_ALLOW_ADMIN_TOOLS=true 或配 admin key),并明确"管理员工具始终操作本地 --db,配 --server 时信任远端 admin 身份"——上一轮的跨域授权问题按"已知并声明"处理,我认可。
  • roleLabel 现在输出 member;sth user add 拒绝多余 positional(internal/cli/user.go:57-59);迁移测试在 SQLite < 3.35 时 t.Skipf 而不是红;TestMCPToolsListed 改用真实 DB 路径。

Should-fix

  • internal/cli/mcp.go:279-296:四个管理员工具的 Description 仍写着 "…(allowed without a key in local-operator mode)",与新行为(无 key 拒绝)相反。这段文案是 agent 在 tools/list 里直接读到的契约,会诱导它以为不配 key 就能管理用户。请改成 "calls are denied without an API key; pass an admin user's key or force with --allow-admin-tools=true"。
  • internal/cli/mcp.go:50-72:进程级 store 只缓存、从不关闭;mcpServe(internal/cli/mcp.go:223)返回时也没有 Close。长期运行的 sth mcp 退出时不做 checkpoint,测试里每个 mcpConfig 也会留下一个未关闭的句柄(t.TempDir 清理在 Windows 上会因此失败)。建议 mcpServe 里 defer cfg.closeStore()(新增一个带锁的 close),或至少在 s.Run 返回后关闭。

Nits

  • internal/cli/mcp.go:80:parseMCPConfig 的注释还写着 "resolveAdminTools applies it as an override"(函数早已改名为 requireAdmin);同类问题在 internal/cli/mcp_test.go:34(meOK 注释说"enough for requireAdmin to succeed",实际返回 member 身份会让 requireAdmin 失败)仍在。
  • internal/cli/mcp_test.go:222:TestMCPTagCategorizeProjectTitleLocal 无端加了 adminOverrideSet: true。requireAdmin 只被 4 个管理员工具调用(mcp.go:769/803/831/924),tag/categorize/project/title 并不需要——去掉 override 才能继续证明"默认拒绝姿态下非管理员工具照常可用"。
  • README.md 的 MCP 小节没提"配 --server 时远端 admin 身份被信任用于本地 store"(只有 CHANGELOG 与 README.cn 写了),两份英文/中文文档不对称。
  • internal/server/auth.go:314:500 分支仍回显 err.Error()(沿用周边风格,可接受)。

minor nits, OK to ship after —— 建议先把四段 tools 描述与新的 fail-closed 行为对齐,并给缓存的 store 加个关闭路径。


由 review-server-neo 自动生成。新 push 会重新评审。

Details

Comment thread internal/cli/mcp.go
return session.NewSQLiteStore(c.dbPath)
}
return session.NewStore()
mcp.AddTool(s, &mcp.Tool{

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should-fix] internal/cli/mcp.go:279-296:四个管理员工具的 Description 仍写着 "…(allowed without a key in local-operator mode)",与新行为(无 key 拒绝)相反。这段文案是 agent 在 tools/list 里直接读到的契约,会诱导它以为不配 key 就能管理用户。请改成 "calls are denied without an API key; pass an admin user's key or force with --allow-admin-tools=true"。

Comment thread internal/cli/mcp.go
// operation itself), re-running schema DDL each time.
store *session.Store
storeMu sync.Mutex
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[should-fix] internal/cli/mcp.go:50-72:进程级 store 只缓存、从不关闭;mcpServe(internal/cli/mcp.go:223)返回时也没有 Close。长期运行的 sth mcp 退出时不做 checkpoint,测试里每个 mcpConfig 也会留下一个未关闭的句柄(t.TempDir 清理在 Windows 上会因此失败)。建议 mcpServe 里 defer cfg.closeStore()(新增一个带锁的 close),或至少在 s.Run 返回后关闭。

Final items from the third-round rs review on #105:

- the four admin tool descriptions said calls were 'allowed without a
  key in local-operator mode' — the opposite of the shipped fail-closed
  behavior; they now state calls are denied without an API key and how
  to enable them (this text is the contract agents read from tools/list)
- mcpServe closes the process-lifetime store on exit (new locked
  closeStore) so long-running sth mcp checkpoints and tests leave no
  dangling handle; openStore documents the no-close contract
- nits: TestMCPTagCategorizeProjectTitleLocal drops its unneeded admin
  override (proving non-admin tools work under the default deny
  posture), parseMCPConfig comment renamed off resolveAdminTools, meOK
  comment made accurate, README.md gains the cross-domain authorization
  note that README.cn already had

@Svtter Svtter left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自动代码评审 (review-server-neo)

✅ CAN MERGE

评审详情

第四轮复审(head 1f9733c)。上一轮两条 Should-fix 均已处理,剩余只是注释/文档层面的陈旧引用,不阻塞。

已修复

  • 四个管理员工具的 Description 已与新行为对齐(internal/cli/mcp.go:293-306:"calls are denied without an API key; pass an admin user's key or force with --allow-admin-tools=true"),agent 在 tools/list 里读到的契约不再误导。
  • 缓存的 store 有关闭路径:新增 closeStore()(internal/cli/mcp.go:196-204)并在 mcpServe 里 defer cfg.closeStore()(internal/cli/mcp.go:234),锁保护的形态也正确,s.Run 返回后无在途调用。
  • TestMCPTagCategorizeProjectTitleLocal 去掉了多余的 admin override,并注明"证明非管理员工具在默认拒绝姿态下可用"(internal/cli/mcp_test.go:223-225)。
  • 前几轮的身份门控、fail-closed、env 解析、迁移与 /api/me 测试仍然完整。

Nits(可选,不阻塞)

  • internal/cli/mcp.go:81-82:parseMCPConfig 的注释仍写 "resolveAdminTools applies it as an override",env 块里还用一句 "(requireAdmin replaced the earlier resolveAdminTools draft; docs elsewhere may still use either name.)" 来圆场;直接改名改成 requireAdmin 更干净。internal/cli/mcp_test.go:34-37(meOK 注释提到并不存在的 "resolveAdmin-style startup probe")同类。
  • internal/cli/mcp_test.go:965-969:TestMCPAdminToolsCallTimeEnforcement 的注释仍写 "no key runs in local-operator mode and is allowed",而函数体断言的是无 key 被拒——注释与断言相反,改成 fail-closed 描述。
  • README.md 的 MCP 安全区仍缺"配 --server 时远端 admin 身份被信任用于本地 --db store"这一句(CHANGELOG 与 README.cn 都有),中英不对称。
  • 测试里的 mcpConfig 不走 closeStore,每个用例仍留一个未关闭的 SQLite 句柄(Linux 无影响,Windows 上 t.TempDir 清理可能报错);可在 newMCPClient 里 t.Cleanup(cfg.closeStore)。
  • internal/server/auth.go:314:500 分支回显 err.Error(),沿用周边风格,可接受。

ship as-is —— 上述几处都是注释/文档陈旧引用,功能与安全语义已自洽,可以合并。


由 review-server-neo 自动生成。新 push 会重新评审。

Details

@Svtter
Svtter merged commit 3a60d37 into main Nov 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: MCP server 支持——sth mcp 子命令,把 CLI 能力暴露为 MCP tools

1 participant