@noetic-tools/acp 1.3.1 → 2.0.0-next.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.
Files changed (58) hide show
  1. package/dist/agents.d.ts +72 -8
  2. package/dist/agents.js +162 -58
  3. package/dist/bin.d.ts +19 -0
  4. package/dist/bin.js +99 -0
  5. package/dist/client.d.ts +14 -0
  6. package/dist/client.js +50 -0
  7. package/dist/connection.d.ts +3 -0
  8. package/dist/connection.js +26 -4
  9. package/dist/define.d.ts +21 -7
  10. package/dist/define.js +139 -15
  11. package/dist/index.d.ts +13 -3
  12. package/dist/index.js +10 -2
  13. package/dist/mcp/node.d.ts +24 -0
  14. package/dist/mcp/node.js +146 -0
  15. package/dist/mcp/server.d.ts +16 -0
  16. package/dist/mcp/server.js +104 -0
  17. package/dist/permission-channel.d.ts +2 -2
  18. package/dist/permission-channel.js +23 -12
  19. package/dist/permissions.d.ts +6 -0
  20. package/dist/permissions.js +7 -0
  21. package/dist/pi/extension.d.ts +31 -0
  22. package/dist/pi/extension.js +223 -0
  23. package/dist/pi/launch.d.ts +11 -0
  24. package/dist/pi/launch.js +35 -0
  25. package/dist/remote.d.ts +52 -0
  26. package/dist/remote.js +108 -0
  27. package/dist/runner/grants.d.ts +47 -0
  28. package/dist/runner/grants.js +127 -0
  29. package/dist/runner/host.d.ts +21 -0
  30. package/dist/runner/host.js +318 -0
  31. package/dist/runner/options.d.ts +21 -0
  32. package/dist/runner/options.js +82 -0
  33. package/dist/runner/protocol.d.ts +101 -0
  34. package/dist/runner/protocol.js +218 -0
  35. package/dist/runner/serve.d.ts +60 -0
  36. package/dist/runner/serve.js +285 -0
  37. package/dist/runner/ws-link.d.ts +6 -0
  38. package/dist/runner/ws-link.js +47 -0
  39. package/dist/serve-permissions.d.ts +8 -2
  40. package/dist/serve-permissions.js +26 -5
  41. package/dist/serve-types.d.ts +6 -5
  42. package/dist/server-env.d.ts +10 -0
  43. package/dist/server-env.js +47 -0
  44. package/dist/spawn.d.ts +12 -0
  45. package/dist/spawn.js +37 -0
  46. package/dist/stdio.d.ts +9 -1
  47. package/dist/stdio.js +97 -55
  48. package/dist/tool-bridge.d.ts +67 -0
  49. package/dist/tool-bridge.js +62 -0
  50. package/dist/tool-permissions.d.ts +46 -0
  51. package/dist/tool-permissions.js +70 -0
  52. package/dist/tool-wiring.d.ts +46 -0
  53. package/dist/tool-wiring.js +203 -0
  54. package/dist/transport-loopback.d.ts +8 -1
  55. package/dist/transport-loopback.js +22 -1
  56. package/dist/turn.d.ts +11 -5
  57. package/dist/turn.js +39 -16
  58. package/package.json +17 -4
@@ -11,7 +11,8 @@
11
11
  */
12
12
  import { frameworkCast } from '@noetic-tools/types';
13
13
  //#region Policy evaluation
14
- const DEFAULT_ASK_TIMEOUT_MS = 5 * 60 * 1e3;
14
+ /** The longest delay a timer takes: about 24.8 days. */
15
+ const MAX_HOOK_TIMEOUT_MS = 2 ** 31 - 1;
15
16
  /**
16
17
  * First decisive rule wins, checked `deny` → `ask` → `allow`; the policy
17
18
  * `default` (or `allow`) applies when nothing matches.
@@ -47,6 +48,7 @@ export class ServePermissionBroker {
47
48
  constructor(forward) {
48
49
  this.forward = forward;
49
50
  }
51
+ /** Ask the client. With no `timeoutMs` the ask waits for an answer or a session cancel. */
50
52
  async ask(prompt, timeoutMs) {
51
53
  let timer;
52
54
  const parked = new Promise((resolve) => {
@@ -54,10 +56,14 @@ export class ServePermissionBroker {
54
56
  sessionId: prompt.sessionId,
55
57
  resolve,
56
58
  });
59
+ if (timeoutMs === undefined) {
60
+ return;
61
+ }
57
62
  timer = setTimeout(() => {
58
63
  this.answer(prompt.requestId, {
59
64
  decision: 'deny',
60
65
  reason: 'permission request timed out',
66
+ timedOut: true,
61
67
  });
62
68
  }, timeoutMs);
63
69
  });
