@noetic-tools/core 5.19.3 → 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.
Files changed (85) hide show
  1. package/dist/adapters/openrouter.d.ts +1 -48
  2. package/dist/adapters/openrouter.js +3 -204
  3. package/dist/builders/acp-agent-checks.d.ts +20 -0
  4. package/dist/builders/acp-agent-checks.js +68 -0
  5. package/dist/builders/acp-agent-tool.d.ts +4 -3
  6. package/dist/builders/acp-agent-tool.js +5 -2
  7. package/dist/builders/connection-ceiling.d.ts +13 -0
  8. package/dist/builders/connection-ceiling.js +17 -0
  9. package/dist/builders/document-entries.d.ts +13 -0
  10. package/dist/builders/document-entries.js +18 -0
  11. package/dist/builders/dynamic-workflow.d.ts +20 -12
  12. package/dist/builders/dynamic-workflow.js +28 -24
  13. package/dist/builders/step-builders.d.ts +14 -6
  14. package/dist/builders/step-builders.js +25 -2
  15. package/dist/builders/workflow-child-nodes.js +7 -0
  16. package/dist/builders/workflow-connection-nodes.d.ts +3 -1
  17. package/dist/builders/workflow-connection-nodes.js +94 -6
  18. package/dist/builders/workflow-connections.js +2 -1
  19. package/dist/builders/workflow-hydrator.d.ts +12 -1
  20. package/dist/builders/workflow-hydrator.js +7 -141
  21. package/dist/builders/workflow-mcp-servers.d.ts +34 -14
  22. package/dist/builders/workflow-mcp-servers.js +211 -104
  23. package/dist/builders/workflow-tool-entries.d.ts +84 -0
  24. package/dist/builders/workflow-tool-entries.js +216 -0
  25. package/dist/connections/durability.d.ts +11 -1
  26. package/dist/connections/durability.js +34 -0
  27. package/dist/connections/entries.d.ts +33 -0
  28. package/dist/connections/entries.js +118 -0
  29. package/dist/connections/errors.d.ts +6 -0
  30. package/dist/connections/errors.js +12 -0
  31. package/dist/connections/harness-wiring.d.ts +4 -4
  32. package/dist/connections/harness-wiring.js +1 -1
  33. package/dist/connections/in-memory-connection.d.ts +2 -0
  34. package/dist/connections/in-memory-connection.js +5 -0
  35. package/dist/connections/model-tools.d.ts +35 -12
  36. package/dist/connections/model-tools.js +348 -202
  37. package/dist/connections/registry-types.d.ts +7 -0
  38. package/dist/connections/registry.d.ts +35 -6
  39. package/dist/connections/registry.js +183 -39
  40. package/dist/connections/step-tools.d.ts +64 -0
  41. package/dist/connections/step-tools.js +154 -0
  42. package/dist/connections/views.d.ts +3 -2
  43. package/dist/connections/views.js +15 -6
  44. package/dist/harness/agent-harness.d.ts +11 -1
  45. package/dist/harness/agent-harness.js +69 -8
  46. package/dist/harness/connection-gate.d.ts +49 -8
  47. package/dist/harness/connection-gate.js +136 -26
  48. package/dist/harness/deps/interpreter.d.ts +3 -3
  49. package/dist/harness/deps/interpreter.js +3 -3
  50. package/dist/harness/harness-options.d.ts +4 -3
  51. package/dist/harness/model-request.js +3 -1
  52. package/dist/harness/model-schema.d.ts +1 -19
  53. package/dist/harness/model-schema.js +0 -28
  54. package/dist/harness/model-tool-round.js +2 -2
  55. package/dist/index.d.ts +5 -5
  56. package/dist/index.js +1 -1
  57. package/dist/interpreter/acp-events.d.ts +25 -1
  58. package/dist/interpreter/acp-events.js +53 -1
  59. package/dist/interpreter/acp-layers.d.ts +20 -0
  60. package/dist/interpreter/acp-layers.js +55 -0
  61. package/dist/interpreter/acp-tools.d.ts +108 -0
  62. package/dist/interpreter/acp-tools.js +462 -0
  63. package/dist/interpreter/collect-tools.d.ts +28 -0
  64. package/dist/interpreter/collect-tools.js +112 -15
  65. package/dist/interpreter/connection-steps.d.ts +0 -2
  66. package/dist/interpreter/connection-steps.js +30 -8
  67. package/dist/interpreter/execute-acp-agent.js +263 -72
  68. package/dist/interpreter/execute-action.d.ts +76 -1
  69. package/dist/interpreter/execute-action.js +35 -42
  70. package/dist/interpreter/execute.d.ts +2 -0
  71. package/dist/interpreter/execute.js +24 -1
  72. package/dist/interpreter/verify-host.js +2 -1
  73. package/dist/runtime/tool-call.d.ts +67 -0
  74. package/dist/runtime/tool-call.js +244 -0
  75. package/dist/runtime/tool-context.d.ts +1 -2
  76. package/dist/runtime/tool-context.js +6 -2
  77. package/dist/schemas/workflow.d.ts +1 -1
  78. package/dist/types/connection-entry.d.ts +22 -0
  79. package/dist/types/connection-entry.js +8 -0
  80. package/dist/util/root-context.d.ts +3 -0
  81. package/dist/util/root-context.js +8 -0
  82. package/dist/util/tool-name.d.ts +8 -0
  83. package/dist/util/tool-name.js +10 -0
  84. package/package.json +2 -2
  85. package/schema/noetic-workflow.schema.json +495 -176
