@amoree-code/ocean 2.22.0 → 3.0.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/README.md +46 -53
- package/dist/application/clients/bundles.js +220 -0
- package/dist/application/clients/chat-plugin.js +4 -42
- package/dist/application/clients/client-detect.js +291 -0
- package/dist/application/clients/clients.js +104 -25
- package/dist/application/clients/desktop-link.js +40 -0
- package/dist/application/clients/desktop-watcher.js +6 -4
- package/dist/application/clients/hook-trust.js +106 -0
- package/dist/application/clients/hooks.js +7 -1
- package/dist/application/clients/mcp-schema-cost.js +74 -0
- package/dist/application/clients/native.js +38 -2
- package/dist/application/clients/registry.js +51 -9
- package/dist/application/clients/status.js +7 -2
- package/dist/application/clients/zip.js +44 -0
- package/dist/application/gateway/webhook-gateway.js +1 -1
- package/dist/application/integrations/charter-copies.js +1 -1
- package/dist/application/integrations/engine-entry-repair.js +4 -3
- package/dist/application/integrations/skill-auto-sync.js +2 -2
- package/dist/application/integrations/skill-filter.js +1 -1
- package/dist/application/integrations/skill-router.js +134 -90
- package/dist/application/layout/layout-apply.js +1 -1
- package/dist/application/layout/workspace-template.js +1 -1
- package/dist/application/onboarding/steps.js +121 -30
- package/dist/application/profiles/profile-doctor.js +118 -0
- package/dist/application/profiles/profile-migration-service.js +208 -0
- package/dist/application/profiles/profile-suggestion.js +3 -7
- package/dist/application/profiles/profile-upgrade-service.js +98 -86
- package/dist/application/pull/history-index.js +1 -1
- package/dist/application/pull/project-match.js +3 -3
- package/dist/application/pull/pull.js +1 -1
- package/dist/application/pull/server-capture.js +92 -9
- package/dist/application/scheduler/local-scheduler.js +2 -2
- package/dist/application/spend/claude-prices.js +55 -0
- package/dist/application/spend/claude-spend.js +365 -0
- package/dist/application/uninstall/uninstall.js +25 -3
- package/dist/application/upkeep/nightly-upkeep.js +4 -4
- package/dist/composition/runtime.js +2 -2
- package/dist/infrastructure/desktop/claude-export.js +1 -1
- package/dist/infrastructure/desktop/desktop-chat.js +60 -13
- package/dist/infrastructure/desktop/indexeddb.js +4 -4
- package/dist/infrastructure/desktop/leveldb.js +2 -2
- package/dist/infrastructure/filesystem/profile-loader.js +3 -45
- package/dist/infrastructure/filesystem/runtime-config-loader.js +1 -1
- package/dist/infrastructure/observability/runtime-logger.js +1 -1
- package/dist/infrastructure/process/agent-tool-executor.js +3 -3
- package/dist/infrastructure/process/process-supervisor.js +1 -1
- package/dist/infrastructure/process/service.js +8 -8
- package/dist/infrastructure/providers/openai-compatible.js +2 -2
- package/dist/infrastructure/providers/provider-invocation.js +1 -1
- package/dist/infrastructure/providers/session-facts-completer.js +78 -5
- package/dist/interfaces/cli/auth-command.js +2 -2
- package/dist/interfaces/cli/brief-command.js +1 -1
- package/dist/interfaces/cli/browser-command.js +6 -6
- package/dist/interfaces/cli/browser-findings.js +6 -9
- package/dist/interfaces/cli/browser-settings.js +10 -10
- package/dist/interfaces/cli/capture-command.js +6 -6
- package/dist/interfaces/cli/client-test-command.js +3 -3
- package/dist/interfaces/cli/clients-command.js +55 -6
- package/dist/interfaces/cli/command-aliases.js +56 -0
- package/dist/interfaces/cli/command-registry.js +48 -1414
- package/dist/interfaces/cli/commands/browser.js +5 -0
- package/dist/interfaces/cli/commands/capture.js +19 -0
- package/dist/interfaces/cli/commands/catalog.js +9 -0
- package/dist/interfaces/cli/commands/client.js +135 -0
- package/dist/interfaces/cli/commands/clients.js +33 -0
- package/dist/interfaces/cli/commands/connectors.js +14 -0
- package/dist/interfaces/cli/commands/context.js +9 -0
- package/dist/interfaces/cli/commands/cost.js +15 -0
- package/dist/interfaces/cli/commands/daily.js +11 -0
- package/dist/interfaces/cli/commands/day.js +25 -0
- package/dist/interfaces/cli/commands/dispatch.js +31 -0
- package/dist/interfaces/cli/commands/doctor.js +60 -0
- package/dist/interfaces/cli/commands/env.js +5 -0
- package/dist/interfaces/cli/commands/gateway.js +16 -0
- package/dist/interfaces/cli/commands/handoff.js +11 -0
- package/dist/interfaces/cli/commands/health.js +17 -0
- package/dist/interfaces/cli/commands/hook.js +142 -0
- package/dist/interfaces/cli/commands/idea.js +11 -0
- package/dist/interfaces/cli/commands/install.js +31 -0
- package/dist/interfaces/cli/commands/intent.js +14 -0
- package/dist/interfaces/cli/commands/intercept.js +22 -0
- package/dist/interfaces/cli/commands/layout.js +11 -0
- package/dist/interfaces/cli/commands/lifecycle.js +5 -0
- package/dist/interfaces/cli/commands/loop.js +73 -0
- package/dist/interfaces/cli/commands/market.js +11 -0
- package/dist/interfaces/cli/commands/mcp.js +57 -0
- package/dist/interfaces/cli/commands/memory.js +5 -0
- package/dist/interfaces/cli/commands/migrate.js +5 -0
- package/dist/interfaces/cli/commands/observe.js +11 -0
- package/dist/interfaces/cli/commands/obsidian.js +111 -0
- package/dist/interfaces/cli/commands/operate.js +7 -0
- package/dist/interfaces/cli/commands/policy.js +16 -0
- package/dist/interfaces/cli/commands/project.js +207 -0
- package/dist/interfaces/cli/commands/repair.js +20 -0
- package/dist/interfaces/cli/commands/run.js +52 -0
- package/dist/interfaces/cli/commands/schedule.js +58 -0
- package/dist/interfaces/cli/commands/service.js +21 -0
- package/dist/interfaces/cli/commands/session.js +182 -0
- package/dist/interfaces/cli/commands/sessions.js +12 -0
- package/dist/interfaces/cli/commands/setup.js +21 -0
- package/dist/interfaces/cli/commands/skill.js +159 -0
- package/dist/interfaces/cli/commands/tasks.js +10 -0
- package/dist/interfaces/cli/commands/update-remove.js +25 -0
- package/dist/interfaces/cli/connectors-command.js +150 -0
- package/dist/interfaces/cli/context-command.js +39 -3
- package/dist/interfaces/cli/cost-command.js +104 -0
- package/dist/interfaces/cli/curator-command.js +1 -1
- package/dist/interfaces/cli/daily-command.js +7 -1
- package/dist/interfaces/cli/desktop-command.js +43 -7
- package/dist/interfaces/cli/distill-command.js +7 -2
- package/dist/interfaces/cli/express-command.js +1 -1
- package/dist/interfaces/cli/failures-command.js +4 -4
- package/dist/interfaces/cli/gateway-findings.js +2 -2
- package/dist/interfaces/cli/governance-command.js +23 -16
- package/dist/interfaces/cli/handoff-command.js +1 -1
- package/dist/interfaces/cli/harvest-command.js +2 -2
- package/dist/interfaces/cli/help-command.js +53 -169
- package/dist/interfaces/cli/hygiene-command.js +1 -1
- package/dist/interfaces/cli/idea-command.js +1 -1
- package/dist/interfaces/cli/intercept-command.js +8 -7
- package/dist/interfaces/cli/interests-command.js +1 -1
- package/dist/interfaces/cli/layout-command.js +2 -0
- package/dist/interfaces/cli/market-command.js +9 -9
- package/dist/interfaces/cli/mcp-gateway-command.js +124 -7
- package/dist/interfaces/cli/mcp-probe-command.js +186 -0
- package/dist/interfaces/cli/mcp-status-command.js +5 -5
- package/dist/interfaces/cli/memory-command.js +41 -7
- package/dist/interfaces/cli/migrate-command.js +2 -2
- package/dist/interfaces/cli/note-command.js +1 -1
- package/dist/interfaces/cli/onboard-command.js +14 -0
- package/dist/interfaces/cli/operate-command.js +5 -5
- package/dist/interfaces/cli/profiles-command.js +61 -1
- package/dist/interfaces/cli/rhythm-command.js +11 -5
- package/dist/interfaces/cli/running-server-findings.js +1 -1
- package/dist/interfaces/cli/scorecard-command.js +8 -8
- package/dist/interfaces/cli/session-facts-command.js +2 -2
- package/dist/interfaces/cli/sessions-command.js +145 -0
- package/dist/interfaces/cli/setup-command.js +15 -2
- package/dist/interfaces/cli/tasks-command.js +63 -13
- package/dist/interfaces/cli/telemetry-command.js +4 -4
- package/dist/interfaces/cli/uninstall-command.js +1 -1
- package/dist/interfaces/cli/week-failures.js +32 -0
- package/dist/main.js +12 -0
- package/node_modules/@ocean/brain/dist/{infrastructure/brain/brain-index.js → brain-index.js} +2 -2
- package/node_modules/@ocean/brain/dist/{brain/brain-markdown.js → brain-markdown.js} +1 -1
- package/node_modules/@ocean/brain/dist/{brain/brain-service.js → brain-service.js} +12 -2
- package/node_modules/@ocean/brain/dist/memory/brain-dump.js +92 -16
- package/node_modules/@ocean/brain/dist/memory/extraction-log.js +125 -0
- package/node_modules/@ocean/brain/dist/memory/extraction-retry.js +54 -0
- package/node_modules/@ocean/brain/dist/memory/fact-routing.js +44 -1
- package/node_modules/@ocean/brain/dist/memory/lesson-activation.js +2 -2
- package/node_modules/@ocean/brain/dist/memory/lesson-learning.js +2 -2
- package/node_modules/@ocean/brain/dist/memory/lessons.js +1 -1
- package/node_modules/@ocean/brain/dist/memory/memory-audit.js +2 -2
- package/node_modules/@ocean/brain/dist/memory/memory-harvest.js +1 -1
- package/node_modules/@ocean/brain/dist/memory/memory-paths.js +8 -0
- package/node_modules/@ocean/brain/dist/memory/memory-review.js +31 -3
- package/node_modules/@ocean/brain/dist/memory/memory-trash.js +29 -0
- package/node_modules/@ocean/brain/dist/memory/memory-write.js +4 -2
- package/node_modules/@ocean/brain/dist/memory/model-narrative.js +2 -2
- package/node_modules/@ocean/brain/dist/memory/profile-facts.js +1 -1
- package/node_modules/@ocean/brain/dist/memory/routing.json +6 -6
- package/node_modules/@ocean/{market/dist/skills → brain/dist/memory}/signal-detection.js +1 -1
- package/node_modules/@ocean/brain/dist/{memory → people}/people.js +1 -1
- package/node_modules/@ocean/brain/dist/sessions-capture/session-facts.js +625 -0
- package/node_modules/@ocean/brain/dist/{memory → sessions-capture}/session-promotion.js +7 -15
- package/node_modules/@ocean/brain/dist/{memory → sessions-capture}/session-summary.js +1 -1
- package/node_modules/@ocean/brain/dist/sessions-capture/session-triage.js +52 -0
- package/node_modules/@ocean/brain/package.json +1 -1
- package/node_modules/@ocean/connect/dist/connectors/connector-hub.js +173 -0
- package/node_modules/@ocean/{market → connect}/dist/install/default-marketplace.js +6 -2
- package/node_modules/@ocean/{market → connect}/dist/install/marketplace-service.js +1 -1
- package/node_modules/@ocean/connect/dist/mcp/mcp-gateway.js +987 -0
- package/node_modules/@ocean/{market → connect}/dist/mcp/mcp-launch.js +102 -5
- package/node_modules/@ocean/{market → connect}/dist/registry/official-registry.js +1 -1
- package/node_modules/@ocean/{market → connect}/dist/security/permissions.js +1 -1
- package/node_modules/@ocean/{market → connect}/dist/skills/skill-hub.js +104 -6
- package/node_modules/@ocean/{market → connect}/package.json +2 -2
- package/node_modules/@ocean/context/dist/{context/context-ladder.js → context-ladder.js} +3 -3
- package/node_modules/@ocean/context/dist/{context/context-references.js → context-references.js} +5 -4
- package/node_modules/@ocean/context/dist/{context/context-cost.js → context-size.js} +6 -6
- package/node_modules/@ocean/context/dist/{context/intent-router.js → intent-router.js} +1 -1
- package/node_modules/@ocean/context/dist/{context/resource-injection.js → resource-injection.js} +3 -1
- package/node_modules/@ocean/context/package.json +1 -1
- package/node_modules/@ocean/core/dist/domain/brain/brain-record.js +1 -1
- package/node_modules/@ocean/core/dist/domain/capabilities/capability-manifest.js +5 -5
- package/node_modules/@ocean/core/dist/domain/connector-entry.js +86 -0
- package/node_modules/@ocean/core/dist/domain/context/context.js +1 -1
- package/node_modules/@ocean/core/dist/domain/context/project-id.js +8 -2
- package/node_modules/@ocean/core/dist/domain/{evidence/evidence.js → evidence.js} +1 -1
- package/node_modules/@ocean/core/dist/domain/mcp/memory-contract.js +4 -1
- package/node_modules/@ocean/core/dist/domain/ports/brain-index-port.js +1 -1
- package/node_modules/@ocean/core/dist/domain/profiles/profile-markdown.js +40 -0
- package/node_modules/@ocean/core/dist/domain/profiles/profile-resolution.js +276 -0
- package/node_modules/@ocean/core/dist/domain/profiles/profile-selection.js +23 -60
- package/node_modules/@ocean/core/dist/domain/profiles/profile-upgrade.js +2 -2
- package/node_modules/@ocean/core/dist/domain/profiles/profile.js +66 -6
- package/node_modules/@ocean/core/dist/domain/profiles/runtime-config.js +1 -1
- package/node_modules/@ocean/core/dist/domain/runs/agent-tools.js +2 -2
- package/node_modules/@ocean/core/dist/domain/sessions/session-identity.js +53 -0
- package/node_modules/@ocean/core/dist/domain/sessions/session-metadata.js +208 -0
- package/node_modules/@ocean/core/dist/domain/{tasks/task-id.js → task-id.js} +2 -2
- package/node_modules/@ocean/core/dist/profiles/profile-files.js +57 -0
- package/node_modules/@ocean/core/dist/profiles/profile-loader.js +97 -0
- package/node_modules/@ocean/core/dist/profiles/project-profile-fill.js +33 -0
- package/node_modules/@ocean/core/dist/profiles/session-profile.js +158 -0
- package/node_modules/@ocean/core/dist/shared/charter.js +110 -0
- package/node_modules/@ocean/core/dist/shared/context-ack.js +1 -1
- package/node_modules/@ocean/core/dist/shared/context-triggers.js +67 -0
- package/node_modules/@ocean/core/dist/shared/paths.js +6 -6
- package/node_modules/@ocean/core/dist/shared/project-legacy.js +232 -0
- package/node_modules/@ocean/core/dist/shared/project-manifest.js +308 -0
- package/node_modules/@ocean/core/dist/shared/project-names.js +87 -0
- package/node_modules/@ocean/core/dist/shared/project-register.js +560 -0
- package/node_modules/@ocean/core/dist/shared/project-resolution.js +83 -861
- package/node_modules/@ocean/core/dist/shared/repo-signals.js +45 -0
- package/node_modules/@ocean/core/dist/shared/task-reference.js +1 -1
- package/node_modules/@ocean/core/dist/shared/text-tokens.js +37 -0
- package/node_modules/@ocean/core/dist/shared/tool-groups.js +224 -0
- package/node_modules/@ocean/core/package.json +4 -1
- package/node_modules/@ocean/services/dist/day/day.js +45 -3
- package/node_modules/@ocean/services/dist/{distill → day}/distill.js +29 -3
- package/node_modules/@ocean/services/dist/{harvest → day}/harvest.js +54 -14
- package/node_modules/@ocean/services/dist/{brief → day}/interest-discovery.js +3 -3
- package/node_modules/@ocean/services/dist/day/note.js +2 -2
- package/node_modules/@ocean/services/dist/{onboarding → day}/onboarding.js +1 -1
- package/node_modules/@ocean/services/dist/day/patterns.js +68 -0
- package/node_modules/@ocean/services/dist/{curator → health}/curator.js +5 -5
- package/node_modules/@ocean/services/dist/{journey → health}/failures.js +7 -5
- package/node_modules/@ocean/services/dist/{hygiene → health}/hygiene.js +2 -2
- package/node_modules/@ocean/services/dist/{journey → health}/lesson-effect.js +1 -1
- package/node_modules/@ocean/services/dist/{scorecard → health}/scorecard.js +3 -3
- package/node_modules/@ocean/services/dist/{upkeep → health}/upkeep.js +2 -2
- package/node_modules/@ocean/services/dist/{doctor → health}/workspace-doctor.js +57 -17
- package/node_modules/@ocean/services/dist/{browser → integrations/browser}/browser-session.js +1 -1
- package/node_modules/@ocean/services/dist/{infrastructure → integrations}/mcp/call-ledger.js +3 -0
- package/node_modules/@ocean/services/dist/{infrastructure → integrations}/mcp/mcp-client.js +19 -3
- package/node_modules/@ocean/services/dist/{infrastructure → integrations}/mcp/obsidian-server.js +4 -4
- package/node_modules/@ocean/services/dist/{infrastructure → integrations}/mcp/ocean-server.js +423 -34
- package/node_modules/@ocean/services/dist/{obsidian → integrations/obsidian}/conflict-log.js +1 -1
- package/node_modules/@ocean/services/dist/{obsidian → integrations/obsidian}/folder-templates.js +8 -5
- package/node_modules/@ocean/services/dist/projects/project-doctor.js +173 -0
- package/node_modules/@ocean/services/dist/projects/project-snippet.js +9 -0
- package/node_modules/@ocean/services/dist/projects/projects-overview.js +2 -2
- package/node_modules/@ocean/services/package.json +1 -1
- package/node_modules/@ocean/{loop/dist/loops → work/dist/runs}/guard-runtime.js +3 -3
- package/node_modules/@ocean/{loop/dist → work/dist/runs}/operations/memory-knowledge-operations.js +4 -12
- package/node_modules/@ocean/{loop/dist → work/dist/runs}/operations/operation-contract.js +2 -2
- package/node_modules/@ocean/{loop/dist → work/dist/runs}/operations/project-operations.js +2 -1
- package/node_modules/@ocean/{loop/dist → work/dist/runs}/operations/task-operations.js +4 -4
- package/node_modules/@ocean/{loop/dist → work/dist/runs}/operations/write-guard.js +1 -1
- package/node_modules/@ocean/{loop → work}/dist/runs/prompt-assembly.js +2 -2
- package/node_modules/@ocean/{loop → work}/dist/runs/run-agent.js +4 -4
- package/node_modules/@ocean/{loop → work}/dist/runs/tool-loop.js +4 -4
- package/node_modules/@ocean/work/dist/sessions/hooks/desktop-facts.js +309 -0
- package/node_modules/@ocean/{loop/dist → work/dist/sessions}/hooks/session-end-hook.js +36 -5
- package/node_modules/@ocean/{loop/dist → work/dist/sessions}/hooks/session-facts-hook.js +144 -14
- package/node_modules/@ocean/{loop/dist → work/dist/sessions}/hooks/session-start-hook.js +129 -12
- package/node_modules/@ocean/work/dist/sessions/hooks/start-brief.js +110 -0
- package/node_modules/@ocean/work/dist/sessions/hooks/unlinked-warning.js +68 -0
- package/node_modules/@ocean/{loop → work}/dist/sessions/session-closeout.js +4 -4
- package/node_modules/@ocean/{loop → work}/dist/sessions/session-projects.js +13 -3
- package/node_modules/@ocean/work/dist/sessions/session-relink.js +300 -0
- package/node_modules/@ocean/{loop → work}/dist/sessions/session-retention.js +1 -1
- package/node_modules/@ocean/{loop → work}/dist/tasks/list-tasks.js +7 -2
- package/node_modules/@ocean/{loop → work}/dist/tasks/renumber-tasks.js +4 -2
- package/node_modules/@ocean/{loop → work}/dist/tasks/session-task.js +2 -2
- package/node_modules/@ocean/{loop → work}/dist/tasks/sync-task-prs.js +77 -7
- package/node_modules/@ocean/work/dist/tasks/task-git.js +63 -0
- package/node_modules/@ocean/{loop/dist/loops → work/dist/tasks}/task-loop.js +4 -4
- package/node_modules/@ocean/{loop → work}/dist/tasks/verification-readiness.js +1 -1
- package/node_modules/@ocean/{loop → work}/dist/tasks/verify-task.js +1 -1
- package/node_modules/@ocean/{loop → work}/package.json +2 -2
- package/ocean-marketplace.json +41 -41
- package/package.json +12 -11
- package/skills/core/daily-harvest/SKILL.md +1 -1
- package/skills/core/ocean-memory/SKILL.md +4 -0
- package/skills/core/use-browser/SKILL.md +30 -7
- package/skills/index.json +1 -1
- package/templates/profiles/default.md +44 -0
- package/templates/profiles/developer.md +45 -0
- package/templates/profiles/reviewer.md +44 -0
- package/templates/profiles/strategist.md +44 -0
- package/templates/profiles-v3/analysis.md +16 -0
- package/templates/profiles-v3/backend.md +16 -0
- package/templates/profiles-v3/base.md +57 -0
- package/templates/profiles-v3/code-review.md +16 -0
- package/templates/profiles-v3/devops.md +16 -0
- package/templates/profiles-v3/docs-office.md +16 -0
- package/templates/profiles-v3/frontend.md +16 -0
- package/templates/profiles-v3/learning.md +16 -0
- package/templates/profiles-v3/marketing-ads.md +16 -0
- package/templates/profiles-v3/marketing-content-seo.md +16 -0
- package/templates/profiles-v3/ocean-ops.md +16 -0
- package/templates/profiles-v3/product-strategy.md +16 -0
- package/templates/profiles-v3/qa-testing.md +16 -0
- package/templates/profiles-v3/security.md +16 -0
- package/templates/workspace/MANIFEST.json +1 -0
- package/templates/workspace/charter/core.md +22 -90
- package/templates/workspace/charter/policies/README.md +7 -1
- package/templates/workspace/charter/policies/claude-code-context.md +6 -1
- package/templates/workspace/charter/policies/concise-output.md +5 -0
- package/templates/workspace/charter/policies/context.md +5 -0
- package/templates/workspace/charter/policies/contracts/task.md +2 -2
- package/templates/workspace/charter/policies/git.md +5 -0
- package/templates/workspace/charter/policies/graph.md +6 -1
- package/templates/workspace/charter/policies/knowledge.md +11 -4
- package/templates/workspace/charter/policies/layout.md +23 -0
- package/templates/workspace/charter/policies/lifecycle.md +5 -0
- package/templates/workspace/charter/policies/memory.md +5 -0
- package/templates/workspace/charter/policies/minimal-change.md +5 -0
- package/templates/workspace/charter/policies/models.md +5 -0
- package/templates/workspace/charter/policies/response.md +5 -0
- package/templates/workspace/charter/policies/skill-observer.md +12 -0
- package/templates/workspace/charter/policies/stack.md +5 -0
- package/templates/workspace/charter/policies/task.md +17 -3
- package/templates/workspace/charter/policies/verification.md +5 -0
- package/node_modules/@ocean/brain/dist/evidence/verification-report.js +0 -13
- package/node_modules/@ocean/brain/dist/memory/daily-narrative.js +0 -142
- package/node_modules/@ocean/brain/dist/memory/session-facts.js +0 -283
- package/node_modules/@ocean/context/dist/context/context-compression.js +0 -66
- package/node_modules/@ocean/loop/dist/hooks/desktop-facts.js +0 -174
- package/node_modules/@ocean/loop/dist/operations/task-linker.js +0 -40
- package/node_modules/@ocean/loop/dist/sessions/session-pointer.js +0 -133
- package/node_modules/@ocean/loop/dist/tasks/task-git.js +0 -28
- package/node_modules/@ocean/market/dist/mcp/mcp-gateway.js +0 -533
- package/templates/profiles/default.json +0 -51
- package/templates/profiles/developer.json +0 -51
- package/templates/profiles/reviewer.json +0 -51
- package/templates/profiles/strategist.json +0 -51
- /package/node_modules/@ocean/brain/dist/{brain/brain-eval.js → brain-eval.js} +0 -0
- /package/node_modules/@ocean/brain/dist/{brain/brain-reindex.js → brain-reindex.js} +0 -0
- /package/node_modules/@ocean/brain/dist/{capture → sessions-capture}/inbox-sync.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/adapters/mcp-adapter.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/adapters/owned-directory.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/adapters/plugin-adapter.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/adapters/skill-adapter.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/capability/adapter.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/capability/catalog.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/capability/content-digest.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/capability/registry.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/install/lock.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/install/own-server.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/mcp/chrome-debugging.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/mcp/mcp-connection.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/mcp/mcp-runner.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/security/description-scan.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/security/secrets.js +0 -0
- /package/node_modules/@ocean/{market → connect}/dist/skills/core-skill-sync.js +0 -0
- /package/node_modules/@ocean/context/dist/{context/context-packet.js → context-packet.js} +0 -0
- /package/node_modules/@ocean/core/dist/domain/{process/process-events.js → process-events.js} +0 -0
- /package/node_modules/@ocean/core/dist/domain/{redaction/redaction.js → redaction.js} +0 -0
- /package/node_modules/@ocean/services/dist/{brief → day}/brief.js +0 -0
- /package/node_modules/@ocean/services/dist/{express → day}/express.js +0 -0
- /package/node_modules/@ocean/services/dist/{brief → day}/source-registry.json +0 -0
- /package/node_modules/@ocean/services/dist/{journey → health}/telemetry.js +0 -0
- /package/node_modules/@ocean/services/dist/{browser → integrations/browser}/browser-service.js +0 -0
- /package/node_modules/@ocean/services/dist/{browser → integrations/browser}/browser-task-runner.js +0 -0
- /package/node_modules/@ocean/services/dist/{infrastructure → integrations}/mcp/mcp-session.js +0 -0
- /package/node_modules/@ocean/services/dist/{infrastructure → integrations}/mcp/server-version.js +0 -0
- /package/node_modules/@ocean/services/dist/{infrastructure → integrations}/mcp/tool-registry.js +0 -0
- /package/node_modules/@ocean/services/dist/{obsidian → integrations/obsidian}/inbox-promotion.js +0 -0
- /package/node_modules/@ocean/services/dist/{obsidian → integrations/obsidian}/vault-discovery.js +0 -0
- /package/node_modules/@ocean/services/dist/{obsidian → integrations/obsidian}/vault-ingestion.js +0 -0
- /package/node_modules/@ocean/services/dist/{obsidian → integrations/obsidian}/vault-sync.js +0 -0
- /package/node_modules/@ocean/services/dist/{obsidian → integrations/obsidian}/vault-writer.js +0 -0
- /package/node_modules/@ocean/{loop/dist → work/dist/runs}/operations/operation-scope.js +0 -0
- /package/node_modules/@ocean/{loop/dist → work/dist/runs}/operations/record-io.js +0 -0
- /package/node_modules/@ocean/{loop/dist → work/dist/runs}/operations/record-operations.js +0 -0
- /package/node_modules/@ocean/{loop → work}/dist/runs/run-authorization.js +0 -0
- /package/node_modules/@ocean/{loop → work}/dist/runs/session-tokens.js +0 -0
- /package/node_modules/@ocean/{loop/dist/handoff → work/dist/sessions}/handoff-service.js +0 -0
- /package/node_modules/@ocean/{loop/dist → work/dist/sessions}/hooks/lifecycle-hooks.js +0 -0
- /package/node_modules/@ocean/{loop/dist/infrastructure/persistence → work/dist/sessions}/session-store.js +0 -0
- /package/node_modules/@ocean/{loop/dist/infrastructure/persistence → work/dist/sessions}/session-validator.js +0 -0
- /package/node_modules/@ocean/{loop → work}/dist/tasks/archive-tasks.js +0 -0
- /package/node_modules/@ocean/{loop → work}/dist/tasks/checkpoint-task.js +0 -0
- /package/node_modules/@ocean/{loop → work}/dist/tasks/claim-task.js +0 -0
- /package/node_modules/@ocean/{loop/dist/loops → work/dist/tasks}/iteration-graph.js +0 -0
- /package/node_modules/@ocean/{loop → work}/dist/tasks/new-task.js +0 -0
- /package/node_modules/@ocean/{loop/dist/loops → work/dist/tasks}/task-loop-record.js +0 -0
- /package/node_modules/@ocean/{loop → work}/dist/tasks/task-verification.js +0 -0
|
@@ -0,0 +1,987 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdir, readdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { marketplaceIndexSchema } from "@ocean/core/domain/capabilities/capability-manifest.js";
|
|
5
|
+
import { engineRoot } from "@ocean/core/shared/paths.js";
|
|
6
|
+
import { scanText } from "../security/description-scan.js";
|
|
7
|
+
import { engineIndexFile } from "./mcp-connection.js";
|
|
8
|
+
import { literalArguments, planMcpLaunch, } from "./mcp-launch.js";
|
|
9
|
+
export const GATEWAY_CAPABILITY = "dev.ocean/mcp-gateway";
|
|
10
|
+
// The gateway's own tools use this namespace, so no member may take it.
|
|
11
|
+
const GATEWAY_PREFIX = "gateway";
|
|
12
|
+
const PROFILE_TOOL = `${GATEWAY_PREFIX}__use_profile`;
|
|
13
|
+
function profileAllows(profile, member) {
|
|
14
|
+
const short = member.capability.split("/").at(-1) ?? member.capability;
|
|
15
|
+
return profile.mcp.some((name) => name === member.capability || name === short || name === member.prefix);
|
|
16
|
+
}
|
|
17
|
+
// One file per member under `dir`. The key covers how the member is started and Ocean's
|
|
18
|
+
// version, so an update, a new pinned version or another launch plan lists it afresh. Env
|
|
19
|
+
// values and headers are left out of the key: they may hold secrets and do not change a
|
|
20
|
+
// server's tools.
|
|
21
|
+
export function gatewayManifestStore(dir, version) {
|
|
22
|
+
const keyOf = (member) => createHash("sha256")
|
|
23
|
+
.update(JSON.stringify(member.transport === "remote"
|
|
24
|
+
? [version, member.capability, member.type, member.url]
|
|
25
|
+
: [version, member.capability, member.command, member.args]))
|
|
26
|
+
.digest("hex");
|
|
27
|
+
const fileOf = (member) => path.join(dir, `${member.capability.toLowerCase().replace(/[^a-z0-9_-]+/g, "_")}.json`);
|
|
28
|
+
return {
|
|
29
|
+
async read(member) {
|
|
30
|
+
try {
|
|
31
|
+
const saved = JSON.parse(await readFile(fileOf(member), "utf8"));
|
|
32
|
+
if (saved?.key !== keyOf(member))
|
|
33
|
+
return undefined;
|
|
34
|
+
const listing = saved.listing;
|
|
35
|
+
if (!Array.isArray(listing?.tools))
|
|
36
|
+
return undefined;
|
|
37
|
+
return {
|
|
38
|
+
tools: listing.tools,
|
|
39
|
+
resources: Array.isArray(listing.resources) ? listing.resources : [],
|
|
40
|
+
prompts: Array.isArray(listing.prompts) ? listing.prompts : [],
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
async write(member, listing) {
|
|
48
|
+
await mkdir(dir, { recursive: true });
|
|
49
|
+
const file = fileOf(member);
|
|
50
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
51
|
+
await writeFile(temporary, `${JSON.stringify({ key: keyOf(member), listing }, null, 2)}\n`);
|
|
52
|
+
await rename(temporary, file);
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
// One-line hints for the browser errors seen most in transcripts (plan §2): a page lost after a
|
|
57
|
+
// reconnect, and element uids from a snapshot taken before the page changed.
|
|
58
|
+
const ERROR_HINTS = [
|
|
59
|
+
[
|
|
60
|
+
/No page found|Page ids have changed|Target closed|No page selected/i,
|
|
61
|
+
"Hint: the browser reconnected or the page closed; call list_pages, then select_page, and retry.",
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
/uid "?[^"\s]*"? not found/i,
|
|
65
|
+
"Hint: the page changed since the last snapshot; call take_snapshot for fresh uids, then retry.",
|
|
66
|
+
],
|
|
67
|
+
];
|
|
68
|
+
export function errorHint(text) {
|
|
69
|
+
return ERROR_HINTS.find(([pattern]) => pattern.test(text))?.[1];
|
|
70
|
+
}
|
|
71
|
+
// A failed tool result gets the hint as one more text block; a successful one is untouched.
|
|
72
|
+
function withHint(result) {
|
|
73
|
+
if (!isRecord(result) || result.isError !== true)
|
|
74
|
+
return result;
|
|
75
|
+
const content = Array.isArray(result.content) ? result.content : [];
|
|
76
|
+
const text = content
|
|
77
|
+
.map((item) => isRecord(item) && typeof item.text === "string" ? item.text : "")
|
|
78
|
+
.join("\n");
|
|
79
|
+
const hint = errorHint(text);
|
|
80
|
+
return hint
|
|
81
|
+
? { ...result, content: [...content, { type: "text", text: hint }] }
|
|
82
|
+
: result;
|
|
83
|
+
}
|
|
84
|
+
const errorMessage = (error) => error instanceof Error ? error.message : String(error);
|
|
85
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
86
|
+
function namespaceFor(capability, taken) {
|
|
87
|
+
const clean = (value) => value.toLowerCase().replace(/[^a-z0-9_-]+/g, "_");
|
|
88
|
+
const short = clean(capability.split("/").at(-1) ?? capability);
|
|
89
|
+
let prefix = taken.has(short) ? clean(capability) : short;
|
|
90
|
+
for (let suffix = 2; taken.has(prefix); suffix++)
|
|
91
|
+
prefix = `${clean(capability)}-${suffix}`;
|
|
92
|
+
taken.add(prefix);
|
|
93
|
+
return prefix;
|
|
94
|
+
}
|
|
95
|
+
const BROWSER = /(^|[^a-z])(browser|chrome|chromium|devtools|playwright|puppeteer|selenium|webdriver)([^a-z]|$)/i;
|
|
96
|
+
// Why an installed server is hidden from a client that was not allowed it by name, or nothing.
|
|
97
|
+
// Declared file writes count; so does driving a browser, recognised by name, since a browser
|
|
98
|
+
// server reads and acts inside every signed-in session whatever it declares.
|
|
99
|
+
export function sensitivityOf(installed) {
|
|
100
|
+
if (installed.permissions.filesystem.write.length)
|
|
101
|
+
return "writes files";
|
|
102
|
+
const names = [
|
|
103
|
+
installed.capability,
|
|
104
|
+
...(installed.server.packages ?? []).map((item) => String(item.identifier)),
|
|
105
|
+
];
|
|
106
|
+
if (names.some((name) => BROWSER.test(name)))
|
|
107
|
+
return "controls a browser";
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
export function canSee(access, client, member) {
|
|
111
|
+
const rule = client === undefined ? undefined : access.clients[client];
|
|
112
|
+
if (rule?.deny?.includes(member.capability))
|
|
113
|
+
return false;
|
|
114
|
+
if (!member.sensitive)
|
|
115
|
+
return true;
|
|
116
|
+
return Boolean(rule?.allow?.includes(member.capability) || rule?.allow?.includes("*"));
|
|
117
|
+
}
|
|
118
|
+
// `ocean clients allow|deny|unset <client> <capability>`: one rule changed, the rest kept.
|
|
119
|
+
export async function updateGatewayAccess(file, client, capability, change) {
|
|
120
|
+
const access = await readGatewayAccess(file);
|
|
121
|
+
const rule = access.clients[client] ?? {};
|
|
122
|
+
const without = (list) => (list ?? []).filter((item) => item !== capability);
|
|
123
|
+
const next = { allow: without(rule.allow), deny: without(rule.deny) };
|
|
124
|
+
if (change === "allow")
|
|
125
|
+
next.allow.push(capability);
|
|
126
|
+
if (change === "deny")
|
|
127
|
+
next.deny.push(capability);
|
|
128
|
+
if (next.allow.length || next.deny.length)
|
|
129
|
+
access.clients[client] = {
|
|
130
|
+
...(next.allow.length ? { allow: next.allow.sort() } : {}),
|
|
131
|
+
...(next.deny.length ? { deny: next.deny.sort() } : {}),
|
|
132
|
+
};
|
|
133
|
+
else
|
|
134
|
+
delete access.clients[client];
|
|
135
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
136
|
+
const temp = `${file}.${process.pid}.tmp`;
|
|
137
|
+
await writeFile(temp, `${JSON.stringify(access, null, 2)}\n`);
|
|
138
|
+
await rename(temp, file);
|
|
139
|
+
return access;
|
|
140
|
+
}
|
|
141
|
+
export async function readGatewayAccess(file) {
|
|
142
|
+
let raw;
|
|
143
|
+
try {
|
|
144
|
+
raw = JSON.parse(await readFile(file, "utf8"));
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
if (error.code === "ENOENT")
|
|
148
|
+
return { clients: {} };
|
|
149
|
+
throw new Error(`Cannot read ${file}: ${errorMessage(error)}`);
|
|
150
|
+
}
|
|
151
|
+
const clients = isRecord(raw) ? raw.clients : undefined;
|
|
152
|
+
const list = (value) => value === undefined ||
|
|
153
|
+
(Array.isArray(value) && value.every((item) => typeof item === "string"));
|
|
154
|
+
if (!isRecord(clients) ||
|
|
155
|
+
!Object.values(clients).every((rule) => isRecord(rule) && list(rule.allow) && list(rule.deny)))
|
|
156
|
+
throw new Error(`${file}: expected {"clients": {"<client name>": {"allow": [...], "deny": [...]}}}`);
|
|
157
|
+
return raw;
|
|
158
|
+
}
|
|
159
|
+
// The servers the gateway routes: the engine's first-party ones, then every MCP server installed
|
|
160
|
+
// from the marketplace, each planned by the same launcher as `ocean market exec` — declared
|
|
161
|
+
// permissions, declared environment only. A third-party server joins only by
|
|
162
|
+
// being installed; anything that cannot be launched here is reported, not fatal.
|
|
163
|
+
export async function gatewayMembers(options) {
|
|
164
|
+
const members = [];
|
|
165
|
+
const skipped = [];
|
|
166
|
+
const taken = new Set([GATEWAY_PREFIX]);
|
|
167
|
+
const notFirstParty = [];
|
|
168
|
+
const firstParty = new Set();
|
|
169
|
+
const index = marketplaceIndexSchema.parse(JSON.parse(await readFile(options.indexFile ?? engineIndexFile(), "utf8")));
|
|
170
|
+
for (const manifest of index.capabilities) {
|
|
171
|
+
if (manifest.kind !== "mcp" || manifest.name === GATEWAY_CAPABILITY)
|
|
172
|
+
continue;
|
|
173
|
+
const item = manifest.spec.packages?.[0];
|
|
174
|
+
if (item?.registryType !== "ocean-engine") {
|
|
175
|
+
notFirstParty.push(manifest.name);
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
firstParty.add(manifest.name);
|
|
179
|
+
if (options.firstParty === false)
|
|
180
|
+
continue;
|
|
181
|
+
members.push({
|
|
182
|
+
capability: manifest.name,
|
|
183
|
+
prefix: namespaceFor(manifest.name, taken),
|
|
184
|
+
command: process.execPath,
|
|
185
|
+
args: [
|
|
186
|
+
path.join(engineRoot(), "dist", "main.js"),
|
|
187
|
+
...literalArguments(item.packageArguments, `${manifest.name} packageArguments`),
|
|
188
|
+
],
|
|
189
|
+
cwd: engineRoot(),
|
|
190
|
+
env: {},
|
|
191
|
+
inheritEnv: true,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
let files = [];
|
|
195
|
+
try {
|
|
196
|
+
files = (await readdir(options.serversRoot))
|
|
197
|
+
.filter((file) => file.endsWith(".json"))
|
|
198
|
+
.sort();
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
// Nothing installed yet.
|
|
202
|
+
}
|
|
203
|
+
for (const file of files) {
|
|
204
|
+
let installed;
|
|
205
|
+
try {
|
|
206
|
+
installed = JSON.parse(await readFile(path.join(options.serversRoot, file), "utf8"));
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
skipped.push({ capability: file, reason: errorMessage(error) });
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
const name = installed.capability;
|
|
213
|
+
// An installed copy of an engine server runs the same code as the member already routed.
|
|
214
|
+
if (name === GATEWAY_CAPABILITY ||
|
|
215
|
+
firstParty.has(name) ||
|
|
216
|
+
members.some((member) => member.capability === name))
|
|
217
|
+
continue;
|
|
218
|
+
const stateDir = path.join(options.stateRoot, path.basename(file, ".json"));
|
|
219
|
+
try {
|
|
220
|
+
const launch = planMcpLaunch(installed, {
|
|
221
|
+
stateDir,
|
|
222
|
+
env: options.env,
|
|
223
|
+
secret: (secret) => options.secrets?.get(name, secret),
|
|
224
|
+
packagesRoot: options.packagesRoot,
|
|
225
|
+
});
|
|
226
|
+
const sensitive = sensitivityOf(installed);
|
|
227
|
+
const base = {
|
|
228
|
+
capability: name,
|
|
229
|
+
prefix: namespaceFor(name, taken),
|
|
230
|
+
...(sensitive ? { sensitive } : {}),
|
|
231
|
+
};
|
|
232
|
+
if (launch.transport === "remote") {
|
|
233
|
+
members.push({
|
|
234
|
+
...base,
|
|
235
|
+
transport: "remote",
|
|
236
|
+
type: launch.type,
|
|
237
|
+
url: launch.url,
|
|
238
|
+
...(Object.keys(launch.headers).length
|
|
239
|
+
? { headers: launch.headers }
|
|
240
|
+
: {}),
|
|
241
|
+
});
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
await mkdir(stateDir, { recursive: true });
|
|
245
|
+
members.push({
|
|
246
|
+
...base,
|
|
247
|
+
command: launch.command,
|
|
248
|
+
args: launch.args,
|
|
249
|
+
cwd: launch.cwd,
|
|
250
|
+
env: launch.env,
|
|
251
|
+
inheritEnv: false,
|
|
252
|
+
...(launch.pin ? { pin: launch.pin } : {}),
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
skipped.push({ capability: name, reason: errorMessage(error) });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
for (const name of notFirstParty)
|
|
260
|
+
if (!members.some((member) => member.capability === name))
|
|
261
|
+
skipped.push({
|
|
262
|
+
capability: name,
|
|
263
|
+
reason: "not first-party; install it from the marketplace to route it through the gateway",
|
|
264
|
+
uninstalled: true,
|
|
265
|
+
});
|
|
266
|
+
return { members, skipped };
|
|
267
|
+
}
|
|
268
|
+
// Every text a model reads for a tool, resource or prompt: each named field, and each key and
|
|
269
|
+
// string nested inside it (a tool's schema property names, titles, enums and defaults included).
|
|
270
|
+
function modelTexts(fields) {
|
|
271
|
+
const texts = [];
|
|
272
|
+
const walk = (value, at) => {
|
|
273
|
+
if (typeof value === "string")
|
|
274
|
+
texts.push({ at, text: value });
|
|
275
|
+
else if (Array.isArray(value))
|
|
276
|
+
value.forEach((item, index) => {
|
|
277
|
+
walk(item, `${at}[${index}]`);
|
|
278
|
+
});
|
|
279
|
+
else if (value && typeof value === "object")
|
|
280
|
+
for (const [key, item] of Object.entries(value)) {
|
|
281
|
+
texts.push({ at: `${at} key`, text: key });
|
|
282
|
+
walk(item, `${at}.${key}`);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
for (const [at, value] of Object.entries(fields))
|
|
286
|
+
walk(value, at);
|
|
287
|
+
return texts;
|
|
288
|
+
}
|
|
289
|
+
function withTimeout(promise, ms, what) {
|
|
290
|
+
let timer;
|
|
291
|
+
return Promise.race([
|
|
292
|
+
promise,
|
|
293
|
+
new Promise((_, reject) => {
|
|
294
|
+
timer = setTimeout(() => reject(new Error(`${what} within ${ms} ms`)), ms);
|
|
295
|
+
}),
|
|
296
|
+
]).finally(() => clearTimeout(timer));
|
|
297
|
+
}
|
|
298
|
+
class GatewayError extends Error {
|
|
299
|
+
code;
|
|
300
|
+
constructor(code, message) {
|
|
301
|
+
super(message);
|
|
302
|
+
this.code = code;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// A member listed from its cached manifest: its process starts on the first call that needs
|
|
306
|
+
// it, so a session that never uses it never pays for it (T-338 P4). A start that fails is tried
|
|
307
|
+
// again on the next call.
|
|
308
|
+
class LazyClient {
|
|
309
|
+
make;
|
|
310
|
+
timeoutMs;
|
|
311
|
+
name;
|
|
312
|
+
onReady;
|
|
313
|
+
#inner;
|
|
314
|
+
#ready;
|
|
315
|
+
constructor(make, timeoutMs, name, onReady) {
|
|
316
|
+
this.make = make;
|
|
317
|
+
this.timeoutMs = timeoutMs;
|
|
318
|
+
this.name = name;
|
|
319
|
+
this.onReady = onReady;
|
|
320
|
+
}
|
|
321
|
+
#ensure() {
|
|
322
|
+
this.#ready ??= (async () => {
|
|
323
|
+
const inner = this.make();
|
|
324
|
+
this.#inner = inner;
|
|
325
|
+
try {
|
|
326
|
+
await withTimeout(inner.connect(), this.timeoutMs, `${this.name} did not start`);
|
|
327
|
+
}
|
|
328
|
+
catch (error) {
|
|
329
|
+
inner.close();
|
|
330
|
+
this.#inner = undefined;
|
|
331
|
+
this.#ready = undefined;
|
|
332
|
+
throw error;
|
|
333
|
+
}
|
|
334
|
+
void Promise.resolve().then(this.onReady);
|
|
335
|
+
return inner;
|
|
336
|
+
})();
|
|
337
|
+
return this.#ready;
|
|
338
|
+
}
|
|
339
|
+
async connect() {
|
|
340
|
+
await this.#ensure();
|
|
341
|
+
}
|
|
342
|
+
async listTools() {
|
|
343
|
+
return (await this.#ensure()).listTools();
|
|
344
|
+
}
|
|
345
|
+
async forwardToolCall(name, args) {
|
|
346
|
+
return (await this.#ensure()).forwardToolCall(name, args);
|
|
347
|
+
}
|
|
348
|
+
async listResources() {
|
|
349
|
+
const inner = await this.#ensure();
|
|
350
|
+
return inner.listResources ? inner.listResources() : [];
|
|
351
|
+
}
|
|
352
|
+
async readResource(uri) {
|
|
353
|
+
const inner = await this.#ensure();
|
|
354
|
+
if (!inner.readResource)
|
|
355
|
+
throw new GatewayError(-32602, `Unknown MCP resource: ${uri}`);
|
|
356
|
+
return inner.readResource(uri);
|
|
357
|
+
}
|
|
358
|
+
async listPrompts() {
|
|
359
|
+
const inner = await this.#ensure();
|
|
360
|
+
return inner.listPrompts ? inner.listPrompts() : [];
|
|
361
|
+
}
|
|
362
|
+
async getPrompt(name, args) {
|
|
363
|
+
const inner = await this.#ensure();
|
|
364
|
+
if (!inner.getPrompt)
|
|
365
|
+
throw new GatewayError(-32602, `Unknown MCP prompt: ${name}`);
|
|
366
|
+
return inner.getPrompt(name, args);
|
|
367
|
+
}
|
|
368
|
+
close() {
|
|
369
|
+
this.#inner?.close();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
// One MCP endpoint in front of many servers. Tools are exposed as `<prefix>__<tool>`; a tool
|
|
373
|
+
// whose texts carry injected instructions is hidden, since clients read them as prompt. The
|
|
374
|
+
// gateway adds no authority and removes none: a call reaches its server with the arguments the
|
|
375
|
+
// client sent, so the server's own checks (an approval fingerprint, say) decide as they would
|
|
376
|
+
// on a direct connection, and the client's permission prompt stays the human gate.
|
|
377
|
+
//
|
|
378
|
+
// With `profiles` (T-338 P4) only the members the active profile names are listed, plus one
|
|
379
|
+
// tool of the gateway's own, `gateway__use_profile`, that switches profile and tells the
|
|
380
|
+
// client its tool list changed.
|
|
381
|
+
export class McpGateway {
|
|
382
|
+
members;
|
|
383
|
+
options;
|
|
384
|
+
#routes = new Map();
|
|
385
|
+
#tools = [];
|
|
386
|
+
// Resource URIs are exposed unchanged (a server's own texts point at them), so a URI two
|
|
387
|
+
// members both list stays with the first. Prompts are namespaced like tools.
|
|
388
|
+
#resourceRoutes = new Map();
|
|
389
|
+
#resources = [];
|
|
390
|
+
#promptRoutes = new Map();
|
|
391
|
+
#prompts = [];
|
|
392
|
+
#clients = [];
|
|
393
|
+
// Every member whose listing is known (started, or read from its manifest), shown or not.
|
|
394
|
+
#loaded = new Map();
|
|
395
|
+
#started;
|
|
396
|
+
// Who is on the other end, from `initialize`; fixed once members start.
|
|
397
|
+
#client;
|
|
398
|
+
#profile;
|
|
399
|
+
// Sends a message to the client: notifications, and the gateway's own requests (`roots/list`).
|
|
400
|
+
#send;
|
|
401
|
+
#initialized = false;
|
|
402
|
+
#clientHasRoots = false;
|
|
403
|
+
#roots;
|
|
404
|
+
#nextRequest = 1;
|
|
405
|
+
#waiting = new Map();
|
|
406
|
+
constructor(members, options) {
|
|
407
|
+
this.members = members;
|
|
408
|
+
this.options = options;
|
|
409
|
+
this.#client = options.clientId;
|
|
410
|
+
this.#profile = options.profiles?.active;
|
|
411
|
+
}
|
|
412
|
+
// How the gateway reaches its client; `serveMcpGateway` attaches stdout.
|
|
413
|
+
attach(send) {
|
|
414
|
+
this.#send = send;
|
|
415
|
+
}
|
|
416
|
+
get profile() {
|
|
417
|
+
return this.#profile?.name;
|
|
418
|
+
}
|
|
419
|
+
#accessible(member) {
|
|
420
|
+
return canSee(this.options.access ?? { clients: {} }, this.#client, member);
|
|
421
|
+
}
|
|
422
|
+
#ungated(member) {
|
|
423
|
+
return Boolean(this.options.ungated?.includes(member.capability));
|
|
424
|
+
}
|
|
425
|
+
#inProfile(member) {
|
|
426
|
+
return (!this.#profile ||
|
|
427
|
+
this.#ungated(member) ||
|
|
428
|
+
profileAllows(this.#profile, member));
|
|
429
|
+
}
|
|
430
|
+
#visible(member) {
|
|
431
|
+
return this.#accessible(member) && this.#inProfile(member);
|
|
432
|
+
}
|
|
433
|
+
// Loads every member this client may see in its profile; one that fails or hangs is left
|
|
434
|
+
// out, never the whole gateway.
|
|
435
|
+
start() {
|
|
436
|
+
this.#started ??= this.#begin();
|
|
437
|
+
return this.#started;
|
|
438
|
+
}
|
|
439
|
+
async #begin() {
|
|
440
|
+
const accessible = this.members.filter((member) => {
|
|
441
|
+
if (this.#accessible(member))
|
|
442
|
+
return true;
|
|
443
|
+
this.#log(`${member.capability} hidden from ${this.#client ?? "an unnamed client"}${member.sensitive ? ` (${member.sensitive}; allow it in gateway-access.json)` : " (denied in gateway-access.json)"}`);
|
|
444
|
+
return false;
|
|
445
|
+
});
|
|
446
|
+
if (this.#profile) {
|
|
447
|
+
const left = accessible.filter((member) => !this.#inProfile(member));
|
|
448
|
+
this.#log(`profile ${this.#profile.name}${left.length ? `: leaves out ${left.map((member) => member.capability).join(", ")}` : ""}`);
|
|
449
|
+
}
|
|
450
|
+
await this.#load(accessible.filter((member) => this.#inProfile(member)));
|
|
451
|
+
this.#rebuild();
|
|
452
|
+
}
|
|
453
|
+
#clientFor(member) {
|
|
454
|
+
return this.options.client(member, {
|
|
455
|
+
roots: () => this.#currentRoots(),
|
|
456
|
+
onListChanged: () => void this.#refresh(member),
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
// Only members about to be shown are loaded: a hidden server never runs and its tools never
|
|
460
|
+
// reach the client's context. One with a cached manifest is listed from it and started on its
|
|
461
|
+
// first call; one without is started now and its manifest saved.
|
|
462
|
+
async #load(members) {
|
|
463
|
+
const timeoutMs = this.options.timeoutMs ?? 15_000;
|
|
464
|
+
const fresh = [];
|
|
465
|
+
for (const member of members) {
|
|
466
|
+
if (this.#loaded.has(member.capability))
|
|
467
|
+
continue;
|
|
468
|
+
const cached = await this.options.manifests
|
|
469
|
+
?.read(member)
|
|
470
|
+
.catch(() => undefined);
|
|
471
|
+
if (!cached) {
|
|
472
|
+
fresh.push(member);
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
const client = new LazyClient(() => this.#clientFor(member), timeoutMs, member.capability, () => this.#refresh(member));
|
|
476
|
+
this.#clients.push(client);
|
|
477
|
+
this.#loaded.set(member.capability, { member, client, listing: cached });
|
|
478
|
+
}
|
|
479
|
+
const clients = fresh.map((member) => this.#clientFor(member));
|
|
480
|
+
this.#clients.push(...clients);
|
|
481
|
+
const results = await Promise.allSettled(fresh.map(async (member, index) => {
|
|
482
|
+
await withTimeout(clients[index].connect(), timeoutMs, `${member.capability} did not start`);
|
|
483
|
+
return this.#list(member, clients[index], timeoutMs);
|
|
484
|
+
}));
|
|
485
|
+
await Promise.all(results.map(async (result, index) => {
|
|
486
|
+
const member = fresh[index];
|
|
487
|
+
if (result.status === "rejected") {
|
|
488
|
+
this.#log(`${member.capability} left out: ${errorMessage(result.reason)}`);
|
|
489
|
+
clients[index].close();
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
this.#loaded.set(member.capability, {
|
|
493
|
+
member,
|
|
494
|
+
client: clients[index],
|
|
495
|
+
listing: result.value,
|
|
496
|
+
});
|
|
497
|
+
await this.options.manifests
|
|
498
|
+
?.write(member, result.value)
|
|
499
|
+
.catch((error) => this.#log(`${member.capability} manifest not saved: ${errorMessage(error)}`));
|
|
500
|
+
}));
|
|
501
|
+
}
|
|
502
|
+
// A started member's tools, resources and prompts. One that cannot list resources or
|
|
503
|
+
// prompts keeps its tools.
|
|
504
|
+
async #list(member, client, timeoutMs) {
|
|
505
|
+
const tools = await withTimeout(client.listTools(), timeoutMs, `${member.capability} did not list its tools`);
|
|
506
|
+
const optional = async (what, list) => {
|
|
507
|
+
if (!list)
|
|
508
|
+
return [];
|
|
509
|
+
try {
|
|
510
|
+
return await withTimeout(list(), timeoutMs, `${member.capability} did not list its ${what}`);
|
|
511
|
+
}
|
|
512
|
+
catch (error) {
|
|
513
|
+
this.#log(`${member.capability} ${what} left out: ${errorMessage(error)}`);
|
|
514
|
+
return [];
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
return {
|
|
518
|
+
tools,
|
|
519
|
+
resources: await optional("resources", client.listResources?.bind(client)),
|
|
520
|
+
prompts: await optional("prompts", client.listPrompts?.bind(client)),
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
// Once a member listed from its manifest has started, its live listing replaces a stale one
|
|
524
|
+
// and the client is told the list changed.
|
|
525
|
+
async #refresh(member) {
|
|
526
|
+
const loaded = this.#loaded.get(member.capability);
|
|
527
|
+
if (!loaded)
|
|
528
|
+
return;
|
|
529
|
+
try {
|
|
530
|
+
const listing = await this.#list(member, loaded.client, this.options.timeoutMs ?? 15_000);
|
|
531
|
+
if (JSON.stringify(listing) === JSON.stringify(loaded.listing))
|
|
532
|
+
return;
|
|
533
|
+
loaded.listing = listing;
|
|
534
|
+
this.#log(`${member.capability} lists other tools now; manifest updated`);
|
|
535
|
+
await this.options.manifests?.write(member, listing);
|
|
536
|
+
this.#rebuild();
|
|
537
|
+
this.#announce();
|
|
538
|
+
}
|
|
539
|
+
catch (error) {
|
|
540
|
+
this.#log(`${member.capability} manifest not refreshed: ${errorMessage(error)}`);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
// The routes and lists the client sees, from every loaded member it may see now.
|
|
544
|
+
#rebuild() {
|
|
545
|
+
this.#routes.clear();
|
|
546
|
+
this.#tools.length = 0;
|
|
547
|
+
this.#resourceRoutes.clear();
|
|
548
|
+
this.#resources.length = 0;
|
|
549
|
+
this.#promptRoutes.clear();
|
|
550
|
+
this.#prompts.length = 0;
|
|
551
|
+
for (const member of this.members) {
|
|
552
|
+
const loaded = this.#loaded.get(member.capability);
|
|
553
|
+
if (!loaded || !this.#visible(member))
|
|
554
|
+
continue;
|
|
555
|
+
for (const tool of loaded.listing.tools)
|
|
556
|
+
this.#expose(member, loaded.client, tool);
|
|
557
|
+
for (const resource of loaded.listing.resources)
|
|
558
|
+
this.#exposeResource(member, loaded.client, resource);
|
|
559
|
+
for (const prompt of loaded.listing.prompts)
|
|
560
|
+
this.#exposePrompt(member, loaded.client, prompt);
|
|
561
|
+
}
|
|
562
|
+
const profileTool = this.#profileTool();
|
|
563
|
+
if (profileTool)
|
|
564
|
+
this.#tools.push(profileTool);
|
|
565
|
+
}
|
|
566
|
+
// The profiles that would show a member this client may see, with those members' prefixes.
|
|
567
|
+
#profileChoices() {
|
|
568
|
+
const gated = this.members.filter((member) => this.#accessible(member) && !this.#ungated(member));
|
|
569
|
+
return (this.options.profiles?.available ?? [])
|
|
570
|
+
.map((profile) => ({
|
|
571
|
+
profile,
|
|
572
|
+
members: gated
|
|
573
|
+
.filter((member) => profileAllows(profile, member))
|
|
574
|
+
.map((member) => member.prefix),
|
|
575
|
+
}))
|
|
576
|
+
.filter((choice) => choice.members.length);
|
|
577
|
+
}
|
|
578
|
+
// Listed only when some profile would show this client something the gateway holds.
|
|
579
|
+
#profileTool() {
|
|
580
|
+
if (!this.#profile)
|
|
581
|
+
return undefined;
|
|
582
|
+
const choices = this.#profileChoices();
|
|
583
|
+
if (!choices.length)
|
|
584
|
+
return undefined;
|
|
585
|
+
const available = this.options.profiles?.available ?? [];
|
|
586
|
+
const names = [
|
|
587
|
+
...new Set([
|
|
588
|
+
...choices.map((choice) => choice.profile.name),
|
|
589
|
+
this.#profile.name,
|
|
590
|
+
...(available.some((profile) => profile.name === "base")
|
|
591
|
+
? ["base"]
|
|
592
|
+
: []),
|
|
593
|
+
]),
|
|
594
|
+
];
|
|
595
|
+
return {
|
|
596
|
+
name: PROFILE_TOOL,
|
|
597
|
+
description: `Switches Ocean's gateway to another profile so its tools are listed. Active: ${this.#profile.name}. Profiles with tools here: ${choices.map((choice) => `${choice.profile.name} (${choice.members.join(", ")})`).join("; ")}. Call it when the start line or ocean_context names one of these profiles, or the task needs one of these tools.`,
|
|
598
|
+
inputSchema: {
|
|
599
|
+
type: "object",
|
|
600
|
+
properties: { name: { type: "string", enum: names } },
|
|
601
|
+
required: ["name"],
|
|
602
|
+
additionalProperties: false,
|
|
603
|
+
},
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
async #useProfile(args) {
|
|
607
|
+
const name = typeof args.name === "string" ? args.name : "";
|
|
608
|
+
const available = this.options.profiles?.available ?? [];
|
|
609
|
+
const profile = available.find((candidate) => candidate.name === name);
|
|
610
|
+
if (!profile)
|
|
611
|
+
return {
|
|
612
|
+
isError: true,
|
|
613
|
+
content: [
|
|
614
|
+
{
|
|
615
|
+
type: "text",
|
|
616
|
+
text: `No profile named "${name}". Installed: ${available.map((candidate) => candidate.name).join(", ")}.`,
|
|
617
|
+
},
|
|
618
|
+
],
|
|
619
|
+
};
|
|
620
|
+
this.#profile = profile;
|
|
621
|
+
await this.#load(this.members.filter((member) => this.#visible(member)));
|
|
622
|
+
this.#rebuild();
|
|
623
|
+
this.#announce();
|
|
624
|
+
const shown = this.members
|
|
625
|
+
.filter((member) => this.#loaded.has(member.capability) &&
|
|
626
|
+
this.#visible(member) &&
|
|
627
|
+
!this.#ungated(member))
|
|
628
|
+
.map((member) => member.prefix);
|
|
629
|
+
this.#log(`profile ${name} (asked by the client): ${shown.join(", ") || "no gateway members"}`);
|
|
630
|
+
return {
|
|
631
|
+
content: [
|
|
632
|
+
{
|
|
633
|
+
type: "text",
|
|
634
|
+
text: `Profile ${name} is active${shown.length ? `; tools from ${shown.join(", ")} are listed` : "; it adds no gateway tools"}. The client was told its tool list changed. If the new tools do not appear, this client does not reload them: start a new session in a project that uses ${name}.`,
|
|
635
|
+
},
|
|
636
|
+
],
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
// T-339 P2: the prompt hook's selection for this folder (primary and secondary profiles).
|
|
640
|
+
// The gateway follows it without `gateway__use_profile`, and only ever widens: members the
|
|
641
|
+
// session already sees stay listed. Returns whether the client was told its list changed.
|
|
642
|
+
async followSelection(selection) {
|
|
643
|
+
const available = this.options.profiles?.available;
|
|
644
|
+
if (!this.#profile || !available)
|
|
645
|
+
return false;
|
|
646
|
+
const names = [selection.primary, ...selection.secondary];
|
|
647
|
+
const mcp = [
|
|
648
|
+
...new Set([
|
|
649
|
+
...this.#profile.mcp,
|
|
650
|
+
...names.flatMap((name) => available.find((profile) => profile.name === name)?.mcp ?? []),
|
|
651
|
+
]),
|
|
652
|
+
];
|
|
653
|
+
const known = available.some((profile) => profile.name === selection.primary);
|
|
654
|
+
const name = known ? selection.primary : this.#profile.name;
|
|
655
|
+
if (name === this.#profile.name && mcp.length === this.#profile.mcp.length)
|
|
656
|
+
return false;
|
|
657
|
+
const before = this.#tools.map((tool) => tool.name).join();
|
|
658
|
+
this.#profile = { name, mcp };
|
|
659
|
+
await this.#load(this.members.filter((member) => this.#visible(member)));
|
|
660
|
+
this.#rebuild();
|
|
661
|
+
if (this.#tools.map((tool) => tool.name).join() === before)
|
|
662
|
+
return false;
|
|
663
|
+
this.#log(`profile ${name} (from the prompt hook's selection)`);
|
|
664
|
+
this.#announce();
|
|
665
|
+
return true;
|
|
666
|
+
}
|
|
667
|
+
// Tells the client the lists changed (MCP `notifications/*/list_changed`).
|
|
668
|
+
#announce() {
|
|
669
|
+
if (!this.#initialized || !this.#send)
|
|
670
|
+
return;
|
|
671
|
+
for (const list of ["tools", "resources", "prompts"])
|
|
672
|
+
this.#send({
|
|
673
|
+
jsonrpc: "2.0",
|
|
674
|
+
method: `notifications/${list}/list_changed`,
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
// Sends the client a request of the gateway's own and waits for its answer.
|
|
678
|
+
#ask(method, ms) {
|
|
679
|
+
const send = this.#send;
|
|
680
|
+
if (!send)
|
|
681
|
+
return Promise.reject(new Error("no client attached"));
|
|
682
|
+
const id = `ocean-gateway-${this.#nextRequest++}`;
|
|
683
|
+
return withTimeout(new Promise((resolve, reject) => {
|
|
684
|
+
this.#waiting.set(id, (message) => {
|
|
685
|
+
if (isRecord(message.error))
|
|
686
|
+
reject(new Error(String(message.error.message ?? "failed")));
|
|
687
|
+
else
|
|
688
|
+
resolve(message.result);
|
|
689
|
+
});
|
|
690
|
+
send({ jsonrpc: "2.0", id, method });
|
|
691
|
+
}), ms, `client did not answer ${method}`).finally(() => this.#waiting.delete(id));
|
|
692
|
+
}
|
|
693
|
+
// The roots a member is given: the client's own when it has them (MCP `roots/list`), else
|
|
694
|
+
// the folder the gateway was started in. Forwarded so a member's file tools (chrome-devtools'
|
|
695
|
+
// screenshots, say) work outside the OS temp directory.
|
|
696
|
+
#currentRoots() {
|
|
697
|
+
const fallback = this.options.roots ?? [];
|
|
698
|
+
if (!this.#clientHasRoots || !this.#send)
|
|
699
|
+
return Promise.resolve(fallback);
|
|
700
|
+
this.#roots ??= this.#ask("roots/list", 5_000)
|
|
701
|
+
.then((result) => {
|
|
702
|
+
const roots = isRecord(result) && Array.isArray(result.roots)
|
|
703
|
+
? result.roots.filter((root) => isRecord(root) && typeof root.uri === "string")
|
|
704
|
+
: [];
|
|
705
|
+
return roots.length ? roots : fallback;
|
|
706
|
+
})
|
|
707
|
+
.catch((error) => {
|
|
708
|
+
this.#log(`roots: ${errorMessage(error)}; using the fallback`);
|
|
709
|
+
this.#roots = undefined;
|
|
710
|
+
return fallback;
|
|
711
|
+
});
|
|
712
|
+
return this.#roots;
|
|
713
|
+
}
|
|
714
|
+
// True when a text the model would read carries injected instructions; then it is hidden.
|
|
715
|
+
#injected(member, kind, name, fields) {
|
|
716
|
+
const findings = modelTexts(fields).flatMap(({ at, text }) => scanText(at, text));
|
|
717
|
+
if (!findings.length)
|
|
718
|
+
return false;
|
|
719
|
+
this.#log(`${member.capability} ${kind} ${name} hidden: ${findings.map((finding) => `${finding.rule} in ${finding.source} ("${finding.excerpt}")`).join("; ")}`);
|
|
720
|
+
return true;
|
|
721
|
+
}
|
|
722
|
+
#exposeResource(member, client, resource) {
|
|
723
|
+
if (!isRecord(resource) || typeof resource.uri !== "string")
|
|
724
|
+
return;
|
|
725
|
+
if (this.#injected(member, "resource", resource.uri, resource))
|
|
726
|
+
return;
|
|
727
|
+
if (this.#resourceRoutes.has(resource.uri)) {
|
|
728
|
+
this.#log(`${member.capability} lists resource ${resource.uri} already routed; kept the first`);
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
this.#resourceRoutes.set(resource.uri, client);
|
|
732
|
+
this.#resources.push(resource);
|
|
733
|
+
}
|
|
734
|
+
#exposePrompt(member, client, prompt) {
|
|
735
|
+
if (!isRecord(prompt) || typeof prompt.name !== "string")
|
|
736
|
+
return;
|
|
737
|
+
if (this.#injected(member, "prompt", prompt.name, prompt))
|
|
738
|
+
return;
|
|
739
|
+
const name = `${member.prefix}__${prompt.name}`;
|
|
740
|
+
if (this.#promptRoutes.has(name)) {
|
|
741
|
+
this.#log(`${member.capability} lists prompt ${prompt.name} twice; kept the first`);
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
this.#promptRoutes.set(name, { client, prompt: prompt.name });
|
|
745
|
+
this.#prompts.push({ ...prompt, name });
|
|
746
|
+
}
|
|
747
|
+
#expose(member, client, tool) {
|
|
748
|
+
if (this.#injected(member, "tool", tool.name, {
|
|
749
|
+
name: tool.name,
|
|
750
|
+
description: tool.description,
|
|
751
|
+
inputSchema: tool.inputSchema,
|
|
752
|
+
}))
|
|
753
|
+
return;
|
|
754
|
+
const name = `${member.prefix}__${tool.name}`;
|
|
755
|
+
if (this.#routes.has(name)) {
|
|
756
|
+
this.#log(`${member.capability} lists ${tool.name} twice; kept the first`);
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
this.#routes.set(name, { client, tool: tool.name });
|
|
760
|
+
this.#tools.push({ ...tool, name });
|
|
761
|
+
}
|
|
762
|
+
// A tool a visible member serves but does not list (Ocean's ops tools outside ocean-ops,
|
|
763
|
+
// T-338 P5): the call goes to that member, whose own answer decides — never invented here.
|
|
764
|
+
#unlisted(name) {
|
|
765
|
+
const split = name.indexOf("__");
|
|
766
|
+
if (split <= 0)
|
|
767
|
+
return undefined;
|
|
768
|
+
const prefix = name.slice(0, split);
|
|
769
|
+
const member = this.members.find((candidate) => candidate.prefix === prefix && this.#visible(candidate));
|
|
770
|
+
const loaded = member && this.#loaded.get(member.capability);
|
|
771
|
+
const tool = name.slice(split + 2);
|
|
772
|
+
// A tool the member did list but the gateway left out (poisoned, injected) stays out.
|
|
773
|
+
if (!loaded || loaded.listing.tools.some((listed) => listed.name === tool))
|
|
774
|
+
return undefined;
|
|
775
|
+
return { client: loaded.client, tool };
|
|
776
|
+
}
|
|
777
|
+
async tools() {
|
|
778
|
+
await this.start();
|
|
779
|
+
return [...this.#tools];
|
|
780
|
+
}
|
|
781
|
+
async call(name, args) {
|
|
782
|
+
await this.start();
|
|
783
|
+
if (name === PROFILE_TOOL && this.#profile)
|
|
784
|
+
return this.#useProfile(args);
|
|
785
|
+
const route = this.#routes.get(name) ?? this.#unlisted(name);
|
|
786
|
+
if (!route)
|
|
787
|
+
throw new GatewayError(-32602, `Unknown MCP tool: ${name}`);
|
|
788
|
+
try {
|
|
789
|
+
return withHint(await route.client.forwardToolCall(route.tool, args));
|
|
790
|
+
}
|
|
791
|
+
catch (error) {
|
|
792
|
+
const message = errorMessage(error);
|
|
793
|
+
const hint = errorHint(message);
|
|
794
|
+
if (!hint)
|
|
795
|
+
throw error;
|
|
796
|
+
throw new Error(`${message}\n${hint}`);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
async resources() {
|
|
800
|
+
await this.start();
|
|
801
|
+
return [...this.#resources];
|
|
802
|
+
}
|
|
803
|
+
async readResource(uri) {
|
|
804
|
+
await this.start();
|
|
805
|
+
const client = this.#resourceRoutes.get(uri);
|
|
806
|
+
if (!client?.readResource)
|
|
807
|
+
throw new GatewayError(-32602, `Unknown MCP resource: ${uri}`);
|
|
808
|
+
return client.readResource(uri);
|
|
809
|
+
}
|
|
810
|
+
async prompts() {
|
|
811
|
+
await this.start();
|
|
812
|
+
return [...this.#prompts];
|
|
813
|
+
}
|
|
814
|
+
async getPrompt(name, args) {
|
|
815
|
+
await this.start();
|
|
816
|
+
const route = this.#promptRoutes.get(name);
|
|
817
|
+
if (!route?.client.getPrompt)
|
|
818
|
+
throw new GatewayError(-32602, `Unknown MCP prompt: ${name}`);
|
|
819
|
+
return route.client.getPrompt(route.prompt, args);
|
|
820
|
+
}
|
|
821
|
+
async handle(request) {
|
|
822
|
+
// MCP 2025-06-18 has no batches; anything but one request object is invalid.
|
|
823
|
+
if (!isRecord(request))
|
|
824
|
+
return {
|
|
825
|
+
jsonrpc: "2.0",
|
|
826
|
+
id: null,
|
|
827
|
+
error: { code: -32600, message: "Invalid Request" },
|
|
828
|
+
};
|
|
829
|
+
// The client's answer to a request of the gateway's own.
|
|
830
|
+
if (request.method === undefined &&
|
|
831
|
+
typeof request.id === "string" &&
|
|
832
|
+
("result" in request || "error" in request)) {
|
|
833
|
+
this.#waiting.get(request.id)?.(request);
|
|
834
|
+
return null;
|
|
835
|
+
}
|
|
836
|
+
// Notifications get no answer.
|
|
837
|
+
if (request.id === undefined) {
|
|
838
|
+
if (request.method === "notifications/initialized")
|
|
839
|
+
this.#initialized = true;
|
|
840
|
+
if (request.method === "notifications/roots/list_changed")
|
|
841
|
+
this.#roots = undefined;
|
|
842
|
+
return null;
|
|
843
|
+
}
|
|
844
|
+
const id = typeof request.id === "string" || typeof request.id === "number"
|
|
845
|
+
? request.id
|
|
846
|
+
: null;
|
|
847
|
+
return this.#respond(request, id);
|
|
848
|
+
}
|
|
849
|
+
async #respond(request, id) {
|
|
850
|
+
try {
|
|
851
|
+
return { jsonrpc: "2.0", id, result: await this.#answer(request) };
|
|
852
|
+
}
|
|
853
|
+
catch (error) {
|
|
854
|
+
return {
|
|
855
|
+
jsonrpc: "2.0",
|
|
856
|
+
id,
|
|
857
|
+
error: {
|
|
858
|
+
code: error instanceof GatewayError ? error.code : -32000,
|
|
859
|
+
message: errorMessage(error),
|
|
860
|
+
},
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
async #answer(request) {
|
|
865
|
+
switch (request.method) {
|
|
866
|
+
case "initialize": {
|
|
867
|
+
const params = isRecord(request.params) ? request.params : {};
|
|
868
|
+
const info = params.clientInfo;
|
|
869
|
+
const named = isRecord(info) && typeof info.name === "string";
|
|
870
|
+
this.#clientHasRoots =
|
|
871
|
+
isRecord(params.capabilities) && isRecord(params.capabilities.roots);
|
|
872
|
+
if (this.options.clientId)
|
|
873
|
+
this.#log(`client: ${this.options.clientId}${named ? ` (says ${String(info.name)})` : ""}`);
|
|
874
|
+
else if (!this.#started && named) {
|
|
875
|
+
this.#client = String(info.name);
|
|
876
|
+
this.#log(`client: ${this.#client}`);
|
|
877
|
+
}
|
|
878
|
+
// Members start while the client finishes its handshake.
|
|
879
|
+
void this.start();
|
|
880
|
+
// The lists change when the profile switches or a cached listing turns out stale.
|
|
881
|
+
const changes = this.options.profiles || this.options.manifests
|
|
882
|
+
? { listChanged: true }
|
|
883
|
+
: {};
|
|
884
|
+
return {
|
|
885
|
+
protocolVersion: "2025-06-18",
|
|
886
|
+
// Members start after this answer, so resources and prompts are always offered; a
|
|
887
|
+
// gateway whose members have none lists them empty, which MCP allows.
|
|
888
|
+
capabilities: {
|
|
889
|
+
tools: { ...changes },
|
|
890
|
+
resources: { ...changes },
|
|
891
|
+
prompts: { ...changes },
|
|
892
|
+
},
|
|
893
|
+
serverInfo: {
|
|
894
|
+
name: this.options.serverName ?? "ocean-gateway",
|
|
895
|
+
version: this.options.version ?? "0.0.0",
|
|
896
|
+
},
|
|
897
|
+
...(this.#instructions()
|
|
898
|
+
? { instructions: this.options.instructions }
|
|
899
|
+
: {}),
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
case "ping":
|
|
903
|
+
return {};
|
|
904
|
+
case "tools/list":
|
|
905
|
+
return { tools: await this.tools() };
|
|
906
|
+
case "tools/call": {
|
|
907
|
+
const params = isRecord(request.params) ? request.params : {};
|
|
908
|
+
const args = params.arguments ?? {};
|
|
909
|
+
if (typeof params.name !== "string" || !isRecord(args))
|
|
910
|
+
throw new GatewayError(-32602, "tools/call needs a tool name and an arguments object");
|
|
911
|
+
return this.call(params.name, args);
|
|
912
|
+
}
|
|
913
|
+
case "resources/list":
|
|
914
|
+
return { resources: await this.resources() };
|
|
915
|
+
case "resources/read": {
|
|
916
|
+
const params = isRecord(request.params) ? request.params : {};
|
|
917
|
+
if (typeof params.uri !== "string")
|
|
918
|
+
throw new GatewayError(-32602, "resources/read needs a uri");
|
|
919
|
+
return this.readResource(params.uri);
|
|
920
|
+
}
|
|
921
|
+
case "prompts/list":
|
|
922
|
+
return { prompts: await this.prompts() };
|
|
923
|
+
case "prompts/get": {
|
|
924
|
+
const params = isRecord(request.params) ? request.params : {};
|
|
925
|
+
const args = params.arguments ?? {};
|
|
926
|
+
if (typeof params.name !== "string" || !isRecord(args))
|
|
927
|
+
throw new GatewayError(-32602, "prompts/get needs a prompt name and an arguments object");
|
|
928
|
+
return this.getPrompt(params.name, args);
|
|
929
|
+
}
|
|
930
|
+
default:
|
|
931
|
+
throw new GatewayError(-32601, `Unsupported MCP method: ${request.method ?? ""}`);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
// Instructions describe Ocean's own server: sent only when this client sees it, in its
|
|
935
|
+
// access rules and its profile.
|
|
936
|
+
#instructions() {
|
|
937
|
+
const { instructions, instructionsMember } = this.options;
|
|
938
|
+
if (!instructions)
|
|
939
|
+
return false;
|
|
940
|
+
if (!instructionsMember)
|
|
941
|
+
return true;
|
|
942
|
+
return this.members.some((member) => member.capability === instructionsMember && this.#visible(member));
|
|
943
|
+
}
|
|
944
|
+
close() {
|
|
945
|
+
for (const client of this.#clients)
|
|
946
|
+
client.close();
|
|
947
|
+
}
|
|
948
|
+
#log(line) {
|
|
949
|
+
this.options.log?.(line);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
// Newline-delimited JSON-RPC over stdio. Requests run concurrently, so a slow tool on one server
|
|
953
|
+
// does not hold up another; members are stopped when the client closes the stream.
|
|
954
|
+
export async function serveMcpGateway(gateway, input = process.stdin, output = process.stdout) {
|
|
955
|
+
let buffer = "";
|
|
956
|
+
const write = (response) => {
|
|
957
|
+
if (response)
|
|
958
|
+
output.write(`${JSON.stringify(response)}\n`);
|
|
959
|
+
};
|
|
960
|
+
gateway.attach(write);
|
|
961
|
+
input.setEncoding("utf8");
|
|
962
|
+
input.on("data", (chunk) => {
|
|
963
|
+
buffer += chunk;
|
|
964
|
+
for (let newline = buffer.indexOf("\n"); newline >= 0; newline = buffer.indexOf("\n")) {
|
|
965
|
+
const line = buffer.slice(0, newline).trim();
|
|
966
|
+
buffer = buffer.slice(newline + 1);
|
|
967
|
+
if (!line)
|
|
968
|
+
continue;
|
|
969
|
+
let request;
|
|
970
|
+
try {
|
|
971
|
+
request = JSON.parse(line);
|
|
972
|
+
}
|
|
973
|
+
catch {
|
|
974
|
+
write({
|
|
975
|
+
jsonrpc: "2.0",
|
|
976
|
+
id: null,
|
|
977
|
+
error: { code: -32700, message: "Parse error" },
|
|
978
|
+
});
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
void gateway.handle(request).then(write);
|
|
982
|
+
}
|
|
983
|
+
});
|
|
984
|
+
await new Promise((resolve) => input.once("end", resolve));
|
|
985
|
+
gateway.close();
|
|
986
|
+
}
|
|
987
|
+
//# sourceMappingURL=mcp-gateway.js.map
|