@waq666/pneuma 0.0.0-stage → 0.7.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/LICENSE +21 -0
- package/README.md +144 -2
- package/bin/dsc.js +91 -0
- package/bin/headless.cjs +40 -0
- package/lib/adapter/core-runtime.d.ts +11 -0
- package/lib/adapter/core-runtime.js +259 -0
- package/lib/adapter/host.d.ts +18 -0
- package/lib/adapter/host.js +14 -0
- package/lib/adapter/runtime.d.ts +15 -0
- package/lib/adapter/runtime.js +290 -0
- package/lib/adapter/transcript.d.ts +184 -0
- package/lib/adapter/transcript.js +605 -0
- package/lib/app/AgentsOverlay.d.ts +18 -0
- package/lib/app/AgentsOverlay.js +63 -0
- package/lib/app/App.d.ts +13 -0
- package/lib/app/App.js +1498 -0
- package/lib/app/ApprovalCard.d.ts +12 -0
- package/lib/app/ApprovalCard.js +76 -0
- package/lib/app/AskCard.d.ts +20 -0
- package/lib/app/AskCard.js +45 -0
- package/lib/app/ChatView.d.ts +32 -0
- package/lib/app/ChatView.js +198 -0
- package/lib/app/Composer.d.ts +50 -0
- package/lib/app/Composer.js +282 -0
- package/lib/app/DiffBlock.d.ts +6 -0
- package/lib/app/DiffBlock.js +37 -0
- package/lib/app/MarkdownView.d.ts +6 -0
- package/lib/app/MarkdownView.js +99 -0
- package/lib/app/ModelPicker.d.ts +7 -0
- package/lib/app/ModelPicker.js +18 -0
- package/lib/app/PlanReviewCard.d.ts +13 -0
- package/lib/app/PlanReviewCard.js +54 -0
- package/lib/app/PresetPicker.d.ts +11 -0
- package/lib/app/PresetPicker.js +19 -0
- package/lib/app/PreviewOverlay.d.ts +14 -0
- package/lib/app/PreviewOverlay.js +22 -0
- package/lib/app/SessionPicker.d.ts +43 -0
- package/lib/app/SessionPicker.js +56 -0
- package/lib/app/SettingsOverlay.d.ts +32 -0
- package/lib/app/SettingsOverlay.js +157 -0
- package/lib/app/SkillsPicker.d.ts +7 -0
- package/lib/app/SkillsPicker.js +19 -0
- package/lib/app/StatusBar.d.ts +22 -0
- package/lib/app/StatusBar.js +272 -0
- package/lib/app/TaskStrips.d.ts +13 -0
- package/lib/app/TaskStrips.js +40 -0
- package/lib/app/ToolCard.d.ts +5 -0
- package/lib/app/ToolCard.js +40 -0
- package/lib/app/TranscriptOverlay.d.ts +12 -0
- package/lib/app/TranscriptOverlay.js +17 -0
- package/lib/app/Welcome.d.ts +6 -0
- package/lib/app/Welcome.js +66 -0
- package/lib/app/attach.d.ts +16 -0
- package/lib/app/attach.js +58 -0
- package/lib/app/click.d.ts +27 -0
- package/lib/app/click.js +89 -0
- package/lib/app/clipboard-image.d.ts +11 -0
- package/lib/app/clipboard-image.js +66 -0
- package/lib/app/cursor.d.ts +4 -0
- package/lib/app/cursor.js +24 -0
- package/lib/app/hello.d.ts +11 -0
- package/lib/app/hello.js +8 -0
- package/lib/app/history-store.d.ts +4 -0
- package/lib/app/history-store.js +41 -0
- package/lib/app/image-blocks.d.ts +12 -0
- package/lib/app/image-blocks.js +125 -0
- package/lib/app/markdown.d.ts +54 -0
- package/lib/app/markdown.js +189 -0
- package/lib/app/settings-model.d.ts +69 -0
- package/lib/app/settings-model.js +114 -0
- package/lib/app/theme.d.ts +166 -0
- package/lib/app/theme.js +141 -0
- package/lib/boot.d.ts +1 -0
- package/lib/boot.js +34 -0
- package/lib/commands.d.ts +49 -0
- package/lib/commands.js +115 -0
- package/lib/contract.d.ts +1973 -0
- package/lib/contract.js +36 -0
- package/lib/core/agent-roles.d.ts +52 -0
- package/lib/core/agent-roles.js +274 -0
- package/lib/core/approval-floor.d.ts +183 -0
- package/lib/core/approval-floor.js +679 -0
- package/lib/core/approval.d.ts +42 -0
- package/lib/core/approval.js +8 -0
- package/lib/core/audit.d.ts +61 -0
- package/lib/core/audit.js +59 -0
- package/lib/core/boot-notices.d.ts +6 -0
- package/lib/core/boot-notices.js +61 -0
- package/lib/core/cdp/actions.d.ts +182 -0
- package/lib/core/cdp/actions.js +649 -0
- package/lib/core/cdp/launch.d.ts +192 -0
- package/lib/core/cdp/launch.js +491 -0
- package/lib/core/cdp/snapshot.d.ts +139 -0
- package/lib/core/cdp/snapshot.js +316 -0
- package/lib/core/cdp/transport.d.ts +160 -0
- package/lib/core/cdp/transport.js +359 -0
- package/lib/core/command-policy.d.ts +108 -0
- package/lib/core/command-policy.js +580 -0
- package/lib/core/commands-completion.d.ts +78 -0
- package/lib/core/commands-completion.js +167 -0
- package/lib/core/compact-anchors.d.ts +76 -0
- package/lib/core/compact-anchors.js +284 -0
- package/lib/core/compact.d.ts +110 -0
- package/lib/core/compact.js +210 -0
- package/lib/core/config-store.d.ts +38 -0
- package/lib/core/config-store.js +307 -0
- package/lib/core/config.d.ts +36 -0
- package/lib/core/config.js +124 -0
- package/lib/core/connection-test.d.ts +60 -0
- package/lib/core/connection-test.js +84 -0
- package/lib/core/desktop-control.d.ts +298 -0
- package/lib/core/desktop-control.js +1311 -0
- package/lib/core/diff-text.d.ts +74 -0
- package/lib/core/diff-text.js +257 -0
- package/lib/core/dock-browser.d.ts +137 -0
- package/lib/core/dock-browser.js +146 -0
- package/lib/core/dock-ops.d.ts +59 -0
- package/lib/core/dock-ops.js +104 -0
- package/lib/core/dock-session.d.ts +39 -0
- package/lib/core/dock-session.js +28 -0
- package/lib/core/dsh-compat/hook.d.ts +18 -0
- package/lib/core/dsh-compat/hook.js +52 -0
- package/lib/core/dsh-compat/resolve-hook.d.ts +5 -0
- package/lib/core/dsh-compat/resolve-hook.js +59 -0
- package/lib/core/dsh-compat/tools-facade.d.ts +47 -0
- package/lib/core/dsh-compat/tools-facade.js +108 -0
- package/lib/core/err-text.d.ts +13 -0
- package/lib/core/err-text.js +15 -0
- package/lib/core/events.d.ts +149 -0
- package/lib/core/events.js +1 -0
- package/lib/core/git-exec.d.ts +23 -0
- package/lib/core/git-exec.js +72 -0
- package/lib/core/git-info.d.ts +47 -0
- package/lib/core/git-info.js +157 -0
- package/lib/core/goal.d.ts +89 -0
- package/lib/core/goal.js +189 -0
- package/lib/core/hooks.d.ts +272 -0
- package/lib/core/hooks.js +818 -0
- package/lib/core/host-browser-link.d.ts +59 -0
- package/lib/core/host-browser-link.js +9 -0
- package/lib/core/host-methods.d.ts +25 -0
- package/lib/core/host-methods.js +103 -0
- package/lib/core/image-diet.d.ts +77 -0
- package/lib/core/image-diet.js +127 -0
- package/lib/core/learnings/ledger.d.ts +73 -0
- package/lib/core/learnings/ledger.js +273 -0
- package/lib/core/learnings/skill-write.d.ts +250 -0
- package/lib/core/learnings/skill-write.js +854 -0
- package/lib/core/learnings/store.d.ts +102 -0
- package/lib/core/learnings/store.js +266 -0
- package/lib/core/lifecycle-hooks.d.ts +189 -0
- package/lib/core/lifecycle-hooks.js +576 -0
- package/lib/core/llm.d.ts +272 -0
- package/lib/core/llm.js +548 -0
- package/lib/core/lockfile.d.ts +17 -0
- package/lib/core/lockfile.js +110 -0
- package/lib/core/loop.d.ts +185 -0
- package/lib/core/loop.js +587 -0
- package/lib/core/lsp/client.d.ts +189 -0
- package/lib/core/lsp/client.js +690 -0
- package/lib/core/lsp/framing.d.ts +84 -0
- package/lib/core/lsp/framing.js +181 -0
- package/lib/core/lsp/line-shift.d.ts +28 -0
- package/lib/core/lsp/line-shift.js +127 -0
- package/lib/core/lsp/manager.d.ts +93 -0
- package/lib/core/lsp/manager.js +418 -0
- package/lib/core/lsp/normalize.d.ts +32 -0
- package/lib/core/lsp/normalize.js +139 -0
- package/lib/core/lsp/servers.d.ts +106 -0
- package/lib/core/lsp/servers.js +352 -0
- package/lib/core/lsp/uri.d.ts +76 -0
- package/lib/core/lsp/uri.js +179 -0
- package/lib/core/market.d.ts +51 -0
- package/lib/core/market.js +358 -0
- package/lib/core/mcp.d.ts +201 -0
- package/lib/core/mcp.js +836 -0
- package/lib/core/memory-eval.d.ts +158 -0
- package/lib/core/memory-eval.js +215 -0
- package/lib/core/memory.d.ts +266 -0
- package/lib/core/memory.js +703 -0
- package/lib/core/mention.d.ts +57 -0
- package/lib/core/mention.js +129 -0
- package/lib/core/microcompact.d.ts +48 -0
- package/lib/core/microcompact.js +79 -0
- package/lib/core/migrate.d.ts +56 -0
- package/lib/core/migrate.js +134 -0
- package/lib/core/model-caps.d.ts +83 -0
- package/lib/core/model-caps.js +150 -0
- package/lib/core/model-discovery.d.ts +19 -0
- package/lib/core/model-discovery.js +69 -0
- package/lib/core/modes.d.ts +72 -0
- package/lib/core/modes.js +152 -0
- package/lib/core/native-bridge.d.ts +135 -0
- package/lib/core/native-bridge.js +563 -0
- package/lib/core/path-policy.d.ts +32 -0
- package/lib/core/path-policy.js +248 -0
- package/lib/core/permission-presets.d.ts +67 -0
- package/lib/core/permission-presets.js +75 -0
- package/lib/core/plugin-loader.d.ts +36 -0
- package/lib/core/plugin-loader.js +294 -0
- package/lib/core/plugin-registry.d.ts +113 -0
- package/lib/core/plugin-registry.js +256 -0
- package/lib/core/prefs.d.ts +82 -0
- package/lib/core/prefs.js +372 -0
- package/lib/core/presets.d.ts +80 -0
- package/lib/core/presets.js +373 -0
- package/lib/core/pricing.d.ts +90 -0
- package/lib/core/pricing.js +126 -0
- package/lib/core/prompt.d.ts +92 -0
- package/lib/core/prompt.js +339 -0
- package/lib/core/ptc.d.ts +66 -0
- package/lib/core/ptc.js +70 -0
- package/lib/core/remote/frames.d.ts +91 -0
- package/lib/core/remote/frames.js +174 -0
- package/lib/core/remote/notify.d.ts +54 -0
- package/lib/core/remote/notify.js +77 -0
- package/lib/core/remote/owner.d.ts +32 -0
- package/lib/core/remote/owner.js +75 -0
- package/lib/core/remote/pairing.d.ts +104 -0
- package/lib/core/remote/pairing.js +261 -0
- package/lib/core/remote/push.d.ts +80 -0
- package/lib/core/remote/push.js +222 -0
- package/lib/core/remote/store.d.ts +18 -0
- package/lib/core/remote/store.js +59 -0
- package/lib/core/remote/tickets.d.ts +20 -0
- package/lib/core/remote/tickets.js +49 -0
- package/lib/core/remote/uploads.d.ts +63 -0
- package/lib/core/remote/uploads.js +161 -0
- package/lib/core/request-attribution.d.ts +26 -0
- package/lib/core/request-attribution.js +86 -0
- package/lib/core/request-invariant.d.ts +61 -0
- package/lib/core/request-invariant.js +199 -0
- package/lib/core/sandbox/backends.d.ts +64 -0
- package/lib/core/sandbox/backends.js +194 -0
- package/lib/core/sandbox/execpolicy.d.ts +93 -0
- package/lib/core/sandbox/execpolicy.js +452 -0
- package/lib/core/sandbox/net-proxy.d.ts +31 -0
- package/lib/core/sandbox/net-proxy.js +162 -0
- package/lib/core/sandbox/policy.d.ts +211 -0
- package/lib/core/sandbox/policy.js +559 -0
- package/lib/core/sandbox/win/abi.d.ts +161 -0
- package/lib/core/sandbox/win/abi.js +169 -0
- package/lib/core/sandbox/win/account.d.ts +71 -0
- package/lib/core/sandbox/win/account.js +206 -0
- package/lib/core/sandbox/win/acl.d.ts +54 -0
- package/lib/core/sandbox/win/acl.js +339 -0
- package/lib/core/sandbox/win/backend.d.ts +61 -0
- package/lib/core/sandbox/win/backend.js +249 -0
- package/lib/core/sandbox/win/doctor.d.ts +24 -0
- package/lib/core/sandbox/win/doctor.js +272 -0
- package/lib/core/sandbox/win/ffi.d.ts +107 -0
- package/lib/core/sandbox/win/ffi.js +166 -0
- package/lib/core/sandbox/win/net-abi.d.ts +154 -0
- package/lib/core/sandbox/win/net-abi.js +316 -0
- package/lib/core/sandbox/win/runner-entry.d.ts +35 -0
- package/lib/core/sandbox/win/runner-entry.js +391 -0
- package/lib/core/sandbox/win/setup.d.ts +60 -0
- package/lib/core/sandbox/win/setup.js +986 -0
- package/lib/core/sandbox/win/token.d.ts +35 -0
- package/lib/core/sandbox/win/token.js +238 -0
- package/lib/core/sandbox/win/wfp.d.ts +41 -0
- package/lib/core/sandbox/win/wfp.js +200 -0
- package/lib/core/sandbox/win/workspace-sid.d.ts +9 -0
- package/lib/core/sandbox/win/workspace-sid.js +45 -0
- package/lib/core/schedule/rule.d.ts +184 -0
- package/lib/core/schedule/rule.js +745 -0
- package/lib/core/schedule/runner.d.ts +185 -0
- package/lib/core/schedule/runner.js +678 -0
- package/lib/core/schedule/store.d.ts +182 -0
- package/lib/core/schedule/store.js +455 -0
- package/lib/core/secrets.d.ts +41 -0
- package/lib/core/secrets.js +150 -0
- package/lib/core/session-export.d.ts +18 -0
- package/lib/core/session-export.js +73 -0
- package/lib/core/session-index.d.ts +159 -0
- package/lib/core/session-index.js +683 -0
- package/lib/core/session-meta.d.ts +41 -0
- package/lib/core/session-meta.js +97 -0
- package/lib/core/session.d.ts +356 -0
- package/lib/core/session.js +998 -0
- package/lib/core/skills.d.ts +60 -0
- package/lib/core/skills.js +171 -0
- package/lib/core/spill.d.ts +159 -0
- package/lib/core/spill.js +329 -0
- package/lib/core/surface-registry.d.ts +45 -0
- package/lib/core/surface-registry.js +67 -0
- package/lib/core/team-board.d.ts +95 -0
- package/lib/core/team-board.js +316 -0
- package/lib/core/throughput.d.ts +46 -0
- package/lib/core/throughput.js +40 -0
- package/lib/core/todo-reminder.d.ts +44 -0
- package/lib/core/todo-reminder.js +55 -0
- package/lib/core/todo.d.ts +81 -0
- package/lib/core/todo.js +222 -0
- package/lib/core/token-estimate.d.ts +55 -0
- package/lib/core/token-estimate.js +82 -0
- package/lib/core/tool-guards.d.ts +103 -0
- package/lib/core/tool-guards.js +77 -0
- package/lib/core/tool-search.d.ts +152 -0
- package/lib/core/tool-search.js +311 -0
- package/lib/core/tools/bash.d.ts +281 -0
- package/lib/core/tools/bash.js +871 -0
- package/lib/core/tools/command-runner.d.ts +84 -0
- package/lib/core/tools/command-runner.js +68 -0
- package/lib/core/tools/fs-tools.d.ts +23 -0
- package/lib/core/tools/fs-tools.js +247 -0
- package/lib/core/tools/index.d.ts +8 -0
- package/lib/core/tools/index.js +4 -0
- package/lib/core/tools/sandbox-args.d.ts +51 -0
- package/lib/core/tools/sandbox-args.js +90 -0
- package/lib/core/tools/search-tools.d.ts +3 -0
- package/lib/core/tools/search-tools.js +153 -0
- package/lib/core/tools.d.ts +96 -0
- package/lib/core/tools.js +53 -0
- package/lib/core/ui-boot-source.d.ts +71 -0
- package/lib/core/ui-boot-source.js +121 -0
- package/lib/core/ui-bundles.d.ts +249 -0
- package/lib/core/ui-bundles.js +830 -0
- package/lib/core/ui-combo.d.ts +49 -0
- package/lib/core/ui-combo.js +55 -0
- package/lib/core/ui-events.d.ts +81 -0
- package/lib/core/ui-events.js +364 -0
- package/lib/core/ui-hmr.d.ts +36 -0
- package/lib/core/ui-hmr.js +128 -0
- package/lib/core/ui-methods.d.ts +69 -0
- package/lib/core/ui-methods.js +341 -0
- package/lib/core/ui-route.d.ts +69 -0
- package/lib/core/ui-route.js +122 -0
- package/lib/core/ui-server.d.ts +75 -0
- package/lib/core/ui-server.js +369 -0
- package/lib/core/ui-trust.d.ts +47 -0
- package/lib/core/ui-trust.js +164 -0
- package/lib/core/untrusted.d.ts +30 -0
- package/lib/core/untrusted.js +48 -0
- package/lib/core/update-check.d.ts +81 -0
- package/lib/core/update-check.js +177 -0
- package/lib/core/usage-log.d.ts +82 -0
- package/lib/core/usage-log.js +306 -0
- package/lib/core/version.d.ts +1 -0
- package/lib/core/version.js +37 -0
- package/lib/core/web-http.d.ts +57 -0
- package/lib/core/web-http.js +137 -0
- package/lib/core/web-static.d.ts +31 -0
- package/lib/core/web-static.js +75 -0
- package/lib/headless.d.ts +1 -0
- package/lib/headless.js +31 -0
- package/lib/host/kernel.d.ts +33 -0
- package/lib/host/kernel.js +451 -0
- package/lib/index.d.ts +41 -0
- package/lib/index.js +35 -0
- package/lib/plugins/agent.d.ts +2 -0
- package/lib/plugins/agent.js +274 -0
- package/lib/plugins/approval-floor.d.ts +2 -0
- package/lib/plugins/approval-floor.js +223 -0
- package/lib/plugins/approval.d.ts +16 -0
- package/lib/plugins/approval.js +674 -0
- package/lib/plugins/ask.d.ts +2 -0
- package/lib/plugins/ask.js +245 -0
- package/lib/plugins/browser.d.ts +68 -0
- package/lib/plugins/browser.js +1341 -0
- package/lib/plugins/commands.d.ts +23 -0
- package/lib/plugins/commands.js +498 -0
- package/lib/plugins/compact.d.ts +21 -0
- package/lib/plugins/compact.js +274 -0
- package/lib/plugins/computer-use.d.ts +47 -0
- package/lib/plugins/computer-use.js +539 -0
- package/lib/plugins/desktop-dock.d.ts +2 -0
- package/lib/plugins/desktop-dock.js +626 -0
- package/lib/plugins/dock-browser.d.ts +34 -0
- package/lib/plugins/dock-browser.js +765 -0
- package/lib/plugins/dsh-compat.d.ts +25 -0
- package/lib/plugins/dsh-compat.js +37 -0
- package/lib/plugins/file-review.d.ts +108 -0
- package/lib/plugins/file-review.js +393 -0
- package/lib/plugins/git.d.ts +26 -0
- package/lib/plugins/git.js +132 -0
- package/lib/plugins/goal.d.ts +20 -0
- package/lib/plugins/goal.js +245 -0
- package/lib/plugins/guards.d.ts +14 -0
- package/lib/plugins/guards.js +18 -0
- package/lib/plugins/hooks.d.ts +19 -0
- package/lib/plugins/hooks.js +583 -0
- package/lib/plugins/host-stdio.d.ts +88 -0
- package/lib/plugins/host-stdio.js +317 -0
- package/lib/plugins/image-diet.d.ts +17 -0
- package/lib/plugins/image-diet.js +85 -0
- package/lib/plugins/lifecycle-hooks.d.ts +26 -0
- package/lib/plugins/lifecycle-hooks.js +338 -0
- package/lib/plugins/llm.d.ts +13 -0
- package/lib/plugins/llm.js +215 -0
- package/lib/plugins/lsp.d.ts +54 -0
- package/lib/plugins/lsp.js +608 -0
- package/lib/plugins/mcp.d.ts +16 -0
- package/lib/plugins/mcp.js +673 -0
- package/lib/plugins/memory.d.ts +17 -0
- package/lib/plugins/memory.js +858 -0
- package/lib/plugins/microcompact.d.ts +18 -0
- package/lib/plugins/microcompact.js +119 -0
- package/lib/plugins/mode.d.ts +18 -0
- package/lib/plugins/mode.js +115 -0
- package/lib/plugins/permission-presets.d.ts +18 -0
- package/lib/plugins/permission-presets.js +135 -0
- package/lib/plugins/plan.d.ts +2 -0
- package/lib/plugins/plan.js +192 -0
- package/lib/plugins/plugin-manager.d.ts +2 -0
- package/lib/plugins/plugin-manager.js +165 -0
- package/lib/plugins/presets.d.ts +2 -0
- package/lib/plugins/presets.js +182 -0
- package/lib/plugins/prompt.d.ts +2 -0
- package/lib/plugins/prompt.js +264 -0
- package/lib/plugins/ptc.d.ts +2 -0
- package/lib/plugins/ptc.js +272 -0
- package/lib/plugins/remote/http.d.ts +11 -0
- package/lib/plugins/remote/http.js +11 -0
- package/lib/plugins/remote/routes.d.ts +24 -0
- package/lib/plugins/remote/routes.js +384 -0
- package/lib/plugins/remote/types.d.ts +135 -0
- package/lib/plugins/remote/types.js +87 -0
- package/lib/plugins/remote.d.ts +17 -0
- package/lib/plugins/remote.js +1028 -0
- package/lib/plugins/runtime-api.d.ts +16 -0
- package/lib/plugins/runtime-api.js +99 -0
- package/lib/plugins/runtime.d.ts +10 -0
- package/lib/plugins/runtime.js +451 -0
- package/lib/plugins/sandbox.d.ts +2 -0
- package/lib/plugins/sandbox.js +761 -0
- package/lib/plugins/schedule.d.ts +26 -0
- package/lib/plugins/schedule.js +538 -0
- package/lib/plugins/self-improve.d.ts +61 -0
- package/lib/plugins/self-improve.js +953 -0
- package/lib/plugins/session-search.d.ts +16 -0
- package/lib/plugins/session-search.js +308 -0
- package/lib/plugins/session-title.d.ts +19 -0
- package/lib/plugins/session-title.js +109 -0
- package/lib/plugins/session.d.ts +7 -0
- package/lib/plugins/session.js +296 -0
- package/lib/plugins/settings.d.ts +3 -0
- package/lib/plugins/settings.js +705 -0
- package/lib/plugins/skills.d.ts +2 -0
- package/lib/plugins/skills.js +360 -0
- package/lib/plugins/spill.d.ts +16 -0
- package/lib/plugins/spill.js +200 -0
- package/lib/plugins/subagent.d.ts +2 -0
- package/lib/plugins/subagent.js +862 -0
- package/lib/plugins/surfaces.d.ts +11 -0
- package/lib/plugins/surfaces.js +20 -0
- package/lib/plugins/team.d.ts +16 -0
- package/lib/plugins/team.js +149 -0
- package/lib/plugins/todo.d.ts +19 -0
- package/lib/plugins/todo.js +188 -0
- package/lib/plugins/tool-search.d.ts +2 -0
- package/lib/plugins/tool-search.js +425 -0
- package/lib/plugins/tools-default.d.ts +12 -0
- package/lib/plugins/tools-default.js +275 -0
- package/lib/plugins/tools.d.ts +16 -0
- package/lib/plugins/tools.js +62 -0
- package/lib/plugins/transcript.d.ts +15 -0
- package/lib/plugins/transcript.js +311 -0
- package/lib/plugins/tui.d.ts +2 -0
- package/lib/plugins/tui.js +98 -0
- package/lib/plugins/waiting.d.ts +12 -0
- package/lib/plugins/waiting.js +34 -0
- package/lib/plugins/web-search.d.ts +14 -0
- package/lib/plugins/web-search.js +263 -0
- package/lib/plugins/webserver.d.ts +48 -0
- package/lib/plugins/webserver.js +228 -0
- package/lib/remote/assets/app-DAcUCOEn.js +14 -0
- package/lib/remote/assets/icon-192.png +0 -0
- package/lib/remote/assets/icon-512.png +0 -0
- package/lib/remote/assets/index.html +34 -0
- package/lib/remote/assets/manifest.json +27 -0
- package/lib/remote/assets/style-DIB6729x.css +1 -0
- package/lib/remote/assets/sw.js +66 -0
- package/lib/services/types.d.ts +1228 -0
- package/lib/services/types.js +1 -0
- package/lib/tools/config-cli.d.ts +1 -0
- package/lib/tools/config-cli.js +89 -0
- package/lib/ui/arrival.d.ts +72 -0
- package/lib/ui/arrival.js +105 -0
- package/lib/ui/assets/app-B70nVIKd.js +69 -0
- package/lib/ui/assets/chunk-2UxHyX5q.js +1 -0
- package/lib/ui/assets/chunk-B1dDrJ26.js +1 -0
- package/lib/ui/assets/chunk-B6aJPvgy.js +1 -0
- package/lib/ui/assets/chunk-BEwlwnbL.js +1 -0
- package/lib/ui/assets/chunk-BGcgwlCI.js +12 -0
- package/lib/ui/assets/chunk-BIGW1oBm.js +1 -0
- package/lib/ui/assets/chunk-BPQ3VLAy.js +1 -0
- package/lib/ui/assets/chunk-BRH0KZbk.js +141 -0
- package/lib/ui/assets/chunk-BX6H_ySx.js +1 -0
- package/lib/ui/assets/chunk-B_P3UbPa.js +1 -0
- package/lib/ui/assets/chunk-B_TYUkTI.js +1 -0
- package/lib/ui/assets/chunk-B_vNuMnf.js +1 -0
- package/lib/ui/assets/chunk-BajyYBss.js +10 -0
- package/lib/ui/assets/chunk-BdnUsdx6.js +1 -0
- package/lib/ui/assets/chunk-Buea-lGh.js +1 -0
- package/lib/ui/assets/chunk-C0PvZCCE.js +1 -0
- package/lib/ui/assets/chunk-C0m74zRD.js +1 -0
- package/lib/ui/assets/chunk-C27-OAKa.js +1 -0
- package/lib/ui/assets/chunk-C2oV4EkX.js +1 -0
- package/lib/ui/assets/chunk-C7zT0LnQ.js +1 -0
- package/lib/ui/assets/chunk-C9GSunVT.js +1 -0
- package/lib/ui/assets/chunk-C9tS-k6U.js +1 -0
- package/lib/ui/assets/chunk-CHLpvVh8.js +1 -0
- package/lib/ui/assets/chunk-CLxacb5B.js +1 -0
- package/lib/ui/assets/chunk-COt5Ahok.js +1 -0
- package/lib/ui/assets/chunk-CPTIWCL2.js +1 -0
- package/lib/ui/assets/chunk-CQLl-L-p.js +1 -0
- package/lib/ui/assets/chunk-CUWRgxuq.js +55 -0
- package/lib/ui/assets/chunk-CWanVSXC.js +1 -0
- package/lib/ui/assets/chunk-Cf5RLm7j.js +1 -0
- package/lib/ui/assets/chunk-CickPsom.js +1 -0
- package/lib/ui/assets/chunk-Cj5Yp3dK.js +1 -0
- package/lib/ui/assets/chunk-Cmh6b_Ma.js +1 -0
- package/lib/ui/assets/chunk-Cp-IABpG.js +1 -0
- package/lib/ui/assets/chunk-CqE71os6.js +1 -0
- package/lib/ui/assets/chunk-CtKP3W-j.js +1 -0
- package/lib/ui/assets/chunk-Cvjx9yec.js +1 -0
- package/lib/ui/assets/chunk-CxGP3dtY.js +1 -0
- package/lib/ui/assets/chunk-CylS5w8V.js +1 -0
- package/lib/ui/assets/chunk-D38fbIBN.js +1 -0
- package/lib/ui/assets/chunk-D97Zzqfu.js +1 -0
- package/lib/ui/assets/chunk-DAi9KRSo.js +1 -0
- package/lib/ui/assets/chunk-DHK4yRfg.js +1 -0
- package/lib/ui/assets/chunk-DJeabLx-.js +1 -0
- package/lib/ui/assets/chunk-DNRxmY5a.js +1 -0
- package/lib/ui/assets/chunk-DNSN1z5r.js +1 -0
- package/lib/ui/assets/chunk-DNasK_ZG.js +1 -0
- package/lib/ui/assets/chunk-DPvrY3IQ.js +1 -0
- package/lib/ui/assets/chunk-DXmwc3jG.js +1 -0
- package/lib/ui/assets/chunk-Dd0kTxKP.js +1 -0
- package/lib/ui/assets/chunk-De0Btta0.js +1 -0
- package/lib/ui/assets/chunk-Des-eS-w.js +1 -0
- package/lib/ui/assets/chunk-Df6bDoY_.js +1 -0
- package/lib/ui/assets/chunk-DijEV5ha.js +1 -0
- package/lib/ui/assets/chunk-DkvoVjww.js +1 -0
- package/lib/ui/assets/chunk-DlC-OlHO.js +1 -0
- package/lib/ui/assets/chunk-DnzrXTVv.js +1 -0
- package/lib/ui/assets/chunk-Dualf0xi.js +1 -0
- package/lib/ui/assets/chunk-EXT84bkm.js +1 -0
- package/lib/ui/assets/chunk-GwR3hkPK.js +1 -0
- package/lib/ui/assets/chunk-P80f7IUj.js +1 -0
- package/lib/ui/assets/chunk-VOosw3JB.js +1 -0
- package/lib/ui/assets/chunk-YQ4lDkt_.js +140 -0
- package/lib/ui/assets/chunk-Yzrsuije.js +1 -0
- package/lib/ui/assets/chunk-Z72CF80r.js +1 -0
- package/lib/ui/assets/chunk-g9-lgVsj.js +1 -0
- package/lib/ui/assets/chunk-vGWfd6FD.js +1 -0
- package/lib/ui/assets/chunk-wDzz0qaB.js +1 -0
- package/lib/ui/assets/chunk-ySF7HWmS.js +1 -0
- package/lib/ui/assets/icon.png +0 -0
- package/lib/ui/assets/index.html +14 -0
- package/lib/ui/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
- package/lib/ui/assets/style-Bq01xH_b.css +1 -0
- package/lib/ui/frames.d.ts +67 -0
- package/lib/ui/frames.js +146 -0
- package/lib/ui/index.d.ts +59 -0
- package/lib/ui/index.js +50 -0
- package/lib/ui/manifest.d.ts +172 -0
- package/lib/ui/manifest.js +158 -0
- package/lib/ui/plugin.d.ts +161 -0
- package/lib/ui/plugin.js +7 -0
- package/lib/ui/plugins/browser/client.js +290 -0
- package/lib/ui/plugins/browser/icon.png +0 -0
- package/lib/ui/plugins/composer/client.js +1843 -0
- package/lib/ui/plugins/composer/icon.png +0 -0
- package/lib/ui/plugins/files/client.js +646 -0
- package/lib/ui/plugins/files/icon.png +0 -0
- package/lib/ui/plugins/manifest.json +107 -0
- package/lib/ui/plugins/plugins/client.js +682 -0
- package/lib/ui/plugins/plugins/icon.png +0 -0
- package/lib/ui/plugins/preview/client.js +197 -0
- package/lib/ui/plugins/preview/icon.png +0 -0
- package/lib/ui/plugins/prompt/client.js +653 -0
- package/lib/ui/plugins/prompt/icon.png +0 -0
- package/lib/ui/plugins/sessions/client.js +1577 -0
- package/lib/ui/plugins/sessions/icon.png +0 -0
- package/lib/ui/plugins/settings/client.js +5470 -0
- package/lib/ui/plugins/settings/icon.png +0 -0
- package/lib/ui/plugins/skills/client.js +485 -0
- package/lib/ui/plugins/skills/icon.png +0 -0
- package/lib/ui/plugins/status/client.js +1420 -0
- package/lib/ui/plugins/status/icon.png +0 -0
- package/lib/ui/plugins/statusbar/client.js +504 -0
- package/lib/ui/plugins/statusbar/icon.png +0 -0
- package/lib/ui/plugins/tasks/client.js +52 -0
- package/lib/ui/plugins/tasks/icon.png +0 -0
- package/lib/ui/plugins/team/client.js +527 -0
- package/lib/ui/plugins/team/icon.png +0 -0
- package/lib/ui/plugins/terminal/client.js +232 -0
- package/lib/ui/plugins/terminal/icon.png +0 -0
- package/lib/ui/plugins/trace/client.js +1376 -0
- package/lib/ui/plugins/trace/icon.png +0 -0
- package/lib/ui/plugins/transcript/client.js +2661 -0
- package/lib/ui/plugins/transcript/icon.png +0 -0
- package/lib/ui/scoped-slots.d.ts +20 -0
- package/lib/ui/scoped-slots.js +21 -0
- package/lib/ui/sdk/@dsc/ui/index.d.ts +242 -0
- package/lib/ui/sdk/@dsc/ui/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-core/contract.d.ts +1973 -0
- package/lib/ui/sdk/@dsc/ui-core/core/diff-text.d.ts +74 -0
- package/lib/ui/sdk/@dsc/ui-core/core/dock-browser.d.ts +137 -0
- package/lib/ui/sdk/@dsc/ui-core/core/dock-ops.d.ts +59 -0
- package/lib/ui/sdk/@dsc/ui-core/core/model-caps.d.ts +83 -0
- package/lib/ui/sdk/@dsc/ui-core/core/native-bridge.d.ts +135 -0
- package/lib/ui/sdk/@dsc/ui-core/core/token-estimate.d.ts +55 -0
- package/lib/ui/sdk/@dsc/ui-core/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/arrival.d.ts +72 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/frames.d.ts +67 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/index.d.ts +59 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/manifest.d.ts +172 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/plugin.d.ts +161 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/scoped-slots.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/services-react.d.ts +27 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/services.d.ts +63 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/slots-react.d.ts +28 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/slots.d.ts +481 -0
- package/lib/ui/sdk/@dsc/ui-core/ui/store.d.ts +89 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/dock-model.d.ts +238 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/dock-state.d.ts +163 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/dock.d.ts +24 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/index.d.ts +15 -0
- package/lib/ui/sdk/@dsc/ui-dockkit/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-layout/app.d.ts +74 -0
- package/lib/ui/sdk/@dsc/ui-layout/appearance.d.ts +70 -0
- package/lib/ui/sdk/@dsc/ui-layout/host-channel.d.ts +112 -0
- package/lib/ui/sdk/@dsc/ui-layout/index.d.ts +34 -0
- package/lib/ui/sdk/@dsc/ui-layout/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-layout/page-state.d.ts +14 -0
- package/lib/ui/sdk/@dsc/ui-layout/review-pane.d.ts +21 -0
- package/lib/ui/sdk/@dsc/ui-layout/runtime-context.d.ts +44 -0
- package/lib/ui/sdk/@dsc/ui-layout/thread-view.d.ts +28 -0
- package/lib/ui/sdk/@dsc/ui-layout/update-button.d.ts +21 -0
- package/lib/ui/sdk/@dsc/ui-primitives/code-file-icon-artwork.d.ts +53 -0
- package/lib/ui/sdk/@dsc/ui-primitives/code-view.d.ts +15 -0
- package/lib/ui/sdk/@dsc/ui-primitives/compat.d.ts +27 -0
- package/lib/ui/sdk/@dsc/ui-primitives/completion-sound.d.ts +24 -0
- package/lib/ui/sdk/@dsc/ui-primitives/confirm.d.ts +33 -0
- package/lib/ui/sdk/@dsc/ui-primitives/css.d.ts +47 -0
- package/lib/ui/sdk/@dsc/ui-primitives/diff-rows.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-primitives/file-icons.d.ts +38 -0
- package/lib/ui/sdk/@dsc/ui-primitives/file-type-classify.d.ts +66 -0
- package/lib/ui/sdk/@dsc/ui-primitives/file-type-icon.d.ts +57 -0
- package/lib/ui/sdk/@dsc/ui-primitives/highlight.d.ts +24 -0
- package/lib/ui/sdk/@dsc/ui-primitives/hover-delay.d.ts +17 -0
- package/lib/ui/sdk/@dsc/ui-primitives/icons.d.ts +109 -0
- package/lib/ui/sdk/@dsc/ui-primitives/index.d.ts +55 -0
- package/lib/ui/sdk/@dsc/ui-primitives/lang-table.d.ts +10 -0
- package/lib/ui/sdk/@dsc/ui-primitives/markdown.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-primitives/native.d.ts +41 -0
- package/lib/ui/sdk/@dsc/ui-primitives/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-primitives/panels.d.ts +86 -0
- package/lib/ui/sdk/@dsc/ui-primitives/paths.d.ts +11 -0
- package/lib/ui/sdk/@dsc/ui-primitives/pdf-view.d.ts +13 -0
- package/lib/ui/sdk/@dsc/ui-primitives/plugin-text.d.ts +15 -0
- package/lib/ui/sdk/@dsc/ui-primitives/select.d.ts +35 -0
- package/lib/ui/sdk/@dsc/ui-primitives/table-view.d.ts +23 -0
- package/lib/ui/sdk/@dsc/ui-primitives/thread-resizer.d.ts +22 -0
- package/lib/ui/sdk/@dsc/ui-primitives/time.d.ts +33 -0
- package/lib/ui/sdk/@dsc/ui-primitives/toast.d.ts +16 -0
- package/lib/ui/sdk/@dsc/ui-primitives/tokens.d.ts +14 -0
- package/lib/ui/sdk/@dsc/ui-primitives/tool-args.d.ts +49 -0
- package/lib/ui/sdk/@dsc/ui-primitives/tool-summary.d.ts +64 -0
- package/lib/ui/sdk/@dsc/ui-renderer/boot.d.ts +121 -0
- package/lib/ui/sdk/@dsc/ui-renderer/hmr.d.ts +43 -0
- package/lib/ui/sdk/@dsc/ui-renderer/index.d.ts +20 -0
- package/lib/ui/sdk/@dsc/ui-renderer/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-slots/index.d.ts +19 -0
- package/lib/ui/sdk/@dsc/ui-slots/package.json +16 -0
- package/lib/ui/sdk/@dsc/ui-slots/slots-react.d.ts +48 -0
- package/lib/ui/sdk/@dsc/ui-slots/slots.d.ts +102 -0
- package/lib/ui/services-react.d.ts +27 -0
- package/lib/ui/services-react.js +32 -0
- package/lib/ui/services.d.ts +63 -0
- package/lib/ui/services.js +121 -0
- package/lib/ui/slots-ink.d.ts +26 -0
- package/lib/ui/slots-ink.js +78 -0
- package/lib/ui/slots-react.d.ts +28 -0
- package/lib/ui/slots-react.js +88 -0
- package/lib/ui/slots.d.ts +481 -0
- package/lib/ui/slots.js +550 -0
- package/lib/ui/store.d.ts +89 -0
- package/lib/ui/store.js +62 -0
- package/lib/ui/tui-plugin-api.d.ts +23 -0
- package/lib/ui/tui-plugin-api.js +23 -0
- package/lib/ui/tui-plugins.d.ts +47 -0
- package/lib/ui/tui-plugins.js +220 -0
- package/package.json +58 -6
package/lib/core/mcp.js
ADDED
|
@@ -0,0 +1,836 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP 客户端底座:stdio 与 streamable-http 两种传输上的 JSON-RPC 收发、工具命名、
|
|
3
|
+
* 子进程环境筛选,以及 Windows 上的启动命令解析。挂到 cordis 上的那一层在
|
|
4
|
+
* `src/plugins/mcp.ts`。
|
|
5
|
+
*
|
|
6
|
+
* 为什么不引 SDK:个人版零新增依赖,而 dsc 用到的线上方法只有 initialize /
|
|
7
|
+
* notifications/initialized / tools/list / tools/call 四条,自己写一遍比拖一条依赖树省事。
|
|
8
|
+
*
|
|
9
|
+
* @module dsc/core/mcp
|
|
10
|
+
*/
|
|
11
|
+
import { spawn } from 'node:child_process';
|
|
12
|
+
import { createHash } from 'node:crypto';
|
|
13
|
+
import { existsSync } from 'node:fs';
|
|
14
|
+
import { isAbsolute, join, resolve } from 'node:path';
|
|
15
|
+
// ── 协议与命名常量(协议常量不随部署变,固定在这里) ──────────────────────────
|
|
16
|
+
/** initialize 里报的协议版本(MCP 2024-11-05)。 */
|
|
17
|
+
const MCP_PROTOCOL_VERSION = '2025-06-18';
|
|
18
|
+
/** initialize 里自报的客户端身份。 */
|
|
19
|
+
const CLIENT_INFO = { name: 'pneuma', version: '0.1.0' };
|
|
20
|
+
/** 工具名长度上限(照 codex / dsh:超长的截断后用哈希尾巴防撞)。 */
|
|
21
|
+
const TOOL_NAME_MAX = 64;
|
|
22
|
+
/** 哈希尾巴的位数。 */
|
|
23
|
+
const TOOL_NAME_HASH = 12;
|
|
24
|
+
/** tools/list 翻页上限:server 一直给新游标时也不能转死。 */
|
|
25
|
+
const MAX_TOOL_PAGES = 100;
|
|
26
|
+
/** 子进程 stderr 最多留多少字符进错误消息(这个是给人看的原因,不是日志)。 */
|
|
27
|
+
const STDERR_TAIL_MAX = 2000;
|
|
28
|
+
/**
|
|
29
|
+
* 允许带进子进程的系统环境变量。
|
|
30
|
+
* 其余一律不传:`process.env` 里有用户的模型 key 与各种令牌,MCP server 是第三方程序,
|
|
31
|
+
* 没有理由让它看见。要传的按 server 配置显式声明。
|
|
32
|
+
*/
|
|
33
|
+
export const BASE_ENV_NAMES = [
|
|
34
|
+
'PATH',
|
|
35
|
+
'PATHEXT',
|
|
36
|
+
'COMSPEC',
|
|
37
|
+
'SYSTEMROOT',
|
|
38
|
+
'SYSTEMDRIVE',
|
|
39
|
+
'WINDIR',
|
|
40
|
+
'TEMP',
|
|
41
|
+
'TMP',
|
|
42
|
+
'HOME',
|
|
43
|
+
'USERPROFILE',
|
|
44
|
+
'LANG',
|
|
45
|
+
];
|
|
46
|
+
/** PATH 里没写 PATHEXT 时按这几个后缀找可执行文件。 */
|
|
47
|
+
const FALLBACK_PATHEXT = ['.COM', '.EXE', '.BAT', '.CMD'];
|
|
48
|
+
// ── 环境变量筛选 ─────────────────────────────────────────────────────────────
|
|
49
|
+
/**
|
|
50
|
+
* 按白名单拼子进程环境。
|
|
51
|
+
*
|
|
52
|
+
* 只放 {@link BASE_ENV_NAMES} 里的系统变量,加上配置里显式声明的名字;
|
|
53
|
+
* 声明了名字但没给值时,从父环境取同名变量(写 `{"GITHUB_TOKEN": ""}` 也能带上宿主的那个)。
|
|
54
|
+
*
|
|
55
|
+
* @param declaredNames - 配置里显式声明的变量名。
|
|
56
|
+
* @param values - 这些名字对应的值(缺了就从 parent 取)。
|
|
57
|
+
* @param parent - 父进程环境,通常是 `process.env`。
|
|
58
|
+
* @returns 交给 spawn 的环境对象(不含任何未声明的变量)。
|
|
59
|
+
*/
|
|
60
|
+
export function buildChildEnv(declaredNames, values, parent) {
|
|
61
|
+
const env = {};
|
|
62
|
+
for (const name of BASE_ENV_NAMES) {
|
|
63
|
+
const value = readEnv(parent, name);
|
|
64
|
+
if (value !== undefined && value !== '')
|
|
65
|
+
env[name] = value;
|
|
66
|
+
}
|
|
67
|
+
for (const name of declaredNames) {
|
|
68
|
+
const value = values[name] ?? readEnv(parent, name);
|
|
69
|
+
if (value !== undefined && value !== '')
|
|
70
|
+
env[name] = value;
|
|
71
|
+
}
|
|
72
|
+
return env;
|
|
73
|
+
}
|
|
74
|
+
/** 按名字读环境变量;Windows 的变量名不分大小写,所以找不到时再比一遍小写。 */
|
|
75
|
+
function readEnv(parent, name) {
|
|
76
|
+
const direct = parent[name];
|
|
77
|
+
if (direct !== undefined)
|
|
78
|
+
return direct;
|
|
79
|
+
if (process.platform !== 'win32')
|
|
80
|
+
return undefined;
|
|
81
|
+
const wanted = name.toLowerCase();
|
|
82
|
+
for (const key of Object.keys(parent)) {
|
|
83
|
+
if (key.toLowerCase() === wanted)
|
|
84
|
+
return parent[key];
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 把配置里的命令解析成 spawn 能用的形式。
|
|
90
|
+
*
|
|
91
|
+
* Windows 上 Node 只执行 PE 文件:`spawn('npx')` 明明 PATH 里有 `npx.cmd`,却会以
|
|
92
|
+
* EINVAL 失败。所以这里按 PATH + PATHEXT 自己找一遍,找到 `.cmd` / `.bat` 就标上要经 shell。
|
|
93
|
+
* 非 Windows 上 Node 自己能找,原样返回。
|
|
94
|
+
*
|
|
95
|
+
* @param command - 配置里写的命令(可以是文件名,也可以是路径)。
|
|
96
|
+
* @param env - 已经筛过的子进程环境(从这里读 PATH / PATHEXT)。
|
|
97
|
+
* @param cwd - 解析相对路径的基准目录;空串按当前目录。
|
|
98
|
+
*/
|
|
99
|
+
export function resolveCommand(command, env, cwd) {
|
|
100
|
+
if (process.platform !== 'win32')
|
|
101
|
+
return { file: command, shell: false };
|
|
102
|
+
const declared = (env.PATHEXT ?? '').split(';').filter((suffix) => suffix !== '');
|
|
103
|
+
const suffixes = declared.length > 0 ? declared : FALLBACK_PATHEXT;
|
|
104
|
+
const roots = /[\\/]/.test(command)
|
|
105
|
+
? [isAbsolute(command) ? command : resolve(cwd === '' ? process.cwd() : cwd, command)]
|
|
106
|
+
: (env.PATH ?? '')
|
|
107
|
+
.split(';')
|
|
108
|
+
.filter((dir) => dir !== '')
|
|
109
|
+
.map((dir) => join(dir, command));
|
|
110
|
+
for (const root of roots) {
|
|
111
|
+
for (const candidate of [root, ...suffixes.map((suffix) => root + suffix)]) {
|
|
112
|
+
if (existsSync(candidate))
|
|
113
|
+
return { file: candidate, shell: needsShell(candidate) };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// 一个都没找到:交给 cmd 自己按 PATHEXT 找,找不到时它会往 stderr 报错,
|
|
117
|
+
// 那句报错会跟着进程退出一起进 problem,比这里瞎猜一句更准。
|
|
118
|
+
return { file: command, shell: true };
|
|
119
|
+
}
|
|
120
|
+
/** `.cmd` / `.bat` 只能经 cmd.exe 跑(Node 直接 CreateProcess 会拒绝)。 */
|
|
121
|
+
function needsShell(file) {
|
|
122
|
+
const lower = file.toLowerCase();
|
|
123
|
+
return lower.endsWith('.cmd') || lower.endsWith('.bat');
|
|
124
|
+
}
|
|
125
|
+
/** shell 那条路上参数由 cmd.exe 解析,带空格或元字符的要自己加引号。 */
|
|
126
|
+
function quoteForCmd(arg) {
|
|
127
|
+
return /[\s"&|<>^()]/.test(arg) ? `"${arg.replace(/"/g, '\\"')}"` : arg;
|
|
128
|
+
}
|
|
129
|
+
// ── 工具命名与风险等级 ───────────────────────────────────────────────────────
|
|
130
|
+
/** 把片段里不能出现在工具名里的字符换掉。 */
|
|
131
|
+
function sanitize(part) {
|
|
132
|
+
return part.replace(/[^A-Za-z0-9_-]/g, '_');
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 拼完整工具名:`mcp__<server>__<tool>`。
|
|
136
|
+
* 超过 64 个字符时截断并接 12 位 sha256 尾巴,两个长名字因此不会撞成一个。
|
|
137
|
+
*
|
|
138
|
+
* @param server - server 名。
|
|
139
|
+
* @param tool - server 那边的工具名。
|
|
140
|
+
*/
|
|
141
|
+
export function mcpToolName(server, tool) {
|
|
142
|
+
const raw = `mcp__${sanitize(server)}__${sanitize(tool)}`;
|
|
143
|
+
if (raw.length <= TOOL_NAME_MAX)
|
|
144
|
+
return raw;
|
|
145
|
+
const digest = createHash('sha256').update(raw).digest('hex').slice(0, TOOL_NAME_HASH);
|
|
146
|
+
return `${raw.slice(0, TOOL_NAME_MAX - TOOL_NAME_HASH - 1)}_${digest}`;
|
|
147
|
+
}
|
|
148
|
+
/** 一个工具的风险等级:配置里点名过就按配置,没点名按 server 的默认值。 */
|
|
149
|
+
export function riskOf(server, tool) {
|
|
150
|
+
return server.risk[tool] ?? server.defaultRisk;
|
|
151
|
+
}
|
|
152
|
+
// ── 服务器清单解析 ───────────────────────────────────────────────────────────
|
|
153
|
+
/**
|
|
154
|
+
* 解析设置里那一行服务器清单 JSON。
|
|
155
|
+
* 数组里每一项一个 server;任何一项不合法就整份拒收,并给出第几项哪里不对——
|
|
156
|
+
* 半份配置连起来比连不上更难查。
|
|
157
|
+
*
|
|
158
|
+
* @param text - 清单原文(空串 = 没有 server)。
|
|
159
|
+
* @param fallbackRisk - 清单里没写 defaultRisk 时按它算。
|
|
160
|
+
*/
|
|
161
|
+
export function parseServerList(text, fallbackRisk) {
|
|
162
|
+
const trimmed = text.trim();
|
|
163
|
+
if (trimmed === '')
|
|
164
|
+
return { servers: [], problem: null };
|
|
165
|
+
let doc;
|
|
166
|
+
try {
|
|
167
|
+
doc = JSON.parse(trimmed);
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
return { servers: [], problem: `不是合法 JSON:${errorText(error)}` };
|
|
171
|
+
}
|
|
172
|
+
if (!Array.isArray(doc))
|
|
173
|
+
return { servers: [], problem: '服务器清单要是一个 JSON 数组' };
|
|
174
|
+
const servers = [];
|
|
175
|
+
const seen = new Set();
|
|
176
|
+
for (let index = 0; index < doc.length; index += 1) {
|
|
177
|
+
const parsed = parseServerEntry(doc[index], index, fallbackRisk);
|
|
178
|
+
if (typeof parsed === 'string')
|
|
179
|
+
return { servers: [], problem: parsed };
|
|
180
|
+
if (seen.has(parsed.name))
|
|
181
|
+
return { servers: [], problem: `第 ${index + 1} 项的 name 与前面重了:${parsed.name}` };
|
|
182
|
+
seen.add(parsed.name);
|
|
183
|
+
servers.push(parsed);
|
|
184
|
+
}
|
|
185
|
+
return { servers, problem: null };
|
|
186
|
+
}
|
|
187
|
+
/** 解析清单里的一项;返回字符串 = 这一项哪里不对。 */
|
|
188
|
+
function parseServerEntry(entry, index, fallbackRisk) {
|
|
189
|
+
const where = `第 ${index + 1} 项`;
|
|
190
|
+
if (entry === null || typeof entry !== 'object' || Array.isArray(entry))
|
|
191
|
+
return `${where}不是一个对象`;
|
|
192
|
+
const raw = entry;
|
|
193
|
+
const name = asText(raw.name);
|
|
194
|
+
if (name === undefined || name === '')
|
|
195
|
+
return `${where}缺 name`;
|
|
196
|
+
if (!/^[A-Za-z0-9_.-]{1,40}$/.test(name)) {
|
|
197
|
+
return `${where}的 name「${name}」只能用字母、数字与 _ . -,且不超过 40 个字`;
|
|
198
|
+
}
|
|
199
|
+
const transport = raw.transport === undefined ? 'stdio' : asText(raw.transport);
|
|
200
|
+
if (transport !== 'stdio' && transport !== 'http')
|
|
201
|
+
return `${where}的 transport 只能是 stdio 或 http`;
|
|
202
|
+
const args = asTextList(raw.args);
|
|
203
|
+
if (args === undefined)
|
|
204
|
+
return `${where}的 args 要是一个字符串数组`;
|
|
205
|
+
const env = asTextMap(raw.env);
|
|
206
|
+
if (env === undefined)
|
|
207
|
+
return `${where}的 env 要是「变量名: 值」的对象`;
|
|
208
|
+
const headers = asTextMap(raw.headers);
|
|
209
|
+
if (headers === undefined)
|
|
210
|
+
return `${where}的 headers 要是「头名: 值」的对象`;
|
|
211
|
+
const risk = asRiskMap(raw.risk);
|
|
212
|
+
if (risk === undefined)
|
|
213
|
+
return `${where}的 risk 要是「工具名: read|write|exec」的对象`;
|
|
214
|
+
const declaredRisk = raw.defaultRisk === undefined ? fallbackRisk : asRisk(raw.defaultRisk);
|
|
215
|
+
if (declaredRisk === undefined)
|
|
216
|
+
return `${where}的 defaultRisk 只能是 read、write 或 exec`;
|
|
217
|
+
const command = asText(raw.command) ?? '';
|
|
218
|
+
const url = asText(raw.url) ?? '';
|
|
219
|
+
if (transport === 'stdio' && command === '')
|
|
220
|
+
return `${where}(${name})是 stdio,必须写 command`;
|
|
221
|
+
if (transport === 'http' && !/^https?:\/\//.test(url)) {
|
|
222
|
+
return `${where}(${name})是 http,url 要以 http:// 或 https:// 开头`;
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
name,
|
|
226
|
+
transport,
|
|
227
|
+
command,
|
|
228
|
+
args,
|
|
229
|
+
cwd: asText(raw.cwd) ?? '',
|
|
230
|
+
env,
|
|
231
|
+
url,
|
|
232
|
+
headers,
|
|
233
|
+
risk,
|
|
234
|
+
defaultRisk: declaredRisk,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
function asText(value) {
|
|
238
|
+
return typeof value === 'string' ? value.trim() : undefined;
|
|
239
|
+
}
|
|
240
|
+
function asTextList(value) {
|
|
241
|
+
if (value === undefined)
|
|
242
|
+
return [];
|
|
243
|
+
if (!Array.isArray(value))
|
|
244
|
+
return undefined;
|
|
245
|
+
const out = [];
|
|
246
|
+
for (const item of value) {
|
|
247
|
+
if (typeof item !== 'string')
|
|
248
|
+
return undefined;
|
|
249
|
+
out.push(item);
|
|
250
|
+
}
|
|
251
|
+
return out;
|
|
252
|
+
}
|
|
253
|
+
function asTextMap(value) {
|
|
254
|
+
if (value === undefined)
|
|
255
|
+
return {};
|
|
256
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
257
|
+
return undefined;
|
|
258
|
+
const out = {};
|
|
259
|
+
for (const [key, item] of Object.entries(value)) {
|
|
260
|
+
if (typeof item !== 'string')
|
|
261
|
+
return undefined;
|
|
262
|
+
out[key] = item;
|
|
263
|
+
}
|
|
264
|
+
return out;
|
|
265
|
+
}
|
|
266
|
+
function asRiskMap(value) {
|
|
267
|
+
if (value === undefined)
|
|
268
|
+
return {};
|
|
269
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
270
|
+
return undefined;
|
|
271
|
+
const out = {};
|
|
272
|
+
for (const [key, item] of Object.entries(value)) {
|
|
273
|
+
const risk = asRisk(item);
|
|
274
|
+
if (risk === undefined)
|
|
275
|
+
return undefined;
|
|
276
|
+
out[key] = risk;
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
/** 认得出就返回风险等级,认不出返回 undefined(调用方决定是报错还是用缺省)。 */
|
|
281
|
+
export function asRisk(value) {
|
|
282
|
+
return value === 'read' || value === 'write' || value === 'exec' ? value : undefined;
|
|
283
|
+
}
|
|
284
|
+
/** 错误对象取一句话;不是 Error 就字符串化。 */
|
|
285
|
+
function errorText(error) {
|
|
286
|
+
return error instanceof Error ? error.message : String(error);
|
|
287
|
+
}
|
|
288
|
+
/** JSON-RPC 错误对象压成一句话。 */
|
|
289
|
+
function rpcErrorMessage(error) {
|
|
290
|
+
if (error === null || typeof error !== 'object')
|
|
291
|
+
return `MCP 错误:${String(error)}`;
|
|
292
|
+
const doc = error;
|
|
293
|
+
const message = typeof doc.message === 'string' ? doc.message : JSON.stringify(error);
|
|
294
|
+
return typeof doc.code === 'number' ? `MCP 错误 ${doc.code}:${message}` : `MCP 错误:${message}`;
|
|
295
|
+
}
|
|
296
|
+
/** 有 error 就抛,没有就取 result。 */
|
|
297
|
+
function unwrapRpc(message) {
|
|
298
|
+
if (message.error !== undefined && message.error !== null)
|
|
299
|
+
throw new Error(rpcErrorMessage(message.error));
|
|
300
|
+
return message.result;
|
|
301
|
+
}
|
|
302
|
+
/** 认得出是个对象就返回,否则 null。 */
|
|
303
|
+
function asRecord(value) {
|
|
304
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value) ? value : null;
|
|
305
|
+
}
|
|
306
|
+
/** 超时或取消,给一句能直接进 problem 的话。 */
|
|
307
|
+
function abortReason(signal, timeoutMs) {
|
|
308
|
+
return signal.aborted ? `请求被取消(超过 ${timeoutMs}ms 的闸门已开)` : `请求超过 ${timeoutMs}ms 没回`;
|
|
309
|
+
}
|
|
310
|
+
/** stdio 通道:一个子进程,按行分帧的 JSON-RPC。 */
|
|
311
|
+
class StdioChannel {
|
|
312
|
+
child;
|
|
313
|
+
timeoutMs;
|
|
314
|
+
onClose;
|
|
315
|
+
pending = new Map();
|
|
316
|
+
buffer = '';
|
|
317
|
+
stderrTail = '';
|
|
318
|
+
nextId = 0;
|
|
319
|
+
closed = false;
|
|
320
|
+
/** T24:server 反向请求的处理缝(elicitation/create);没挂就按 method-not-found 回。 */
|
|
321
|
+
serverRequestHandler;
|
|
322
|
+
constructor(child, timeoutMs, onClose) {
|
|
323
|
+
this.child = child;
|
|
324
|
+
this.timeoutMs = timeoutMs;
|
|
325
|
+
this.onClose = onClose;
|
|
326
|
+
child.stdout?.on('data', (chunk) => {
|
|
327
|
+
this.onStdout(chunk.toString('utf8'));
|
|
328
|
+
});
|
|
329
|
+
child.stderr?.on('data', (chunk) => {
|
|
330
|
+
this.stderrTail = `${this.stderrTail}${chunk.toString('utf8')}`.slice(-STDERR_TAIL_MAX);
|
|
331
|
+
});
|
|
332
|
+
child.on('error', (error) => {
|
|
333
|
+
this.shutdown(`MCP server 进程起不来:${error.message}`);
|
|
334
|
+
});
|
|
335
|
+
// stdin 出错说明管道断了,紧跟着就是 exit;这里不重复报,交给 exit 那条路。
|
|
336
|
+
child.stdin?.on('error', () => { });
|
|
337
|
+
child.on('exit', (code, signal) => {
|
|
338
|
+
const tail = this.stderrTail.trim();
|
|
339
|
+
this.shutdown(`MCP server 进程退出(${signal ?? code ?? '未知'})${tail === '' ? '' : `:${tail}`}`);
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
/** T24:挂 server 反向请求的处理缝(连接握手完就挂)。 */
|
|
343
|
+
handleServerRequests(handler) {
|
|
344
|
+
this.serverRequestHandler = handler;
|
|
345
|
+
}
|
|
346
|
+
get alive() {
|
|
347
|
+
return !this.closed && this.child.exitCode === null;
|
|
348
|
+
}
|
|
349
|
+
request(method, params, signal) {
|
|
350
|
+
return new Promise((resolveValue, rejectValue) => {
|
|
351
|
+
if (this.closed) {
|
|
352
|
+
rejectValue(new Error('MCP 连接已经关了'));
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const id = (this.nextId += 1);
|
|
356
|
+
const gate = AbortSignal.any([signal, AbortSignal.timeout(this.timeoutMs)]);
|
|
357
|
+
const onAbort = () => {
|
|
358
|
+
const entry = this.pending.get(id);
|
|
359
|
+
entry?.settle(new Error(`${abortReason(signal, this.timeoutMs)}:${method}`));
|
|
360
|
+
};
|
|
361
|
+
const entry = {
|
|
362
|
+
settle: (error, value) => {
|
|
363
|
+
if (!this.pending.delete(id))
|
|
364
|
+
return;
|
|
365
|
+
gate.removeEventListener('abort', onAbort);
|
|
366
|
+
if (error === null)
|
|
367
|
+
resolveValue(value);
|
|
368
|
+
else
|
|
369
|
+
rejectValue(error);
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
this.pending.set(id, entry);
|
|
373
|
+
// 已经把闸门拉下的调用方(上一轮被打断)不必再往管道里写一行
|
|
374
|
+
if (gate.aborted) {
|
|
375
|
+
entry.settle(new Error(`${abortReason(signal, this.timeoutMs)}:${method}`));
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
gate.addEventListener('abort', onAbort, { once: true });
|
|
379
|
+
const stdin = this.child.stdin;
|
|
380
|
+
if (stdin === null || stdin.destroyed) {
|
|
381
|
+
entry.settle(new Error('MCP 子进程的输入口已经关了'));
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
stdin.write(`${JSON.stringify({ jsonrpc: '2.0', id, method, params })}\n`, (error) => {
|
|
385
|
+
if (error !== null && error !== undefined)
|
|
386
|
+
entry.settle(new Error(`往 MCP 子进程写请求失败:${error.message}`));
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
notify(method, params) {
|
|
391
|
+
const stdin = this.child.stdin;
|
|
392
|
+
if (this.closed || stdin === null || stdin.destroyed)
|
|
393
|
+
return;
|
|
394
|
+
const doc = { jsonrpc: '2.0', method };
|
|
395
|
+
if (params !== undefined)
|
|
396
|
+
doc.params = params;
|
|
397
|
+
stdin.write(`${JSON.stringify(doc)}\n`, (error) => {
|
|
398
|
+
// 通知发不出去不影响结论:紧随其后的请求会以同样的原因失败,那时才报给调用方。
|
|
399
|
+
if (error !== null && error !== undefined)
|
|
400
|
+
this.shutdown(`往 MCP 子进程写通知失败:${error.message}`);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
close() {
|
|
404
|
+
this.shutdown('MCP 连接被主动关闭');
|
|
405
|
+
if (this.child.exitCode === null)
|
|
406
|
+
this.child.kill();
|
|
407
|
+
}
|
|
408
|
+
/** 收尾:拒掉所有挂着的请求,再通知插件层这条连接没了。 */
|
|
409
|
+
shutdown(reason) {
|
|
410
|
+
if (this.closed)
|
|
411
|
+
return;
|
|
412
|
+
this.closed = true;
|
|
413
|
+
for (const entry of [...this.pending.values()])
|
|
414
|
+
entry.settle(new Error(reason));
|
|
415
|
+
this.pending.clear();
|
|
416
|
+
this.onClose();
|
|
417
|
+
}
|
|
418
|
+
onStdout(chunk) {
|
|
419
|
+
this.buffer += chunk;
|
|
420
|
+
for (;;) {
|
|
421
|
+
const index = this.buffer.indexOf('\n');
|
|
422
|
+
if (index < 0)
|
|
423
|
+
break;
|
|
424
|
+
const line = this.buffer.slice(0, index).trim();
|
|
425
|
+
this.buffer = this.buffer.slice(index + 1);
|
|
426
|
+
if (line !== '')
|
|
427
|
+
this.onLine(line);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
onLine(line) {
|
|
431
|
+
let doc;
|
|
432
|
+
try {
|
|
433
|
+
doc = JSON.parse(line);
|
|
434
|
+
}
|
|
435
|
+
catch {
|
|
436
|
+
// server 往 stdout 打日志、或者半截帧:MCP 只认整行 JSON,这些行直接丢
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
const message = asRecord(doc);
|
|
440
|
+
if (message === null)
|
|
441
|
+
return;
|
|
442
|
+
const id = message.id;
|
|
443
|
+
const method = message.method;
|
|
444
|
+
if (typeof id !== 'number') {
|
|
445
|
+
// 没有 id 的是 server 通知(notifications/...):本客户端只听请求,通知丢掉
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
if (typeof method === 'string') {
|
|
449
|
+
// T24:带 id 带 method 是 server **反向发起的请求**(elicitation/create)。
|
|
450
|
+
// 处理缝异步出结果再按 id 回写;没挂缝或处理抛错都回 JSON-RPC 错误,server 自己兜。
|
|
451
|
+
this.onServerRequest(id, method, asRecord(message.params) ?? {});
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const entry = this.pending.get(id);
|
|
455
|
+
if (entry === undefined)
|
|
456
|
+
return;
|
|
457
|
+
if (message.error !== undefined && message.error !== null) {
|
|
458
|
+
entry.settle(new Error(rpcErrorMessage(message.error)));
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
entry.settle(null, message.result);
|
|
462
|
+
}
|
|
463
|
+
/** T24:处理一条 server 反向请求并回写应答(异步;写不进去就算了,连接随后会断)。 */
|
|
464
|
+
onServerRequest(id, method, params) {
|
|
465
|
+
const respond = (result) => {
|
|
466
|
+
const stdin = this.child.stdin;
|
|
467
|
+
if (this.closed || stdin === null || stdin.destroyed)
|
|
468
|
+
return;
|
|
469
|
+
stdin.write(`${JSON.stringify({ jsonrpc: '2.0', id, result })}\n`, () => { });
|
|
470
|
+
};
|
|
471
|
+
const reject = (message, code = -32601) => {
|
|
472
|
+
const stdin = this.child.stdin;
|
|
473
|
+
if (this.closed || stdin === null || stdin.destroyed)
|
|
474
|
+
return;
|
|
475
|
+
stdin.write(`${JSON.stringify({ jsonrpc: '2.0', id, error: { code, message } })}\n`, () => { });
|
|
476
|
+
};
|
|
477
|
+
const handler = this.serverRequestHandler;
|
|
478
|
+
if (handler === undefined) {
|
|
479
|
+
reject(`客户端不支持 ${method}`);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
void handler(method, params).then(respond, (error) => reject(errorText(error), -32603));
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
/** http 通道:streamable-http,一问一答,Session id 记下来在后续请求回填。 */
|
|
486
|
+
class HttpChannel {
|
|
487
|
+
url;
|
|
488
|
+
extraHeaders;
|
|
489
|
+
timeoutMs;
|
|
490
|
+
nextId = 0;
|
|
491
|
+
sessionId;
|
|
492
|
+
closed = false;
|
|
493
|
+
constructor(url, extraHeaders, timeoutMs) {
|
|
494
|
+
this.url = url;
|
|
495
|
+
this.extraHeaders = extraHeaders;
|
|
496
|
+
this.timeoutMs = timeoutMs;
|
|
497
|
+
}
|
|
498
|
+
get alive() {
|
|
499
|
+
return !this.closed;
|
|
500
|
+
}
|
|
501
|
+
async request(method, params, signal) {
|
|
502
|
+
const id = (this.nextId += 1);
|
|
503
|
+
return await this.post({ jsonrpc: '2.0', id, method, params }, id, signal);
|
|
504
|
+
}
|
|
505
|
+
notify(method, params) {
|
|
506
|
+
const doc = { jsonrpc: '2.0', method };
|
|
507
|
+
if (params !== undefined)
|
|
508
|
+
doc.params = params;
|
|
509
|
+
// 通知不等应答;发失败也不影响后面第一个真请求的结论,那条路上会报。
|
|
510
|
+
void this.post(doc, null, new AbortController().signal).catch(() => { });
|
|
511
|
+
}
|
|
512
|
+
close() {
|
|
513
|
+
this.closed = true;
|
|
514
|
+
}
|
|
515
|
+
/** 发一条 JSON-RPC 消息;expectId 非 null 时等那条 id 的应答。 */
|
|
516
|
+
async post(payload, expectId, signal) {
|
|
517
|
+
if (this.closed)
|
|
518
|
+
throw new Error('MCP 连接已经关了');
|
|
519
|
+
const gate = AbortSignal.any([signal, AbortSignal.timeout(this.timeoutMs)]);
|
|
520
|
+
const headers = {
|
|
521
|
+
'content-type': 'application/json',
|
|
522
|
+
accept: 'application/json, text/event-stream',
|
|
523
|
+
...this.extraHeaders,
|
|
524
|
+
};
|
|
525
|
+
if (this.sessionId !== undefined)
|
|
526
|
+
headers['mcp-session-id'] = this.sessionId;
|
|
527
|
+
let response;
|
|
528
|
+
try {
|
|
529
|
+
response = await fetch(this.url, { method: 'POST', headers, body: JSON.stringify(payload), signal: gate });
|
|
530
|
+
}
|
|
531
|
+
catch (error) {
|
|
532
|
+
throw new Error(`MCP HTTP 请求发不出去(${abortReason(signal, this.timeoutMs)}):${errorText(error)}`);
|
|
533
|
+
}
|
|
534
|
+
// server 可能在任意一条响应上发新的 Session id,见到就记下来
|
|
535
|
+
const session = response.headers.get('mcp-session-id');
|
|
536
|
+
if (session !== null && session !== '')
|
|
537
|
+
this.sessionId = session;
|
|
538
|
+
if (!response.ok) {
|
|
539
|
+
const detail = await response.text().then((text) => text.slice(0, 300), () => '');
|
|
540
|
+
throw new Error(`MCP HTTP ${response.status}:${detail}`);
|
|
541
|
+
}
|
|
542
|
+
if (expectId === null) {
|
|
543
|
+
await response.body?.cancel().catch(() => { });
|
|
544
|
+
return undefined;
|
|
545
|
+
}
|
|
546
|
+
const contentType = response.headers.get('content-type') ?? '';
|
|
547
|
+
if (contentType.includes('text/event-stream'))
|
|
548
|
+
return await readSseFor(response, expectId);
|
|
549
|
+
const text = await response.text();
|
|
550
|
+
if (text.trim() === '')
|
|
551
|
+
return undefined;
|
|
552
|
+
let doc;
|
|
553
|
+
try {
|
|
554
|
+
doc = JSON.parse(text);
|
|
555
|
+
}
|
|
556
|
+
catch {
|
|
557
|
+
throw new Error(`MCP HTTP 回的不是 JSON:${text.slice(0, 200)}`);
|
|
558
|
+
}
|
|
559
|
+
const message = asRecord(doc);
|
|
560
|
+
if (message === null)
|
|
561
|
+
throw new Error('MCP HTTP 回的不是一个 JSON-RPC 对象');
|
|
562
|
+
return unwrapRpc(message);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* 读 SSE 响应体,直到出现 expectId 那条应答,然后立刻取消读取。
|
|
567
|
+
* 非 JSON 的行(心跳、注释)与别人家 id 的事件都跳过。
|
|
568
|
+
*/
|
|
569
|
+
async function readSseFor(response, expectId) {
|
|
570
|
+
const body = response.body;
|
|
571
|
+
if (body === null)
|
|
572
|
+
throw new Error('MCP HTTP 声明是 SSE,却没有响应体');
|
|
573
|
+
const reader = body.getReader();
|
|
574
|
+
const decoder = new TextDecoder();
|
|
575
|
+
let buffer = '';
|
|
576
|
+
try {
|
|
577
|
+
for (;;) {
|
|
578
|
+
const { done, value } = await reader.read();
|
|
579
|
+
if (done)
|
|
580
|
+
break;
|
|
581
|
+
buffer += decoder.decode(value, { stream: true });
|
|
582
|
+
let index = buffer.indexOf('\n');
|
|
583
|
+
while (index >= 0) {
|
|
584
|
+
const line = buffer.slice(0, index).trim();
|
|
585
|
+
buffer = buffer.slice(index + 1);
|
|
586
|
+
const payload = line.startsWith('data:') ? line.slice(5).trim() : '';
|
|
587
|
+
if (payload !== '' && payload !== '[DONE]') {
|
|
588
|
+
const match = matchSseChunk(payload, expectId);
|
|
589
|
+
if (match.matched) {
|
|
590
|
+
await reader.cancel();
|
|
591
|
+
return match.value;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
index = buffer.indexOf('\n');
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
finally {
|
|
599
|
+
reader.releaseLock();
|
|
600
|
+
}
|
|
601
|
+
throw new Error('MCP HTTP 的 SSE 流读完了,也没等到这个请求的应答');
|
|
602
|
+
}
|
|
603
|
+
/** 一条 SSE data 是不是我们等的那个应答;匹配上时顺便把结果取出来(有 error 就抛)。 */
|
|
604
|
+
function matchSseChunk(payload, expectId) {
|
|
605
|
+
let doc;
|
|
606
|
+
try {
|
|
607
|
+
doc = JSON.parse(payload);
|
|
608
|
+
}
|
|
609
|
+
catch {
|
|
610
|
+
// SSE 里混着非 JSON 的心跳分片,跳过继续读
|
|
611
|
+
return { matched: false, value: undefined };
|
|
612
|
+
}
|
|
613
|
+
const message = asRecord(doc);
|
|
614
|
+
if (message === null || message.id !== expectId)
|
|
615
|
+
return { matched: false, value: undefined };
|
|
616
|
+
return { matched: true, value: unwrapRpc(message) };
|
|
617
|
+
}
|
|
618
|
+
// ── 连接 ─────────────────────────────────────────────────────────────────────
|
|
619
|
+
/** elicitation 等人点卡的上限(毫秒):审批卡可以慢慢等,但不能永远挂着。 */
|
|
620
|
+
const ELICITATION_TIMEOUT_MS = 600_000;
|
|
621
|
+
/**
|
|
622
|
+
* 建一条连接并握手(initialize → notifications/initialized)。
|
|
623
|
+
* 只握手,不列工具——列工具是 {@link McpConnection.listTools} 的事,
|
|
624
|
+
* 插件层要在拿到连接之后才能把它记成「当前这条」。
|
|
625
|
+
*
|
|
626
|
+
* @param server - 目标 server 的配置。
|
|
627
|
+
* @param options - 超时、断线回调与 elicitation 处理缝。
|
|
628
|
+
*/
|
|
629
|
+
export async function openConnection(server, options) {
|
|
630
|
+
const isStdio = server.transport !== 'http';
|
|
631
|
+
const channel = isStdio
|
|
632
|
+
? new StdioChannel(spawnStdio(server, options.cwd ?? ''), options.callTimeoutMs, options.onClose)
|
|
633
|
+
: new HttpChannel(server.url, server.headers, options.callTimeoutMs);
|
|
634
|
+
let instructions = '';
|
|
635
|
+
let capabilities = { resources: false, prompts: false };
|
|
636
|
+
try {
|
|
637
|
+
// T37:initialize 的应答不再整个丢掉——里面的 `instructions` 是 server 交代
|
|
638
|
+
// 自家工具怎么用的说明书,摘出来随连接交还。
|
|
639
|
+
// T24:capabilities 里带上 elicitation(只有 stdio 通道声明——http 一问一答,
|
|
640
|
+
// server 的反向请求根本送不进来);server 的应答里 capabilities.resources/prompts
|
|
641
|
+
// 决定资源与提示模板工具要不要注册。
|
|
642
|
+
const initResult = await channel.request('initialize', {
|
|
643
|
+
protocolVersion: MCP_PROTOCOL_VERSION,
|
|
644
|
+
capabilities: isStdio ? { elicitation: {} } : {},
|
|
645
|
+
clientInfo: CLIENT_INFO,
|
|
646
|
+
}, AbortSignal.timeout(options.connectTimeoutMs));
|
|
647
|
+
const initDoc = asRecord(initResult);
|
|
648
|
+
const declared = initDoc?.instructions;
|
|
649
|
+
if (typeof declared === 'string')
|
|
650
|
+
instructions = declared;
|
|
651
|
+
const serverCaps = asRecord(initDoc?.capabilities);
|
|
652
|
+
capabilities = { resources: serverCaps?.resources !== undefined, prompts: serverCaps?.prompts !== undefined };
|
|
653
|
+
channel.notify('notifications/initialized');
|
|
654
|
+
}
|
|
655
|
+
catch (error) {
|
|
656
|
+
channel.close();
|
|
657
|
+
throw error;
|
|
658
|
+
}
|
|
659
|
+
// T24:elicitation 缝只挂在 stdio 通道上——server 发来 elicitation/create 就走
|
|
660
|
+
// 调用方给的审批回调;没给回调、方法不认识、或处理抛错,都按 JSON-RPC 错误回给 server。
|
|
661
|
+
if (channel instanceof StdioChannel) {
|
|
662
|
+
channel.handleServerRequests(async (method, params) => {
|
|
663
|
+
if (method !== 'elicitation/create' || options.onElicitation === undefined) {
|
|
664
|
+
throw new Error(`客户端不支持 ${method}`);
|
|
665
|
+
}
|
|
666
|
+
const message = typeof params.message === 'string' ? params.message : '';
|
|
667
|
+
const schema = asRecord(params.requestedSchema) ?? {};
|
|
668
|
+
const answer = await options.onElicitation({ message, requestedSchema: schema });
|
|
669
|
+
return { action: answer.action };
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
return {
|
|
673
|
+
get alive() {
|
|
674
|
+
return channel.alive;
|
|
675
|
+
},
|
|
676
|
+
instructions,
|
|
677
|
+
capabilities,
|
|
678
|
+
listTools: (signal) => listAllTools(channel, signal),
|
|
679
|
+
callTool: (name, args, signal) => callTool(channel, name, args, signal),
|
|
680
|
+
listResources: (signal) => paginate(channel, 'resources/list', 'resources', signal, readResourceSpec, '这个 server 没有声明 resources 能力'),
|
|
681
|
+
readResource: (uri, signal) => readResource(channel, uri, signal),
|
|
682
|
+
listPrompts: (signal) => paginate(channel, 'prompts/list', 'prompts', signal, readPromptSpec, '这个 server 没有声明 prompts 能力'),
|
|
683
|
+
getPrompt: (name, args, signal) => getPrompt(channel, name, args, signal),
|
|
684
|
+
close: () => {
|
|
685
|
+
channel.close();
|
|
686
|
+
},
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* 按配置起子进程:环境是筛过的,命令是按 PATH + PATHEXT 解析过的。
|
|
691
|
+
*
|
|
692
|
+
* @param fallbackCwd - 配置没写 `cwd` 时用哪个目录起进程(调用方给当前会话的目录)。
|
|
693
|
+
* 两边都为空的极少数情形才交给系统(继承宿主进程目录)——那是兜底,不是常态。
|
|
694
|
+
*/
|
|
695
|
+
function spawnStdio(server, fallbackCwd) {
|
|
696
|
+
const env = buildChildEnv(Object.keys(server.env), server.env, process.env);
|
|
697
|
+
const dir = server.cwd !== '' ? server.cwd : fallbackCwd;
|
|
698
|
+
const plan = resolveCommand(server.command, env, dir);
|
|
699
|
+
return spawn(plan.file, plan.shell ? server.args.map(quoteForCmd) : server.args, {
|
|
700
|
+
env,
|
|
701
|
+
cwd: dir === '' ? undefined : dir,
|
|
702
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
703
|
+
windowsHide: true,
|
|
704
|
+
shell: plan.shell,
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
/** 列全部工具,跟着 nextCursor 翻页;游标兜圈子时立刻收手。 */
|
|
708
|
+
async function listAllTools(channel, signal) {
|
|
709
|
+
return paginate(channel, 'tools/list', 'tools', signal, readToolSpec, '这个 server 没有工具');
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* T24:把 tools/list 的翻页循环抽成通用形态——resources/list 与 prompts/list 是
|
|
713
|
+
* 同一套「请求 → 取数组 → 跟 nextCursor 翻页 → 游标兜圈子收手」的骨架。
|
|
714
|
+
*/
|
|
715
|
+
async function paginate(channel, method, itemKey, signal, readItem, emptyMessage) {
|
|
716
|
+
const items = [];
|
|
717
|
+
const seen = new Set();
|
|
718
|
+
let cursor;
|
|
719
|
+
for (let page = 0; page < MAX_TOOL_PAGES; page += 1) {
|
|
720
|
+
const result = await channel.request(method, cursor === undefined ? {} : { cursor }, signal);
|
|
721
|
+
const doc = asRecord(result);
|
|
722
|
+
const listed = doc?.[itemKey];
|
|
723
|
+
if (Array.isArray(listed)) {
|
|
724
|
+
for (const item of listed) {
|
|
725
|
+
const read = readItem(item);
|
|
726
|
+
if (read !== null)
|
|
727
|
+
items.push(read);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
const next = doc?.nextCursor;
|
|
731
|
+
if (typeof next !== 'string' || next === '' || seen.has(next))
|
|
732
|
+
return items;
|
|
733
|
+
seen.add(next);
|
|
734
|
+
cursor = next;
|
|
735
|
+
}
|
|
736
|
+
return items;
|
|
737
|
+
}
|
|
738
|
+
/** 读 server 返回的一个工具定义;没有名字的项丢掉(dsc 的工具名必须有名字)。 */
|
|
739
|
+
function readToolSpec(item) {
|
|
740
|
+
const doc = asRecord(item);
|
|
741
|
+
if (doc === null || typeof doc.name !== 'string' || doc.name === '')
|
|
742
|
+
return null;
|
|
743
|
+
const schema = asRecord(doc.inputSchema);
|
|
744
|
+
return {
|
|
745
|
+
name: doc.name,
|
|
746
|
+
description: typeof doc.description === 'string' ? doc.description : '',
|
|
747
|
+
parameters: schema ?? { type: 'object', properties: {} },
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
/** 读 resources/list 的一项;没有 uri 的丢掉。 */
|
|
751
|
+
function readResourceSpec(item) {
|
|
752
|
+
const doc = asRecord(item);
|
|
753
|
+
if (doc === null || typeof doc.uri !== 'string' || doc.uri === '')
|
|
754
|
+
return null;
|
|
755
|
+
return {
|
|
756
|
+
uri: doc.uri,
|
|
757
|
+
name: typeof doc.name === 'string' ? doc.name : doc.uri,
|
|
758
|
+
description: typeof doc.description === 'string' ? doc.description : '',
|
|
759
|
+
mimeType: typeof doc.mimeType === 'string' ? doc.mimeType : '',
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
/** 读 prompts/list 的一项;没有名字的丢掉。 */
|
|
763
|
+
function readPromptSpec(item) {
|
|
764
|
+
const doc = asRecord(item);
|
|
765
|
+
if (doc === null || typeof doc.name !== 'string' || doc.name === '')
|
|
766
|
+
return null;
|
|
767
|
+
const listed = Array.isArray(doc.arguments) ? doc.arguments : [];
|
|
768
|
+
const names = listed
|
|
769
|
+
.map((entry) => asRecord(entry))
|
|
770
|
+
.filter((entry) => entry !== null && typeof entry.name === 'string')
|
|
771
|
+
.map((entry) => entry.name);
|
|
772
|
+
return {
|
|
773
|
+
name: doc.name,
|
|
774
|
+
description: typeof doc.description === 'string' ? doc.description : '',
|
|
775
|
+
arguments: names,
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
/** 读一个资源:文本内容拼起来;blob(base64 二进制)只给一句说明。 */
|
|
779
|
+
async function readResource(channel, uri, signal) {
|
|
780
|
+
const result = await channel.request('resources/read', { uri }, signal);
|
|
781
|
+
const doc = asRecord(result) ?? {};
|
|
782
|
+
const contents = Array.isArray(doc.contents) ? doc.contents : [];
|
|
783
|
+
const parts = [];
|
|
784
|
+
for (const item of contents) {
|
|
785
|
+
const entry = asRecord(item);
|
|
786
|
+
if (entry === null)
|
|
787
|
+
continue;
|
|
788
|
+
if (typeof entry.text === 'string') {
|
|
789
|
+
parts.push(entry.text);
|
|
790
|
+
continue;
|
|
791
|
+
}
|
|
792
|
+
parts.push(typeof entry.blob === 'string' ? `[二进制资源(${String(entry.mimeType ?? '未知类型')}),纯文本结果里显示不了]` : `[不支持的资源内容]`);
|
|
793
|
+
}
|
|
794
|
+
const text = parts.length === 0 ? '(这个资源没有内容)' : parts.join('\n---\n');
|
|
795
|
+
// 不在这里做防注入围栏——与 tools/call 一致,围栏是插件层注册工具时统一套的
|
|
796
|
+
return { text, isError: false };
|
|
797
|
+
}
|
|
798
|
+
/** 取一份提示模板:messages 里的文本拼一串。 */
|
|
799
|
+
async function getPrompt(channel, name, args, signal) {
|
|
800
|
+
const result = await channel.request('prompts/get', { name, arguments: args }, signal);
|
|
801
|
+
const doc = asRecord(result) ?? {};
|
|
802
|
+
const messages = Array.isArray(doc.messages) ? doc.messages : [];
|
|
803
|
+
const parts = [];
|
|
804
|
+
for (const item of messages) {
|
|
805
|
+
const entry = asRecord(item);
|
|
806
|
+
const content = asRecord(entry?.content);
|
|
807
|
+
if (content !== null && typeof content.text === 'string') {
|
|
808
|
+
parts.push(`[${String(entry?.role ?? 'user')}]\n${content.text}`);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
return { text: parts.length === 0 ? '(这份提示模板没有文本内容)' : parts.join('\n\n'), isError: false };
|
|
812
|
+
}
|
|
813
|
+
/** 调一次工具,把 content 里的文本部分拼起来。 */
|
|
814
|
+
async function callTool(channel, name, args, signal) {
|
|
815
|
+
const result = await channel.request('tools/call', { name, arguments: args }, signal);
|
|
816
|
+
const doc = asRecord(result) ?? {};
|
|
817
|
+
return { text: readContentText(doc.content), isError: doc.isError === true };
|
|
818
|
+
}
|
|
819
|
+
/** content 数组里的文本拼起来;图片这类非文本内容只留一句说明。 */
|
|
820
|
+
function readContentText(content) {
|
|
821
|
+
if (!Array.isArray(content))
|
|
822
|
+
return '(这次调用没有返回内容)';
|
|
823
|
+
const parts = [];
|
|
824
|
+
for (const item of content) {
|
|
825
|
+
const doc = asRecord(item);
|
|
826
|
+
if (doc === null)
|
|
827
|
+
continue;
|
|
828
|
+
if (doc.type === 'text' && typeof doc.text === 'string') {
|
|
829
|
+
parts.push(doc.text);
|
|
830
|
+
continue;
|
|
831
|
+
}
|
|
832
|
+
if (typeof doc.type === 'string')
|
|
833
|
+
parts.push(`[${doc.type} 类型的内容,纯文本结果里显示不了]`);
|
|
834
|
+
}
|
|
835
|
+
return parts.length === 0 ? '(这次调用没有返回内容)' : parts.join('\n');
|
|
836
|
+
}
|