@humain/terminal 0.0.2 → 0.0.3-beta.0
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/CHANGELOG.md +241 -24
- package/README.md +143 -119
- package/dist/cli/args.d.ts +4 -2
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +37 -39
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/auth-check.d.ts +19 -0
- package/dist/cli/auth-check.d.ts.map +1 -0
- package/dist/cli/auth-check.js +50 -0
- package/dist/cli/auth-check.js.map +1 -0
- package/dist/cli/auth-command.d.ts +24 -0
- package/dist/cli/auth-command.d.ts.map +1 -0
- package/dist/cli/auth-command.js +103 -0
- package/dist/cli/auth-command.js.map +1 -0
- package/dist/cli/config-selector.d.ts.map +1 -1
- package/dist/cli/config-selector.js +2 -2
- package/dist/cli/config-selector.js.map +1 -1
- package/dist/cli/credential-print.d.ts +5 -15
- package/dist/cli/credential-print.d.ts.map +1 -1
- package/dist/cli/credential-print.js +42 -83
- package/dist/cli/credential-print.js.map +1 -1
- package/dist/cli/experimental/auth.d.ts +16 -0
- package/dist/cli/experimental/auth.d.ts.map +1 -0
- package/dist/cli/experimental/auth.js +13 -0
- package/dist/cli/experimental/auth.js.map +1 -0
- package/dist/cli/experimental/cli.d.ts +6 -0
- package/dist/cli/experimental/cli.d.ts.map +1 -0
- package/dist/cli/experimental/cli.js +5 -0
- package/dist/cli/experimental/cli.js.map +1 -0
- package/dist/cli/experimental/command-options.d.ts +17 -0
- package/dist/cli/experimental/command-options.d.ts.map +1 -0
- package/dist/cli/experimental/command-options.js +35 -0
- package/dist/cli/experimental/command-options.js.map +1 -0
- package/dist/cli/experimental/command.d.ts +63 -0
- package/dist/cli/experimental/command.d.ts.map +1 -0
- package/dist/cli/experimental/command.js +130 -0
- package/dist/cli/experimental/command.js.map +1 -0
- package/dist/cli/experimental/commands/client.d.ts +13 -0
- package/dist/cli/experimental/commands/client.d.ts.map +1 -0
- package/dist/cli/experimental/commands/client.js +25 -0
- package/dist/cli/experimental/commands/client.js.map +1 -0
- package/dist/cli/experimental/commands/pi.d.ts +15 -0
- package/dist/cli/experimental/commands/pi.d.ts.map +1 -0
- package/dist/cli/experimental/commands/pi.js +28 -0
- package/dist/cli/experimental/commands/pi.js.map +1 -0
- package/dist/cli/experimental/commands/server.d.ts +13 -0
- package/dist/cli/experimental/commands/server.d.ts.map +1 -0
- package/dist/cli/experimental/commands/server.js +25 -0
- package/dist/cli/experimental/commands/server.js.map +1 -0
- package/dist/cli/experimental/transport-address.d.ts +10 -0
- package/dist/cli/experimental/transport-address.d.ts.map +1 -0
- package/dist/cli/experimental/transport-address.js +38 -0
- package/dist/cli/experimental/transport-address.js.map +1 -0
- package/dist/cli/list-models.d.ts +1 -1
- package/dist/cli/list-models.d.ts.map +1 -1
- package/dist/cli/list-models.js +2 -2
- package/dist/cli/list-models.js.map +1 -1
- package/dist/cli/startup-ui.d.ts +1 -1
- package/dist/cli/startup-ui.d.ts.map +1 -1
- package/dist/cli/startup-ui.js +2 -2
- package/dist/cli/startup-ui.js.map +1 -1
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +3 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/remote-session.d.ts +53 -0
- package/dist/client/remote-session.d.ts.map +1 -0
- package/dist/client/remote-session.js +340 -0
- package/dist/client/remote-session.js.map +1 -0
- package/dist/client/transcript.d.ts +12 -0
- package/dist/client/transcript.d.ts.map +1 -0
- package/dist/client/transcript.js +98 -0
- package/dist/client/transcript.js.map +1 -0
- package/dist/core/agent-session-services.d.ts +1 -0
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +1 -0
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/agent-session.d.ts +1 -10
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +72 -70
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage.d.ts +25 -9
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +237 -39
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +60 -41
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +2 -1
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +3 -0
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +27 -4
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/http-dispatcher.d.ts.map +1 -1
- package/dist/core/http-dispatcher.js +5 -0
- package/dist/core/http-dispatcher.js.map +1 -1
- package/dist/core/keybindings.d.ts +45 -5
- package/dist/core/keybindings.d.ts.map +1 -1
- package/dist/core/keybindings.js.map +1 -1
- package/dist/core/model-config.d.ts +29 -5
- package/dist/core/model-config.d.ts.map +1 -1
- package/dist/core/model-config.js +4 -0
- package/dist/core/model-config.js.map +1 -1
- package/dist/core/model-registry.d.ts +5 -3
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +13 -10
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver.d.ts +4 -3
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +42 -10
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/model-runtime.d.ts +30 -13
- package/dist/core/model-runtime.d.ts.map +1 -1
- package/dist/core/model-runtime.js +233 -73
- package/dist/core/model-runtime.js.map +1 -1
- package/dist/core/models-store.d.ts +12 -7
- package/dist/core/models-store.d.ts.map +1 -1
- package/dist/core/models-store.js +83 -14
- package/dist/core/models-store.js.map +1 -1
- package/dist/core/package-manager.d.ts +6 -3
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +80 -61
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/pi-manifest.d.ts +8 -0
- package/dist/core/pi-manifest.d.ts.map +1 -0
- package/dist/core/pi-manifest.js +28 -0
- package/dist/core/pi-manifest.js.map +1 -0
- package/dist/core/provider-composer.d.ts +4 -1
- package/dist/core/provider-composer.d.ts.map +1 -1
- package/dist/core/provider-composer.js +36 -15
- package/dist/core/provider-composer.js.map +1 -1
- package/dist/core/remote-catalog-provider.d.ts.map +1 -1
- package/dist/core/remote-catalog-provider.js +73 -67
- package/dist/core/remote-catalog-provider.js.map +1 -1
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +2 -2
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/runtime-credentials.d.ts +5 -5
- package/dist/core/runtime-credentials.d.ts.map +1 -1
- package/dist/core/runtime-credentials.js +11 -8
- package/dist/core/runtime-credentials.js.map +1 -1
- package/dist/core/sdk.d.ts +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +2 -2
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +3 -1
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts +12 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +40 -16
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +8 -0
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tools/bash.d.ts +5 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +16 -15
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/edit.d.ts +4 -0
- package/dist/core/tools/edit.d.ts.map +1 -1
- package/dist/core/tools/edit.js +11 -7
- package/dist/core/tools/edit.js.map +1 -1
- package/dist/core/tools/find.d.ts +7 -0
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +16 -19
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/grep.d.ts +4 -0
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +5 -1
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/core/tools/ls.d.ts +4 -0
- package/dist/core/tools/ls.d.ts.map +1 -1
- package/dist/core/tools/ls.js +5 -1
- package/dist/core/tools/ls.js.map +1 -1
- package/dist/core/tools/read.d.ts +4 -0
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +7 -3
- package/dist/core/tools/read.js.map +1 -1
- package/dist/core/tools/write.d.ts +4 -0
- package/dist/core/tools/write.d.ts.map +1 -1
- package/dist/core/tools/write.js +6 -2
- package/dist/core/tools/write.js.map +1 -1
- package/dist/extensions/llama/index.d.ts.map +1 -1
- package/dist/extensions/llama/index.js +11 -2
- package/dist/extensions/llama/index.js.map +1 -1
- package/dist/extensions/llama/provider.d.ts.map +1 -1
- package/dist/extensions/llama/provider.js +21 -7
- package/dist/extensions/llama/provider.js.map +1 -1
- package/dist/humain/agent-execution-protocol.d.ts +218 -0
- package/dist/humain/agent-execution-protocol.d.ts.map +1 -0
- package/dist/humain/agent-execution-protocol.js +204 -0
- package/dist/humain/agent-execution-protocol.js.map +1 -0
- package/dist/humain/agent-execution-runtime.d.ts +35 -0
- package/dist/humain/agent-execution-runtime.d.ts.map +1 -0
- package/dist/humain/agent-execution-runtime.js +170 -0
- package/dist/humain/agent-execution-runtime.js.map +1 -0
- package/dist/humain/index.d.ts.map +1 -1
- package/dist/humain/index.js +8 -7
- package/dist/humain/index.js.map +1 -1
- package/dist/humain/mcp-adapter/vendor/CHANGELOG.md +660 -0
- package/dist/humain/mcp-adapter/vendor/LICENSE +21 -0
- package/dist/humain/mcp-adapter/vendor/README.md +733 -0
- package/dist/humain/mcp-adapter/vendor/abort.ts +34 -0
- package/dist/humain/mcp-adapter/vendor/agent-dir.ts +60 -0
- package/dist/humain/mcp-adapter/vendor/agent-plugin-loader.ts +377 -0
- package/dist/humain/mcp-adapter/vendor/app-bridge.bundle.js +67 -0
- package/dist/humain/mcp-adapter/vendor/banner.png +0 -0
- package/dist/humain/mcp-adapter/vendor/cli.js +209 -0
- package/dist/humain/mcp-adapter/vendor/commands.ts +627 -0
- package/dist/humain/mcp-adapter/vendor/config.ts +1226 -0
- package/dist/humain/mcp-adapter/vendor/consent-manager.ts +64 -0
- package/dist/humain/mcp-adapter/vendor/direct-tools.ts +604 -0
- package/dist/humain/mcp-adapter/vendor/elicitation-handler.ts +348 -0
- package/dist/humain/mcp-adapter/vendor/error-signal.ts +21 -0
- package/dist/humain/mcp-adapter/vendor/errors.ts +223 -0
- package/dist/humain/mcp-adapter/vendor/glimpse-ui.ts +80 -0
- package/dist/humain/mcp-adapter/vendor/host-html-template.ts +461 -0
- package/dist/humain/mcp-adapter/vendor/index.bundle.d.mts +4 -0
- package/dist/humain/mcp-adapter/vendor/index.bundle.mjs +15923 -0
- package/dist/humain/mcp-adapter/vendor/index.ts +898 -0
- package/dist/humain/mcp-adapter/vendor/init.ts +649 -0
- package/dist/humain/mcp-adapter/vendor/json-schema-validator.ts +69 -0
- package/dist/humain/mcp-adapter/vendor/lifecycle.ts +147 -0
- package/dist/humain/mcp-adapter/vendor/logger.ts +169 -0
- package/dist/humain/mcp-adapter/vendor/mcp-auth-flow.ts +993 -0
- package/dist/humain/mcp-adapter/vendor/mcp-auth.ts +1000 -0
- package/dist/humain/mcp-adapter/vendor/mcp-callback-server.ts +510 -0
- package/dist/humain/mcp-adapter/vendor/mcp-code.ts +354 -0
- package/dist/humain/mcp-adapter/vendor/mcp-keyring-helper.cjs +89 -0
- package/dist/humain/mcp-adapter/vendor/mcp-oauth-provider.ts +616 -0
- package/dist/humain/mcp-adapter/vendor/mcp-output-guard.ts +408 -0
- package/dist/humain/mcp-adapter/vendor/mcp-panel.ts +1015 -0
- package/dist/humain/mcp-adapter/vendor/mcp-probe.ts +187 -0
- package/dist/humain/mcp-adapter/vendor/mcp-script-worker.mjs +130 -0
- package/dist/humain/mcp-adapter/vendor/mcp-setup-panel.ts +666 -0
- package/dist/humain/mcp-adapter/vendor/mcp-status.ts +109 -0
- package/dist/humain/mcp-adapter/vendor/mcp-trace.ts +306 -0
- package/dist/humain/mcp-adapter/vendor/metadata-cache.ts +353 -0
- package/dist/humain/mcp-adapter/vendor/npx-resolver.ts +543 -0
- package/dist/humain/mcp-adapter/vendor/oauth-handler.ts +30 -0
- package/dist/humain/mcp-adapter/vendor/oauth.ts +48 -0
- package/dist/humain/mcp-adapter/vendor/onboarding-state.ts +76 -0
- package/dist/humain/mcp-adapter/vendor/package.json +178 -0
- package/dist/humain/mcp-adapter/vendor/panel-keys.ts +53 -0
- package/dist/humain/mcp-adapter/vendor/prompts.ts +353 -0
- package/dist/humain/mcp-adapter/vendor/proxy-modes.ts +1331 -0
- package/dist/humain/mcp-adapter/vendor/resource-tools.ts +17 -0
- package/dist/humain/mcp-adapter/vendor/runtime-owner.ts +91 -0
- package/dist/humain/mcp-adapter/vendor/sampling-handler.ts +284 -0
- package/dist/humain/mcp-adapter/vendor/search-ranking.ts +206 -0
- package/dist/humain/mcp-adapter/vendor/server-manager.ts +1124 -0
- package/dist/humain/mcp-adapter/vendor/session-recovery.ts +151 -0
- package/dist/humain/mcp-adapter/vendor/skills/mcp-scripting/SKILL.md +38 -0
- package/dist/humain/mcp-adapter/vendor/state.ts +62 -0
- package/dist/humain/mcp-adapter/vendor/tool-approval.ts +182 -0
- package/dist/humain/mcp-adapter/vendor/tool-metadata.ts +276 -0
- package/dist/humain/mcp-adapter/vendor/tool-registrar.ts +253 -0
- package/dist/humain/mcp-adapter/vendor/tool-result-renderer.ts +423 -0
- package/dist/humain/mcp-adapter/vendor/ts-shape.ts +149 -0
- package/dist/humain/mcp-adapter/vendor/types.ts +859 -0
- package/dist/humain/mcp-adapter/vendor/ui-app-bridge-helpers.ts +40 -0
- package/dist/humain/mcp-adapter/vendor/ui-resource-handler.ts +250 -0
- package/dist/humain/mcp-adapter/vendor/ui-server.ts +866 -0
- package/dist/humain/mcp-adapter/vendor/ui-session.ts +570 -0
- package/dist/humain/mcp-adapter/vendor/ui-stream-types.ts +89 -0
- package/dist/humain/mcp-adapter/vendor/ui-tool-visibility.ts +25 -0
- package/dist/humain/mcp-adapter/vendor/unix-socket-transport.ts +85 -0
- package/dist/humain/mcp-adapter/vendor/utils.ts +310 -0
- package/dist/humain/mcp-adapter.d.ts +3 -0
- package/dist/humain/mcp-adapter.d.ts.map +1 -0
- package/dist/humain/mcp-adapter.js +5 -0
- package/dist/humain/mcp-adapter.js.map +1 -0
- package/dist/humain/pi-agent-execution-host.d.ts +34 -0
- package/dist/humain/pi-agent-execution-host.d.ts.map +1 -0
- package/dist/humain/pi-agent-execution-host.js +77 -0
- package/dist/humain/pi-agent-execution-host.js.map +1 -0
- package/dist/humain/web-access/host.d.ts +4 -0
- package/dist/humain/web-access/host.d.ts.map +1 -0
- package/dist/humain/web-access/host.js +3 -0
- package/dist/humain/web-access/host.js.map +1 -0
- package/dist/humain/web-access/index.d.ts +3 -0
- package/dist/humain/web-access/index.d.ts.map +1 -0
- package/dist/humain/web-access/index.js +5 -0
- package/dist/humain/web-access/index.js.map +1 -0
- package/dist/humain/web-access/vendor/activity.d.ts +34 -0
- package/dist/humain/web-access/vendor/activity.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/activity.js +68 -0
- package/dist/humain/web-access/vendor/activity.js.map +1 -0
- package/dist/humain/web-access/vendor/anysearch.d.ts +8 -0
- package/dist/humain/web-access/vendor/anysearch.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/anysearch.js +167 -0
- package/dist/humain/web-access/vendor/anysearch.js.map +1 -0
- package/dist/humain/web-access/vendor/bocha.d.ts +4 -0
- package/dist/humain/web-access/vendor/bocha.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/bocha.js +219 -0
- package/dist/humain/web-access/vendor/bocha.js.map +1 -0
- package/dist/humain/web-access/vendor/brave.d.ts +4 -0
- package/dist/humain/web-access/vendor/brave.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/brave.js +194 -0
- package/dist/humain/web-access/vendor/brave.js.map +1 -0
- package/dist/humain/web-access/vendor/brightdata-unlocker.d.ts +12 -0
- package/dist/humain/web-access/vendor/brightdata-unlocker.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/brightdata-unlocker.js +230 -0
- package/dist/humain/web-access/vendor/brightdata-unlocker.js.map +1 -0
- package/dist/humain/web-access/vendor/brightdata.d.ts +8 -0
- package/dist/humain/web-access/vendor/brightdata.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/brightdata.js +519 -0
- package/dist/humain/web-access/vendor/brightdata.js.map +1 -0
- package/dist/humain/web-access/vendor/chrome-cookies.d.ts +10 -0
- package/dist/humain/web-access/vendor/chrome-cookies.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/chrome-cookies.js +438 -0
- package/dist/humain/web-access/vendor/chrome-cookies.js.map +1 -0
- package/dist/humain/web-access/vendor/content-find.d.ts +11 -0
- package/dist/humain/web-access/vendor/content-find.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/content-find.js +117 -0
- package/dist/humain/web-access/vendor/content-find.js.map +1 -0
- package/dist/humain/web-access/vendor/credential-source.d.ts +28 -0
- package/dist/humain/web-access/vendor/credential-source.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/credential-source.js +151 -0
- package/dist/humain/web-access/vendor/credential-source.js.map +1 -0
- package/dist/humain/web-access/vendor/curator-page.d.ts +29 -0
- package/dist/humain/web-access/vendor/curator-page.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/curator-page.js +3554 -0
- package/dist/humain/web-access/vendor/curator-page.js.map +1 -0
- package/dist/humain/web-access/vendor/curator-server.d.ts +93 -0
- package/dist/humain/web-access/vendor/curator-server.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/curator-server.js +688 -0
- package/dist/humain/web-access/vendor/curator-server.js.map +1 -0
- package/dist/humain/web-access/vendor/datalab-pdf-extract.d.ts +60 -0
- package/dist/humain/web-access/vendor/datalab-pdf-extract.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/datalab-pdf-extract.js +416 -0
- package/dist/humain/web-access/vendor/datalab-pdf-extract.js.map +1 -0
- package/dist/humain/web-access/vendor/declared-web-links.d.ts +16 -0
- package/dist/humain/web-access/vendor/declared-web-links.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/declared-web-links.js +168 -0
- package/dist/humain/web-access/vendor/declared-web-links.js.map +1 -0
- package/dist/humain/web-access/vendor/duckduckgo.d.ts +4 -0
- package/dist/humain/web-access/vendor/duckduckgo.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/duckduckgo.js +123 -0
- package/dist/humain/web-access/vendor/duckduckgo.js.map +1 -0
- package/dist/humain/web-access/vendor/exa.d.ts +10 -0
- package/dist/humain/web-access/vendor/exa.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/exa.js +426 -0
- package/dist/humain/web-access/vendor/exa.js.map +1 -0
- package/dist/humain/web-access/vendor/extract.d.ts +46 -0
- package/dist/humain/web-access/vendor/extract.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/extract.js +1064 -0
- package/dist/humain/web-access/vendor/extract.js.map +1 -0
- package/dist/humain/web-access/vendor/feature-config.d.ts +3 -0
- package/dist/humain/web-access/vendor/feature-config.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/feature-config.js +27 -0
- package/dist/humain/web-access/vendor/feature-config.js.map +1 -0
- package/dist/humain/web-access/vendor/fetch-params.d.ts +25 -0
- package/dist/humain/web-access/vendor/fetch-params.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/fetch-params.js +57 -0
- package/dist/humain/web-access/vendor/fetch-params.js.map +1 -0
- package/dist/humain/web-access/vendor/firecrawl.d.ts +12 -0
- package/dist/humain/web-access/vendor/firecrawl.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/firecrawl.js +239 -0
- package/dist/humain/web-access/vendor/firecrawl.js.map +1 -0
- package/dist/humain/web-access/vendor/gemini-api.d.ts +27 -0
- package/dist/humain/web-access/vendor/gemini-api.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/gemini-api.js +230 -0
- package/dist/humain/web-access/vendor/gemini-api.js.map +1 -0
- package/dist/humain/web-access/vendor/gemini-pdf-extract.d.ts +9 -0
- package/dist/humain/web-access/vendor/gemini-pdf-extract.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/gemini-pdf-extract.js +85 -0
- package/dist/humain/web-access/vendor/gemini-pdf-extract.js.map +1 -0
- package/dist/humain/web-access/vendor/gemini-search.d.ts +40 -0
- package/dist/humain/web-access/vendor/gemini-search.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/gemini-search.js +814 -0
- package/dist/humain/web-access/vendor/gemini-search.js.map +1 -0
- package/dist/humain/web-access/vendor/gemini-url-context.d.ts +4 -0
- package/dist/humain/web-access/vendor/gemini-url-context.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/gemini-url-context.js +101 -0
- package/dist/humain/web-access/vendor/gemini-url-context.js.map +1 -0
- package/dist/humain/web-access/vendor/gemini-web-config.d.ts +4 -0
- package/dist/humain/web-access/vendor/gemini-web-config.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/gemini-web-config.js +42 -0
- package/dist/humain/web-access/vendor/gemini-web-config.js.map +1 -0
- package/dist/humain/web-access/vendor/gemini-web.d.ts +18 -0
- package/dist/humain/web-access/vendor/gemini-web.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/gemini-web.js +385 -0
- package/dist/humain/web-access/vendor/gemini-web.js.map +1 -0
- package/dist/humain/web-access/vendor/github-api.d.ts +7 -0
- package/dist/humain/web-access/vendor/github-api.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/github-api.js +160 -0
- package/dist/humain/web-access/vendor/github-api.js.map +1 -0
- package/dist/humain/web-access/vendor/github-extract.d.ts +13 -0
- package/dist/humain/web-access/vendor/github-extract.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/github-extract.js +635 -0
- package/dist/humain/web-access/vendor/github-extract.js.map +1 -0
- package/dist/humain/web-access/vendor/index.d.ts +4 -0
- package/dist/humain/web-access/vendor/index.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/index.js +3002 -0
- package/dist/humain/web-access/vendor/index.js.map +1 -0
- package/dist/humain/web-access/vendor/jina-search.d.ts +8 -0
- package/dist/humain/web-access/vendor/jina-search.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/jina-search.js +255 -0
- package/dist/humain/web-access/vendor/jina-search.js.map +1 -0
- package/dist/humain/web-access/vendor/kagi.d.ts +15 -0
- package/dist/humain/web-access/vendor/kagi.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/kagi.js +262 -0
- package/dist/humain/web-access/vendor/kagi.js.map +1 -0
- package/dist/humain/web-access/vendor/ollama.d.ts +15 -0
- package/dist/humain/web-access/vendor/ollama.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/ollama.js +216 -0
- package/dist/humain/web-access/vendor/ollama.js.map +1 -0
- package/dist/humain/web-access/vendor/openai-search.d.ts +15 -0
- package/dist/humain/web-access/vendor/openai-search.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/openai-search.js +459 -0
- package/dist/humain/web-access/vendor/openai-search.js.map +1 -0
- package/dist/humain/web-access/vendor/page-query.d.ts +16 -0
- package/dist/humain/web-access/vendor/page-query.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/page-query.js +79 -0
- package/dist/humain/web-access/vendor/page-query.js.map +1 -0
- package/dist/humain/web-access/vendor/parallel.d.ts +12 -0
- package/dist/humain/web-access/vendor/parallel.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/parallel.js +343 -0
- package/dist/humain/web-access/vendor/parallel.js.map +1 -0
- package/dist/humain/web-access/vendor/pdf-extract.d.ts +44 -0
- package/dist/humain/web-access/vendor/pdf-extract.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/pdf-extract.js +298 -0
- package/dist/humain/web-access/vendor/pdf-extract.js.map +1 -0
- package/dist/humain/web-access/vendor/perplexity.d.ts +20 -0
- package/dist/humain/web-access/vendor/perplexity.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/perplexity.js +159 -0
- package/dist/humain/web-access/vendor/perplexity.js.map +1 -0
- package/dist/humain/web-access/vendor/querit.d.ts +10 -0
- package/dist/humain/web-access/vendor/querit.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/querit.js +335 -0
- package/dist/humain/web-access/vendor/querit.js.map +1 -0
- package/dist/humain/web-access/vendor/render-search-error.d.ts +57 -0
- package/dist/humain/web-access/vendor/render-search-error.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/render-search-error.js +130 -0
- package/dist/humain/web-access/vendor/render-search-error.js.map +1 -0
- package/dist/humain/web-access/vendor/rsc-extract.d.ts +12 -0
- package/dist/humain/web-access/vendor/rsc-extract.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/rsc-extract.js +329 -0
- package/dist/humain/web-access/vendor/rsc-extract.js.map +1 -0
- package/dist/humain/web-access/vendor/search1api.d.ts +10 -0
- package/dist/humain/web-access/vendor/search1api.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/search1api.js +251 -0
- package/dist/humain/web-access/vendor/search1api.js.map +1 -0
- package/dist/humain/web-access/vendor/searchinfinity.d.ts +8 -0
- package/dist/humain/web-access/vendor/searchinfinity.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/searchinfinity.js +229 -0
- package/dist/humain/web-access/vendor/searchinfinity.js.map +1 -0
- package/dist/humain/web-access/vendor/searxng.d.ts +4 -0
- package/dist/humain/web-access/vendor/searxng.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/searxng.js +238 -0
- package/dist/humain/web-access/vendor/searxng.js.map +1 -0
- package/dist/humain/web-access/vendor/serpbase.d.ts +4 -0
- package/dist/humain/web-access/vendor/serpbase.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/serpbase.js +211 -0
- package/dist/humain/web-access/vendor/serpbase.js.map +1 -0
- package/dist/humain/web-access/vendor/serpdive.d.ts +8 -0
- package/dist/humain/web-access/vendor/serpdive.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/serpdive.js +267 -0
- package/dist/humain/web-access/vendor/serpdive.js.map +1 -0
- package/dist/humain/web-access/vendor/source-check.d.ts +99 -0
- package/dist/humain/web-access/vendor/source-check.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/source-check.js +205 -0
- package/dist/humain/web-access/vendor/source-check.js.map +1 -0
- package/dist/humain/web-access/vendor/ssrf-protection.d.ts +50 -0
- package/dist/humain/web-access/vendor/ssrf-protection.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/ssrf-protection.js +466 -0
- package/dist/humain/web-access/vendor/ssrf-protection.js.map +1 -0
- package/dist/humain/web-access/vendor/storage.d.ts +55 -0
- package/dist/humain/web-access/vendor/storage.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/storage.js +485 -0
- package/dist/humain/web-access/vendor/storage.js.map +1 -0
- package/dist/humain/web-access/vendor/summary-model-scope.d.ts +25 -0
- package/dist/humain/web-access/vendor/summary-model-scope.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/summary-model-scope.js +98 -0
- package/dist/humain/web-access/vendor/summary-model-scope.js.map +1 -0
- package/dist/humain/web-access/vendor/summary-review.d.ts +24 -0
- package/dist/humain/web-access/vendor/summary-review.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/summary-review.js +341 -0
- package/dist/humain/web-access/vendor/summary-review.js.map +1 -0
- package/dist/humain/web-access/vendor/tavily.d.ts +8 -0
- package/dist/humain/web-access/vendor/tavily.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/tavily.js +194 -0
- package/dist/humain/web-access/vendor/tavily.js.map +1 -0
- package/dist/humain/web-access/vendor/tinyfish.d.ts +11 -0
- package/dist/humain/web-access/vendor/tinyfish.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/tinyfish.js +331 -0
- package/dist/humain/web-access/vendor/tinyfish.js.map +1 -0
- package/dist/humain/web-access/vendor/utils.d.ts +20 -0
- package/dist/humain/web-access/vendor/utils.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/utils.js +91 -0
- package/dist/humain/web-access/vendor/utils.js.map +1 -0
- package/dist/humain/web-access/vendor/video-extract.d.ts +16 -0
- package/dist/humain/web-access/vendor/video-extract.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/video-extract.js +335 -0
- package/dist/humain/web-access/vendor/video-extract.js.map +1 -0
- package/dist/humain/web-access/vendor/xai-search.d.ts +13 -0
- package/dist/humain/web-access/vendor/xai-search.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/xai-search.js +331 -0
- package/dist/humain/web-access/vendor/xai-search.js.map +1 -0
- package/dist/humain/web-access/vendor/youtube-extract.d.ts +27 -0
- package/dist/humain/web-access/vendor/youtube-extract.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/youtube-extract.js +282 -0
- package/dist/humain/web-access/vendor/youtube-extract.js.map +1 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts +2 -2
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +169 -102
- package/dist/main.js.map +1 -1
- package/dist/modes/agent-exec-mode.d.ts +14 -0
- package/dist/modes/agent-exec-mode.d.ts.map +1 -0
- package/dist/modes/agent-exec-mode.js +92 -0
- package/dist/modes/agent-exec-mode.js.map +1 -0
- package/dist/modes/index.d.ts +4 -2
- package/dist/modes/index.d.ts.map +1 -1
- package/dist/modes/index.js +2 -1
- package/dist/modes/index.js.map +1 -1
- package/dist/modes/interactive/components/assistant-message.d.ts +5 -2
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/assistant-message.js +13 -4
- package/dist/modes/interactive/components/assistant-message.js.map +1 -1
- package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -1
- package/dist/modes/interactive/components/custom-editor.js +7 -0
- package/dist/modes/interactive/components/custom-editor.js.map +1 -1
- package/dist/modes/interactive/components/earendil-announcement.d.ts +5 -0
- package/dist/modes/interactive/components/earendil-announcement.d.ts.map +1 -0
- package/dist/modes/interactive/components/earendil-announcement.js +40 -0
- package/dist/modes/interactive/components/earendil-announcement.js.map +1 -0
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +1 -1
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/markdown-transform.d.ts +3 -0
- package/dist/modes/interactive/components/markdown-transform.d.ts.map +1 -0
- package/dist/modes/interactive/components/markdown-transform.js +19 -0
- package/dist/modes/interactive/components/markdown-transform.js.map +1 -0
- package/dist/modes/interactive/components/mermaid.d.ts +11 -0
- package/dist/modes/interactive/components/mermaid.d.ts.map +1 -0
- package/dist/modes/interactive/components/mermaid.js +75 -0
- package/dist/modes/interactive/components/mermaid.js.map +1 -0
- package/dist/modes/interactive/components/model-selector.d.ts +1 -1
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/model-selector.js +16 -4
- package/dist/modes/interactive/components/model-selector.js.map +1 -1
- package/dist/modes/interactive/components/scoped-models-selector.d.ts +4 -0
- package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/scoped-models-selector.js +26 -0
- package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts +8 -2
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +30 -0
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/components/user-message.d.ts +3 -1
- package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/user-message.js +9 -2
- package/dist/modes/interactive/components/user-message.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +45 -9
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +581 -211
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/dark.json +1 -0
- package/dist/modes/interactive/theme/light.json +1 -0
- package/dist/modes/interactive/theme/theme-controller.d.ts +3 -0
- package/dist/modes/interactive/theme/theme-controller.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme-controller.js +10 -1
- package/dist/modes/interactive/theme/theme-controller.js.map +1 -1
- package/dist/modes/interactive/theme/theme-schema.json +5 -1
- package/dist/modes/interactive/theme/theme.d.ts +2 -2
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +24 -6
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/json-event.d.ts +28 -0
- package/dist/modes/json-event.d.ts.map +1 -0
- package/dist/modes/json-event.js +12 -0
- package/dist/modes/json-event.js.map +1 -0
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +12 -2
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +12 -5
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +55 -1
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +13 -3
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +15 -0
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/dist/package-manager-cli.d.ts.map +1 -1
- package/dist/package-manager-cli.js +11 -9
- package/dist/package-manager-cli.js.map +1 -1
- package/dist/rpc-entry.d.ts.map +1 -1
- package/dist/rpc-entry.js +3 -2
- package/dist/rpc-entry.js.map +1 -1
- package/dist/server/create-harness.d.ts +26 -0
- package/dist/server/create-harness.d.ts.map +1 -0
- package/dist/server/create-harness.js +104 -0
- package/dist/server/create-harness.js.map +1 -0
- package/dist/utils/abort.d.ts +5 -0
- package/dist/utils/abort.d.ts.map +1 -0
- package/dist/utils/abort.js +48 -0
- package/dist/utils/abort.js.map +1 -0
- package/dist/utils/clipboard.d.ts +1 -1
- package/dist/utils/clipboard.d.ts.map +1 -1
- package/dist/utils/clipboard.js +22 -2
- package/dist/utils/clipboard.js.map +1 -1
- package/dist/utils/management-http.d.ts +23 -0
- package/dist/utils/management-http.d.ts.map +1 -0
- package/dist/utils/management-http.js +50 -0
- package/dist/utils/management-http.js.map +1 -0
- package/dist/utils/paths.d.ts +3 -0
- package/dist/utils/paths.d.ts.map +1 -1
- package/dist/utils/paths.js +23 -1
- package/dist/utils/paths.js.map +1 -1
- package/dist/utils/tool-result-images.d.ts +19 -0
- package/dist/utils/tool-result-images.d.ts.map +1 -0
- package/dist/utils/tool-result-images.js +45 -0
- package/dist/utils/tool-result-images.js.map +1 -0
- package/dist/utils/tools-manager.d.ts.map +1 -1
- package/dist/utils/tools-manager.js +13 -25
- package/dist/utils/tools-manager.js.map +1 -1
- package/dist/utils/version-check.d.ts +4 -0
- package/dist/utils/version-check.d.ts.map +1 -1
- package/dist/utils/version-check.js +23 -5
- package/dist/utils/version-check.js.map +1 -1
- package/docs/compaction.md +2 -2
- package/docs/custom-provider.md +7 -5
- package/docs/extensions.md +30 -5
- package/docs/humain-terminal.md +21 -1
- package/docs/json.md +24 -19
- package/docs/keybindings.md +46 -14
- package/docs/models.md +32 -11
- package/docs/providers.md +9 -1
- package/docs/quickstart.md +39 -37
- package/docs/rpc.md +31 -11
- package/docs/sdk.md +21 -2
- package/docs/security.md +21 -21
- package/docs/session-format.md +14 -14
- package/docs/settings.md +4 -1
- package/docs/themes.md +5 -3
- package/docs/usage.md +7 -0
- package/examples/extensions/custom-compaction.ts +3 -18
- package/examples/extensions/custom-provider-anthropic/index.ts +5 -1
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +5 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/doom-overlay/wad-finder.ts +7 -3
- package/examples/extensions/gondolin/package-lock.json +2 -2
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/handoff.ts +4 -13
- package/examples/extensions/qna.ts +5 -9
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/summarize.ts +9 -20
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/rpc-extension-ui.ts +11 -2
- package/examples/sdk/02-custom-model.ts +2 -3
- package/examples/sdk/09-api-keys-and-oauth.ts +1 -1
- package/examples/sdk/12-full-control.ts +1 -1
- package/examples/sdk/README.md +1 -1
- package/npm-shrinkwrap.json +2771 -535
- package/package.json +40 -11
- package/src/humain/mcp-adapter/UPSTREAM.md +15 -0
- package/src/humain/mcp-adapter/upstream.json +7 -0
- package/src/humain/mcp-adapter/vendor/CHANGELOG.md +660 -0
- package/src/humain/mcp-adapter/vendor/LICENSE +21 -0
- package/src/humain/web-access/UPSTREAM.md +17 -0
- package/src/humain/web-access/upstream.json +23 -0
- package/src/humain/web-access/vendor/LICENSE +21 -0
|
@@ -0,0 +1,1124 @@
|
|
|
1
|
+
import { mkdirSync } from "node:fs";
|
|
2
|
+
import {
|
|
3
|
+
Client,
|
|
4
|
+
SdkHttpError,
|
|
5
|
+
SSEClientTransport,
|
|
6
|
+
StreamableHTTPClientTransport,
|
|
7
|
+
UnauthorizedError,
|
|
8
|
+
type GetPromptResult,
|
|
9
|
+
type ReadResourceResult,
|
|
10
|
+
type RequestOptions,
|
|
11
|
+
type UrlElicitationRequiredError,
|
|
12
|
+
type VersionNegotiationOptions,
|
|
13
|
+
} from "@modelcontextprotocol/client";
|
|
14
|
+
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
|
15
|
+
import { UnixSocketClientTransport } from "./unix-socket-transport.ts";
|
|
16
|
+
import { probeMcpEndpoint } from "./mcp-probe.ts";
|
|
17
|
+
import {
|
|
18
|
+
isServerDisabled,
|
|
19
|
+
type McpTool,
|
|
20
|
+
type McpResource,
|
|
21
|
+
type McpPrompt,
|
|
22
|
+
type ServerDefinition,
|
|
23
|
+
type ServerStreamResultPatchNotification,
|
|
24
|
+
type Transport,
|
|
25
|
+
type McpTraceSettings,
|
|
26
|
+
SERVER_STREAM_RESULT_PATCH_METHOD,
|
|
27
|
+
serverStreamResultPatchNotificationSchema,
|
|
28
|
+
} from "./types.ts";
|
|
29
|
+
import { resolveNpxBinary } from "./npx-resolver.ts";
|
|
30
|
+
import { createJsonSchemaValidator } from "./json-schema-validator.ts";
|
|
31
|
+
import { logger } from "./logger.ts";
|
|
32
|
+
import { McpOAuthProvider } from "./mcp-oauth-provider.ts";
|
|
33
|
+
import { extractOAuthConfig, supportsOAuth, type McpOAuthRuntime } from "./mcp-auth-flow.ts";
|
|
34
|
+
import { invalidateAuthEntryCache, type AuthStorageOptions } from "./mcp-auth.ts";
|
|
35
|
+
import { registerSamplingHandler, type ServerSamplingConfig } from "./sampling-handler.ts";
|
|
36
|
+
import {
|
|
37
|
+
handleUrlElicitation,
|
|
38
|
+
registerElicitationHandler,
|
|
39
|
+
type ServerElicitationConfig,
|
|
40
|
+
} from "./elicitation-handler.ts";
|
|
41
|
+
import {
|
|
42
|
+
interpolateEnvVars,
|
|
43
|
+
resolveBearerToken,
|
|
44
|
+
resolveCommandSecret,
|
|
45
|
+
resolveCommandSecretsRecord,
|
|
46
|
+
resolveConfigPath,
|
|
47
|
+
resolveServerUrl,
|
|
48
|
+
} from "./utils.ts";
|
|
49
|
+
import { abortable, throwIfAborted } from "./abort.ts";
|
|
50
|
+
import { combineAbortSignals } from "./runtime-owner.ts";
|
|
51
|
+
import {
|
|
52
|
+
createMcpTraceWriter,
|
|
53
|
+
isMcpTraceEnabled,
|
|
54
|
+
McpTraceWriter,
|
|
55
|
+
type McpTraceObserver,
|
|
56
|
+
traceTransportKind,
|
|
57
|
+
wrapTransportWithMcpTrace,
|
|
58
|
+
} from "./mcp-trace.ts";
|
|
59
|
+
|
|
60
|
+
const MAX_CAPTURED_STDERR_BYTES = 8 * 1024;
|
|
61
|
+
const MAX_CAPTURED_STDERR_LINES = 3;
|
|
62
|
+
const abortCleanupPromises = new WeakMap<object, Promise<void>>();
|
|
63
|
+
|
|
64
|
+
type HttpAuthProviderState =
|
|
65
|
+
| { status: "disabled" }
|
|
66
|
+
| { status: "implicit-deferred" }
|
|
67
|
+
| { status: "explicit"; provider: McpOAuthProvider }
|
|
68
|
+
| { status: "implicit-challenged"; provider: McpOAuthProvider };
|
|
69
|
+
|
|
70
|
+
function isUnauthorizedHttpError(error: unknown): boolean {
|
|
71
|
+
return error instanceof UnauthorizedError || (error instanceof SdkHttpError && error.status === 401);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function shouldFallbackToSse(error: unknown, definition: ServerDefinition): boolean {
|
|
75
|
+
if (definition.protocolVersion === "2026-07-28") return false;
|
|
76
|
+
return error instanceof SdkHttpError && [404, 405, 406, 415].includes(error.status);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function resolveVersionNegotiation(definition: ServerDefinition): VersionNegotiationOptions | undefined {
|
|
80
|
+
switch (definition.protocolVersion) {
|
|
81
|
+
case undefined:
|
|
82
|
+
case "legacy":
|
|
83
|
+
return undefined;
|
|
84
|
+
case "auto":
|
|
85
|
+
return { mode: "auto" };
|
|
86
|
+
case "2026-07-28":
|
|
87
|
+
return { mode: { pin: "2026-07-28" } };
|
|
88
|
+
default:
|
|
89
|
+
throw new Error(`Invalid MCP protocolVersion: ${String(definition.protocolVersion)}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function boundedStderrChunk(chunk: Buffer | string): Buffer {
|
|
94
|
+
if (Buffer.isBuffer(chunk)) {
|
|
95
|
+
const start = Math.max(0, chunk.byteLength - MAX_CAPTURED_STDERR_BYTES);
|
|
96
|
+
return Buffer.from(chunk.subarray(start));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Limit string conversion before encoding; Buffer.from(largeString) would
|
|
100
|
+
// otherwise allocate the entire stderr event before applying the cap.
|
|
101
|
+
const suffix = chunk.length > MAX_CAPTURED_STDERR_BYTES
|
|
102
|
+
? chunk.slice(-MAX_CAPTURED_STDERR_BYTES)
|
|
103
|
+
: chunk;
|
|
104
|
+
const bytes = Buffer.from(suffix, "utf8");
|
|
105
|
+
return bytes.byteLength > MAX_CAPTURED_STDERR_BYTES
|
|
106
|
+
? Buffer.from(bytes.subarray(bytes.byteLength - MAX_CAPTURED_STDERR_BYTES))
|
|
107
|
+
: bytes;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function appendStderrTail(tail: Buffer, chunk: Buffer | string): Buffer {
|
|
111
|
+
const bytes = boundedStderrChunk(chunk);
|
|
112
|
+
if (bytes.length === 0) return tail;
|
|
113
|
+
if (tail.length === 0) return bytes;
|
|
114
|
+
const combined = Buffer.concat([tail, bytes]);
|
|
115
|
+
return combined.length > MAX_CAPTURED_STDERR_BYTES
|
|
116
|
+
? Buffer.from(combined.subarray(combined.length - MAX_CAPTURED_STDERR_BYTES))
|
|
117
|
+
: combined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface ServerConnection {
|
|
121
|
+
client: Client;
|
|
122
|
+
transport: Transport;
|
|
123
|
+
definition: ServerDefinition;
|
|
124
|
+
tools: McpTool[];
|
|
125
|
+
resources: McpResource[];
|
|
126
|
+
prompts: McpPrompt[];
|
|
127
|
+
/** True when prompts were advertised but prompts/list failed. */
|
|
128
|
+
promptDiscoveryFailed?: boolean;
|
|
129
|
+
instructions?: string;
|
|
130
|
+
lastUsedAt: number;
|
|
131
|
+
inFlight: number;
|
|
132
|
+
status: "connected" | "closed" | "needs-auth";
|
|
133
|
+
/** True once this needs-auth episode discarded the cached credential. */
|
|
134
|
+
credentialsInvalidated?: boolean;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
type UiStreamListener = (serverName: string, notification: ServerStreamResultPatchNotification["params"]) => void;
|
|
139
|
+
type MetadataListChangedListener = (serverName: string, reason: string) => void;
|
|
140
|
+
|
|
141
|
+
export class McpServerManager {
|
|
142
|
+
private connections = new Map<string, ServerConnection>();
|
|
143
|
+
private connectPromises = new Map<string, Promise<ServerConnection>>();
|
|
144
|
+
private reconnectPromises = new Map<string, Promise<ServerConnection>>();
|
|
145
|
+
private uiStreamListeners = new Map<string, UiStreamListener>();
|
|
146
|
+
private samplingConfig: ServerSamplingConfig | undefined;
|
|
147
|
+
private metadataListChangedListener: MetadataListChangedListener | undefined;
|
|
148
|
+
private elicitationConfig: ServerElicitationConfig | undefined;
|
|
149
|
+
private authStorageOptions: AuthStorageOptions = {};
|
|
150
|
+
private oauthRuntime: McpOAuthRuntime | undefined;
|
|
151
|
+
private acceptedUrlElicitations = new Map<string, Set<string>>();
|
|
152
|
+
private defaultRequestTimeoutMs: number | undefined;
|
|
153
|
+
private runtimeSignal: AbortSignal | undefined;
|
|
154
|
+
private closePromises = new Map<string, Promise<void>>();
|
|
155
|
+
private closeGenerations = new Map<string, number>();
|
|
156
|
+
private connectAttempts = new Map<string, AbortController>();
|
|
157
|
+
private traceSettings: McpTraceSettings | undefined;
|
|
158
|
+
private traceWriter: McpTraceWriter | undefined;
|
|
159
|
+
private stopped = false;
|
|
160
|
+
|
|
161
|
+
/** Default cwd for stdio servers without an explicit config `cwd`. */
|
|
162
|
+
constructor(private readonly defaultCwd?: string) {}
|
|
163
|
+
|
|
164
|
+
setSamplingConfig(config: ServerSamplingConfig | undefined): void {
|
|
165
|
+
this.samplingConfig = config;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
setMetadataListChangedListener(listener: MetadataListChangedListener | undefined): void {
|
|
169
|
+
this.metadataListChangedListener = listener;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
setElicitationConfig(config: ServerElicitationConfig | undefined): void {
|
|
173
|
+
this.elicitationConfig = config;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
setRuntimeSignal(signal: AbortSignal | undefined): void {
|
|
177
|
+
this.runtimeSignal = signal;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
setDefaultRequestTimeoutMs(timeoutMs: number | undefined): void {
|
|
181
|
+
this.defaultRequestTimeoutMs = normalizeRequestTimeoutMs(timeoutMs);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
setTraceConfig(settings: McpTraceSettings | undefined): void {
|
|
185
|
+
this.traceSettings = settings;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
setAuthStorageOptions(options: AuthStorageOptions): void {
|
|
189
|
+
this.authStorageOptions = options;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
setOAuthRuntime(runtime: McpOAuthRuntime): void {
|
|
193
|
+
this.oauthRuntime = runtime;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
getRequestOptions(name: string, signal?: AbortSignal): RequestOptions | undefined {
|
|
197
|
+
const connection = this.connections.get(name);
|
|
198
|
+
return this.buildRequestOptions(connection?.definition, signal);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private getResolvedRequestTimeoutMs(definition?: ServerDefinition): number | undefined {
|
|
202
|
+
if (definition?.requestTimeoutMs !== undefined) {
|
|
203
|
+
return normalizeRequestTimeoutMs(definition.requestTimeoutMs);
|
|
204
|
+
}
|
|
205
|
+
return this.defaultRequestTimeoutMs;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private buildRequestOptions(
|
|
209
|
+
definition?: ServerDefinition,
|
|
210
|
+
signal?: AbortSignal,
|
|
211
|
+
): RequestOptions | undefined {
|
|
212
|
+
const timeout = this.getResolvedRequestTimeoutMs(definition);
|
|
213
|
+
const ownedSignal = combineAbortSignals(this.runtimeSignal, signal);
|
|
214
|
+
|
|
215
|
+
if (!ownedSignal && timeout === undefined) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
...(ownedSignal ? { signal: ownedSignal } : {}),
|
|
221
|
+
...(timeout !== undefined ? { timeout } : {}),
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async connect(name: string, definition: ServerDefinition, signal?: AbortSignal): Promise<ServerConnection> {
|
|
226
|
+
if (isServerDisabled(definition)) throw new Error(`MCP server "${name}" is disabled`);
|
|
227
|
+
if (this.stopped) throw new Error("MCP server manager is closed");
|
|
228
|
+
const ownedSignal = combineAbortSignals(this.runtimeSignal, signal);
|
|
229
|
+
throwIfAborted(ownedSignal);
|
|
230
|
+
const closing = this.closePromises.get(name);
|
|
231
|
+
if (closing) await abortable(closing, ownedSignal);
|
|
232
|
+
throwIfAborted(ownedSignal);
|
|
233
|
+
|
|
234
|
+
// Dedupe concurrent connection attempts.
|
|
235
|
+
if (this.connectPromises.has(name)) {
|
|
236
|
+
return abortable(this.connectPromises.get(name)!, ownedSignal);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const existing = this.connections.get(name);
|
|
240
|
+
if (existing?.status === "connected") {
|
|
241
|
+
existing.lastUsedAt = Date.now();
|
|
242
|
+
return existing;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const credentialsInvalidated = existing?.status === "needs-auth"
|
|
246
|
+
&& existing.credentialsInvalidated === true;
|
|
247
|
+
const generation = this.closeGenerations.get(name) ?? 0;
|
|
248
|
+
const attemptController = new AbortController();
|
|
249
|
+
const attemptSignal = combineAbortSignals(ownedSignal, attemptController.signal);
|
|
250
|
+
const connectionAttempt = this.createConnection(name, definition, attemptSignal, ownedSignal, credentialsInvalidated);
|
|
251
|
+
const promise = definition.url
|
|
252
|
+
? connectionAttempt.catch(async error => { throw await this.enrichHttpConnectionError(definition, error); })
|
|
253
|
+
: connectionAttempt;
|
|
254
|
+
this.connectPromises.set(name, promise);
|
|
255
|
+
this.connectAttempts.set(name, attemptController);
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
const connection = await promise;
|
|
259
|
+
if (attemptController.signal.aborted || (this.closeGenerations.get(name) ?? 0) !== generation) {
|
|
260
|
+
await this.disposeConnection(connection);
|
|
261
|
+
throwIfAborted(attemptSignal);
|
|
262
|
+
throw new Error(`MCP connection for ${name} was closed while connecting`);
|
|
263
|
+
}
|
|
264
|
+
this.connections.set(name, connection);
|
|
265
|
+
return connection;
|
|
266
|
+
} finally {
|
|
267
|
+
if (this.connectPromises.get(name) === promise) this.connectPromises.delete(name);
|
|
268
|
+
if (this.connectAttempts.get(name) === attemptController) this.connectAttempts.delete(name);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Reconnect a server whose connection was proven stale (e.g. by a 404
|
|
274
|
+
* "session no longer exists" response). Single-flight per server name —
|
|
275
|
+
* concurrent callers that raced to the same failure share one reconnect —
|
|
276
|
+
* and identity-guarded: `staleConnection` is only torn down if it is
|
|
277
|
+
* still the manager's current connection for `name`. If a concurrent
|
|
278
|
+
* reconnect (or an unrelated connect()) already replaced it with a fresh
|
|
279
|
+
* connection, that fresh connection is returned untouched.
|
|
280
|
+
*/
|
|
281
|
+
async reconnect(
|
|
282
|
+
name: string,
|
|
283
|
+
definition: ServerDefinition,
|
|
284
|
+
staleConnection: ServerConnection,
|
|
285
|
+
signal?: AbortSignal,
|
|
286
|
+
): Promise<ServerConnection> {
|
|
287
|
+
if (isServerDisabled(definition)) throw new Error(`MCP server "${name}" is disabled`);
|
|
288
|
+
if (this.stopped) throw new Error("MCP server manager is closed");
|
|
289
|
+
const ownedSignal = combineAbortSignals(this.runtimeSignal, signal);
|
|
290
|
+
throwIfAborted(ownedSignal);
|
|
291
|
+
const inFlight = this.reconnectPromises.get(name);
|
|
292
|
+
if (inFlight) {
|
|
293
|
+
return abortable(inFlight, ownedSignal);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const promise = this.doReconnect(name, definition, staleConnection, ownedSignal).finally(() => {
|
|
297
|
+
if (this.reconnectPromises.get(name) === promise) {
|
|
298
|
+
this.reconnectPromises.delete(name);
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
this.reconnectPromises.set(name, promise);
|
|
302
|
+
return abortable(promise, ownedSignal);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private async doReconnect(
|
|
306
|
+
name: string,
|
|
307
|
+
definition: ServerDefinition,
|
|
308
|
+
staleConnection: ServerConnection,
|
|
309
|
+
signal?: AbortSignal,
|
|
310
|
+
): Promise<ServerConnection> {
|
|
311
|
+
throwIfAborted(signal);
|
|
312
|
+
const current = this.connections.get(name);
|
|
313
|
+
|
|
314
|
+
// Never tear down a connection we didn't prove stale: if the map no
|
|
315
|
+
// longer holds the connection we were asked to replace, someone else
|
|
316
|
+
// already reconnected (or connected) first.
|
|
317
|
+
if (current !== staleConnection) {
|
|
318
|
+
return current ?? this.connect(name, definition, signal);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const staleInFlight = staleConnection.inFlight;
|
|
322
|
+
await this.close(name);
|
|
323
|
+
const fresh = await this.connect(name, definition, signal);
|
|
324
|
+
fresh.inFlight = Math.max(fresh.inFlight, staleInFlight);
|
|
325
|
+
return fresh;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private async createConnection(
|
|
329
|
+
name: string,
|
|
330
|
+
definition: ServerDefinition,
|
|
331
|
+
signal?: AbortSignal,
|
|
332
|
+
requestSignal?: AbortSignal,
|
|
333
|
+
credentialsInvalidated = false,
|
|
334
|
+
): Promise<ServerConnection> {
|
|
335
|
+
throwIfAborted(signal);
|
|
336
|
+
|
|
337
|
+
const tracingEnabled = isMcpTraceEnabled(definition, this.traceSettings);
|
|
338
|
+
const traceWriter = tracingEnabled
|
|
339
|
+
? (this.traceWriter ??= createMcpTraceWriter(this.defaultCwd, this.traceSettings ?? {}))
|
|
340
|
+
: undefined;
|
|
341
|
+
const traceObserver: McpTraceObserver | undefined = traceWriter
|
|
342
|
+
? { record: event => traceWriter.write(event) }
|
|
343
|
+
: undefined;
|
|
344
|
+
|
|
345
|
+
let client: Client;
|
|
346
|
+
let transport: Transport;
|
|
347
|
+
let clientConnected = false;
|
|
348
|
+
let invalidated = credentialsInvalidated;
|
|
349
|
+
let transportAlreadyTraced = false;
|
|
350
|
+
let stderrTail: Buffer<ArrayBufferLike> = Buffer.alloc(0);
|
|
351
|
+
const configuredTransports = [definition.command, definition.url, definition.socket]
|
|
352
|
+
.filter(value => typeof value === "string" && value.length > 0);
|
|
353
|
+
if (configuredTransports.length !== 1) {
|
|
354
|
+
throw new Error(`Server ${name} must configure exactly one of command, url, or socket`);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const requestOptions = this.buildRequestOptions(definition, requestSignal);
|
|
358
|
+
|
|
359
|
+
if (definition.command) {
|
|
360
|
+
client = this.createClient(name, definition);
|
|
361
|
+
let command = definition.command;
|
|
362
|
+
let args = (definition.args ?? []).map(interpolateEnvVars);
|
|
363
|
+
|
|
364
|
+
if (command === "npx" || command === "npm") {
|
|
365
|
+
const resolved = await resolveNpxBinary(command, args, signal);
|
|
366
|
+
if (resolved) {
|
|
367
|
+
command = resolved.isJs ? "node" : resolved.binPath;
|
|
368
|
+
args = resolved.isJs ? [resolved.binPath, ...resolved.extraArgs] : resolved.extraArgs;
|
|
369
|
+
logger.debug(`${name} resolved to ${resolved.binPath} (skipping npm parent)`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
throwIfAborted(signal);
|
|
373
|
+
|
|
374
|
+
if (definition.pluginDataDir) mkdirSync(definition.pluginDataDir, { recursive: true });
|
|
375
|
+
const cwd = resolveConfigPath(definition.cwd) ?? this.defaultCwd;
|
|
376
|
+
const stdioTransport = new StdioClientTransport({
|
|
377
|
+
command,
|
|
378
|
+
args,
|
|
379
|
+
env: resolveEnv(definition.env, name, definition.literalEnv === true),
|
|
380
|
+
...(cwd !== undefined ? { cwd } : {}),
|
|
381
|
+
stderr: definition.debug ? "inherit" : "pipe",
|
|
382
|
+
});
|
|
383
|
+
// Keep non-debug child diagnostics available for connection failures without
|
|
384
|
+
// retaining an unbounded stream or changing the existing debug behavior.
|
|
385
|
+
if (stdioTransport.stderr) {
|
|
386
|
+
stdioTransport.stderr.on("data", (chunk: Buffer | string) => {
|
|
387
|
+
stderrTail = appendStderrTail(stderrTail, chunk);
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
transport = stdioTransport;
|
|
391
|
+
} else if (definition.url) {
|
|
392
|
+
const httpConnection = await this.connectHttpClient(
|
|
393
|
+
definition,
|
|
394
|
+
name,
|
|
395
|
+
requestOptions,
|
|
396
|
+
signal,
|
|
397
|
+
traceObserver,
|
|
398
|
+
invalidated,
|
|
399
|
+
);
|
|
400
|
+
client = httpConnection.client;
|
|
401
|
+
transport = httpConnection.transport;
|
|
402
|
+
invalidated = httpConnection.credentialsInvalidated;
|
|
403
|
+
if (httpConnection.status === "needs-auth") {
|
|
404
|
+
return {
|
|
405
|
+
client,
|
|
406
|
+
transport,
|
|
407
|
+
definition,
|
|
408
|
+
tools: [],
|
|
409
|
+
resources: [],
|
|
410
|
+
prompts: [],
|
|
411
|
+
lastUsedAt: Date.now(),
|
|
412
|
+
inFlight: 0,
|
|
413
|
+
status: "needs-auth",
|
|
414
|
+
credentialsInvalidated: invalidated,
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
clientConnected = true;
|
|
418
|
+
transportAlreadyTraced = traceObserver !== undefined;
|
|
419
|
+
} else {
|
|
420
|
+
client = this.createClient(name, definition);
|
|
421
|
+
transport = new UnixSocketClientTransport(resolveConfigPath(definition.socket!)!);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (traceObserver && !transportAlreadyTraced) {
|
|
425
|
+
const traceTransportKindValue = traceTransportKind(definition, transport);
|
|
426
|
+
transport = wrapTransportWithMcpTrace(transport, name, traceTransportKindValue, traceObserver);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
try {
|
|
430
|
+
throwIfAborted(signal);
|
|
431
|
+
if (!clientConnected) {
|
|
432
|
+
await this.connectClientWithAbort(client, transport, requestOptions, signal);
|
|
433
|
+
}
|
|
434
|
+
this.attachAdapterNotificationHandlers(name, client);
|
|
435
|
+
|
|
436
|
+
const instructions = client.getInstructions?.();
|
|
437
|
+
const connection: ServerConnection = {
|
|
438
|
+
client,
|
|
439
|
+
transport,
|
|
440
|
+
definition,
|
|
441
|
+
tools: [],
|
|
442
|
+
resources: [],
|
|
443
|
+
prompts: [],
|
|
444
|
+
...(instructions !== undefined ? { instructions } : {}),
|
|
445
|
+
lastUsedAt: Date.now(),
|
|
446
|
+
inFlight: 0,
|
|
447
|
+
status: "connected",
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
// Reflect the SDK's own close signal in connection status, guarded by
|
|
451
|
+
// identity so a stale connection's late close can never clobber a fresh
|
|
452
|
+
// connection. The SDK client owns the transport callbacks.
|
|
453
|
+
client.onclose = () => {
|
|
454
|
+
if (this.connections.get(name) === connection) {
|
|
455
|
+
connection.status = "closed";
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
// Discover tools, resources, and prompts. Resource and prompt listing is
|
|
460
|
+
// optional: only servers advertising the capability are queried.
|
|
461
|
+
const [tools, resources, promptResult] = await Promise.all([
|
|
462
|
+
this.fetchAllTools(client, requestOptions),
|
|
463
|
+
this.fetchAllResources(client, requestOptions),
|
|
464
|
+
this.fetchAllPrompts(client, requestOptions),
|
|
465
|
+
]);
|
|
466
|
+
connection.tools = tools;
|
|
467
|
+
connection.resources = resources;
|
|
468
|
+
connection.prompts = promptResult.prompts;
|
|
469
|
+
connection.promptDiscoveryFailed = promptResult.failed;
|
|
470
|
+
|
|
471
|
+
return connection;
|
|
472
|
+
} catch (error) {
|
|
473
|
+
// If connectClientWithAbort closed the transport, await that exact close.
|
|
474
|
+
// Otherwise the SDK client owns its transport and performs cleanup once.
|
|
475
|
+
const abortCleanup = abortCleanupPromises.get(transport);
|
|
476
|
+
const abortCleanupFailed = error instanceof AggregateError && error.message === "MCP connection abort cleanup failed";
|
|
477
|
+
const cleanupResults = abortCleanupFailed
|
|
478
|
+
? []
|
|
479
|
+
: await Promise.allSettled([
|
|
480
|
+
abortCleanup ?? Promise.resolve().then(() => client.close()),
|
|
481
|
+
]);
|
|
482
|
+
const cleanupFailures = cleanupResults.flatMap(result => result.status === "rejected" ? [result.reason] : []);
|
|
483
|
+
let reportedError: unknown = error;
|
|
484
|
+
if (cleanupFailures.length > 0) {
|
|
485
|
+
reportedError = new AggregateError([error, ...cleanupFailures], "MCP connection setup failed");
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// A cleanup failure remains a setup failure rather than being hidden
|
|
489
|
+
// behind needs-auth.
|
|
490
|
+
if (isUnauthorizedHttpError(error) && supportsOAuth(definition) && cleanupFailures.length === 0) {
|
|
491
|
+
if (!invalidated) {
|
|
492
|
+
invalidateAuthEntryCache(name);
|
|
493
|
+
invalidated = true;
|
|
494
|
+
}
|
|
495
|
+
return {
|
|
496
|
+
client,
|
|
497
|
+
transport,
|
|
498
|
+
definition,
|
|
499
|
+
tools: [],
|
|
500
|
+
resources: [],
|
|
501
|
+
prompts: [],
|
|
502
|
+
lastUsedAt: Date.now(),
|
|
503
|
+
inFlight: 0,
|
|
504
|
+
status: "needs-auth",
|
|
505
|
+
credentialsInvalidated: invalidated,
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (stderrTail.length > 0) {
|
|
510
|
+
const stderrText = stderrTail.toString("utf8").trim();
|
|
511
|
+
const lines = stderrText.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
|
|
512
|
+
if (lines.length > 0) {
|
|
513
|
+
const baseMessage = reportedError instanceof Error ? reportedError.message : String(reportedError);
|
|
514
|
+
const detail = lines.slice(-MAX_CAPTURED_STDERR_LINES).join(" — ");
|
|
515
|
+
throw new Error(`${baseMessage} (${detail})`, { cause: reportedError });
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
throw reportedError;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
private async enrichHttpConnectionError(definition: ServerDefinition, error: unknown): Promise<Error> {
|
|
523
|
+
const originalMessage = error instanceof Error ? error.message : String(error);
|
|
524
|
+
try {
|
|
525
|
+
const probe = await probeMcpEndpoint(resolveServerUrl(definition)!);
|
|
526
|
+
return new Error(`${originalMessage} — probe: ${probe.classification}`, { cause: error });
|
|
527
|
+
} catch {
|
|
528
|
+
return error instanceof Error ? error : new Error(originalMessage);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
private async connectClientWithAbort(
|
|
533
|
+
client: Client,
|
|
534
|
+
transport: Transport,
|
|
535
|
+
requestOptions?: RequestOptions,
|
|
536
|
+
signal?: AbortSignal,
|
|
537
|
+
): Promise<void> {
|
|
538
|
+
throwIfAborted(signal);
|
|
539
|
+
let abortCleanup: Promise<void> | undefined;
|
|
540
|
+
const closeTransport = () => {
|
|
541
|
+
abortCleanup = Promise.resolve().then(() => transport.close());
|
|
542
|
+
abortCleanupPromises.set(transport, abortCleanup);
|
|
543
|
+
};
|
|
544
|
+
signal?.addEventListener("abort", closeTransport, { once: true });
|
|
545
|
+
try {
|
|
546
|
+
await abortable(client.connect(transport, requestOptions), signal);
|
|
547
|
+
await abortCleanup;
|
|
548
|
+
} catch (error) {
|
|
549
|
+
if (abortCleanup) {
|
|
550
|
+
try {
|
|
551
|
+
await abortCleanup;
|
|
552
|
+
} catch (cleanupError) {
|
|
553
|
+
throw new AggregateError([error, cleanupError], "MCP connection abort cleanup failed");
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
throw error;
|
|
557
|
+
} finally {
|
|
558
|
+
signal?.removeEventListener("abort", closeTransport);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
private buildClientCapabilities() {
|
|
563
|
+
return {
|
|
564
|
+
...(this.samplingConfig ? { sampling: {} } : {}),
|
|
565
|
+
...(this.elicitationConfig
|
|
566
|
+
? {
|
|
567
|
+
elicitation: {
|
|
568
|
+
form: {},
|
|
569
|
+
...(this.elicitationConfig.allowUrl ? { url: {} } : {}),
|
|
570
|
+
},
|
|
571
|
+
}
|
|
572
|
+
: {}),
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
private createClient(serverName: string, definition: ServerDefinition): Client {
|
|
577
|
+
const capabilities = this.buildClientCapabilities();
|
|
578
|
+
const versionNegotiation = resolveVersionNegotiation(definition);
|
|
579
|
+
let client: Client;
|
|
580
|
+
client = new Client(
|
|
581
|
+
{ name: `pi-mcp-${serverName}`, version: "1.0.0" },
|
|
582
|
+
{
|
|
583
|
+
jsonSchemaValidator: createJsonSchemaValidator(),
|
|
584
|
+
...(versionNegotiation ? { versionNegotiation } : {}),
|
|
585
|
+
...(Object.keys(capabilities).length > 0 ? { capabilities } : {}),
|
|
586
|
+
listChanged: {
|
|
587
|
+
tools: {
|
|
588
|
+
onChanged: (error: Error | null, tools: McpTool[] | null) => {
|
|
589
|
+
this.handleToolsListChanged(serverName, client, error, tools);
|
|
590
|
+
},
|
|
591
|
+
},
|
|
592
|
+
resources: {
|
|
593
|
+
onChanged: (error: Error | null, resources: McpResource[] | null) => {
|
|
594
|
+
this.handleResourcesListChanged(serverName, client, error, resources);
|
|
595
|
+
},
|
|
596
|
+
},
|
|
597
|
+
prompts: {
|
|
598
|
+
onChanged: (error: Error | null, prompts: McpPrompt[] | null) => {
|
|
599
|
+
this.handlePromptsListChanged(serverName, client, error, prompts);
|
|
600
|
+
},
|
|
601
|
+
},
|
|
602
|
+
},
|
|
603
|
+
},
|
|
604
|
+
);
|
|
605
|
+
if (this.samplingConfig) {
|
|
606
|
+
registerSamplingHandler(client, { ...this.samplingConfig, serverName });
|
|
607
|
+
}
|
|
608
|
+
if (this.elicitationConfig) {
|
|
609
|
+
registerElicitationHandler(client, {
|
|
610
|
+
...this.elicitationConfig,
|
|
611
|
+
serverName,
|
|
612
|
+
onUrlAccepted: elicitationId => this.rememberUrlElicitation(serverName, elicitationId),
|
|
613
|
+
});
|
|
614
|
+
if (this.elicitationConfig.allowUrl) {
|
|
615
|
+
client.setNotificationHandler("notifications/elicitation/complete", notification => {
|
|
616
|
+
if (this.runtimeSignal?.aborted) return;
|
|
617
|
+
const accepted = this.acceptedUrlElicitations.get(serverName);
|
|
618
|
+
if (!accepted?.delete(notification.params.elicitationId)) return;
|
|
619
|
+
this.elicitationConfig?.ui.notify(
|
|
620
|
+
`MCP browser interaction for ${serverName} completed. You can retry the tool now.`,
|
|
621
|
+
"info",
|
|
622
|
+
);
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
return client;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
private handleToolsListChanged(
|
|
630
|
+
serverName: string,
|
|
631
|
+
client: Client,
|
|
632
|
+
error: Error | null,
|
|
633
|
+
tools: McpTool[] | null,
|
|
634
|
+
): void {
|
|
635
|
+
if (error) {
|
|
636
|
+
logger.debug(`MCP: tools/list_changed refresh failed for ${serverName}: ${error.message}`);
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
if (!tools) return;
|
|
640
|
+
const connection = this.connections.get(serverName);
|
|
641
|
+
if (!connection || connection.client !== client || connection.status !== "connected") return;
|
|
642
|
+
connection.tools = tools;
|
|
643
|
+
this.metadataListChangedListener?.(serverName, "tools-list-changed");
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
private handlePromptsListChanged(
|
|
647
|
+
serverName: string,
|
|
648
|
+
client: Client,
|
|
649
|
+
error: Error | null,
|
|
650
|
+
prompts: McpPrompt[] | null,
|
|
651
|
+
): void {
|
|
652
|
+
if (error) {
|
|
653
|
+
logger.debug(`MCP: prompts/list_changed refresh failed for ${serverName}: ${error.message}`);
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
if (!prompts) return;
|
|
657
|
+
const connection = this.connections.get(serverName);
|
|
658
|
+
if (!connection || connection.client !== client || connection.status !== "connected") return;
|
|
659
|
+
connection.prompts = prompts;
|
|
660
|
+
connection.promptDiscoveryFailed = false;
|
|
661
|
+
this.metadataListChangedListener?.(serverName, "prompts-list-changed");
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
private handleResourcesListChanged(
|
|
665
|
+
serverName: string,
|
|
666
|
+
client: Client,
|
|
667
|
+
error: Error | null,
|
|
668
|
+
resources: McpResource[] | null,
|
|
669
|
+
): void {
|
|
670
|
+
if (error) {
|
|
671
|
+
logger.debug(`MCP: resources/list_changed refresh failed for ${serverName}: ${error.message}`);
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
if (!resources) return;
|
|
675
|
+
const connection = this.connections.get(serverName);
|
|
676
|
+
if (!connection || connection.client !== client || connection.status !== "connected") return;
|
|
677
|
+
connection.resources = resources;
|
|
678
|
+
this.metadataListChangedListener?.(serverName, "resources-list-changed");
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
async handleUrlElicitationRequired(
|
|
682
|
+
serverName: string,
|
|
683
|
+
error: UrlElicitationRequiredError,
|
|
684
|
+
): Promise<"accept" | "decline" | "cancel"> {
|
|
685
|
+
if (this.runtimeSignal?.aborted || !this.elicitationConfig?.allowUrl) return "cancel";
|
|
686
|
+
for (const params of error.elicitations) {
|
|
687
|
+
const result = await handleUrlElicitation({
|
|
688
|
+
...this.elicitationConfig,
|
|
689
|
+
serverName,
|
|
690
|
+
onUrlAccepted: elicitationId => this.rememberUrlElicitation(serverName, elicitationId),
|
|
691
|
+
}, params);
|
|
692
|
+
if (result.action !== "accept") return result.action;
|
|
693
|
+
}
|
|
694
|
+
return "accept";
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
private rememberUrlElicitation(serverName: string, elicitationId: string): void {
|
|
698
|
+
if (this.runtimeSignal?.aborted) return;
|
|
699
|
+
let accepted = this.acceptedUrlElicitations.get(serverName);
|
|
700
|
+
if (!accepted) {
|
|
701
|
+
accepted = new Set();
|
|
702
|
+
this.acceptedUrlElicitations.set(serverName, accepted);
|
|
703
|
+
}
|
|
704
|
+
accepted.add(elicitationId);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
private async connectHttpClient(
|
|
708
|
+
definition: ServerDefinition,
|
|
709
|
+
serverName: string,
|
|
710
|
+
requestOptions: RequestOptions | undefined,
|
|
711
|
+
signal?: AbortSignal,
|
|
712
|
+
traceObserver?: McpTraceObserver,
|
|
713
|
+
credentialsInvalidated = false,
|
|
714
|
+
): Promise<{ client: Client; transport: Transport; status: "connected" | "needs-auth"; credentialsInvalidated: boolean }> {
|
|
715
|
+
throwIfAborted(signal);
|
|
716
|
+
const serverUrl = resolveServerUrl(definition)!;
|
|
717
|
+
const url = new URL(serverUrl);
|
|
718
|
+
|
|
719
|
+
// Resolve secret commands only for this connection attempt, without
|
|
720
|
+
// mutating the persisted configuration.
|
|
721
|
+
const hasCommandHeader = Object.values(definition.headers ?? {})
|
|
722
|
+
.some(value => value.startsWith("!") && !value.startsWith("!!"));
|
|
723
|
+
const headers = resolveCommandSecretsRecord(
|
|
724
|
+
definition.headers,
|
|
725
|
+
key => `MCP server "${serverName}" HTTP header "${key}"`,
|
|
726
|
+
) ?? {};
|
|
727
|
+
|
|
728
|
+
// Resolve bearer auth before creating requestInit so every attempted
|
|
729
|
+
// transport receives the same headers.
|
|
730
|
+
const commandBearer = definition.bearerToken?.startsWith("!") && !definition.bearerToken.startsWith("!!")
|
|
731
|
+
? definition.bearerToken
|
|
732
|
+
: undefined;
|
|
733
|
+
if (definition.auth === "bearer") {
|
|
734
|
+
const token = commandBearer
|
|
735
|
+
? resolveCommandSecret(commandBearer, `MCP server "${serverName}" HTTP bearer token`)
|
|
736
|
+
: resolveBearerToken(definition);
|
|
737
|
+
if (token) headers["Authorization"] = `Bearer ${token}`;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
if (hasCommandHeader || commandBearer) {
|
|
741
|
+
try {
|
|
742
|
+
new Headers(headers);
|
|
743
|
+
} catch {
|
|
744
|
+
throw new Error(`Failed to resolve MCP server "${serverName}" HTTP command secret: command returned an invalid header value`);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
const requestInit = Object.keys(headers).length > 0 ? { headers } : undefined;
|
|
749
|
+
const createAuthProvider = (): McpOAuthProvider => new McpOAuthProvider(
|
|
750
|
+
serverName,
|
|
751
|
+
serverUrl,
|
|
752
|
+
extractOAuthConfig(definition),
|
|
753
|
+
{ onRedirect: async () => {} },
|
|
754
|
+
this.authStorageOptions,
|
|
755
|
+
this.oauthRuntime?.signal,
|
|
756
|
+
);
|
|
757
|
+
|
|
758
|
+
// Explicit OAuth checks secure storage immediately. Implicit OAuth defers
|
|
759
|
+
// provider construction until the server proves authentication is needed.
|
|
760
|
+
let authState: HttpAuthProviderState = supportsOAuth(definition)
|
|
761
|
+
? definition.auth === undefined
|
|
762
|
+
? { status: "implicit-deferred" }
|
|
763
|
+
: { status: "explicit", provider: createAuthProvider() }
|
|
764
|
+
: { status: "disabled" };
|
|
765
|
+
|
|
766
|
+
const attempt = async (
|
|
767
|
+
kind: "streamable-http" | "sse",
|
|
768
|
+
): Promise<
|
|
769
|
+
| { status: "connected"; client: Client; transport: Transport }
|
|
770
|
+
| { status: "failed"; client: Client; transport: Transport; error: unknown }
|
|
771
|
+
> => {
|
|
772
|
+
const authProvider = "provider" in authState ? authState.provider : undefined;
|
|
773
|
+
const transportOptions = {
|
|
774
|
+
...(requestInit !== undefined ? { requestInit } : {}),
|
|
775
|
+
...(authProvider !== undefined ? { authProvider } : {}),
|
|
776
|
+
...(authProvider !== undefined
|
|
777
|
+
&& definition.oauth !== false
|
|
778
|
+
&& definition.oauth?.skipIssuerMetadataValidation === true
|
|
779
|
+
? { skipIssuerMetadataValidation: true }
|
|
780
|
+
: {}),
|
|
781
|
+
};
|
|
782
|
+
const baseTransport: Transport = kind === "streamable-http"
|
|
783
|
+
? new StreamableHTTPClientTransport(url, transportOptions)
|
|
784
|
+
: new SSEClientTransport(url, transportOptions);
|
|
785
|
+
const transport = traceObserver
|
|
786
|
+
? wrapTransportWithMcpTrace(baseTransport, serverName, kind, traceObserver)
|
|
787
|
+
: baseTransport;
|
|
788
|
+
const client = this.createClient(serverName, definition);
|
|
789
|
+
|
|
790
|
+
try {
|
|
791
|
+
await this.connectClientWithAbort(client, transport, requestOptions, signal);
|
|
792
|
+
return { status: "connected", client, transport };
|
|
793
|
+
} catch (error) {
|
|
794
|
+
const abortCleanupFailed = error instanceof AggregateError
|
|
795
|
+
&& error.message === "MCP connection abort cleanup failed";
|
|
796
|
+
if (!abortCleanupFailed) {
|
|
797
|
+
try {
|
|
798
|
+
await (abortCleanupPromises.get(transport) ?? client.close());
|
|
799
|
+
} catch (cleanupError) {
|
|
800
|
+
throw new AggregateError([error, cleanupError], "MCP HTTP connection cleanup failed");
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
return { status: "failed", client, transport, error };
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
// Connect the real client once. Retry Streamable HTTP only for an implicit
|
|
808
|
+
// OAuth challenge; use SSE only for definitive endpoint incompatibility.
|
|
809
|
+
// Agent Plugins set httpTransport, and their declared transport is used without fallback.
|
|
810
|
+
let kind: "streamable-http" | "sse" = definition.httpTransport ?? "streamable-http";
|
|
811
|
+
let invalidated = credentialsInvalidated;
|
|
812
|
+
for (;;) {
|
|
813
|
+
const result = await attempt(kind);
|
|
814
|
+
if (result.status === "connected") return { ...result, credentialsInvalidated: invalidated };
|
|
815
|
+
if (result.error instanceof AggregateError
|
|
816
|
+
&& result.error.message === "MCP connection abort cleanup failed") {
|
|
817
|
+
throw result.error;
|
|
818
|
+
}
|
|
819
|
+
if (signal?.aborted) throwIfAborted(signal);
|
|
820
|
+
|
|
821
|
+
if (authState.status === "implicit-deferred" && isUnauthorizedHttpError(result.error)) {
|
|
822
|
+
authState = { status: "implicit-challenged", provider: createAuthProvider() };
|
|
823
|
+
continue;
|
|
824
|
+
}
|
|
825
|
+
if (isUnauthorizedHttpError(result.error)) {
|
|
826
|
+
if (supportsOAuth(definition)) {
|
|
827
|
+
if (!invalidated) {
|
|
828
|
+
invalidateAuthEntryCache(serverName);
|
|
829
|
+
invalidated = true;
|
|
830
|
+
}
|
|
831
|
+
return {
|
|
832
|
+
client: result.client,
|
|
833
|
+
transport: result.transport,
|
|
834
|
+
status: "needs-auth",
|
|
835
|
+
credentialsInvalidated: invalidated,
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
throw result.error;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
if (definition.httpTransport === undefined && kind === "streamable-http" && shouldFallbackToSse(result.error, definition)) {
|
|
842
|
+
kind = "sse";
|
|
843
|
+
continue;
|
|
844
|
+
}
|
|
845
|
+
throw result.error;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
private async fetchAllTools(client: Client, requestOptions?: RequestOptions): Promise<McpTool[]> {
|
|
850
|
+
const allTools: McpTool[] = [];
|
|
851
|
+
let cursor: string | undefined;
|
|
852
|
+
|
|
853
|
+
do {
|
|
854
|
+
const result = await client.listTools(cursor ? { cursor } : undefined, requestOptions);
|
|
855
|
+
allTools.push(...(result.tools ?? []));
|
|
856
|
+
cursor = result.nextCursor;
|
|
857
|
+
} while (cursor);
|
|
858
|
+
|
|
859
|
+
return allTools;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
private async fetchAllPrompts(
|
|
863
|
+
client: Client,
|
|
864
|
+
requestOptions?: RequestOptions,
|
|
865
|
+
): Promise<{ prompts: McpPrompt[]; failed: boolean }> {
|
|
866
|
+
const capabilities = client.getServerCapabilities?.();
|
|
867
|
+
if (!capabilities?.prompts) return { prompts: [], failed: false };
|
|
868
|
+
|
|
869
|
+
try {
|
|
870
|
+
const prompts: McpPrompt[] = [];
|
|
871
|
+
let cursor: string | undefined;
|
|
872
|
+
do {
|
|
873
|
+
const result = await client.listPrompts(cursor ? { cursor } : undefined, requestOptions);
|
|
874
|
+
prompts.push(...(result.prompts ?? []));
|
|
875
|
+
cursor = result.nextCursor;
|
|
876
|
+
} while (cursor);
|
|
877
|
+
return { prompts, failed: false };
|
|
878
|
+
} catch (error) {
|
|
879
|
+
if (requestOptions?.signal?.aborted) throwIfAborted(requestOptions.signal);
|
|
880
|
+
if (isUnauthorizedHttpError(error)) throw error;
|
|
881
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
882
|
+
logger.debug(`MCP: prompts/list failed: ${message}`);
|
|
883
|
+
return { prompts: [], failed: true };
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
private async fetchAllResources(client: Client, requestOptions?: RequestOptions): Promise<McpResource[]> {
|
|
888
|
+
const capabilities = client.getServerCapabilities?.();
|
|
889
|
+
if (!capabilities?.resources) return [];
|
|
890
|
+
|
|
891
|
+
try {
|
|
892
|
+
const allResources: McpResource[] = [];
|
|
893
|
+
let cursor: string | undefined;
|
|
894
|
+
|
|
895
|
+
do {
|
|
896
|
+
const result = await client.listResources(cursor ? { cursor } : undefined, requestOptions);
|
|
897
|
+
allResources.push(...(result.resources ?? []));
|
|
898
|
+
cursor = result.nextCursor;
|
|
899
|
+
} while (cursor);
|
|
900
|
+
|
|
901
|
+
return allResources;
|
|
902
|
+
} catch (error) {
|
|
903
|
+
if (requestOptions?.signal?.aborted) {
|
|
904
|
+
throwIfAborted(requestOptions.signal);
|
|
905
|
+
}
|
|
906
|
+
if (isUnauthorizedHttpError(error)) throw error;
|
|
907
|
+
// The server advertises resources but the listing failed
|
|
908
|
+
return [];
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
private attachAdapterNotificationHandlers(serverName: string, client: Client): void {
|
|
913
|
+
client.setNotificationHandler(
|
|
914
|
+
SERVER_STREAM_RESULT_PATCH_METHOD,
|
|
915
|
+
{ params: serverStreamResultPatchNotificationSchema.shape.params },
|
|
916
|
+
params => {
|
|
917
|
+
const listener = this.uiStreamListeners.get(params.streamToken);
|
|
918
|
+
if (!listener) return;
|
|
919
|
+
listener(serverName, params);
|
|
920
|
+
},
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
registerUiStreamListener(streamToken: string, listener: UiStreamListener): void {
|
|
925
|
+
this.uiStreamListeners.set(streamToken, listener);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
removeUiStreamListener(streamToken: string): void {
|
|
929
|
+
this.uiStreamListeners.delete(streamToken);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
async getPrompt(
|
|
933
|
+
name: string,
|
|
934
|
+
promptName: string,
|
|
935
|
+
args?: Record<string, string>,
|
|
936
|
+
signal?: AbortSignal,
|
|
937
|
+
): Promise<GetPromptResult> {
|
|
938
|
+
const connection = this.connections.get(name);
|
|
939
|
+
if (!connection || connection.status !== "connected") {
|
|
940
|
+
throw new Error(`Server "${name}" is not connected`);
|
|
941
|
+
}
|
|
942
|
+
try {
|
|
943
|
+
this.touch(name);
|
|
944
|
+
this.incrementInFlight(name);
|
|
945
|
+
return await connection.client.getPrompt(
|
|
946
|
+
{ name: promptName, ...(args ? { arguments: args } : {}) },
|
|
947
|
+
this.getRequestOptions(name, signal),
|
|
948
|
+
);
|
|
949
|
+
} finally {
|
|
950
|
+
this.decrementInFlight(name);
|
|
951
|
+
this.touch(name);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
async readResource(name: string, uri: string, signal?: AbortSignal): Promise<ReadResourceResult> {
|
|
956
|
+
if (isServerDisabled(this.connections.get(name)?.definition)) {
|
|
957
|
+
throw new Error(`MCP server "${name}" is disabled`);
|
|
958
|
+
}
|
|
959
|
+
const connection = this.connections.get(name);
|
|
960
|
+
if (!connection || connection.status !== "connected") {
|
|
961
|
+
throw new Error(`Server "${name}" is not connected`);
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
try {
|
|
965
|
+
this.touch(name);
|
|
966
|
+
this.incrementInFlight(name);
|
|
967
|
+
return await connection.client.readResource({ uri }, this.getRequestOptions(name, signal));
|
|
968
|
+
} finally {
|
|
969
|
+
this.decrementInFlight(name);
|
|
970
|
+
this.touch(name);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
async close(name: string): Promise<void> {
|
|
975
|
+
this.closeGenerations.set(name, (this.closeGenerations.get(name) ?? 0) + 1);
|
|
976
|
+
this.connectAttempts.get(name)?.abort(new Error(`MCP connection ${name} was closed`));
|
|
977
|
+
|
|
978
|
+
const connection = this.connections.get(name);
|
|
979
|
+
if (!connection) {
|
|
980
|
+
const pendingClose = this.closePromises.get(name);
|
|
981
|
+
if (pendingClose) {
|
|
982
|
+
await pendingClose;
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
const pendingConnect = this.connectPromises.get(name);
|
|
986
|
+
if (pendingConnect) {
|
|
987
|
+
try {
|
|
988
|
+
await pendingConnect;
|
|
989
|
+
} catch (error) {
|
|
990
|
+
if (this.containsCleanupFailure(error)) throw error;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
// Delete before awaiting SDK cleanup so a replacement cannot be removed by
|
|
997
|
+
// an old close operation finishing later.
|
|
998
|
+
connection.status = "closed";
|
|
999
|
+
this.connections.delete(name);
|
|
1000
|
+
this.acceptedUrlElicitations.delete(name);
|
|
1001
|
+
const closing = this.disposeConnection(connection).finally(() => {
|
|
1002
|
+
if (this.closePromises.get(name) === closing) this.closePromises.delete(name);
|
|
1003
|
+
});
|
|
1004
|
+
this.closePromises.set(name, closing);
|
|
1005
|
+
return closing;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
private async disposeConnection(connection: ServerConnection): Promise<void> {
|
|
1009
|
+
const results = await Promise.allSettled([
|
|
1010
|
+
// Only client.close() is needed; the client owns the transport and will close it internally.
|
|
1011
|
+
Promise.resolve().then(() => connection.client.close()),
|
|
1012
|
+
this.traceWriter?.flush() ?? Promise.resolve(),
|
|
1013
|
+
]);
|
|
1014
|
+
const failures = results.flatMap(result => result.status === "rejected" ? [result.reason] : []);
|
|
1015
|
+
if (failures.length > 0) throw new AggregateError(failures, "MCP connection cleanup failed");
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
async closeAll(): Promise<void> {
|
|
1019
|
+
this.stopped = true;
|
|
1020
|
+
const names = new Set([...this.connections.keys(), ...this.connectPromises.keys()]);
|
|
1021
|
+
for (const name of names) {
|
|
1022
|
+
this.closeGenerations.set(name, (this.closeGenerations.get(name) ?? 0) + 1);
|
|
1023
|
+
this.connectAttempts.get(name)?.abort(new Error(`MCP connection ${name} was closed`));
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
const pendingConnects = [...this.connectPromises.values()];
|
|
1027
|
+
const currentNames = [...this.connections.keys()];
|
|
1028
|
+
const pendingResults = await Promise.allSettled(pendingConnects);
|
|
1029
|
+
const results = await Promise.allSettled(currentNames.map(name => this.close(name)));
|
|
1030
|
+
|
|
1031
|
+
// A connect that resolved during the first close snapshot is still fenced;
|
|
1032
|
+
// close any handle that was already inserted before its attempt settled.
|
|
1033
|
+
const lateNames = [...this.connections.keys()];
|
|
1034
|
+
const lateResults = await Promise.allSettled(lateNames.map(name => this.close(name)));
|
|
1035
|
+
const failures = [...pendingResults, ...results, ...lateResults]
|
|
1036
|
+
.flatMap(result => result.status === "rejected" ? [result.reason] : [])
|
|
1037
|
+
.filter(error => this.containsCleanupFailure(error));
|
|
1038
|
+
this.uiStreamListeners.clear();
|
|
1039
|
+
this.acceptedUrlElicitations.clear();
|
|
1040
|
+
this.samplingConfig = undefined;
|
|
1041
|
+
this.elicitationConfig = undefined;
|
|
1042
|
+
await this.traceWriter?.flush();
|
|
1043
|
+
if (failures.length > 0) throw new AggregateError(failures, "MCP manager cleanup failed");
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
private containsCleanupFailure(error: unknown): boolean {
|
|
1047
|
+
const pending: unknown[] = [error];
|
|
1048
|
+
const seen = new Set<unknown>();
|
|
1049
|
+
while (pending.length > 0) {
|
|
1050
|
+
const current = pending.pop();
|
|
1051
|
+
if (!(current instanceof Error) || seen.has(current)) continue;
|
|
1052
|
+
seen.add(current);
|
|
1053
|
+
if (current instanceof AggregateError) {
|
|
1054
|
+
if (/cleanup failed|setup failed/.test(current.message)) return true;
|
|
1055
|
+
pending.push(...current.errors);
|
|
1056
|
+
}
|
|
1057
|
+
if (current.cause !== undefined) pending.push(current.cause);
|
|
1058
|
+
}
|
|
1059
|
+
return false;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
isConnecting(name: string): boolean {
|
|
1063
|
+
return this.connectPromises.has(name);
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
getConnection(name: string): ServerConnection | undefined {
|
|
1067
|
+
return this.connections.get(name);
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
getAllConnections(): Map<string, ServerConnection> {
|
|
1071
|
+
return new Map(this.connections);
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
touch(name: string): void {
|
|
1075
|
+
const connection = this.connections.get(name);
|
|
1076
|
+
if (connection) {
|
|
1077
|
+
connection.lastUsedAt = Date.now();
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
incrementInFlight(name: string): void {
|
|
1082
|
+
const connection = this.connections.get(name);
|
|
1083
|
+
if (connection) {
|
|
1084
|
+
connection.inFlight = (connection.inFlight ?? 0) + 1;
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
decrementInFlight(name: string): void {
|
|
1089
|
+
const connection = this.connections.get(name);
|
|
1090
|
+
if (connection && connection.inFlight) {
|
|
1091
|
+
connection.inFlight--;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
isIdle(name: string, timeoutMs: number): boolean {
|
|
1096
|
+
const connection = this.connections.get(name);
|
|
1097
|
+
if (!connection || connection.status !== "connected") return false;
|
|
1098
|
+
if (connection.inFlight > 0) return false;
|
|
1099
|
+
return (Date.now() - connection.lastUsedAt) > timeoutMs;
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
/**
|
|
1104
|
+
* Resolve environment variables with interpolation.
|
|
1105
|
+
*/
|
|
1106
|
+
function resolveEnv(env: Record<string, string> | undefined, serverName: string, literalEnv = false): Record<string, string> {
|
|
1107
|
+
const resolved: Record<string, string> = {};
|
|
1108
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
1109
|
+
if (value !== undefined) resolved[key] = value;
|
|
1110
|
+
}
|
|
1111
|
+
if (literalEnv) return env ? { ...resolved, ...env } : resolved;
|
|
1112
|
+
|
|
1113
|
+
const overrides = resolveCommandSecretsRecord(
|
|
1114
|
+
env,
|
|
1115
|
+
key => `MCP server "${serverName}" stdio env "${key}"`,
|
|
1116
|
+
);
|
|
1117
|
+
return overrides ? { ...resolved, ...overrides } : resolved;
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
function normalizeRequestTimeoutMs(timeoutMs: number | undefined): number | undefined {
|
|
1121
|
+
return typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0
|
|
1122
|
+
? timeoutMs
|
|
1123
|
+
: undefined;
|
|
1124
|
+
}
|