@@ -1,24 +1,44 @@
1
1
  /**
2
- * The MCP servers a document's `acp-agent` nodes hand to an agent. A stdio
3
- * server is a command the agent launches, and a header or environment value
4
- * reaches the agent process, so a document a host did not write may name only
5
- * the servers the host's `mcp` allowlist grants (spec 26 § MCP servers).
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.
6
11
  */
7
- import type { AcpMcpServer, Lazy, McpAllowlist, SecretResolver, WorkflowDocument, WorkflowMcpServer } from '@noetic-tools/types';
12
+ import type { ConnectionConfig, McpAllowlist, SecretResolver, Spawn, WorkflowDocument, WorkflowMcpServer } from '@noetic-tools/types';
8
13
  /**
9
- * Checks every MCP server a document's `acp-agent` nodes name against the
10
- * host's allowlist. No allowlist grants nothing. Inline subflows count; named
11
- * refs resolve from the host's own registry and are not checked.
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.
12
18
  *
13
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.
14
- * @throws `NoeticConfigError` `CONNECTION_INVALID` for a value that is a `$secret` when the harness has no `secrets` resolver, or an allowlist URL pattern that does not parse.
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.
15
22
  */
16
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";
17
28
  /**
18
- * The servers an `acp-agent` step gets: as written, or — when a value is a
19
- * `$secret` — resolved through the harness's `environment.secrets` when the
20
- * session starts.
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`.
21
37
  */
22
- export declare function stepMcpServers(servers: ReadonlyArray<WorkflowMcpServer> | undefined): Lazy<ReadonlyArray<AcpMcpServer> | undefined>;
23
- /** The planner's view of the servers it may give an `acp-agent` node. */
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. */
24
44
  export declare function mcpPrompt(allowlist: McpAllowlist | undefined): string;
@@ -1,124 +1,185 @@
1
1
  /**
2
- * The MCP servers a document's `acp-agent` nodes hand to an agent. A stdio
3
- * server is a command the agent launches, and a header or environment value
4
- * reaches the agent process, so a document a host did not write may name only
5
- * the servers the host's `mcp` allowlist grants (spec 26 § MCP servers).
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.
6
11
  */
7
12
  import { NoeticConfigError, walkWorkflow } from '@noetic-tools/types';
8
- import { harnessInternal } from '../util/harness-internals.js';
9
13
  import { urlPatternMatches } from './mcp-url-pattern.js';
10
14
  //#endregion
11
15
  //#region Public API
12
16
  /**
13
- * Checks every MCP server a document's `acp-agent` nodes name against the
14
- * host's allowlist. No allowlist grants nothing. Inline subflows count; named
15
- * refs resolve from the host's own registry and are not checked.
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.
16
21
  *
17
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.
18
- * @throws `NoeticConfigError` `CONNECTION_INVALID` for a value that is a `$secret` when the harness has no `secrets` resolver, or an allowlist URL pattern that does not parse.
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.
19
25
  */
