@herbertgao/pi-extensions 2026.9.17 → 2026.9.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -15
- package/node_modules/@herbertgao/pi-bark/package.json +2 -2
- package/node_modules/@herbertgao/pi-cc-extensions/README.en.md +4 -1
- package/node_modules/@herbertgao/pi-cc-extensions/README.md +4 -1
- package/node_modules/@herbertgao/pi-cc-extensions/package.json +75 -75
- package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +6 -0
- package/node_modules/@herbertgao/pi-subagents/package.json +7 -7
- package/node_modules/@herbertgao/resume-from/package.json +4 -2
- package/node_modules/@herbertgao/sol-pi/package.json +6 -6
- package/node_modules/@juicesharp/rpiv-ask-user-question/package.json +99 -99
- package/node_modules/@pi-plugins/fast-mode/package.json +1 -1
- package/node_modules/@tifan/pi-handoff/package.json +1 -1
- package/node_modules/@tifan/pi-handoff/src/index.ts +56 -26
- package/node_modules/@tifan/pi-mermaid-open/package.json +1 -1
- package/node_modules/@tifan/pi-preferred-thinking/package.json +1 -1
- package/node_modules/@tifan/pi-recap/package.json +1 -1
- package/node_modules/@tifan/pi-rename/package.json +1 -1
- package/node_modules/pi-antigravity/package.json +2 -2
- package/node_modules/pi-lens/package.json +158 -158
- package/node_modules/pi-multi-account/package.json +2 -2
- package/node_modules/pi-typesafe/README.md +1 -1
- package/node_modules/pi-typesafe/dist/auth.d.ts +9 -5
- package/node_modules/pi-typesafe/dist/auth.js +7 -3
- package/node_modules/pi-typesafe/dist/backends.d.ts +33 -3
- package/node_modules/pi-typesafe/dist/backends.js +95 -1
- package/node_modules/pi-typesafe/dist/client.d.ts +5 -5
- package/node_modules/pi-typesafe/dist/client.js +12 -9
- package/node_modules/pi-typesafe/dist/credentials.d.ts +14 -8
- package/node_modules/pi-typesafe/dist/credentials.js +10 -7
- package/node_modules/pi-typesafe/dist/errors.d.ts +2 -2
- package/node_modules/pi-typesafe/dist/index.d.ts +2 -2
- package/node_modules/pi-typesafe/dist/index.js +1 -1
- package/node_modules/pi-typesafe/dist/login.d.ts +2 -2
- package/node_modules/pi-typesafe/dist/login.js +2 -2
- package/node_modules/pi-typesafe/package.json +5 -5
- package/node_modules/pi-web-access/package.json +2 -2
- package/node_modules/remote-pi/package.json +5 -1
- package/package.json +16 -28
- package/node_modules/pi-mcp-adapter/CHANGELOG.md +0 -1013
- package/node_modules/pi-mcp-adapter/LICENSE +0 -21
- package/node_modules/pi-mcp-adapter/OAUTH.md +0 -369
- package/node_modules/pi-mcp-adapter/README.md +0 -1021
- package/node_modules/pi-mcp-adapter/abort.ts +0 -34
- package/node_modules/pi-mcp-adapter/agent-dir.ts +0 -68
- package/node_modules/pi-mcp-adapter/agent-plugin-loader.ts +0 -416
- package/node_modules/pi-mcp-adapter/agent-plugin-provenance.ts +0 -36
- package/node_modules/pi-mcp-adapter/app-bridge.bundle.js +0 -67
- package/node_modules/pi-mcp-adapter/banner.png +0 -0
- package/node_modules/pi-mcp-adapter/bearer-command-resolver.ts +0 -193
- package/node_modules/pi-mcp-adapter/claude-plugin-loader.ts +0 -365
- package/node_modules/pi-mcp-adapter/cli.js +0 -454
- package/node_modules/pi-mcp-adapter/commands.ts +0 -861
- package/node_modules/pi-mcp-adapter/config.ts +0 -1565
- package/node_modules/pi-mcp-adapter/consent-manager.ts +0 -95
- package/node_modules/pi-mcp-adapter/direct-tool-surface.ts +0 -244
- package/node_modules/pi-mcp-adapter/direct-tools.ts +0 -428
- package/node_modules/pi-mcp-adapter/dist/abort.d.ts +0 -2
- package/node_modules/pi-mcp-adapter/dist/abort.js +0 -36
- package/node_modules/pi-mcp-adapter/dist/abort.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/agent-dir.d.ts +0 -10
- package/node_modules/pi-mcp-adapter/dist/agent-dir.js +0 -63
- package/node_modules/pi-mcp-adapter/dist/agent-dir.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-loader.d.ts +0 -8
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-loader.js +0 -398
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-loader.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-provenance.d.ts +0 -1
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-provenance.js +0 -31
- package/node_modules/pi-mcp-adapter/dist/agent-plugin-provenance.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.d.ts +0 -6
- package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.js +0 -194
- package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/claude-plugin-loader.d.ts +0 -9
- package/node_modules/pi-mcp-adapter/dist/claude-plugin-loader.js +0 -362
- package/node_modules/pi-mcp-adapter/dist/claude-plugin-loader.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/config.d.ts +0 -134
- package/node_modules/pi-mcp-adapter/dist/config.js +0 -1361
- package/node_modules/pi-mcp-adapter/dist/config.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/consent-manager.d.ts +0 -18
- package/node_modules/pi-mcp-adapter/dist/consent-manager.js +0 -88
- package/node_modules/pi-mcp-adapter/dist/consent-manager.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/elicitation-handler.d.ts +0 -17
- package/node_modules/pi-mcp-adapter/dist/elicitation-handler.js +0 -316
- package/node_modules/pi-mcp-adapter/dist/elicitation-handler.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/errors.d.ts +0 -131
- package/node_modules/pi-mcp-adapter/dist/errors.js +0 -278
- package/node_modules/pi-mcp-adapter/dist/errors.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/http-ca.d.ts +0 -8
- package/node_modules/pi-mcp-adapter/dist/http-ca.js +0 -88
- package/node_modules/pi-mcp-adapter/dist/http-ca.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/jev-client.d.ts +0 -14
- package/node_modules/pi-mcp-adapter/dist/jev-client.js +0 -367
- package/node_modules/pi-mcp-adapter/dist/jev-client.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/jev-contracts.d.ts +0 -77
- package/node_modules/pi-mcp-adapter/dist/jev-contracts.js +0 -2
- package/node_modules/pi-mcp-adapter/dist/jev-contracts.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/jev-key-store.d.ts +0 -51
- package/node_modules/pi-mcp-adapter/dist/jev-key-store.js +0 -205
- package/node_modules/pi-mcp-adapter/dist/jev-key-store.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/json-schema-validator.d.ts +0 -2
- package/node_modules/pi-mcp-adapter/dist/json-schema-validator.js +0 -68
- package/node_modules/pi-mcp-adapter/dist/json-schema-validator.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/lifecycle.d.ts +0 -56
- package/node_modules/pi-mcp-adapter/dist/lifecycle.js +0 -410
- package/node_modules/pi-mcp-adapter/dist/lifecycle.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/logger.d.ts +0 -51
- package/node_modules/pi-mcp-adapter/dist/logger.js +0 -131
- package/node_modules/pi-mcp-adapter/dist/logger.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.d.ts +0 -26
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.js +0 -103
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.d.ts +0 -118
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.js +0 -1097
- package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-auth.d.ts +0 -170
- package/node_modules/pi-mcp-adapter/dist/mcp-auth.js +0 -1142
- package/node_modules/pi-mcp-adapter/dist/mcp-auth.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-bearer-store.d.ts +0 -24
- package/node_modules/pi-mcp-adapter/dist/mcp-bearer-store.js +0 -203
- package/node_modules/pi-mcp-adapter/dist/mcp-bearer-store.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.d.ts +0 -53
- package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.js +0 -441
- package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.d.ts +0 -148
- package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.js +0 -689
- package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-probe.d.ts +0 -6
- package/node_modules/pi-mcp-adapter/dist/mcp-probe.js +0 -166
- package/node_modules/pi-mcp-adapter/dist/mcp-probe.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-tasks.d.ts +0 -102
- package/node_modules/pi-mcp-adapter/dist/mcp-tasks.js +0 -369
- package/node_modules/pi-mcp-adapter/dist/mcp-tasks.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/mcp-trace.d.ts +0 -95
- package/node_modules/pi-mcp-adapter/dist/mcp-trace.js +0 -242
- package/node_modules/pi-mcp-adapter/dist/mcp-trace.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/metadata-cache.d.ts +0 -19
- package/node_modules/pi-mcp-adapter/dist/metadata-cache.js +0 -301
- package/node_modules/pi-mcp-adapter/dist/metadata-cache.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/npx-resolver.d.ts +0 -6
- package/node_modules/pi-mcp-adapter/dist/npx-resolver.js +0 -505
- package/node_modules/pi-mcp-adapter/dist/npx-resolver.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/package-mcp-loader.d.ts +0 -2
- package/node_modules/pi-mcp-adapter/dist/package-mcp-loader.js +0 -144
- package/node_modules/pi-mcp-adapter/dist/package-mcp-loader.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/request-headers-command.d.ts +0 -10
- package/node_modules/pi-mcp-adapter/dist/request-headers-command.js +0 -312
- package/node_modules/pi-mcp-adapter/dist/request-headers-command.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/resource-tools.d.ts +0 -1
- package/node_modules/pi-mcp-adapter/dist/resource-tools.js +0 -15
- package/node_modules/pi-mcp-adapter/dist/resource-tools.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/runtime-owner.d.ts +0 -13
- package/node_modules/pi-mcp-adapter/dist/runtime-owner.js +0 -97
- package/node_modules/pi-mcp-adapter/dist/runtime-owner.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/sampling-handler.d.ts +0 -17
- package/node_modules/pi-mcp-adapter/dist/sampling-handler.js +0 -203
- package/node_modules/pi-mcp-adapter/dist/sampling-handler.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/secure-keyring.d.ts +0 -11
- package/node_modules/pi-mcp-adapter/dist/secure-keyring.js +0 -172
- package/node_modules/pi-mcp-adapter/dist/secure-keyring.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/server-manager.d.ts +0 -157
- package/node_modules/pi-mcp-adapter/dist/server-manager.js +0 -1745
- package/node_modules/pi-mcp-adapter/dist/server-manager.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/session-approvals.d.ts +0 -34
- package/node_modules/pi-mcp-adapter/dist/session-approvals.js +0 -140
- package/node_modules/pi-mcp-adapter/dist/session-approvals.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/session-recovery.d.ts +0 -26
- package/node_modules/pi-mcp-adapter/dist/session-recovery.js +0 -142
- package/node_modules/pi-mcp-adapter/dist/session-recovery.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/state.d.ts +0 -73
- package/node_modules/pi-mcp-adapter/dist/state.js +0 -2
- package/node_modules/pi-mcp-adapter/dist/state.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/types.d.ts +0 -642
- package/node_modules/pi-mcp-adapter/dist/types.js +0 -331
- package/node_modules/pi-mcp-adapter/dist/types.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/ui-app-bridge-helpers.d.ts +0 -6
- package/node_modules/pi-mcp-adapter/dist/ui-app-bridge-helpers.js +0 -37
- package/node_modules/pi-mcp-adapter/dist/ui-app-bridge-helpers.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.d.ts +0 -17
- package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.js +0 -219
- package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/ui-stream-types.d.ts +0 -104
- package/node_modules/pi-mcp-adapter/dist/ui-stream-types.js +0 -59
- package/node_modules/pi-mcp-adapter/dist/ui-stream-types.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/ui-tool-visibility.d.ts +0 -4
- package/node_modules/pi-mcp-adapter/dist/ui-tool-visibility.js +0 -27
- package/node_modules/pi-mcp-adapter/dist/ui-tool-visibility.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.d.ts +0 -15
- package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.js +0 -85
- package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.js.map +0 -1
- package/node_modules/pi-mcp-adapter/dist/utils.d.ts +0 -41
- package/node_modules/pi-mcp-adapter/dist/utils.js +0 -417
- package/node_modules/pi-mcp-adapter/dist/utils.js.map +0 -1
- package/node_modules/pi-mcp-adapter/elicitation-handler.ts +0 -358
- package/node_modules/pi-mcp-adapter/error-signal.ts +0 -32
- package/node_modules/pi-mcp-adapter/errors.ts +0 -364
- package/node_modules/pi-mcp-adapter/examples/jev-accessibility-loop.mjs +0 -85
- package/node_modules/pi-mcp-adapter/examples/jev-semantic-filter.mjs +0 -34
- package/node_modules/pi-mcp-adapter/failure-backoff.ts +0 -23
- package/node_modules/pi-mcp-adapter/glimpse-ui.ts +0 -80
- package/node_modules/pi-mcp-adapter/host-html-template.ts +0 -515
- package/node_modules/pi-mcp-adapter/http-ca.ts +0 -84
- package/node_modules/pi-mcp-adapter/index.ts +0 -2106
- package/node_modules/pi-mcp-adapter/init.ts +0 -721
- package/node_modules/pi-mcp-adapter/jev-client.ts +0 -300
- package/node_modules/pi-mcp-adapter/jev-contracts.ts +0 -53
- package/node_modules/pi-mcp-adapter/jev-key-store.ts +0 -192
- package/node_modules/pi-mcp-adapter/json-schema-validator.ts +0 -83
- package/node_modules/pi-mcp-adapter/lazy-loader.ts +0 -7
- package/node_modules/pi-mcp-adapter/lifecycle.ts +0 -457
- package/node_modules/pi-mcp-adapter/logger.ts +0 -169
- package/node_modules/pi-mcp-adapter/mcp-auth-fetch.ts +0 -113
- package/node_modules/pi-mcp-adapter/mcp-auth-flow.ts +0 -1296
- package/node_modules/pi-mcp-adapter/mcp-auth.ts +0 -1287
- package/node_modules/pi-mcp-adapter/mcp-bearer-store.ts +0 -250
- package/node_modules/pi-mcp-adapter/mcp-callback-server.ts +0 -534
- package/node_modules/pi-mcp-adapter/mcp-code.ts +0 -495
- package/node_modules/pi-mcp-adapter/mcp-install.ts +0 -60
- package/node_modules/pi-mcp-adapter/mcp-keyring-helper.cjs +0 -89
- package/node_modules/pi-mcp-adapter/mcp-oauth-provider.ts +0 -819
- package/node_modules/pi-mcp-adapter/mcp-output-guard.ts +0 -511
- package/node_modules/pi-mcp-adapter/mcp-panel-theme.ts +0 -104
- package/node_modules/pi-mcp-adapter/mcp-panel.ts +0 -1099
- package/node_modules/pi-mcp-adapter/mcp-probe.ts +0 -201
- package/node_modules/pi-mcp-adapter/mcp-references.ts +0 -373
- package/node_modules/pi-mcp-adapter/mcp-script-worker.mjs +0 -137
- package/node_modules/pi-mcp-adapter/mcp-setup-panel.ts +0 -747
- package/node_modules/pi-mcp-adapter/mcp-status.ts +0 -105
- package/node_modules/pi-mcp-adapter/mcp-tasks.ts +0 -467
- package/node_modules/pi-mcp-adapter/mcp-trace.ts +0 -306
- package/node_modules/pi-mcp-adapter/metadata-cache.ts +0 -362
- package/node_modules/pi-mcp-adapter/namespace-tools.ts +0 -334
- package/node_modules/pi-mcp-adapter/npx-resolver.ts +0 -543
- package/node_modules/pi-mcp-adapter/oauth-handler.ts +0 -30
- package/node_modules/pi-mcp-adapter/oauth.ts +0 -52
- package/node_modules/pi-mcp-adapter/onboarding-state.ts +0 -76
- package/node_modules/pi-mcp-adapter/package-mcp-loader.ts +0 -156
- package/node_modules/pi-mcp-adapter/package.json +0 -219
- package/node_modules/pi-mcp-adapter/panel-keys.ts +0 -53
- package/node_modules/pi-mcp-adapter/prompts.ts +0 -385
- package/node_modules/pi-mcp-adapter/proxy-modes.ts +0 -1735
- package/node_modules/pi-mcp-adapter/request-headers-command.ts +0 -333
- package/node_modules/pi-mcp-adapter/resource-tools.ts +0 -17
- package/node_modules/pi-mcp-adapter/runtime-owner.ts +0 -106
- package/node_modules/pi-mcp-adapter/sampling-handler.ts +0 -261
- package/node_modules/pi-mcp-adapter/sandbox-proxy-template.ts +0 -144
- package/node_modules/pi-mcp-adapter/search-ranking.ts +0 -309
- package/node_modules/pi-mcp-adapter/secure-keyring.ts +0 -170
- package/node_modules/pi-mcp-adapter/semantic-search.ts +0 -186
- package/node_modules/pi-mcp-adapter/server-manager.ts +0 -2088
- package/node_modules/pi-mcp-adapter/session-approvals.ts +0 -201
- package/node_modules/pi-mcp-adapter/session-recovery.ts +0 -158
- package/node_modules/pi-mcp-adapter/skills/mcp-scripting/SKILL.md +0 -49
- package/node_modules/pi-mcp-adapter/state.ts +0 -71
- package/node_modules/pi-mcp-adapter/tool-approval.ts +0 -205
- package/node_modules/pi-mcp-adapter/tool-metadata.ts +0 -302
- package/node_modules/pi-mcp-adapter/tool-registrar.ts +0 -255
- package/node_modules/pi-mcp-adapter/tool-result-renderer.ts +0 -573
- package/node_modules/pi-mcp-adapter/ts-shape.ts +0 -149
- package/node_modules/pi-mcp-adapter/types.ts +0 -1120
- package/node_modules/pi-mcp-adapter/ui-app-bridge-helpers.ts +0 -40
- package/node_modules/pi-mcp-adapter/ui-resource-handler.ts +0 -266
- package/node_modules/pi-mcp-adapter/ui-server.ts +0 -993
- package/node_modules/pi-mcp-adapter/ui-session.ts +0 -624
- package/node_modules/pi-mcp-adapter/ui-stream-types.ts +0 -89
- package/node_modules/pi-mcp-adapter/ui-tool-visibility.ts +0 -25
- package/node_modules/pi-mcp-adapter/unix-socket-transport.ts +0 -85
- package/node_modules/pi-mcp-adapter/utils.ts +0 -464
|
@@ -1,457 +0,0 @@
|
|
|
1
|
-
import { SdkError, SdkErrorCode } from "@modelcontextprotocol/client";
|
|
2
|
-
import { isServerDisabled, type ServerDefinition } from "./types.ts";
|
|
3
|
-
import {
|
|
4
|
-
isTransientHttpConnectError,
|
|
5
|
-
isUnauthorizedHttpError,
|
|
6
|
-
type McpServerManager,
|
|
7
|
-
type ServerConnection,
|
|
8
|
-
} from "./server-manager.ts";
|
|
9
|
-
import { hasPendingAuth } from "./mcp-auth-flow.ts";
|
|
10
|
-
import { logger } from "./logger.ts";
|
|
11
|
-
import { formatTerminalError, parallelLimit, sanitizeTerminalText } from "./utils.ts";
|
|
12
|
-
import { isTerminatedSession } from "./session-recovery.ts";
|
|
13
|
-
|
|
14
|
-
export type ReconnectCallback = (serverName: string) => void | Promise<void>;
|
|
15
|
-
export type ReconnectFailureCallback = (serverName: string, error: unknown) => void;
|
|
16
|
-
export type HealthRestoredCallback = (serverName: string) => void | Promise<void>;
|
|
17
|
-
export type AuthRequiredCallback = (serverName: string) => void | Promise<void>;
|
|
18
|
-
|
|
19
|
-
const KEEP_ALIVE_RETRY_BASE_MS = 30_000;
|
|
20
|
-
const KEEP_ALIVE_RETRY_MAX_MS = 5 * 60_000;
|
|
21
|
-
const KEEP_ALIVE_CHECK_CONCURRENCY = 10;
|
|
22
|
-
|
|
23
|
-
interface RetryState {
|
|
24
|
-
attempts: number;
|
|
25
|
-
nextAttemptAt: number;
|
|
26
|
-
connection: ServerConnection | undefined;
|
|
27
|
-
status: ServerConnection["status"] | undefined;
|
|
28
|
-
warningReported: boolean;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export class McpLifecycleManager {
|
|
32
|
-
private keepAliveServers = new Map<string, ServerDefinition>();
|
|
33
|
-
private allServers = new Map<string, ServerDefinition>();
|
|
34
|
-
private serverSettings = new Map<string, { idleTimeout?: number }>();
|
|
35
|
-
private globalIdleTimeout = 10 * 60 * 1000;
|
|
36
|
-
private healthCheckInterval: NodeJS.Timeout | undefined;
|
|
37
|
-
private onReconnect: ReconnectCallback | undefined;
|
|
38
|
-
private onReconnectFailure: ReconnectFailureCallback | undefined;
|
|
39
|
-
private onHealthRestored: HealthRestoredCallback | undefined;
|
|
40
|
-
private onAuthRequired: AuthRequiredCallback | undefined;
|
|
41
|
-
private onIdleShutdown: ((serverName: string) => void) | undefined;
|
|
42
|
-
private activeHealthCheck: Promise<void> | undefined;
|
|
43
|
-
private activeConvergence: Promise<void> | undefined;
|
|
44
|
-
private shutdownPromise: Promise<void> | undefined;
|
|
45
|
-
private retryStates = new Map<string, RetryState>();
|
|
46
|
-
private pendingMetadataPublications = new Set<string>();
|
|
47
|
-
private stopped = false;
|
|
48
|
-
private removeHealthAbortListener: (() => void) | undefined;
|
|
49
|
-
|
|
50
|
-
constructor(
|
|
51
|
-
private readonly manager: McpServerManager,
|
|
52
|
-
private readonly hasPendingAuthForServer = hasPendingAuth,
|
|
53
|
-
) {}
|
|
54
|
-
|
|
55
|
-
setReconnectCallback(callback: ReconnectCallback): void {
|
|
56
|
-
this.onReconnect = callback;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
setReconnectFailureCallback(callback: ReconnectFailureCallback): void {
|
|
60
|
-
this.onReconnectFailure = callback;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
setHealthRestoredCallback(callback: HealthRestoredCallback): void {
|
|
64
|
-
this.onHealthRestored = callback;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
setAuthRequiredCallback(callback: AuthRequiredCallback): void {
|
|
68
|
-
this.onAuthRequired = callback;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
markKeepAlive(name: string, definition: ServerDefinition): void {
|
|
72
|
-
if (isServerDisabled(definition)) return;
|
|
73
|
-
this.keepAliveServers.set(name, definition);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
registerServer(name: string, definition: ServerDefinition, settings?: { idleTimeout?: number }): void {
|
|
77
|
-
if (isServerDisabled(definition)) return;
|
|
78
|
-
this.allServers.set(name, definition);
|
|
79
|
-
if (settings?.idleTimeout !== undefined) this.serverSettings.set(name, settings);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
unregisterServer(name: string): void {
|
|
83
|
-
this.allServers.delete(name);
|
|
84
|
-
this.keepAliveServers.delete(name);
|
|
85
|
-
this.serverSettings.delete(name);
|
|
86
|
-
this.retryStates.delete(name);
|
|
87
|
-
this.pendingMetadataPublications.delete(name);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
setGlobalIdleTimeout(minutes: number): void {
|
|
91
|
-
this.globalIdleTimeout = minutes * 60 * 1000;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
setIdleShutdownCallback(callback: (serverName: string) => void): void {
|
|
95
|
-
this.onIdleShutdown = callback;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
startHealthChecks(signalOrInterval?: AbortSignal | number, maybeIntervalMs = 30000): void {
|
|
99
|
-
const signal = typeof signalOrInterval === "number" ? undefined : signalOrInterval;
|
|
100
|
-
const intervalMs = typeof signalOrInterval === "number" ? signalOrInterval : maybeIntervalMs;
|
|
101
|
-
this.stopped = false;
|
|
102
|
-
if (signal?.aborted) {
|
|
103
|
-
this.stopped = true;
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
const stop = () => {
|
|
107
|
-
this.stopped = true;
|
|
108
|
-
if (this.healthCheckInterval) clearInterval(this.healthCheckInterval);
|
|
109
|
-
this.healthCheckInterval = undefined;
|
|
110
|
-
};
|
|
111
|
-
signal?.addEventListener("abort", stop, { once: true });
|
|
112
|
-
this.removeHealthAbortListener = () => signal?.removeEventListener("abort", stop);
|
|
113
|
-
this.healthCheckInterval = setInterval(() => {
|
|
114
|
-
if (this.stopped || signal?.aborted || this.activeHealthCheck) return;
|
|
115
|
-
const check = this.checkConnections(signal)
|
|
116
|
-
.catch(error => {
|
|
117
|
-
console.error(`MCP: Health check failed: ${formatTerminalError(error)}`);
|
|
118
|
-
})
|
|
119
|
-
.finally(() => {
|
|
120
|
-
if (this.activeHealthCheck === check) this.activeHealthCheck = undefined;
|
|
121
|
-
});
|
|
122
|
-
this.activeHealthCheck = check;
|
|
123
|
-
}, intervalMs);
|
|
124
|
-
this.healthCheckInterval.unref();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async ensureConverged(signal?: AbortSignal): Promise<void> {
|
|
128
|
-
if (this.stopped || signal?.aborted) return;
|
|
129
|
-
if (this.activeConvergence) return this.activeConvergence;
|
|
130
|
-
|
|
131
|
-
const check = this.checkKeepAliveConnections(signal);
|
|
132
|
-
this.activeConvergence = check;
|
|
133
|
-
try {
|
|
134
|
-
await check;
|
|
135
|
-
} finally {
|
|
136
|
-
if (this.activeConvergence === check) this.activeConvergence = undefined;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private async checkConnections(signal?: AbortSignal): Promise<void> {
|
|
141
|
-
if (this.stopped || signal?.aborted) return;
|
|
142
|
-
await this.ensureConverged(signal);
|
|
143
|
-
if (this.stopped || signal?.aborted) return;
|
|
144
|
-
|
|
145
|
-
for (const [name] of this.allServers) {
|
|
146
|
-
if (this.keepAliveServers.has(name)) continue;
|
|
147
|
-
const timeout = this.getIdleTimeout(name);
|
|
148
|
-
if (timeout > 0 && this.manager.isIdle(name, timeout)) {
|
|
149
|
-
await this.manager.close(name);
|
|
150
|
-
if (this.stopped || signal?.aborted) return;
|
|
151
|
-
this.onIdleShutdown?.(name);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
private async checkKeepAliveConnections(signal?: AbortSignal): Promise<void> {
|
|
157
|
-
if (this.stopped || signal?.aborted) return;
|
|
158
|
-
await parallelLimit(
|
|
159
|
-
[...this.keepAliveServers],
|
|
160
|
-
KEEP_ALIVE_CHECK_CONCURRENCY,
|
|
161
|
-
([name, definition]) => this.checkKeepAliveConnection(name, definition, signal),
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
private async checkKeepAliveConnection(
|
|
166
|
-
name: string,
|
|
167
|
-
definition: ServerDefinition,
|
|
168
|
-
signal?: AbortSignal,
|
|
169
|
-
retrySuperseded = true,
|
|
170
|
-
): Promise<void> {
|
|
171
|
-
if (isServerDisabled(definition) || this.stopped || signal?.aborted) return;
|
|
172
|
-
// Fence against unregisterServer racing an in-flight convergence pass.
|
|
173
|
-
// Identity comparison also rejects stale passes after a same-name
|
|
174
|
-
// replacement registration, which a name check would wrongly accept.
|
|
175
|
-
if (this.keepAliveServers.get(name) !== definition) return;
|
|
176
|
-
const connection = this.manager.getConnection(name);
|
|
177
|
-
if (connection?.status === "needs-auth") {
|
|
178
|
-
this.pendingMetadataPublications.delete(name);
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
if (!this.shouldAttemptConnection(name, connection)) return;
|
|
182
|
-
if (!connection || connection.status !== "connected") {
|
|
183
|
-
if (this.hasPendingAuthForServer(name)) {
|
|
184
|
-
logger.debug(`Skipping reconnect for ${name} while OAuth authorization is pending`);
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
let freshConnection: ServerConnection;
|
|
188
|
-
try {
|
|
189
|
-
freshConnection = await this.manager.connect(name, definition, signal);
|
|
190
|
-
} catch (error) {
|
|
191
|
-
if (this.stopped || signal?.aborted) return;
|
|
192
|
-
this.reportConnectionFailure(name, definition, error, "reconnect", this.manager.getConnection(name));
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
if (this.stopped || signal?.aborted) return;
|
|
196
|
-
if (freshConnection.status === "needs-auth") {
|
|
197
|
-
await this.notifyAuthRequired(name, definition, freshConnection);
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
if (freshConnection.status !== "connected") {
|
|
201
|
-
this.reportConnectionFailure(
|
|
202
|
-
name,
|
|
203
|
-
definition,
|
|
204
|
-
new Error(`MCP server ${name} did not return a connected session`),
|
|
205
|
-
"reconnect",
|
|
206
|
-
freshConnection,
|
|
207
|
-
);
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
logger.debug(`Reconnected to ${name}`);
|
|
211
|
-
await this.publishConnectedMetadata(name, definition, freshConnection);
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (this.pendingMetadataPublications.has(name)) {
|
|
216
|
-
await this.publishConnectedMetadata(name, definition, connection);
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
if (!definition.url) return;
|
|
220
|
-
const hadSessionId = (connection.transport as { sessionId?: string } | undefined)?.sessionId != null;
|
|
221
|
-
let refreshResult: Awaited<ReturnType<McpServerManager["refreshTools"]>>;
|
|
222
|
-
try {
|
|
223
|
-
refreshResult = await this.manager.refreshTools(name, connection, signal);
|
|
224
|
-
} catch (error) {
|
|
225
|
-
if (this.stopped || signal?.aborted) return;
|
|
226
|
-
const current = this.manager.getConnection(name);
|
|
227
|
-
if (current !== connection || connection.status !== "connected") {
|
|
228
|
-
await this.handleSupersededConnection(name, definition, connection, signal, retrySuperseded);
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
if (!shouldReconnectAfterRefresh(error, hadSessionId, definition)) {
|
|
232
|
-
this.reportConnectionFailure(name, definition, error, "refresh", connection);
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
if (this.hasPendingAuthForServer(name)) {
|
|
236
|
-
logger.debug(`Skipping reconnect for ${name} while OAuth authorization is pending`);
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
let freshConnection: ServerConnection;
|
|
240
|
-
try {
|
|
241
|
-
freshConnection = await this.manager.reconnect(name, definition, connection, signal);
|
|
242
|
-
} catch (reconnectError) {
|
|
243
|
-
if (this.stopped || signal?.aborted) return;
|
|
244
|
-
this.reportConnectionFailure(name, definition, reconnectError, "reconnect", this.manager.getConnection(name));
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
if (this.stopped || signal?.aborted) return;
|
|
248
|
-
if (freshConnection.status === "needs-auth") {
|
|
249
|
-
await this.notifyAuthRequired(name, definition, freshConnection);
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
if (freshConnection.status !== "connected") {
|
|
253
|
-
this.reportConnectionFailure(
|
|
254
|
-
name,
|
|
255
|
-
definition,
|
|
256
|
-
new Error(`MCP server ${name} did not return a connected session`),
|
|
257
|
-
"reconnect",
|
|
258
|
-
freshConnection,
|
|
259
|
-
);
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
logger.debug(`Reconnected stale MCP session for ${name}`);
|
|
263
|
-
await this.publishConnectedMetadata(name, definition, freshConnection);
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
if (refreshResult === "superseded") {
|
|
268
|
-
await this.handleSupersededConnection(name, definition, connection, signal, retrySuperseded);
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
if (refreshResult === "refresh-timeout") {
|
|
272
|
-
this.deferRefreshTimeout(name, definition, connection);
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
if (this.keepAliveServers.get(name) !== definition) return;
|
|
276
|
-
if (this.retryStates.delete(name)) {
|
|
277
|
-
await this.onHealthRestored?.(name);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
private async handleSupersededConnection(
|
|
282
|
-
name: string,
|
|
283
|
-
definition: ServerDefinition,
|
|
284
|
-
staleConnection: ServerConnection,
|
|
285
|
-
signal: AbortSignal | undefined,
|
|
286
|
-
retrySuperseded: boolean,
|
|
287
|
-
): Promise<void> {
|
|
288
|
-
const current = this.manager.getConnection(name);
|
|
289
|
-
if (this.keepAliveServers.get(name) !== definition) return;
|
|
290
|
-
if (current === staleConnection && current.status === "connected") {
|
|
291
|
-
if (this.retryStates.delete(name)) await this.onHealthRestored?.(name);
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
if (current?.status === "connected") {
|
|
295
|
-
await this.publishConnectedMetadata(name, definition, current);
|
|
296
|
-
return;
|
|
297
|
-
}
|
|
298
|
-
if (current?.status === "needs-auth") {
|
|
299
|
-
await this.notifyAuthRequired(name, definition, current);
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
if (retrySuperseded) {
|
|
303
|
-
await this.checkKeepAliveConnection(name, definition, signal, false);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
private async publishConnectedMetadata(name: string, definition: ServerDefinition, connection: ServerConnection): Promise<void> {
|
|
308
|
-
// Fence stale convergence passes by definition identity so disposal, and
|
|
309
|
-
// disposal followed by a same-name replacement, never re-track this
|
|
310
|
-
// server. Close the pass's own connection only when it is still current,
|
|
311
|
-
// so a replacement's connection is never touched.
|
|
312
|
-
if (this.keepAliveServers.get(name) !== definition) {
|
|
313
|
-
if (this.manager.getConnection(name) === connection) await this.manager.close(name);
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
this.pendingMetadataPublications.add(name);
|
|
317
|
-
try {
|
|
318
|
-
await this.onReconnect?.(name);
|
|
319
|
-
this.pendingMetadataPublications.delete(name);
|
|
320
|
-
this.retryStates.delete(name);
|
|
321
|
-
} catch (error) {
|
|
322
|
-
if (this.stopped) return;
|
|
323
|
-
this.reportConnectionFailure(name, definition, error, "publish", connection);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
private async notifyAuthRequired(name: string, definition: ServerDefinition, connection: ServerConnection): Promise<void> {
|
|
328
|
-
// Fence stale convergence passes by definition identity. Close the
|
|
329
|
-
// leftover needs-auth connection only while it is still the manager's
|
|
330
|
-
// current entry, so a replacement's connection is never touched and the
|
|
331
|
-
// stale client/transport cannot leak when a replacement connects later.
|
|
332
|
-
if (this.keepAliveServers.get(name) !== definition) {
|
|
333
|
-
if (this.manager.getConnection(name) === connection) await this.manager.close(name);
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
this.pendingMetadataPublications.delete(name);
|
|
337
|
-
this.retryStates.delete(name);
|
|
338
|
-
try {
|
|
339
|
-
await this.onAuthRequired?.(name);
|
|
340
|
-
} catch (error) {
|
|
341
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
342
|
-
logger.debug(`MCP: auth-required callback failed for ${name}: ${sanitizeTerminalText(message)}`);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
private shouldAttemptConnection(name: string, connection: ServerConnection | undefined): boolean {
|
|
347
|
-
const retry = this.retryStates.get(name);
|
|
348
|
-
if (!retry) return true;
|
|
349
|
-
if (retry.connection !== connection || retry.status !== connection?.status) {
|
|
350
|
-
this.retryStates.delete(name);
|
|
351
|
-
return true;
|
|
352
|
-
}
|
|
353
|
-
return Date.now() >= retry.nextAttemptAt;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
private connectionFailureTarget(action: "refresh" | "reconnect" | "publish", name: string): string {
|
|
357
|
-
if (action === "reconnect") return `reconnect to ${name}`;
|
|
358
|
-
if (action === "publish") return `publish metadata for ${name}`;
|
|
359
|
-
return `refresh ${name}`;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
private reportConnectionFailure(
|
|
363
|
-
name: string,
|
|
364
|
-
definition: ServerDefinition,
|
|
365
|
-
error: unknown,
|
|
366
|
-
action: "refresh" | "reconnect" | "publish",
|
|
367
|
-
connection: ServerConnection | undefined,
|
|
368
|
-
): void {
|
|
369
|
-
if (!this.recordRetry(name, definition, connection)) return;
|
|
370
|
-
this.onReconnectFailure?.(name, error);
|
|
371
|
-
if (isTransientHttpConnectError(error)) return;
|
|
372
|
-
const retry = this.retryStates.get(name);
|
|
373
|
-
if (retry?.warningReported) return;
|
|
374
|
-
if (retry) retry.warningReported = true;
|
|
375
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
376
|
-
console.error(`MCP: Failed to ${this.connectionFailureTarget(action, name)}: ${sanitizeTerminalText(message)}`);
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
private deferRefreshTimeout(
|
|
380
|
-
name: string,
|
|
381
|
-
definition: ServerDefinition,
|
|
382
|
-
connection: ServerConnection | undefined,
|
|
383
|
-
): void {
|
|
384
|
-
if (!this.recordRetry(name, definition, connection)) return;
|
|
385
|
-
logger.debug(`MCP: keep-alive tools/list refresh timed out for ${name}; retrying after backoff`);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
private recordRetry(
|
|
389
|
-
name: string,
|
|
390
|
-
definition: ServerDefinition,
|
|
391
|
-
connection: ServerConnection | undefined,
|
|
392
|
-
): boolean {
|
|
393
|
-
// Do not recreate retry/failure state from a stale convergence pass after
|
|
394
|
-
// disposal or a same-name replacement registration.
|
|
395
|
-
if (this.keepAliveServers.get(name) !== definition) return false;
|
|
396
|
-
const previous = this.retryStates.get(name);
|
|
397
|
-
const attempts = (previous?.attempts ?? 0) + 1;
|
|
398
|
-
const delay = Math.min(
|
|
399
|
-
KEEP_ALIVE_RETRY_BASE_MS * 2 ** Math.min(attempts - 1, 10),
|
|
400
|
-
KEEP_ALIVE_RETRY_MAX_MS,
|
|
401
|
-
);
|
|
402
|
-
this.retryStates.set(name, {
|
|
403
|
-
attempts,
|
|
404
|
-
nextAttemptAt: Date.now() + delay,
|
|
405
|
-
connection,
|
|
406
|
-
status: connection?.status,
|
|
407
|
-
warningReported: previous?.warningReported ?? false,
|
|
408
|
-
});
|
|
409
|
-
return true;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
private getIdleTimeout(name: string): number {
|
|
413
|
-
const perServer = this.serverSettings.get(name)?.idleTimeout;
|
|
414
|
-
if (perServer !== undefined) return perServer * 60 * 1000;
|
|
415
|
-
return this.globalIdleTimeout;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
async gracefulShutdown(): Promise<void> {
|
|
419
|
-
if (this.shutdownPromise) return this.shutdownPromise;
|
|
420
|
-
this.shutdownPromise = this.shutdownOnce();
|
|
421
|
-
return this.shutdownPromise;
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
private async shutdownOnce(): Promise<void> {
|
|
425
|
-
this.stopped = true;
|
|
426
|
-
if (this.healthCheckInterval) clearInterval(this.healthCheckInterval);
|
|
427
|
-
this.healthCheckInterval = undefined;
|
|
428
|
-
this.removeHealthAbortListener?.();
|
|
429
|
-
this.removeHealthAbortListener = undefined;
|
|
430
|
-
await this.activeHealthCheck;
|
|
431
|
-
this.activeHealthCheck = undefined;
|
|
432
|
-
await this.activeConvergence;
|
|
433
|
-
this.activeConvergence = undefined;
|
|
434
|
-
this.onReconnect = undefined;
|
|
435
|
-
this.onReconnectFailure = undefined;
|
|
436
|
-
this.onHealthRestored = undefined;
|
|
437
|
-
this.onAuthRequired = undefined;
|
|
438
|
-
this.onIdleShutdown = undefined;
|
|
439
|
-
this.retryStates.clear();
|
|
440
|
-
this.pendingMetadataPublications.clear();
|
|
441
|
-
if (typeof this.manager.closeAll === "function") {
|
|
442
|
-
await this.manager.closeAll();
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
function shouldReconnectAfterRefresh(
|
|
448
|
-
error: unknown,
|
|
449
|
-
hadSessionId: boolean,
|
|
450
|
-
definition: ServerDefinition,
|
|
451
|
-
): boolean {
|
|
452
|
-
if (isTerminatedSession(error, hadSessionId)) return true;
|
|
453
|
-
// Reconnect baked bearer sources so connect-time resolution runs again.
|
|
454
|
-
if (definition.auth === "bearer" && isUnauthorizedHttpError(error)) return true;
|
|
455
|
-
return error instanceof SdkError
|
|
456
|
-
&& (error.code === SdkErrorCode.NotConnected || error.code === SdkErrorCode.ConnectionClosed);
|
|
457
|
-
}
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Centralized logging for MCP UI operations.
|
|
3
|
-
* Provides structured, contextual logs with levels.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
7
|
-
|
|
8
|
-
export interface LogContext {
|
|
9
|
-
server?: string;
|
|
10
|
-
session?: string;
|
|
11
|
-
tool?: string;
|
|
12
|
-
uri?: string;
|
|
13
|
-
[key: string]: unknown;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface LogEntry {
|
|
17
|
-
level: LogLevel;
|
|
18
|
-
message: string;
|
|
19
|
-
context?: LogContext;
|
|
20
|
-
error?: Error;
|
|
21
|
-
timestamp: Date;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
type LogHandler = (entry: LogEntry) => void;
|
|
25
|
-
|
|
26
|
-
const LEVEL_PRIORITY: Record<LogLevel, number> = {
|
|
27
|
-
debug: 0,
|
|
28
|
-
info: 1,
|
|
29
|
-
warn: 2,
|
|
30
|
-
error: 3,
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const LEVEL_PREFIX: Record<LogLevel, string> = {
|
|
34
|
-
debug: "[MCP-UI:DEBUG]",
|
|
35
|
-
info: "[MCP-UI]",
|
|
36
|
-
warn: "[MCP-UI:WARN]",
|
|
37
|
-
error: "[MCP-UI:ERROR]",
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
class Logger {
|
|
41
|
-
private minLevel: LogLevel = "info";
|
|
42
|
-
private handlers: LogHandler[] = [];
|
|
43
|
-
private defaultContext: LogContext = {};
|
|
44
|
-
|
|
45
|
-
setLevel(level: LogLevel): void {
|
|
46
|
-
this.minLevel = level;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
setDefaultContext(context: LogContext): void {
|
|
50
|
-
this.defaultContext = context;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
addHandler(handler: LogHandler): void {
|
|
54
|
-
this.handlers.push(handler);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
clearHandlers(): void {
|
|
58
|
-
this.handlers = [];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
private shouldLog(level: LogLevel): boolean {
|
|
62
|
-
return LEVEL_PRIORITY[level] >= LEVEL_PRIORITY[this.minLevel];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private emit(level: LogLevel, message: string, context?: LogContext, error?: Error): void {
|
|
66
|
-
if (!this.shouldLog(level)) return;
|
|
67
|
-
|
|
68
|
-
const entry: LogEntry = {
|
|
69
|
-
level,
|
|
70
|
-
message,
|
|
71
|
-
context: { ...this.defaultContext, ...context },
|
|
72
|
-
...(error !== undefined ? { error } : {}),
|
|
73
|
-
timestamp: new Date(),
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
// Default console output
|
|
77
|
-
const prefix = LEVEL_PREFIX[level];
|
|
78
|
-
const contextStr = formatContext(entry.context);
|
|
79
|
-
const fullMessage = contextStr ? `${prefix} ${message} ${contextStr}` : `${prefix} ${message}`;
|
|
80
|
-
|
|
81
|
-
if (level === "error") {
|
|
82
|
-
console.error(fullMessage, error ?? "");
|
|
83
|
-
} else if (level === "warn") {
|
|
84
|
-
console.warn(fullMessage);
|
|
85
|
-
} else if (level === "debug") {
|
|
86
|
-
console.debug(fullMessage);
|
|
87
|
-
} else {
|
|
88
|
-
console.log(fullMessage);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Custom handlers
|
|
92
|
-
for (const handler of this.handlers) {
|
|
93
|
-
try {
|
|
94
|
-
handler(entry);
|
|
95
|
-
} catch {
|
|
96
|
-
// Ignore handler errors
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
debug(message: string, context?: LogContext): void {
|
|
102
|
-
this.emit("debug", message, context);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
info(message: string, context?: LogContext): void {
|
|
106
|
-
this.emit("info", message, context);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
warn(message: string, context?: LogContext): void {
|
|
110
|
-
this.emit("warn", message, context);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
error(message: string, error?: Error, context?: LogContext): void {
|
|
114
|
-
this.emit("error", message, context, error);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Create a child logger with additional default context.
|
|
119
|
-
*/
|
|
120
|
-
child(context: LogContext): ChildLogger {
|
|
121
|
-
return new ChildLogger(this, context);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
class ChildLogger {
|
|
126
|
-
constructor(
|
|
127
|
-
private parent: Logger,
|
|
128
|
-
private context: LogContext
|
|
129
|
-
) {}
|
|
130
|
-
|
|
131
|
-
debug(message: string, context?: LogContext): void {
|
|
132
|
-
this.parent.debug(message, { ...this.context, ...context });
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
info(message: string, context?: LogContext): void {
|
|
136
|
-
this.parent.info(message, { ...this.context, ...context });
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
warn(message: string, context?: LogContext): void {
|
|
140
|
-
this.parent.warn(message, { ...this.context, ...context });
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
error(message: string, error?: Error, context?: LogContext): void {
|
|
144
|
-
this.parent.error(message, error, { ...this.context, ...context });
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
child(context: LogContext): ChildLogger {
|
|
148
|
-
return new ChildLogger(this.parent, { ...this.context, ...context });
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function formatContext(context?: LogContext): string {
|
|
153
|
-
if (!context || Object.keys(context).length === 0) return "";
|
|
154
|
-
const parts: string[] = [];
|
|
155
|
-
for (const [key, value] of Object.entries(context)) {
|
|
156
|
-
if (value !== undefined && value !== null) {
|
|
157
|
-
parts.push(`${key}=${typeof value === "string" ? value : JSON.stringify(value)}`);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return parts.length > 0 ? `(${parts.join(", ")})` : "";
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Singleton instance
|
|
164
|
-
export const logger = new Logger();
|
|
165
|
-
|
|
166
|
-
// Enable debug mode via environment variable
|
|
167
|
-
if (process.env.MCP_UI_DEBUG === "1" || process.env.MCP_UI_DEBUG === "true") {
|
|
168
|
-
logger.setLevel("debug");
|
|
169
|
-
}
|