import { Client, ClientCredentialsProvider, computeScopeUnion, ProtocolError, SdkError, SdkErrorCode, SSEClientTransport, StreamableHTTPClientTransport, UnsupportedProtocolVersionError, type AuthProvider, type OAuthClientProvider, type VersionNegotiationMode, type VersionNegotiationOptions, } from "@modelcontextprotocol/client"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import type { Logger, McpCallToolOptions, McpCallToolRequest, McpConnectionSnapshot, McpConnectOptions, McpContentBlock, McpElicitationPort, McpElicitationTrace, McpListResourcesRequest, McpListResourcesResult, McpListResourceTemplatesResult, McpNotificationPort, McpOAuthConfig, McpPort, McpReadResourceRequest, McpReadResourceResult, McpResourceSubscriptionRequest, McpServerConfig, McpServerStatus, McpToolCallResult, McpToolDescriptor, OfficialMcpAuthFailureReason, OfficialMcpAuthHeadersPort, OfficialMcpTrustedOriginRegistry, } from "@zcode/contracts"; import { McpResourceSubscribeUnsupportedError, ZCODE_MCP_SERVER_REQUEST_ID_META_KEY, } from "@zcode/contracts"; import { OFFICIAL_MCP_AUTH_META_KEY, ZCODE_OFFICIAL_MCP_AUTH_TYPE, type McpServerFailureKind, type OfficialMcpAuthFailureKind, } from "@zcode/shared"; import { buildMcpAppsClientCapabilities } from "@zcode/shared/mcp-apps"; import { createHash, randomUUID } from "node:crypto"; import { resolve } from "node:path"; import { isDeepStrictEqual } from "node:util"; import { createSharedZCodeCredentialStore, type SharedZCodeCredentialStore, } from "../auth/shared-credentials.js"; import { normalizeMcpToolDescriptor } from "./descriptor.js"; import { buildMcpStdioEnv, createMcpTransportFetch, type NetworkEgressEnvPolicy, } from "./network.js"; import { loadCredentialPair } from "./oauth-credentials.js"; import { classifyInteractiveAuthorizationTrigger, type InteractiveAuthorizationTrigger, } from "./oauth-errors.js"; import { MCP_OAUTH_AUTHORIZATION_TRANSACTION_TTL_MS, runMcpInteractiveAuthorization, type McpInteractiveAuthorizationOutcome, } from "./oauth-interactive.js"; import { createMcpOAuthTokenProvider } from "./oauth-provider.js"; import { createCredentialKeyPrefix, type McpOAuthAuthorizationContext, type McpOAuthRuntimeOptions, } from "./oauth.js"; import { createOfficialMcpAuthFetch, OfficialMcpAuthError, type OfficialMcpServerResponseInfo, } from "./official-auth.js"; import { createMcpConnectionPool, type McpConnectionContext, type McpConnectionPool, } from "./pool.js"; import { terminateMcpStdioProcessTree } from "./process-tree.js"; import { McpResourceSubscriptionRegistry } from "./resource-subscriptions.js"; import { ProcessTreeStdioClientTransport } from "./stdio-transport.js"; import type { McpTelemetryTracker } from "./telemetry.js"; import { createMcpDeadline, McpTimeoutError, remainingMcpDeadlineMs, waitWithinMcpDeadline, withTimeout, type McpDeadline, } from "./timeout.js"; const DEFAULT_MCP_TIMEOUT_MS = 30_000; // 存活探测只允许占用很短的时间:它挂在设置页刷新的同步路径上,超时即判死并触发重连。 const MCP_PING_TIMEOUT_MS = 5_000; const MAX_MCP_VERSION_PROBE_TIMEOUT_MS = 5_000; const MCP_STDIO_STDERR_LOG_MAX_CHARS = 4_000; /** * span → request id 的暂存条数上限。正常情况下每条都会在同一次 tool call 结束时被取走, * 留下的只有无人认领的(如连接期请求),几十条足够,纯为防止长会话下无界增长。 */ const MAX_TRACKED_SERVER_REQUEST_IDS = 64; export interface CreateMcpAdapterOptions { clientName?: string; clientVersion?: string; connectionContext?: McpConnectionContext; env?: NodeJS.ProcessEnv; logger?: Logger; telemetry?: McpTelemetryTracker; mcpOAuth?: McpOAuthRuntimeOptions; network?: NetworkEgressEnvPolicy; /** * 官方 Server MCP 鉴权依赖。trustedOrigins 缺失时仍 fail closed;authHeadersPort * 可缺省,此时各请求匿名降级并交给服务端做权威判定。 */ officialMcpAuth?: { authHeadersPort?: OfficialMcpAuthHeadersPort; trustedOrigins: OfficialMcpTrustedOriginRegistry; /** * 当前 ZCode API origin。stdio 形态没有 `url` 可供校验,targetOrigin 只能由宿主给出 * ——插件因此无法把身份头导向别的 origin。 * 与 trustedOrigins 的 `resolveZCodeApiOrigin` 必须同源,否则两侧判定会分叉。 */ resolveZCodeApiOrigin?: () => string; workspaceIdentity?: string; }; workingDirectory?: string; /** 有此端口才向 server 宣告 elicitation 能力;缺省时 server 的 elicitation/create 收到 method not found。 */ elicitation?: McpElicitationPort; /** server → client 通知(资源更新 / 列表变化 / 工具列表变化 / 日志)的出口;缺省只记 debug。 */ notifications?: McpNotificationPort; } type McpClient = Client; type McpTransport = StdioClientTransport | StreamableHTTPClientTransport | SSEClientTransport; type AuthorizationCodeOAuthConfig = Extract; /** * stdio 官方 MCP 的身份头载荷,随每条出站协议消息的 `_meta` 下发。 * * 失败也下发(`ok: false` + 枚举 reason);stdio 插件拿不到头时不会去打官方端点。HTTP 路径则 * 由 adapter 发起无身份的 tools/call,让 ZCode server 返回权威结构化错误。把 reason 交给 stdio * 插件才能让它把"未登录"与"无 Coding Plan * 套餐"如实呈现给用户,而不是静默降级成一句莫名其妙的失败。 */ type OfficialMcpAuthMetaPayload = | { ok: true; headers: Record } | { ok: false; reason: OfficialMcpAuthFailureReason }; /** 订阅的 server → client 通知方法名(MCP 规范)。 */ export const MCP_NOTIFICATION_TOOLS_LIST_CHANGED = "notifications/tools/list_changed"; export const MCP_NOTIFICATION_RESOURCES_UPDATED = "notifications/resources/updated"; export const MCP_NOTIFICATION_RESOURCES_LIST_CHANGED = "notifications/resources/list_changed"; export const MCP_NOTIFICATION_MESSAGE = "notifications/message"; interface McpServerRecord { client?: McpClient; abortController?: AbortController; connecting?: Promise; config: McpServerConfig; status: McpServerStatus; tools: McpToolDescriptor[]; /** 收到 tools/list_changed 后置位,下一次 listTools 重拉。 */ toolsStale?: boolean; transport?: McpTransport; } /** resources/list 项:只透传标准字段(含 _meta),丢掉 SDK 附加的运行时字段。 */ function pickResourceDescriptor( resource: unknown, includeUri = true, ): { uri?: string; name?: string; title?: string; description?: string; mimeType?: string; _meta?: Record; } { const item = isRecord(resource) ? resource : {}; return { ...(includeUri ? { uri: typeof item.uri === "string" ? item.uri : "" } : {}), ...(typeof item.name === "string" ? { name: item.name } : {}), ...(typeof item.title === "string" ? { title: item.title } : {}), ...(typeof item.description === "string" ? { description: item.description } : {}), ...(typeof item.mimeType === "string" ? { mimeType: item.mimeType } : {}), ...(isRecord(item._meta) ? { _meta: item._meta } : {}), }; } export function createMcpAdapter(options: CreateMcpAdapterOptions = {}): McpPort { return new NodeMcpAdapter(options); } export function createMcpAdapterConnectionPool( options: CreateMcpAdapterOptions = {}, ): McpConnectionPool { return createMcpConnectionPool({ logger: options.logger, telemetry: options.telemetry, createAdapter: ({ connectionContext, workingDirectory }) => createMcpAdapter({ ...options, connectionContext, workingDirectory: workingDirectory ?? options.workingDirectory, }), }); } export { createMcpConnectionPool, type McpConnectionPool, type McpConnectionPoolOptions, } from "./pool.js"; export { createMcpTelemetryTracker, resolvePluginName, type McpTelemetryTracker, type McpTrackedProcess, } from "./telemetry.js"; /** elicitation 模式白名单(MCP form 与 兼容别名)。 */ const SUPPORTED_ELICITATION_MODES = new Set(["form", "openai/form", "openaiForm"]); function isSupportedElicitationMode(mode: unknown): boolean { return mode === undefined || (typeof mode === "string" && SUPPORTED_ELICITATION_MODES.has(mode)); } class NodeMcpAdapter implements McpPort { private readonly adapterInstanceId = randomUUID(); private readonly clientName: string; private readonly clientVersion: string; private readonly connectionContext?: McpConnectionContext; private readonly env?: NodeJS.ProcessEnv; private readonly logger?: Logger; private readonly mcpOAuth?: McpOAuthRuntimeOptions; private readonly network?: NetworkEgressEnvPolicy; private readonly officialMcpAuth?: CreateMcpAdapterOptions["officialMcpAuth"]; private readonly telemetry?: McpTelemetryTracker; private readonly connectionGenerations = new Map(); private credentialStore?: SharedZCodeCredentialStore; /** * 官方鉴权失败分类的暂存槽。不能从 error 对象读——SDK 的 version * negotiation 会把 OfficialMcpAuthError 重新包装成普通 Error,instanceof 失效; * 也不允许按错误文本反解。因此在抛出点写入,failConnection 取用后立即清除。 */ private readonly lastOfficialAuthKind = new Map(); /** * span → 服务端 request id。只有官方 MCP 会写入(唯一能看到响应头的地方是 auth fetch * wrapper),供 in-band 失败(HTTP 200 + `isError`)把 id 带回 tool result。 * * 用 span 而不是 traceId 作键:traceId 覆盖整个顶层 session,同一 session 的多次调用 * 共用它,关联会串号;span 是一次 tool call 的粒度。 * * 有界并即取即删:拿不到匹配的 span(如 initialize / tools/list,它们没有 `_meta`) * 就让条目自然被挤出,绝不"取最近一次"兜底——那会把上一次调用的 id 贴到这一次的失败上。 */ private readonly serverRequestIdBySpan = new Map(); private readonly connectionDiagnosticByServer = new Map< string, Pick >(); private readonly records = new Map(); private readonly workingDirectory?: string; private readonly elicitation?: McpElicitationPort; private readonly notifications?: McpNotificationPort; /** 资源订阅登记表 serverName → uri → subscriberKey;唯一 owner,连接层计数。 */ private readonly resourceSubscriptions = new McpResourceSubscriptionRegistry(); /** tools/list_changed 累计次数,runtime 按此判断是否需要重注册。 */ private toolListRevisionCounter = 0; /** 每个 server 当前正在执行的工具调用 trace;elicitation 借此归属到会话 / 工具行。 */ private readonly activeCallTraces = new Map(); constructor(options: CreateMcpAdapterOptions) { this.clientName = options.clientName ?? "zcode"; this.clientVersion = options.clientVersion ?? "0.0.0"; this.connectionContext = options.connectionContext; this.env = options.env; this.logger = options.logger?.child({ ...this.connectionContext, module: "adapters.mcp", }); this.mcpOAuth = options.mcpOAuth; this.network = options.network; this.officialMcpAuth = options.officialMcpAuth; this.telemetry = options.telemetry; this.workingDirectory = options.workingDirectory; this.elicitation = options.elicitation; this.notifications = options.notifications; } async connectConfiguredServers( servers: Record, options: McpConnectOptions = {}, ): Promise { const startedAt = Date.now(); const serverNames = Object.keys(servers); this.logger?.info("MCP configured servers connection started", { event: "mcp.configured_servers.connect.started", serverCount: serverNames.length, serverNames, status: "started", }); const configuredNames = new Set(Object.keys(servers)); await Promise.all( Array.from(this.records.keys()) .filter((name) => !configuredNames.has(name)) .map((name) => this.disconnectServer(name)), ); await Promise.all( Object.entries(servers).map(([name, config]) => { const record = this.records.get(name); if ( record?.connecting && record.status.status === "connecting" && record.status.authorization && isDeepStrictEqual(record.config, config) ) { // 相同配置的全量收敛可能与 OAuth callback 等待重叠;重新 connect // 会关闭原 session,使浏览器中已打开的授权 URL、PKCE/state 和 callback 一并失效。 // 连接生命周期可以共享,但 Session 的 15 秒等待预算和 AbortSignal 不能继承设置页的 5 分钟预算。 return this.waitForSharedConnection(name, record, options); } return this.connectServer(name, config, options); }), ); const statuses = await this.status(); const tools = await this.listTools(); const statusCounts = Object.values(statuses).reduce>( (counts, status) => { counts[status.status] = (counts[status.status] ?? 0) + 1; return counts; }, {}, ); this.logger?.info("MCP configured servers connection completed", { durationMs: Date.now() - startedAt, event: "mcp.configured_servers.connect.completed", serverCount: serverNames.length, status: "completed", statusCounts, toolCount: tools.length, }); return { statuses, tools, }; } async connectServer( name: string, config: McpServerConfig, options: McpConnectOptions = {}, ): Promise { const startedAt = Date.now(); const timeoutMs = config.timeoutMs ?? DEFAULT_MCP_TIMEOUT_MS; const generation = this.nextConnectionGeneration(name); this.lastOfficialAuthKind.delete(name); this.connectionDiagnosticByServer.delete(name); this.logger?.info("MCP server connection started", { event: "mcp.server.connect.started", mcpServerName: name, status: "started", timeoutMs, transport: config.type, }); await this.closeRecord(name); if (config.enabled === false) { const status = this.createStatus(config, "disabled"); this.records.set(name, { config, status, tools: [] }); this.logger?.info("MCP server connection skipped", { durationMs: Date.now() - startedAt, event: "mcp.server.connect.skipped", mcpServerName: name, status: "completed", transport: config.type, }); return status; } const abortController = new AbortController(); const abortExternal = () => { abortController.abort( options.signal?.reason instanceof Error ? options.signal.reason : undefined, ); }; if (options.signal?.aborted) { abortExternal(); } else { options.signal?.addEventListener("abort", abortExternal, { once: true }); } const connectingStatus = this.createStatus(config, "connecting"); const record: McpServerRecord = { abortController, config, status: connectingStatus, tools: [], }; this.records.set(name, record); const connecting = this.openServerConnection({ config, generation, name, oauthAuthorizationTimeoutMs: options.oauthAuthorizationTimeoutMs, signal: abortController.signal, timeoutMs, workingDirectory: options.workingDirectory, }).finally(() => { options.signal?.removeEventListener("abort", abortExternal); }); record.connecting = connecting; // 过去 `oauthAuthorizationTimeoutMs`(session 的 15 秒)被当成 OAuth 事务寿命, // 15 秒后连同 callback listener 一起关掉,真人根本来不及在浏览器里完成授权(现场证据: // 一次成功授权耗时约 74 秒)。现在它只作为**本 caller 的等待预算**:到点返回当时的 // snapshot(含授权 URL),后台连接与 300 秒授权事务继续存活。 return await this.waitForSharedConnection(name, record, options); } async disconnectServer(name: string): Promise { const record = this.records.get(name); if (!record) return undefined; this.nextConnectionGeneration(name); await this.closeRecord(name); const status = this.createStatus(record.config, "disconnected"); this.records.set(name, { config: record.config, status, tools: [], }); return status; } async status(): Promise> { return Object.fromEntries( Array.from(this.records.entries()).map(([name, record]) => [name, record.status]), ); } // HTTP/SSE MCP 服务被停掉时不会派发 onclose(没有常驻流可断),record 会长期停在 // connected;设置页刷新读到的就是这份"无声死亡"的旧快照,看起来像刷新按钮没生效。 // ping 是 MCP 基础协议方法,用它把 transport 存活性显式化。 async pingServer(name: string, options: { timeoutMs?: number } = {}): Promise { const record = this.records.get(name); if (!record?.client || record.status.status !== "connected") { return false; } const timeoutMs = Math.min( options.timeoutMs ?? MCP_PING_TIMEOUT_MS, record.config.timeoutMs ?? DEFAULT_MCP_TIMEOUT_MS, ); const generation = this.connectionGenerations.get(name) ?? 0; try { await record.client.ping({ timeout: timeoutMs }); return true; } catch (error) { // server 回了 JSON-RPC 错误(例如未实现 ping)说明连接本身是活的,不能据此拆连接。 if (isPeerAnsweredError(error)) { return true; } if (!this.isCurrentConnection(name, generation)) return false; const current = this.records.get(name); if (current && current.client === record.client) { current.status = this.createStatus(current.config, "disconnected", { error: "MCP server did not answer ping", failureKind: "unexpected_disconnect", }); } this.logger?.warn("MCP server ping failed", { ...this.connectionContext, error: error instanceof Error ? error.message : String(error), event: "mcp.server.ping.failed", mcpServerName: name, status: "failed", transport: record.config.type, }); return false; } } toolListRevision(): number { return this.toolListRevisionCounter; } async listTools(): Promise { // 收到 notifications/tools/list_changed 的 server 在下一次 listTools 时重新拉取 // (runtime 只在回合开始前调用,回合内工具表不变)。 for (const [name, record] of this.records) { if (!record.toolsStale || !record.client || record.status.status !== "connected") continue; try { const listed = await record.client.listTools(); record.tools = listed.tools.map((tool) => normalizeMcpToolDescriptor( name, tool, record.config.timeoutMs, record.config.type === "http" && record.config.auth?.type === ZCODE_OFFICIAL_MCP_AUTH_TYPE, ), ); record.status = { ...record.status, toolCount: record.tools.length }; record.toolsStale = false; this.logger?.info("MCP tool list refreshed after list_changed", { event: "mcp.tools.refreshed", mcpServerName: name, toolCount: record.tools.length, }); } catch (error) { this.logger?.warn("MCP tool list refresh failed", { event: "mcp.tools.refresh_failed", mcpServerName: name, error: error instanceof Error ? error.message : String(error), }); } } return Array.from(this.records.values()).flatMap((record) => record.tools); } /** `resources/list`,分页游标透传。 */ async listResources( request: McpListResourcesRequest, options: McpCallToolOptions = {}, ): Promise { const { record, deadline, timeoutMessage } = await this.resolveForResourceRequest( request.serverName, "resources/list", options, ); const result = await record.client.listResources( request.cursor ? { cursor: request.cursor } : {}, { signal: options.signal, timeout: remainingMcpDeadlineMs(deadline, timeoutMessage) }, ); return { resources: (result.resources ?? []).map((resource) => { const { uri, ...rest } = pickResourceDescriptor(resource); return { uri: uri ?? "", ...rest }; }), ...(typeof result.nextCursor === "string" ? { nextCursor: result.nextCursor } : {}), }; } async listResourceTemplates( request: McpListResourcesRequest, options: McpCallToolOptions = {}, ): Promise { const { record, deadline, timeoutMessage } = await this.resolveForResourceRequest( request.serverName, "resources/templates/list", options, ); const result = await record.client.listResourceTemplates( request.cursor ? { cursor: request.cursor } : {}, { signal: options.signal, timeout: remainingMcpDeadlineMs(deadline, timeoutMessage) }, ); return { resourceTemplates: (result.resourceTemplates ?? []).map((template) => { const item = template as Record; return { uriTemplate: typeof item.uriTemplate === "string" ? item.uriTemplate : "", ...pickResourceDescriptor(item, false), }; }), ...(typeof result.nextCursor === "string" ? { nextCursor: result.nextCursor } : {}), }; } /** * 按 (serverName, uri) 计数的订阅。同一连接被多个 session 共享时只向 server 订阅一次; * server 没声明 resources.subscribe 能力 → McpResourceSubscribeUnsupportedError。 */ async subscribeResource( request: McpResourceSubscriptionRequest, options: McpCallToolOptions = {}, ): Promise { const { record, deadline, timeoutMessage } = await this.resolveForResourceRequest( request.serverName, "resources/subscribe", options, ); if (!record.client.getServerCapabilities()?.resources?.subscribe) { throw new McpResourceSubscribeUnsupportedError(request.serverName); } const { first } = this.resourceSubscriptions.add( request.serverName, request.uri, request.subscriberKey, ); if (!first) return; try { await record.client.subscribeResource( { uri: request.uri }, { signal: options.signal, timeout: remainingMcpDeadlineMs(deadline, timeoutMessage) }, ); } catch (error) { this.resourceSubscriptions.remove(request.serverName, request.uri, request.subscriberKey); throw error; } this.logger?.info("MCP resource subscribed", { event: "mcp.resource.subscribe", mcpServerName: request.serverName, uri: request.uri, status: "completed", }); } async unsubscribeResource( request: McpResourceSubscriptionRequest, options: McpCallToolOptions = {}, ): Promise { const removed = this.resourceSubscriptions.remove( request.serverName, request.uri, request.subscriberKey, ); if (!removed?.last) return; await this.unsubscribeOnServer(request.serverName, request.uri, options); } async unsubscribeResourcesBySubscriber(subscriberKeyPrefix: string): Promise { const released = this.resourceSubscriptions.removeByPrefix(subscriberKeyPrefix); for (const { serverName, uri } of released) { await this.unsubscribeOnServer(serverName, uri, {}); } return released.length; } /** 登记表快照(测试 / 诊断):serverName → uri → 订阅者数。 */ resourceSubscriptionCounts(): Record> { return this.resourceSubscriptions.counts(); } private async unsubscribeOnServer( serverName: string, uri: string, options: McpCallToolOptions, ): Promise { const record = this.records.get(serverName); if (!record?.client || record.status.status !== "connected") return; try { await record.client.unsubscribeResource({ uri }, { signal: options.signal }); this.logger?.info("MCP resource unsubscribed", { event: "mcp.resource.unsubscribe", mcpServerName: serverName, uri, status: "completed", }); } catch (error) { // 退订失败只记日志:登记表已清,server 多发的通知因没有订阅者会被丢弃。 this.logger?.warn("MCP resource unsubscribe failed", { event: "mcp.resource.unsubscribe", mcpServerName: serverName, uri, error: error instanceof Error ? error.message : String(error), status: "failed", }); } } /** 断连重连后按登记表重放 subscribe(A3);单条失败不影响其它。 */ private async replayResourceSubscriptions(serverName: string, client: McpClient): Promise { const uris = this.resourceSubscriptions.uris(serverName); if (uris.length === 0) return; if (!client.getServerCapabilities()?.resources?.subscribe) return; for (const uri of uris) { try { await client.subscribeResource({ uri }); } catch (error) { this.logger?.warn("MCP resource subscription replay failed", { event: "mcp.resource.subscribe", mcpServerName: serverName, uri, error: error instanceof Error ? error.message : String(error), status: "failed", }); } } this.logger?.info("MCP resource subscriptions replayed after reconnect", { event: "mcp.resource.subscribe.replayed", mcpServerName: serverName, uriCount: uris.length, }); } private async resolveForResourceRequest( serverName: string, operation: string, options: McpCallToolOptions, ): Promise<{ record: McpServerRecord & { client: McpClient }; deadline: McpDeadline; timeoutMessage: string; }> { const timeoutMs = options.timeoutMs ?? this.records.get(serverName)?.config.timeoutMs ?? DEFAULT_MCP_TIMEOUT_MS; const deadline = createMcpDeadline(timeoutMs); const timeoutMessage = `MCP ${operation} ${serverName} timed out after ${timeoutMs}ms`; const record = await this.resolveConnectedRecord( serverName, deadline, timeoutMessage, options.signal, ); return { record, deadline, timeoutMessage }; } /** * server 主动通知。订阅类通知附上当前登记的 subscriberKey(宿主据此按会话拆分); * tools/list_changed 只标记 stale,下一次 listTools 再拉;没有端口时 SDK 默认丢弃。 */ private installNotificationHandlers(client: McpClient, serverName: string): void { const setNotificationHandler = (client as unknown as { setNotificationHandler?: unknown }) .setNotificationHandler; if (typeof setNotificationHandler !== "function") return; // SDK 2.x:第一个参数是通知方法名(spec 方法直接用字符串),schema 是可选的第二个参数。 const typed = client as unknown as { setNotificationHandler(method: string, handler: (notification: unknown) => void): void; }; typed.setNotificationHandler(MCP_NOTIFICATION_TOOLS_LIST_CHANGED, () => { const record = this.records.get(serverName); if (record) record.toolsStale = true; this.toolListRevisionCounter += 1; this.notifications?.onNotification({ kind: "toolListChanged", serverName }); }); typed.setNotificationHandler(MCP_NOTIFICATION_RESOURCES_UPDATED, (notification) => { const uri = (notification as { params?: { uri?: unknown } }).params?.uri; if (typeof uri !== "string") return; this.notifications?.onNotification({ kind: "resourceUpdated", serverName, uri, subscribers: this.resourceSubscriptions.subscribers(serverName, uri), }); }); typed.setNotificationHandler(MCP_NOTIFICATION_RESOURCES_LIST_CHANGED, () => { this.notifications?.onNotification({ kind: "resourceListChanged", serverName, subscribers: this.resourceSubscriptions.allSubscribers(serverName), }); }); typed.setNotificationHandler(MCP_NOTIFICATION_MESSAGE, (notification) => { const params = (notification as { params?: Record }).params ?? {}; this.notifications?.onNotification({ kind: "loggingMessage", serverName, level: typeof params.level === "string" ? params.level : "info", ...(typeof params.logger === "string" ? { logger: params.logger } : {}), data: params.data, }); }); } async callTool( request: McpCallToolRequest, options: McpCallToolOptions = {}, ): Promise { const timeoutMs = options.timeoutMs ?? this.records.get(request.serverName)?.config.timeoutMs ?? DEFAULT_MCP_TIMEOUT_MS; const deadline = createMcpDeadline(timeoutMs); const timeoutMessage = `MCP tool ${request.serverName}/${request.toolName} timed out after ${timeoutMs}ms`; let record: McpServerRecord & { client: McpClient }; if (options.appConnection) { const snapshot = this.appConnectionSnapshot(request.serverName); const current = this.records.get(request.serverName); if ( !snapshot || snapshot.identity !== options.appConnection.identity || snapshot.generation !== options.appConnection.generation || !current?.client ) throw new Error("MCP App connection changed"); options.signal?.throwIfAborted(); record = current as McpServerRecord & { client: McpClient }; } else { record = await this.resolveConnectedRecord( request.serverName, deadline, timeoutMessage, options.signal, ); } try { return await this.callToolOnClient( record.client, request, remainingMcpDeadlineMs(deadline, timeoutMessage), options.signal, options.onProgress, ); } catch (error) { if (options.appConnection) throw error; // 连接建立后 token 过期、被撤销或 scope 不足时, // 过去这些认证错误原样冒泡,用户看到裸错误且永远不会自愈——OAuth 自愈只存在于 // startup connect 路径。现在运行期与建连期共用同一套 Phase 2 → Phase 1 编排。 const trigger = classifyInteractiveAuthorizationTrigger(error); if (trigger && record.config.type !== "stdio") { return await this.recoverToolCallAuthorization({ error, record, request, deadline, timeoutMessage, trigger, ...(options.signal ? { signal: options.signal } : {}), ...(options.onProgress ? { onProgress: options.onProgress } : {}), }); } // 防 onclose 尚未派发的竞态:SDK 在 transport 已断时抛裸 "Not connected"。 // 只对这一种确定的断连错误重连重试一次,其余错误原样冒泡。 if (!(error instanceof Error) || error.message !== "Not connected") throw error; try { await waitWithinMcpDeadline( this.reconnectForCall(request.serverName, record.config), deadline, timeoutMessage, options.signal, ); } catch (reconnectError) { this.logger?.warn("MCP server reconnect failed", { error: reconnectError instanceof Error ? reconnectError.message : String(reconnectError), event: "mcp.server.reconnect.failed", mcpServerName: request.serverName, status: "failed", }); throw error; } const revived = this.records.get(request.serverName); if (!revived?.client || revived.status.status !== "connected") throw error; return await this.callToolOnClient( revived.client, request, remainingMcpDeadlineMs(deadline, timeoutMessage), options.signal, options.onProgress, ); } } /** * 插件 UI 的 `ui://` 资源读取。与 callTool 共用连接等待 / 断连重连骨架(resolveConnectedRecord), * 保证走同一条连接与 OAuth 恢复路径;不做 tool 级 OAuth 二次恢复——资源读取失败直接冒泡, * 由 UI 回退到普通卡片。 */ async readResource( request: McpReadResourceRequest, options: McpCallToolOptions = {}, ): Promise { const timeoutMs = options.timeoutMs ?? this.records.get(request.serverName)?.config.timeoutMs ?? DEFAULT_MCP_TIMEOUT_MS; const deadline = createMcpDeadline(timeoutMs); const timeoutMessage = `MCP resource ${request.serverName} ${request.uri} timed out after ${timeoutMs}ms`; const record = await this.resolveConnectedRecord( request.serverName, deadline, timeoutMessage, options.signal, ); const result = await record.client.readResource( { uri: request.uri }, { signal: options.signal, timeout: remainingMcpDeadlineMs(deadline, timeoutMessage) }, ); return { contents: (result.contents ?? []).map((content) => { const item = content as Record; return { uri: typeof item.uri === "string" ? item.uri : request.uri, ...(typeof item.mimeType === "string" ? { mimeType: item.mimeType } : {}), ...(typeof item.text === "string" ? { text: item.text } : {}), ...(typeof item.blob === "string" ? { blob: item.blob } : {}), ...(isRecord(item._meta) ? { _meta: item._meta } : {}), }; }), }; } /** * callTool / readResource 共用:等待共享 connecting 任务、对已断连 record 重连一次、 * 返回处于 connected 的 record。抽出来是为了让资源读取与工具调用走完全相同的恢复路径。 */ private async resolveConnectedRecord( serverName: string, deadline: McpDeadline, timeoutMessage: string, signal: AbortSignal | undefined, ): Promise { const pending = this.records.get(serverName)?.connecting; if (pending) { // 修复原因:connecting 是 adapter 持有的共享连接/OAuth 恢复任务。过去这里裸 await, // tool caller 的 timeout/abort 完全失效;但直接 abort 底层任务又会关闭其他 caller 共用的 // callback listener。这里只限制当前 waiter,共享任务继续由 record 生命周期持有。 await waitWithinMcpDeadline(pending, deadline, timeoutMessage, signal); } // Bugfix:stdio MCP 子进程死亡后(如 node_repl 被异步错误击穿),此前没有任何恢复路径: // 连接只在 session 创建时建立一次,session resume 也不重建,该会话的工具从此永远失败。 // 这里在调用前对已断连的 record 重连一次;server 进程内状态(如 REPL 变量)不可恢复, // 但工具本身恢复可用。 const disconnected = this.records.get(serverName); if (disconnected && disconnected.status.status === "disconnected") { await waitWithinMcpDeadline( this.reconnectForCall(serverName, disconnected.config), deadline, timeoutMessage, signal, ); } const record = this.records.get(serverName); if (!record?.client || record.status.status !== "connected") { throw new Error(`MCP server is not connected: ${serverName}`); } return record as McpServerRecord & { client: McpClient }; } /** 连接期诊断按 server 保存;tool call request id 继续按 span 隔离。 */ private rememberServerResponse( serverName: string, response: OfficialMcpServerResponseInfo, ): void { if ( !response.spanId && response.rpcMethod !== "tools/call" && this.records.get(serverName)?.status.status === "connecting" ) { if (response.failureKind) { this.connectionDiagnosticByServer.set(serverName, { failureKind: response.failureKind, ...(response.serverRequestId ? { serverRequestId: response.serverRequestId } : {}), }); } return; } if (!response.spanId) return; if (!response.serverRequestId) return; // 401 重试会对同一 span 产生两条响应,后写覆盖——留下的是最终那次,正是要报的那个。 this.serverRequestIdBySpan.set(response.spanId, response.serverRequestId); while (this.serverRequestIdBySpan.size > MAX_TRACKED_SERVER_REQUEST_IDS) { const oldest = this.serverRequestIdBySpan.keys().next(); if (oldest.done) break; this.serverRequestIdBySpan.delete(oldest.value); } } /** 取出并清除该 span 的 request id。取不到返回 undefined,不做任何兜底猜测。 */ private takeServerRequestId(spanId: string | undefined): string | undefined { if (!spanId) return undefined; const requestId = this.serverRequestIdBySpan.get(spanId); if (requestId !== undefined) this.serverRequestIdBySpan.delete(spanId); return requestId; } /** * 解析 stdio 官方 MCP 本次出站协议消息的身份头。 * 返回 undefined 表示"不是官方 stdio server"——此时 `_meta` 里绝不能出现该键,否则等于把 * 身份头广播给任意第三方插件。 */ private async resolveOfficialStdioAuthMeta( serverName: string, config: McpServerConfig, signal: AbortSignal | undefined, ): Promise { if (config.type !== "stdio" || !isOfficialAuthConfig(config) || !config.official) { return undefined; } const official = config.official; const authHeadersPort = this.officialMcpAuth?.authHeadersPort; const trustedOrigins = this.officialMcpAuth?.trustedOrigins; const resolveZCodeApiOrigin = this.officialMcpAuth?.resolveZCodeApiOrigin; const logBase = { event: "mcp.official_auth.stdio_meta", mcpKey: official.mcpKey, mcpServerName: serverName, module: "adapters.mcp", }; const fail = (reason: OfficialMcpAuthFailureReason): OfficialMcpAuthMetaPayload => { // 刻意不写 lastOfficialAuthKind:那个 map 只被 failConnection 读取,用来给**连接失败** // 打分类标签。stdio 的身份头缺失不会让连接失败,写进去会一直留着,等到该 server 之后 // 因为别的原因(子进程死掉等)真正断连时被当成断连原因记进日志,属误导。 // 本路径的可观测性由下面这条自己的 event + 下发给插件的 reason 承担。 this.logger?.warn("Official MCP stdio auth headers unavailable", { ...logBase, reason, status: "failed", }); return { ok: false, reason }; }; // standalone CLI 没有 host auth port。不静默省略该键:插件区分不了"宿主不支持"与 // "宿主支持但我没登录",只有显式 reason 才能给出正确的用户提示。 if (!authHeadersPort || !trustedOrigins || !resolveZCodeApiOrigin) { return fail("official_auth_unavailable"); } // stdio 没有 url,origin 由宿主给出而非插件声明。isTrusted 在此退化为恒真断言,但仍要调用: // 它同时校验 https、拒绝带 username/password 的 URL,并让 dev loopback 开关继续生效。 // // 这两步原来裸调用。origin 解析依赖 settings / 运行时环境,isTrusted 是 // 注入的实现,两者都可能抛。异常裸冒泡会绕过整个失败分类:插件收不到 `{ok:false, reason}`, // 而 reason 是跨 adapter / host / UI 的契约(决定提示文案与是否重试)。因此统一映射为 // official_auth_unavailable——宿主侧解析不出可信 origin,对插件而言就是"官方鉴权不可用"。 // 错误文本只进日志,绝不参与流程判断。 let targetOrigin: string; let trust: Awaited>; try { targetOrigin = resolveZCodeApiOrigin(); trust = await trustedOrigins.isTrusted({ mcpKey: official.mcpKey, origin: targetOrigin, pluginId: official.pluginId, }); } catch (error) { this.logger?.warn("Official MCP stdio origin resolution failed", { ...logBase, error: error instanceof Error ? error.message : String(error), errorName: error instanceof Error ? error.name : "unknown", pluginId: official.pluginId, }); return fail("official_auth_unavailable"); } if (!trust.trusted) { this.logger?.warn("Official MCP stdio origin is not trusted", { ...logBase, detail: trust.detail ?? "unknown", pluginId: official.pluginId, targetOrigin, }); return fail("official_mcp_origin_untrusted"); } const resolved = await authHeadersPort.resolveHeaders({ mcpKey: official.mcpKey, pluginId: official.pluginId, targetOrigin, ...(this.officialMcpAuth?.workspaceIdentity ? { workspaceIdentity: this.officialMcpAuth.workspaceIdentity } : {}), ...(this.workingDirectory ? { workspacePath: this.workingDirectory } : {}), ...(signal ? { signal } : {}), }); if (!resolved.ok) return fail(resolved.reason); // 只记 header 名与套餐维度,绝不记 header 值——日志留存周期不受控。 this.logger?.debug("Official MCP stdio auth headers attached", { ...logBase, identityHeaderNames: Object.keys(resolved.headers) .map((name) => name.toLowerCase()) .sort(), ...(resolved.headers["Bigmodel-Target-Type"] ? { identityTargetType: resolved.headers["Bigmodel-Target-Type"] } : {}), status: "completed", }); return { ok: true, headers: resolved.headers }; } /** server 主动 elicitation/create:归属到该 server 当前工具调用的会话,交给宿主提问端口。 */ private installElicitationHandler(client: McpClient, serverName: string): void { const port = this.elicitation; const setRequestHandler = (client as unknown as { setRequestHandler?: unknown }) .setRequestHandler; if (!port || typeof setRequestHandler !== "function") return; ( client as unknown as { setRequestHandler( method: "elicitation/create", handler: (request: { params: { message: string; requestedSchema?: unknown }; }) => Promise, ): void; } ).setRequestHandler("elicitation/create", async (request) => { // 接受缺省 / "form" / 兼容接口的 "openai/form" / "openaiForm";url 等其它模式 decline。 const mode = (request.params as { mode?: unknown }).mode; if (!isSupportedElicitationMode(mode)) { this.logger?.warn("MCP elicitation mode not supported, declining", { event: "mcp.elicitation.unsupported_mode", mcpServerName: serverName, mode: String(mode), }); return { action: "decline" }; } const trace = this.activeCallTraces.get(serverName); const result = await port.requestElicitation({ serverName, message: request.params.message, requestedSchema: request.params.requestedSchema, ...(trace ? { trace } : {}), }); return result.action === "accept" ? { action: "accept", content: result.content ?? {} } : { action: result.action }; }); } /** 调用期间登记 trace,server 在这段时间发起的 elicitation 才能归属到会话 / 工具行。 */ private async callToolOnClient( client: McpClient, request: McpCallToolRequest, timeoutMs: number, signal: AbortSignal | undefined, onProgress?: McpCallToolOptions["onProgress"], ): Promise { this.activeCallTraces.set(request.serverName, { ...(request.trace?.sessionId ? { sessionId: String(request.trace.sessionId) } : {}), ...(request.trace?.turnId ? { turnId: String(request.trace.turnId) } : {}), ...(request.trace?.traceId ? { traceId: String(request.trace.traceId) } : {}), }); try { return await this.callToolOnClientInner(client, request, timeoutMs, signal, onProgress); } finally { this.activeCallTraces.delete(request.serverName); } } private async callToolOnClientInner( client: McpClient, request: McpCallToolRequest, timeoutMs: number, signal: AbortSignal | undefined, onProgress?: McpCallToolOptions["onProgress"], ): Promise { // 工具调用此前完全无日志:超时时既看不到预算是多少,也无法区分"服务端慢"与 // "客户端预算太小"。这里记录预算与耗时,但只记参数的 key(值可能是用户输入)。 const logBase = { event: "mcp.tool.call", mcpServerName: request.serverName, mcpToolName: request.toolName, module: "adapters.mcp", timeoutMs, }; const argumentKeys = Object.keys(request.arguments ?? {}).sort(); this.logger?.debug("MCP tool call started", { ...logBase, argumentKeys, status: "started", }); const startedAt = Date.now(); try { const result = await client.callTool( { name: request.toolName, arguments: request.arguments ?? {}, ...(request.trace || request.runtimeScope || request.workspaceKey || request.workspacePath ? { _meta: mcpRequestMeta(request) } : {}), }, { signal, timeout: timeoutMs, // 进度通知重置超时(A8):长任务只要持续上报就不会被 timeoutMs 打断;总时长由工具级 _meta.timeoutMs 约束。 resetTimeoutOnProgress: true, ...(onProgress ? { onprogress: (notification: { progress: number; total?: number; message?: string; }) => onProgress({ progress: notification.progress, ...(notification.total !== undefined ? { total: notification.total } : {}), ...(notification.message !== undefined ? { message: notification.message } : {}), }), } : {}), }, ); const durationMs = Date.now() - startedAt; const isError = typeof result.isError === "boolean" ? result.isError : false; // 官方 MCP 的 in-band 失败(配额耗尽、无套餐)是 HTTP 200 + isError,wrapper 那条 // 非 2xx warn 覆盖不到;request id 也只有 wrapper 能看到,所以在这里按 span 取回。 const serverRequestId = this.takeServerRequestId(request.trace?.spanId); const outcome = { ...logBase, contentBlocks: Array.isArray(result.content) ? result.content.length : 0, durationMs, hasStructuredContent: result.structuredContent !== undefined, // 业务级失败(isError)与传输级失败不同,必须能分开统计。 isError, ...(serverRequestId ? { serverRequestId } : {}), }; if (isError) { // 之前 in-band 失败只有这条 debug,而生产 logger 最低级别是 Info——等于配额耗尽 // 这类失败在生产日志里完全不可见。 this.logger?.warn("MCP tool returned an error", { ...outcome, status: "failed" }); } else { this.logger?.debug("MCP tool call completed", { ...outcome, status: "completed" }); } const meta = isRecord(result._meta) ? result._meta : undefined; return { content: Array.isArray(result.content) ? (result.content as McpContentBlock[]) : [{ type: "text", text: "" }], structuredContent: result.structuredContent, isError: typeof result.isError === "boolean" ? result.isError : undefined, // 只在失败时附加:成功路径上它是纯噪声。服务端已给的键一律不覆盖。 _meta: isError && serverRequestId ? { ...meta, [ZCODE_MCP_SERVER_REQUEST_ID_META_KEY]: serverRequestId } : meta, }; } catch (error) { const durationMs = Date.now() - startedAt; const message = error instanceof Error ? error.message : String(error); // 判定是否为超时:SDK 超时会抛 MCP error code -32001 (RequestTimeout), // 底层 fetch abort 抛 AbortError。两者都要能一眼认出,否则只能看到裸 message。 const timedOut = /timed?\s*out|timeout/i.test(message) || (error instanceof Error && error.name === "AbortError"); // 传输级失败也带上:4xx/5xx 时 SDK 抛出的 message 里没有 request id。 const serverRequestId = this.takeServerRequestId(request.trace?.spanId); this.logger?.warn("MCP tool call failed", { ...logBase, argumentKeys, durationMs, error: message, ...(serverRequestId ? { serverRequestId } : {}), errorName: error instanceof Error ? error.name : "unknown", status: "failed", timedOut, // 耗时贴着预算 ⇒ 是我们掐断的;远小于预算 ⇒ 是对端或网络断的。 ...(timedOut ? { budgetExhausted: durationMs >= timeoutMs * 0.9 } : {}), }); throw error; } } private async reconnectForCall(name: string, config: McpServerConfig): Promise { this.logger?.warn("MCP server reconnecting after lost connection", { event: "mcp.server.reconnect.started", mcpServerName: name, status: "started", transport: config.type, }); await this.connectServer(name, config); } /** * 运行期认证恢复:Phase 2 交互授权 → Phase 1 重连 → 原 tool call 最多安全重试一次。 * * 与建连期共用 `runInteractiveOAuthAuthorization`,因此单飞、fencing、caller 预算语义完全一致。 */ private async recoverToolCallAuthorization(input: { deadline: McpDeadline; error: unknown; record: McpServerRecord; request: McpCallToolRequest; signal?: AbortSignal; onProgress?: McpCallToolOptions["onProgress"]; timeoutMessage: string; trigger: InteractiveAuthorizationTrigger; }): Promise { const { record, request } = input; const config = record.config; if (config.type === "stdio") throw input.error; const oauthConfig = resolveAuthorizationCodeOAuthConfig(config); if (!oauthConfig) throw input.error; this.logger?.warn("MCP tool call requires OAuth authorization", { event: "mcp.oauth.tool_call.authorization_required", mcpServerName: request.serverName, oauthTriggerReason: input.trigger.reason, status: "started", toolName: request.toolName, }); const recovery = this.ensureToolCallAuthorizationRecovery({ config, name: request.serverName, oauthConfig, record, trigger: input.trigger, }); const recoveredStatus = await waitWithinMcpDeadline( recovery, input.deadline, input.timeoutMessage, input.signal, ); if (recoveredStatus.status !== "connected") { throw input.error; } const revived = this.records.get(request.serverName); if (!revived?.client || revived.status.status !== "connected") throw input.error; return await this.callToolOnClient( revived.client, request, remainingMcpDeadlineMs(input.deadline, input.timeoutMessage), input.signal, input.onProgress, ); } /** * 创建或复用运行期 OAuth 恢复。完整的 Phase 2 → Phase 1 由 adapter-owned record 持有; * tool caller 只能等待,不能用自己的 AbortSignal 终止共享事务。 */ private ensureToolCallAuthorizationRecovery(input: { config: Extract; name: string; oauthConfig: AuthorizationCodeOAuthConfig; record: McpServerRecord; trigger: InteractiveAuthorizationTrigger; }): Promise { const current = this.records.get(input.name); if ( current?.connecting && current.status.status === "connecting" && isDeepStrictEqual(current.config, input.config) ) { return current.connecting; } const generation = this.nextConnectionGeneration(input.name); const abortController = new AbortController(); const recoveryRecord: McpServerRecord = { abortController, config: input.config, status: this.createStatus(input.config, "connecting", { toolCount: input.record.tools.length, }), // 运行期工具已经向 core 广告;恢复期间保留 descriptor,避免设置页/借用端口误判工具消失。 tools: input.record.tools, }; this.records.set(input.name, recoveryRecord); const connecting = this.runToolCallAuthorizationRecovery({ abortController, config: input.config, generation, name: input.name, oauthConfig: input.oauthConfig, previousClient: input.record.client, previousTransport: input.record.transport, trigger: input.trigger, }); recoveryRecord.connecting = connecting; return connecting; } private async runToolCallAuthorizationRecovery(input: { abortController: AbortController; config: Extract; generation: number; name: string; oauthConfig: AuthorizationCodeOAuthConfig; previousClient?: McpClient; previousTransport?: McpTransport; trigger: InteractiveAuthorizationTrigger; }): Promise { const startedAt = Date.now(); try { // 原 transport 的握手与 token 已失效,必须由共享 owner 统一退休;不能调用 connectServer, // 否则 closeRecord 会 abort recoveryRecord 自己的 controller,形成自取消。 await this.closeClientAndTransport(input.name, input.previousClient, input.previousTransport); const outcome = await this.runInteractiveOAuthAuthorization({ config: input.config, generation: input.generation, name: input.name, oauthConfig: input.oauthConfig, serverUrl: input.config.url, signal: input.abortController.signal, trigger: input.trigger, }); if (outcome.status === "authorized" || outcome.status === "already-authorized") { return await this.openServerConnection({ config: input.config, generation: input.generation, name: input.name, oauthAuthorizationAttempted: true, signal: input.abortController.signal, timeoutMs: input.config.timeoutMs ?? DEFAULT_MCP_TIMEOUT_MS, }); } return await this.failConnection({ config: input.config, error: outcome.status === "pending" ? new Error( `MCP server ${input.name} OAuth authorization is still in progress; complete it in the browser and reconnect`, ) : outcome.error, failureKind: "oauth_authorization_failed", generation: input.generation, name: input.name, startedAt, }); } catch (error) { // 防御边界:共享 recovery promise 必须是 total operation。任何未来新增的编排异常也只能 // 收敛为 failed record,不能留下 rejected connecting promise 污染后续 snapshot。 return await this.failConnection({ config: input.config, error, failureKind: "oauth_authorization_failed", generation: input.generation, name: input.name, startedAt, }); } } async close(): Promise { const startedAt = Date.now(); const serverCount = this.records.size; for (const name of this.records.keys()) { this.nextConnectionGeneration(name); } await Promise.all(Array.from(this.records.keys()).map((name) => this.closeRecord(name))); this.records.clear(); this.connectionDiagnosticByServer.clear(); this.logger?.info("MCP adapter closed", { durationMs: Date.now() - startedAt, event: "mcp.adapter.closed", serverCount, status: "completed", }); } private waitForSharedConnection( name: string, record: McpServerRecord, options: McpConnectOptions, ): Promise { const connecting = record.connecting; if (!connecting) return Promise.resolve(record.status); if (options.oauthAuthorizationTimeoutMs === undefined && !options.signal) { return connecting; } let abortHandler: (() => void) | undefined; let timeoutId: ReturnType | undefined; const currentStatus = () => this.records.get(name)?.status ?? record.status; const waiters: Promise[] = [connecting]; const timeoutMs = options.oauthAuthorizationTimeoutMs; if (timeoutMs !== undefined) { waiters.push( new Promise((resolvePromise) => { timeoutId = setTimeout(() => resolvePromise(currentStatus()), timeoutMs); }), ); } if (options.signal) { waiters.push( new Promise((resolvePromise) => { if (options.signal?.aborted) { resolvePromise(currentStatus()); return; } abortHandler = () => resolvePromise(currentStatus()); options.signal?.addEventListener("abort", abortHandler, { once: true, }); }), ); } return Promise.race(waiters).finally(() => { if (timeoutId !== undefined) clearTimeout(timeoutId); if (abortHandler) options.signal?.removeEventListener("abort", abortHandler); }); } private async openServerConnection(input: { config: McpServerConfig; generation: number; name: string; oauthAuthorizationTimeoutMs?: number; signal: AbortSignal; timeoutMs: number; workingDirectory?: string; oauthAuthorizationAttempted?: boolean; }): Promise { const { config, generation, name, oauthAuthorizationAttempted = false, oauthAuthorizationTimeoutMs, signal, timeoutMs, workingDirectory, } = input; const startedAt = Date.now(); let client: McpClient | undefined; let connectDurationMs: number | undefined; let getRecentStderr: (() => string | undefined) | undefined; let listToolsDurationMs: number | undefined; let transport: McpTransport | undefined; let failureKind: McpServerFailureKind = config.type === "stdio" ? "process_start_failed" : "network_unreachable"; try { const transportBundle = await this.createTransport( config, name, generation, oauthAuthorizationTimeoutMs, workingDirectory, signal, ); transport = transportBundle.transport; getRecentStderr = this.attachStdioLogging(name, transport); client = new Client( { name: this.clientName, version: this.clientVersion, }, { versionNegotiation: resolveVersionNegotiation(config, timeoutMs), capabilities: { // MCP Apps 规范:宿主经 `extensions["io.modelcontextprotocol/ui"]` 宣告能渲染的 UI mimeType, // 官方 SDK 的 server 据此决定是否下发 ui 资源(getUiCapability)。 ...buildMcpAppsClientCapabilities(), // 只宣告 form 模式(url 模式不做);server 据此选择 elicitation 形态。 ...(this.elicitation ? { elicitation: { form: {} } } : {}), }, }, ); this.installElicitationHandler(client, name); this.installNotificationHandlers(client, name); this.updateCurrentRecord(name, generation, { client, transport, }); const connectStartedAt = Date.now(); await withTimeout( client.connect(transport), timeoutMs, `MCP server ${name} connection timed out after ${timeoutMs}ms`, signal, ); connectDurationMs = Date.now() - connectStartedAt; failureKind = "tool_list_failed"; const listToolsStartedAt = Date.now(); const listed = await withTimeout( client.listTools(), timeoutMs, `MCP server ${name} tool listing timed out after ${timeoutMs}ms`, signal, ); listToolsDurationMs = Date.now() - listToolsStartedAt; const tools = listed.tools.map((tool) => normalizeMcpToolDescriptor( name, tool, config.timeoutMs, // 只有 http 形态置位。这个标记的用途是**信任结果里的结构化标识** // (额度耗尽 / 无套餐),因此判据必须是"结果由谁产出": // - http:结果来自 ZCode 后端。fetch wrapper 对每次请求校验 origin;登录态只在 // tools/call 解析,缺失时由同一可信后端返回结构化 coding_plan_required; // - stdio:结果由插件进程自己产出,可以任意伪造 `{"error_code":"quota_exceeded"}`, // 从而在用户输入框上方弹出"额度用完 / 请开通 Coding Plan"的误导提示。 // 原判据是 `type !== "sse"`,把 stdio 一起放了进来,等于这道门槛在 stdio 上为零。 // 注意这不是在挡凭证外泄(那由 origin 校验负责),而是在挡**结果伪造**。 config.type === "http" && config.auth?.type === ZCODE_OFFICIAL_MCP_AUTH_TYPE, ), ); const negotiatedProtocolEra = client.getProtocolEra(); const negotiatedProtocolVersion = client.getNegotiatedProtocolVersion(); const status = this.createStatus(config, "connected", { protocolEra: negotiatedProtocolEra, toolCount: tools.length, }); if (!this.isCurrentConnection(name, generation)) { await this.closeClientAndTransport(name, client, transport); return this.records.get(name)?.status ?? status; } this.connectionDiagnosticByServer.delete(name); this.records.set(name, { client, config, status, tools, transport, }); // A3:重连(同一 adapter 实例)后按登记表重放订阅;首次连接登记表为空,直接跳过。 await this.replayResourceSubscriptions(name, client); const mcpTransportPid = getStdioTransportPid(transport); const mcpProcessIdentity = mcpTransportPid != null && this.connectionContext ? this.telemetry?.recordProcessStarted({ connectionId: this.connectionContext.mcpConnectionId, pid: mcpTransportPid, }) : undefined; // stdio MCP 子进程死亡(如 node_repl 被 REPL cell 的异步错误击穿)不能完全 // 静默——不记日志、状态停留在 connected,后续调用只会抛裸的 "Not connected"。 // 挂 onclose 把意外断连显式化;主动关闭路径会先清掉 onclose(见 closeClientAndTransport)。 client.onclose = () => { if (!this.isCurrentConnection(name, generation)) return; const current = this.records.get(name); if (!current || current.client !== client) return; this.nextConnectionGeneration(name); const recentStderr = getRecentStderr?.(); const processExit = getStdioTransportExitInfo(transport); current.status = this.createStatus(current.config, "disconnected", { error: "MCP server connection closed unexpectedly", failureKind: "unexpected_disconnect", }); this.logger?.warn("MCP server connection lost", { ...this.connectionContext, event: "mcp.server.connection_lost", mcpServerName: name, ...(mcpTransportPid != null ? { mcpTransportPid } : {}), ...(mcpProcessIdentity ?? {}), ...(processExit ? { exitCode: processExit.exitCode, signal: processExit.signal ?? undefined } : {}), status: "failed", transport: current.config.type, ...(recentStderr ? { stderr: recentStderr } : {}), }); if ( current.config.type === "stdio" && this.connectionContext && (processExit || !isStdioTransportProcessAlive(transport)) ) { this.telemetry?.recordProcessCrashed({ connectionId: this.connectionContext.mcpConnectionId, exitCode: processExit?.exitCode ?? null, signal: processExit?.signal ?? null, }); } }; // 此前连接日志只记录 transport,auto 协商后无法判断实际走 modern 还是 legacy。 // 同时记录配置策略和 SDK 握手结果,避免把 `auto` 误当成最终协议版本。 // 连接池上下文和 stdio transport PID 过去未进入同一事件,无法关联 session、 // workspace、协议版本和真实子进程;stdio PID 只代表最终会话 transport,不代表 probe child。 this.logger?.info("MCP server connected", { ...this.connectionContext, connectDurationMs, durationMs: Date.now() - startedAt, event: "mcp.server.connected", listToolsDurationMs, mcpClientName: this.clientName, mcpClientVersion: this.clientVersion, mcpProtocolEra: negotiatedProtocolEra ?? "unknown", mcpProtocolVersion: negotiatedProtocolVersion ?? "unknown", mcpServerName: name, ...(mcpProcessIdentity ?? {}), ...(mcpTransportPid != null ? { mcpTransportPid } : {}), mcpVersionNegotiationMode: formatVersionNegotiationMode(config), status: "completed", toolCount: tools.length, transport: config.type, }); return status; } catch (error) { // 过去这里等的是旧 provider 自己开的 listener,且用 session 的 15 秒预算做 // 硬关闭——15 秒是 caller 的等待预算,不是授权事务的寿命。现在按错误类型判定是否需要 // 交互授权,并把授权交给 Phase 2 独立事务(独立锁、fresh DCR、300 秒事务 TTL)。 const trigger = oauthAuthorizationAttempted ? undefined : classifyInteractiveAuthorizationTrigger(error); const authorizationCodeOAuthConfig = trigger && config.type !== "stdio" ? resolveAuthorizationCodeOAuthConfig(config) : undefined; if (trigger && authorizationCodeOAuthConfig && config.type !== "stdio") { // negotiation 失败时 SDK 已关闭 transport,不可复用;Phase 2 也不需要 transport。 await this.closeClientAndTransport(name, client, transport); const outcome = await this.runInteractiveOAuthAuthorization({ config, generation, name, oauthConfig: authorizationCodeOAuthConfig, serverUrl: config.url, signal, trigger, }); if (outcome.status === "authorized" || outcome.status === "already-authorized") { return await this.openServerConnection({ ...input, oauthAuthorizationAttempted: true, }); } // client/transport 已在进入 Phase 2 前关闭,这里不再传入;failureKind 沿用 // 诊断分类,让设置页把"授权没完成"与网络/进程类失败区分开。 return this.failConnection({ config, connectDurationMs, error: outcome.status === "pending" ? new Error( `MCP server ${name} OAuth authorization is still in progress; complete it in the browser and reconnect`, ) : outcome.error, failureKind: "oauth_authorization_failed", generation, getRecentStderr, listToolsDurationMs, name, startedAt, }); } // `protocol_negotiation_failed` 枚举与 UI 文案在 shared/i18n 里早已存在,但 adapter // 侧一直没有产出方——auto/pin 模式下 SDK 的 server/discover probe 硬失败(典型:飞书项目 // MCP 对未知方法回 HTTP 200 + id:null 的非标 JSON-RPC error,body 过不了 // JSONRPCMessageSchema)会一路落到默认 failureKind "network_unreachable",设置页因此 // 显示误导性的"网络不可达"。这里按结构化错误类型(SdkErrorCode / isInstance)识别 SDK // 协商失败并产出正确分类,不依赖错误文本;withTimeout 不包装错误(timeout.ts 只透传 // reject),cause 链仅作防御性兜底。 const negotiationFailureKind = isProtocolNegotiationFailure(error) ? ("protocol_negotiation_failed" as const) : undefined; return this.failConnection({ client, config, connectDurationMs, error, generation, getRecentStderr, listToolsDurationMs, name, startedAt, transport, failureKind: negotiationFailureKind ?? failureKind, }); } } /** * Phase 2:交互授权。 * * 授权事务的寿命是 300 秒,与 caller 的等待预算(session 15 秒)无关;caller 侧的收口发生在 * `connectServer` / `waitForSharedConnection`,本方法不感知 caller 预算。 */ private async runInteractiveOAuthAuthorization(input: { config: Extract; generation: number; name: string; oauthConfig: AuthorizationCodeOAuthConfig; serverUrl: string; signal: AbortSignal; trigger: InteractiveAuthorizationTrigger; }): Promise { try { const oauthOptions = this.createAuthorizationCodeOAuthOptions( input.config, input.name, input.generation, ); const credentialStore = oauthOptions?.credentialStore ?? createSharedZCodeCredentialStore(); const keyPrefix = createCredentialKeyPrefix(input.name, input.serverUrl, input.oauthConfig); // 403 step-up 的最终 scope 必须是 config ∪ token.scope ∪ challenge // 的并集。只带 challenge scope 重新授权时,授权服务器可能按新请求收回先前授予的 scope, // 下一个请求换个 challenge 又 403,形成重授权乒乓。token response 的 scope 允许缺失 // (RFC 6749 §3.3),所以配置里声明过的 scope 必须显式并入,不能只看 token 回显。 let requestedScope: string | undefined = input.oauthConfig.scope; if (input.trigger.requiredScope) { const currentPair = await loadCredentialPair(credentialStore, keyPrefix); requestedScope = computeScopeUnion( input.oauthConfig.scope, currentPair?.tokens?.scope, input.trigger.requiredScope, ); } return await runMcpInteractiveAuthorization({ adapterInstanceId: this.adapterInstanceId, config: input.oauthConfig, credentialStore, fetchFn: createMcpTransportFetch({ env: this.env, network: this.network }), // 403 step-up:requiredScope 是当前 token scope 的严格超集时 refresh 无法扩权 // (RFC 6749 §6),必须强制重新授权,否则新 scope 会被静默丢弃并再次 403。 ...(input.trigger.reason === "insufficient_scope" ? { forceReauthorization: true } : {}), keyPrefix, logger: this.logger, ...(oauthOptions?.onAuthorizationRequired ? { onAuthorizationRequired: oauthOptions.onAuthorizationRequired } : {}), ...(oauthOptions?.openAuthorizationUrl ? { openAuthorizationUrl: oauthOptions.openAuthorizationUrl } : {}), ...(requestedScope ? { requestedScope } : {}), ...(input.trigger.resourceMetadataUrl ? { resourceMetadataUrl: new URL(input.trigger.resourceMetadataUrl) } : {}), serverName: input.name, serverUrl: input.serverUrl, signal: input.signal, transactionTtlMs: MCP_OAUTH_AUTHORIZATION_TRANSACTION_TTL_MS, }); } catch (error) { // 本方法的返回类型已经把编排失败建模为 outcome。过去 credential load、 // authz lease 或 follower callback 的异常会裸 reject,绕过 failConnection,留下 // status=connecting + rejected record.connecting,并让 connectConfiguredServers 整批失败。 this.logger?.warn("MCP OAuth authorization orchestration failed", { error: error instanceof Error ? error.message : String(error), errorName: error instanceof Error ? error.name : "unknown", event: "mcp.oauth.authorization.orchestration_failed", mcpServerName: input.name, status: "failed", }); return { status: "failed", error }; } } private async failConnection(input: { client?: McpClient; config: McpServerConfig; connectDurationMs?: number; error: unknown; failureKind?: McpServerFailureKind; generation: number; getRecentStderr?: () => string | undefined; listToolsDurationMs?: number; name: string; startedAt: number; transport?: McpTransport; }): Promise { const { client, config, connectDurationMs, error, failureKind: fallbackFailureKind, generation, getRecentStderr, listToolsDurationMs, name, startedAt, transport, } = input; const message = error instanceof Error ? error.message : String(error); // 官方 MCP 鉴权失败的稳定分类必须落进日志:failConnection 原先只记 // error.message,而多数分类并不出现在 message 文本里(只有 auth-port 那条带上了), // 导致 official_mcp_origin_untrusted / official_auth_rejected 等在生产日志里 grep 不到。 const officialAuthKind = (error instanceof OfficialMcpAuthError ? error.kind : undefined) ?? this.lastOfficialAuthKind.get(name); this.lastOfficialAuthKind.delete(name); const responseDiagnostic = this.connectionDiagnosticByServer.get(name); this.connectionDiagnosticByServer.delete(name); const failureKind = (officialAuthKind === "official_mcp_origin_untrusted" ? "official_origin_untrusted" : undefined) ?? responseDiagnostic?.failureKind ?? (error instanceof McpTimeoutError && fallbackFailureKind !== "tool_list_failed" ? "connection_timeout" : undefined) ?? fallbackFailureKind ?? "connection_failed"; const displayMessage = responseDiagnostic?.serverRequestId ? `${message} - ${responseDiagnostic.serverRequestId}` : message; const status = this.createStatus(config, "failed", { error: displayMessage, failureKind, ...(responseDiagnostic?.serverRequestId ? { serverRequestId: responseDiagnostic.serverRequestId } : {}), }); const recentStderr = getRecentStderr?.(); const mcpTransportPid = getStdioTransportPid(transport); await this.closeClientAndTransport(name, client, transport); if (!this.isCurrentConnection(name, generation)) { return this.records.get(name)?.status ?? status; } this.records.set(name, { config, status, tools: [] }); this.logger?.warn("MCP server connection failed", { ...this.connectionContext, connectDurationMs, durationMs: Date.now() - startedAt, error: displayMessage, event: "mcp.server.failed", listToolsDurationMs, mcpServerName: name, ...(officialAuthKind ? { officialAuthKind } : {}), ...(mcpTransportPid != null ? { mcpTransportPid } : {}), status: "failed", ...(recentStderr ? { stderr: recentStderr } : {}), transport: config.type, }); return status; } private async createTransport( config: McpServerConfig, serverName: string, generation: number, _oauthAuthorizationTimeoutMs?: number, workingDirectory?: string, signal?: AbortSignal, ): Promise<{ transport: McpTransport }> { if (config.type === "stdio") { return { transport: new ProcessTreeStdioClientTransport({ command: config.command, args: config.args ?? [], cwd: config.cwd ? resolve(workingDirectory ?? this.workingDirectory ?? process.cwd(), config.cwd) : (workingDirectory ?? this.workingDirectory), env: { ...buildMcpStdioEnv({ env: this.env, network: this.network }), ...config.env, }, stderr: "pipe", ...(isOfficialAuthConfig(config) && config.official ? { requestMetaProvider: async () => { const authMeta = await this.resolveOfficialStdioAuthMeta( serverName, config, signal, ); return authMeta ? { [OFFICIAL_MCP_AUTH_META_KEY]: authMeta } : undefined; }, } : {}), }), }; } const fetch = createMcpTransportFetch({ env: this.env, network: this.network, }); if (config.type === "http") { const officialAuthFetch = this.createOfficialAuthFetch(config, serverName, generation); return { transport: new StreamableHTTPClientTransport(new URL(config.url), { // 官方鉴权路径下 authProvider 必为 undefined:不落 OAuth 凭据、 // 不起 localhost 回调 server、401/403 不转授权流程。 authProvider: this.createOAuthClientProvider(serverName, config), fetch: officialAuthFetch ?? fetch, requestInit: config.headers ? { headers: config.headers } : undefined, }), }; } return { transport: new SSEClientTransport(new URL(config.url), { authProvider: this.createOAuthClientProvider(serverName, config), fetch, requestInit: config.headers ? { headers: config.headers } : undefined, }), }; } /** * 官方鉴权 MCP 的动态 fetch。返回 undefined 表示走普通 MCP 路径。 * * trusted origin 依赖缺失时直接 fail closed。auth port 可以缺失:wrapper 仍校验 origin, * 各请求匿名降级并由服务端做权威判定。 */ private createOfficialAuthFetch( config: McpServerConfig, serverName: string, generation: number, ): typeof globalThis.fetch | undefined { if (!isOfficialAuthConfig(config) || config.type !== "http" || !config.official) { return undefined; } const official = config.official; const authHeadersPort = this.officialMcpAuth?.authHeadersPort; const trustedOrigins = this.officialMcpAuth?.trustedOrigins; if (!trustedOrigins) { return (() => { throw new OfficialMcpAuthError( "official_auth_unavailable", `official MCP trusted origin registry is not available in this runtime: ${serverName}`, ); }) as unknown as typeof globalThis.fetch; } return createOfficialMcpAuthFetch({ baseFetch: createMcpTransportFetch({ env: this.env, network: this.network }), official, onAuthFailure: (kind) => this.lastOfficialAuthKind.set(serverName, kind), onServerResponse: (response) => { if (this.isCurrentConnection(serverName, generation)) { this.rememberServerResponse(serverName, response); } }, serverName, trustedOrigins, url: config.url, ...(authHeadersPort ? { authHeadersPort } : {}), ...(this.logger ? { logger: this.logger } : {}), ...(this.officialMcpAuth?.workspaceIdentity ? { workspaceIdentity: this.officialMcpAuth.workspaceIdentity } : {}), ...(this.workingDirectory ? { workspacePath: this.workingDirectory } : {}), }); } /** * 运行期 auth provider。 * * 过去这里对任何没有 Authorization header 的 HTTP/SSE MCP * 都创建一个完整 OAuth session——而 session 在返回前就 `listen(0)` 起了一个 callback server, * 即使凭据完全有效、根本不需要授权。同时完整 `OAuthClientProvider` 会让 401 走 SDK 的 * `auth()`,绕过我们的 refresh 单飞锁。 * * 现在 authorization_code 一律使用纯 AuthProvider:被动连接零 listener、零 discovery、零 DCR, * 交互授权只在 Phase 2 事务里发生。 */ private createOAuthClientProvider( serverName: string, config: McpServerConfig, ): AuthProvider | OAuthClientProvider | undefined { if (config.type === "stdio") return undefined; // 官方鉴权与 OAuth 互斥:官方 MCP 的失败只能由 ZCode 登录/套餐解决, // 交出任何 authProvider 都会让 401 误转成 MCP 授权流程。 if (isOfficialAuthConfig(config)) return undefined; const authorizationCodeOAuthConfig = resolveAuthorizationCodeOAuthConfig(config); if (authorizationCodeOAuthConfig) { return createMcpOAuthTokenProvider({ config: authorizationCodeOAuthConfig, credentialStore: this.resolveCredentialStore(), fetchFn: createMcpTransportFetch({ env: this.env, network: this.network }), keyPrefix: createCredentialKeyPrefix(serverName, config.url, authorizationCodeOAuthConfig), ...(this.logger ? { logger: this.logger } : {}), serverName, serverUrl: config.url, }); } if (config.oauth?.type === "client_credentials") { return new ClientCredentialsProvider({ clientId: config.oauth.clientId, clientName: config.oauth.clientName ?? `${this.clientName}-${serverName}`, clientSecret: config.oauth.clientSecret, scope: config.oauth.scope, }); } return undefined; } private resolveCredentialStore(): SharedZCodeCredentialStore { this.credentialStore ??= this.mcpOAuth?.credentialStore ?? createSharedZCodeCredentialStore(); return this.credentialStore; } private createAuthorizationCodeOAuthOptions( config: McpServerConfig, serverName: string, generation: number, oauthAuthorizationTimeoutMs?: number, ): McpOAuthRuntimeOptions | undefined { if (config.type === "stdio") return this.mcpOAuth; return { ...this.mcpOAuth, authorizationTimeoutMs: oauthAuthorizationTimeoutMs ?? this.mcpOAuth?.authorizationTimeoutMs, onAuthorizationRequired: async (context) => { this.updateCurrentRecordStatus(serverName, generation, { authorization: createOAuthAuthorizationStatus(context), status: "connecting", }); await this.mcpOAuth?.onAuthorizationRequired?.(context); }, }; } private attachStdioLogging(name: string, transport: McpTransport): () => string | undefined { const stderrBuffer = createBoundedTextBuffer(MCP_STDIO_STDERR_LOG_MAX_CHARS); const stderr = ( transport as { stderr?: { on(event: "data", handler: (chunk: Buffer) => void): void }; } ).stderr; stderr?.on("data", (chunk) => { const text = chunk.toString("utf8"); stderrBuffer.append(text); this.logger?.debug("MCP stdio stderr", { event: "mcp.stdio.stderr", mcpServerName: name, stderr: sanitizeMcpStdioStderr(text).slice(0, MCP_STDIO_STDERR_LOG_MAX_CHARS), }); }); return () => { const text = stderrBuffer.read(); if (!text) return undefined; // 生产日志里单独的 Connection closed 无法定位 stdio MCP 子进程退出原因。 // 只在失败事件附带尾部 stderr,并先脱敏,避免把凭据或高频输出写入生产日志。 return sanitizeMcpStdioStderr(text).slice(-MCP_STDIO_STDERR_LOG_MAX_CHARS); }; } private async closeRecord(name: string): Promise { const record = this.records.get(name); if (!record) return; record.abortController?.abort(new Error(`MCP server ${name} connection closed`)); if (!record.client && !record.transport) return; await this.closeClientAndTransport(name, record.client, record.transport); } private async closeClientAndTransport( name: string, client?: McpClient, transport?: McpTransport, ): Promise { const startedAt = Date.now(); const mcpTransportPid = getStdioTransportPid(transport); // 主动关闭前先摘掉 connection_lost 监听,避免正常回收被误报为意外断连。 if (client) client.onclose = undefined; // MCP SDK close 只保证直接 stdio 子进程退出,npx/npm wrapper 拉起的 MCP server // 或 chrome-devtools-mcp watchdog 可能残留;这里先按进程树显式回收,再走 SDK close 清理协议状态。 await this.terminateStdioProcessTree(name, transport); try { await client?.close(); } catch (error) { this.logger?.debug("MCP client close failed", { error: error instanceof Error ? error.message : String(error), event: "mcp.client.close.failed", mcpServerName: name, }); } try { await transport?.close(); } catch (error) { this.logger?.debug("MCP transport close failed", { error: error instanceof Error ? error.message : String(error), event: "mcp.transport.close.failed", mcpServerName: name, }); } if ( this.connectionContext && transport instanceof ProcessTreeStdioClientTransport && !transport.processAlive ) { this.telemetry?.recordProcessClosed({ connectionId: this.connectionContext.mcpConnectionId, }); } this.logger?.info("MCP server closed", { ...this.connectionContext, durationMs: Date.now() - startedAt, event: "mcp.server.closed", mcpServerName: name, ...(mcpTransportPid != null ? { mcpTransportPid } : {}), status: "completed", }); } private async terminateStdioProcessTree(name: string, transport?: McpTransport): Promise { const pid = getStdioTransportPid(transport); if (pid == null) return; try { await terminateMcpStdioProcessTree(pid); } catch (error) { this.logger?.warn("MCP stdio process tree cleanup failed", { ...this.connectionContext, error: error instanceof Error ? error.message : String(error), event: "mcp.stdio.process_tree_cleanup.failed", mcpServerName: name, mcpTransportPid: pid, pid, status: "failed", }); } } private readonly appConnectionListeners = new Map void>>(); onAppConnectionInvalidated(name: string, listener: () => void): () => void { const listeners = this.appConnectionListeners.get(name) ?? new Set(); listeners.add(listener); this.appConnectionListeners.set(name, listeners); return () => { listeners.delete(listener); if (!listeners.size) this.appConnectionListeners.delete(name); }; } appConnectionSnapshot(name: string): { identity: string; generation: number } | null { const record = this.records.get(name); if (!record?.client || record.status.status !== "connected") return null; // 只向宿主输出摘要;配置中的凭据不进入协议或日志。属性排序避免配置序列化次序造成换源。 const canonical = (value: unknown): unknown => { if (Array.isArray(value)) return value.map(canonical); if (value && typeof value === "object") return Object.fromEntries( Object.entries(value) .sort(([a], [b]) => a.localeCompare(b)) .map(([k, v]) => [k, canonical(v)]), ); return value; }; return { identity: createHash("sha256") .update( JSON.stringify([ process.platform, process.execPath, this.workingDirectory ?? "", name, canonical(record.config), ]), ) .digest("hex"), generation: this.connectionGenerations.get(name) ?? 0, }; } private nextConnectionGeneration(name: string): number { const generation = (this.connectionGenerations.get(name) ?? 0) + 1; this.connectionGenerations.set(name, generation); // eslint-disable-next-line unicorn/no-useless-spread -- 回调可能增删监听器,本次通知必须固定接收者。 for (const listener of [...(this.appConnectionListeners.get(name) ?? [])]) listener(); return generation; } private isCurrentConnection(name: string, generation: number): boolean { return this.connectionGenerations.get(name) === generation; } private updateCurrentRecord( name: string, generation: number, patch: Partial>, ): void { if (!this.isCurrentConnection(name, generation)) return; const record = this.records.get(name); if (!record) return; Object.assign(record, patch); } private updateCurrentRecordStatus( name: string, generation: number, patch: Partial, ): void { if (!this.isCurrentConnection(name, generation)) return; const record = this.records.get(name); if (!record) return; record.status = { ...record.status, ...patch, updatedAt: new Date().toISOString(), }; } private createStatus( config: McpServerConfig, status: McpServerStatus["status"], extra: { authorization?: McpServerStatus["authorization"]; error?: string; failureKind?: McpServerStatus["failureKind"]; protocolEra?: McpServerStatus["protocolEra"]; serverRequestId?: string; toolCount?: number; } = {}, ): McpServerStatus { return { status, transport: config.type, toolCount: extra.toolCount ?? 0, updatedAt: new Date().toISOString(), authorization: extra.authorization, error: extra.error, failureKind: extra.failureKind, protocolEra: extra.protocolEra, serverRequestId: extra.serverRequestId, }; } } function mcpRequestMeta(request: McpCallToolRequest): Record { // nodeRepl.requestMeta 暴露。ZCode 所有 MCP server 都可忽略这些扩展键;node_repl browser // bridge 则以它们作为回到当前 BrowserControlPort session 的唯一关联依据。runtime_scope // 不能从 child session id 猜测,必须由实际执行工具的 runtime 显式透传。 const requestContext = { ...(request.trace ? { trace_id: request.trace.traceId } : {}), ...(request.trace?.spanId ? { span_id: request.trace.spanId } : {}), ...(request.trace?.parentSpanId ? { parent_span_id: request.trace.parentSpanId } : {}), ...(request.trace?.sessionId ? { session_id: request.trace.sessionId } : {}), ...(request.trace?.turnId ? { turn_id: request.trace.turnId } : {}), ...(request.runtimeScope ? { runtime_scope: request.runtimeScope } : {}), ...(request.workspacePath ? { workspace_path: request.workspacePath } : {}), ...(request.workspaceIdentity ? { workspace_identity: request.workspaceIdentity } : {}), ...(request.workspaceKey ? { workspace_key: request.workspaceKey } : {}), ...(request.remoteSessionId ? { remote_session_id: request.remoteSessionId } : {}), ...(request.clientMode ? { client_mode: request.clientMode } : {}), ...(request.deliveryKind ? { delivery_kind: request.deliveryKind } : {}), ...(request.turnId && !request.trace?.turnId ? { turn_id: request.turnId } : {}), }; return { ...requestContext, "com.zcode/request-context": requestContext, }; } function resolveVersionNegotiationMode(config: McpServerConfig): VersionNegotiationMode { if (config.protocolVersion === "2026-07-28") return { pin: "2026-07-28" }; // deprecated SSE transport 本身只承载 legacy era;显式 modern pin 仍应失败而不能静默降级。 if (config.type === "sse") return "legacy"; if (config.protocolVersion === "legacy") return "legacy"; return "auto"; } /** * SDK 版本协商(auto/pin 的 server/discover probe)失败的稳定识别。 * * 结构化判定,禁止匹配错误文本: * - `SdkError(SdkErrorCode.EraNegotiationFailed)`:probe 硬失败(含非标 legacy server 的 * malformed 200 响应,经 transport 层 Zod 校验失败 + normalizeReply 落入 network-error 分支); * - `UnsupportedProtocolVersionError`:recognized modern error,pin 版本不被 server 接受。 */ function isProtocolNegotiationFailure(error: unknown): boolean { if (SdkError.isInstance(error) && error.code === SdkErrorCode.EraNegotiationFailed) { return true; } if (UnsupportedProtocolVersionError.isInstance(error)) return true; const cause = (error as { cause?: unknown } | undefined)?.cause; if (cause !== undefined && cause !== error) { return isProtocolNegotiationFailure(cause); } return false; } function formatVersionNegotiationMode(config: McpServerConfig): string { const mode = resolveVersionNegotiationMode(config); return typeof mode === "object" ? mode.pin : mode; } function resolveVersionNegotiation( config: McpServerConfig, timeoutMs: number, ): VersionNegotiationOptions { const mode = resolveVersionNegotiationMode(config); if (mode === "legacy") return { mode }; // pin 没有 legacy fallback,probe 就是唯一 initialize 路径;沿用 auto 的 5 秒 // 保护上限会无视 server 的长连接预算,把冷启动正常但超过 5 秒的 node_repl 静默移出工具池。 const probeTimeoutMs = typeof mode === "object" ? Math.max(1, Math.floor(timeoutMs)) : Math.min(MAX_MCP_VERSION_PROBE_TIMEOUT_MS, Math.max(1, Math.floor(timeoutMs / 2))); return { mode, probe: { // 原因:SDK 的 stdio auto/pin 会先启动 disposable sibling;若沿用 SDK 60s 默认值, // ZCode 的总连接超时可能先结束并让 probe 残留,也不给 legacy initialize 留预算。 timeoutMs: probeTimeoutMs, }, }; } function createOAuthAuthorizationStatus( context: McpOAuthAuthorizationContext, ): NonNullable { return { type: "oauth_authorization_code", authorizationUrl: context.authorizationUrl, startedAt: new Date().toISOString(), }; } function resolveAuthorizationCodeOAuthConfig( config: McpServerConfig, ): AuthorizationCodeOAuthConfig | undefined { if (config.type === "stdio") return undefined; // 官方鉴权与 MCP OAuth 互斥。必须位于所有既有分支之前: // 官方 MCP 既不写 oauth 字段、又禁止静态 authorization 头,若不在此短路就会落进 // 下面的 authorization_code 兜底,导致 401 时弹出 MCP 授权 UI —— 而官方鉴权失败 // 只能由 ZCode 自身的登录/套餐解决,不可能由目标 MCP 的 OAuth 授权解决。 if (isOfficialAuthConfig(config)) return undefined; if (config.oauth?.type === "authorization_code") return config.oauth; if (config.oauth?.type === "client_credentials") return undefined; if (hasAuthorizationHeader(config.headers)) return undefined; // 新建 HTTP/SSE MCP 常只保存 URL;OAuth 支持应由服务端 // WWW-Authenticate / discovery 触发,不能要求配置里预先写 oauth 字段。 return { type: "authorization_code", }; } /** * auth.type/provider 精确命中且 provenance 存在时为真;provenance 缺失说明不是 Plugin loader * 产出的配置。 * * 覆盖 http 与 stdio 两种形态——两者的凭证投递通道不同,但"是否官方鉴权" * 的判定同源。调用方若只关心某一形态,需自行再判 `config.type`(如 createOfficialAuthFetch * 只处理 http、_meta 注入只处理 stdio)。 */ function isOfficialAuthConfig(config: McpServerConfig): boolean { return ( (config.type === "http" || config.type === "stdio") && config.auth?.type === "zcode_official" && config.auth.provider === "jwt_token" && config.official !== undefined ); } function hasAuthorizationHeader(headers: Record | undefined): boolean { if (!headers) return false; return Object.keys(headers).some((name) => name.toLowerCase() === "authorization"); } function createBoundedTextBuffer(maxChars: number): { append(text: string): void; read(): string; } { let value = ""; return { append(text: string) { if (!text) return; value = `${value}${text}`; if (value.length > maxChars) { value = value.slice(-maxChars); } }, read() { return value; }, }; } function sanitizeMcpStdioStderr(text: string): string { const sensitiveKey = String.raw`(?:api[_-]?key|access[_-]?key|secret(?:[_-]?key)?|private[_-]?key|token|password|passwd|pass|mysql_pass|mysql_password)`; let result = text.replace(/(bearer\s+)[^\s"']+/gi, "$1[Redacted]"); result = result.replace( /(\bauthorization\b\s*[:=]\s*)(bearer\s+)?[^\r\n]+/gi, (_match, prefix: string, bearer: string | undefined) => `${prefix}${bearer ? "Bearer " : ""}[Redacted]`, ); result = result.replace(new RegExp(`([?&]${sensitiveKey}=)[^&\\s]+`, "gi"), "$1[Redacted]"); result = result.replace( new RegExp(`(["']${sensitiveKey}["']\\s*:\\s*)(["'])(?:(?!\\2).)*\\2`, "gi"), "$1$2[Redacted]$2", ); result = result.replace( new RegExp(`(\\b${sensitiveKey}\\b\\s*[:=]\\s*)(["']?)[^\\s"',;)}]+`, "gi"), "$1$2[Redacted]", ); return result.replace(/([a-z][a-z0-9+.-]*:\/\/)[^:\s/@]+:[^@\s/]+@/gi, "$1[Redacted]@"); } function getStdioTransportPid(transport?: McpTransport): number | undefined { if (!(transport instanceof StdioClientTransport)) return undefined; const pid = transport.pid; return typeof pid === "number" && Number.isInteger(pid) && pid > 0 ? pid : undefined; } function getStdioTransportExitInfo(transport?: McpTransport): | { exitCode: number | null; signal: NodeJS.Signals | null; } | undefined { return transport instanceof ProcessTreeStdioClientTransport ? transport.processExit : undefined; } function isStdioTransportProcessAlive(transport?: McpTransport): boolean { return transport instanceof ProcessTreeStdioClientTransport && transport.processAlive; } function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } // server 回了 JSON-RPC 错误响应(数字 code)说明请求走通了、连接是活的; // SDK 本地错误(SdkError,字符串 code:REQUEST_TIMEOUT / CONNECTION_CLOSED / NOT_CONNECTED // / SEND_FAILED)才代表 transport 已断。code 类型判断兜底 instanceof 在多份 SDK 实例下失效的情况。 function isPeerAnsweredError(error: unknown): boolean { if (error instanceof ProtocolError) return true; return isRecord(error) && typeof error.code === "number"; }