20
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
+ }
21
45
  for (const node of walkWorkflow(doc.root)) {
22
46
  for (const server of serversOf(node)) {
23
- checkServer(node.id, server, allowlist ?? {});
24
- if (!secrets && !isLiteral(server)) {
25
- throw noResolver();
26
- }
47
+ claim(`Node '${node.id}'`, server);
27
48
  }
28
49
  }
29
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";
30
61
  /**
31
- * The servers an `acp-agent` step gets: as written, or — when a value is a
32
- * `$secret` — resolved through the harness's `environment.secrets` when the
33
- * session starts.
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`.
34
70
  */
35
- export function stepMcpServers(servers) {
36
- if (!servers || servers.every(isLiteral)) {
37
- return servers;
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
+ });
38
81
  }
39
- return (ctx) => resolveServers(servers, ctx);
82
+ return {
83
+ provider: serverProvider(server, open, spawn),
84
+ ...(grant.policy
85
+ ? {
86
+ policy: grant.policy,
87
+ }
88
+ : {}),
89
+ };
40
90
  }
41
- /** The planner's view of the servers it may give an `acp-agent` node. */
91
+ /** The planner's view of the MCP servers it may attach: each URL pattern, with the headers its secrets fill. */
42
92
  export function mcpPrompt(allowlist) {
43
93
  const lines = [
44
94
  ...(allowlist?.urls ?? []).map(urlLine),
45
95
  ...(allowlist?.commands ?? []).map(commandLine),
46
96
  ];
47
97
  if (lines.length === 0) {
48
- return '\nNo MCP servers are available: do not give acp-agent nodes "mcpServers".';
98
+ return '\nNo MCP servers are available: do not give a node an { "mcp": … } tool entry.';
49
99
  }
50
- return `\nMCP servers an acp-agent node may list in "mcpServers" (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):
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):
51
101
  ${lines.join('\n')}`;
52
102
  }
53
103
  //#endregion
54
104
  //#region Checks
55
105
  function serversOf(node) {
56
- return node.kind === 'acp-agent' ? (node.mcpServers ?? []) : [];
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
+ : []);
57
114
  }
58
- function checkServer(nodeId, server, allowlist) {
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) {
59
123
  if ('url' in server) {
60
- checkUrlServer(nodeId, server, allowlist.urls ?? []);
61
- return;
124
+ return urlGrant(where, server, allowlist.urls ?? []);
62
125
  }
63
- checkStdioServer(nodeId, server, allowlist.commands ?? []);
126
+ return commandGrant(where, server, allowlist.commands ?? []);
64
127
  }