@@ -110,7 +116,7 @@ export class ServePermissionBroker {
110
116
  */
111
117
  export function createServePermissionLayer(params) {
112
118
  const { sessionId, policy, broker, presentation } = params;
113
- const askTimeoutMs = policy.askTimeoutMs ?? DEFAULT_ASK_TIMEOUT_MS;
119
+ const { askTimeoutMs } = policy;
114
120
  const beforeToolCall = async (hookParams) => {
115
121
  const { toolName, toolArgs, callId } = hookParams;
116
122
  const kind = presentation.kindOf(toolName);
@@ -143,6 +149,20 @@ export function createServePermissionLayer(params) {
143
149
  kind,
144
150
  args: toolArgs,
145
151
  }, askTimeoutMs);
152
+ // Nobody at the editor answered. Rather than refuse, hand the call to the
153
+ // harness's approval seam, where a person can still answer it (spec 08 § Held
154
+ // tool calls). The editor's turn is waiting on it, so the turn stays live.
155
+ const approve = hookParams.ctx?.approve;
156
+ if (reply.timedOut && approve && callId !== undefined) {
157
+ return {
158
+ decision: await approve({
159
+ toolName,
160
+ toolArgs,
161
+ callId,
162
+ park: false,
163
+ }),
164
+ };
165
+ }
146
166
  if (reply.decision === 'allow') {
147
167
  return {
148
168
  decision: {
@@ -167,9 +187,10 @@ export function createServePermissionLayer(params) {
167
187
  beforeToolCall,
168
188
  },
169
189
  timeouts: {
170
- // The broker owns the real deadline (askTimeoutMs → deny); this outer
171
- // lifecycle timeout only backstops a wedged broker.
172
- beforeToolCall: askTimeoutMs + 10e3,
190
+ // The broker owns the editor's deadline (askTimeoutMs, none unless set). After it
191
+ // a call may be held for a person through the harness, for as long as that takes, so the
192
+ // lifecycle timeout is the longest a timer allows and only backstops a wedge.
193
+ beforeToolCall: MAX_HOOK_TIMEOUT_MS,
173
194
  },
174
195
  // A gate fails CLOSED: if the hook ever throws or blows the backstop
175
196
  // timeout, the lifecycle denies the call rather than abstaining into the
@@ -5,7 +5,7 @@
5
5
  * `@noetic-tools/chat-sdk` uses — so this package keeps depending only on
6
6
  * `@noetic-tools/types`.
7
7
  */
8
- import type { AcpMcpServer, AcpToolKind, ExecuteInput, ExecuteOptions, FsAdapter, Item, SessionScope, ShellAdapter, StreamEvent, StreamingItem, ToolAcpDeclaration } from '@noetic-tools/types';
8
+ import type { AcpMcpServer, AcpToolKind, ExecuteArgument, ExecuteOptions, FsAdapter, Item, SessionScope, ShellAdapter, StreamEvent, StreamingItem, ToolAcpDeclaration } from '@noetic-tools/types';
9
9
  import type * as acp from '@zed-industries/agent-client-protocol';
10
10
  /**
11
11
  * The slice of `AgentHarnessContract` the ACP server drives. Any harness (or
@@ -13,7 +13,7 @@ import type * as acp from '@zed-industries/agent-client-protocol';
13
13
  * @public
14
14
  */
15
15
  export interface AcpServeHarness {
16
- execute(input: ExecuteInput, options?: ExecuteOptions): Promise<void>;
16
+ execute(input: ExecuteArgument, options?: ExecuteOptions): Promise<void>;
17
17
  getFullStream(scope?: SessionScope): AsyncIterable<StreamEvent>;
18
18
  getItemStream(scope?: SessionScope): AsyncIterable<StreamingItem>;
19
19
  seedSessionHistory(threadId: string, items: ReadonlyArray<Item>): void;
@@ -77,9 +77,10 @@ export interface AcpServePermissionPolicy {
77
77
  default?: AcpServePermissionDecision;
78
78
  rules?: ReadonlyArray<AcpServePermissionRule>;
79
79
  /**
80
- * How long an `ask` waits for the client's answer, in milliseconds.
81
- * Defaults to 5 minutes. On expiry the call is denied — waiting must not
82
- * become approval.
80
+ * How long an `ask` waits for the client's answer, in milliseconds. With
81
+ * none, the ask waits until the client answers or the turn is cancelled. On
82
+ * expiry the call goes to the harness's approval seam, and is denied when
83
+ * there is none: waiting must not become approval.
83
84
  */
84
85
  askTimeoutMs?: number;
85
86
  }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The environment a stdio MCP server starts with, wherever it starts. The MCP
3
+ * SDK's stdio client passes a server only a short list of this process's
4
+ * variables (`getDefaultEnvironment()`), so a server noetic starts on an
5
+ * agent's machine gets the same list from that machine, never the rest of
6
+ * its environment. The list is the SDK's (sudo's defaults):
7
+ * https://github.com/modelcontextprotocol/typescript-sdk/blob/main/src/client/stdio.ts
8
+ */
9
+ /** This process's inherited variables, with `env` over them. */
10
+ export declare function serverEnv(env: Readonly<Record<string, string | undefined>> | undefined): Record<string, string | undefined>;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The environment a stdio MCP server starts with, wherever it starts. The MCP
3
+ * SDK's stdio client passes a server only a short list of this process's
4
+ * variables (`getDefaultEnvironment()`), so a server noetic starts on an
5
+ * agent's machine gets the same list from that machine, never the rest of
6
+ * its environment. The list is the SDK's (sudo's defaults):
7
+ * https://github.com/modelcontextprotocol/typescript-sdk/blob/main/src/client/stdio.ts
8
+ */
9
+ const INHERITED = process.platform === 'win32'
10
+ ? [
11
+ 'APPDATA',
12
+ 'HOMEDRIVE',
13
+ 'HOMEPATH',
14
+ 'LOCALAPPDATA',
15
+ 'PATH',
16
+ 'PROCESSOR_ARCHITECTURE',
17
+ 'SYSTEMDRIVE',
18
+ 'SYSTEMROOT',
19
+ 'TEMP',
20
+ 'USERNAME',
21
+ 'USERPROFILE',
22
+ 'PROGRAMFILES',
23
+ ]
24
+ : [
25
+ 'HOME',
26
+ 'LOGNAME',
27
+ 'PATH',
28
+ 'SHELL',
29
+ 'TERM',
30
+ 'USER',
31
+ ];
32
+ /** This process's inherited variables, with `env` over them. */
33
+ export function serverEnv(env) {
34
+ const inherited = {};
35
+ for (const name of INHERITED) {
36
+ const value = process.env[name];
37
+ // A shell function exported as a variable runs in every shell the server starts.
38
+ if (value === undefined || value.startsWith('()')) {
39
+ continue;
40
+ }
41
+ inherited[name] = value;
42
+ }
43
+ return {
44
+ ...inherited,
45
+ ...env,
46
+ };
47
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Start a process on an agent's machine and hold its stdio — the seam a stdio
3
+ * MCP server that depends on that machine is started through, with Noetic
4
+ * still its MCP client on the host.
5
+ */
6
+ import type { AcpMachineFactory, Spawn } from '@noetic-tools/types';
7
+ /**
8
+ * Each call opens the machine, starts `command` there, and releases the
9
+ * machine when the process's stream closes.
10
+ * @public
11
+ */
12
+ export declare function spawnOn(machine: AcpMachineFactory, signal?: AbortSignal): Spawn;
package/dist/spawn.js ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Start a process on an agent's machine and hold its stdio — the seam a stdio
3
+ * MCP server that depends on that machine is started through, with Noetic
4
+ * still its MCP client on the host.
5
+ */
6
+ /**
7
+ * Each call opens the machine, starts `command` there, and releases the
8
+ * machine when the process's stream closes.
9
+ * @public
10
+ */
11
+ export function spawnOn(machine, signal) {
12
+ return async (command) => {
13
+ const opened = await machine({
14
+ signal,
15
+ });
16
+ let process;
17
+ try {
18
+ process = await opened.spawn(command);
19
+ }
20
+ catch (e) {
21
+ await opened.close().catch(() => undefined);
22
+ throw e;
23
+ }
24
+ return {
25
+ readable: process.readable,
26
+ writable: process.writable,
27
+ async close() {
28
+ try {
29
+ await process.close();
30
+ }
31
+ finally {
32
+ await opened.close();
33
+ }
34
+ },
35
+ };
36
+ };
37
+ }
package/dist/stdio.d.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  * lazy dynamic import, so `claudeCode()` works with no extra wiring while
8
8
  * keeping the dependency off the main graph.
9
9
  */
10
- import type { AcpTransportFactory } from '@noetic-tools/types';
10
+ import type { AcpMachineFactory, AcpTransportFactory } from '@noetic-tools/types';
11
11
  /** @public How to launch an ACP agent as a child process. */
12
12
  export interface StdioAgentSpec {
13
13
  /** Identifier used in error messages. */
@@ -25,3 +25,11 @@ export interface StdioAgentSpec {
25
25
  * @public
26
26
  */
27
27
  export declare function stdioAcpTransport(spec: StdioAgentSpec): AcpTransportFactory;
28
+ /**
29
+ * This machine: the bridge is reached on its own loopback address, and the
30
+ * agent and any MCP server beside it run as child processes. The agent gets
31
+ * this process's environment; an MCP server, as the MCP SDK starts one, only
32
+ * `serverEnv`'s short list.
33
+ * @public
34
+ */
35
+ export declare function localMachine(agentId?: string): AcpMachineFactory;
package/dist/stdio.js CHANGED
@@ -10,6 +10,7 @@
10
10
  import { spawn } from 'node:child_process';
11
11
  import { Readable, Writable } from 'node:stream';
12
12
  import { AcpConnectError, frameworkCast } from '@noetic-tools/types';
13
+ import { serverEnv } from './server-env.js';
13
14
  /**
14
15
  * Build a transport factory that spawns `spec.command` and speaks ACP over its
15
16
  * stdin/stdout. The child's stderr is inherited so agent diagnostics reach the
@@ -17,66 +18,107 @@ import { AcpConnectError, frameworkCast } from '@noetic-tools/types';
17
18
  * @public
18
19
  */
19
20
  export function stdioAcpTransport(spec) {
20
- return async (opts) => {
21
- const child = spawn(spec.command, [
22
- ...(spec.args ?? []),
23
- ], {
24
- cwd: opts.cwd,
25
- env: {
26
- ...process.env,
27
- ...spec.env,
28
- ...opts.env,
29
- },
30
- stdio: [
31
- 'pipe',
32
- 'pipe',
33
- 'inherit',
34
- ],
35
- });
36
- const stdin = child.stdin;
37
- const stdout = child.stdout;
38
- if (!stdin || !stdout) {
39
- child.kill();
40
- throw new AcpConnectError({
41
- agentId: spec.agentId,
42
- message: `ACP agent '${spec.agentId}' was spawned without usable stdio pipes.`,
43
- });
44
- }
45
- // A missing binary surfaces asynchronously on the child, long after spawn()
46
- // returned. Capture it so the failure is reported against the agent rather
47
- // than escaping as an unhandled 'error' event.
48
- let spawnError;
49
- child.once('error', (error) => {
50
- spawnError = error;
51
- });
52
- const onAbort = () => {
53
- child.kill();
21
+ return async (opts) => spawnStdio(spec.agentId, {
22
+ command: spec.command,
23
+ args: spec.args,
24
+ env: {
25
+ ...process.env,
26
+ ...spec.env,
27
+ ...opts.env,
28
+ },
29
+ cwd: opts.cwd,
30
+ }, opts.signal);
31
+ }
32
+ /**
33
+ * This machine: the bridge is reached on its own loopback address, and the
34
+ * agent and any MCP server beside it run as child processes. The agent gets
35
+ * this process's environment; an MCP server, as the MCP SDK starts one, only
36
+ * `serverEnv`'s short list.
37
+ * @public
38
+ */
39
+ export function localMachine(agentId = 'local') {
40
+ return async ({ signal }) => {
41
+ const started = new Set();
42
+ const start = async (command, env) => {
43
+ const transport = await spawnStdio(agentId, {
44
+ ...command,
45
+ env,
46
+ }, signal);
47
+ started.add(transport);
48
+ return transport;
54
49
  };
55
- opts.signal?.addEventListener('abort', onAbort, {
56
- once: true,
57
- });
58
- let closed = false;
59
50
  return {
60
- // `node:stream`'s web-stream types are structurally the same as the
61
- // global ones but nominally distinct, so the bridge is cast once here
62
- // rather than leaking a Node-specific type through the transport contract.
63
- readable: frameworkCast(Readable.toWeb(stdout)),
64
- writable: frameworkCast(Writable.toWeb(stdin)),
51
+ reach: async (bridge) => bridge,
52
+ launch: (command) => start(command, {
53
+ ...process.env,
54
+ ...command.env,
55
+ }),
56
+ spawn: (command) => start(command, serverEnv(command.env)),
65
57
  async close() {
66
- if (closed) {
67
- return;
68
- }
69
- closed = true;
70
- opts.signal?.removeEventListener('abort', onAbort);
71
- child.kill();
72
- if (spawnError) {
73
- throw new AcpConnectError({
74
- agentId: spec.agentId,
75
- message: `ACP agent '${spec.agentId}' (${spec.command}) failed to start.`,
76
- cause: spawnError,
77
- });
58
+ for (const transport of started) {
59
+ await transport.close().catch(() => undefined);
78
60
  }
61
+ started.clear();
79
62
  },
80
63
  };
81
64
  };
82
65
  }
66
+ /** Spawn `command` with exactly its `env` and carry its stdin/stdout as a byte stream. */
67
+ async function spawnStdio(agentId, command, signal) {
68
+ const child = spawn(command.command, [
69
+ ...(command.args ?? []),
70
+ ], {
71
+ cwd: command.cwd,
72
+ env: command.env,
73
+ stdio: [
74
+ 'pipe',
75
+ 'pipe',
76
+ 'inherit',
77
+ ],
78
+ });
79
+ const stdin = child.stdin;
80
+ const stdout = child.stdout;
81
+ if (!stdin || !stdout) {
82
+ child.kill();
83
+ throw new AcpConnectError({
84
+ agentId,
85
+ message: `ACP agent '${agentId}' was spawned without usable stdio pipes.`,
86
+ });
87
+ }
88
+ // A missing binary surfaces asynchronously on the child, long after spawn()
89
+ // returned. Capture it so the failure is reported against the agent rather
90
+ // than escaping as an unhandled 'error' event.
91
+ let spawnError;
92
+ child.once('error', (error) => {
93
+ spawnError = error;
94
+ });
95
+ const onAbort = () => {
96
+ child.kill();
97
+ };
98
+ signal?.addEventListener('abort', onAbort, {
99
+ once: true,
100
+ });
101
+ let closed = false;
102
+ return {
103
+ // `node:stream`'s web-stream types are structurally the same as the
104
+ // global ones but nominally distinct, so the bridge is cast once here
105
+ // rather than leaking a Node-specific type through the transport contract.
106
+ readable: frameworkCast(Readable.toWeb(stdout)),
107
+ writable: frameworkCast(Writable.toWeb(stdin)),
108
+ async close() {
109
+ if (closed) {
110
+ return;
111
+ }
112
+ closed = true;
113
+ signal?.removeEventListener('abort', onAbort);
114
+ child.kill();
115
+ if (spawnError) {
116
+ throw new AcpConnectError({
117
+ agentId,
118
+ message: `ACP agent '${agentId}' (${command.command}) failed to start.`,
119
+ cause: spawnError,
120
+ });
121
+ }
122
+ },
123
+ };
124
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Serves a step's Noetic tools to the agent over MCP, configured so the
3
+ * agent's model sees them exactly as a `callModel` model would.
4
+ *
5
+ * Out of the box agents hide MCP tools behind a search or code step, cut large
6
+ * results, and run their own approval review. Each preset's
7
+ * {@link AcpToolWiring} turns those off. Some agents only read those settings
8
+ * at launch, so the bridge starts before the agent process does.
9
+ */
10
+ import type { AcpMcpServer, AcpTool, AcpToolHost, ToolBridgeAddress } from '@noetic-tools/types';
11
+ import type { BridgedPermissionCheck } from './tool-permissions.js';
12
+ export type { ToolBridgeAddress } from '@noetic-tools/types';
13
+ /** @public What a preset's launch settings are built from. */
14
+ export interface ToolBridgeLaunch {
15
+ /** The bridge as the agent reaches it, from its own machine. */
16
+ readonly bridge: ToolBridgeAddress;
17
+ readonly tools: ReadonlyArray<AcpTool>;
18
+ /** The user's own MCP servers for the session. */
19
+ readonly mcpServers: ReadonlyArray<AcpMcpServer>;
20
+ /** The preset's environment, to merge into rather than replace. */
21
+ readonly env: Record<string, string | undefined>;
22
+ }
23
+ /**
24
+ * How one agent takes the bridge.
25
+ * @public
26
+ */
27
+ export interface AcpToolWiring {
28
+ /**
29
+ * `'session'`: the bridge joins the `session/new` MCP servers.
30
+ * `'launch'`: `launchEnv` declares every MCP server — the bridge and the
31
+ * user's own — and `session/new` carries none. For agents that drop
32
+ * per-server settings from servers given in `session/new`.
33
+ */
34
+ readonly declare: 'session' | 'launch';
35
+ /** Environment for the agent process that switches its tool handling to match `callModel`. */
36
+ launchEnv?(launch: ToolBridgeLaunch): Record<string, string>;
37
+ /**
38
+ * Recognizes this agent's permission requests for bridged tools, which are
39
+ * then allowed. Without one, every request goes to the step's policy.
40
+ */
41
+ readonly permits?: BridgedPermissionCheck;
42
+ }
43
+ /** A running bridge, owned by one connection. */
44
+ export interface ToolBridge {
45
+ readonly declare: AcpToolWiring['declare'];
46
+ /** The MCP server entry for `session/new`. */
47
+ readonly server: AcpMcpServer;
48
+ readonly env: Record<string, string>;
49
+ readonly permits?: BridgedPermissionCheck;
50
+ close(): Promise<void>;
51
+ }
52
+ /**
53
+ * Start the loopback MCP server, carry it to the agent's machine, and work
54
+ * out the agent's launch settings from the address it reaches there. The
55
+ * node-only server module loads on first use, so the package's main entry
56
+ * stays runtime-neutral.
57
+ */
58
+ export declare function startToolBridge(opts: {
59
+ wiring: AcpToolWiring;
60
+ getHost: () => AcpToolHost | undefined;
61
+ serverName: string;
62
+ tools: ReadonlyArray<AcpTool>;
63
+ mcpServers: ReadonlyArray<AcpMcpServer>;
64
+ env?: Record<string, string | undefined>;
65
+ /** Carries the bridge to the agent's machine; this machine's loopback by default. */
66
+ reach?: (bridge: ToolBridgeAddress) => Promise<ToolBridgeAddress>;
67
+ }): Promise<ToolBridge>;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Serves a step's Noetic tools to the agent over MCP, configured so the
3
+ * agent's model sees them exactly as a `callModel` model would.
4
+ *
5
+ * Out of the box agents hide MCP tools behind a search or code step, cut large
6
+ * results, and run their own approval review. Each preset's
7
+ * {@link AcpToolWiring} turns those off. Some agents only read those settings
8
+ * at launch, so the bridge starts before the agent process does.
9
+ */
10
+ //#endregion
11
+ //#region Public API
12
+ /**
13
+ * Start the loopback MCP server, carry it to the agent's machine, and work
14
+ * out the agent's launch settings from the address it reaches there. The
15
+ * node-only server module loads on first use, so the package's main entry
16
+ * stays runtime-neutral.
17
+ */
18
+ export async function startToolBridge(opts) {
19
+ const { serveToolsOverHttp } = await import('./mcp/node.js');
20
+ const http = await serveToolsOverHttp(opts.getHost);
21
+ const local = {
22
+ serverName: opts.serverName,
23
+ url: http.url,
24
+ headers: [
25
+ {
26
+ name: 'Authorization',
27
+ value: `Bearer ${http.token}`,
28
+ },
29
+ ],
30
+ };
31
+ let bridge;
32
+ let env;
33
+ try {
34
+ bridge = opts.reach ? await opts.reach(local) : local;
35
+ env =
36
+ opts.wiring.launchEnv?.({
37
+ bridge,
38
+ tools: opts.tools,
39
+ mcpServers: opts.mcpServers,
40
+ env: opts.env ?? {},
41
+ }) ?? {};
42
+ }
43
+ catch (e) {
44
+ await http.close();
45
+ throw e;
46
+ }
47
+ return {
48
+ declare: opts.wiring.declare,
49
+ server: {
50
+ type: 'http',
51
+ name: bridge.serverName,
52
+ url: bridge.url,
53
+ headers: [
54
+ ...bridge.headers,
55
+ ],
56
+ },
57
+ env,
58
+ permits: opts.wiring.permits,
59
+ close: () => http.close(),
60
+ };
61
+ }
62
+ //#endregion
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Recognizes an agent's permission request for one of the bridged Noetic tools.
3
+ *
4
+ * A bridged tool is gated by the same steering and `needsApproval` checks as
5
+ * in `callModel`, when the call reaches the bridge. The agent's own
6
+ * permission request is a second gate `callModel` never has, so it is
7
+ * answered `allow` without consulting the step's ACP permission policy.
8
+ *
9
+ * Because that skips the policy, each agent is recognized only by fields its
10
+ * own adapter sets, never by a title alone: some agents title a call with
11
+ * text the model wrote (a subagent's description), which could spell a
12
+ * bridged tool's name. An agent with no such field gets no automatic answer;
13
+ * its requests go to the step's policy like any other.
14
+ */
15
+ import type { AcpPermissionOption, AcpRequestPermissionRequest, AcpToolHost } from '@noetic-tools/types';
16
+ type ToolCallUpdate = AcpRequestPermissionRequest['toolCall'];
17
+ /**
18
+ * True when a permission request is for one of the host's tools. `announced`
19
+ * is the agent's earlier `tool_call` update for the same id.
20
+ * @public
21
+ */
22
+ export type BridgedPermissionCheck = (request: AcpRequestPermissionRequest, host: AcpToolHost, announced: ToolCallUpdate | undefined) => boolean;
23
+ /**
24
+ * Claude Code names the tool it runs in `_meta.claudeCode.toolName`, and the
25
+ * MCP server in `_meta.claudeCode.mcpServer.name` — both set by its adapter,
26
+ * not the model.
27
+ * @public
28
+ */
29
+ export declare const claudeBridgedPermission: BridgedPermissionCheck;
30
+ /**
31
+ * Codex marks an MCP approval with `_meta.is_mcp_tool_approval` and sends no
32
+ * title; the `tool_call` it announced for the same id carries
33
+ * `_meta.is_mcp_tool_call` and the title `mcp.<server>.<tool>`, which Codex
34
+ * builds from the server and tool it is calling.
35
+ * @public
36
+ */
37
+ export declare const codexBridgedPermission: BridgedPermissionCheck;
38
+ /**
39
+ * Gemini CLI titles a call with display text, but builds its id from the
40
+ * registered tool name: `mcp_<server>_<tool>__call_<n>`.
41
+ * @public
42
+ */
43
+ export declare const geminiBridgedPermission: BridgedPermissionCheck;
44
+ /** The option that allows the call once, falling back to any allow option. */
45
+ export declare function allowOnceOption(options: ReadonlyArray<AcpPermissionOption>): AcpPermissionOption | undefined;
46
+ export {};
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Recognizes an agent's permission request for one of the bridged Noetic tools.
3
+ *
4
+ * A bridged tool is gated by the same steering and `needsApproval` checks as
5
+ * in `callModel`, when the call reaches the bridge. The agent's own
6
+ * permission request is a second gate `callModel` never has, so it is
7
+ * answered `allow` without consulting the step's ACP permission policy.
8
+ *
9
+ * Because that skips the policy, each agent is recognized only by fields its
10
+ * own adapter sets, never by a title alone: some agents title a call with
11
+ * text the model wrote (a subagent's description), which could spell a
12
+ * bridged tool's name. An agent with no such field gets no automatic answer;
13
+ * its requests go to the step's policy like any other.
14
+ */
15
+ import { bridgedToolTitles, geminiToolNameMatches } from '@noetic-tools/types';
16
+ //#endregion
17
+ //#region Helpers
18
+ function recordOf(value) {
19
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
20
+ return undefined;
21
+ }
22
+ return Object.fromEntries(Object.entries(value));
23
+ }
24
+ /** The exact titles `form` gives the host's tools. */
25
+ function titlesOf(host, form) {
26
+ return new Set(host.tools.flatMap((tool) => bridgedToolTitles(host.serverName, tool.name)[form] ?? []));
27
+ }
28
+ const CLAUDE_FORM = 0;
29
+ const CODEX_FORM = 1;
30
+ //#endregion
31
+ //#region Public API
32
+ /**
33
+ * Claude Code names the tool it runs in `_meta.claudeCode.toolName`, and the
34
+ * MCP server in `_meta.claudeCode.mcpServer.name` — both set by its adapter,
35
+ * not the model.
36
+ * @public
37
+ */
38
+ export const claudeBridgedPermission = (request, host) => {
39
+ const claudeCode = recordOf(recordOf(request.toolCall._meta)?.claudeCode);
40
+ const toolName = claudeCode?.toolName;
41
+ return (recordOf(claudeCode?.mcpServer)?.name === host.serverName &&
42
+ typeof toolName === 'string' &&
43
+ titlesOf(host, CLAUDE_FORM).has(toolName));
44
+ };
45
+ /**
46
+ * Codex marks an MCP approval with `_meta.is_mcp_tool_approval` and sends no
47
+ * title; the `tool_call` it announced for the same id carries
48
+ * `_meta.is_mcp_tool_call` and the title `mcp.<server>.<tool>`, which Codex
49
+ * builds from the server and tool it is calling.
50
+ * @public
51
+ */
52
+ export const codexBridgedPermission = (request, host, announced) => {
53
+ const title = announced?.title;
54
+ return (recordOf(request._meta)?.is_mcp_tool_approval === true &&
55
+ recordOf(announced?._meta)?.is_mcp_tool_call === true &&
56
+ typeof title === 'string' &&
57
+ titlesOf(host, CODEX_FORM).has(title));
58
+ };
59
+ /**
60
+ * Gemini CLI titles a call with display text, but builds its id from the
61
+ * registered tool name: `mcp_<server>_<tool>__call_<n>`.
62
+ * @public
63
+ */
64
+ export const geminiBridgedPermission = (request, host) => host.tools.some((tool) => geminiToolNameMatches(host.serverName, tool.name, request.toolCall.toolCallId));
65
+ /** The option that allows the call once, falling back to any allow option. */
66
+ export function allowOnceOption(options) {
67
+ return (options.find((option) => option.kind === 'allow_once') ??
68
+ options.find((option) => option.kind === 'allow_always'));
69
+ }
70
+ //#endregion