@wahack/pi-coding-agent 15.11.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 +10031 -0
- package/README.md +36 -0
- package/examples/README.md +21 -0
- package/examples/custom-tools/README.md +104 -0
- package/examples/custom-tools/hello/index.ts +20 -0
- package/examples/extensions/README.md +142 -0
- package/examples/extensions/api-demo.ts +79 -0
- package/examples/extensions/chalk-logger.ts +25 -0
- package/examples/extensions/hello.ts +31 -0
- package/examples/extensions/pirate.ts +43 -0
- package/examples/extensions/plan-mode.ts +549 -0
- package/examples/extensions/reload-runtime.ts +38 -0
- package/examples/extensions/thinking-note.ts +13 -0
- package/examples/extensions/tools.ts +145 -0
- package/examples/extensions/with-deps/index.ts +36 -0
- package/examples/extensions/with-deps/package-lock.json +31 -0
- package/examples/extensions/with-deps/package.json +17 -0
- package/examples/hooks/README.md +56 -0
- package/examples/hooks/auto-commit-on-exit.ts +48 -0
- package/examples/hooks/confirm-destructive.ts +58 -0
- package/examples/hooks/custom-compaction.ts +115 -0
- package/examples/hooks/dirty-repo-guard.ts +51 -0
- package/examples/hooks/file-trigger.ts +40 -0
- package/examples/hooks/git-checkpoint.ts +52 -0
- package/examples/hooks/handoff.ts +149 -0
- package/examples/hooks/permission-gate.ts +33 -0
- package/examples/hooks/protected-paths.ts +29 -0
- package/examples/hooks/qna.ts +118 -0
- package/examples/hooks/status-line.ts +39 -0
- package/examples/sdk/01-minimal.ts +21 -0
- package/examples/sdk/02-custom-model.ts +49 -0
- package/examples/sdk/03-custom-prompt.ts +46 -0
- package/examples/sdk/04-skills.ts +43 -0
- package/examples/sdk/06-extensions.ts +82 -0
- package/examples/sdk/06-hooks.ts +61 -0
- package/examples/sdk/07-context-files.ts +35 -0
- package/examples/sdk/08-prompt-templates.ts +41 -0
- package/examples/sdk/08-slash-commands.ts +46 -0
- package/examples/sdk/09-api-keys-and-oauth.ts +54 -0
- package/examples/sdk/11-sessions.ts +47 -0
- package/examples/sdk/12-redis-sessions.ts +54 -0
- package/examples/sdk/13-sql-sessions.ts +61 -0
- package/examples/sdk/README.md +172 -0
- package/package.json +554 -0
- package/scripts/build-binary.ts +100 -0
- package/scripts/bundle-dist.ts +90 -0
- package/scripts/format-prompts.ts +68 -0
- package/scripts/generate-docs-index.ts +40 -0
- package/scripts/generate-template.ts +33 -0
- package/scripts/omp +42 -0
- package/scripts/omp.ts +19 -0
- package/src/async/index.ts +1 -0
- package/src/async/job-manager.ts +625 -0
- package/src/auto-thinking/classifier.ts +185 -0
- package/src/autoresearch/command-resume.md +14 -0
- package/src/autoresearch/dashboard.ts +436 -0
- package/src/autoresearch/git.ts +319 -0
- package/src/autoresearch/helpers.ts +218 -0
- package/src/autoresearch/index.ts +536 -0
- package/src/autoresearch/prompt-setup.md +43 -0
- package/src/autoresearch/prompt.md +103 -0
- package/src/autoresearch/resume-message.md +10 -0
- package/src/autoresearch/state.ts +273 -0
- package/src/autoresearch/storage.ts +699 -0
- package/src/autoresearch/tools/init-experiment.ts +272 -0
- package/src/autoresearch/tools/log-experiment.ts +524 -0
- package/src/autoresearch/tools/run-experiment.ts +407 -0
- package/src/autoresearch/tools/update-notes.ts +109 -0
- package/src/autoresearch/types.ts +168 -0
- package/src/bun-imports.d.ts +28 -0
- package/src/capability/context-file.ts +44 -0
- package/src/capability/extension-module.ts +34 -0
- package/src/capability/extension.ts +47 -0
- package/src/capability/fs.ts +117 -0
- package/src/capability/hook.ts +40 -0
- package/src/capability/index.ts +436 -0
- package/src/capability/instruction.ts +37 -0
- package/src/capability/mcp.ts +74 -0
- package/src/capability/prompt.ts +35 -0
- package/src/capability/rule-buckets.ts +66 -0
- package/src/capability/rule.ts +261 -0
- package/src/capability/settings.ts +34 -0
- package/src/capability/skill.ts +63 -0
- package/src/capability/slash-command.ts +40 -0
- package/src/capability/ssh.ts +41 -0
- package/src/capability/system-prompt.ts +34 -0
- package/src/capability/tool.ts +38 -0
- package/src/capability/types.ts +168 -0
- package/src/cli/agents-cli.ts +138 -0
- package/src/cli/args.ts +340 -0
- package/src/cli/auth-broker-cli.ts +895 -0
- package/src/cli/auth-gateway-cli.ts +611 -0
- package/src/cli/classify-install-target.ts +76 -0
- package/src/cli/claude-trace-cli.ts +795 -0
- package/src/cli/commands/init-xdg.ts +27 -0
- package/src/cli/completion-gen.ts +550 -0
- package/src/cli/config-cli.ts +418 -0
- package/src/cli/dry-balance-cli.ts +856 -0
- package/src/cli/extension-flags.ts +48 -0
- package/src/cli/file-processor.ts +133 -0
- package/src/cli/gallery-cli.ts +230 -0
- package/src/cli/gallery-fixtures/agentic.ts +407 -0
- package/src/cli/gallery-fixtures/codeintel.ts +187 -0
- package/src/cli/gallery-fixtures/edit.ts +194 -0
- package/src/cli/gallery-fixtures/fs.ts +220 -0
- package/src/cli/gallery-fixtures/index.ts +40 -0
- package/src/cli/gallery-fixtures/interaction.ts +49 -0
- package/src/cli/gallery-fixtures/memory.ts +81 -0
- package/src/cli/gallery-fixtures/misc.ts +250 -0
- package/src/cli/gallery-fixtures/search.ts +213 -0
- package/src/cli/gallery-fixtures/shell.ts +167 -0
- package/src/cli/gallery-fixtures/types.ts +57 -0
- package/src/cli/gallery-fixtures/web.ts +158 -0
- package/src/cli/gallery-screenshot.ts +279 -0
- package/src/cli/grep-cli.ts +160 -0
- package/src/cli/grievances-cli.ts +256 -0
- package/src/cli/initial-message.ts +58 -0
- package/src/cli/list-models.ts +194 -0
- package/src/cli/plugin-cli.ts +996 -0
- package/src/cli/read-cli.ts +57 -0
- package/src/cli/session-picker.ts +79 -0
- package/src/cli/setup-cli.ts +231 -0
- package/src/cli/shell-cli.ts +176 -0
- package/src/cli/ssh-cli.ts +179 -0
- package/src/cli/startup-cwd.ts +68 -0
- package/src/cli/stats-cli.ts +238 -0
- package/src/cli/tiny-models-cli.ts +127 -0
- package/src/cli/update-cli.ts +611 -0
- package/src/cli/usage-cli.ts +603 -0
- package/src/cli/web-search-cli.ts +132 -0
- package/src/cli/worktree-cli.ts +291 -0
- package/src/cli-commands.ts +79 -0
- package/src/cli.ts +200 -0
- package/src/commands/acp.ts +24 -0
- package/src/commands/agents.ts +57 -0
- package/src/commands/auth-broker.ts +99 -0
- package/src/commands/auth-gateway.ts +69 -0
- package/src/commands/commit.ts +46 -0
- package/src/commands/complete.ts +66 -0
- package/src/commands/completions.ts +60 -0
- package/src/commands/config.ts +51 -0
- package/src/commands/dry-balance.ts +43 -0
- package/src/commands/gallery.ts +52 -0
- package/src/commands/grep.ts +48 -0
- package/src/commands/grievances.ts +51 -0
- package/src/commands/install.ts +107 -0
- package/src/commands/launch.ts +169 -0
- package/src/commands/plugin.ts +78 -0
- package/src/commands/read.ts +38 -0
- package/src/commands/setup.ts +67 -0
- package/src/commands/shell.ts +29 -0
- package/src/commands/ssh.ts +60 -0
- package/src/commands/stats.ts +29 -0
- package/src/commands/tiny-models.ts +36 -0
- package/src/commands/update.ts +21 -0
- package/src/commands/usage.ts +35 -0
- package/src/commands/web-search.ts +42 -0
- package/src/commands/worktree.ts +56 -0
- package/src/commit/agentic/agent.ts +317 -0
- package/src/commit/agentic/fallback.ts +96 -0
- package/src/commit/agentic/index.ts +355 -0
- package/src/commit/agentic/prompts/analyze-file.md +22 -0
- package/src/commit/agentic/prompts/session-user.md +25 -0
- package/src/commit/agentic/prompts/split-confirm.md +1 -0
- package/src/commit/agentic/prompts/system.md +38 -0
- package/src/commit/agentic/state.ts +60 -0
- package/src/commit/agentic/tools/analyze-file.ts +146 -0
- package/src/commit/agentic/tools/git-file-diff.ts +191 -0
- package/src/commit/agentic/tools/git-hunk.ts +50 -0
- package/src/commit/agentic/tools/git-overview.ts +81 -0
- package/src/commit/agentic/tools/index.ts +54 -0
- package/src/commit/agentic/tools/propose-changelog.ts +144 -0
- package/src/commit/agentic/tools/propose-commit.ts +109 -0
- package/src/commit/agentic/tools/recent-commits.ts +81 -0
- package/src/commit/agentic/tools/schemas.ts +23 -0
- package/src/commit/agentic/tools/split-commit.ts +245 -0
- package/src/commit/agentic/topo-sort.ts +44 -0
- package/src/commit/agentic/trivial.ts +51 -0
- package/src/commit/agentic/validation.ts +183 -0
- package/src/commit/analysis/conventional.ts +64 -0
- package/src/commit/analysis/index.ts +4 -0
- package/src/commit/analysis/scope.ts +242 -0
- package/src/commit/analysis/summary.ts +105 -0
- package/src/commit/analysis/validation.ts +66 -0
- package/src/commit/changelog/detect.ts +40 -0
- package/src/commit/changelog/generate.ts +97 -0
- package/src/commit/changelog/index.ts +234 -0
- package/src/commit/changelog/parse.ts +44 -0
- package/src/commit/cli.ts +85 -0
- package/src/commit/git/diff.ts +148 -0
- package/src/commit/index.ts +5 -0
- package/src/commit/map-reduce/index.ts +69 -0
- package/src/commit/map-reduce/map-phase.ts +193 -0
- package/src/commit/map-reduce/reduce-phase.ts +49 -0
- package/src/commit/map-reduce/utils.ts +9 -0
- package/src/commit/message.ts +11 -0
- package/src/commit/model-selection.ts +92 -0
- package/src/commit/pipeline.ts +243 -0
- package/src/commit/prompts/analysis-system.md +148 -0
- package/src/commit/prompts/analysis-user.md +38 -0
- package/src/commit/prompts/changelog-system.md +50 -0
- package/src/commit/prompts/changelog-user.md +18 -0
- package/src/commit/prompts/file-observer-system.md +24 -0
- package/src/commit/prompts/file-observer-user.md +8 -0
- package/src/commit/prompts/reduce-system.md +50 -0
- package/src/commit/prompts/reduce-user.md +17 -0
- package/src/commit/prompts/summary-retry.md +3 -0
- package/src/commit/prompts/summary-system.md +38 -0
- package/src/commit/prompts/summary-user.md +13 -0
- package/src/commit/prompts/types-description.md +2 -0
- package/src/commit/shared-llm.ts +77 -0
- package/src/commit/types.ts +118 -0
- package/src/commit/utils/exclusions.ts +42 -0
- package/src/commit/utils.ts +58 -0
- package/src/config/api-key-resolver.ts +60 -0
- package/src/config/append-only-context-mode.ts +31 -0
- package/src/config/config-file.ts +317 -0
- package/src/config/file-lock.ts +164 -0
- package/src/config/keybindings.ts +628 -0
- package/src/config/mcp-schema.json +230 -0
- package/src/config/model-discovery.ts +554 -0
- package/src/config/model-registry.ts +2090 -0
- package/src/config/model-resolver.ts +1502 -0
- package/src/config/model-roles.ts +74 -0
- package/src/config/models-config-schema.ts +226 -0
- package/src/config/models-config.ts +129 -0
- package/src/config/prompt-templates.ts +185 -0
- package/src/config/resolve-config-value.ts +94 -0
- package/src/config/settings-schema.ts +3530 -0
- package/src/config/settings.ts +1178 -0
- package/src/config.ts +242 -0
- package/src/cursor.ts +340 -0
- package/src/dap/client.ts +760 -0
- package/src/dap/config.ts +189 -0
- package/src/dap/defaults.json +212 -0
- package/src/dap/index.ts +4 -0
- package/src/dap/session.ts +1441 -0
- package/src/dap/types.ts +610 -0
- package/src/debug/index.ts +515 -0
- package/src/debug/log-formatting.ts +58 -0
- package/src/debug/log-viewer.ts +908 -0
- package/src/debug/profiler.ts +162 -0
- package/src/debug/protocol-probe.ts +267 -0
- package/src/debug/raw-sse-buffer.ts +273 -0
- package/src/debug/raw-sse.ts +292 -0
- package/src/debug/report-bundle.ts +374 -0
- package/src/debug/system-info.ts +111 -0
- package/src/debug/terminal-info.ts +124 -0
- package/src/discovery/agents-md.ts +67 -0
- package/src/discovery/agents.ts +230 -0
- package/src/discovery/at-imports.ts +273 -0
- package/src/discovery/builtin-defaults.ts +39 -0
- package/src/discovery/builtin-rules/index.ts +54 -0
- package/src/discovery/builtin-rules/rs-box-leak.md +48 -0
- package/src/discovery/builtin-rules/rs-future-prelude.md +23 -0
- package/src/discovery/builtin-rules/rs-lazylock.md +51 -0
- package/src/discovery/builtin-rules/rs-match-ergonomics.md +67 -0
- package/src/discovery/builtin-rules/rs-parking-lot.md +44 -0
- package/src/discovery/builtin-rules/rs-result-type.md +19 -0
- package/src/discovery/builtin-rules/ts-bare-catch.md +38 -0
- package/src/discovery/builtin-rules/ts-import-type.md +42 -0
- package/src/discovery/builtin-rules/ts-no-any.md +56 -0
- package/src/discovery/builtin-rules/ts-no-deprecated-leftovers.md +44 -0
- package/src/discovery/builtin-rules/ts-no-dynamic-import.md +39 -0
- package/src/discovery/builtin-rules/ts-no-return-type.md +45 -0
- package/src/discovery/builtin-rules/ts-no-test-timers.md +55 -0
- package/src/discovery/builtin-rules/ts-no-tiny-functions.md +51 -0
- package/src/discovery/builtin-rules/ts-promise-with-resolvers.md +65 -0
- package/src/discovery/builtin-rules/ts-redundant-clear-guard.md +75 -0
- package/src/discovery/builtin-rules/ts-set-map.md +28 -0
- package/src/discovery/builtin.ts +906 -0
- package/src/discovery/claude-plugins.ts +386 -0
- package/src/discovery/claude.ts +584 -0
- package/src/discovery/cline.ts +83 -0
- package/src/discovery/codex.ts +522 -0
- package/src/discovery/cursor.ts +220 -0
- package/src/discovery/gemini.ts +383 -0
- package/src/discovery/github.ts +154 -0
- package/src/discovery/helpers.ts +1016 -0
- package/src/discovery/index.ts +81 -0
- package/src/discovery/mcp-json.ts +171 -0
- package/src/discovery/omp-extension-roots.ts +190 -0
- package/src/discovery/omp-plugins.ts +383 -0
- package/src/discovery/opencode.ts +398 -0
- package/src/discovery/plugin-dir-roots.ts +28 -0
- package/src/discovery/ssh.ts +153 -0
- package/src/discovery/substitute-plugin-root.ts +29 -0
- package/src/discovery/vscode.ts +105 -0
- package/src/discovery/windsurf.ts +147 -0
- package/src/edit/apply-patch/index.ts +87 -0
- package/src/edit/apply-patch/parser.ts +174 -0
- package/src/edit/diff.ts +999 -0
- package/src/edit/file-snapshot-store.ts +91 -0
- package/src/edit/hashline/block-resolver.ts +33 -0
- package/src/edit/hashline/diff.ts +290 -0
- package/src/edit/hashline/execute.ts +242 -0
- package/src/edit/hashline/filesystem.ts +130 -0
- package/src/edit/hashline/index.ts +5 -0
- package/src/edit/hashline/noop-loop-guard.ts +99 -0
- package/src/edit/hashline/params.ts +18 -0
- package/src/edit/index.ts +571 -0
- package/src/edit/modes/apply-patch.lark +19 -0
- package/src/edit/modes/apply-patch.ts +53 -0
- package/src/edit/modes/patch.ts +1891 -0
- package/src/edit/modes/replace.ts +1137 -0
- package/src/edit/normalize.ts +345 -0
- package/src/edit/notebook.ts +242 -0
- package/src/edit/read-file.ts +25 -0
- package/src/edit/renderer.ts +769 -0
- package/src/edit/streaming.ts +517 -0
- package/src/eval/__tests__/agent-bridge.test.ts +708 -0
- package/src/eval/__tests__/bridge-timeout.test.ts +64 -0
- package/src/eval/__tests__/budget-bridge.test.ts +69 -0
- package/src/eval/__tests__/completion-bridge.test.ts +412 -0
- package/src/eval/__tests__/helpers-local-roots.test.ts +58 -0
- package/src/eval/__tests__/idle-timeout.test.ts +80 -0
- package/src/eval/__tests__/js-context-manager.test.ts +241 -0
- package/src/eval/__tests__/kernel-spawn.test.ts +103 -0
- package/src/eval/agent-bridge.ts +319 -0
- package/src/eval/backend.ts +71 -0
- package/src/eval/bridge-timeout.ts +44 -0
- package/src/eval/budget-bridge.ts +48 -0
- package/src/eval/completion-bridge.ts +207 -0
- package/src/eval/concurrency-bridge.ts +34 -0
- package/src/eval/idle-timeout.ts +91 -0
- package/src/eval/index.ts +4 -0
- package/src/eval/js/context-manager.ts +502 -0
- package/src/eval/js/executor.ts +173 -0
- package/src/eval/js/index.ts +51 -0
- package/src/eval/js/shared/helpers.ts +283 -0
- package/src/eval/js/shared/indirect-eval.ts +30 -0
- package/src/eval/js/shared/local-module-loader.ts +342 -0
- package/src/eval/js/shared/prelude.ts +2 -0
- package/src/eval/js/shared/prelude.txt +246 -0
- package/src/eval/js/shared/rewrite-imports.ts +532 -0
- package/src/eval/js/shared/runtime.ts +352 -0
- package/src/eval/js/shared/types.ts +18 -0
- package/src/eval/js/tool-bridge.ts +162 -0
- package/src/eval/js/worker-core.ts +132 -0
- package/src/eval/js/worker-entry.ts +30 -0
- package/src/eval/js/worker-protocol.ts +47 -0
- package/src/eval/py/__tests__/prelude.test.ts +19 -0
- package/src/eval/py/display.ts +71 -0
- package/src/eval/py/executor.ts +742 -0
- package/src/eval/py/index.ts +68 -0
- package/src/eval/py/kernel.ts +748 -0
- package/src/eval/py/prelude.py +658 -0
- package/src/eval/py/prelude.ts +3 -0
- package/src/eval/py/runner.py +1133 -0
- package/src/eval/py/runtime.ts +276 -0
- package/src/eval/py/spawn-options.ts +126 -0
- package/src/eval/py/tool-bridge.ts +182 -0
- package/src/eval/session-id.ts +8 -0
- package/src/eval/types.ts +48 -0
- package/src/exa/index.ts +2 -0
- package/src/exa/mcp-client.ts +370 -0
- package/src/exa/types.ts +69 -0
- package/src/exec/bash-executor.ts +419 -0
- package/src/exec/exec.ts +53 -0
- package/src/exec/non-interactive-env.ts +48 -0
- package/src/export/custom-share.ts +65 -0
- package/src/export/html/index.ts +164 -0
- package/src/export/html/template.css +1051 -0
- package/src/export/html/template.generated.ts +2 -0
- package/src/export/html/template.html +46 -0
- package/src/export/html/template.js +2271 -0
- package/src/export/html/template.macro.ts +25 -0
- package/src/export/html/vendor/highlight.min.js +1213 -0
- package/src/export/html/vendor/marked.min.js +6 -0
- package/src/export/ttsr.ts +583 -0
- package/src/extensibility/custom-commands/bundled/ci-green/index.ts +54 -0
- package/src/extensibility/custom-commands/bundled/review/index.ts +489 -0
- package/src/extensibility/custom-commands/index.ts +2 -0
- package/src/extensibility/custom-commands/loader.ts +238 -0
- package/src/extensibility/custom-commands/types.ts +113 -0
- package/src/extensibility/custom-tools/index.ts +7 -0
- package/src/extensibility/custom-tools/loader.ts +269 -0
- package/src/extensibility/custom-tools/types.ts +270 -0
- package/src/extensibility/custom-tools/wrapper.ts +47 -0
- package/src/extensibility/extensions/compact-handler.ts +40 -0
- package/src/extensibility/extensions/get-commands-handler.ts +78 -0
- package/src/extensibility/extensions/index.ts +16 -0
- package/src/extensibility/extensions/loader.ts +572 -0
- package/src/extensibility/extensions/runner.ts +922 -0
- package/src/extensibility/extensions/types.ts +1322 -0
- package/src/extensibility/extensions/wrapper.ts +223 -0
- package/src/extensibility/hooks/index.ts +5 -0
- package/src/extensibility/hooks/loader.ts +257 -0
- package/src/extensibility/hooks/runner.ts +425 -0
- package/src/extensibility/hooks/tool-wrapper.ts +107 -0
- package/src/extensibility/hooks/types.ts +606 -0
- package/src/extensibility/legacy-pi-ai-shim.ts +24 -0
- package/src/extensibility/legacy-pi-coding-agent-shim.ts +15 -0
- package/src/extensibility/plugins/doctor.ts +65 -0
- package/src/extensibility/plugins/git-url.ts +367 -0
- package/src/extensibility/plugins/index.ts +9 -0
- package/src/extensibility/plugins/installer.ts +192 -0
- package/src/extensibility/plugins/legacy-pi-compat.ts +682 -0
- package/src/extensibility/plugins/loader.ts +313 -0
- package/src/extensibility/plugins/manager.ts +827 -0
- package/src/extensibility/plugins/marketplace/cache.ts +136 -0
- package/src/extensibility/plugins/marketplace/fetcher.ts +317 -0
- package/src/extensibility/plugins/marketplace/index.ts +6 -0
- package/src/extensibility/plugins/marketplace/manager.ts +770 -0
- package/src/extensibility/plugins/marketplace/registry.ts +196 -0
- package/src/extensibility/plugins/marketplace/source-resolver.ts +147 -0
- package/src/extensibility/plugins/marketplace/types.ts +191 -0
- package/src/extensibility/plugins/marketplace-auto-update.ts +49 -0
- package/src/extensibility/plugins/parser.ts +105 -0
- package/src/extensibility/plugins/types.ts +194 -0
- package/src/extensibility/shared-events.ts +343 -0
- package/src/extensibility/skills.ts +312 -0
- package/src/extensibility/slash-commands.ts +227 -0
- package/src/extensibility/tool-proxy.ts +25 -0
- package/src/extensibility/typebox.ts +418 -0
- package/src/extensibility/utils.ts +44 -0
- package/src/goals/index.ts +3 -0
- package/src/goals/runtime.ts +528 -0
- package/src/goals/state.ts +37 -0
- package/src/goals/tools/goal-tool.ts +251 -0
- package/src/hindsight/backend.ts +354 -0
- package/src/hindsight/bank.ts +156 -0
- package/src/hindsight/client.ts +598 -0
- package/src/hindsight/config.ts +175 -0
- package/src/hindsight/content.ts +210 -0
- package/src/hindsight/index.ts +8 -0
- package/src/hindsight/mental-models.ts +429 -0
- package/src/hindsight/seeds.json +32 -0
- package/src/hindsight/state.ts +488 -0
- package/src/hindsight/transcript.ts +71 -0
- package/src/index.ts +59 -0
- package/src/internal-urls/agent-protocol.ts +146 -0
- package/src/internal-urls/artifact-protocol.ts +107 -0
- package/src/internal-urls/docs-index.generated.ts +106 -0
- package/src/internal-urls/history-protocol.ts +113 -0
- package/src/internal-urls/index.ts +25 -0
- package/src/internal-urls/issue-pr-protocol.ts +584 -0
- package/src/internal-urls/json-query.ts +126 -0
- package/src/internal-urls/local-protocol.ts +287 -0
- package/src/internal-urls/mcp-protocol.ts +151 -0
- package/src/internal-urls/memory-protocol.ts +169 -0
- package/src/internal-urls/omp-protocol.ts +93 -0
- package/src/internal-urls/parse.ts +72 -0
- package/src/internal-urls/registry-helpers.ts +25 -0
- package/src/internal-urls/router.ts +105 -0
- package/src/internal-urls/rule-protocol.ts +45 -0
- package/src/internal-urls/skill-protocol.ts +96 -0
- package/src/internal-urls/types.ts +152 -0
- package/src/internal-urls/vault-protocol.ts +936 -0
- package/src/irc/bus.ts +292 -0
- package/src/lib/xai-http.ts +124 -0
- package/src/lsp/client.ts +1193 -0
- package/src/lsp/clients/biome-client.ts +264 -0
- package/src/lsp/clients/index.ts +50 -0
- package/src/lsp/clients/lsp-linter-client.ts +93 -0
- package/src/lsp/clients/swiftlint-client.ts +120 -0
- package/src/lsp/config.ts +502 -0
- package/src/lsp/defaults.json +493 -0
- package/src/lsp/diagnostics-ledger.ts +51 -0
- package/src/lsp/edits.ts +267 -0
- package/src/lsp/index.ts +2477 -0
- package/src/lsp/lspmux.ts +233 -0
- package/src/lsp/render.ts +694 -0
- package/src/lsp/startup-events.ts +13 -0
- package/src/lsp/types.ts +455 -0
- package/src/lsp/utils.ts +718 -0
- package/src/main.ts +1325 -0
- package/src/mcp/client.ts +484 -0
- package/src/mcp/config-writer.ts +225 -0
- package/src/mcp/config.ts +365 -0
- package/src/mcp/index.ts +29 -0
- package/src/mcp/json-rpc.ts +122 -0
- package/src/mcp/loader.ts +124 -0
- package/src/mcp/manager.ts +1275 -0
- package/src/mcp/oauth-discovery.ts +442 -0
- package/src/mcp/oauth-flow.ts +442 -0
- package/src/mcp/render.ts +132 -0
- package/src/mcp/smithery-auth.ts +104 -0
- package/src/mcp/smithery-connect.ts +145 -0
- package/src/mcp/smithery-registry.ts +477 -0
- package/src/mcp/timeout.ts +59 -0
- package/src/mcp/tool-bridge.ts +426 -0
- package/src/mcp/tool-cache.ts +117 -0
- package/src/mcp/transports/http.ts +519 -0
- package/src/mcp/transports/index.ts +6 -0
- package/src/mcp/transports/stdio.ts +528 -0
- package/src/mcp/types.ts +423 -0
- package/src/memories/index.ts +1150 -0
- package/src/memories/storage.ts +577 -0
- package/src/memory-backend/index.ts +18 -0
- package/src/memory-backend/local-backend.ts +39 -0
- package/src/memory-backend/off-backend.ts +25 -0
- package/src/memory-backend/resolve.ts +25 -0
- package/src/memory-backend/runtime.ts +66 -0
- package/src/memory-backend/types.ts +166 -0
- package/src/mnemopi/backend.ts +547 -0
- package/src/mnemopi/config.ts +160 -0
- package/src/mnemopi/index.ts +3 -0
- package/src/mnemopi/state.ts +584 -0
- package/src/modes/acp/acp-agent.ts +2407 -0
- package/src/modes/acp/acp-client-bridge.ts +154 -0
- package/src/modes/acp/acp-event-mapper.ts +929 -0
- package/src/modes/acp/acp-mode.ts +23 -0
- package/src/modes/acp/index.ts +2 -0
- package/src/modes/acp/terminal-auth.ts +37 -0
- package/src/modes/components/agent-dashboard.ts +1206 -0
- package/src/modes/components/agent-hub.ts +1071 -0
- package/src/modes/components/assistant-message.ts +307 -0
- package/src/modes/components/bash-execution.ts +220 -0
- package/src/modes/components/bordered-loader.ts +41 -0
- package/src/modes/components/branch-summary-message.ts +45 -0
- package/src/modes/components/btw-panel.ts +104 -0
- package/src/modes/components/chat-block.ts +111 -0
- package/src/modes/components/compaction-summary-message.ts +87 -0
- package/src/modes/components/copy-selector.ts +206 -0
- package/src/modes/components/countdown-timer.ts +75 -0
- package/src/modes/components/custom-editor.ts +398 -0
- package/src/modes/components/custom-message.ts +63 -0
- package/src/modes/components/diff.ts +277 -0
- package/src/modes/components/dynamic-border.ts +34 -0
- package/src/modes/components/error-banner.ts +33 -0
- package/src/modes/components/eval-execution.ts +158 -0
- package/src/modes/components/execution-shared.ts +101 -0
- package/src/modes/components/extensions/extension-dashboard.ts +399 -0
- package/src/modes/components/extensions/extension-list.ts +502 -0
- package/src/modes/components/extensions/index.ts +9 -0
- package/src/modes/components/extensions/inspector-panel.ts +317 -0
- package/src/modes/components/extensions/state-manager.ts +627 -0
- package/src/modes/components/extensions/types.ts +186 -0
- package/src/modes/components/footer.ts +274 -0
- package/src/modes/components/history-search.ts +280 -0
- package/src/modes/components/hook-editor.ts +167 -0
- package/src/modes/components/hook-input.ts +87 -0
- package/src/modes/components/hook-message.ts +66 -0
- package/src/modes/components/hook-selector.ts +660 -0
- package/src/modes/components/index.ts +38 -0
- package/src/modes/components/keybinding-hints.ts +65 -0
- package/src/modes/components/late-diagnostics-message.ts +60 -0
- package/src/modes/components/login-dialog.ts +164 -0
- package/src/modes/components/mcp-add-wizard.ts +1340 -0
- package/src/modes/components/message-frame.ts +88 -0
- package/src/modes/components/model-selector.ts +1271 -0
- package/src/modes/components/oauth-selector.ts +368 -0
- package/src/modes/components/omfg-panel.ts +141 -0
- package/src/modes/components/overlay-box.ts +108 -0
- package/src/modes/components/plan-review-overlay.ts +820 -0
- package/src/modes/components/plan-toc.ts +138 -0
- package/src/modes/components/plugin-selector.ts +95 -0
- package/src/modes/components/plugin-settings.ts +722 -0
- package/src/modes/components/queue-mode-selector.ts +56 -0
- package/src/modes/components/read-tool-group.ts +670 -0
- package/src/modes/components/segment-track.ts +52 -0
- package/src/modes/components/session-selector.ts +625 -0
- package/src/modes/components/settings-defs.ts +189 -0
- package/src/modes/components/settings-selector.ts +651 -0
- package/src/modes/components/show-images-selector.ts +45 -0
- package/src/modes/components/skill-message.ts +89 -0
- package/src/modes/components/status-line/component.ts +869 -0
- package/src/modes/components/status-line/context-thresholds.ts +79 -0
- package/src/modes/components/status-line/git-utils.ts +42 -0
- package/src/modes/components/status-line/index.ts +5 -0
- package/src/modes/components/status-line/presets.ts +106 -0
- package/src/modes/components/status-line/segments.ts +584 -0
- package/src/modes/components/status-line/separators.ts +55 -0
- package/src/modes/components/status-line/token-rate.ts +66 -0
- package/src/modes/components/status-line/types.ts +108 -0
- package/src/modes/components/theme-selector.ts +63 -0
- package/src/modes/components/thinking-selector.ts +52 -0
- package/src/modes/components/tiny-title-download-progress.ts +90 -0
- package/src/modes/components/tips.txt +19 -0
- package/src/modes/components/todo-reminder.ts +38 -0
- package/src/modes/components/tool-execution.ts +1024 -0
- package/src/modes/components/transcript-container.ts +608 -0
- package/src/modes/components/tree-selector.ts +978 -0
- package/src/modes/components/ttsr-notification.ts +122 -0
- package/src/modes/components/user-message-selector.ts +227 -0
- package/src/modes/components/user-message.ts +66 -0
- package/src/modes/components/visual-truncate.ts +63 -0
- package/src/modes/components/welcome.ts +493 -0
- package/src/modes/controllers/btw-controller.ts +105 -0
- package/src/modes/controllers/command-controller-shared.ts +109 -0
- package/src/modes/controllers/command-controller.ts +1566 -0
- package/src/modes/controllers/event-controller.ts +1054 -0
- package/src/modes/controllers/extension-ui-controller.ts +886 -0
- package/src/modes/controllers/input-controller.ts +1073 -0
- package/src/modes/controllers/mcp-command-controller.ts +2017 -0
- package/src/modes/controllers/omfg-controller.ts +283 -0
- package/src/modes/controllers/omfg-rule.ts +647 -0
- package/src/modes/controllers/selector-controller.ts +1108 -0
- package/src/modes/controllers/ssh-command-controller.ts +384 -0
- package/src/modes/controllers/streaming-reveal.ts +279 -0
- package/src/modes/controllers/tan-command-controller.ts +173 -0
- package/src/modes/controllers/todo-command-controller.ts +485 -0
- package/src/modes/data/emojis.json +1 -0
- package/src/modes/emoji-autocomplete.ts +285 -0
- package/src/modes/gradient-highlight.ts +87 -0
- package/src/modes/image-references.ts +117 -0
- package/src/modes/index.ts +17 -0
- package/src/modes/interactive-mode.ts +3370 -0
- package/src/modes/internal-url-autocomplete.ts +143 -0
- package/src/modes/loop-limit.ts +140 -0
- package/src/modes/magic-keywords.ts +20 -0
- package/src/modes/markdown-prose.ts +247 -0
- package/src/modes/oauth-manual-input.ts +69 -0
- package/src/modes/orchestrate.ts +42 -0
- package/src/modes/print-mode.ts +126 -0
- package/src/modes/prompt-action-autocomplete.ts +260 -0
- package/src/modes/rpc/host-tools.ts +186 -0
- package/src/modes/rpc/host-uris.ts +235 -0
- package/src/modes/rpc/rpc-client.ts +963 -0
- package/src/modes/rpc/rpc-mode.ts +947 -0
- package/src/modes/rpc/rpc-subagents.ts +265 -0
- package/src/modes/rpc/rpc-types.ts +458 -0
- package/src/modes/runtime-init.ts +116 -0
- package/src/modes/session-observer-registry.ts +146 -0
- package/src/modes/setup-version.ts +11 -0
- package/src/modes/setup-wizard/index.ts +99 -0
- package/src/modes/setup-wizard/lazy.ts +16 -0
- package/src/modes/setup-wizard/scenes/glyph.ts +96 -0
- package/src/modes/setup-wizard/scenes/outro.ts +35 -0
- package/src/modes/setup-wizard/scenes/providers.ts +69 -0
- package/src/modes/setup-wizard/scenes/sign-in.ts +205 -0
- package/src/modes/setup-wizard/scenes/splash.ts +201 -0
- package/src/modes/setup-wizard/scenes/theme.ts +299 -0
- package/src/modes/setup-wizard/scenes/types.ts +48 -0
- package/src/modes/setup-wizard/scenes/web-search.ts +129 -0
- package/src/modes/setup-wizard/wizard-overlay.ts +275 -0
- package/src/modes/shared.ts +47 -0
- package/src/modes/theme/dark.json +95 -0
- package/src/modes/theme/defaults/alabaster.json +93 -0
- package/src/modes/theme/defaults/amethyst.json +96 -0
- package/src/modes/theme/defaults/anthracite.json +93 -0
- package/src/modes/theme/defaults/basalt.json +91 -0
- package/src/modes/theme/defaults/birch.json +95 -0
- package/src/modes/theme/defaults/dark-abyss.json +91 -0
- package/src/modes/theme/defaults/dark-arctic.json +104 -0
- package/src/modes/theme/defaults/dark-aurora.json +95 -0
- package/src/modes/theme/defaults/dark-catppuccin.json +107 -0
- package/src/modes/theme/defaults/dark-cavern.json +91 -0
- package/src/modes/theme/defaults/dark-copper.json +95 -0
- package/src/modes/theme/defaults/dark-cosmos.json +90 -0
- package/src/modes/theme/defaults/dark-cyberpunk.json +102 -0
- package/src/modes/theme/defaults/dark-dracula.json +98 -0
- package/src/modes/theme/defaults/dark-eclipse.json +91 -0
- package/src/modes/theme/defaults/dark-ember.json +95 -0
- package/src/modes/theme/defaults/dark-equinox.json +90 -0
- package/src/modes/theme/defaults/dark-forest.json +96 -0
- package/src/modes/theme/defaults/dark-github.json +105 -0
- package/src/modes/theme/defaults/dark-gruvbox.json +112 -0
- package/src/modes/theme/defaults/dark-lavender.json +95 -0
- package/src/modes/theme/defaults/dark-lunar.json +89 -0
- package/src/modes/theme/defaults/dark-midnight.json +95 -0
- package/src/modes/theme/defaults/dark-monochrome.json +94 -0
- package/src/modes/theme/defaults/dark-monokai.json +98 -0
- package/src/modes/theme/defaults/dark-nebula.json +90 -0
- package/src/modes/theme/defaults/dark-nord.json +97 -0
- package/src/modes/theme/defaults/dark-ocean.json +101 -0
- package/src/modes/theme/defaults/dark-one.json +100 -0
- package/src/modes/theme/defaults/dark-poimandres.json +142 -0
- package/src/modes/theme/defaults/dark-rainforest.json +91 -0
- package/src/modes/theme/defaults/dark-reef.json +91 -0
- package/src/modes/theme/defaults/dark-retro.json +92 -0
- package/src/modes/theme/defaults/dark-rose-pine.json +96 -0
- package/src/modes/theme/defaults/dark-sakura.json +95 -0
- package/src/modes/theme/defaults/dark-slate.json +95 -0
- package/src/modes/theme/defaults/dark-solarized.json +97 -0
- package/src/modes/theme/defaults/dark-solstice.json +90 -0
- package/src/modes/theme/defaults/dark-starfall.json +91 -0
- package/src/modes/theme/defaults/dark-sunset.json +99 -0
- package/src/modes/theme/defaults/dark-swamp.json +90 -0
- package/src/modes/theme/defaults/dark-synthwave.json +103 -0
- package/src/modes/theme/defaults/dark-taiga.json +91 -0
- package/src/modes/theme/defaults/dark-terminal.json +95 -0
- package/src/modes/theme/defaults/dark-tokyo-night.json +101 -0
- package/src/modes/theme/defaults/dark-tundra.json +91 -0
- package/src/modes/theme/defaults/dark-twilight.json +91 -0
- package/src/modes/theme/defaults/dark-volcanic.json +91 -0
- package/src/modes/theme/defaults/graphite.json +92 -0
- package/src/modes/theme/defaults/index.ts +199 -0
- package/src/modes/theme/defaults/light-arctic.json +107 -0
- package/src/modes/theme/defaults/light-aurora-day.json +91 -0
- package/src/modes/theme/defaults/light-canyon.json +91 -0
- package/src/modes/theme/defaults/light-catppuccin.json +106 -0
- package/src/modes/theme/defaults/light-cirrus.json +90 -0
- package/src/modes/theme/defaults/light-coral.json +95 -0
- package/src/modes/theme/defaults/light-cyberpunk.json +96 -0
- package/src/modes/theme/defaults/light-dawn.json +90 -0
- package/src/modes/theme/defaults/light-dunes.json +91 -0
- package/src/modes/theme/defaults/light-eucalyptus.json +95 -0
- package/src/modes/theme/defaults/light-forest.json +100 -0
- package/src/modes/theme/defaults/light-frost.json +95 -0
- package/src/modes/theme/defaults/light-github.json +115 -0
- package/src/modes/theme/defaults/light-glacier.json +91 -0
- package/src/modes/theme/defaults/light-gruvbox.json +108 -0
- package/src/modes/theme/defaults/light-haze.json +90 -0
- package/src/modes/theme/defaults/light-honeycomb.json +95 -0
- package/src/modes/theme/defaults/light-lagoon.json +91 -0
- package/src/modes/theme/defaults/light-lavender.json +95 -0
- package/src/modes/theme/defaults/light-meadow.json +91 -0
- package/src/modes/theme/defaults/light-mint.json +95 -0
- package/src/modes/theme/defaults/light-monochrome.json +101 -0
- package/src/modes/theme/defaults/light-ocean.json +99 -0
- package/src/modes/theme/defaults/light-one.json +99 -0
- package/src/modes/theme/defaults/light-opal.json +91 -0
- package/src/modes/theme/defaults/light-orchard.json +91 -0
- package/src/modes/theme/defaults/light-paper.json +95 -0
- package/src/modes/theme/defaults/light-poimandres.json +142 -0
- package/src/modes/theme/defaults/light-prism.json +90 -0
- package/src/modes/theme/defaults/light-retro.json +98 -0
- package/src/modes/theme/defaults/light-sand.json +95 -0
- package/src/modes/theme/defaults/light-savanna.json +91 -0
- package/src/modes/theme/defaults/light-solarized.json +102 -0
- package/src/modes/theme/defaults/light-soleil.json +90 -0
- package/src/modes/theme/defaults/light-sunset.json +99 -0
- package/src/modes/theme/defaults/light-synthwave.json +98 -0
- package/src/modes/theme/defaults/light-tokyo-night.json +111 -0
- package/src/modes/theme/defaults/light-wetland.json +91 -0
- package/src/modes/theme/defaults/light-zenith.json +89 -0
- package/src/modes/theme/defaults/limestone.json +94 -0
- package/src/modes/theme/defaults/mahogany.json +97 -0
- package/src/modes/theme/defaults/marble.json +93 -0
- package/src/modes/theme/defaults/obsidian.json +91 -0
- package/src/modes/theme/defaults/onyx.json +91 -0
- package/src/modes/theme/defaults/pearl.json +93 -0
- package/src/modes/theme/defaults/porcelain.json +91 -0
- package/src/modes/theme/defaults/quartz.json +96 -0
- package/src/modes/theme/defaults/sandstone.json +95 -0
- package/src/modes/theme/defaults/titanium.json +90 -0
- package/src/modes/theme/light.json +93 -0
- package/src/modes/theme/mermaid-cache.ts +29 -0
- package/src/modes/theme/shimmer.ts +235 -0
- package/src/modes/theme/theme-schema.json +459 -0
- package/src/modes/theme/theme.ts +2676 -0
- package/src/modes/turn-budget.ts +31 -0
- package/src/modes/types.ts +359 -0
- package/src/modes/ultrathink.ts +41 -0
- package/src/modes/utils/context-usage.ts +339 -0
- package/src/modes/utils/copy-targets.ts +360 -0
- package/src/modes/utils/hotkeys-markdown.ts +61 -0
- package/src/modes/utils/keybinding-matchers.ts +51 -0
- package/src/modes/utils/tools-markdown.ts +27 -0
- package/src/modes/utils/ui-helpers.ts +801 -0
- package/src/modes/workflow.ts +42 -0
- package/src/plan-mode/approved-plan.ts +186 -0
- package/src/plan-mode/plan-handoff.ts +37 -0
- package/src/plan-mode/plan-protection.ts +31 -0
- package/src/plan-mode/state.ts +6 -0
- package/src/priority.json +41 -0
- package/src/prompts/agents/designer.md +66 -0
- package/src/prompts/agents/explore.md +58 -0
- package/src/prompts/agents/frontmatter.md +11 -0
- package/src/prompts/agents/init.md +33 -0
- package/src/prompts/agents/librarian.md +119 -0
- package/src/prompts/agents/oracle.md +55 -0
- package/src/prompts/agents/plan.md +48 -0
- package/src/prompts/agents/reviewer.md +140 -0
- package/src/prompts/agents/task.md +16 -0
- package/src/prompts/ci-green-request.md +36 -0
- package/src/prompts/dry-balance-bench.md +8 -0
- package/src/prompts/goals/goal-budget-limit.md +16 -0
- package/src/prompts/goals/goal-continuation.md +28 -0
- package/src/prompts/goals/goal-mode-active.md +23 -0
- package/src/prompts/memories/consolidation.md +30 -0
- package/src/prompts/memories/read-path.md +11 -0
- package/src/prompts/memories/stage_one_input.md +6 -0
- package/src/prompts/memories/stage_one_system.md +21 -0
- package/src/prompts/review-custom-request.md +22 -0
- package/src/prompts/review-headless-request.md +16 -0
- package/src/prompts/review-request.md +69 -0
- package/src/prompts/steering/user-interjection.md +10 -0
- package/src/prompts/system/agent-creation-architect.md +50 -0
- package/src/prompts/system/agent-creation-user.md +6 -0
- package/src/prompts/system/auto-continue.md +1 -0
- package/src/prompts/system/auto-thinking-difficulty-local.md +14 -0
- package/src/prompts/system/auto-thinking-difficulty.md +12 -0
- package/src/prompts/system/background-tan-dispatch.md +8 -0
- package/src/prompts/system/btw-user.md +8 -0
- package/src/prompts/system/commit-message-system.md +14 -0
- package/src/prompts/system/custom-system-prompt.md +64 -0
- package/src/prompts/system/eager-todo.md +13 -0
- package/src/prompts/system/empty-stop-retry.md +6 -0
- package/src/prompts/system/irc-incoming.md +7 -0
- package/src/prompts/system/manual-continue.md +7 -0
- package/src/prompts/system/memory-consolidation-system.md +8 -0
- package/src/prompts/system/memory-extraction-system.md +26 -0
- package/src/prompts/system/omfg-user.md +50 -0
- package/src/prompts/system/orchestrate-notice.md +40 -0
- package/src/prompts/system/plan-mode-active.md +109 -0
- package/src/prompts/system/plan-mode-approved.md +25 -0
- package/src/prompts/system/plan-mode-compact-instructions.md +16 -0
- package/src/prompts/system/plan-mode-reference.md +11 -0
- package/src/prompts/system/plan-mode-subagent.md +33 -0
- package/src/prompts/system/plan-mode-tool-decision-reminder.md +9 -0
- package/src/prompts/system/project-prompt.md +52 -0
- package/src/prompts/system/subagent-system-prompt.md +64 -0
- package/src/prompts/system/subagent-user-prompt.md +3 -0
- package/src/prompts/system/subagent-yield-reminder.md +12 -0
- package/src/prompts/system/system-prompt.md +258 -0
- package/src/prompts/system/tiny-title-system.md +8 -0
- package/src/prompts/system/title-system.md +16 -0
- package/src/prompts/system/ttsr-interrupt.md +7 -0
- package/src/prompts/system/ttsr-tool-reminder.md +5 -0
- package/src/prompts/system/ultrathink-notice.md +3 -0
- package/src/prompts/system/web-search.md +25 -0
- package/src/prompts/system/workflow-notice.md +70 -0
- package/src/prompts/tools/apply-patch.md +65 -0
- package/src/prompts/tools/ask.md +30 -0
- package/src/prompts/tools/ast-edit.md +39 -0
- package/src/prompts/tools/ast-grep.md +42 -0
- package/src/prompts/tools/async-result.md +8 -0
- package/src/prompts/tools/bash.md +46 -0
- package/src/prompts/tools/browser.md +73 -0
- package/src/prompts/tools/checkpoint.md +16 -0
- package/src/prompts/tools/debug.md +34 -0
- package/src/prompts/tools/eval.md +92 -0
- package/src/prompts/tools/find.md +36 -0
- package/src/prompts/tools/github.md +21 -0
- package/src/prompts/tools/goal.md +18 -0
- package/src/prompts/tools/image-gen.md +7 -0
- package/src/prompts/tools/inspect-image-system.md +20 -0
- package/src/prompts/tools/inspect-image.md +32 -0
- package/src/prompts/tools/irc.md +59 -0
- package/src/prompts/tools/job.md +19 -0
- package/src/prompts/tools/lsp-late-diagnostic.md +8 -0
- package/src/prompts/tools/lsp.md +42 -0
- package/src/prompts/tools/memory-edit.md +8 -0
- package/src/prompts/tools/patch.md +70 -0
- package/src/prompts/tools/read.md +84 -0
- package/src/prompts/tools/recall.md +5 -0
- package/src/prompts/tools/reflect.md +5 -0
- package/src/prompts/tools/render-mermaid.md +9 -0
- package/src/prompts/tools/replace.md +30 -0
- package/src/prompts/tools/resolve.md +9 -0
- package/src/prompts/tools/retain.md +6 -0
- package/src/prompts/tools/rewind.md +13 -0
- package/src/prompts/tools/search-tool-bm25.md +32 -0
- package/src/prompts/tools/search.md +24 -0
- package/src/prompts/tools/ssh.md +31 -0
- package/src/prompts/tools/task-summary.md +17 -0
- package/src/prompts/tools/task.md +88 -0
- package/src/prompts/tools/todo.md +62 -0
- package/src/prompts/tools/web-search.md +10 -0
- package/src/prompts/tools/write.md +14 -0
- package/src/registry/agent-lifecycle.ts +218 -0
- package/src/registry/agent-registry.ts +151 -0
- package/src/sdk.ts +2558 -0
- package/src/secrets/index.ts +123 -0
- package/src/secrets/obfuscator.ts +298 -0
- package/src/secrets/regex.ts +21 -0
- package/src/session/agent-session.ts +10121 -0
- package/src/session/agent-storage.ts +455 -0
- package/src/session/artifacts.ts +135 -0
- package/src/session/auth-broker-config.ts +131 -0
- package/src/session/auth-storage.ts +29 -0
- package/src/session/blob-store.ts +255 -0
- package/src/session/client-bridge.ts +85 -0
- package/src/session/history-storage.ts +348 -0
- package/src/session/indexed-session-storage.ts +430 -0
- package/src/session/messages.ts +541 -0
- package/src/session/redis-session-storage.ts +170 -0
- package/src/session/session-dump-format.ts +209 -0
- package/src/session/session-history-format.ts +246 -0
- package/src/session/session-manager.ts +3676 -0
- package/src/session/session-storage.ts +529 -0
- package/src/session/shake-types.ts +43 -0
- package/src/session/sql-session-storage.ts +314 -0
- package/src/session/streaming-output.ts +1330 -0
- package/src/session/tool-choice-queue.ts +213 -0
- package/src/session/yield-queue.ts +173 -0
- package/src/slash-commands/acp-builtins.ts +70 -0
- package/src/slash-commands/builtin-registry.ts +1798 -0
- package/src/slash-commands/helpers/context-report.ts +39 -0
- package/src/slash-commands/helpers/format.ts +46 -0
- package/src/slash-commands/helpers/marketplace-manager.ts +25 -0
- package/src/slash-commands/helpers/mcp.ts +532 -0
- package/src/slash-commands/helpers/parse.ts +85 -0
- package/src/slash-commands/helpers/ssh.ts +195 -0
- package/src/slash-commands/helpers/stats-dashboard.ts +85 -0
- package/src/slash-commands/helpers/todo.ts +279 -0
- package/src/slash-commands/helpers/usage-report.ts +95 -0
- package/src/slash-commands/marketplace-install-parser.ts +99 -0
- package/src/slash-commands/types.ts +135 -0
- package/src/ssh/config-writer.ts +183 -0
- package/src/ssh/connection-manager.ts +509 -0
- package/src/ssh/ssh-executor.ts +189 -0
- package/src/ssh/sshfs-mount.ts +140 -0
- package/src/ssh/utils.ts +8 -0
- package/src/stt/downloader.ts +71 -0
- package/src/stt/index.ts +3 -0
- package/src/stt/recorder.ts +351 -0
- package/src/stt/setup.ts +52 -0
- package/src/stt/stt-controller.ts +160 -0
- package/src/stt/transcribe.py +70 -0
- package/src/stt/transcriber.ts +91 -0
- package/src/stubs/natives/index.ts +814 -0
- package/src/stubs/natives/package.json +7 -0
- package/src/stubs/tui/index.ts +282 -0
- package/src/stubs/tui/package.json +7 -0
- package/src/system-prompt.ts +611 -0
- package/src/task/agents.ts +167 -0
- package/src/task/commands.ts +132 -0
- package/src/task/discovery.ts +122 -0
- package/src/task/executor.ts +2133 -0
- package/src/task/index.ts +1419 -0
- package/src/task/name-generator.ts +1577 -0
- package/src/task/omp-command.ts +26 -0
- package/src/task/output-manager.ts +88 -0
- package/src/task/parallel.ts +116 -0
- package/src/task/render.ts +1381 -0
- package/src/task/repair-args.ts +129 -0
- package/src/task/subprocess-tool-registry.ts +88 -0
- package/src/task/types.ts +336 -0
- package/src/task/worktree.ts +514 -0
- package/src/telemetry-export.ts +144 -0
- package/src/thinking.ts +167 -0
- package/src/tiny/compiled-runtime.ts +179 -0
- package/src/tiny/device.ts +111 -0
- package/src/tiny/dtype.ts +101 -0
- package/src/tiny/models.ts +242 -0
- package/src/tiny/text.ts +165 -0
- package/src/tiny/title-client.ts +543 -0
- package/src/tiny/title-protocol.ts +56 -0
- package/src/tiny/worker.ts +568 -0
- package/src/tool-discovery/mode.ts +24 -0
- package/src/tool-discovery/tool-index.ts +256 -0
- package/src/tools/approval.ts +189 -0
- package/src/tools/archive-reader.ts +721 -0
- package/src/tools/ask.ts +928 -0
- package/src/tools/ast-edit.ts +642 -0
- package/src/tools/ast-grep.ts +452 -0
- package/src/tools/auto-generated-guard.ts +322 -0
- package/src/tools/bash-command-fixup.ts +37 -0
- package/src/tools/bash-interactive.ts +408 -0
- package/src/tools/bash-interceptor.ts +67 -0
- package/src/tools/bash-pty-selection.ts +14 -0
- package/src/tools/bash-skill-urls.ts +248 -0
- package/src/tools/bash.ts +1386 -0
- package/src/tools/browser/attach.ts +175 -0
- package/src/tools/browser/launch.ts +660 -0
- package/src/tools/browser/readable.ts +112 -0
- package/src/tools/browser/registry.ts +197 -0
- package/src/tools/browser/render.ts +216 -0
- package/src/tools/browser/tab-protocol.ts +105 -0
- package/src/tools/browser/tab-supervisor.ts +628 -0
- package/src/tools/browser/tab-worker-entry.ts +21 -0
- package/src/tools/browser/tab-worker.ts +1226 -0
- package/src/tools/browser.ts +343 -0
- package/src/tools/checkpoint.ts +136 -0
- package/src/tools/conflict-detect.ts +718 -0
- package/src/tools/context.ts +39 -0
- package/src/tools/debug.ts +1067 -0
- package/src/tools/eval-backends.ts +27 -0
- package/src/tools/eval-render.ts +752 -0
- package/src/tools/eval.ts +577 -0
- package/src/tools/fetch.ts +1926 -0
- package/src/tools/file-recorder.ts +35 -0
- package/src/tools/find.ts +609 -0
- package/src/tools/fs-cache-invalidation.ts +28 -0
- package/src/tools/gh-cache-invalidation.ts +255 -0
- package/src/tools/gh-format.ts +12 -0
- package/src/tools/gh-renderer.ts +481 -0
- package/src/tools/gh.ts +3720 -0
- package/src/tools/github-cache.ts +637 -0
- package/src/tools/grouped-file-output.ts +210 -0
- package/src/tools/image-gen.ts +1517 -0
- package/src/tools/index.ts +599 -0
- package/src/tools/inspect-image-renderer.ts +132 -0
- package/src/tools/inspect-image.ts +174 -0
- package/src/tools/irc.ts +723 -0
- package/src/tools/job.ts +557 -0
- package/src/tools/json-tree.ts +243 -0
- package/src/tools/jtd-to-json-schema.ts +219 -0
- package/src/tools/jtd-to-typescript.ts +136 -0
- package/src/tools/jtd-utils.ts +102 -0
- package/src/tools/list-limit.ts +40 -0
- package/src/tools/match-line-format.ts +20 -0
- package/src/tools/memory-edit.ts +59 -0
- package/src/tools/memory-recall.ts +100 -0
- package/src/tools/memory-reflect.ts +88 -0
- package/src/tools/memory-render.ts +202 -0
- package/src/tools/memory-retain.ts +91 -0
- package/src/tools/output-meta.ts +754 -0
- package/src/tools/output-schema-validator.ts +132 -0
- package/src/tools/path-utils.ts +1054 -0
- package/src/tools/plan-mode-guard.ts +108 -0
- package/src/tools/puppeteer/00_stealth_tampering.txt +63 -0
- package/src/tools/puppeteer/01_stealth_activity.txt +20 -0
- package/src/tools/puppeteer/02_stealth_hairline.txt +11 -0
- package/src/tools/puppeteer/03_stealth_botd.txt +384 -0
- package/src/tools/puppeteer/04_stealth_iframe.txt +81 -0
- package/src/tools/puppeteer/05_stealth_webgl.txt +75 -0
- package/src/tools/puppeteer/06_stealth_screen.txt +72 -0
- package/src/tools/puppeteer/07_stealth_fonts.txt +97 -0
- package/src/tools/puppeteer/08_stealth_audio.txt +51 -0
- package/src/tools/puppeteer/09_stealth_locale.txt +46 -0
- package/src/tools/puppeteer/10_stealth_plugins.txt +206 -0
- package/src/tools/puppeteer/11_stealth_hardware.txt +8 -0
- package/src/tools/puppeteer/12_stealth_codecs.txt +40 -0
- package/src/tools/puppeteer/13_stealth_worker.txt +74 -0
- package/src/tools/read.ts +2929 -0
- package/src/tools/render-mermaid.ts +69 -0
- package/src/tools/render-utils.ts +838 -0
- package/src/tools/renderers.ts +77 -0
- package/src/tools/report-tool-issue.ts +534 -0
- package/src/tools/resolve.ts +276 -0
- package/src/tools/review.ts +253 -0
- package/src/tools/search-tool-bm25.ts +351 -0
- package/src/tools/search.ts +1580 -0
- package/src/tools/sqlite-reader.ts +828 -0
- package/src/tools/ssh.ts +349 -0
- package/src/tools/todo.ts +982 -0
- package/src/tools/tool-errors.ts +62 -0
- package/src/tools/tool-result.ts +94 -0
- package/src/tools/tool-timeouts.ts +30 -0
- package/src/tools/tts.ts +133 -0
- package/src/tools/write.ts +1217 -0
- package/src/tools/yield.ts +269 -0
- package/src/tui/code-cell.ts +216 -0
- package/src/tui/file-list.ts +55 -0
- package/src/tui/hyperlink.ts +175 -0
- package/src/tui/index.ts +12 -0
- package/src/tui/output-block.ts +240 -0
- package/src/tui/status-line.ts +54 -0
- package/src/tui/tree-list.ts +84 -0
- package/src/tui/types.ts +15 -0
- package/src/tui/utils.ts +103 -0
- package/src/utils/block-context.ts +312 -0
- package/src/utils/changelog.ts +132 -0
- package/src/utils/clipboard.ts +193 -0
- package/src/utils/command-args.ts +76 -0
- package/src/utils/commit-message-generator.ts +151 -0
- package/src/utils/edit-mode.ts +41 -0
- package/src/utils/enhanced-paste.ts +230 -0
- package/src/utils/event-bus.ts +33 -0
- package/src/utils/external-editor.ts +65 -0
- package/src/utils/file-display-mode.ts +45 -0
- package/src/utils/file-mentions.ts +281 -0
- package/src/utils/git.ts +1833 -0
- package/src/utils/image-loading.ts +132 -0
- package/src/utils/image-resize.ts +309 -0
- package/src/utils/jj.ts +248 -0
- package/src/utils/lang-from-path.ts +239 -0
- package/src/utils/markit.ts +89 -0
- package/src/utils/open.ts +55 -0
- package/src/utils/session-color.ts +68 -0
- package/src/utils/shell-snapshot.ts +187 -0
- package/src/utils/sixel.ts +69 -0
- package/src/utils/title-generator.ts +373 -0
- package/src/utils/tool-choice.ts +33 -0
- package/src/utils/tools-manager.ts +363 -0
- package/src/web/kagi.ts +305 -0
- package/src/web/parallel.ts +353 -0
- package/src/web/scrapers/artifacthub.ts +207 -0
- package/src/web/scrapers/arxiv.ts +83 -0
- package/src/web/scrapers/aur.ts +162 -0
- package/src/web/scrapers/biorxiv.ts +133 -0
- package/src/web/scrapers/bluesky.ts +262 -0
- package/src/web/scrapers/brew.ts +172 -0
- package/src/web/scrapers/cheatsh.ts +68 -0
- package/src/web/scrapers/chocolatey.ts +196 -0
- package/src/web/scrapers/choosealicense.ts +95 -0
- package/src/web/scrapers/cisa-kev.ts +87 -0
- package/src/web/scrapers/clojars.ts +154 -0
- package/src/web/scrapers/coingecko.ts +177 -0
- package/src/web/scrapers/crates-io.ts +97 -0
- package/src/web/scrapers/crossref.ts +136 -0
- package/src/web/scrapers/devto.ts +147 -0
- package/src/web/scrapers/discogs.ts +306 -0
- package/src/web/scrapers/discourse.ts +197 -0
- package/src/web/scrapers/dockerhub.ts +138 -0
- package/src/web/scrapers/docs-rs.ts +653 -0
- package/src/web/scrapers/fdroid.ts +134 -0
- package/src/web/scrapers/firefox-addons.ts +191 -0
- package/src/web/scrapers/flathub.ts +223 -0
- package/src/web/scrapers/github-gist.ts +58 -0
- package/src/web/scrapers/github.ts +704 -0
- package/src/web/scrapers/gitlab.ts +401 -0
- package/src/web/scrapers/go-pkg.ts +266 -0
- package/src/web/scrapers/hackage.ts +140 -0
- package/src/web/scrapers/hackernews.ts +189 -0
- package/src/web/scrapers/hex.ts +105 -0
- package/src/web/scrapers/huggingface.ts +321 -0
- package/src/web/scrapers/iacr.ts +89 -0
- package/src/web/scrapers/index.ts +252 -0
- package/src/web/scrapers/jetbrains-marketplace.ts +159 -0
- package/src/web/scrapers/lemmy.ts +203 -0
- package/src/web/scrapers/lobsters.ts +175 -0
- package/src/web/scrapers/mastodon.ts +292 -0
- package/src/web/scrapers/maven.ts +138 -0
- package/src/web/scrapers/mdn.ts +173 -0
- package/src/web/scrapers/metacpan.ts +222 -0
- package/src/web/scrapers/musicbrainz.ts +250 -0
- package/src/web/scrapers/npm.ts +98 -0
- package/src/web/scrapers/nuget.ts +183 -0
- package/src/web/scrapers/nvd.ts +222 -0
- package/src/web/scrapers/ollama.ts +239 -0
- package/src/web/scrapers/open-vsx.ts +106 -0
- package/src/web/scrapers/opencorporates.ts +292 -0
- package/src/web/scrapers/openlibrary.ts +336 -0
- package/src/web/scrapers/orcid.ts +286 -0
- package/src/web/scrapers/osv.ts +176 -0
- package/src/web/scrapers/packagist.ts +160 -0
- package/src/web/scrapers/pub-dev.ts +143 -0
- package/src/web/scrapers/pubmed.ts +211 -0
- package/src/web/scrapers/pypi.ts +112 -0
- package/src/web/scrapers/rawg.ts +110 -0
- package/src/web/scrapers/readthedocs.ts +120 -0
- package/src/web/scrapers/reddit.ts +95 -0
- package/src/web/scrapers/repology.ts +251 -0
- package/src/web/scrapers/rfc.ts +201 -0
- package/src/web/scrapers/rubygems.ts +103 -0
- package/src/web/scrapers/searchcode.ts +189 -0
- package/src/web/scrapers/sec-edgar.ts +261 -0
- package/src/web/scrapers/semantic-scholar.ts +171 -0
- package/src/web/scrapers/snapcraft.ts +187 -0
- package/src/web/scrapers/sourcegraph.ts +336 -0
- package/src/web/scrapers/spdx.ts +108 -0
- package/src/web/scrapers/spotify.ts +198 -0
- package/src/web/scrapers/stackoverflow.ts +120 -0
- package/src/web/scrapers/terraform.ts +277 -0
- package/src/web/scrapers/tldr.ts +47 -0
- package/src/web/scrapers/twitter.ts +94 -0
- package/src/web/scrapers/types.ts +397 -0
- package/src/web/scrapers/utils.ts +109 -0
- package/src/web/scrapers/vimeo.ts +133 -0
- package/src/web/scrapers/vscode-marketplace.ts +187 -0
- package/src/web/scrapers/w3c.ts +156 -0
- package/src/web/scrapers/wikidata.ts +344 -0
- package/src/web/scrapers/wikipedia.ts +84 -0
- package/src/web/scrapers/youtube.ts +325 -0
- package/src/web/search/index.ts +292 -0
- package/src/web/search/provider.ts +157 -0
- package/src/web/search/providers/anthropic.ts +318 -0
- package/src/web/search/providers/base.ts +89 -0
- package/src/web/search/providers/brave.ts +152 -0
- package/src/web/search/providers/codex.ts +591 -0
- package/src/web/search/providers/exa.ts +400 -0
- package/src/web/search/providers/gemini.ts +460 -0
- package/src/web/search/providers/jina.ts +111 -0
- package/src/web/search/providers/kagi.ts +86 -0
- package/src/web/search/providers/kimi.ts +196 -0
- package/src/web/search/providers/parallel.ts +225 -0
- package/src/web/search/providers/perplexity.ts +730 -0
- package/src/web/search/providers/searxng.ts +313 -0
- package/src/web/search/providers/synthetic.ts +114 -0
- package/src/web/search/providers/tavily.ts +176 -0
- package/src/web/search/providers/utils.ts +128 -0
- package/src/web/search/providers/zai.ts +333 -0
- package/src/web/search/render.ts +262 -0
- package/src/web/search/types.ts +482 -0
- package/src/web/search/utils.ts +17 -0
- package/src/workspace-tree.ts +286 -0
|
@@ -0,0 +1,2017 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Command Controller
|
|
3
|
+
*
|
|
4
|
+
* Handles /mcp subcommands for managing MCP servers.
|
|
5
|
+
*/
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import { type Component, replaceTabs, Spacer, Text } from './stubs/tui/index.ts';
|
|
8
|
+
import { getMCPConfigPath, getProjectDir } from "@oh-my-pi/pi-utils";
|
|
9
|
+
import type { SourceMeta } from "../../capability/types";
|
|
10
|
+
import { analyzeAuthError, discoverOAuthEndpoints, MCPManager } from "../../mcp";
|
|
11
|
+
import { connectToServer, disconnectServer, listTools } from "../../mcp/client";
|
|
12
|
+
import {
|
|
13
|
+
addMCPServer,
|
|
14
|
+
readDisabledServers,
|
|
15
|
+
readMCPConfigFile,
|
|
16
|
+
removeMCPServer,
|
|
17
|
+
setServerDisabled,
|
|
18
|
+
updateMCPServer,
|
|
19
|
+
} from "../../mcp/config-writer";
|
|
20
|
+
import { MCPOAuthFlow } from "../../mcp/oauth-flow";
|
|
21
|
+
import {
|
|
22
|
+
clearSmitheryApiKey,
|
|
23
|
+
createSmitheryCliAuthSession,
|
|
24
|
+
getSmitheryApiKey,
|
|
25
|
+
getSmitheryLoginUrl,
|
|
26
|
+
pollSmitheryCliAuthSession,
|
|
27
|
+
saveSmitheryApiKey,
|
|
28
|
+
} from "../../mcp/smithery-auth";
|
|
29
|
+
import { SmitheryConnectError } from "../../mcp/smithery-connect";
|
|
30
|
+
import {
|
|
31
|
+
SmitheryRegistryError,
|
|
32
|
+
type SmitherySearchResult,
|
|
33
|
+
searchSmitheryRegistry,
|
|
34
|
+
toConfigName,
|
|
35
|
+
} from "../../mcp/smithery-registry";
|
|
36
|
+
import type { MCPAuthConfig, MCPServerConfig, MCPServerConnection } from "../../mcp/types";
|
|
37
|
+
import type { OAuthCredential } from "../../session/auth-storage";
|
|
38
|
+
import { shortenPath } from "../../tools/render-utils";
|
|
39
|
+
import { urlHyperlinkAlways } from "../../tui";
|
|
40
|
+
import { openPath } from "../../utils/open";
|
|
41
|
+
import { ChatBlock } from "../components/chat-block";
|
|
42
|
+
import { MCPAddWizard } from "../components/mcp-add-wizard";
|
|
43
|
+
import { TranscriptBlock } from "../components/transcript-container";
|
|
44
|
+
import { parseCommandArgs } from "../shared";
|
|
45
|
+
import { theme } from "../theme/theme";
|
|
46
|
+
import type { InteractiveModeContext } from "../types";
|
|
47
|
+
import { groupBySource, parseRemoveArgs, readScopeFlag, showCommandMessage } from "./command-controller-shared";
|
|
48
|
+
|
|
49
|
+
const MCP_MANUAL_INPUT_PROVIDER_ID = "mcp";
|
|
50
|
+
const MCP_MANUAL_LOGIN_TIP = "Headless? Paste the redirect URL or code with /login <value>.";
|
|
51
|
+
function withTimeout<T>(promise: Promise<T>, timeoutMs: number, message: string, onTimeout?: () => void): Promise<T> {
|
|
52
|
+
const { promise: timeoutPromise, reject } = Promise.withResolvers<T>();
|
|
53
|
+
const timer = setTimeout(() => {
|
|
54
|
+
onTimeout?.();
|
|
55
|
+
reject(new Error(message));
|
|
56
|
+
}, timeoutMs);
|
|
57
|
+
return Promise.race([promise, timeoutPromise]).finally(() => clearTimeout(timer));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Renders the MCP OAuth fallback URL without hard-wrapping the copy target. */
|
|
61
|
+
export class MCPAuthorizationLinkPrompt implements Component {
|
|
62
|
+
readonly #url: string;
|
|
63
|
+
|
|
64
|
+
constructor(url: string) {
|
|
65
|
+
this.#url = url;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
invalidate(): void {}
|
|
69
|
+
|
|
70
|
+
render(_width: number): readonly string[] {
|
|
71
|
+
const link = urlHyperlinkAlways(this.#url, "Click here to authorize");
|
|
72
|
+
return [
|
|
73
|
+
` ${theme.fg("success", "Open authorization URL:")}`,
|
|
74
|
+
` ${theme.fg("accent", link)}`,
|
|
75
|
+
` ${theme.fg("muted", `Copy URL: ${replaceTabs(this.#url)}`)}`,
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Animated "Connecting to …" transcript block. Owns its spinner interval: it
|
|
82
|
+
* starts on mount and is cleared on {@link ChatBlock.finish}/dispose, so callers
|
|
83
|
+
* never juggle `setInterval`/`clearInterval` or `requestRender` by hand.
|
|
84
|
+
*/
|
|
85
|
+
class McpConnectingBlock extends ChatBlock {
|
|
86
|
+
readonly #text: Text;
|
|
87
|
+
|
|
88
|
+
constructor(private readonly serverName: string) {
|
|
89
|
+
super();
|
|
90
|
+
this.addChild(new Spacer(1));
|
|
91
|
+
const frame = theme.spinnerFrames[0] ?? "|";
|
|
92
|
+
this.#text = new Text(theme.fg("muted", `${frame} Connecting to "${serverName}"...`), 1, 0);
|
|
93
|
+
this.addChild(this.#text);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
protected override onMount(): void {
|
|
97
|
+
const frames = theme.spinnerFrames;
|
|
98
|
+
let frame = 0;
|
|
99
|
+
const interval = setInterval(() => {
|
|
100
|
+
frame++;
|
|
101
|
+
this.#text.setText(
|
|
102
|
+
theme.fg("muted", `${frames[frame % frames.length] ?? "|"} Connecting to "${this.serverName}"...`),
|
|
103
|
+
);
|
|
104
|
+
this.requestRender();
|
|
105
|
+
}, 80);
|
|
106
|
+
this.onCleanup(() => clearInterval(interval));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Replace the spinner line with a terminal status; pair with {@link finish}. */
|
|
110
|
+
setStatus(text: string): void {
|
|
111
|
+
this.#text.setText(text);
|
|
112
|
+
this.requestRender();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Outcome of {@link MCPCommandController}'s OAuth handler.
|
|
118
|
+
*
|
|
119
|
+
* `clientId`/`clientSecret` are populated when the OAuth provider required (or
|
|
120
|
+
* accepted) dynamic client registration; callers MUST persist them alongside
|
|
121
|
+
* `credentialId` so subsequent token refreshes and reauthorizations can reuse
|
|
122
|
+
* the same registered client. Both are also set when the caller pre-supplied a
|
|
123
|
+
* client id via the wizard or `oauth.clientId` in `mcp.json`, in which case the
|
|
124
|
+
* write-back is a no-op.
|
|
125
|
+
*/
|
|
126
|
+
interface OAuthFlowResult {
|
|
127
|
+
credentialId: string;
|
|
128
|
+
clientId?: string;
|
|
129
|
+
clientSecret?: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
type MCPAddScope = "user" | "project";
|
|
133
|
+
type MCPAddTransport = "http" | "sse";
|
|
134
|
+
|
|
135
|
+
type MCPAddParsed = {
|
|
136
|
+
initialName?: string;
|
|
137
|
+
scope: MCPAddScope;
|
|
138
|
+
quickConfig?: MCPServerConfig;
|
|
139
|
+
isCommandQuickAdd?: boolean;
|
|
140
|
+
hasAuthToken?: boolean;
|
|
141
|
+
error?: string;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
type MCPSearchParsed = {
|
|
145
|
+
keyword: string;
|
|
146
|
+
scope: MCPAddScope;
|
|
147
|
+
limit: number;
|
|
148
|
+
semantic: boolean;
|
|
149
|
+
error?: string;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export class MCPCommandController {
|
|
153
|
+
constructor(private ctx: InteractiveModeContext) {}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Handle /mcp command and route to subcommands
|
|
157
|
+
*/
|
|
158
|
+
async handle(text: string): Promise<void> {
|
|
159
|
+
const parts = text.trim().split(/\s+/);
|
|
160
|
+
const subcommand = parts[1]?.toLowerCase();
|
|
161
|
+
|
|
162
|
+
if (!subcommand || subcommand === "help") {
|
|
163
|
+
this.#showHelp();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
switch (subcommand) {
|
|
168
|
+
case "add":
|
|
169
|
+
await this.#handleAdd(text);
|
|
170
|
+
break;
|
|
171
|
+
case "list":
|
|
172
|
+
await this.#handleList();
|
|
173
|
+
break;
|
|
174
|
+
case "remove":
|
|
175
|
+
case "rm":
|
|
176
|
+
await this.#handleRemove(text);
|
|
177
|
+
break;
|
|
178
|
+
case "test":
|
|
179
|
+
await this.#handleTest(parts[2]);
|
|
180
|
+
break;
|
|
181
|
+
case "reauth":
|
|
182
|
+
await this.#handleReauth(parts[2]);
|
|
183
|
+
break;
|
|
184
|
+
case "unauth":
|
|
185
|
+
await this.#handleUnauth(parts[2]);
|
|
186
|
+
break;
|
|
187
|
+
case "enable":
|
|
188
|
+
await this.#handleSetEnabled(parts[2], true);
|
|
189
|
+
break;
|
|
190
|
+
case "disable":
|
|
191
|
+
await this.#handleSetEnabled(parts[2], false);
|
|
192
|
+
break;
|
|
193
|
+
case "resources":
|
|
194
|
+
await this.#handleResources();
|
|
195
|
+
break;
|
|
196
|
+
case "prompts":
|
|
197
|
+
await this.#handlePrompts();
|
|
198
|
+
break;
|
|
199
|
+
case "notifications":
|
|
200
|
+
await this.#handleNotifications();
|
|
201
|
+
break;
|
|
202
|
+
case "smithery-search":
|
|
203
|
+
await this.#handleSearch(text);
|
|
204
|
+
break;
|
|
205
|
+
case "smithery-login":
|
|
206
|
+
await this.#handleSmitheryLogin();
|
|
207
|
+
break;
|
|
208
|
+
case "smithery-logout":
|
|
209
|
+
await this.#handleSmitheryLogout();
|
|
210
|
+
break;
|
|
211
|
+
case "reconnect":
|
|
212
|
+
await this.#handleReconnect(parts[2]);
|
|
213
|
+
break;
|
|
214
|
+
case "reload":
|
|
215
|
+
await this.#handleReload();
|
|
216
|
+
break;
|
|
217
|
+
default:
|
|
218
|
+
this.ctx.showError(`Unknown subcommand: ${subcommand}. Type /mcp help for usage.`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Show help text
|
|
224
|
+
*/
|
|
225
|
+
#showHelp(): void {
|
|
226
|
+
const helpText = [
|
|
227
|
+
"",
|
|
228
|
+
theme.bold("MCP Server Management"),
|
|
229
|
+
"",
|
|
230
|
+
"Manage Model Context Protocol (MCP) servers for external tool integrations.",
|
|
231
|
+
"",
|
|
232
|
+
theme.fg("accent", "Commands:"),
|
|
233
|
+
" /mcp add Add a new MCP server (interactive wizard)",
|
|
234
|
+
" /mcp add <name> [--scope project|user] [--url <url> --transport http|sse] [--token <token>] [-- <command...>]",
|
|
235
|
+
" /mcp list List all configured MCP servers",
|
|
236
|
+
" /mcp remove <name> [--scope project|user] Remove an MCP server (default: project)",
|
|
237
|
+
" /mcp test <name> Test connection to an MCP server",
|
|
238
|
+
" /mcp reauth <name> Reauthorize OAuth for an MCP server",
|
|
239
|
+
" /mcp unauth <name> Remove OAuth auth from an MCP server",
|
|
240
|
+
" /mcp enable <name> Enable an MCP server",
|
|
241
|
+
" /mcp disable <name> Disable an MCP server",
|
|
242
|
+
" /mcp smithery-search <keyword> [--scope project|user] [--limit <1-100>] [--semantic]",
|
|
243
|
+
" Search Smithery registry and deploy from picker",
|
|
244
|
+
" /mcp smithery-login Login to Smithery and cache API key",
|
|
245
|
+
" /mcp smithery-logout Remove cached Smithery API key",
|
|
246
|
+
" /mcp reconnect <name> Reconnect to a specific MCP server",
|
|
247
|
+
" /mcp reload Force reload and rediscover MCP runtime tools",
|
|
248
|
+
" /mcp resources List available resources from connected servers",
|
|
249
|
+
" /mcp prompts List available prompts from connected servers",
|
|
250
|
+
" /mcp notifications Show notification capabilities and subscription state",
|
|
251
|
+
" /mcp help Show this help message",
|
|
252
|
+
"",
|
|
253
|
+
].join("\n");
|
|
254
|
+
|
|
255
|
+
this.#showMessage(helpText);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
#parseAddCommand(text: string): MCPAddParsed {
|
|
259
|
+
const prefixMatch = text.match(/^\/mcp\s+add\b\s*(.*)$/i);
|
|
260
|
+
const rest = prefixMatch?.[1]?.trim() ?? "";
|
|
261
|
+
if (!rest) {
|
|
262
|
+
return { scope: "project" };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const tokens = parseCommandArgs(rest);
|
|
266
|
+
if (tokens.length === 0) {
|
|
267
|
+
return { scope: "project" };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
let name: string | undefined;
|
|
271
|
+
let scope: MCPAddScope = "project";
|
|
272
|
+
let url: string | undefined;
|
|
273
|
+
let transport: MCPAddTransport = "http";
|
|
274
|
+
let authToken: string | undefined;
|
|
275
|
+
let commandTokens: string[] | undefined;
|
|
276
|
+
|
|
277
|
+
let i = 0;
|
|
278
|
+
if (!tokens[0].startsWith("-")) {
|
|
279
|
+
name = tokens[0];
|
|
280
|
+
i = 1;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
while (i < tokens.length) {
|
|
284
|
+
const argToken = tokens[i];
|
|
285
|
+
if (argToken === "--") {
|
|
286
|
+
commandTokens = tokens.slice(i + 1);
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
if (argToken === "--scope") {
|
|
290
|
+
const r = readScopeFlag(tokens[i + 1]);
|
|
291
|
+
if (!r.ok) {
|
|
292
|
+
return { scope, error: r.error };
|
|
293
|
+
}
|
|
294
|
+
scope = r.scope;
|
|
295
|
+
i += 2;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (argToken === "--url") {
|
|
299
|
+
const value = tokens[i + 1];
|
|
300
|
+
if (!value) {
|
|
301
|
+
return { scope, error: "Missing value for --url." };
|
|
302
|
+
}
|
|
303
|
+
url = value;
|
|
304
|
+
i += 2;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (argToken === "--transport") {
|
|
308
|
+
const value = tokens[i + 1];
|
|
309
|
+
if (!value || (value !== "http" && value !== "sse")) {
|
|
310
|
+
return { scope, error: "Invalid --transport value. Use http or sse." };
|
|
311
|
+
}
|
|
312
|
+
transport = value;
|
|
313
|
+
i += 2;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
if (argToken === "--token") {
|
|
317
|
+
const value = tokens[i + 1];
|
|
318
|
+
if (!value) {
|
|
319
|
+
return { scope, error: "Missing value for --token." };
|
|
320
|
+
}
|
|
321
|
+
authToken = value;
|
|
322
|
+
i += 2;
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
return { scope, error: `Unknown option: ${argToken}` };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const hasQuick = Boolean(url) || Boolean(commandTokens && commandTokens.length > 0);
|
|
329
|
+
if (!hasQuick) {
|
|
330
|
+
return { scope, initialName: name };
|
|
331
|
+
}
|
|
332
|
+
if (!name) {
|
|
333
|
+
return { scope, error: "Server name required for quick add. Usage: /mcp add <name> ..." };
|
|
334
|
+
}
|
|
335
|
+
if (url && commandTokens && commandTokens.length > 0) {
|
|
336
|
+
return { scope, error: "Use either --url or -- <command...>, not both." };
|
|
337
|
+
}
|
|
338
|
+
if (authToken && !url) {
|
|
339
|
+
return { scope, error: "--token requires --url (HTTP/SSE transport)." };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (commandTokens && commandTokens.length > 0) {
|
|
343
|
+
const [command, ...args] = commandTokens;
|
|
344
|
+
const config: MCPServerConfig = {
|
|
345
|
+
type: "stdio",
|
|
346
|
+
command,
|
|
347
|
+
args: args.length > 0 ? args : undefined,
|
|
348
|
+
};
|
|
349
|
+
return { scope, initialName: name, quickConfig: config, isCommandQuickAdd: true };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const useHttpTransport = transport === "http";
|
|
353
|
+
let normalizedUrl = url!;
|
|
354
|
+
if (!/^https?:\/\//i.test(normalizedUrl)) {
|
|
355
|
+
normalizedUrl = `https://${normalizedUrl}`;
|
|
356
|
+
}
|
|
357
|
+
const config: MCPServerConfig = {
|
|
358
|
+
type: useHttpTransport ? "http" : "sse",
|
|
359
|
+
url: normalizedUrl,
|
|
360
|
+
headers: authToken ? { Authorization: `Bearer ${authToken}` } : undefined,
|
|
361
|
+
};
|
|
362
|
+
return {
|
|
363
|
+
scope,
|
|
364
|
+
initialName: name,
|
|
365
|
+
quickConfig: config,
|
|
366
|
+
isCommandQuickAdd: false,
|
|
367
|
+
hasAuthToken: Boolean(authToken),
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
#parseSearchCommand(text: string): MCPSearchParsed {
|
|
372
|
+
const prefixMatch = text.match(/^\/mcp\s+smithery-search\b\s*(.*)$/i);
|
|
373
|
+
const rest = prefixMatch?.[1]?.trim() ?? "";
|
|
374
|
+
const tokens = parseCommandArgs(rest);
|
|
375
|
+
if (tokens.length === 0) {
|
|
376
|
+
return {
|
|
377
|
+
keyword: "",
|
|
378
|
+
scope: "project",
|
|
379
|
+
limit: 20,
|
|
380
|
+
semantic: false,
|
|
381
|
+
error: "Keyword required. Usage: /mcp smithery-search <keyword> [--scope project|user] [--limit <1-100>] [--semantic]",
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const keywordParts: string[] = [];
|
|
386
|
+
let scope: MCPAddScope = "project";
|
|
387
|
+
let limit = 20;
|
|
388
|
+
let semantic = false;
|
|
389
|
+
|
|
390
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
391
|
+
const token = tokens[i];
|
|
392
|
+
if (token === "--scope") {
|
|
393
|
+
const value = tokens[i + 1];
|
|
394
|
+
if (!value || (value !== "project" && value !== "user")) {
|
|
395
|
+
return { keyword: "", scope, limit, semantic, error: "Invalid --scope value. Use project or user." };
|
|
396
|
+
}
|
|
397
|
+
scope = value;
|
|
398
|
+
i++;
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
if (token === "--limit") {
|
|
402
|
+
const value = tokens[i + 1];
|
|
403
|
+
if (!value) {
|
|
404
|
+
return { keyword: "", scope, limit, semantic, error: "Missing value for --limit." };
|
|
405
|
+
}
|
|
406
|
+
const parsed = Number(value);
|
|
407
|
+
if (!Number.isInteger(parsed) || parsed < 1 || parsed > 100) {
|
|
408
|
+
return {
|
|
409
|
+
keyword: "",
|
|
410
|
+
scope,
|
|
411
|
+
limit,
|
|
412
|
+
semantic,
|
|
413
|
+
error: "Invalid --limit value. Use an integer between 1 and 100.",
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
limit = parsed;
|
|
417
|
+
i++;
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
if (token === "--semantic") {
|
|
421
|
+
semantic = true;
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
if (token.startsWith("--")) {
|
|
425
|
+
return { keyword: "", scope, limit, semantic, error: `Unknown option: ${token}` };
|
|
426
|
+
}
|
|
427
|
+
keywordParts.push(token);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const keyword = keywordParts.join(" ").trim();
|
|
431
|
+
if (!keyword) {
|
|
432
|
+
return {
|
|
433
|
+
keyword: "",
|
|
434
|
+
scope,
|
|
435
|
+
limit,
|
|
436
|
+
semantic,
|
|
437
|
+
error: "Keyword required. Usage: /mcp smithery-search <keyword> [--scope project|user] [--limit <1-100>] [--semantic]",
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return { keyword, scope, limit, semantic };
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Handle /mcp add - Launch interactive wizard or quick-add from args
|
|
446
|
+
*/
|
|
447
|
+
async #handleAdd(text: string): Promise<void> {
|
|
448
|
+
const parsed = this.#parseAddCommand(text);
|
|
449
|
+
if (parsed.error) {
|
|
450
|
+
this.ctx.showError(parsed.error);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (parsed.quickConfig && parsed.initialName) {
|
|
454
|
+
let finalConfig = parsed.quickConfig;
|
|
455
|
+
|
|
456
|
+
// Quick-add with URL should still perform auth detection and OAuth flow,
|
|
457
|
+
// matching wizard behavior. Command quick-add intentionally skips this.
|
|
458
|
+
if (!parsed.isCommandQuickAdd && (finalConfig.type === "http" || finalConfig.type === "sse")) {
|
|
459
|
+
try {
|
|
460
|
+
await this.#handleTestConnection(finalConfig);
|
|
461
|
+
} catch (error) {
|
|
462
|
+
if (parsed.hasAuthToken) {
|
|
463
|
+
this.ctx.showError(
|
|
464
|
+
`Authentication failed for "${parsed.initialName}": ${error instanceof Error ? error.message : String(error)}`,
|
|
465
|
+
);
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
const authResult = analyzeAuthError(error as Error, finalConfig.url);
|
|
469
|
+
if (authResult.requiresAuth) {
|
|
470
|
+
let oauth = authResult.authType === "oauth" ? (authResult.oauth ?? null) : null;
|
|
471
|
+
if (!oauth && finalConfig.url) {
|
|
472
|
+
try {
|
|
473
|
+
oauth = await discoverOAuthEndpoints(
|
|
474
|
+
finalConfig.url,
|
|
475
|
+
authResult.authServerUrl,
|
|
476
|
+
authResult.resourceMetadataUrl,
|
|
477
|
+
);
|
|
478
|
+
} catch {
|
|
479
|
+
// Ignore discovery error and handle below.
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (!oauth) {
|
|
484
|
+
this.ctx.showError(
|
|
485
|
+
`Authentication required for "${parsed.initialName}", but OAuth endpoints could not be discovered. ` +
|
|
486
|
+
`Use /mcp add ${parsed.initialName} (wizard) or configure auth manually.`,
|
|
487
|
+
);
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
try {
|
|
492
|
+
const oauthClientSecret = finalConfig.oauth?.clientSecret ?? "";
|
|
493
|
+
const oauthResult = await this.#handleOAuthFlow(
|
|
494
|
+
oauth.authorizationUrl,
|
|
495
|
+
oauth.tokenUrl,
|
|
496
|
+
oauth.clientId ?? finalConfig.oauth?.clientId ?? "",
|
|
497
|
+
oauthClientSecret,
|
|
498
|
+
oauth.scopes ?? "",
|
|
499
|
+
finalConfig.oauth?.callbackPort,
|
|
500
|
+
finalConfig.oauth?.callbackPath,
|
|
501
|
+
finalConfig.oauth?.redirectUri,
|
|
502
|
+
);
|
|
503
|
+
const persistedClientId = oauthResult.clientId ?? oauth.clientId ?? finalConfig.oauth?.clientId;
|
|
504
|
+
const persistedClientSecret = oauthResult.clientSecret ?? finalConfig.oauth?.clientSecret;
|
|
505
|
+
finalConfig = {
|
|
506
|
+
...finalConfig,
|
|
507
|
+
auth: {
|
|
508
|
+
type: "oauth",
|
|
509
|
+
credentialId: oauthResult.credentialId,
|
|
510
|
+
tokenUrl: oauth.tokenUrl,
|
|
511
|
+
clientId: persistedClientId,
|
|
512
|
+
clientSecret: persistedClientSecret,
|
|
513
|
+
},
|
|
514
|
+
oauth: {
|
|
515
|
+
...finalConfig.oauth,
|
|
516
|
+
clientId: persistedClientId ?? finalConfig.oauth?.clientId,
|
|
517
|
+
clientSecret: persistedClientSecret ?? finalConfig.oauth?.clientSecret,
|
|
518
|
+
},
|
|
519
|
+
};
|
|
520
|
+
} catch (oauthError) {
|
|
521
|
+
this.ctx.showError(
|
|
522
|
+
`OAuth flow failed for "${parsed.initialName}": ${oauthError instanceof Error ? oauthError.message : String(oauthError)}`,
|
|
523
|
+
);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
await this.#handleWizardComplete(parsed.initialName, finalConfig, parsed.scope);
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Save current editor state
|
|
535
|
+
const done = () => {
|
|
536
|
+
this.ctx.editorContainer.clear();
|
|
537
|
+
this.ctx.editorContainer.addChild(this.ctx.editor);
|
|
538
|
+
this.ctx.ui.setFocus(this.ctx.editor);
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
// Create wizard with OAuth handler and connection test
|
|
542
|
+
const wizard = new MCPAddWizard(
|
|
543
|
+
async (name: string, config: MCPServerConfig, scope: "user" | "project") => {
|
|
544
|
+
done();
|
|
545
|
+
await this.#handleWizardComplete(name, config, scope);
|
|
546
|
+
},
|
|
547
|
+
() => {
|
|
548
|
+
done();
|
|
549
|
+
this.#handleWizardCancel();
|
|
550
|
+
},
|
|
551
|
+
async (authUrl: string, tokenUrl: string, clientId: string, clientSecret: string, scopes: string) => {
|
|
552
|
+
return await this.#handleOAuthFlow(authUrl, tokenUrl, clientId, clientSecret, scopes);
|
|
553
|
+
},
|
|
554
|
+
async (config: MCPServerConfig) => {
|
|
555
|
+
return await this.#handleTestConnection(config);
|
|
556
|
+
},
|
|
557
|
+
() => {
|
|
558
|
+
this.ctx.ui.requestRender();
|
|
559
|
+
},
|
|
560
|
+
parsed.initialName,
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
// Replace editor with wizard
|
|
564
|
+
this.ctx.editorContainer.clear();
|
|
565
|
+
this.ctx.editorContainer.addChild(wizard);
|
|
566
|
+
this.ctx.ui.setFocus(wizard);
|
|
567
|
+
this.ctx.ui.requestRender();
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Handle OAuth authentication flow for MCP server
|
|
572
|
+
*/
|
|
573
|
+
async #handleOAuthFlow(
|
|
574
|
+
authUrl: string,
|
|
575
|
+
tokenUrl: string,
|
|
576
|
+
clientId: string,
|
|
577
|
+
clientSecret: string,
|
|
578
|
+
scopes: string,
|
|
579
|
+
callbackPort?: number,
|
|
580
|
+
callbackPath?: string,
|
|
581
|
+
redirectUri?: string,
|
|
582
|
+
): Promise<OAuthFlowResult> {
|
|
583
|
+
const authStorage = this.ctx.session.modelRegistry.authStorage;
|
|
584
|
+
let parsedAuthUrl: URL;
|
|
585
|
+
|
|
586
|
+
// Validate OAuth URLs
|
|
587
|
+
try {
|
|
588
|
+
parsedAuthUrl = new URL(authUrl);
|
|
589
|
+
new URL(tokenUrl);
|
|
590
|
+
} catch (_error) {
|
|
591
|
+
throw new Error(
|
|
592
|
+
`Invalid OAuth URLs. Please check:\n Authorization URL: ${authUrl}\n Token URL: ${tokenUrl}`,
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const resolvedClientId = clientId.trim() || parsedAuthUrl.searchParams.get("client_id") || undefined;
|
|
597
|
+
const resolvedClientSecret = clientSecret.trim() || undefined;
|
|
598
|
+
|
|
599
|
+
const manualInput = this.ctx.oauthManualInput;
|
|
600
|
+
if (manualInput.hasPending()) {
|
|
601
|
+
const pendingProvider = manualInput.pendingProviderId ?? "another provider";
|
|
602
|
+
throw new Error(
|
|
603
|
+
`OAuth login already in progress for ${pendingProvider}. Complete or cancel it before starting MCP OAuth.`,
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
let manualInputClaim: { promise: Promise<string>; clear: (reason?: string) => void } | undefined;
|
|
607
|
+
const oauthTimeout = new AbortController();
|
|
608
|
+
try {
|
|
609
|
+
// Create OAuth flow
|
|
610
|
+
const flow = new MCPOAuthFlow(
|
|
611
|
+
{
|
|
612
|
+
authorizationUrl: authUrl,
|
|
613
|
+
tokenUrl: tokenUrl,
|
|
614
|
+
clientId: resolvedClientId,
|
|
615
|
+
clientSecret: resolvedClientSecret,
|
|
616
|
+
scopes: scopes || undefined,
|
|
617
|
+
redirectUri,
|
|
618
|
+
callbackPort,
|
|
619
|
+
callbackPath,
|
|
620
|
+
},
|
|
621
|
+
{
|
|
622
|
+
onAuth: (info: { url: string; instructions?: string }) => {
|
|
623
|
+
// Show auth URL prominently in chat as one block
|
|
624
|
+
const block = new TranscriptBlock();
|
|
625
|
+
this.ctx.present(block);
|
|
626
|
+
block.addChild(new Text(theme.fg("accent", "━━━ OAuth Authorization Required ━━━"), 1, 0));
|
|
627
|
+
block.addChild(new Spacer(1));
|
|
628
|
+
block.addChild(new Text(theme.fg("muted", "Preparing browser authorization..."), 1, 0));
|
|
629
|
+
block.addChild(new Spacer(1));
|
|
630
|
+
block.addChild(
|
|
631
|
+
new Text(
|
|
632
|
+
theme.fg("muted", "Waiting for authorization... (Press Ctrl+C to cancel, 5 minute timeout)"),
|
|
633
|
+
1,
|
|
634
|
+
0,
|
|
635
|
+
),
|
|
636
|
+
);
|
|
637
|
+
block.addChild(new Text(theme.fg("muted", MCP_MANUAL_LOGIN_TIP), 1, 0));
|
|
638
|
+
block.addChild(new Spacer(1));
|
|
639
|
+
block.addChild(new Text(theme.fg("accent", "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"), 1, 0));
|
|
640
|
+
// Try to open browser automatically
|
|
641
|
+
try {
|
|
642
|
+
openPath(info.url);
|
|
643
|
+
|
|
644
|
+
// Show confirmation that browser should open
|
|
645
|
+
block.addChild(new Spacer(1));
|
|
646
|
+
block.addChild(new Text(theme.fg("success", "→ Opening browser automatically..."), 1, 0));
|
|
647
|
+
block.addChild(new Spacer(1));
|
|
648
|
+
block.addChild(new Text(theme.fg("muted", "Alternative if browser did not open:"), 1, 0));
|
|
649
|
+
block.addChild(new MCPAuthorizationLinkPrompt(info.url));
|
|
650
|
+
this.ctx.ui.requestRender();
|
|
651
|
+
} catch (_error) {
|
|
652
|
+
// Show error if browser doesn't open
|
|
653
|
+
block.addChild(new Spacer(1));
|
|
654
|
+
block.addChild(new Text(theme.fg("warning", "→ Could not open browser automatically"), 1, 0));
|
|
655
|
+
block.addChild(new MCPAuthorizationLinkPrompt(info.url));
|
|
656
|
+
this.ctx.ui.requestRender();
|
|
657
|
+
}
|
|
658
|
+
},
|
|
659
|
+
onProgress: (message: string) => {
|
|
660
|
+
this.ctx.present([new Spacer(1), new Text(theme.fg("muted", message), 1, 0)]);
|
|
661
|
+
},
|
|
662
|
+
onManualCodeInput: () => {
|
|
663
|
+
if (manualInputClaim) return manualInputClaim.promise;
|
|
664
|
+
const pendingInput = manualInput.tryClaimInput(MCP_MANUAL_INPUT_PROVIDER_ID);
|
|
665
|
+
if (!pendingInput) {
|
|
666
|
+
const pendingProvider = manualInput.pendingProviderId ?? "another provider";
|
|
667
|
+
throw new Error(
|
|
668
|
+
`OAuth login already in progress for ${pendingProvider}. Complete or cancel it before starting MCP OAuth.`,
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
manualInputClaim = pendingInput;
|
|
672
|
+
return pendingInput.promise;
|
|
673
|
+
},
|
|
674
|
+
signal: oauthTimeout.signal,
|
|
675
|
+
},
|
|
676
|
+
);
|
|
677
|
+
|
|
678
|
+
// Execute OAuth flow with 5 minute timeout
|
|
679
|
+
const credentials = await withTimeout(
|
|
680
|
+
flow.login(),
|
|
681
|
+
5 * 60 * 1000,
|
|
682
|
+
"OAuth flow timed out after 5 minutes",
|
|
683
|
+
() => oauthTimeout.abort("MCP OAuth flow timed out"),
|
|
684
|
+
);
|
|
685
|
+
|
|
686
|
+
this.ctx.present([
|
|
687
|
+
new Spacer(1),
|
|
688
|
+
new Text(theme.fg("success", "✓ Authorization completed in browser."), 1, 0),
|
|
689
|
+
]);
|
|
690
|
+
|
|
691
|
+
// Generate a unique credential ID
|
|
692
|
+
const credentialId = `mcp_oauth_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
|
|
693
|
+
|
|
694
|
+
// Store credentials in auth storage
|
|
695
|
+
const oauthCredential: OAuthCredential = {
|
|
696
|
+
type: "oauth",
|
|
697
|
+
...credentials,
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
// Store under a synthetic provider name
|
|
701
|
+
await authStorage.set(credentialId, oauthCredential);
|
|
702
|
+
|
|
703
|
+
return {
|
|
704
|
+
credentialId,
|
|
705
|
+
clientId: flow.resolvedClientId,
|
|
706
|
+
clientSecret: flow.registeredClientSecret,
|
|
707
|
+
};
|
|
708
|
+
} catch (error) {
|
|
709
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
710
|
+
|
|
711
|
+
// Provide helpful error messages based on failure type
|
|
712
|
+
if (errorMsg.includes("timeout") || errorMsg.includes("timed out")) {
|
|
713
|
+
throw new Error("OAuth flow timed out. Please try again.");
|
|
714
|
+
} else if (errorMsg.includes("403") || errorMsg.includes("unauthorized")) {
|
|
715
|
+
throw new Error("OAuth authorization failed. Please check your client credentials.");
|
|
716
|
+
} else if (errorMsg.includes("invalid_grant")) {
|
|
717
|
+
throw new Error("OAuth authorization code is invalid or expired. Please try again.");
|
|
718
|
+
} else if (errorMsg.includes("ECONNREFUSED") || errorMsg.includes("fetch failed")) {
|
|
719
|
+
throw new Error("Could not connect to OAuth server. Please check the URLs and your network connection.");
|
|
720
|
+
} else {
|
|
721
|
+
throw new Error(`OAuth authentication failed: ${errorMsg}`);
|
|
722
|
+
}
|
|
723
|
+
} finally {
|
|
724
|
+
manualInputClaim?.clear("Manual MCP OAuth input cleared");
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Test connection to an MCP server.
|
|
730
|
+
* Throws an error if connection fails (used for auto-detection).
|
|
731
|
+
*/
|
|
732
|
+
async #handleTestConnection(config: MCPServerConfig): Promise<void> {
|
|
733
|
+
// Create temporary connection using a test name
|
|
734
|
+
const testName = `test_${Date.now()}`;
|
|
735
|
+
let resolvedConfig: MCPServerConfig;
|
|
736
|
+
if (this.ctx.mcpManager) {
|
|
737
|
+
resolvedConfig = await this.ctx.mcpManager.prepareConfig(config);
|
|
738
|
+
} else {
|
|
739
|
+
const tempManager = new MCPManager(getProjectDir());
|
|
740
|
+
tempManager.setAuthStorage(this.ctx.session.modelRegistry.authStorage);
|
|
741
|
+
resolvedConfig = await tempManager.prepareConfig(config);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const connection = await connectToServer(testName, resolvedConfig);
|
|
745
|
+
await disconnectServer(connection);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
async #findConfiguredServer(
|
|
749
|
+
name: string,
|
|
750
|
+
): Promise<{ filePath: string; scope: "user" | "project"; config: MCPServerConfig } | null> {
|
|
751
|
+
const cwd = getProjectDir();
|
|
752
|
+
const userPath = getMCPConfigPath("user", cwd);
|
|
753
|
+
const projectPath = getMCPConfigPath("project", cwd);
|
|
754
|
+
|
|
755
|
+
const [userConfig, projectConfig] = await Promise.all([
|
|
756
|
+
readMCPConfigFile(userPath),
|
|
757
|
+
readMCPConfigFile(projectPath),
|
|
758
|
+
]);
|
|
759
|
+
|
|
760
|
+
if (userConfig.mcpServers?.[name]) {
|
|
761
|
+
return { filePath: userPath, scope: "user", config: userConfig.mcpServers[name] };
|
|
762
|
+
}
|
|
763
|
+
if (projectConfig.mcpServers?.[name]) {
|
|
764
|
+
return { filePath: projectPath, scope: "project", config: projectConfig.mcpServers[name] };
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
// Check standalone fallback files (mcp.json, .mcp.json) in the project root —
|
|
768
|
+
// these match the discovery paths used by the mcp-json provider. Reads run in
|
|
769
|
+
// parallel (mirroring user/project above) but precedence is preserved by the
|
|
770
|
+
// for-loop's iteration order: mcp.json wins over .mcp.json on a same-name hit.
|
|
771
|
+
const standalonePaths = [path.join(cwd, "mcp.json"), path.join(cwd, ".mcp.json")];
|
|
772
|
+
const fallbackConfigs = await Promise.all(
|
|
773
|
+
standalonePaths.map(async fallbackPath => {
|
|
774
|
+
try {
|
|
775
|
+
return await readMCPConfigFile(fallbackPath);
|
|
776
|
+
} catch {
|
|
777
|
+
// Malformed JSON in a standalone file — skip and continue lookup.
|
|
778
|
+
return null;
|
|
779
|
+
}
|
|
780
|
+
}),
|
|
781
|
+
);
|
|
782
|
+
for (const [index, fallbackConfig] of fallbackConfigs.entries()) {
|
|
783
|
+
const config = fallbackConfig?.mcpServers?.[name];
|
|
784
|
+
if (config) {
|
|
785
|
+
return { filePath: standalonePaths[index]!, scope: "project", config };
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
return null;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
async #removeManagedOAuthCredential(credentialId: string | undefined): Promise<void> {
|
|
792
|
+
if (!credentialId?.startsWith("mcp_oauth_")) return;
|
|
793
|
+
await this.ctx.session.modelRegistry.authStorage.remove(credentialId);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
#stripOAuthAuth(config: MCPServerConfig): MCPServerConfig {
|
|
797
|
+
const next = { ...config } as MCPServerConfig & { auth?: MCPAuthConfig };
|
|
798
|
+
delete next.auth;
|
|
799
|
+
return next;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
async #resolveOAuthEndpointsFromServer(config: MCPServerConfig): Promise<{
|
|
803
|
+
authorizationUrl: string;
|
|
804
|
+
tokenUrl: string;
|
|
805
|
+
clientId?: string;
|
|
806
|
+
scopes?: string;
|
|
807
|
+
}> {
|
|
808
|
+
// First test if server actually needs auth by connecting without OAuth
|
|
809
|
+
let connectionSucceeded = false;
|
|
810
|
+
let connectionError: Error | undefined;
|
|
811
|
+
try {
|
|
812
|
+
await this.#handleTestConnection(this.#stripOAuthAuth(config));
|
|
813
|
+
connectionSucceeded = true;
|
|
814
|
+
} catch (error) {
|
|
815
|
+
connectionError = error as Error;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// Server connected fine without auth — reauth is not needed
|
|
819
|
+
if (connectionSucceeded) {
|
|
820
|
+
throw new Error("Server connection succeeded without OAuth; reauthorization is not required.");
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// Analyze the connection error to extract OAuth endpoints
|
|
824
|
+
const authResult = analyzeAuthError(connectionError!, "url" in config ? config.url : undefined);
|
|
825
|
+
let oauth = authResult.authType === "oauth" ? (authResult.oauth ?? null) : null;
|
|
826
|
+
|
|
827
|
+
if (!oauth && (config.type === "http" || config.type === "sse") && config.url) {
|
|
828
|
+
oauth = await discoverOAuthEndpoints(config.url, authResult.authServerUrl, authResult.resourceMetadataUrl);
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
if (!oauth) {
|
|
832
|
+
throw new Error("Could not discover OAuth endpoints from server response.");
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
return oauth;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
async #waitForServerConnectionWithAnimation(
|
|
839
|
+
name: string,
|
|
840
|
+
options?: { suppressDisconnectedWarning?: boolean },
|
|
841
|
+
): Promise<"connected" | "connecting" | "disconnected"> {
|
|
842
|
+
if (!this.ctx.mcpManager) return "disconnected";
|
|
843
|
+
|
|
844
|
+
const block = new McpConnectingBlock(name);
|
|
845
|
+
this.ctx.present(block);
|
|
846
|
+
|
|
847
|
+
try {
|
|
848
|
+
try {
|
|
849
|
+
await withTimeout(this.ctx.mcpManager.waitForConnection(name), 10_000, "Connection still pending");
|
|
850
|
+
} catch {
|
|
851
|
+
// Ignore timeout/errors here and use status check below.
|
|
852
|
+
}
|
|
853
|
+
const state = this.ctx.mcpManager.getConnectionStatus(name);
|
|
854
|
+
if (state === "connected") {
|
|
855
|
+
// Connection may complete after initial reload; rebind runtime MCP tools now.
|
|
856
|
+
await this.ctx.session.refreshMCPTools(this.ctx.mcpManager.getTools());
|
|
857
|
+
}
|
|
858
|
+
if (state === "connected") {
|
|
859
|
+
block.setStatus(theme.fg("success", `${theme.status.enabled} Connected to "${name}"`));
|
|
860
|
+
} else if (state === "connecting") {
|
|
861
|
+
block.setStatus(theme.fg("muted", `◌ "${name}" is still connecting...`));
|
|
862
|
+
} else {
|
|
863
|
+
block.setStatus(
|
|
864
|
+
options?.suppressDisconnectedWarning
|
|
865
|
+
? theme.fg("muted", `◌ Connection check complete for "${name}"`)
|
|
866
|
+
: theme.fg("warning", `⚠ Could not connect to "${name}" yet`),
|
|
867
|
+
);
|
|
868
|
+
}
|
|
869
|
+
return state;
|
|
870
|
+
} finally {
|
|
871
|
+
block.finish();
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
async #syncManagerConnection(name: string, config: MCPServerConfig): Promise<void> {
|
|
876
|
+
if (!this.ctx.mcpManager) return;
|
|
877
|
+
if (this.ctx.mcpManager.getConnectionStatus(name) !== "disconnected") return;
|
|
878
|
+
await this.ctx.mcpManager.connectServers({ [name]: config }, {});
|
|
879
|
+
if (this.ctx.mcpManager.getConnectionStatus(name) === "connected") {
|
|
880
|
+
await this.ctx.session.refreshMCPTools(this.ctx.mcpManager.getTools());
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
async #handleWizardComplete(name: string, config: MCPServerConfig, scope: "user" | "project"): Promise<void> {
|
|
885
|
+
try {
|
|
886
|
+
// Determine file path
|
|
887
|
+
const cwd = getProjectDir();
|
|
888
|
+
const filePath = getMCPConfigPath(scope, cwd);
|
|
889
|
+
|
|
890
|
+
// Add server to config
|
|
891
|
+
await addMCPServer(filePath, name, config);
|
|
892
|
+
|
|
893
|
+
// Reload MCP manager
|
|
894
|
+
await this.#reloadMCP();
|
|
895
|
+
const state =
|
|
896
|
+
config.enabled === false
|
|
897
|
+
? "disconnected"
|
|
898
|
+
: await this.#waitForServerConnectionWithAnimation(name, { suppressDisconnectedWarning: true });
|
|
899
|
+
let isConnected = state === "connected";
|
|
900
|
+
const isConnecting = state === "connecting";
|
|
901
|
+
|
|
902
|
+
// Fallback: if manager state is still disconnected but direct test works,
|
|
903
|
+
// report as connected to avoid false-negative messaging.
|
|
904
|
+
if (!isConnected && !isConnecting && config.enabled !== false) {
|
|
905
|
+
try {
|
|
906
|
+
await this.#handleTestConnection(config);
|
|
907
|
+
isConnected = true;
|
|
908
|
+
await this.#syncManagerConnection(name, config);
|
|
909
|
+
} catch {
|
|
910
|
+
// Keep disconnected status
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
// refreshMCPTools preserves the prior MCP tool selection, so tools from
|
|
915
|
+
// brand-new servers are registered in the registry but never activated.
|
|
916
|
+
// Explicitly activate the newly added server's tools now.
|
|
917
|
+
if (isConnected && this.ctx.mcpManager) {
|
|
918
|
+
const serverTools = this.ctx.mcpManager.getTools().filter(t => t.mcpServerName === name);
|
|
919
|
+
if (serverTools.length > 0) {
|
|
920
|
+
const currentActive = this.ctx.session.getActiveToolNames();
|
|
921
|
+
const toActivate = serverTools.map(t => t.name).filter(n => this.ctx.session.getToolByName(n));
|
|
922
|
+
if (toActivate.length > 0) {
|
|
923
|
+
await this.ctx.session.setActiveToolsByName([...new Set([...currentActive, ...toActivate])]);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
// Show success message
|
|
929
|
+
const scopeLabel = scope === "user" ? "user" : "project";
|
|
930
|
+
const lines = ["", theme.fg("success", `+ Added server "${name}" to ${scopeLabel} config`), ""];
|
|
931
|
+
|
|
932
|
+
if (isConnected) {
|
|
933
|
+
lines.push(theme.fg("success", `${theme.status.enabled} Successfully connected to server`));
|
|
934
|
+
lines.push("");
|
|
935
|
+
} else if (isConnecting) {
|
|
936
|
+
lines.push(theme.fg("muted", `◌ Server is connecting in background...`));
|
|
937
|
+
lines.push(theme.fg("muted", ` Run ${theme.fg("accent", `/mcp test ${name}`)} in a few seconds.`));
|
|
938
|
+
lines.push("");
|
|
939
|
+
} else {
|
|
940
|
+
lines.push(theme.fg("warning", `⚠ Server added but not yet connected`));
|
|
941
|
+
lines.push(theme.fg("muted", ` Run ${theme.fg("accent", `/mcp test ${name}`)} to test the connection.`));
|
|
942
|
+
lines.push("");
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
lines.push(theme.fg("muted", `Run ${theme.fg("accent", "/mcp list")} to see all configured servers.`));
|
|
946
|
+
lines.push("");
|
|
947
|
+
|
|
948
|
+
this.#showMessage(lines.join("\n"));
|
|
949
|
+
} catch (error) {
|
|
950
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
951
|
+
|
|
952
|
+
// Provide helpful error messages
|
|
953
|
+
let helpText = "";
|
|
954
|
+
if (errorMsg.includes("EACCES") || errorMsg.includes("permission denied")) {
|
|
955
|
+
helpText = "\n\nTip: Check file permissions for the config directory.";
|
|
956
|
+
} else if (errorMsg.includes("ENOSPC")) {
|
|
957
|
+
helpText = "\n\nTip: Insufficient disk space.";
|
|
958
|
+
} else if (errorMsg.includes("already exists")) {
|
|
959
|
+
helpText = `\n\nTip: Use ${theme.fg("accent", "/mcp list")} to see existing servers.`;
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
this.ctx.showError(`Failed to add server: ${errorMsg}${helpText}`);
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
#handleWizardCancel(): void {
|
|
967
|
+
this.#showMessage(
|
|
968
|
+
[
|
|
969
|
+
"",
|
|
970
|
+
theme.fg("muted", "Server creation cancelled."),
|
|
971
|
+
"",
|
|
972
|
+
theme.fg("dim", "Tip: Press Ctrl+C or Esc anytime to cancel"),
|
|
973
|
+
"",
|
|
974
|
+
].join("\n"),
|
|
975
|
+
);
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* Handle /mcp list - Show all configured servers
|
|
980
|
+
*/
|
|
981
|
+
async #handleList(): Promise<void> {
|
|
982
|
+
try {
|
|
983
|
+
const cwd = getProjectDir();
|
|
984
|
+
|
|
985
|
+
// Load from both user and project configs
|
|
986
|
+
const userPath = getMCPConfigPath("user", cwd);
|
|
987
|
+
const projectPath = getMCPConfigPath("project", cwd);
|
|
988
|
+
|
|
989
|
+
const userPathLabel = shortenPath(userPath);
|
|
990
|
+
const projectPathLabel = shortenPath(projectPath);
|
|
991
|
+
const [userConfig, projectConfig] = await Promise.all([
|
|
992
|
+
readMCPConfigFile(userPath),
|
|
993
|
+
readMCPConfigFile(projectPath),
|
|
994
|
+
]);
|
|
995
|
+
|
|
996
|
+
const userServers = Object.keys(userConfig.mcpServers ?? {});
|
|
997
|
+
const projectServers = Object.keys(projectConfig.mcpServers ?? {});
|
|
998
|
+
|
|
999
|
+
// Collect runtime-discovered servers not in config files
|
|
1000
|
+
const configServerNames = new Set([...userServers, ...projectServers]);
|
|
1001
|
+
const disabledServerNames = new Set(await readDisabledServers(userPath));
|
|
1002
|
+
const discoveredServers: { name: string; source: SourceMeta }[] = [];
|
|
1003
|
+
if (this.ctx.mcpManager) {
|
|
1004
|
+
for (const name of this.ctx.mcpManager.getAllServerNames()) {
|
|
1005
|
+
if (configServerNames.has(name)) continue;
|
|
1006
|
+
if (disabledServerNames.has(name)) continue;
|
|
1007
|
+
const source = this.ctx.mcpManager.getSource(name);
|
|
1008
|
+
if (source) {
|
|
1009
|
+
discoveredServers.push({ name, source });
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
if (
|
|
1015
|
+
userServers.length === 0 &&
|
|
1016
|
+
projectServers.length === 0 &&
|
|
1017
|
+
discoveredServers.length === 0 &&
|
|
1018
|
+
disabledServerNames.size === 0
|
|
1019
|
+
) {
|
|
1020
|
+
this.#showMessage(
|
|
1021
|
+
[
|
|
1022
|
+
"",
|
|
1023
|
+
theme.fg("muted", "No MCP servers configured."),
|
|
1024
|
+
"",
|
|
1025
|
+
`Use ${theme.fg("accent", "/mcp add")} to add a server.`,
|
|
1026
|
+
"",
|
|
1027
|
+
].join("\n"),
|
|
1028
|
+
);
|
|
1029
|
+
return;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
const lines: string[] = ["", theme.bold("Configured MCP Servers"), ""];
|
|
1033
|
+
|
|
1034
|
+
// Show user-level servers
|
|
1035
|
+
if (userServers.length > 0) {
|
|
1036
|
+
lines.push(theme.fg("accent", "User level") + theme.fg("muted", ` (${userPathLabel}):`));
|
|
1037
|
+
for (const name of userServers) {
|
|
1038
|
+
const config = userConfig.mcpServers![name];
|
|
1039
|
+
const type = config.type ?? "stdio";
|
|
1040
|
+
const state =
|
|
1041
|
+
config.enabled === false
|
|
1042
|
+
? "inactive"
|
|
1043
|
+
: (this.ctx.mcpManager?.getConnectionStatus(name) ?? "disconnected");
|
|
1044
|
+
const status =
|
|
1045
|
+
state === "inactive"
|
|
1046
|
+
? theme.fg("warning", " ◌ inactive")
|
|
1047
|
+
: state === "connected"
|
|
1048
|
+
? theme.fg("success", " ● connected")
|
|
1049
|
+
: state === "connecting"
|
|
1050
|
+
? theme.fg("muted", " ◌ connecting")
|
|
1051
|
+
: theme.fg("muted", " ○ not connected");
|
|
1052
|
+
lines.push(` ${theme.fg("accent", name)}${status} ${theme.fg("dim", `[${type}]`)}`);
|
|
1053
|
+
}
|
|
1054
|
+
lines.push("");
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// Show project-level servers
|
|
1058
|
+
if (projectServers.length > 0) {
|
|
1059
|
+
lines.push(theme.fg("accent", "Project level") + theme.fg("muted", ` (${projectPathLabel}):`));
|
|
1060
|
+
for (const name of projectServers) {
|
|
1061
|
+
const config = projectConfig.mcpServers![name];
|
|
1062
|
+
const type = config.type ?? "stdio";
|
|
1063
|
+
const state =
|
|
1064
|
+
config.enabled === false
|
|
1065
|
+
? "inactive"
|
|
1066
|
+
: (this.ctx.mcpManager?.getConnectionStatus(name) ?? "disconnected");
|
|
1067
|
+
const status =
|
|
1068
|
+
state === "inactive"
|
|
1069
|
+
? theme.fg("warning", " ◌ inactive")
|
|
1070
|
+
: state === "connected"
|
|
1071
|
+
? theme.fg("success", " ● connected")
|
|
1072
|
+
: state === "connecting"
|
|
1073
|
+
? theme.fg("muted", " ◌ connecting")
|
|
1074
|
+
: theme.fg("muted", " ○ not connected");
|
|
1075
|
+
lines.push(` ${theme.fg("accent", name)}${status} ${theme.fg("dim", `[${type}]`)}`);
|
|
1076
|
+
}
|
|
1077
|
+
lines.push("");
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
// Show discovered servers (from .claude.json, .cursor/mcp.json, .vscode/mcp.json, etc.)
|
|
1081
|
+
if (discoveredServers.length > 0) {
|
|
1082
|
+
for (const { providerName, shortPath, items: entries } of groupBySource(discoveredServers, e => e.source)) {
|
|
1083
|
+
lines.push(theme.fg("accent", providerName) + theme.fg("muted", ` (${shortPath}):`));
|
|
1084
|
+
for (const { name } of entries) {
|
|
1085
|
+
const state = this.ctx.mcpManager!.getConnectionStatus(name);
|
|
1086
|
+
const status =
|
|
1087
|
+
state === "connected"
|
|
1088
|
+
? theme.fg("success", " ● connected")
|
|
1089
|
+
: state === "connecting"
|
|
1090
|
+
? theme.fg("muted", " ◌ connecting")
|
|
1091
|
+
: theme.fg("muted", " ○ not connected");
|
|
1092
|
+
lines.push(` ${theme.fg("accent", name)}${status}`);
|
|
1093
|
+
}
|
|
1094
|
+
lines.push("");
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
// Show servers disabled via /mcp disable (from third-party configs)
|
|
1099
|
+
const relevantDisabled = [...disabledServerNames].filter(n => !configServerNames.has(n));
|
|
1100
|
+
if (relevantDisabled.length > 0) {
|
|
1101
|
+
lines.push(theme.fg("accent", "Disabled") + theme.fg("muted", " (discovered servers):"));
|
|
1102
|
+
for (const name of relevantDisabled) {
|
|
1103
|
+
lines.push(` ${theme.fg("accent", name)}${theme.fg("warning", " ◌ disabled")}`);
|
|
1104
|
+
}
|
|
1105
|
+
lines.push("");
|
|
1106
|
+
}
|
|
1107
|
+
this.#showMessage(lines.join("\n"));
|
|
1108
|
+
} catch (error) {
|
|
1109
|
+
this.ctx.showError(`Failed to list servers: ${error instanceof Error ? error.message : String(error)}`);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
/**
|
|
1114
|
+
* Handle /mcp remove <name> - Remove a server
|
|
1115
|
+
*/
|
|
1116
|
+
async #handleRemove(text: string): Promise<void> {
|
|
1117
|
+
const match = text.match(/^\/mcp\s+(?:remove|rm)\b\s*(.*)$/i);
|
|
1118
|
+
const rest = match?.[1]?.trim() ?? "";
|
|
1119
|
+
const parsed = parseRemoveArgs(rest);
|
|
1120
|
+
if (!parsed.ok) {
|
|
1121
|
+
this.ctx.showError(parsed.error);
|
|
1122
|
+
return;
|
|
1123
|
+
}
|
|
1124
|
+
const { name, scope } = parsed.value;
|
|
1125
|
+
|
|
1126
|
+
if (!name) {
|
|
1127
|
+
this.ctx.showError("Server name required. Usage: /mcp remove <name> [--scope project|user]");
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
try {
|
|
1132
|
+
const cwd = getProjectDir();
|
|
1133
|
+
const userPath = getMCPConfigPath("user", cwd);
|
|
1134
|
+
const projectPath = getMCPConfigPath("project", cwd);
|
|
1135
|
+
const filePath = scope === "user" ? userPath : projectPath;
|
|
1136
|
+
const config = await readMCPConfigFile(filePath);
|
|
1137
|
+
if (!config.mcpServers?.[name]) {
|
|
1138
|
+
this.ctx.showError(`Server "${name}" not found in ${scope} config.`);
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
// Disconnect if connected
|
|
1143
|
+
if (this.ctx.mcpManager?.getConnection(name)) {
|
|
1144
|
+
await this.ctx.mcpManager.disconnectServer(name);
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
// Remove from config
|
|
1148
|
+
await removeMCPServer(filePath, name);
|
|
1149
|
+
|
|
1150
|
+
// Reload MCP manager
|
|
1151
|
+
await this.#reloadMCP();
|
|
1152
|
+
|
|
1153
|
+
this.#showMessage(["", theme.fg("success", `- Removed server "${name}" from ${scope} config`), ""].join("\n"));
|
|
1154
|
+
} catch (error) {
|
|
1155
|
+
this.ctx.showError(`Failed to remove server: ${error instanceof Error ? error.message : String(error)}`);
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
/**
|
|
1160
|
+
* Handle /mcp test <name> - Test connection to a server
|
|
1161
|
+
*/
|
|
1162
|
+
async #handleTest(name: string | undefined): Promise<void> {
|
|
1163
|
+
if (!name) {
|
|
1164
|
+
this.ctx.showError("Server name required. Usage: /mcp test <name>");
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
const originalOnEscape = this.ctx.editor.onEscape;
|
|
1169
|
+
const abortController = new AbortController();
|
|
1170
|
+
this.ctx.editor.onEscape = () => {
|
|
1171
|
+
abortController.abort();
|
|
1172
|
+
};
|
|
1173
|
+
|
|
1174
|
+
let connection: MCPServerConnection | undefined;
|
|
1175
|
+
try {
|
|
1176
|
+
const found = await this.#findConfiguredServer(name);
|
|
1177
|
+
|
|
1178
|
+
if (!found) {
|
|
1179
|
+
this.ctx.showError(
|
|
1180
|
+
`Server "${name}" not found.\n\nTip: Run ${theme.fg("accent", "/mcp list")} to see available servers.`,
|
|
1181
|
+
);
|
|
1182
|
+
return;
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
const { config } = found;
|
|
1186
|
+
if (config.enabled === false) {
|
|
1187
|
+
this.ctx.showError(`Server "${name}" is disabled. Run /mcp enable ${name} first.`);
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
this.#showMessage(
|
|
1192
|
+
["", theme.fg("muted", `Testing connection to "${name}"... (esc to cancel)`), ""].join("\n"),
|
|
1193
|
+
);
|
|
1194
|
+
|
|
1195
|
+
// Resolve auth config if needed
|
|
1196
|
+
let resolvedConfig: MCPServerConfig;
|
|
1197
|
+
if (this.ctx.mcpManager) {
|
|
1198
|
+
resolvedConfig = await this.ctx.mcpManager.prepareConfig(config);
|
|
1199
|
+
} else {
|
|
1200
|
+
const tempManager = new MCPManager(getProjectDir());
|
|
1201
|
+
tempManager.setAuthStorage(this.ctx.session.modelRegistry.authStorage);
|
|
1202
|
+
resolvedConfig = await tempManager.prepareConfig(config);
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
// Create temporary connection
|
|
1206
|
+
connection = await connectToServer(name, resolvedConfig, { signal: abortController.signal });
|
|
1207
|
+
|
|
1208
|
+
// List tools to verify connection
|
|
1209
|
+
const tools = await listTools(connection, { signal: abortController.signal });
|
|
1210
|
+
|
|
1211
|
+
const lines = [
|
|
1212
|
+
"",
|
|
1213
|
+
theme.fg("success", `${theme.status.enabled} Successfully connected to "${name}"`),
|
|
1214
|
+
"",
|
|
1215
|
+
` Server: ${connection.serverInfo.name} v${connection.serverInfo.version}`,
|
|
1216
|
+
` Tools: ${tools.length}`,
|
|
1217
|
+
];
|
|
1218
|
+
|
|
1219
|
+
// Show tool names if there are any
|
|
1220
|
+
if (tools.length > 0 && tools.length <= 10) {
|
|
1221
|
+
lines.push("");
|
|
1222
|
+
lines.push(" Available tools:");
|
|
1223
|
+
for (const tool of tools) {
|
|
1224
|
+
lines.push(` • ${tool.name}`);
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
lines.push("");
|
|
1229
|
+
await this.#syncManagerConnection(name, config);
|
|
1230
|
+
this.#showMessage(lines.join("\n"));
|
|
1231
|
+
} catch (error) {
|
|
1232
|
+
if (abortController.signal.aborted || (error instanceof Error && error.name === "AbortError")) {
|
|
1233
|
+
this.ctx.showStatus(`Cancelled MCP test for "${name}"`);
|
|
1234
|
+
return;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
1238
|
+
|
|
1239
|
+
// Provide helpful error messages
|
|
1240
|
+
let helpText = "";
|
|
1241
|
+
if (errorMsg.includes("ENOENT") || errorMsg.includes("not found")) {
|
|
1242
|
+
helpText = "\n\nTip: Check that the command or URL is correct.";
|
|
1243
|
+
} else if (errorMsg.includes("EACCES")) {
|
|
1244
|
+
helpText = "\n\nTip: Check file/command permissions.";
|
|
1245
|
+
} else if (errorMsg.includes("ECONNREFUSED")) {
|
|
1246
|
+
helpText = "\n\nTip: Check that the server is running and the URL/port is correct.";
|
|
1247
|
+
} else if (errorMsg.includes("timeout")) {
|
|
1248
|
+
helpText = "\n\nTip: The server may be slow or unresponsive. Try increasing the timeout.";
|
|
1249
|
+
} else if (errorMsg.includes("401") || errorMsg.includes("403")) {
|
|
1250
|
+
helpText = "\n\nTip: Check your authentication credentials.";
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
this.ctx.showError(`Failed to connect to "${name}": ${errorMsg}${helpText}`);
|
|
1254
|
+
} finally {
|
|
1255
|
+
this.ctx.editor.onEscape = originalOnEscape;
|
|
1256
|
+
if (connection) {
|
|
1257
|
+
// Best-effort: don't block UI on cleanup.
|
|
1258
|
+
void disconnectServer(connection);
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
async #handleSetEnabled(name: string | undefined, enabled: boolean): Promise<void> {
|
|
1264
|
+
if (!name) {
|
|
1265
|
+
this.ctx.showError(`Server name required. Usage: /mcp ${enabled ? "enable" : "disable"} <name>`);
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
try {
|
|
1270
|
+
const found = await this.#findConfiguredServer(name);
|
|
1271
|
+
if (!found) {
|
|
1272
|
+
// Check if this is a discovered server from a third-party config
|
|
1273
|
+
const userConfigPath = getMCPConfigPath("user", getProjectDir());
|
|
1274
|
+
const disabledServers = new Set(await readDisabledServers(userConfigPath));
|
|
1275
|
+
const isDiscovered = this.ctx.mcpManager?.getSource(name);
|
|
1276
|
+
const isCurrentlyDisabled = disabledServers.has(name);
|
|
1277
|
+
if (!isDiscovered && !isCurrentlyDisabled) {
|
|
1278
|
+
this.ctx.showError(`Server "${name}" not found.`);
|
|
1279
|
+
return;
|
|
1280
|
+
}
|
|
1281
|
+
if (isCurrentlyDisabled === !enabled) {
|
|
1282
|
+
this.#showMessage(
|
|
1283
|
+
["", theme.fg("muted", `Server "${name}" is already ${enabled ? "enabled" : "disabled"}.`), ""].join(
|
|
1284
|
+
"\n",
|
|
1285
|
+
),
|
|
1286
|
+
);
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
1289
|
+
await setServerDisabled(userConfigPath, name, !enabled);
|
|
1290
|
+
if (enabled) {
|
|
1291
|
+
await this.#reloadMCP();
|
|
1292
|
+
const state = await this.#waitForServerConnectionWithAnimation(name);
|
|
1293
|
+
const status =
|
|
1294
|
+
state === "connected"
|
|
1295
|
+
? theme.fg("success", "Connected")
|
|
1296
|
+
: state === "connecting"
|
|
1297
|
+
? theme.fg("muted", "Connecting")
|
|
1298
|
+
: theme.fg("warning", "Not connected yet");
|
|
1299
|
+
this.#showMessage(
|
|
1300
|
+
[
|
|
1301
|
+
"",
|
|
1302
|
+
theme.fg("success", `${theme.status.enabled} Enabled "${name}"`),
|
|
1303
|
+
"",
|
|
1304
|
+
` Status: ${status}`,
|
|
1305
|
+
"",
|
|
1306
|
+
].join("\n"),
|
|
1307
|
+
);
|
|
1308
|
+
} else {
|
|
1309
|
+
await this.ctx.mcpManager?.disconnectServer(name);
|
|
1310
|
+
await this.ctx.session.refreshMCPTools(this.ctx.mcpManager?.getTools() ?? []);
|
|
1311
|
+
this.#showMessage(["", theme.fg("muted", `${theme.status.disabled} Disabled "${name}"`), ""].join("\n"));
|
|
1312
|
+
}
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
if ((found.config.enabled ?? true) === enabled) {
|
|
1317
|
+
this.#showMessage(
|
|
1318
|
+
["", theme.fg("muted", `Server "${name}" is already ${enabled ? "enabled" : "disabled"}.`), ""].join(
|
|
1319
|
+
"\n",
|
|
1320
|
+
),
|
|
1321
|
+
);
|
|
1322
|
+
return;
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
const updated: MCPServerConfig = { ...found.config, enabled };
|
|
1326
|
+
await updateMCPServer(found.filePath, name, updated);
|
|
1327
|
+
await this.#reloadMCP();
|
|
1328
|
+
|
|
1329
|
+
let status = "";
|
|
1330
|
+
if (enabled) {
|
|
1331
|
+
const state = await this.#waitForServerConnectionWithAnimation(name);
|
|
1332
|
+
status =
|
|
1333
|
+
state === "connected"
|
|
1334
|
+
? theme.fg("success", "Connected")
|
|
1335
|
+
: state === "connecting"
|
|
1336
|
+
? theme.fg("muted", "Connecting")
|
|
1337
|
+
: theme.fg("warning", "Not connected yet");
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
const lines = [
|
|
1341
|
+
"",
|
|
1342
|
+
enabled
|
|
1343
|
+
? theme.fg("success", `${theme.status.enabled} Enabled "${name}" (${found.scope} config)`)
|
|
1344
|
+
: theme.fg("muted", `${theme.status.disabled} Disabled "${name}" (${found.scope} config)`),
|
|
1345
|
+
];
|
|
1346
|
+
if (status) {
|
|
1347
|
+
lines.push("");
|
|
1348
|
+
lines.push(` Status: ${status}`);
|
|
1349
|
+
}
|
|
1350
|
+
lines.push("");
|
|
1351
|
+
this.#showMessage(lines.join("\n"));
|
|
1352
|
+
} catch (error) {
|
|
1353
|
+
this.ctx.showError(
|
|
1354
|
+
`Failed to ${enabled ? "enable" : "disable"} server: ${error instanceof Error ? error.message : String(error)}`,
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
async #handleUnauth(name: string | undefined): Promise<void> {
|
|
1360
|
+
if (!name) {
|
|
1361
|
+
this.ctx.showError("Server name required. Usage: /mcp unauth <name>");
|
|
1362
|
+
return;
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
try {
|
|
1366
|
+
const found = await this.#findConfiguredServer(name);
|
|
1367
|
+
if (!found) {
|
|
1368
|
+
this.ctx.showError(`Server "${name}" not found.`);
|
|
1369
|
+
return;
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
const currentAuth = (found.config as MCPServerConfig & { auth?: MCPAuthConfig }).auth;
|
|
1373
|
+
if (currentAuth?.type === "oauth") {
|
|
1374
|
+
await this.#removeManagedOAuthCredential(currentAuth.credentialId);
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
const updated = this.#stripOAuthAuth(found.config);
|
|
1378
|
+
await updateMCPServer(found.filePath, name, updated);
|
|
1379
|
+
await this.#reloadMCP();
|
|
1380
|
+
|
|
1381
|
+
this.#showMessage(
|
|
1382
|
+
["", theme.fg("success", `- Cleared auth for "${name}" (${found.scope} config)`), ""].join("\n"),
|
|
1383
|
+
);
|
|
1384
|
+
} catch (error) {
|
|
1385
|
+
this.ctx.showError(`Failed to clear auth: ${error instanceof Error ? error.message : String(error)}`);
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
async #handleReauth(name: string | undefined): Promise<void> {
|
|
1390
|
+
if (!name) {
|
|
1391
|
+
this.ctx.showError("Server name required. Usage: /mcp reauth <name>");
|
|
1392
|
+
return;
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
try {
|
|
1396
|
+
const found = await this.#findConfiguredServer(name);
|
|
1397
|
+
if (!found) {
|
|
1398
|
+
this.ctx.showError(`Server "${name}" not found.`);
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1402
|
+
if (found.config.enabled === false) {
|
|
1403
|
+
this.ctx.showError(`Server "${name}" is disabled. Run /mcp enable ${name} first.`);
|
|
1404
|
+
return;
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
const currentAuth = (found.config as MCPServerConfig & { auth?: MCPAuthConfig }).auth;
|
|
1408
|
+
if (currentAuth?.type === "oauth") {
|
|
1409
|
+
await this.#removeManagedOAuthCredential(currentAuth.credentialId);
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
const baseConfig = this.#stripOAuthAuth(found.config);
|
|
1413
|
+
const oauth = await this.#resolveOAuthEndpointsFromServer(baseConfig);
|
|
1414
|
+
const oauthClientSecret = found.config.oauth?.clientSecret ?? currentAuth?.clientSecret ?? "";
|
|
1415
|
+
|
|
1416
|
+
this.#showMessage(["", theme.fg("muted", `Reauthorizing "${name}"...`), ""].join("\n"));
|
|
1417
|
+
|
|
1418
|
+
const oauthResult = await this.#handleOAuthFlow(
|
|
1419
|
+
oauth.authorizationUrl,
|
|
1420
|
+
oauth.tokenUrl,
|
|
1421
|
+
oauth.clientId ?? found.config.oauth?.clientId ?? "",
|
|
1422
|
+
oauthClientSecret,
|
|
1423
|
+
oauth.scopes ?? "",
|
|
1424
|
+
found.config.oauth?.callbackPort,
|
|
1425
|
+
found.config.oauth?.callbackPath,
|
|
1426
|
+
found.config.oauth?.redirectUri,
|
|
1427
|
+
);
|
|
1428
|
+
|
|
1429
|
+
const persistedClientId = oauthResult.clientId ?? oauth.clientId ?? found.config.oauth?.clientId;
|
|
1430
|
+
const persistedClientSecret = oauthResult.clientSecret ?? (oauthClientSecret || undefined);
|
|
1431
|
+
|
|
1432
|
+
const updated: MCPServerConfig = {
|
|
1433
|
+
...baseConfig,
|
|
1434
|
+
auth: {
|
|
1435
|
+
type: "oauth",
|
|
1436
|
+
credentialId: oauthResult.credentialId,
|
|
1437
|
+
tokenUrl: oauth.tokenUrl,
|
|
1438
|
+
clientId: persistedClientId,
|
|
1439
|
+
clientSecret: persistedClientSecret,
|
|
1440
|
+
},
|
|
1441
|
+
oauth: {
|
|
1442
|
+
...found.config.oauth,
|
|
1443
|
+
clientId: persistedClientId ?? found.config.oauth?.clientId,
|
|
1444
|
+
clientSecret: persistedClientSecret ?? found.config.oauth?.clientSecret,
|
|
1445
|
+
},
|
|
1446
|
+
};
|
|
1447
|
+
await updateMCPServer(found.filePath, name, updated);
|
|
1448
|
+
await this.#reloadMCP();
|
|
1449
|
+
const state = await this.#waitForServerConnectionWithAnimation(name);
|
|
1450
|
+
|
|
1451
|
+
const lines = [
|
|
1452
|
+
"",
|
|
1453
|
+
theme.fg("success", `✓ Reauthorized "${name}" (${found.scope} config)`),
|
|
1454
|
+
"",
|
|
1455
|
+
` Status: ${
|
|
1456
|
+
state === "connected"
|
|
1457
|
+
? theme.fg("success", "connected")
|
|
1458
|
+
: state === "connecting"
|
|
1459
|
+
? theme.fg("muted", "connecting")
|
|
1460
|
+
: theme.fg("warning", "not connected")
|
|
1461
|
+
}`,
|
|
1462
|
+
"",
|
|
1463
|
+
];
|
|
1464
|
+
this.#showMessage(lines.join("\n"));
|
|
1465
|
+
} catch (error) {
|
|
1466
|
+
this.ctx.showError(`Failed to reauthorize server: ${error instanceof Error ? error.message : String(error)}`);
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
async #handleReload(): Promise<void> {
|
|
1471
|
+
try {
|
|
1472
|
+
this.#showMessage(["", theme.fg("muted", "Reloading MCP servers and runtime tools..."), ""].join("\n"));
|
|
1473
|
+
await this.#reloadMCP();
|
|
1474
|
+
const connectedCount = this.ctx.mcpManager?.getConnectedServers().length ?? 0;
|
|
1475
|
+
this.#showMessage(
|
|
1476
|
+
[
|
|
1477
|
+
"",
|
|
1478
|
+
theme.fg("success", `${theme.icon.loop} MCP reload complete`),
|
|
1479
|
+
` Connected servers: ${connectedCount}`,
|
|
1480
|
+
"",
|
|
1481
|
+
].join("\n"),
|
|
1482
|
+
);
|
|
1483
|
+
} catch (error) {
|
|
1484
|
+
this.ctx.showError(`Failed to reload MCP: ${error instanceof Error ? error.message : String(error)}`);
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
/**
|
|
1489
|
+
* Handle /mcp reconnect <name> - Reconnect to a specific server.
|
|
1490
|
+
*/
|
|
1491
|
+
async #handleReconnect(name: string | undefined): Promise<void> {
|
|
1492
|
+
if (!name) {
|
|
1493
|
+
this.ctx.showError("Server name required. Usage: /mcp reconnect <name>");
|
|
1494
|
+
return;
|
|
1495
|
+
}
|
|
1496
|
+
if (!this.ctx.mcpManager) {
|
|
1497
|
+
this.ctx.showError("MCP manager not available.");
|
|
1498
|
+
return;
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
this.#showMessage(["", theme.fg("muted", `Reconnecting to "${name}"...`), ""].join("\n"));
|
|
1502
|
+
|
|
1503
|
+
try {
|
|
1504
|
+
const connection = await this.ctx.mcpManager.reconnectServer(name, { manual: true });
|
|
1505
|
+
if (connection) {
|
|
1506
|
+
// refreshMCPTools re-registers tools and preserves the user's prior
|
|
1507
|
+
// MCP tool selection. No need to call activateDiscoveredMCPTools —
|
|
1508
|
+
// that would broaden the selection to all server tools.
|
|
1509
|
+
await this.ctx.session.refreshMCPTools(this.ctx.mcpManager.getTools());
|
|
1510
|
+
const serverTools = this.ctx.mcpManager.getTools().filter(t => t.mcpServerName === name);
|
|
1511
|
+
this.#showMessage(
|
|
1512
|
+
[
|
|
1513
|
+
"\n",
|
|
1514
|
+
theme.fg("success", `${theme.status.enabled} Reconnected to "${name}"`),
|
|
1515
|
+
` Tools: ${serverTools.length}`,
|
|
1516
|
+
"\n",
|
|
1517
|
+
].join("\n"),
|
|
1518
|
+
);
|
|
1519
|
+
} else {
|
|
1520
|
+
this.ctx.showError(`Failed to reconnect to "${name}". Check server status and logs.`);
|
|
1521
|
+
}
|
|
1522
|
+
} catch (error) {
|
|
1523
|
+
this.ctx.showError(
|
|
1524
|
+
`Failed to reconnect to "${name}": ${error instanceof Error ? error.message : String(error)}`,
|
|
1525
|
+
);
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
/**
|
|
1530
|
+
* Reload MCP manager with new configs
|
|
1531
|
+
*/
|
|
1532
|
+
async #reloadMCP(): Promise<void> {
|
|
1533
|
+
if (!this.ctx.mcpManager) {
|
|
1534
|
+
return;
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
// Disconnect all existing servers
|
|
1538
|
+
await this.ctx.mcpManager.disconnectAll();
|
|
1539
|
+
|
|
1540
|
+
// Rediscover and connect
|
|
1541
|
+
const result = await this.ctx.mcpManager.discoverAndConnect();
|
|
1542
|
+
await this.ctx.session.refreshMCPTools(this.ctx.mcpManager.getTools());
|
|
1543
|
+
|
|
1544
|
+
// Show any connection errors
|
|
1545
|
+
if (result.errors.size > 0) {
|
|
1546
|
+
const errorLines = ["", theme.fg("warning", "Some servers failed to connect:"), ""];
|
|
1547
|
+
for (const [serverName, error] of result.errors.entries()) {
|
|
1548
|
+
errorLines.push(` ${serverName}: ${error}`);
|
|
1549
|
+
}
|
|
1550
|
+
errorLines.push("");
|
|
1551
|
+
this.#showMessage(errorLines.join("\n"));
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
/**
|
|
1556
|
+
* Handle /mcp resources - Show available resources from connected servers
|
|
1557
|
+
*/
|
|
1558
|
+
async #handleResources(): Promise<void> {
|
|
1559
|
+
if (!this.ctx.mcpManager) {
|
|
1560
|
+
this.ctx.showError("No MCP manager available.");
|
|
1561
|
+
return;
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
const servers = this.ctx.mcpManager.getConnectedServers();
|
|
1565
|
+
const lines: string[] = ["", theme.bold("MCP Resources"), ""];
|
|
1566
|
+
let hasAny = false;
|
|
1567
|
+
|
|
1568
|
+
for (const name of servers) {
|
|
1569
|
+
const data = this.ctx.mcpManager.getServerResources(name);
|
|
1570
|
+
if (!data) continue;
|
|
1571
|
+
const { resources, templates } = data;
|
|
1572
|
+
if (resources.length === 0 && templates.length === 0) continue;
|
|
1573
|
+
hasAny = true;
|
|
1574
|
+
|
|
1575
|
+
lines.push(`${theme.fg("accent", name)}:`);
|
|
1576
|
+
for (const r of resources) {
|
|
1577
|
+
const desc = r.description ? ` ${theme.fg("dim", r.description)}` : "";
|
|
1578
|
+
const mime = r.mimeType ? ` ${theme.fg("dim", `[${r.mimeType}]`)}` : "";
|
|
1579
|
+
lines.push(` ${theme.fg("success", r.uri)}${mime}${desc}`);
|
|
1580
|
+
}
|
|
1581
|
+
if (templates.length > 0) {
|
|
1582
|
+
lines.push(` ${theme.fg("muted", "Templates:")}`);
|
|
1583
|
+
for (const t of templates) {
|
|
1584
|
+
const desc = t.description ? ` ${theme.fg("dim", t.description)}` : "";
|
|
1585
|
+
lines.push(` ${theme.fg("accent", t.uriTemplate)}${desc}`);
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
lines.push("");
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
if (!hasAny) {
|
|
1592
|
+
lines.push(theme.fg("muted", "No resources available on connected servers."));
|
|
1593
|
+
lines.push("");
|
|
1594
|
+
}
|
|
1595
|
+
this.#showMessage(lines.join("\n"));
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
/**
|
|
1599
|
+
* Handle /mcp prompts - Show available prompts from connected servers
|
|
1600
|
+
*/
|
|
1601
|
+
async #handlePrompts(): Promise<void> {
|
|
1602
|
+
if (!this.ctx.mcpManager) {
|
|
1603
|
+
this.ctx.showError("No MCP manager available.");
|
|
1604
|
+
return;
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
const servers = this.ctx.mcpManager.getConnectedServers();
|
|
1608
|
+
const lines: string[] = ["", theme.bold("MCP Prompts"), ""];
|
|
1609
|
+
let hasAny = false;
|
|
1610
|
+
|
|
1611
|
+
for (const name of servers) {
|
|
1612
|
+
const prompts = this.ctx.mcpManager.getServerPrompts(name);
|
|
1613
|
+
if (!prompts?.length) continue;
|
|
1614
|
+
hasAny = true;
|
|
1615
|
+
|
|
1616
|
+
lines.push(`${theme.fg("accent", name)}:`);
|
|
1617
|
+
for (const p of prompts) {
|
|
1618
|
+
const commandName = `${name}:${p.name}`;
|
|
1619
|
+
const desc = p.description ? ` ${theme.fg("dim", p.description)}` : "";
|
|
1620
|
+
lines.push(` ${theme.fg("success", `/${commandName}`)}${desc}`);
|
|
1621
|
+
if (p.arguments?.length) {
|
|
1622
|
+
for (const arg of p.arguments) {
|
|
1623
|
+
const required = arg.required ? theme.fg("warning", " *") : "";
|
|
1624
|
+
const argDesc = arg.description ? ` - ${arg.description}` : "";
|
|
1625
|
+
lines.push(` ${arg.name}=${required}${theme.fg("dim", argDesc)}`);
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
lines.push("");
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
if (!hasAny) {
|
|
1633
|
+
lines.push(theme.fg("muted", "No prompts available on connected servers."));
|
|
1634
|
+
lines.push("");
|
|
1635
|
+
}
|
|
1636
|
+
this.#showMessage(lines.join("\n"));
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
/**
|
|
1640
|
+
* Handle /mcp notifications - Show notification and subscription state
|
|
1641
|
+
*/
|
|
1642
|
+
async #handleNotifications(): Promise<void> {
|
|
1643
|
+
if (!this.ctx.mcpManager) {
|
|
1644
|
+
this.ctx.showError("No MCP manager available.");
|
|
1645
|
+
return;
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
const { enabled, subscriptions } = this.ctx.mcpManager.getNotificationState();
|
|
1649
|
+
const servers = this.ctx.mcpManager.getConnectedServers();
|
|
1650
|
+
const statusIcon = enabled ? theme.fg("success", "enabled") : theme.fg("warning", "disabled");
|
|
1651
|
+
const lines: string[] = ["", theme.bold("MCP Notifications"), ""];
|
|
1652
|
+
lines.push(` Status: ${statusIcon} ${theme.fg("dim", "(mcp.notifications setting)")}`);
|
|
1653
|
+
lines.push("");
|
|
1654
|
+
|
|
1655
|
+
let hasAny = false;
|
|
1656
|
+
for (const name of servers) {
|
|
1657
|
+
const connection = this.ctx.mcpManager.getConnection(name);
|
|
1658
|
+
if (!connection) continue;
|
|
1659
|
+
const caps = connection.capabilities;
|
|
1660
|
+
const supportsResources = caps.resources !== undefined;
|
|
1661
|
+
const supportsSubscribe = caps.resources?.subscribe === true;
|
|
1662
|
+
const supportsToolsChanged = caps.tools?.listChanged === true;
|
|
1663
|
+
const supportsPromptsChanged = caps.prompts?.listChanged === true;
|
|
1664
|
+
const supportsResourcesChanged = caps.resources?.listChanged === true;
|
|
1665
|
+
|
|
1666
|
+
const hasNotifications =
|
|
1667
|
+
supportsToolsChanged || supportsPromptsChanged || supportsResourcesChanged || supportsSubscribe;
|
|
1668
|
+
if (!hasNotifications) continue;
|
|
1669
|
+
hasAny = true;
|
|
1670
|
+
|
|
1671
|
+
lines.push(`${theme.fg("accent", name)}:`);
|
|
1672
|
+
const check = theme.fg("success", "✓");
|
|
1673
|
+
const cross = theme.fg("dim", "✗");
|
|
1674
|
+
if (supportsToolsChanged) lines.push(` ${check} tools/list_changed`);
|
|
1675
|
+
if (supportsResourcesChanged) lines.push(` ${check} resources/list_changed`);
|
|
1676
|
+
if (supportsPromptsChanged) lines.push(` ${check} prompts/list_changed`);
|
|
1677
|
+
|
|
1678
|
+
if (supportsSubscribe) {
|
|
1679
|
+
const subscribedUris = subscriptions.get(name);
|
|
1680
|
+
const subCount = subscribedUris?.size ?? 0;
|
|
1681
|
+
const subStatus =
|
|
1682
|
+
enabled && subCount > 0
|
|
1683
|
+
? theme.fg("success", `subscribed (${subCount} URI${subCount !== 1 ? "s" : ""})`)
|
|
1684
|
+
: enabled
|
|
1685
|
+
? theme.fg("muted", "no active subscriptions")
|
|
1686
|
+
: theme.fg("dim", "inactive (notifications disabled)");
|
|
1687
|
+
lines.push(` ${check} resources/subscribe ${subStatus}`);
|
|
1688
|
+
if (enabled && subscribedUris && subscribedUris.size > 0) {
|
|
1689
|
+
for (const uri of subscribedUris) {
|
|
1690
|
+
lines.push(` ${theme.fg("success", "✓")} ${theme.fg("dim", uri)}`);
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
} else if (supportsResources) {
|
|
1694
|
+
lines.push(` ${cross} resources/subscribe ${theme.fg("dim", "not supported")}`);
|
|
1695
|
+
}
|
|
1696
|
+
lines.push("");
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
if (!hasAny) {
|
|
1700
|
+
lines.push(theme.fg("muted", "No servers support notifications."));
|
|
1701
|
+
lines.push("");
|
|
1702
|
+
}
|
|
1703
|
+
this.#showMessage(lines.join("\n"));
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
async #validateSmitheryApiKey(apiKey: string): Promise<void> {
|
|
1707
|
+
await searchSmitheryRegistry("mcp", { limit: 1, apiKey });
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
async #promptSmitheryApiKey(promptLabel: string): Promise<string | null> {
|
|
1711
|
+
for (;;) {
|
|
1712
|
+
const input = await this.ctx.showHookInput(promptLabel);
|
|
1713
|
+
if (input === undefined) return null;
|
|
1714
|
+
const apiKey = input.trim();
|
|
1715
|
+
if (!apiKey) {
|
|
1716
|
+
this.ctx.showError("Smithery API key cannot be empty.");
|
|
1717
|
+
continue;
|
|
1718
|
+
}
|
|
1719
|
+
try {
|
|
1720
|
+
await this.#validateSmitheryApiKey(apiKey);
|
|
1721
|
+
return apiKey;
|
|
1722
|
+
} catch (error) {
|
|
1723
|
+
this.ctx.showError(
|
|
1724
|
+
`Smithery API key validation failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1725
|
+
);
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1730
|
+
async #handleSmitheryLoginWithApiKey(): Promise<boolean> {
|
|
1731
|
+
const apiKey = await this.#promptSmitheryApiKey("Smithery API key (Esc to cancel)");
|
|
1732
|
+
if (!apiKey) return false;
|
|
1733
|
+
await saveSmitheryApiKey(apiKey);
|
|
1734
|
+
this.ctx.showStatus("Smithery API key saved.");
|
|
1735
|
+
return true;
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
async #waitForSmitheryCliApiKey(sessionId: string, signal: AbortSignal): Promise<string> {
|
|
1739
|
+
const pollIntervalMs = 2_000;
|
|
1740
|
+
const timeoutMs = 300_000;
|
|
1741
|
+
const startedAt = Date.now();
|
|
1742
|
+
|
|
1743
|
+
while (!signal.aborted) {
|
|
1744
|
+
if (Date.now() - startedAt >= timeoutMs) {
|
|
1745
|
+
throw new Error("Smithery authorization timed out after 5 minutes.");
|
|
1746
|
+
}
|
|
1747
|
+
const response = await pollSmitheryCliAuthSession(sessionId, signal);
|
|
1748
|
+
if (response.status === "success" && response.apiKey) {
|
|
1749
|
+
return response.apiKey;
|
|
1750
|
+
}
|
|
1751
|
+
if (response.status === "error") {
|
|
1752
|
+
throw new Error(response.message ?? "Smithery authorization failed.");
|
|
1753
|
+
}
|
|
1754
|
+
await Bun.sleep(pollIntervalMs);
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
throw new Error("Smithery authorization cancelled.");
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
async #handleSmitheryBrowserLogin(): Promise<boolean> {
|
|
1761
|
+
const session = await createSmitheryCliAuthSession();
|
|
1762
|
+
const fallbackLoginUrl = getSmitheryLoginUrl();
|
|
1763
|
+
this.#showMessage(
|
|
1764
|
+
[
|
|
1765
|
+
"",
|
|
1766
|
+
theme.bold("Smithery Login"),
|
|
1767
|
+
theme.fg("muted", "Browser authorization started. Complete auth in your browser."),
|
|
1768
|
+
theme.fg("dim", "Authorize URL:"),
|
|
1769
|
+
theme.fg("accent", session.authUrl),
|
|
1770
|
+
theme.fg("dim", `Fallback: ${fallbackLoginUrl}`),
|
|
1771
|
+
"",
|
|
1772
|
+
].join("\n"),
|
|
1773
|
+
);
|
|
1774
|
+
try {
|
|
1775
|
+
openPath(session.authUrl);
|
|
1776
|
+
} catch {
|
|
1777
|
+
// URL is already shown above.
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
const apiKey = await this.#waitForSmitheryCliApiKey(session.sessionId, new AbortController().signal);
|
|
1781
|
+
await this.#validateSmitheryApiKey(apiKey);
|
|
1782
|
+
await saveSmitheryApiKey(apiKey);
|
|
1783
|
+
this.ctx.showStatus("Smithery API key saved.");
|
|
1784
|
+
return true;
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
async #promptSmitheryLogin(reason: string): Promise<boolean> {
|
|
1788
|
+
this.#showMessage(
|
|
1789
|
+
[
|
|
1790
|
+
"",
|
|
1791
|
+
theme.fg("muted", `Smithery authentication required (${reason}).`),
|
|
1792
|
+
theme.fg("muted", "If browser auth fails, you can paste an API key."),
|
|
1793
|
+
"",
|
|
1794
|
+
].join("\n"),
|
|
1795
|
+
);
|
|
1796
|
+
try {
|
|
1797
|
+
return await this.#handleSmitheryBrowserLogin();
|
|
1798
|
+
} catch (error) {
|
|
1799
|
+
this.ctx.showWarning(
|
|
1800
|
+
`Browser authorization failed: ${error instanceof Error ? error.message : String(error)}. Falling back to API key.`,
|
|
1801
|
+
);
|
|
1802
|
+
return await this.#handleSmitheryLoginWithApiKey();
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
#getSmitheryErrorStatus(error: unknown): number | undefined {
|
|
1807
|
+
if (error instanceof SmitheryRegistryError || error instanceof SmitheryConnectError) {
|
|
1808
|
+
return error.status;
|
|
1809
|
+
}
|
|
1810
|
+
return undefined;
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1813
|
+
#toSmitheryAuthReason(status: number): string {
|
|
1814
|
+
return status === 429 ? "rate limited by Smithery" : "forbidden/unauthorized with Smithery";
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
async #requireSmitheryApiKey(reason: string): Promise<string> {
|
|
1818
|
+
let apiKey = await getSmitheryApiKey();
|
|
1819
|
+
if (apiKey) return apiKey;
|
|
1820
|
+
|
|
1821
|
+
const loggedIn = await this.#promptSmitheryLogin(reason);
|
|
1822
|
+
if (!loggedIn) {
|
|
1823
|
+
throw new Error("Smithery login cancelled. Run /mcp smithery-login, then retry /mcp smithery-search.");
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
apiKey = await getSmitheryApiKey();
|
|
1827
|
+
if (!apiKey) {
|
|
1828
|
+
throw new Error("Smithery API key not found after login.");
|
|
1829
|
+
}
|
|
1830
|
+
return apiKey;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
async #runSmitheryOperationWithAuthRetry<T>(operation: (apiKey: string) => Promise<T>, reason: string): Promise<T> {
|
|
1834
|
+
const apiKey = await this.#requireSmitheryApiKey(reason);
|
|
1835
|
+
try {
|
|
1836
|
+
return await operation(apiKey);
|
|
1837
|
+
} catch (error) {
|
|
1838
|
+
const status = this.#getSmitheryErrorStatus(error);
|
|
1839
|
+
if (status === undefined || ![401, 403, 429].includes(status)) {
|
|
1840
|
+
throw error;
|
|
1841
|
+
}
|
|
1842
|
+
const loggedIn = await this.#promptSmitheryLogin(this.#toSmitheryAuthReason(status));
|
|
1843
|
+
if (!loggedIn) {
|
|
1844
|
+
throw error;
|
|
1845
|
+
}
|
|
1846
|
+
const retryApiKey = await this.#requireSmitheryApiKey(reason);
|
|
1847
|
+
return await operation(retryApiKey);
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
async #handleSmitheryLogin(): Promise<void> {
|
|
1852
|
+
const ok = await this.#promptSmitheryLogin("login");
|
|
1853
|
+
if (!ok) {
|
|
1854
|
+
this.ctx.showStatus("Smithery login cancelled.");
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
async #handleSmitheryLogout(): Promise<void> {
|
|
1859
|
+
const removed = await clearSmitheryApiKey();
|
|
1860
|
+
this.ctx.showStatus(removed ? "Smithery API key removed." : "No cached Smithery API key found.");
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
async #nextAvailableServerName(scope: MCPAddScope, baseName: string): Promise<string> {
|
|
1864
|
+
const filePath = getMCPConfigPath(scope, getProjectDir());
|
|
1865
|
+
const config = await readMCPConfigFile(filePath);
|
|
1866
|
+
const existingNames = new Set(Object.keys(config.mcpServers ?? {}));
|
|
1867
|
+
if (!existingNames.has(baseName)) return baseName;
|
|
1868
|
+
for (let i = 2; i <= 999; i++) {
|
|
1869
|
+
const candidate = `${baseName}-${i}`;
|
|
1870
|
+
if (!existingNames.has(candidate)) return candidate;
|
|
1871
|
+
}
|
|
1872
|
+
return `${baseName}-${Date.now()}`;
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1875
|
+
async #promptDeploymentServerName(scope: MCPAddScope, defaultName: string): Promise<string | null> {
|
|
1876
|
+
for (;;) {
|
|
1877
|
+
const input = await this.ctx.showHookInput(`Server name for deploy (default: ${defaultName})`, defaultName);
|
|
1878
|
+
if (input === undefined) return null;
|
|
1879
|
+
const proposed = input.trim() || defaultName;
|
|
1880
|
+
if (!proposed) {
|
|
1881
|
+
this.ctx.showError("Server name cannot be empty.");
|
|
1882
|
+
continue;
|
|
1883
|
+
}
|
|
1884
|
+
const filePath = getMCPConfigPath(scope, getProjectDir());
|
|
1885
|
+
const config = await readMCPConfigFile(filePath);
|
|
1886
|
+
if (config.mcpServers?.[proposed]) {
|
|
1887
|
+
this.ctx.showError(`Server "${proposed}" already exists in ${scope} config.`);
|
|
1888
|
+
continue;
|
|
1889
|
+
}
|
|
1890
|
+
return proposed;
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
async #promptRequiredRegistryInputs(result: SmitherySearchResult): Promise<Record<string, string> | null> {
|
|
1895
|
+
const values: Record<string, string> = {};
|
|
1896
|
+
for (const input of result.requiredInputs) {
|
|
1897
|
+
const label = input.required ? `${input.key} (required)` : `${input.key} (optional)`;
|
|
1898
|
+
const prompt = `${label}${input.description ? ` - ${input.description}` : ""}`;
|
|
1899
|
+
const userInput = await this.ctx.showHookInput(prompt, input.defaultValue);
|
|
1900
|
+
if (userInput === undefined) {
|
|
1901
|
+
if (input.required) return null;
|
|
1902
|
+
continue;
|
|
1903
|
+
}
|
|
1904
|
+
const value = userInput.trim();
|
|
1905
|
+
if (!value) {
|
|
1906
|
+
if (input.required) {
|
|
1907
|
+
this.ctx.showError(`Missing required value for "${input.key}".`);
|
|
1908
|
+
return null;
|
|
1909
|
+
}
|
|
1910
|
+
continue;
|
|
1911
|
+
}
|
|
1912
|
+
values[input.key] = value;
|
|
1913
|
+
}
|
|
1914
|
+
return values;
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
#applyRegistryInputOverrides(config: MCPServerConfig, values: Record<string, string>): MCPServerConfig {
|
|
1918
|
+
if (Object.keys(values).length === 0) return config;
|
|
1919
|
+
if (config.type !== "stdio") {
|
|
1920
|
+
return config;
|
|
1921
|
+
}
|
|
1922
|
+
const args = [...(config.args ?? [])];
|
|
1923
|
+
const configJson = JSON.stringify(values);
|
|
1924
|
+
const index = args.indexOf("--config");
|
|
1925
|
+
if (index >= 0) {
|
|
1926
|
+
if (index + 1 < args.length) {
|
|
1927
|
+
args[index + 1] = configJson;
|
|
1928
|
+
} else {
|
|
1929
|
+
args.push(configJson);
|
|
1930
|
+
}
|
|
1931
|
+
} else {
|
|
1932
|
+
args.push("--config", configJson);
|
|
1933
|
+
}
|
|
1934
|
+
return { ...config, args };
|
|
1935
|
+
}
|
|
1936
|
+
|
|
1937
|
+
async #pickRegistryResult(results: SmitherySearchResult[], keyword: string): Promise<SmitherySearchResult | null> {
|
|
1938
|
+
const options = results.map((result, index) => {
|
|
1939
|
+
const label = `${index + 1}. ${result.display.displayName} (${result.display.transport}, uses ${result.display.useCount})`;
|
|
1940
|
+
return label.length > 120 ? `${label.slice(0, 117)}...` : label;
|
|
1941
|
+
});
|
|
1942
|
+
const selected = await this.ctx.showHookSelector(`Registry results for "${keyword}"`, options);
|
|
1943
|
+
if (!selected) return null;
|
|
1944
|
+
const prefix = selected.split(".", 1)[0];
|
|
1945
|
+
const index = Number(prefix) - 1;
|
|
1946
|
+
if (!Number.isInteger(index) || index < 0 || index >= results.length) return null;
|
|
1947
|
+
return results[index] ?? null;
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
async #deployRegistryResult(result: SmitherySearchResult, scope: MCPAddScope): Promise<void> {
|
|
1951
|
+
const baseName = toConfigName(result.name);
|
|
1952
|
+
const defaultName = await this.#nextAvailableServerName(scope, baseName);
|
|
1953
|
+
const serverName = await this.#promptDeploymentServerName(scope, defaultName);
|
|
1954
|
+
if (!serverName) {
|
|
1955
|
+
this.ctx.showStatus("MCP deploy cancelled.");
|
|
1956
|
+
return;
|
|
1957
|
+
}
|
|
1958
|
+
const inputValues = await this.#promptRequiredRegistryInputs(result);
|
|
1959
|
+
if (inputValues === null) {
|
|
1960
|
+
this.ctx.showStatus("MCP deploy cancelled.");
|
|
1961
|
+
return;
|
|
1962
|
+
}
|
|
1963
|
+
const config = this.#applyRegistryInputOverrides(result.config, inputValues);
|
|
1964
|
+
await this.#handleWizardComplete(serverName, config, scope);
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
async #handleSearch(text: string): Promise<void> {
|
|
1968
|
+
const parsed = this.#parseSearchCommand(text);
|
|
1969
|
+
if (parsed.error) {
|
|
1970
|
+
this.ctx.showError(parsed.error);
|
|
1971
|
+
return;
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
try {
|
|
1975
|
+
this.#showMessage(
|
|
1976
|
+
["", theme.fg("muted", `Searching Smithery registry for "${parsed.keyword}"...`), ""].join("\n"),
|
|
1977
|
+
);
|
|
1978
|
+
const results = await this.#runSmitheryOperationWithAuthRetry(
|
|
1979
|
+
apiKey =>
|
|
1980
|
+
searchSmitheryRegistry(parsed.keyword, {
|
|
1981
|
+
limit: parsed.limit,
|
|
1982
|
+
apiKey,
|
|
1983
|
+
includeSemantic: parsed.semantic,
|
|
1984
|
+
}),
|
|
1985
|
+
"required for smithery-search",
|
|
1986
|
+
);
|
|
1987
|
+
if (results.length === 0) {
|
|
1988
|
+
this.#showMessage(
|
|
1989
|
+
["", theme.fg("warning", `No Smithery results found for "${parsed.keyword}".`), ""].join("\n"),
|
|
1990
|
+
);
|
|
1991
|
+
return;
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
const selected = await this.#pickRegistryResult(results, parsed.keyword);
|
|
1995
|
+
if (!selected) {
|
|
1996
|
+
this.ctx.showStatus("MCP Smithery selection cancelled.");
|
|
1997
|
+
return;
|
|
1998
|
+
}
|
|
1999
|
+
|
|
2000
|
+
await this.#deployRegistryResult(selected, parsed.scope);
|
|
2001
|
+
} catch (error) {
|
|
2002
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2003
|
+
if (/authentication was cancelled|login cancelled/i.test(message)) {
|
|
2004
|
+
this.ctx.showError(`${message} Run /mcp smithery-login to authenticate first.`);
|
|
2005
|
+
return;
|
|
2006
|
+
}
|
|
2007
|
+
this.ctx.showError(`Smithery search failed: ${message}`);
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
|
|
2011
|
+
/**
|
|
2012
|
+
* Show a message in the chat
|
|
2013
|
+
*/
|
|
2014
|
+
#showMessage(text: string): void {
|
|
2015
|
+
showCommandMessage(this.ctx, text);
|
|
2016
|
+
}
|
|
2017
|
+
}
|