65
- function checkUrlServer(nodeId, server, grants) {
66
- const url = plainUrl(nodeId, server);
128
+ function urlGrant(where, server, grants) {
129
+ const url = plainUrl(where, server);
67
130
  const matching = grants.filter((g) => urlPatternMatches(g.url, url));
68
131
  if (matching.length === 0) {
69
- throw notAllowed(`Node '${nodeId}' gives the agent MCP server '${server.name}' at ${server.url}, which the host does not allow.`, `Allowed MCP server URLs: ${grants.map((g) => g.url).join(', ') || '(none)'}.`);
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)'}.`);
70
133
  }
71
- checkValues(server.headers, matching, {
72
- nodeId,
134
+ return bindingGrant(server.headers ?? [], matching, {
135
+ where,
73
136
  server: server.name,
74
137
  what: 'header',
75
138
  });
76
139
  }
77
- function checkStdioServer(nodeId, server, grants) {
78
- const matching = grants.filter((g) => g.command === server.command && sameArgs(g.args ?? [], server.args));
140
+ function commandGrant(where, server, grants) {
141
+ const matching = grants.filter((g) => g.command === server.command && sameArgs(g.args ?? [], server.args ?? []));
79
142
  if (matching.length === 0) {
80
- throw notAllowed(`Node '${nodeId}' gives the agent 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)'}.`);
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)'}.`);
81
144
  }
82
- checkValues(server.env, matching, {
83
- nodeId,
145
+ return bindingGrant(server.env ?? [], matching, {
146
+ where,
84
147
  server: server.name,
85
148
  what: 'environment variable',
86
149
  });
87
150
  }
88
151
  /**
89
- * Every header or environment value must be a `$secret` that one grant matching
90
- * the server binds to that name — the same grant for all of them, so a looser
91
- * grant listed first cannot hide a tighter one that allows the values. Each
92
- * name appears once: which of two `Authorization` headers the agent sends is
93
- * up to the agent.
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.
94
155
  */
95
- function checkValues(values, grants, where) {
96
- const twice = values.find((entry, i) => values.slice(0, i).some((earlier) => sameName(earlier.name, entry.name, where.what)));
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)));
97
159
  if (twice) {
98
- throw notAllowed(`Node '${where.nodeId}' sets ${where.what} '${twice.name}' on MCP server '${where.server}' more than once.`, `Name each ${where.what} once.`);
160
+ throw notAllowed(`${where} sets ${what} '${twice.name}' on MCP server '${server}' more than once.`, `Name each ${what} once.`);
99
161
  }
100
- if (grants.some((g) => values.every((entry) => binds(g, entry, where.what)))) {
101
- return;
162
+ const grant = grants.find((g) => values.every((entry) => binds(g, entry, what)));
163
+ if (grant) {
164
+ return grant;
102
165
  }
103
- const { nodeId, server, what } = where;
104
166
  const entry = values.find((e) => !grants.some((g) => binds(g, e, what)));
105
167
  if (!entry) {
106
- throw notAllowed(`Node '${nodeId}' 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.`);
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.`);
107
169
  }
108
170
  const granted = grants.flatMap((g) => Object.entries(g.secrets ?? {})
109
171
  .filter(([, name]) => sameName(name, entry.name, what))
110
172
  .map(([secret]) => `{ "$secret": "${secret}" }`));
111
- throw notAllowed(`Node '${nodeId}' sets ${what} '${entry.name}' on MCP server '${server}' to a value the host does not allow.`, granted.length > 0
173
+ throw notAllowed(`${where} sets ${what} '${entry.name}' on MCP server '${server}' to a value the host does not allow.`, granted.length > 0
112
174
  ? `'${entry.name}' takes only ${granted.join(' or ')}.`
113
175
  : `The host binds no secret to ${what} '${entry.name}'.`);
114
176
  }
115
177
  function binds(grant, entry, what) {
116
- if (!isSecret(entry.value) ||
117
- !grant.secrets ||
118
- !Object.hasOwn(grant.secrets, entry.value.$secret)) {
178
+ const secrets = grant.secrets;
179
+ if (!secrets || !Object.hasOwn(secrets, entry.value.$secret)) {
119
180
  return false;
120
181
  }
121
- return sameName(grant.secrets[entry.value.$secret] ?? '', entry.name, what);
182
+ return sameName(secrets[entry.value.$secret] ?? '', entry.name, what);
122
183
  }
123
184
  /** HTTP header names are case-insensitive; environment variable names are not. */
124
185
  function sameName(granted, written, what) {
@@ -128,23 +189,28 @@ function sameName(granted, written, what) {
128
189
  * The server's URL, when it is http(s), carries no user or password
129
190
  * (`http://localhost:1@evil.example/` would otherwise match
130
191
  * `http://localhost:*`), and is written exactly as `URL` reads it back. The
131
- * agent gets the URL as written, so a URL whose reading differs — a backslash,
132
- * a tab, an uppercase host — is refused rather than checked in one form and
133
- * used in another.
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.
134
197
  */
135
- function plainUrl(nodeId, server) {
198
+ function plainUrl(where, server) {
136
199
  const parsed = URL.canParse(server.url) ? new URL(server.url) : undefined;
137
200
  if (!parsed ||
138
201
  (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') ||
139
202
  parsed.username !== '' ||
140
203
  parsed.password !== '') {
141
- throw notAllowed(`Node '${nodeId}' gives the agent 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.');
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.');
142
205
  }
143
206
  if (parsed.href !== server.url) {
144
- throw notAllowed(`Node '${nodeId}' gives the agent MCP server '${server.name}' at ${JSON.stringify(server.url)}, which reads as ${parsed.href}.`, `Write the URL as ${parsed.href}.`);
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}.`);
145
208
  }
146
209
  return parsed;
147
210
  }
211
+ function valuesOf(server) {
212
+ return ('url' in server ? server.headers : server.env) ?? [];
213
+ }
148
214
  function sameArgs(a, b) {
149
215
  return a.length === b.length && a.every((arg, i) => arg === b[i]);
150
216
  }
@@ -161,60 +227,101 @@ function notAllowed(message, hint) {
161
227
  hint,
162
228
  });
163
229
  }
164
- function noResolver() {
230
+ function nameTaken(where, server) {
165
231
  return new NoeticConfigError({
166
- code: 'CONNECTION_INVALID',
167
- message: 'An MCP server uses a $secret but the harness has no secret resolver.',
168
- hint: 'Set `environment.secrets` on the AgentHarness to resolve secret names.',
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.',
169
235
  });
170
236
  }
171
- //#endregion
172
- //#region Secrets
173
- async function resolveServers(servers, ctx) {
174
- const resolver = harnessInternal(ctx.harness, 'secrets');
175
- if (!resolver) {
176
- throw noResolver();
177
- }
178
- const signal = ctx.aborted ? AbortSignal.abort(ctx.abortReason) : new AbortController().signal;
179
- const resolve = async (entry) => ({
180
- name: entry.name,
181
- value: isSecret(entry.value)
182
- ? await resolver.resolve(entry.value.$secret, {
183
- signal,
184
- })
185
- : entry.value,
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.',
186
242
  });
187
- return Promise.all(servers.map(async (server) => withValues(server, await Promise.all(valuesOf(server).map(resolve)))));
188
- }
189
- function valuesOf(server) {
190
- return 'url' in server ? server.headers : server.env;
191
- }
192
- function withValues(server, values) {
193
- return 'url' in server
194
- ? {
195
- ...server,
196
- headers: values,
197
- }
198
- : {
199
- ...server,
200
- env: values,
201
- };
202
- }
203
- /** Whether no header or environment value is a `$secret`, so the server is already an ACP one. */
204
- function isLiteral(server) {
205
- return valuesOf(server).every((entry) => !isSecret(entry.value));
206
- }
207
- function isSecret(value) {
208
- return typeof value !== 'string';
209
243
  }
210
244
  //#endregion
211
245
  //#region Prompt
212
246
  function urlLine(grant) {
213
247
  const headers = Object.entries(grant.secrets ?? {}).map(([secret, header]) => `{ "name": "${header}", "value": { "$secret": "${secret}" } }`);
214
- return `- { "type": "http" or "sse", "name": "<name>", "url": "<URL matching ${grant.url}>", "headers": [${headers.join(', ')}] } (headers optional)`;
248
+ return `- { "mcp": { "name": "<name>", "type": "http" or "sse", "url": "<URL matching ${grant.url}>", "headers": [${headers.join(', ')}] } } (headers optional)`;
215
249
  }
216
250
  function commandLine(grant) {
217
251
  const env = Object.entries(grant.secrets ?? {}).map(([secret, variable]) => `{ "name": "${variable}", "value": { "$secret": "${secret}" } }`);
218
- return `- { "name": "<name>", "command": ${JSON.stringify(grant.command)}, "args": ${JSON.stringify(grant.args ?? [])}, "env": [${env.join(', ')}] } (env optional)`;
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;
219
326
  }
220
327
  //#endregion
@@ -0,0 +1,84 @@
1
+ /**
2
+ * A node's `tools` entries as live step tools (spec 26 § Tools). `{ type }` is
3
+ * a registered client tool or an `openrouter:*` server tool; `{ connection,
4
+ * ops? }` picks a harness or turn connection's model tools; `{ mcp }` is an
5
+ * MCP server the step attaches, built from the host's `mcp` allowlist. A
6
+ * document never sets a connection's policy, and under a `connections`
7
+ * allowlist a pick never reaches past the operations it grants.
8
+ */
9
+ import type { ConnectionAllowlist, ConnectionConfig, ConnectionPick, McpAllowlist, Spawn, Step, StepToolEntry, Tool, WorkflowDocument, WorkflowStepConnectionEntry, WorkflowToolEntry } from '@noetic-tools/types';
10
+ import type { ConnectionManifestEntries } from './workflow-connections.js';
11
+ /** What hydrating a node's `tools` needs from the host. */
12
+ export interface ToolEntryHosts {
13
+ readonly tools: ReadonlyMap<string, Tool>;
14
+ /** What the document may use of each connection; absent allows every connection. */
15
+ readonly allowlist?: ConnectionAllowlist;
16
+ readonly mcp?: McpAllowlist;
17
+ /** Where a stdio `{ mcp }` server starts: the node's ACP agent's machine. Absent starts it here. */
18
+ readonly spawn?: Spawn;
19
+ /** The connections the node's document declares; its `{ mcp }` entries are the document's own servers. */
20
+ readonly connections?: ConnectionManifestEntries;
21
+ }
22
+ /**
23
+ * The step tools a `callModel` or `acp-agent` node stands for. Under a
24
+ * connections allowlist a node gets exactly what it lists; a node with no
25
+ * `tools` has none, and `withCeiling` bounds the connections it is offered.
26
+ * Without an allowlist an empty list is no list.
27
+ *
28
+ * @throws `NoeticConfigError` as `stepTools`.
29
+ */
30
+ export declare function nodeTools(node: {
31
+ readonly id: string;
32
+ readonly tools?: ReadonlyArray<WorkflowToolEntry>;
33
+ }, hosts: ToolEntryHosts): StepToolEntry[] | undefined;
34
+ /**
35
+ * Records on `built`, the step for a node with no `tools` under a
36
+ * connections allowlist, the most of the turn's connections it may be
37
+ * offered: each granted connection with exactly its granted operations, plus
38
+ * the MCP servers its document's `connections` attach — never every
39
+ * connection the harness holds. Its other tools are those of any step with no
40
+ * `tools`. Returns `built`.
41
+ */
42
+ export declare function withCeiling<S extends object>(built: S, node: {
43
+ readonly tools?: ReadonlyArray<WorkflowToolEntry>;
44
+ }, hosts: Pick<ToolEntryHosts, 'allowlist' | 'connections'>): S;
45
+ /**
46
+ * The step tools a `callModel` node's `tools` entries stand for.
47
+ *
48
+ * @throws `NoeticConfigError` `UNKNOWN_TOOL_REFERENCE` for a tool the registry does not hold; the errors of `connectionEntries`.
49
+ */
50
+ export declare function stepTools(nodeId: string, entries: ReadonlyArray<WorkflowToolEntry>, hosts: ToolEntryHosts): StepToolEntry[];
51
+ /**
52
+ * The connection entries and picks a node's `{ connection }` and `{ mcp }`
53
+ * entries stand for. A stdio `{ mcp }` server starts through `hosts.spawn`
54
+ * when given: where the node's ACP agent runs.
55
+ *
56
+ * @throws `NoeticConfigError` `CONNECTION_NOT_ALLOWED` for a server or operation the host does not grant; `CONNECTION_INVALID` when the host gives no `mcp.provider`.
57
+ */
58
+ export declare function connectionEntries(nodeId: string, entries: ReadonlyArray<WorkflowStepConnectionEntry>, hosts: Pick<ToolEntryHosts, 'allowlist' | 'mcp' | 'spawn' | 'connections'>): Array<ConnectionPick | ConnectionConfig>;
59
+ /**
60
+ * Records the `{ "mcp" }` entries of a document's `connections` on the step
61
+ * built for it, so the turn attaches them when the document runs, by the
62
+ * names the document gives them.
63
+ *
64
+ * @throws `NoeticConfigError` as `mcpEntry`.
65
+ */
66
+ export declare function recordDocumentConnections(root: Step, doc: WorkflowDocument, mcp: McpAllowlist | undefined): void;
67
+ /**
68
+ * Checks every `{ connection }` tool entry against the host's allowlist: it
69
+ * names an allowed connection, and each of its `ops` globs stays inside the
70
+ * operations the allowlist grants. Without `ops` the pick takes exactly the
71
+ * granted operations. A pick of an MCP server the document's own
72
+ * `connections` attach answers to the `mcp` allowlist instead.
73
+ *
74
+ * @throws `NoeticConfigError` `CONNECTION_NOT_ALLOWED` for a connection or operation glob the allowlist does not grant.
75
+ */
76
+ export declare function checkToolConnections(doc: WorkflowDocument, allowlist: ConnectionAllowlist): void;
77
+ /** A complete sentence naming what is registered, for a hint. */
78
+ export declare function registeredSentence(noun: string, names: Iterable<string>): string;
79
+ /**
80
+ * Resolves tool names against the registry.
81
+ *
82
+ * @throws `NoeticConfigError` `UNKNOWN_TOOL_REFERENCE` for a name it does not hold.
83
+ */
84
+ export declare function resolveTools(names: ReadonlyArray<string>, registry: ReadonlyMap<string, Tool>): Tool[];