@amoree-code/ocean 1.4.0 → 1.5.1
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/dist/application/clients/clients.js +122 -6
- package/dist/infrastructure/mcp/remote-mcp-client.js +81 -0
- package/dist/interfaces/cli/clients-command.js +39 -2
- package/dist/interfaces/cli/command-registry.js +7 -3
- package/dist/interfaces/cli/gateway-findings.js +32 -0
- package/dist/interfaces/cli/help-command.js +3 -3
- package/dist/interfaces/cli/market-command.js +142 -5
- package/dist/interfaces/cli/mcp-gateway-command.js +36 -11
- package/dist/interfaces/cli/mcp-status-command.js +135 -0
- package/dist/interfaces/cli/onboard-command.js +6 -2
- package/node_modules/@ocean/brain/package.json +1 -1
- package/node_modules/@ocean/context/package.json +1 -1
- package/node_modules/@ocean/core/package.json +1 -1
- package/node_modules/@ocean/loop/package.json +1 -1
- package/node_modules/@ocean/market/dist/adapters/mcp-adapter.js +13 -2
- package/node_modules/@ocean/market/dist/install/default-marketplace.js +1 -0
- package/node_modules/@ocean/market/dist/install/own-server.js +110 -0
- package/node_modules/@ocean/market/dist/mcp/mcp-gateway.js +240 -24
- package/node_modules/@ocean/market/dist/mcp/mcp-launch.js +167 -23
- package/node_modules/@ocean/market/dist/mcp/mcp-runner.js +6 -1
- package/node_modules/@ocean/market/dist/registry/official-registry.js +76 -0
- package/node_modules/@ocean/market/dist/security/secrets.js +66 -0
- package/node_modules/@ocean/market/package.json +1 -1
- package/node_modules/@ocean/services/dist/infrastructure/mcp/mcp-client.js +27 -2
- package/node_modules/@ocean/services/package.json +1 -1
- package/ocean-marketplace.json +28 -28
- package/package.json +7 -7
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdir, readdir, readFile } from "node:fs/promises";
|
|
1
|
+
import { mkdir, readdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { marketplaceIndexSchema } from "@ocean/core/domain/capabilities/capability-manifest.js";
|
|
4
4
|
import { engineRoot } from "@ocean/core/shared/paths.js";
|
|
@@ -16,6 +16,70 @@ function namespaceFor(capability, taken) {
|
|
|
16
16
|
taken.add(prefix);
|
|
17
17
|
return prefix;
|
|
18
18
|
}
|
|
19
|
+
const BROWSER = /(^|[^a-z])(browser|chrome|chromium|devtools|playwright|puppeteer|selenium|webdriver)([^a-z]|$)/i;
|
|
20
|
+
// Why an installed server is hidden from a client that was not allowed it by name, or nothing.
|
|
21
|
+
// Declared file writes count; so does driving a browser, recognised by name, since a browser
|
|
22
|
+
// server reads and acts inside every signed-in session whatever it declares.
|
|
23
|
+
export function sensitivityOf(installed) {
|
|
24
|
+
if (installed.permissions.filesystem.write.length)
|
|
25
|
+
return "writes files";
|
|
26
|
+
const names = [
|
|
27
|
+
installed.capability,
|
|
28
|
+
...(installed.server.packages ?? []).map((item) => String(item.identifier)),
|
|
29
|
+
];
|
|
30
|
+
if (names.some((name) => BROWSER.test(name)))
|
|
31
|
+
return "controls a browser";
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
export function canSee(access, client, member) {
|
|
35
|
+
const rule = client === undefined ? undefined : access.clients[client];
|
|
36
|
+
if (rule?.deny?.includes(member.capability))
|
|
37
|
+
return false;
|
|
38
|
+
if (!member.sensitive)
|
|
39
|
+
return true;
|
|
40
|
+
return Boolean(rule?.allow?.includes(member.capability) || rule?.allow?.includes("*"));
|
|
41
|
+
}
|
|
42
|
+
// `ocean clients allow|deny|unset <client> <capability>`: one rule changed, the rest kept.
|
|
43
|
+
export async function updateGatewayAccess(file, client, capability, change) {
|
|
44
|
+
const access = await readGatewayAccess(file);
|
|
45
|
+
const rule = access.clients[client] ?? {};
|
|
46
|
+
const without = (list) => (list ?? []).filter((item) => item !== capability);
|
|
47
|
+
const next = { allow: without(rule.allow), deny: without(rule.deny) };
|
|
48
|
+
if (change === "allow")
|
|
49
|
+
next.allow.push(capability);
|
|
50
|
+
if (change === "deny")
|
|
51
|
+
next.deny.push(capability);
|
|
52
|
+
if (next.allow.length || next.deny.length)
|
|
53
|
+
access.clients[client] = {
|
|
54
|
+
...(next.allow.length ? { allow: next.allow.sort() } : {}),
|
|
55
|
+
...(next.deny.length ? { deny: next.deny.sort() } : {}),
|
|
56
|
+
};
|
|
57
|
+
else
|
|
58
|
+
delete access.clients[client];
|
|
59
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
60
|
+
const temp = `${file}.${process.pid}.tmp`;
|
|
61
|
+
await writeFile(temp, `${JSON.stringify(access, null, 2)}\n`);
|
|
62
|
+
await rename(temp, file);
|
|
63
|
+
return access;
|
|
64
|
+
}
|
|
65
|
+
export async function readGatewayAccess(file) {
|
|
66
|
+
let raw;
|
|
67
|
+
try {
|
|
68
|
+
raw = JSON.parse(await readFile(file, "utf8"));
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error.code === "ENOENT")
|
|
72
|
+
return { clients: {} };
|
|
73
|
+
throw new Error(`Cannot read ${file}: ${errorMessage(error)}`);
|
|
74
|
+
}
|
|
75
|
+
const clients = isRecord(raw) ? raw.clients : undefined;
|
|
76
|
+
const list = (value) => value === undefined ||
|
|
77
|
+
(Array.isArray(value) && value.every((item) => typeof item === "string"));
|
|
78
|
+
if (!isRecord(clients) ||
|
|
79
|
+
!Object.values(clients).every((rule) => isRecord(rule) && list(rule.allow) && list(rule.deny)))
|
|
80
|
+
throw new Error(`${file}: expected {"clients": {"<client name>": {"allow": [...], "deny": [...]}}}`);
|
|
81
|
+
return raw;
|
|
82
|
+
}
|
|
19
83
|
// The servers the gateway routes: the engine's first-party ones, then every MCP server installed
|
|
20
84
|
// from the marketplace, each planned by the same launcher as `ocean market exec` — declared
|
|
21
85
|
// permissions, declared environment only. A third-party server joins only by
|
|
@@ -25,6 +89,7 @@ export async function gatewayMembers(options) {
|
|
|
25
89
|
const skipped = [];
|
|
26
90
|
const taken = new Set();
|
|
27
91
|
const notFirstParty = [];
|
|
92
|
+
const firstParty = new Set();
|
|
28
93
|
const index = marketplaceIndexSchema.parse(JSON.parse(await readFile(options.indexFile ?? engineIndexFile(), "utf8")));
|
|
29
94
|
for (const manifest of index.capabilities) {
|
|
30
95
|
if (manifest.kind !== "mcp" || manifest.name === GATEWAY_CAPABILITY)
|
|
@@ -34,6 +99,9 @@ export async function gatewayMembers(options) {
|
|
|
34
99
|
notFirstParty.push(manifest.name);
|
|
35
100
|
continue;
|
|
36
101
|
}
|
|
102
|
+
firstParty.add(manifest.name);
|
|
103
|
+
if (options.firstParty === false)
|
|
104
|
+
continue;
|
|
37
105
|
members.push({
|
|
38
106
|
capability: manifest.name,
|
|
39
107
|
prefix: namespaceFor(manifest.name, taken),
|
|
@@ -68,6 +136,7 @@ export async function gatewayMembers(options) {
|
|
|
68
136
|
const name = installed.capability;
|
|
69
137
|
// An installed copy of an engine server runs the same code as the member already routed.
|
|
70
138
|
if (name === GATEWAY_CAPABILITY ||
|
|
139
|
+
firstParty.has(name) ||
|
|
71
140
|
members.some((member) => member.capability === name))
|
|
72
141
|
continue;
|
|
73
142
|
const stateDir = path.join(options.stateRoot, path.basename(file, ".json"));
|
|
@@ -75,18 +144,29 @@ export async function gatewayMembers(options) {
|
|
|
75
144
|
const launch = planMcpLaunch(installed, {
|
|
76
145
|
stateDir,
|
|
77
146
|
env: options.env,
|
|
147
|
+
secret: (secret) => options.secrets?.get(name, secret),
|
|
78
148
|
});
|
|
149
|
+
const sensitive = sensitivityOf(installed);
|
|
150
|
+
const base = {
|
|
151
|
+
capability: name,
|
|
152
|
+
prefix: namespaceFor(name, taken),
|
|
153
|
+
...(sensitive ? { sensitive } : {}),
|
|
154
|
+
};
|
|
79
155
|
if (launch.transport === "remote") {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
156
|
+
members.push({
|
|
157
|
+
...base,
|
|
158
|
+
transport: "remote",
|
|
159
|
+
type: launch.type,
|
|
160
|
+
url: launch.url,
|
|
161
|
+
...(Object.keys(launch.headers).length
|
|
162
|
+
? { headers: launch.headers }
|
|
163
|
+
: {}),
|
|
83
164
|
});
|
|
84
165
|
continue;
|
|
85
166
|
}
|
|
86
167
|
await mkdir(stateDir, { recursive: true });
|
|
87
168
|
members.push({
|
|
88
|
-
|
|
89
|
-
prefix: namespaceFor(name, taken),
|
|
169
|
+
...base,
|
|
90
170
|
command: launch.command,
|
|
91
171
|
args: launch.args,
|
|
92
172
|
cwd: launch.cwd,
|
|
@@ -106,9 +186,9 @@ export async function gatewayMembers(options) {
|
|
|
106
186
|
});
|
|
107
187
|
return { members, skipped };
|
|
108
188
|
}
|
|
109
|
-
// Every text a model reads for a tool:
|
|
110
|
-
//
|
|
111
|
-
function
|
|
189
|
+
// Every text a model reads for a tool, resource or prompt: each named field, and each key and
|
|
190
|
+
// string nested inside it (a tool's schema property names, titles, enums and defaults included).
|
|
191
|
+
function modelTexts(fields) {
|
|
112
192
|
const texts = [];
|
|
113
193
|
const walk = (value, at) => {
|
|
114
194
|
if (typeof value === "string")
|
|
@@ -123,9 +203,8 @@ function toolTexts(tool) {
|
|
|
123
203
|
walk(item, `${at}.${key}`);
|
|
124
204
|
}
|
|
125
205
|
};
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
walk(tool.inputSchema, "inputSchema");
|
|
206
|
+
for (const [at, value] of Object.entries(fields))
|
|
207
|
+
walk(value, at);
|
|
129
208
|
return texts;
|
|
130
209
|
}
|
|
131
210
|
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -155,42 +234,117 @@ export class McpGateway {
|
|
|
155
234
|
options;
|
|
156
235
|
#routes = new Map();
|
|
157
236
|
#tools = [];
|
|
237
|
+
// Resource URIs are exposed unchanged (a server's own texts point at them), so a URI two
|
|
238
|
+
// members both list stays with the first. Prompts are namespaced like tools.
|
|
239
|
+
#resourceRoutes = new Map();
|
|
240
|
+
#resources = [];
|
|
241
|
+
#promptRoutes = new Map();
|
|
242
|
+
#prompts = [];
|
|
158
243
|
#clients = [];
|
|
159
244
|
#started;
|
|
245
|
+
// Who is on the other end, from `initialize`; fixed once members start.
|
|
246
|
+
#client;
|
|
160
247
|
constructor(members, options) {
|
|
161
248
|
this.members = members;
|
|
162
249
|
this.options = options;
|
|
250
|
+
this.#client = options.clientId;
|
|
163
251
|
}
|
|
164
252
|
// Starts every member at once; one that fails or hangs is left out, never the whole gateway.
|
|
165
253
|
start() {
|
|
166
254
|
this.#started ??= this.#connectAll();
|
|
167
255
|
return this.#started;
|
|
168
256
|
}
|
|
257
|
+
// Only the members this client may see are started at all: a hidden server never runs and its
|
|
258
|
+
// tools never reach the client's context.
|
|
169
259
|
async #connectAll() {
|
|
170
260
|
const timeoutMs = this.options.timeoutMs ?? 15_000;
|
|
171
|
-
const
|
|
261
|
+
const access = this.options.access ?? { clients: {} };
|
|
262
|
+
const members = this.members.filter((member) => {
|
|
263
|
+
if (canSee(access, this.#client, member))
|
|
264
|
+
return true;
|
|
265
|
+
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)"}`);
|
|
266
|
+
return false;
|
|
267
|
+
});
|
|
268
|
+
const clients = members.map((member) => this.options.client(member));
|
|
172
269
|
this.#clients.push(...clients);
|
|
173
|
-
const results = await Promise.allSettled(
|
|
270
|
+
const results = await Promise.allSettled(members.map(async (member, index) => {
|
|
174
271
|
await withTimeout(clients[index].connect(), timeoutMs, `${member.capability} did not start`);
|
|
175
|
-
|
|
272
|
+
const tools = await withTimeout(clients[index].listTools(), timeoutMs, `${member.capability} did not list its tools`);
|
|
273
|
+
// A member that cannot list resources or prompts keeps its tools.
|
|
274
|
+
const optional = async (what, list) => {
|
|
275
|
+
if (!list)
|
|
276
|
+
return [];
|
|
277
|
+
try {
|
|
278
|
+
return await withTimeout(list(), timeoutMs, `${member.capability} did not list its ${what}`);
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
this.#log(`${member.capability} ${what} left out: ${errorMessage(error)}`);
|
|
282
|
+
return [];
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
const client = clients[index];
|
|
286
|
+
const listing = {
|
|
287
|
+
tools,
|
|
288
|
+
resources: await optional("resources", client.listResources?.bind(client)),
|
|
289
|
+
prompts: await optional("prompts", client.listPrompts?.bind(client)),
|
|
290
|
+
};
|
|
291
|
+
return listing;
|
|
176
292
|
}));
|
|
177
293
|
results.forEach((result, index) => {
|
|
178
|
-
const member =
|
|
294
|
+
const member = members[index];
|
|
179
295
|
if (result.status === "rejected") {
|
|
180
296
|
this.#log(`${member.capability} left out: ${errorMessage(result.reason)}`);
|
|
181
297
|
clients[index].close();
|
|
182
298
|
return;
|
|
183
299
|
}
|
|
184
|
-
for (const tool of result.value)
|
|
300
|
+
for (const tool of result.value.tools)
|
|
185
301
|
this.#expose(member, clients[index], tool);
|
|
302
|
+
for (const resource of result.value.resources)
|
|
303
|
+
this.#exposeResource(member, clients[index], resource);
|
|
304
|
+
for (const prompt of result.value.prompts)
|
|
305
|
+
this.#exposePrompt(member, clients[index], prompt);
|
|
186
306
|
});
|
|
187
307
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
308
|
+
// True when a text the model would read carries injected instructions; then it is hidden.
|
|
309
|
+
#injected(member, kind, name, fields) {
|
|
310
|
+
const findings = modelTexts(fields).flatMap(({ at, text }) => scanText(at, text));
|
|
311
|
+
if (!findings.length)
|
|
312
|
+
return false;
|
|
313
|
+
this.#log(`${member.capability} ${kind} ${name} hidden: ${findings.map((finding) => `${finding.rule} in ${finding.source} ("${finding.excerpt}")`).join("; ")}`);
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
#exposeResource(member, client, resource) {
|
|
317
|
+
if (!isRecord(resource) || typeof resource.uri !== "string")
|
|
318
|
+
return;
|
|
319
|
+
if (this.#injected(member, "resource", resource.uri, resource))
|
|
320
|
+
return;
|
|
321
|
+
if (this.#resourceRoutes.has(resource.uri)) {
|
|
322
|
+
this.#log(`${member.capability} lists resource ${resource.uri} already routed; kept the first`);
|
|
192
323
|
return;
|
|
193
324
|
}
|
|
325
|
+
this.#resourceRoutes.set(resource.uri, client);
|
|
326
|
+
this.#resources.push(resource);
|
|
327
|
+
}
|
|
328
|
+
#exposePrompt(member, client, prompt) {
|
|
329
|
+
if (!isRecord(prompt) || typeof prompt.name !== "string")
|
|
330
|
+
return;
|
|
331
|
+
if (this.#injected(member, "prompt", prompt.name, prompt))
|
|
332
|
+
return;
|
|
333
|
+
const name = `${member.prefix}__${prompt.name}`;
|
|
334
|
+
if (this.#promptRoutes.has(name)) {
|
|
335
|
+
this.#log(`${member.capability} lists prompt ${prompt.name} twice; kept the first`);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
this.#promptRoutes.set(name, { client, prompt: prompt.name });
|
|
339
|
+
this.#prompts.push({ ...prompt, name });
|
|
340
|
+
}
|
|
341
|
+
#expose(member, client, tool) {
|
|
342
|
+
if (this.#injected(member, "tool", tool.name, {
|
|
343
|
+
name: tool.name,
|
|
344
|
+
description: tool.description,
|
|
345
|
+
inputSchema: tool.inputSchema,
|
|
346
|
+
}))
|
|
347
|
+
return;
|
|
194
348
|
const name = `${member.prefix}__${tool.name}`;
|
|
195
349
|
if (this.#routes.has(name)) {
|
|
196
350
|
this.#log(`${member.capability} lists ${tool.name} twice; kept the first`);
|
|
@@ -210,6 +364,28 @@ export class McpGateway {
|
|
|
210
364
|
throw new GatewayError(-32602, `Unknown MCP tool: ${name}`);
|
|
211
365
|
return route.client.forwardToolCall(route.tool, args);
|
|
212
366
|
}
|
|
367
|
+
async resources() {
|
|
368
|
+
await this.start();
|
|
369
|
+
return [...this.#resources];
|
|
370
|
+
}
|
|
371
|
+
async readResource(uri) {
|
|
372
|
+
await this.start();
|
|
373
|
+
const client = this.#resourceRoutes.get(uri);
|
|
374
|
+
if (!client?.readResource)
|
|
375
|
+
throw new GatewayError(-32602, `Unknown MCP resource: ${uri}`);
|
|
376
|
+
return client.readResource(uri);
|
|
377
|
+
}
|
|
378
|
+
async prompts() {
|
|
379
|
+
await this.start();
|
|
380
|
+
return [...this.#prompts];
|
|
381
|
+
}
|
|
382
|
+
async getPrompt(name, args) {
|
|
383
|
+
await this.start();
|
|
384
|
+
const route = this.#promptRoutes.get(name);
|
|
385
|
+
if (!route?.client.getPrompt)
|
|
386
|
+
throw new GatewayError(-32602, `Unknown MCP prompt: ${name}`);
|
|
387
|
+
return route.client.getPrompt(route.prompt, args);
|
|
388
|
+
}
|
|
213
389
|
async handle(request) {
|
|
214
390
|
// MCP 2025-06-18 has no batches; anything but one request object is invalid.
|
|
215
391
|
if (!isRecord(request))
|
|
@@ -243,20 +419,33 @@ export class McpGateway {
|
|
|
243
419
|
}
|
|
244
420
|
async #answer(request) {
|
|
245
421
|
switch (request.method) {
|
|
246
|
-
case "initialize":
|
|
422
|
+
case "initialize": {
|
|
423
|
+
const info = isRecord(request.params)
|
|
424
|
+
? request.params.clientInfo
|
|
425
|
+
: undefined;
|
|
426
|
+
const named = isRecord(info) && typeof info.name === "string";
|
|
427
|
+
if (this.options.clientId)
|
|
428
|
+
this.#log(`client: ${this.options.clientId}${named ? ` (says ${String(info.name)})` : ""}`);
|
|
429
|
+
else if (!this.#started && named) {
|
|
430
|
+
this.#client = String(info.name);
|
|
431
|
+
this.#log(`client: ${this.#client}`);
|
|
432
|
+
}
|
|
247
433
|
// Members start while the client finishes its handshake.
|
|
248
434
|
void this.start();
|
|
249
435
|
return {
|
|
250
436
|
protocolVersion: "2025-06-18",
|
|
251
|
-
|
|
437
|
+
// Members start after this answer, so resources and prompts are always offered; a
|
|
438
|
+
// gateway whose members have none lists them empty, which MCP allows.
|
|
439
|
+
capabilities: { tools: {}, resources: {}, prompts: {} },
|
|
252
440
|
serverInfo: {
|
|
253
441
|
name: this.options.serverName ?? "ocean-gateway",
|
|
254
442
|
version: this.options.version ?? "0.0.0",
|
|
255
443
|
},
|
|
256
|
-
...(this
|
|
444
|
+
...(this.#instructions()
|
|
257
445
|
? { instructions: this.options.instructions }
|
|
258
446
|
: {}),
|
|
259
447
|
};
|
|
448
|
+
}
|
|
260
449
|
case "ping":
|
|
261
450
|
return {};
|
|
262
451
|
case "tools/list":
|
|
@@ -268,10 +457,37 @@ export class McpGateway {
|
|
|
268
457
|
throw new GatewayError(-32602, "tools/call needs a tool name and an arguments object");
|
|
269
458
|
return this.call(params.name, args);
|
|
270
459
|
}
|
|
460
|
+
case "resources/list":
|
|
461
|
+
return { resources: await this.resources() };
|
|
462
|
+
case "resources/read": {
|
|
463
|
+
const params = isRecord(request.params) ? request.params : {};
|
|
464
|
+
if (typeof params.uri !== "string")
|
|
465
|
+
throw new GatewayError(-32602, "resources/read needs a uri");
|
|
466
|
+
return this.readResource(params.uri);
|
|
467
|
+
}
|
|
468
|
+
case "prompts/list":
|
|
469
|
+
return { prompts: await this.prompts() };
|
|
470
|
+
case "prompts/get": {
|
|
471
|
+
const params = isRecord(request.params) ? request.params : {};
|
|
472
|
+
const args = params.arguments ?? {};
|
|
473
|
+
if (typeof params.name !== "string" || !isRecord(args))
|
|
474
|
+
throw new GatewayError(-32602, "prompts/get needs a prompt name and an arguments object");
|
|
475
|
+
return this.getPrompt(params.name, args);
|
|
476
|
+
}
|
|
271
477
|
default:
|
|
272
478
|
throw new GatewayError(-32601, `Unsupported MCP method: ${request.method ?? ""}`);
|
|
273
479
|
}
|
|
274
480
|
}
|
|
481
|
+
#instructions() {
|
|
482
|
+
const { instructions, instructionsMember } = this.options;
|
|
483
|
+
if (!instructions)
|
|
484
|
+
return false;
|
|
485
|
+
if (!instructionsMember)
|
|
486
|
+
return true;
|
|
487
|
+
const access = this.options.access ?? { clients: {} };
|
|
488
|
+
return this.members.some((member) => member.capability === instructionsMember &&
|
|
489
|
+
canSee(access, this.#client, member));
|
|
490
|
+
}
|
|
275
491
|
close() {
|
|
276
492
|
for (const client of this.#clients)
|
|
277
493
|
client.close();
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import os from "node:os";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import { remoteUrlAllowed, } from "../adapters/mcp-adapter.js";
|
|
3
4
|
import { PermissionPolicy } from "../security/permissions.js";
|
|
5
|
+
const remoteTransports = ["streamable-http", "sse"];
|
|
4
6
|
// Where each runner takes its own (runtime) arguments: before the package for npx and uvx,
|
|
5
7
|
// after `run` for docker, so container flags such as `-e` or `-v` reach `docker run`.
|
|
6
8
|
const runners = {
|
|
@@ -23,6 +25,15 @@ const runners = {
|
|
|
23
25
|
],
|
|
24
26
|
}),
|
|
25
27
|
};
|
|
28
|
+
// The command a registry type is started with (`npx`, `uvx`, `docker`), as an `exec` permission
|
|
29
|
+
// names it.
|
|
30
|
+
export function runnerCommand(registryType) {
|
|
31
|
+
const runner = runners[registryType];
|
|
32
|
+
if (!runner)
|
|
33
|
+
throw new Error(`registry type ${registryType} is not supported`);
|
|
34
|
+
return runner("x", undefined, []).command;
|
|
35
|
+
}
|
|
36
|
+
// First-party servers: every argument is a literal.
|
|
26
37
|
export function literalArguments(list, where) {
|
|
27
38
|
if (list === undefined)
|
|
28
39
|
return [];
|
|
@@ -39,36 +50,173 @@ export function literalArguments(list, where) {
|
|
|
39
50
|
return [argument.value];
|
|
40
51
|
});
|
|
41
52
|
}
|
|
53
|
+
// `{name}` placeholders in a registry value (`Bearer {api_key}`, `TOKEN={token}`).
|
|
54
|
+
const PLACEHOLDER = /\{([A-Za-z_][A-Za-z0-9_.-]*)\}/g;
|
|
55
|
+
const placeholders = (value) => [...value.matchAll(PLACEHOLDER)].map((match) => match[1]);
|
|
56
|
+
const asList = (value) => Array.isArray(value) ? value : [];
|
|
57
|
+
// The stdio package a launch uses, or none (then the first remote).
|
|
58
|
+
export function stdioPackage(server) {
|
|
59
|
+
return server.packages?.find((item) => {
|
|
60
|
+
const transport = item.transport?.type;
|
|
61
|
+
return transport === undefined || transport === "stdio";
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
export function launchInputs(server) {
|
|
65
|
+
const inputs = new Map();
|
|
66
|
+
const add = (input) => {
|
|
67
|
+
const have = inputs.get(input.name);
|
|
68
|
+
inputs.set(input.name, have
|
|
69
|
+
? {
|
|
70
|
+
...have,
|
|
71
|
+
required: have.required || input.required,
|
|
72
|
+
secret: have.secret || input.secret,
|
|
73
|
+
}
|
|
74
|
+
: input);
|
|
75
|
+
};
|
|
76
|
+
const fromTemplate = (item, kind, value) => {
|
|
77
|
+
for (const name of placeholders(value)) {
|
|
78
|
+
const variable = item.variables?.[name];
|
|
79
|
+
add({
|
|
80
|
+
name,
|
|
81
|
+
kind,
|
|
82
|
+
required: item.isRequired === true || variable?.isRequired === true,
|
|
83
|
+
secret: item.isSecret === true || variable?.isSecret === true,
|
|
84
|
+
description: typeof variable?.description === "string"
|
|
85
|
+
? variable.description
|
|
86
|
+
: typeof item.description === "string"
|
|
87
|
+
? item.description
|
|
88
|
+
: undefined,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const stdio = stdioPackage(server);
|
|
93
|
+
if (stdio) {
|
|
94
|
+
for (const variable of asList(stdio.environmentVariables))
|
|
95
|
+
if (typeof variable.name === "string")
|
|
96
|
+
add({
|
|
97
|
+
name: variable.name,
|
|
98
|
+
kind: "env",
|
|
99
|
+
required: variable.isRequired === true,
|
|
100
|
+
secret: variable.isSecret === true,
|
|
101
|
+
description: typeof variable.description === "string"
|
|
102
|
+
? variable.description
|
|
103
|
+
: undefined,
|
|
104
|
+
});
|
|
105
|
+
for (const argument of [
|
|
106
|
+
...asList(stdio.runtimeArguments),
|
|
107
|
+
...asList(stdio.packageArguments),
|
|
108
|
+
])
|
|
109
|
+
if (typeof argument.value === "string")
|
|
110
|
+
fromTemplate(argument, "argument", argument.value);
|
|
111
|
+
return [...inputs.values()];
|
|
112
|
+
}
|
|
113
|
+
const remote = server.remotes?.[0];
|
|
114
|
+
for (const header of asList(remote?.headers)) {
|
|
115
|
+
if (typeof header.name !== "string")
|
|
116
|
+
continue;
|
|
117
|
+
if (typeof header.value === "string")
|
|
118
|
+
fromTemplate(header, "header", header.value);
|
|
119
|
+
else
|
|
120
|
+
add({
|
|
121
|
+
name: header.name,
|
|
122
|
+
kind: "header",
|
|
123
|
+
required: header.isRequired === true,
|
|
124
|
+
secret: header.isSecret === true,
|
|
125
|
+
description: typeof header.description === "string"
|
|
126
|
+
? header.description
|
|
127
|
+
: undefined,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return [...inputs.values()];
|
|
131
|
+
}
|
|
132
|
+
const missing = (capability, name) => new Error(`${capability} needs ${name}: set it in the environment, or run \`ocean market secret set ${capability} ${name}\``);
|
|
42
133
|
// Turns an installed server into the exact process (or endpoint) to connect to, checking every
|
|
43
|
-
// step against what the capability declared: the runner must be a declared `exec`, every
|
|
44
|
-
//
|
|
45
|
-
//
|
|
134
|
+
// step against what the capability declared: the runner must be a declared `exec`, every value
|
|
135
|
+
// it is given a declared `env`, a remote's host a declared `network`. The process gets only
|
|
136
|
+
// those variables (plus PATH so the runner resolves). A value comes from the environment, then
|
|
137
|
+
// from `secret` (the secret store); an optional one that is missing is left out.
|
|
46
138
|
export function planMcpLaunch(installed, options) {
|
|
47
139
|
const home = options.home ?? os.homedir();
|
|
48
140
|
const hostEnv = options.env ?? process.env;
|
|
49
141
|
const policy = new PermissionPolicy(installed.capability, installed.permissions, home);
|
|
50
|
-
const { server } = installed;
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
return
|
|
54
|
-
}
|
|
142
|
+
const { server, capability } = installed;
|
|
143
|
+
const value = (name) => {
|
|
144
|
+
policy.require({ type: "env", target: name });
|
|
145
|
+
return hostEnv[name] ?? options.secret?.(name);
|
|
146
|
+
};
|
|
147
|
+
// A template with every placeholder filled, or undefined when one has no value.
|
|
148
|
+
const fill = (template) => {
|
|
149
|
+
let complete = true;
|
|
150
|
+
const filled = template.replace(PLACEHOLDER, (_, name) => {
|
|
151
|
+
const found = value(name);
|
|
152
|
+
if (found === undefined)
|
|
153
|
+
complete = false;
|
|
154
|
+
return found ?? "";
|
|
155
|
+
});
|
|
156
|
+
return complete ? filled : undefined;
|
|
157
|
+
};
|
|
158
|
+
const resolveArguments = (list, where) => {
|
|
159
|
+
if (list === undefined)
|
|
160
|
+
return [];
|
|
161
|
+
if (!Array.isArray(list))
|
|
162
|
+
throw new Error(`${where} must be an array`);
|
|
163
|
+
return list.flatMap((argument) => {
|
|
164
|
+
const raw = argument?.value ?? argument?.default;
|
|
165
|
+
if (typeof raw !== "string")
|
|
166
|
+
throw new Error(`${capability} ${where}: an argument needs a value or a default`);
|
|
167
|
+
const filled = fill(raw);
|
|
168
|
+
if (filled === undefined) {
|
|
169
|
+
if (argument.isRequired === true)
|
|
170
|
+
throw missing(capability, placeholders(raw)[0]);
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
if (argument.type === "named") {
|
|
174
|
+
if (typeof argument.name !== "string")
|
|
175
|
+
throw new Error(`${where}: a named argument needs a name`);
|
|
176
|
+
return [argument.name, filled];
|
|
177
|
+
}
|
|
178
|
+
return [filled];
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
const stdio = stdioPackage(server);
|
|
55
182
|
if (!stdio) {
|
|
56
183
|
const remote = server.remotes?.[0];
|
|
57
184
|
if (!remote)
|
|
58
|
-
throw new Error(`${
|
|
185
|
+
throw new Error(`${capability} has no stdio package or remote endpoint`);
|
|
186
|
+
// Checked again here, not only at install: the installed file is plain JSON on disk.
|
|
59
187
|
const url = new URL(String(remote.url));
|
|
188
|
+
if (!remoteUrlAllowed(url))
|
|
189
|
+
throw new Error(`${capability}: remote ${url.href} must be https (or http on loopback)`);
|
|
190
|
+
const type = String(remote.type ?? "streamable-http");
|
|
191
|
+
if (!remoteTransports.includes(type))
|
|
192
|
+
throw new Error(`${capability}: remote transport ${type} is not supported`);
|
|
60
193
|
policy.require({ type: "network", target: url.hostname });
|
|
194
|
+
const headers = {};
|
|
195
|
+
for (const header of asList(remote.headers)) {
|
|
196
|
+
if (typeof header.name !== "string")
|
|
197
|
+
continue;
|
|
198
|
+
const filled = typeof header.value === "string"
|
|
199
|
+
? fill(header.value)
|
|
200
|
+
: value(header.name);
|
|
201
|
+
if (filled !== undefined)
|
|
202
|
+
headers[header.name] = filled;
|
|
203
|
+
else if (header.isRequired === true)
|
|
204
|
+
throw missing(capability, typeof header.value === "string"
|
|
205
|
+
? placeholders(header.value)[0]
|
|
206
|
+
: header.name);
|
|
207
|
+
}
|
|
61
208
|
return {
|
|
62
209
|
transport: "remote",
|
|
63
|
-
type:
|
|
210
|
+
type: type,
|
|
64
211
|
url: url.href,
|
|
212
|
+
headers,
|
|
65
213
|
};
|
|
66
214
|
}
|
|
67
215
|
const registryType = String(stdio.registryType);
|
|
68
216
|
const runner = runners[registryType];
|
|
69
217
|
if (!runner)
|
|
70
|
-
throw new Error(`${
|
|
71
|
-
const base = runner(String(stdio.identifier), typeof stdio.version === "string" ? stdio.version : undefined,
|
|
218
|
+
throw new Error(`${capability}: registry type ${registryType} is not supported`);
|
|
219
|
+
const base = runner(String(stdio.identifier), typeof stdio.version === "string" ? stdio.version : undefined, resolveArguments(stdio.runtimeArguments, "runtimeArguments"));
|
|
72
220
|
policy.require({ type: "exec", target: base.command });
|
|
73
221
|
// The node running Ocean comes first, so its npx resolves even when a GUI client (Claude
|
|
74
222
|
// Desktop, VS Code) starts this with the login PATH, which lacks a version-managed node.
|
|
@@ -79,23 +227,19 @@ export function planMcpLaunch(installed, options) {
|
|
|
79
227
|
].join(path.delimiter),
|
|
80
228
|
HOME: home,
|
|
81
229
|
};
|
|
82
|
-
const
|
|
83
|
-
? stdio.environmentVariables
|
|
84
|
-
: [];
|
|
85
|
-
for (const variable of declaredVariables) {
|
|
230
|
+
for (const variable of asList(stdio.environmentVariables)) {
|
|
86
231
|
if (typeof variable?.name !== "string")
|
|
87
|
-
throw new Error(`${
|
|
88
|
-
|
|
89
|
-
const value = hostEnv[variable.name] ??
|
|
232
|
+
throw new Error(`${capability}: environment variable without a name`);
|
|
233
|
+
const found = value(variable.name) ??
|
|
90
234
|
(typeof variable.default === "string" ? variable.default : undefined);
|
|
91
|
-
if (
|
|
92
|
-
env[variable.name] =
|
|
235
|
+
if (found !== undefined)
|
|
236
|
+
env[variable.name] = found;
|
|
93
237
|
else if (variable.isRequired === true)
|
|
94
|
-
throw
|
|
238
|
+
throw missing(capability, variable.name);
|
|
95
239
|
}
|
|
96
240
|
const args = [
|
|
97
241
|
...base.args,
|
|
98
|
-
...
|
|
242
|
+
...resolveArguments(stdio.packageArguments, "packageArguments"),
|
|
99
243
|
];
|
|
100
244
|
return {
|
|
101
245
|
transport: "stdio",
|
|
@@ -2,6 +2,7 @@ import { spawn } from "node:child_process";
|
|
|
2
2
|
import { mkdir, readFile } from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { mcpServerFile } from "../adapters/mcp-adapter.js";
|
|
5
|
+
import { keychainStore } from "../security/secrets.js";
|
|
5
6
|
import { planMcpLaunch } from "./mcp-launch.js";
|
|
6
7
|
// The one way an installed MCP server is started: planned against its declared permissions,
|
|
7
8
|
// then run with stdio passed through, so a client can point at
|
|
@@ -16,7 +17,11 @@ export async function runInstalledMcp(name, options) {
|
|
|
16
17
|
}
|
|
17
18
|
const stateDir = path.join(options.stateRoot, path.basename(mcpServerFile(options.serversRoot, name), ".json"));
|
|
18
19
|
await mkdir(stateDir, { recursive: true });
|
|
19
|
-
const
|
|
20
|
+
const secrets = options.secrets ?? keychainStore();
|
|
21
|
+
const launch = planMcpLaunch(installed, {
|
|
22
|
+
stateDir,
|
|
23
|
+
secret: (secret) => secrets.get(installed.capability, secret),
|
|
24
|
+
});
|
|
20
25
|
if (launch.transport === "remote")
|
|
21
26
|
throw new Error(`${name} is a remote server (${launch.type} ${launch.url}); connect the client to it directly`);
|
|
22
27
|
const child = spawn(launch.command, launch.args, {
|