bug-启动服务后提示:MCP initialization failed
作者你好,非常感谢分享 pi-web 项目,下面是部分接入后遇到的问题,可以辅助判断下如何修复。我本地依赖下面的修复能解决问题
一、现象
运行 pi-web 后,每次新建会话都会触发以下错误,且反复出现:
MCP initialization failed: TypeError: Cannot read properties of undefined (reading 'fg')
at ignore-listed frames
TypeError: Cannot read properties of undefined (reading 'fg')
at ignore-listed frames
⨯ uncaughtException: TypeError: Cannot read properties of undefined (reading 'fg')
at ignore-listed frames
(重复若干次...)
时间线:
▲ Next.js 16.2.9 ← 服务启动
✓ Ready in 197ms
MCP initialization failed: ... ← 首次报错
[pi-web] session_start dispatched to extensions for session ... ← session 创建成功,但已携带错误
⨯ uncaughtException: ... ← 后续定时器反复触发
影响范围:安装了 pi-mcp-adapter 扩展的 pi-web 环境。
二、调用链还原
pi-web 启动
└─ 浏览器请求新建会话 → POST /api/agent/new
└─ startRpcSession() 创建 AgentSession
└─ AgentSessionWrapper.start()
└─ AgentSessionWrapper.beginExtensionBinding()
└─ inner.bindExtensions() / extensionRunner.setUIContext()
└─ pi-mcp-adapter 扩展的 initializeMcp() 被触发
├─ 连接 MCP 服务器...
├─ lifecycle.setReconnectCallback(() => {
│ updateStatusBar(state)
│ })
└─ lifecycle.startHealthChecks()
└─ 定时健康检查 → 触发 reconnect 回调
└─ updateStatusBar(state)
└─ ui.theme.fg("accent", ...)
└─ ui.theme === undefined
→ TypeError! 💥
三、根因定位:三行代码的致命组合
第 1 行(pi-web rpc-manager.ts 行 782)
pi-web 在 RPC 模式下为扩展提供的 UI 上下文中,theme getter 直接返回 undefined:
// lib/rpc-manager.ts, createExtensionUiContext() 方法内
get theme() { return undefined; },
设计意图:pi-web 是 Web UI,没有 TUI 终端主题,因此不提供 theme 对象。注释也明确写了 "Theme switching is not supported in pi-web extension UI yet"。
第 2 行(pi-mcp-adapter init.ts 行 264)
pi-mcp-adapter 的 updateStatusBar 函数假定 ui 存在则 ui.theme 也一定存在,只做了对 ui 自身的 falsy 守卫,未守卫 ui.theme:
// pi-mcp-adapter/init.ts
export function updateStatusBar(state: McpExtensionState): void {
const ui = state.ui;
if (!ui) return; // ← 只检查了 ui
// ...
ui.setStatus("mcp", ui.theme.fg("accent", // ← ui.theme 是 undefined
`MCP: ${connectedCount}/${total} servers`));
}
ui.theme 是 undefined,访问 undefined.fg 触发 TypeError: Cannot read properties of undefined (reading 'fg')。
第 3 行(pi-mcp-adapter init.ts 行 174)
initializeMcp 在结束时启动定时健康检查,将 updateStatusBar 注册为 reconnect 回调:
// pi-mcp-adapter/init.ts
lifecycle.setReconnectCallback((serverName) => {
updateServerMetadata(state, serverName);
updateMetadataCache(state, serverName);
state.failureTracker.delete(serverName);
updateStatusBar(state); // ← 每次重连都触发
});
lifecycle.startHealthChecks(); // ← 启动定时器
由于该回调在定时器/事件循环中异步执行,异常未被任何 try/catch 捕获,表现为持续的 uncaughtException。
四、为什么之前没发现
- 不使用
pi-mcp-adapter扩展的 pi-web 用户不会触发此错误。 - pi CLI(TUI 模式)正常提供了 theme 对象,
ui.theme.fg(...)工作正常。 - 该问题存在于 pi-web 的 RPC 模式限制(无 theme)与 pi-mcp-adapter 的乐观假设(有 ui 就有 theme)之间的接口契约 mismatch。
五、修复方案
修复思路
在 pi-web 侧提供一个最小化的 dummy theme 对象,使其 fg、bg、bold 方法直接透传文本(不添加 ANSI 转义码,因为 Web UI 不需要终端控制字符)。
TypeScript 源码修复(lib/rpc-manager.ts)
修改前:
get theme() { return undefined; },
修改后:
// Provide a minimal dummy theme so extensions that call theme.fg/bg/bold
// (e.g., pi-mcp-adapter's updateStatusBar) do not crash with
// "Cannot read properties of undefined (reading 'fg')".
// ANSI escape codes are not needed here — pi-web renders status text as-is.
get theme() {
return {
fg: (_color: string, text: string) => text,
bg: (_color: string, text: string) => text,
bold: (text: string) => text,
};
},
生产环境编译产物修复
全局安装的 pi-web(pnpm 路径):
C:\Users\Administrator\AppData\Local\pnpm\global\5\.pnpm\
@[email protected]_...\node_modules\@agegr\pi-web\.next\
server\chunks\7601.js
该文件是 Next.js 编译后的 minified 产物,get theme getter 位于单行内:
get theme(){return}
使用 PowerShell 正则替换为:
get theme(){return{fg:(a,b)=>{return b},bg:(a,b)=>{return b},bold:a=>{return a}}}
具体命令:
$chunk = 'C:\Users\Administrator\AppData\Local\pnpm\global\5\.pnpm\@[email protected]_@lobeh_a64e309d864d3267dcecc8c578c3f9c6\node_modules\@agegr\pi-web\.next\server\chunks\7601.js'
(Get-Content $chunk -Raw) -replace 'get theme\(\)\{return\}', 'get theme(){return{fg:(a,b)=>{return b},bg:(a,b)=>{return b},bold:a=>{return a}}}' | Set-Content $chunk -NoNewline
生效方式
重启 pi-web:
pi-web -H <host>
谢谢 fixed 3c7a71298fa030f07abbddf4a6f75b8020c07eb9