memorix icon indicating copy to clipboard operation
memorix copied to clipboard

Codex 场景下,Memorix 在会话建立后主动 roots/list,导致 MCP 工具无法稳定注入

Open ruan-cat opened this issue 7 months ago • 1 comments

背景

这条 issue 由 Codex(GPT-5.4,high reasoning,fast tier) 代理排查后整理提交。

本次连续排查耗时约 35 分钟,包含:

  1. 本地配置核对
  2. Codex / VS Code 扩展运行链排查
  3. stdio 与 HTTP 两条 MCP 链路对照
  4. source map / SDK 代码定位
  5. 运行日志与协议行为交叉验证

现象

在 Windows + VS Code Codex 扩展 场景下,Memorix 服务本身是正常的,但 Codex 新会话里始终拿不到 mcp__memorix__* 工具。

用户已经确认过两层状态:

  1. Memorix 服务层正常
    • memorix status 正常
    • codex mcp get memorix 能读到配置
    • memorix serve / memorix serve-http 都能启动
  2. 会话注入层异常
    • 新开的 Codex 会话里没有 mcp__memorix__*
    • list_mcp_resources 为空
    • list_mcp_resource_templates 里没有 Memorix

所以这里不是“项目没有历史记忆”,而是“会话没有拿到 Memorix MCP 工具注入”。


排查环境

  • 系统:Windows
  • Memorix:1.0.6
  • shell 侧 codex:0.120.0
  • VS Code 扩展内置 codex.exe:0.119.0-alpha.28
  • 目标项目:一个正常的 git monorepo

起始配置 1:stdio

README 当前给 Codex 的示例是:

[mcp_servers.memorix]
command = "memorix"
args = ["serve"]

在 Windows 下,这一层还会叠加一个问题:command = "memorix" 往往经过 pnpm/PowerShell shim,shell 能跑,不代表 Codex 的非 shell child_process 启动链一定稳定。

我本地先把这个问题绕开了,改成了 HTTP:

本地最小绕过配置:HTTP

[mcp_servers.memorix]
type = "http"
url = "http://127.0.0.1:3211/mcp"

memorix background status 与后台日志都确认 /mcp 控制面正常。

但是,切到 HTTP 后,Codex 会话侧问题仍然没有根治。这说明不只是 Windows stdio 启动链的问题。


关键探究结论

1. Memorix 服务本身不是“起不来”

我直接探测 memorix serve,可以完成 initialize 和 tools/list,也能列出完整工具集。

所以问题不在“server 本身完全不可用”。

2. Codex 侧有关键告警

在用 VS Code 扩展内置 codex.exe 做运行验证时,日志里出现过这条关键信息:

WARN rmcp::service::client: Received unexpected message Request(... ListRootsRequest ...)

也就是 Codex client 把来自服务端的 roots/list 视为 unexpected message。

3. 通过 source map 已经定位到 Memorix 自己的显式调用点

我继续追 source map,定位到了 不是 SDK 自动发的,而是 Memorix 自己在两条链路里都显式调用了 server.server.listRoots()。

stdio 路径

src/cli/commands/serve.ts

const tryRootsSwitch = async () => {
  const { roots } = await server.server.listRoots();
  // ...
};

tryRootsSwitch().catch(() => {});

也就是在:

await server.connect(transport);

之后,立刻异步主动请求 roots。

HTTP 路径

src/cli/commands/serve-http.ts

const tryRootsSwitch = async () => {
  const { roots } = await server.server.listRoots();
  // ...
};

queueMicrotask(() => {
  tryRootsSwitch().catch(() => {});
});

也就是 HTTP 会话建立后同样会主动发送 roots/list。

4. 这不是 McpServer 高层自动帮你发的

我也检查了 @modelcontextprotocol/sdk 的 McpServer / Server / Protocol 行为。

结论是:

  1. Server.listRoots() 确实存在
  2. 但不是 McpServer.connect() 自动替服务端发
  3. 这里是 Memorix 自己显式调用了底层 API

5. 这类 roots/list 时机本身就很可疑

根据 MCP 官方已接受的 SEP-2260:

  • roots/list
  • sampling/createMessage
  • elicitation/create

都应该 关联到一个正在处理的 client request。

不应该在会话刚建立后,作为 standalone server-initiated request 主动发出。

参考:

所以从协议语义上看,这里更像是 Memorix 的时序/实现问题,而不是 Codex 无理地“过严”。


我对根因的判断

我目前的判断是:

  1. Windows 下 stdio 配置 确实存在一层额外脆弱性(shim / child-process 启动链)
  2. 但即使切到 HTTP,问题仍然存在
  3. 真正的兼容主因是:Memorix 在 stdio / HTTP 两条链路里都会主动发送 standalone roots/list
  4. Codex client 把这类请求视为 unexpected,是有协议约束依据的

所以这条 issue 的主问题,我倾向于归因到:

Memorix 在连接建立后主动 listRoots() 的设计与部分 MCP client(尤其 Codex)的实现/规范边界不兼容。


建议修复方案

建议 1:不要在 connect 后立刻主动 listRoots()

建议移除以下模式:

  • stdio:await server.connect(transport) 之后立即 tryRootsSwitch()
  • HTTP:queueMicrotask(() => tryRootsSwitch())

建议 2:只在明确的 client request 处理链里读取 roots

如果确实需要 roots,建议放到某个明确的 client request 上下文内,而不是独立地由 server 主动发起。

建议 3:更优先依赖 memorix_session_start({ projectRoot })

尤其在 HTTP/control-plane 模式下,projectRoot 显式绑定已经是更清晰、更稳定的方案。

建议 4:Windows 下 README 里的 Codex 示例最好补充说明

当前 README 里的 Codex 示例是:

[mcp_servers.memorix]
command = "memorix"
args = ["serve"]

在 Windows 下,这个配置可能还会踩到 shim/child-process 的问题。即使你们暂时不改默认文档,至少也建议加一条 Windows 注意事项。


本地临时绕过方案

我本地当前采用的是:

  1. 全局 Codex 配置切到 HTTP:
[mcp_servers.memorix]
type = "http"
url = "http://127.0.0.1:3211/mcp"
  1. 后台运行 Memorix control plane
  2. 不再依赖 Windows stdio shim 启动链

但要强调:

  • 这只是启动链绕过,不是根因修复
  • 因为 HTTP 路径里你们同样主动调用了 server.server.listRoots()

可以帮助你们复核的关键点

请重点看这两个调用点:

  • src/cli/commands/serve.ts
  • src/cli/commands/serve-http.ts

尤其是这类逻辑:

const { roots } = await server.server.listRoots();

如果你们愿意,我可以继续补一版:

  1. 一个更小的复现脚本思路
  2. 或者一版我认为更稳妥的代码修改建议

谢谢。

ruan-cat avatar Apr 14 '26 19:04 ruan-cat

Fixed in v1.0.7 Removed proactive listRoots() calls from both serve.ts and serve-http.ts after session establishment. This was violating MCP SEP-2260 which requires server-initiated requests to be associated with a client request. Codex (and other strict MCP clients) treated the standalone roots/list as unexpected, breaking tool injection. Project binding now relies on: (1) RootsListChangedNotification handler (client-initiated, legitimate), (2) memorix_session_start({ projectRoot }) for explicit binding, (3) cwd-based deferred-binding as fallback. Verified: MCP initialize + tools/list returns 29 tools without any unexpected server-initiated requests.

AVIDS2 avatar Apr 15 '26 03:04 AVIDS2