@noetic-tools/core 5.20.0-next.1 → 5.20.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/dist/adapters/openrouter.d.ts +1 -48
- package/dist/adapters/openrouter.js +3 -204
- package/dist/builders/acp-agent-checks.d.ts +20 -0
- package/dist/builders/acp-agent-checks.js +68 -0
- package/dist/builders/acp-agent-tool.d.ts +4 -3
- package/dist/builders/acp-agent-tool.js +5 -2
- package/dist/builders/connection-ceiling.d.ts +13 -0
- package/dist/builders/connection-ceiling.js +17 -0
- package/dist/builders/document-entries.d.ts +13 -0
- package/dist/builders/document-entries.js +18 -0
- package/dist/builders/dynamic-workflow.d.ts +53 -8
- package/dist/builders/dynamic-workflow.js +53 -25
- package/dist/builders/mcp-url-pattern.d.ts +13 -0
- package/dist/builders/mcp-url-pattern.js +85 -0
- package/dist/builders/step-builders.d.ts +14 -6
- package/dist/builders/step-builders.js +25 -2
- package/dist/builders/workflow-acp-grants.d.ts +42 -0
- package/dist/builders/workflow-acp-grants.js +224 -0
- package/dist/builders/workflow-child-nodes.js +8 -0
- package/dist/builders/workflow-connection-nodes.d.ts +13 -3
- package/dist/builders/workflow-connection-nodes.js +129 -3
- package/dist/builders/workflow-connections.js +2 -1
- package/dist/builders/workflow-hydrator.d.ts +24 -1
- package/dist/builders/workflow-hydrator.js +8 -167
- package/dist/builders/workflow-mcp-servers.d.ts +44 -0
- package/dist/builders/workflow-mcp-servers.js +327 -0
- package/dist/builders/workflow-tool-entries.d.ts +84 -0
- package/dist/builders/workflow-tool-entries.js +216 -0
- package/dist/connections/durability.d.ts +11 -1
- package/dist/connections/durability.js +34 -0
- package/dist/connections/entries.d.ts +33 -0
- package/dist/connections/entries.js +118 -0
- package/dist/connections/errors.d.ts +6 -0
- package/dist/connections/errors.js +12 -0
- package/dist/connections/harness-wiring.d.ts +4 -4
- package/dist/connections/harness-wiring.js +1 -1
- package/dist/connections/in-memory-connection.d.ts +2 -0
- package/dist/connections/in-memory-connection.js +5 -0
- package/dist/connections/model-tools.d.ts +35 -12
- package/dist/connections/model-tools.js +348 -202
- package/dist/connections/registry-types.d.ts +7 -0
- package/dist/connections/registry.d.ts +35 -6
- package/dist/connections/registry.js +183 -39
- package/dist/connections/step-tools.d.ts +64 -0
- package/dist/connections/step-tools.js +154 -0
- package/dist/connections/views.d.ts +3 -2
- package/dist/connections/views.js +15 -6
- package/dist/harness/agent-harness.d.ts +11 -1
- package/dist/harness/agent-harness.js +69 -8
- package/dist/harness/connection-gate.d.ts +49 -8
- package/dist/harness/connection-gate.js +137 -26
- package/dist/harness/deps/interpreter.d.ts +3 -3
- package/dist/harness/deps/interpreter.js +3 -3
- package/dist/harness/harness-options.d.ts +4 -3
- package/dist/harness/model-request.js +3 -1
- package/dist/harness/model-schema.d.ts +1 -19
- package/dist/harness/model-schema.js +0 -28
- package/dist/harness/model-tool-round.js +2 -2
- package/dist/index.d.ts +5 -5
- package/dist/index.js +1 -1
- package/dist/interpreter/acp-events.d.ts +25 -1
- package/dist/interpreter/acp-events.js +53 -1
- package/dist/interpreter/acp-layers.d.ts +20 -0
- package/dist/interpreter/acp-layers.js +55 -0
- package/dist/interpreter/acp-tools.d.ts +108 -0
- package/dist/interpreter/acp-tools.js +462 -0
- package/dist/interpreter/collect-tools.d.ts +28 -0
- package/dist/interpreter/collect-tools.js +112 -15
- package/dist/interpreter/connection-steps.d.ts +0 -2
- package/dist/interpreter/connection-steps.js +30 -8
- package/dist/interpreter/execute-acp-agent.js +263 -72
- package/dist/interpreter/execute-action.d.ts +76 -1
- package/dist/interpreter/execute-action.js +35 -42
- package/dist/interpreter/execute.d.ts +2 -0
- package/dist/interpreter/execute.js +24 -1
- package/dist/interpreter/verify-host.js +2 -1
- package/dist/runtime/tool-call.d.ts +67 -0
- package/dist/runtime/tool-call.js +244 -0
- package/dist/runtime/tool-context.d.ts +1 -2
- package/dist/runtime/tool-context.js +6 -2
- package/dist/schemas/workflow.d.ts +1 -1
- package/dist/types/connection-entry.d.ts +22 -0
- package/dist/types/connection-entry.js +8 -0
- package/dist/util/harness-internals.d.ts +2 -0
- package/dist/util/root-context.d.ts +3 -0
- package/dist/util/root-context.js +8 -0
- package/dist/util/tool-name.d.ts +8 -0
- package/dist/util/tool-name.js +10 -0
- package/package.json +3 -3
- package/schema/noetic-workflow.schema.json +498 -128
|
@@ -5,61 +5,35 @@
|
|
|
5
5
|
* `loop`, etc.) so hydrated steps are indistinguishable from programmatic
|
|
6
6
|
* ones — they register in the step registry, carry retry policies, etc.
|
|
7
7
|
*/
|
|
8
|
-
import { frameworkCast,
|
|
8
|
+
import { frameworkCast, NoeticConfigError } from '@noetic-tools/types';
|
|
9
9
|
import { compileExpression, compute, readInput } from '../expression/jsonata.js';
|
|
10
10
|
import { markRecorded, stepOutputsOf } from '../expression/step-outputs.js';
|
|
11
11
|
import { shorthandStepId, stepEntryToNode, stepListEntries } from '../schemas/workflow.js';
|
|
12
|
-
import { DetachedHandleImpl } from '../util/detached-handle.js';
|
|
13
12
|
import { outputText } from './output-text.js';
|
|
14
|
-
import { callModel, runCode
|
|
13
|
+
import { callModel, runCode } from './step-builders.js';
|
|
15
14
|
import { hydrateInParallelNode, hydrateSpawnNode, hydrateSubflowNode, hydrateWithContextNode, } from './workflow-child-nodes.js';
|
|
16
|
-
import { hydrateCallNode, hydrateWaitNode } from './workflow-connection-nodes.js';
|
|
15
|
+
import { hydrateAcpAgentNode, hydrateCallNode, hydrateRunCodeNode, hydrateWaitNode, } from './workflow-connection-nodes.js';
|
|
17
16
|
import { checkDocumentConnections } from './workflow-connections.js';
|
|
18
17
|
import { hydrateConditionalNode, hydrateDecideNode, hydrateLoopNode, hydrateRouteSwitchNode, hydrateScheduleNode, } from './workflow-control-nodes.js';
|
|
19
18
|
import { documentScope, everyDocumentItem, hydrateEmitItems, nodeScope, } from './workflow-custom-items.js';
|
|
20
19
|
import { workflowReplayKey } from './workflow-replay-key.js';
|
|
20
|
+
import { nodeTools, recordDocumentConnections, registeredSentence, withCeiling, } from './workflow-tool-entries.js';
|
|
21
21
|
import { hydrateVerifyNode } from './workflow-verify.js';
|
|
22
22
|
//#endregion
|
|
23
23
|
//#region Node Hydrators
|
|
24
|
-
/**
|
|
25
|
-
* Resolve an llm node's `tools` entries into a combined client+server list.
|
|
26
|
-
*
|
|
27
|
-
* Every entry is `{ type, parameters? }`. A reserved `openrouter:*` literal is
|
|
28
|
-
* a SERVER tool (flows through unchanged); any other `type` is a CLIENT tool
|
|
29
|
-
* whose `type` is the registered tool NAME (resolved from the registry). Both
|
|
30
|
-
* kinds combine into one `tools` array; the interpreter partitions them again
|
|
31
|
-
* at execution.
|
|
32
|
-
*/
|
|
33
|
-
function resolveCallModelTools(entries, registry) {
|
|
34
|
-
const toolNames = [];
|
|
35
|
-
const serverSpecs = [];
|
|
36
|
-
for (const entry of entries ?? []) {
|
|
37
|
-
if (isServerToolSpec(entry)) {
|
|
38
|
-
serverSpecs.push(entry);
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
toolNames.push(entry.type);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return [
|
|
45
|
-
...resolveTools(toolNames, registry),
|
|
46
|
-
...serverSpecs,
|
|
47
|
-
];
|
|
48
|
-
}
|
|
49
24
|
function hydrateCallModelNode(node, ctx) {
|
|
50
25
|
if (node.kind !== 'callModel') {
|
|
51
26
|
return frameworkCast(undefined);
|
|
52
27
|
}
|
|
53
|
-
|
|
54
|
-
return callModel({
|
|
28
|
+
return withCeiling(callModel({
|
|
55
29
|
id: node.id,
|
|
56
30
|
model: node.model ?? 'openai/gpt-4o',
|
|
57
31
|
instructions: node.instructions,
|
|
58
|
-
tools:
|
|
32
|
+
tools: nodeTools(node, ctx),
|
|
59
33
|
toolChoice: node.toolChoice,
|
|
60
34
|
params: node.params,
|
|
61
35
|
output: resolveOutputCodec(node.output, node.id, ctx),
|
|
62
|
-
});
|
|
36
|
+
}), node, ctx);
|
|
63
37
|
}
|
|
64
38
|
/**
|
|
65
39
|
* Resolve a `callModel` node's `output` codec reference to a live `OutputCodec`.
|
|
@@ -69,16 +43,6 @@ function hydrateCallModelNode(node, ctx) {
|
|
|
69
43
|
* runtime still returns the codec's real value. This is the same erasure the
|
|
70
44
|
* rest of the hydrator applies via `frameworkCast`.
|
|
71
45
|
*/
|
|
72
|
-
/** A complete sentence naming what is registered, for a hint. */
|
|
73
|
-
function registeredSentence(noun, names) {
|
|
74
|
-
const list = [
|
|
75
|
-
...names,
|
|
76
|
-
];
|
|
77
|
-
if (list.length === 0) {
|
|
78
|
-
return `No ${noun} are registered.`;
|
|
79
|
-
}
|
|
80
|
-
return `Registered ${noun}: ${list.join(', ')}.`;
|
|
81
|
-
}
|
|
82
46
|
function resolveOutputCodec(ref, nodeId, ctx) {
|
|
83
47
|
if (!ref) {
|
|
84
48
|
return undefined;
|
|
@@ -137,31 +101,6 @@ function hydrateInvokeToolNode(node, ctx) {
|
|
|
137
101
|
},
|
|
138
102
|
}));
|
|
139
103
|
}
|
|
140
|
-
function hydrateRunCodeNode(node, ctx) {
|
|
141
|
-
if (node.kind !== 'runCode') {
|
|
142
|
-
return frameworkCast(undefined);
|
|
143
|
-
}
|
|
144
|
-
const code = node.execute;
|
|
145
|
-
const subprocessRef = node.subprocess;
|
|
146
|
-
return frameworkCast(runCode({
|
|
147
|
-
id: node.id,
|
|
148
|
-
retry: node.retry,
|
|
149
|
-
execute: async (input, execCtx) => {
|
|
150
|
-
const adapter = resolveSubprocessAdapter({
|
|
151
|
-
ref: subprocessRef,
|
|
152
|
-
hydrationCtx: ctx,
|
|
153
|
-
execCtx,
|
|
154
|
-
nodeId: node.id,
|
|
155
|
-
});
|
|
156
|
-
return runCodeViaSubprocess({
|
|
157
|
-
adapter,
|
|
158
|
-
nodeId: node.id,
|
|
159
|
-
code,
|
|
160
|
-
input: outputText(input),
|
|
161
|
-
});
|
|
162
|
-
},
|
|
163
|
-
}));
|
|
164
|
-
}
|
|
165
104
|
function hydrateComputeNode(node, ctx) {
|
|
166
105
|
if (node.kind !== 'compute') {
|
|
167
106
|
return frameworkCast(undefined);
|
|
@@ -189,31 +128,6 @@ function hydrateSequenceNode(node, ctx) {
|
|
|
189
128
|
},
|
|
190
129
|
}));
|
|
191
130
|
}
|
|
192
|
-
function hydrateAcpAgentNode(node, ctx) {
|
|
193
|
-
if (node.kind !== 'acp-agent') {
|
|
194
|
-
return frameworkCast(undefined);
|
|
195
|
-
}
|
|
196
|
-
const agent = ctx.acpAgents?.get(node.agent);
|
|
197
|
-
if (!agent) {
|
|
198
|
-
throw new NoeticConfigError({
|
|
199
|
-
code: 'UNKNOWN_ACP_AGENT_REFERENCE',
|
|
200
|
-
message: `ACP agent '${node.agent}' referenced in workflow node '${node.id}' is not registered.`,
|
|
201
|
-
hint: 'Pass agent adapters via HydrationContext.acpAgents, e.g. createAcpAgentRegistry(claudeCode()) from @noetic-tools/acp.',
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
return step.acpAgent({
|
|
205
|
-
id: node.id,
|
|
206
|
-
agent,
|
|
207
|
-
prompt: node.prompt,
|
|
208
|
-
cwd: node.cwd,
|
|
209
|
-
mode: node.mode,
|
|
210
|
-
model: node.model,
|
|
211
|
-
mcpServers: node.mcpServers,
|
|
212
|
-
permissions: node.permissions,
|
|
213
|
-
clientCapabilities: node.clientCapabilities,
|
|
214
|
-
session: node.session,
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
131
|
//#endregion
|
|
218
132
|
//#region Document Scope
|
|
219
133
|
/** Nodes hydrated outside any `hydrateWorkflow` (a bare `hydrateNode`) share one scope. */
|
|
@@ -274,80 +188,6 @@ const NODE_HYDRATORS = {
|
|
|
274
188
|
verify: hydrateVerifyNode,
|
|
275
189
|
};
|
|
276
190
|
//#endregion
|
|
277
|
-
//#region Helpers
|
|
278
|
-
function resolveTools(toolNames, registry) {
|
|
279
|
-
if (!toolNames || toolNames.length === 0) {
|
|
280
|
-
return [];
|
|
281
|
-
}
|
|
282
|
-
return toolNames.map((name) => {
|
|
283
|
-
const resolved = registry.get(name);
|
|
284
|
-
if (!resolved) {
|
|
285
|
-
throw new NoeticConfigError({
|
|
286
|
-
code: 'UNKNOWN_TOOL_REFERENCE',
|
|
287
|
-
message: `Tool '${name}' referenced in workflow is not registered.`,
|
|
288
|
-
hint: `Register '${name}' in HydrationContext.tools before hydrating. ${registeredSentence('tools', registry.keys())}`,
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
return resolved;
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
//#endregion
|
|
295
|
-
//#region Run Node Helpers
|
|
296
|
-
/**
|
|
297
|
-
* Resolves the subprocess adapter for a `runCode` node. When the node names an
|
|
298
|
-
* adapter ref, the host must supply a `resolveSubprocess` resolver; otherwise
|
|
299
|
-
* the step falls back to the harness adapter on the execution context.
|
|
300
|
-
*/
|
|
301
|
-
function resolveSubprocessAdapter(opts) {
|
|
302
|
-
const { ref, hydrationCtx, execCtx, nodeId } = opts;
|
|
303
|
-
if (ref === undefined) {
|
|
304
|
-
return execCtx.subprocess;
|
|
305
|
-
}
|
|
306
|
-
const resolved = hydrationCtx.resolveSubprocess?.(ref);
|
|
307
|
-
if (!resolved) {
|
|
308
|
-
throw new NoeticConfigError({
|
|
309
|
-
code: 'UNKNOWN_SUBPROCESS_REFERENCE',
|
|
310
|
-
message: `Subprocess adapter '${ref}' referenced in run node '${nodeId}' could not be resolved.`,
|
|
311
|
-
hint: 'Pass a `resolveSubprocess(ref)` mapping in the HydrationContext, or omit `subprocess` to use the harness default (ctx.subprocess).',
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
return resolved;
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Dispatches a `runCode` node's code string through a subprocess adapter: ships the
|
|
318
|
-
* code plus the JSON-stringified input as a process request (input on stdin),
|
|
319
|
-
* waits for the handle to settle, and returns the captured stdout as the step
|
|
320
|
-
* output. A non-zero exit / failed handle surfaces as a thrown error.
|
|
321
|
-
*
|
|
322
|
-
* The code is never eval'd in-process (Cloudflare Workers forbid eval). The
|
|
323
|
-
* adapter owns running the code and capturing stdout into `handle.metadata.result`.
|
|
324
|
-
*/
|
|
325
|
-
async function runCodeViaSubprocess(opts) {
|
|
326
|
-
const { adapter, nodeId, code, input } = opts;
|
|
327
|
-
const request = {
|
|
328
|
-
kind: 'process',
|
|
329
|
-
command: 'node',
|
|
330
|
-
args: [
|
|
331
|
-
'-e',
|
|
332
|
-
code,
|
|
333
|
-
],
|
|
334
|
-
stdin: input,
|
|
335
|
-
metadata: {
|
|
336
|
-
noeticRun: true,
|
|
337
|
-
stepId: nodeId,
|
|
338
|
-
code,
|
|
339
|
-
input,
|
|
340
|
-
},
|
|
341
|
-
};
|
|
342
|
-
const handle = new DetachedHandleImpl({
|
|
343
|
-
id: `run-${nodeId}`,
|
|
344
|
-
stepId: nodeId,
|
|
345
|
-
spawned: adapter.spawn(request),
|
|
346
|
-
});
|
|
347
|
-
const result = await handle.await();
|
|
348
|
-
return outputText(result);
|
|
349
|
-
}
|
|
350
|
-
//#endregion
|
|
351
191
|
//#region Public API
|
|
352
192
|
/**
|
|
353
193
|
* Hydrates a single `WorkflowNode` into a live `Step`.
|
|
@@ -426,6 +266,7 @@ export function hydrateWorkflow(doc, ctx, customItems) {
|
|
|
426
266
|
...everyDocumentItem(doc),
|
|
427
267
|
...root.customItems,
|
|
428
268
|
};
|
|
269
|
+
recordDocumentConnections(frameworkCast(root), doc, ctx.mcp);
|
|
429
270
|
return root;
|
|
430
271
|
}
|
|
431
272
|
//#endregion
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The MCP servers a document names: `{ "mcp": … }` entries in a node's
|
|
3
|
+
* `tools` or the document's `connections`. Each becomes a connection entry,
|
|
4
|
+
* with noetic as the MCP client, built by the host's `mcp.provider` from the
|
|
5
|
+
* one grant that allows it — so a document a host did not write reaches only
|
|
6
|
+
* the URLs and exact commands the host grants, with only the secrets the
|
|
7
|
+
* grant binds and the policy the grant sets (spec 26 § MCP servers).
|
|
8
|
+
*
|
|
9
|
+
* One server through one `provider` and `spawn` is always one provider
|
|
10
|
+
* object, so two nodes that name it share one connection.
|
|
11
|
+
*/
|
|
12
|
+
import type { ConnectionConfig, McpAllowlist, SecretResolver, Spawn, WorkflowDocument, WorkflowMcpServer } from '@noetic-tools/types';
|
|
13
|
+
/**
|
|
14
|
+
* Checks every MCP server a document names — in its `connections`, or a
|
|
15
|
+
* `callModel` or `acp-agent` node's `tools` — against the host's allowlist.
|
|
16
|
+
* No allowlist grants nothing. Inline subflows count; named refs resolve from
|
|
17
|
+
* the host's own registry and are not checked.
|
|
18
|
+
*
|
|
19
|
+
* @throws `NoeticConfigError` `CONNECTION_NOT_ALLOWED` for a server, header or environment value the allowlist does not grant, or a header or environment variable named twice.
|
|
20
|
+
* @throws `NoeticConfigError` `CONNECTION_INVALID` for a `$secret` value when the harness has no `secrets` resolver, or an allowlist URL pattern that does not parse.
|
|
21
|
+
* @throws `NoeticConfigError` `CONNECTION_NAME_TAKEN` for two different servers under one name.
|
|
22
|
+
*/
|
|
23
|
+
export declare function checkMcpServers(doc: WorkflowDocument, allowlist: McpAllowlist | undefined, secrets: SecretResolver | undefined): void;
|
|
24
|
+
/** The servers a document's `connections` bring, as `{ "mcp": … }` entries. */
|
|
25
|
+
export declare function documentServers(doc: WorkflowDocument): WorkflowMcpServer[];
|
|
26
|
+
/** Names the document's `connections` in errors, where a node's id would go. */
|
|
27
|
+
export declare const DOCUMENT_WHERE = "The document's connections";
|
|
28
|
+
/**
|
|
29
|
+
* The connection entry for a server a node or the document names: the host's
|
|
30
|
+
* provider for it, with the policy of the grant that allows it. A stdio
|
|
31
|
+
* server an `acp-agent` node names starts through its agent's `spawn`, where
|
|
32
|
+
* the agent runs. The same server is always the same provider, so two nodes
|
|
33
|
+
* that name it share one connection, and a different server under a taken
|
|
34
|
+
* name is `CONNECTION_NAME_TAKEN` when the turn attaches it.
|
|
35
|
+
*
|
|
36
|
+
* @throws `NoeticConfigError` `CONNECTION_NOT_ALLOWED` when no grant allows it; `CONNECTION_INVALID` when the allowlist has no `provider`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function mcpEntry(where: string, server: WorkflowMcpServer, host: {
|
|
39
|
+
readonly allowlist: McpAllowlist | undefined;
|
|
40
|
+
/** Where a stdio server starts: its `acp-agent` node's agent. */
|
|
41
|
+
readonly spawn?: Spawn;
|
|
42
|
+
}): ConnectionConfig;
|
|
43
|
+
/** The planner's view of the MCP servers it may attach: each URL pattern, with the headers its secrets fill. */
|
|
44
|
+
export declare function mcpPrompt(allowlist: McpAllowlist | undefined): string;
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The MCP servers a document names: `{ "mcp": … }` entries in a node's
|
|
3
|
+
* `tools` or the document's `connections`. Each becomes a connection entry,
|
|
4
|
+
* with noetic as the MCP client, built by the host's `mcp.provider` from the
|
|
5
|
+
* one grant that allows it — so a document a host did not write reaches only
|
|
6
|
+
* the URLs and exact commands the host grants, with only the secrets the
|
|
7
|
+
* grant binds and the policy the grant sets (spec 26 § MCP servers).
|
|
8
|
+
*
|
|
9
|
+
* One server through one `provider` and `spawn` is always one provider
|
|
10
|
+
* object, so two nodes that name it share one connection.
|
|
11
|
+
*/
|
|
12
|
+
import { NoeticConfigError, walkWorkflow } from '@noetic-tools/types';
|
|
13
|
+
import { urlPatternMatches } from './mcp-url-pattern.js';
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region Public API
|
|
16
|
+
/**
|
|
17
|
+
* Checks every MCP server a document names — in its `connections`, or a
|
|
18
|
+
* `callModel` or `acp-agent` node's `tools` — against the host's allowlist.
|
|
19
|
+
* No allowlist grants nothing. Inline subflows count; named refs resolve from
|
|
20
|
+
* the host's own registry and are not checked.
|
|
21
|
+
*
|
|
22
|
+
* @throws `NoeticConfigError` `CONNECTION_NOT_ALLOWED` for a server, header or environment value the allowlist does not grant, or a header or environment variable named twice.
|
|
23
|
+
* @throws `NoeticConfigError` `CONNECTION_INVALID` for a `$secret` value when the harness has no `secrets` resolver, or an allowlist URL pattern that does not parse.
|
|
24
|
+
* @throws `NoeticConfigError` `CONNECTION_NAME_TAKEN` for two different servers under one name.
|
|
25
|
+
*/
|
|
26
|
+
export function checkMcpServers(doc, allowlist, secrets) {
|
|
27
|
+
const check = (where, server) => {
|
|
28
|
+
grantOf(where, server, allowlist ?? {});
|
|
29
|
+
if (!secrets && valuesOf(server).length > 0) {
|
|
30
|
+
throw noResolver(server.name);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const named = new Map();
|
|
34
|
+
const claim = (where, server) => {
|
|
35
|
+
check(where, server);
|
|
36
|
+
const held = named.get(server.name);
|
|
37
|
+
if (held && JSON.stringify(held) !== JSON.stringify(server)) {
|
|
38
|
+
throw nameTaken(where, server.name);
|
|
39
|
+
}
|
|
40
|
+
named.set(server.name, server);
|
|
41
|
+
};
|
|
42
|
+
for (const server of documentServers(doc)) {
|
|
43
|
+
claim(DOCUMENT_WHERE, server);
|
|
44
|
+
}
|
|
45
|
+
for (const node of walkWorkflow(doc.root)) {
|
|
46
|
+
for (const server of serversOf(node)) {
|
|
47
|
+
claim(`Node '${node.id}'`, server);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** The servers a document's `connections` bring, as `{ "mcp": … }` entries. */
|
|
52
|
+
export function documentServers(doc) {
|
|
53
|
+
return Object.values(doc.connections ?? {}).flatMap((entry) => entry.mcp
|
|
54
|
+
? [
|
|
55
|
+
entry.mcp,
|
|
56
|
+
]
|
|
57
|
+
: []);
|
|
58
|
+
}
|
|
59
|
+
/** Names the document's `connections` in errors, where a node's id would go. */
|
|
60
|
+
export const DOCUMENT_WHERE = "The document's connections";
|
|
61
|
+
/**
|
|
62
|
+
* The connection entry for a server a node or the document names: the host's
|
|
63
|
+
* provider for it, with the policy of the grant that allows it. A stdio
|
|
64
|
+
* server an `acp-agent` node names starts through its agent's `spawn`, where
|
|
65
|
+
* the agent runs. The same server is always the same provider, so two nodes
|
|
66
|
+
* that name it share one connection, and a different server under a taken
|
|
67
|
+
* name is `CONNECTION_NAME_TAKEN` when the turn attaches it.
|
|
68
|
+
*
|
|
69
|
+
* @throws `NoeticConfigError` `CONNECTION_NOT_ALLOWED` when no grant allows it; `CONNECTION_INVALID` when the allowlist has no `provider`.
|
|
70
|
+
*/
|
|
71
|
+
export function mcpEntry(where, server, host) {
|
|
72
|
+
const { allowlist, spawn } = host;
|
|
73
|
+
const grant = grantOf(where, server, allowlist ?? {});
|
|
74
|
+
const open = allowlist?.provider;
|
|
75
|
+
if (!open) {
|
|
76
|
+
throw new NoeticConfigError({
|
|
77
|
+
code: 'CONNECTION_INVALID',
|
|
78
|
+
message: `${where} names MCP server '${server.name}', but the host gives no way to connect it.`,
|
|
79
|
+
hint: 'Set `mcp.provider` on the allowlist: `provider: mcp`, from @noetic-tools/mcp.',
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
provider: serverProvider(server, open, spawn),
|
|
84
|
+
...(grant.policy
|
|
85
|
+
? {
|
|
86
|
+
policy: grant.policy,
|
|
87
|
+
}
|
|
88
|
+
: {}),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** The planner's view of the MCP servers it may attach: each URL pattern, with the headers its secrets fill. */
|
|
92
|
+
export function mcpPrompt(allowlist) {
|
|
93
|
+
const lines = [
|
|
94
|
+
...(allowlist?.urls ?? []).map(urlLine),
|
|
95
|
+
...(allowlist?.commands ?? []).map(commandLine),
|
|
96
|
+
];
|
|
97
|
+
if (lines.length === 0) {
|
|
98
|
+
return '\nNo MCP servers are available: do not give a node an { "mcp": … } tool entry.';
|
|
99
|
+
}
|
|
100
|
+
return `\nMCP servers a callModel or acp-agent node may attach as a "tools" entry, or the document as a "connections" entry (no others; header and env values only as the { "$secret": "<name>" } shown; write each URL as \`new URL(url).href\` gives it back, e.g. "https://host/" with a lowercase host and a path):
|
|
101
|
+
${lines.join('\n')}`;
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region Checks
|
|
105
|
+
function serversOf(node) {
|
|
106
|
+
if (node.kind !== 'callModel' && node.kind !== 'acp-agent') {
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
return (node.tools ?? []).flatMap((entry) => 'mcp' in entry
|
|
110
|
+
? [
|
|
111
|
+
entry.mcp,
|
|
112
|
+
]
|
|
113
|
+
: []);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The grant that allows a server: the first that matches it and binds every
|
|
117
|
+
* one of its header or environment values, so a looser grant listed first
|
|
118
|
+
* cannot hide a tighter one that allows the values.
|
|
119
|
+
*
|
|
120
|
+
* @throws `NoeticConfigError` `CONNECTION_NOT_ALLOWED` when none does, or a value is named twice.
|
|
121
|
+
*/
|
|
122
|
+
function grantOf(where, server, allowlist) {
|
|
123
|
+
if ('url' in server) {
|
|
124
|
+
return urlGrant(where, server, allowlist.urls ?? []);
|
|
125
|
+
}
|
|
126
|
+
return commandGrant(where, server, allowlist.commands ?? []);
|
|
127
|
+
}
|
|
128
|
+
function urlGrant(where, server, grants) {
|
|
129
|
+
const url = plainUrl(where, server);
|
|
130
|
+
const matching = grants.filter((g) => urlPatternMatches(g.url, url));
|
|
131
|
+
if (matching.length === 0) {
|
|
132
|
+
throw notAllowed(`${where} names MCP server '${server.name}' at ${server.url}, which the host does not allow.`, `Allowed MCP server URLs: ${grants.map((g) => g.url).join(', ') || '(none)'}.`);
|
|
133
|
+
}
|
|
134
|
+
return bindingGrant(server.headers ?? [], matching, {
|
|
135
|
+
where,
|
|
136
|
+
server: server.name,
|
|
137
|
+
what: 'header',
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function commandGrant(where, server, grants) {
|
|
141
|
+
const matching = grants.filter((g) => g.command === server.command && sameArgs(g.args ?? [], server.args ?? []));
|
|
142
|
+
if (matching.length === 0) {
|
|
143
|
+
throw notAllowed(`${where} names stdio MCP server '${server.name}' (${commandText(server.command, server.args ?? [])}), which the host does not allow.`, `Allowed MCP server commands: ${grants.map((g) => commandText(g.command, g.args ?? [])).join('; ') || '(none)'}.`);
|
|
144
|
+
}
|
|
145
|
+
return bindingGrant(server.env ?? [], matching, {
|
|
146
|
+
where,
|
|
147
|
+
server: server.name,
|
|
148
|
+
what: 'environment variable',
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The first matching grant that binds every header or environment value —
|
|
153
|
+
* each a `$secret` the grant binds to that name. Each name appears once:
|
|
154
|
+
* which of two `Authorization` headers a server receives is up to the client.
|
|
155
|
+
*/
|
|
156
|
+
function bindingGrant(values, grants, site) {
|
|
157
|
+
const { where, server, what } = site;
|
|
158
|
+
const twice = values.find((entry, i) => values.slice(0, i).some((earlier) => sameName(earlier.name, entry.name, what)));
|
|
159
|
+
if (twice) {
|
|
160
|
+
throw notAllowed(`${where} sets ${what} '${twice.name}' on MCP server '${server}' more than once.`, `Name each ${what} once.`);
|
|
161
|
+
}
|
|
162
|
+
const grant = grants.find((g) => values.every((entry) => binds(g, entry, what)));
|
|
163
|
+
if (grant) {
|
|
164
|
+
return grant;
|
|
165
|
+
}
|
|
166
|
+
const entry = values.find((e) => !grants.some((g) => binds(g, e, what)));
|
|
167
|
+
if (!entry) {
|
|
168
|
+
throw notAllowed(`${where} sets ${what}s ${values.map((e) => `'${e.name}'`).join(', ')} on MCP server '${server}', which no one grant allows together.`, `Name only the ${what}s one grant binds.`);
|
|
169
|
+
}
|
|
170
|
+
const granted = grants.flatMap((g) => Object.entries(g.secrets ?? {})
|
|
171
|
+
.filter(([, name]) => sameName(name, entry.name, what))
|
|
172
|
+
.map(([secret]) => `{ "$secret": "${secret}" }`));
|
|
173
|
+
throw notAllowed(`${where} sets ${what} '${entry.name}' on MCP server '${server}' to a value the host does not allow.`, granted.length > 0
|
|
174
|
+
? `'${entry.name}' takes only ${granted.join(' or ')}.`
|
|
175
|
+
: `The host binds no secret to ${what} '${entry.name}'.`);
|
|
176
|
+
}
|
|
177
|
+
function binds(grant, entry, what) {
|
|
178
|
+
const secrets = grant.secrets;
|
|
179
|
+
if (!secrets || !Object.hasOwn(secrets, entry.value.$secret)) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
return sameName(secrets[entry.value.$secret] ?? '', entry.name, what);
|
|
183
|
+
}
|
|
184
|
+
/** HTTP header names are case-insensitive; environment variable names are not. */
|
|
185
|
+
function sameName(granted, written, what) {
|
|
186
|
+
return what === 'header' ? granted.toLowerCase() === written.toLowerCase() : granted === written;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* The server's URL, when it is http(s), carries no user or password
|
|
190
|
+
* (`http://localhost:1@evil.example/` would otherwise match
|
|
191
|
+
* `http://localhost:*`), and is written exactly as `URL` reads it back. The
|
|
192
|
+
* server is connected at the URL as written, so a URL whose reading differs —
|
|
193
|
+
* a backslash, a tab, an uppercase host — is refused rather than checked in
|
|
194
|
+
* one form and used in another.
|
|
195
|
+
*
|
|
196
|
+
* @throws `NoeticConfigError` `CONNECTION_NOT_ALLOWED` otherwise.
|
|
197
|
+
*/
|
|
198
|
+
function plainUrl(where, server) {
|
|
199
|
+
const parsed = URL.canParse(server.url) ? new URL(server.url) : undefined;
|
|
200
|
+
if (!parsed ||
|
|
201
|
+
(parsed.protocol !== 'http:' && parsed.protocol !== 'https:') ||
|
|
202
|
+
parsed.username !== '' ||
|
|
203
|
+
parsed.password !== '') {
|
|
204
|
+
throw notAllowed(`${where} names MCP server '${server.name}' at ${JSON.stringify(server.url)}, which is not an http: or https: URL without a user or password.`, 'Give an http: or https: URL with no user or password.');
|
|
205
|
+
}
|
|
206
|
+
if (parsed.href !== server.url) {
|
|
207
|
+
throw notAllowed(`${where} names MCP server '${server.name}' at ${JSON.stringify(server.url)}, which reads as ${parsed.href}.`, `Write the URL as ${parsed.href}.`);
|
|
208
|
+
}
|
|
209
|
+
return parsed;
|
|
210
|
+
}
|
|
211
|
+
function valuesOf(server) {
|
|
212
|
+
return ('url' in server ? server.headers : server.env) ?? [];
|
|
213
|
+
}
|
|
214
|
+
function sameArgs(a, b) {
|
|
215
|
+
return a.length === b.length && a.every((arg, i) => arg === b[i]);
|
|
216
|
+
}
|
|
217
|
+
function commandText(command, args) {
|
|
218
|
+
return JSON.stringify([
|
|
219
|
+
command,
|
|
220
|
+
...args,
|
|
221
|
+
]);
|
|
222
|
+
}
|
|
223
|
+
function notAllowed(message, hint) {
|
|
224
|
+
return new NoeticConfigError({
|
|
225
|
+
code: 'CONNECTION_NOT_ALLOWED',
|
|
226
|
+
message,
|
|
227
|
+
hint,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
function nameTaken(where, server) {
|
|
231
|
+
return new NoeticConfigError({
|
|
232
|
+
code: 'CONNECTION_NAME_TAKEN',
|
|
233
|
+
message: `${where} names MCP server '${server}', which the document already names with a different URL, command or values.`,
|
|
234
|
+
hint: 'Give each server its own name, or write the same server the same way everywhere.',
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
function noResolver(server) {
|
|
238
|
+
return new NoeticConfigError({
|
|
239
|
+
code: 'CONNECTION_INVALID',
|
|
240
|
+
message: `MCP server '${server}' uses a $secret, but the harness has no secret resolver.`,
|
|
241
|
+
hint: 'Set `environment.secrets` on the AgentHarness to resolve secret names.',
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
//#endregion
|
|
245
|
+
//#region Prompt
|
|
246
|
+
function urlLine(grant) {
|
|
247
|
+
const headers = Object.entries(grant.secrets ?? {}).map(([secret, header]) => `{ "name": "${header}", "value": { "$secret": "${secret}" } }`);
|
|
248
|
+
return `- { "mcp": { "name": "<name>", "type": "http" or "sse", "url": "<URL matching ${grant.url}>", "headers": [${headers.join(', ')}] } } (headers optional)`;
|
|
249
|
+
}
|
|
250
|
+
function commandLine(grant) {
|
|
251
|
+
const env = Object.entries(grant.secrets ?? {}).map(([secret, variable]) => `{ "name": "${variable}", "value": { "$secret": "${secret}" } }`);
|
|
252
|
+
return `- { "mcp": { "name": "<name>", "command": ${JSON.stringify(grant.command)}, "args": ${JSON.stringify(grant.args ?? [])}, "env": [${env.join(', ')}] } } (env optional)`;
|
|
253
|
+
}
|
|
254
|
+
//#endregion
|
|
255
|
+
//#region Providers
|
|
256
|
+
/** Providers already built, by `provider`, then by `spawn`, then by server. */
|
|
257
|
+
const PROVIDERS = new WeakMap();
|
|
258
|
+
/** Stands in for "no spawn" as a WeakMap key. */
|
|
259
|
+
const NO_SPAWN = {};
|
|
260
|
+
/**
|
|
261
|
+
* The provider for a granted server. It resolves the server's secrets when it
|
|
262
|
+
* connects, so a value never sits in the step graph.
|
|
263
|
+
*/
|
|
264
|
+
function serverProvider(server, open, spawn) {
|
|
265
|
+
const bySpawn = PROVIDERS.get(open) ?? new WeakMap();
|
|
266
|
+
PROVIDERS.set(open, bySpawn);
|
|
267
|
+
const built = bySpawn.get(spawn ?? NO_SPAWN) ?? new Map();
|
|
268
|
+
bySpawn.set(spawn ?? NO_SPAWN, built);
|
|
269
|
+
const key = JSON.stringify(server);
|
|
270
|
+
const held = built.get(key);
|
|
271
|
+
if (held) {
|
|
272
|
+
return held;
|
|
273
|
+
}
|
|
274
|
+
const provider = {
|
|
275
|
+
name: server.name,
|
|
276
|
+
connect: async (opts) => open(await granted(server, spawn, opts)).connect(opts),
|
|
277
|
+
};
|
|
278
|
+
built.set(key, provider);
|
|
279
|
+
return provider;
|
|
280
|
+
}
|
|
281
|
+
/** The server in ACP's shape with each secret resolved through the harness and registered for scrubbing. */
|
|
282
|
+
async function granted(server, spawn, opts) {
|
|
283
|
+
if ('url' in server) {
|
|
284
|
+
return {
|
|
285
|
+
name: server.name,
|
|
286
|
+
type: server.type,
|
|
287
|
+
url: server.url,
|
|
288
|
+
headers: await resolveValues(server.headers ?? [], opts),
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
name: server.name,
|
|
293
|
+
command: server.command,
|
|
294
|
+
args: server.args ?? [],
|
|
295
|
+
env: await resolveValues(server.env ?? [], opts),
|
|
296
|
+
...(spawn
|
|
297
|
+
? {
|
|
298
|
+
spawn,
|
|
299
|
+
}
|
|
300
|
+
: {}),
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* @throws `NoeticConfigError` `CONNECTION_INVALID` when the harness has no secret resolver.
|
|
305
|
+
*/
|
|
306
|
+
async function resolveValues(values, opts) {
|
|
307
|
+
const secrets = opts.secrets;
|
|
308
|
+
if (values.length === 0) {
|
|
309
|
+
return [];
|
|
310
|
+
}
|
|
311
|
+
if (!secrets) {
|
|
312
|
+
throw noResolver(opts.name);
|
|
313
|
+
}
|
|
314
|
+
const resolved = [];
|
|
315
|
+
for (const entry of values) {
|
|
316
|
+
const value = await secrets.resolve(entry.value.$secret, {
|
|
317
|
+
signal: opts.signal,
|
|
318
|
+
});
|
|
319
|
+
opts.redact?.(value);
|
|
320
|
+
resolved.push({
|
|
321
|
+
name: entry.name,
|
|
322
|
+
value,
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
return resolved;
|
|
326
|
+
}
|
|
327
|
+
//#endregion
|