@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.
- package/dist/agents.d.ts +72 -8
- package/dist/agents.js +162 -58
- package/dist/bin.d.ts +19 -0
- package/dist/bin.js +99 -0
- package/dist/client.d.ts +14 -0
- package/dist/client.js +50 -0
- package/dist/connection.d.ts +3 -0
- package/dist/connection.js +26 -4
- package/dist/define.d.ts +21 -7
- package/dist/define.js +139 -15
- package/dist/index.d.ts +13 -3
- package/dist/index.js +10 -2
- package/dist/mcp/node.d.ts +24 -0
- package/dist/mcp/node.js +146 -0
- package/dist/mcp/server.d.ts +16 -0
- package/dist/mcp/server.js +104 -0
- package/dist/permission-channel.d.ts +2 -2
- package/dist/permission-channel.js +23 -12
- package/dist/permissions.d.ts +6 -0
- package/dist/permissions.js +7 -0
- package/dist/pi/extension.d.ts +31 -0
- package/dist/pi/extension.js +223 -0
- package/dist/pi/launch.d.ts +11 -0
- package/dist/pi/launch.js +35 -0
- package/dist/remote.d.ts +52 -0
- package/dist/remote.js +108 -0
- package/dist/runner/grants.d.ts +47 -0
- package/dist/runner/grants.js +127 -0
- package/dist/runner/host.d.ts +21 -0
- package/dist/runner/host.js +318 -0
- package/dist/runner/options.d.ts +21 -0
- package/dist/runner/options.js +82 -0
- package/dist/runner/protocol.d.ts +101 -0
- package/dist/runner/protocol.js +218 -0
- package/dist/runner/serve.d.ts +60 -0
- package/dist/runner/serve.js +285 -0
- package/dist/runner/ws-link.d.ts +6 -0
- package/dist/runner/ws-link.js +47 -0
- package/dist/serve-permissions.d.ts +8 -2
- package/dist/serve-permissions.js +26 -5
- package/dist/serve-types.d.ts +6 -5
- package/dist/server-env.d.ts +10 -0
- package/dist/server-env.js +47 -0
- package/dist/spawn.d.ts +12 -0
- package/dist/spawn.js +37 -0
- package/dist/stdio.d.ts +9 -1
- package/dist/stdio.js +97 -55
- package/dist/tool-bridge.d.ts +67 -0
- package/dist/tool-bridge.js +62 -0
- package/dist/tool-permissions.d.ts +46 -0
- package/dist/tool-permissions.js +70 -0
- package/dist/tool-wiring.d.ts +46 -0
- package/dist/tool-wiring.js +203 -0
- package/dist/transport-loopback.d.ts +8 -1
- package/dist/transport-loopback.js +22 -1
- package/dist/turn.d.ts +11 -5
- package/dist/turn.js +39 -16
- package/package.json +17 -4
package/dist/connection.js
CHANGED
|
@@ -187,8 +187,9 @@ class AcpAgentConnectionImpl {
|
|
|
187
187
|
authMethods;
|
|
188
188
|
protocolVersion;
|
|
189
189
|
watch;
|
|
190
|
+
tools;
|
|
190
191
|
sessions = new Map();
|
|
191
|
-
constructor(agentId, agent, transport, client, agentCapabilities, authMethods, protocolVersion, watch) {
|
|
192
|
+
constructor(agentId, agent, transport, client, agentCapabilities, authMethods, protocolVersion, watch, tools) {
|
|
192
193
|
this.agentId = agentId;
|
|
193
194
|
this.agent = agent;
|
|
194
195
|
this.transport = transport;
|
|
@@ -197,6 +198,7 @@ class AcpAgentConnectionImpl {
|
|
|
197
198
|
this.authMethods = authMethods;
|
|
198
199
|
this.protocolVersion = protocolVersion;
|
|
199
200
|
this.watch = watch;
|
|
201
|
+
this.tools = tools;
|
|
200
202
|
}
|
|
201
203
|
/** The sink the client hands every `session/update` notification. */
|
|
202
204
|
get sink() {
|
|
@@ -216,7 +218,7 @@ class AcpAgentConnectionImpl {
|
|
|
216
218
|
const response = await Promise.race([
|
|
217
219
|
this.agent.newSession({
|
|
218
220
|
cwd: opts.cwd,
|
|
219
|
-
mcpServers: this.
|
|
221
|
+
mcpServers: this.sessionMcpServers(opts.mcpServers),
|
|
220
222
|
}),
|
|
221
223
|
this.watch.death,
|
|
222
224
|
]);
|
|
@@ -236,7 +238,7 @@ class AcpAgentConnectionImpl {
|
|
|
236
238
|
this.agent.loadSession({
|
|
237
239
|
sessionId: opts.sessionId,
|
|
238
240
|
cwd: opts.cwd,
|
|
239
|
-
mcpServers: this.
|
|
241
|
+
mcpServers: this.sessionMcpServers(opts.mcpServers),
|
|
240
242
|
}),
|
|
241
243
|
this.watch.death,
|
|
242
244
|
]);
|
|
@@ -251,12 +253,31 @@ class AcpAgentConnectionImpl {
|
|
|
251
253
|
// for another one during teardown.
|
|
252
254
|
await this.transport.close().catch(() => undefined);
|
|
253
255
|
await this.client.dispose();
|
|
256
|
+
await this.tools?.close().catch(() => undefined);
|
|
254
257
|
}
|
|
255
258
|
//#region internals
|
|
256
259
|
track(session) {
|
|
257
260
|
this.sessions.set(session.sessionId, session);
|
|
258
261
|
return session;
|
|
259
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* The servers `session/new` carries: the user's plus the tool bridge, or
|
|
265
|
+
* none when the agent was given them all at launch.
|
|
266
|
+
*/
|
|
267
|
+
sessionMcpServers(servers) {
|
|
268
|
+
if (this.tools?.declare === 'launch') {
|
|
269
|
+
this.checkedMcpServers(servers);
|
|
270
|
+
return [];
|
|
271
|
+
}
|
|
272
|
+
return this.checkedMcpServers([
|
|
273
|
+
...(servers ?? []),
|
|
274
|
+
...(this.tools
|
|
275
|
+
? [
|
|
276
|
+
this.tools.server,
|
|
277
|
+
]
|
|
278
|
+
: []),
|
|
279
|
+
]);
|
|
280
|
+
}
|
|
260
281
|
/** Reject HTTP/SSE MCP transports the agent never advertised support for. */
|
|
261
282
|
checkedMcpServers(servers) {
|
|
262
283
|
if (!servers || servers.length === 0) {
|
|
@@ -302,6 +323,7 @@ export async function openAcpConnection(opts) {
|
|
|
302
323
|
const host = opts.host;
|
|
303
324
|
const client = new NoeticAcpClient({
|
|
304
325
|
host,
|
|
326
|
+
permitsBridged: opts.tools?.permits,
|
|
305
327
|
onNotify: (notification) => {
|
|
306
328
|
sink?.(notification);
|
|
307
329
|
host.onSessionUpdate(notification);
|
|
@@ -330,7 +352,7 @@ export async function openAcpConnection(opts) {
|
|
|
330
352
|
cause: error,
|
|
331
353
|
});
|
|
332
354
|
}
|
|
333
|
-
const connection = new AcpAgentConnectionImpl(opts.agentId, agent, opts.transport, client, initialized.agentCapabilities ?? {}, initialized.authMethods ?? [], initialized.protocolVersion, watch);
|
|
355
|
+
const connection = new AcpAgentConnectionImpl(opts.agentId, agent, opts.transport, client, initialized.agentCapabilities ?? {}, initialized.authMethods ?? [], initialized.protocolVersion, watch, opts.tools);
|
|
334
356
|
sink = connection.sink;
|
|
335
357
|
return connection;
|
|
336
358
|
}
|
package/dist/define.d.ts
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
* `defineAcpAgent` — the one-call constructor behind every agent preset.
|
|
3
3
|
*
|
|
4
4
|
* Under ACP an "adapter" is just a way to reach a JSON-RPC peer, so an agent is
|
|
5
|
-
* fully described by its id plus a
|
|
6
|
-
*
|
|
7
|
-
* and lives in
|
|
5
|
+
* fully described by its id plus how to start it (a command on a machine) or
|
|
6
|
+
* attach to it (a transport). Everything else — the handshake, sessions,
|
|
7
|
+
* prompts, permissions, terminals — is protocol-generic and lives in
|
|
8
|
+
* {@link openAcpConnection}.
|
|
8
9
|
*/
|
|
9
|
-
import type { AcpAgent, AcpTransportFactory } from '@noetic-tools/types';
|
|
10
|
+
import type { AcpAgent, AcpMachineFactory, AcpTransportFactory, SpawnCommand } from '@noetic-tools/types';
|
|
11
|
+
import type { AcpToolWiring } from './tool-bridge.js';
|
|
10
12
|
/** @public Options for {@link defineAcpAgent}. */
|
|
11
13
|
export interface DefineAcpAgentOptions {
|
|
12
14
|
/**
|
|
@@ -14,10 +16,22 @@ export interface DefineAcpAgentOptions {
|
|
|
14
16
|
* messages. Free-form — the set of agents is open.
|
|
15
17
|
*/
|
|
16
18
|
agentId: string;
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
+
/** Starts the agent: this command, run on {@link machine}. */
|
|
20
|
+
command?: SpawnCommand;
|
|
21
|
+
/** Where {@link command} runs. This machine by default. */
|
|
22
|
+
machine?: AcpMachineFactory;
|
|
23
|
+
/**
|
|
24
|
+
* Opens the byte stream to an agent this package does not start. Used in
|
|
25
|
+
* place of `command`.
|
|
26
|
+
*/
|
|
27
|
+
transport?: AcpTransportFactory;
|
|
19
28
|
/** Environment layered onto every connection this agent opens. */
|
|
20
29
|
env?: Record<string, string | undefined>;
|
|
30
|
+
/**
|
|
31
|
+
* How this agent takes Noetic tools. Defaults to {@link sessionToolWiring}:
|
|
32
|
+
* the bridge joins `session/new` with no agent-specific settings.
|
|
33
|
+
*/
|
|
34
|
+
tools?: AcpToolWiring;
|
|
21
35
|
}
|
|
22
|
-
/** @public Build an {@link AcpAgent} from a transport factory. */
|
|
36
|
+
/** @public Build an {@link AcpAgent} from a command or a transport factory. */
|
|
23
37
|
export declare function defineAcpAgent(def: DefineAcpAgentOptions): AcpAgent;
|
package/dist/define.js
CHANGED
|
@@ -2,28 +2,152 @@
|
|
|
2
2
|
* `defineAcpAgent` — the one-call constructor behind every agent preset.
|
|
3
3
|
*
|
|
4
4
|
* Under ACP an "adapter" is just a way to reach a JSON-RPC peer, so an agent is
|
|
5
|
-
* fully described by its id plus a
|
|
6
|
-
*
|
|
7
|
-
* and lives in
|
|
5
|
+
* fully described by its id plus how to start it (a command on a machine) or
|
|
6
|
+
* attach to it (a transport). Everything else — the handshake, sessions,
|
|
7
|
+
* prompts, permissions, terminals — is protocol-generic and lives in
|
|
8
|
+
* {@link openAcpConnection}.
|
|
8
9
|
*/
|
|
9
10
|
import { openAcpConnection } from './connection.js';
|
|
10
|
-
|
|
11
|
+
import { spawnOn } from './spawn.js';
|
|
12
|
+
import { startToolBridge } from './tool-bridge.js';
|
|
13
|
+
import { sessionToolWiring } from './tool-wiring.js';
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region Public API
|
|
16
|
+
/** @public Build an {@link AcpAgent} from a command or a transport factory. */
|
|
11
17
|
export function defineAcpAgent(def) {
|
|
18
|
+
if (!def.command && !def.transport) {
|
|
19
|
+
throw new TypeError(`defineAcpAgent(${JSON.stringify(def.agentId)}) requires either a 'command' to start or a 'transport'.`);
|
|
20
|
+
}
|
|
12
21
|
return {
|
|
13
22
|
specificationVersion: 'acp-v1',
|
|
14
23
|
agentId: def.agentId,
|
|
24
|
+
refusesTools: def.command ? undefined : attachedRefusal(def.tools ?? sessionToolWiring),
|
|
25
|
+
spawn: def.command ? spawnOn(def.machine ?? lazyLocalMachine(def.agentId)) : undefined,
|
|
15
26
|
async connect(opts) {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
const signal = opts.signal ?? opts.host.signal;
|
|
28
|
+
const opened = await openTarget(def, opts.host.cwd, signal);
|
|
29
|
+
let bridge;
|
|
30
|
+
try {
|
|
31
|
+
bridge = await openBridge(def, opts, opened.reach);
|
|
32
|
+
const transport = await opened.open(bridge?.env ?? {});
|
|
33
|
+
return await openAcpConnection({
|
|
34
|
+
agentId: def.agentId,
|
|
35
|
+
transport: closingWith(transport, opened),
|
|
36
|
+
host: opts.host,
|
|
37
|
+
signal,
|
|
38
|
+
tools: bridge,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
await bridge?.close();
|
|
43
|
+
await opened.close().catch(() => undefined);
|
|
44
|
+
throw e;
|
|
45
|
+
}
|
|
27
46
|
},
|
|
28
47
|
};
|
|
29
48
|
}
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region Helpers
|
|
51
|
+
/**
|
|
52
|
+
* An attached agent was started by someone else and may run anywhere, so
|
|
53
|
+
* neither the bridge's loopback address nor the wiring's launch settings are
|
|
54
|
+
* sure to reach it.
|
|
55
|
+
*/
|
|
56
|
+
function attachedRefusal(wiring) {
|
|
57
|
+
const hint = 'Give the agent a command and a `machine` instead of a `transport` — `webSocketMachine()` or `commandMachine()` from @noetic-tools/acp/remote reach a remote runner (`noetic-acp serve`) — or leave `tools` off this step.';
|
|
58
|
+
if (wiring.launchEnv) {
|
|
59
|
+
return {
|
|
60
|
+
code: 'ACP_TRANSPORT_CANNOT_LAUNCH',
|
|
61
|
+
reason: 'it attaches through a custom `transport` to an agent someone else started, so its tool launch settings cannot be applied, and the transport has no `launch()`.',
|
|
62
|
+
hint,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
code: 'ACP_TRANSPORT_CANNOT_REACH',
|
|
67
|
+
reason: 'it attaches through a custom `transport`, which has no `reach()` to carry the tool bridge to it.',
|
|
68
|
+
hint,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** A command starts on its machine; a transport attaches as it is. */
|
|
72
|
+
async function openTarget(def, cwd, signal) {
|
|
73
|
+
const command = def.command;
|
|
74
|
+
if (!command) {
|
|
75
|
+
const transport = def.transport;
|
|
76
|
+
if (!transport) {
|
|
77
|
+
throw new TypeError(`defineAcpAgent(${JSON.stringify(def.agentId)}) has nothing to open.`);
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
open: (env) => transport({
|
|
81
|
+
cwd,
|
|
82
|
+
env: {
|
|
83
|
+
...def.env,
|
|
84
|
+
...env,
|
|
85
|
+
},
|
|
86
|
+
signal,
|
|
87
|
+
}),
|
|
88
|
+
// Unreached: a step never gives an attached agent tools (`refusesTools`).
|
|
89
|
+
reach: async () => {
|
|
90
|
+
throw new TypeError(`ACP agent '${def.agentId}' attaches through a transport and cannot reach the tool bridge.`);
|
|
91
|
+
},
|
|
92
|
+
close: async () => undefined,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const machine = await (def.machine ?? lazyLocalMachine(def.agentId))({
|
|
96
|
+
signal,
|
|
97
|
+
});
|
|
98
|
+
return {
|
|
99
|
+
open: (env) => machine.launch({
|
|
100
|
+
...command,
|
|
101
|
+
cwd,
|
|
102
|
+
env: {
|
|
103
|
+
...def.env,
|
|
104
|
+
...command.env,
|
|
105
|
+
...env,
|
|
106
|
+
},
|
|
107
|
+
}),
|
|
108
|
+
reach: (bridge) => machine.reach(bridge),
|
|
109
|
+
close: () => machine.close(),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Defer the `node:child_process` import until a connection is actually opened,
|
|
114
|
+
* so importing `@noetic-tools/acp` in a browser or worker stays safe.
|
|
115
|
+
*/
|
|
116
|
+
function lazyLocalMachine(agentId) {
|
|
117
|
+
return async (opts) => {
|
|
118
|
+
const { localMachine } = await import('./stdio.js');
|
|
119
|
+
return await localMachine(agentId)(opts);
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** Closing the agent's stream releases the machine it runs on. */
|
|
123
|
+
function closingWith(transport, opened) {
|
|
124
|
+
return {
|
|
125
|
+
readable: transport.readable,
|
|
126
|
+
writable: transport.writable,
|
|
127
|
+
async close() {
|
|
128
|
+
try {
|
|
129
|
+
await transport.close();
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
await opened.close();
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/** Start the tool bridge when the step gave tools, before the agent starts. */
|
|
138
|
+
async function openBridge(def, opts, reach) {
|
|
139
|
+
const host = opts.host.tools;
|
|
140
|
+
if (!host) {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
return await startToolBridge({
|
|
144
|
+
wiring: def.tools ?? sessionToolWiring,
|
|
145
|
+
getHost: () => opts.host.tools,
|
|
146
|
+
serverName: host.serverName,
|
|
147
|
+
tools: host.tools,
|
|
148
|
+
mcpServers: opts.mcpServers ?? [],
|
|
149
|
+
env: def.env,
|
|
150
|
+
reach,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
package/dist/index.d.ts
CHANGED
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
* implementation detail of the client and are free to change.
|
|
17
17
|
*/
|
|
18
18
|
/** @public */
|
|
19
|
-
export type { AcpPresetOptions, AcpProcessSpec } from './agents.js';
|
|
19
|
+
export type { AcpPresetOptions, AcpProcessSpec, GeminiOptions } from './agents.js';
|
|
20
20
|
/** @public */
|
|
21
|
-
export { claudeCode, codex, customAcpAgent, gemini, opencode, pi } from './agents.js';
|
|
21
|
+
export { claudeCode, codex, customAcpAgent, gemini, opencode, PRESET_COMMANDS, PRESET_ENV, pi, } from './agents.js';
|
|
22
22
|
/** @public */
|
|
23
23
|
export type { DefineAcpAgentOptions } from './define.js';
|
|
24
24
|
/** @public */
|
|
@@ -28,9 +28,19 @@ export type { AcpAgentRegistry } from './registry.js';
|
|
|
28
28
|
/** @public */
|
|
29
29
|
export { createAcpAgentRegistry } from './registry.js';
|
|
30
30
|
/** @public */
|
|
31
|
+
export type { AcpToolWiring, ToolBridgeAddress, ToolBridgeLaunch } from './tool-bridge.js';
|
|
32
|
+
/** @public */
|
|
33
|
+
export type { BridgedPermissionCheck } from './tool-permissions.js';
|
|
34
|
+
/** @public */
|
|
35
|
+
export { claudeBridgedPermission, codexBridgedPermission, geminiBridgedPermission, } from './tool-permissions.js';
|
|
36
|
+
/** @public */
|
|
37
|
+
export { claudeToolWiring, codexToolWiring, geminiToolWiring, opencodeToolWiring, piToolWiring, sessionToolWiring, } from './tool-wiring.js';
|
|
38
|
+
/** @public */
|
|
39
|
+
export { spawnOn } from './spawn.js';
|
|
40
|
+
/** @public */
|
|
31
41
|
export type { AcpLoopbackPair } from './transport-loopback.js';
|
|
32
42
|
/** @public */
|
|
33
|
-
export { createAcpLoopbackPair, loopbackTransport } from './transport-loopback.js';
|
|
43
|
+
export { createAcpLoopbackPair, loopbackMachine, loopbackTransport } from './transport-loopback.js';
|
|
34
44
|
/** @public */
|
|
35
45
|
export type { AcpPermissionPrompt, AcpPermissionReply, AskUserForPermissionOptions, } from './permission-channel.js';
|
|
36
46
|
/** @public */
|
package/dist/index.js
CHANGED
|
@@ -16,13 +16,21 @@
|
|
|
16
16
|
* implementation detail of the client and are free to change.
|
|
17
17
|
*/
|
|
18
18
|
/** @public */
|
|
19
|
-
export { claudeCode, codex, customAcpAgent, gemini, opencode, pi } from './agents.js';
|
|
19
|
+
export { claudeCode, codex, customAcpAgent, gemini, opencode, PRESET_COMMANDS, PRESET_ENV, pi, } from './agents.js';
|
|
20
20
|
/** @public */
|
|
21
21
|
export { defineAcpAgent } from './define.js';
|
|
22
22
|
/** @public */
|
|
23
23
|
export { createAcpAgentRegistry } from './registry.js';
|
|
24
24
|
/** @public */
|
|
25
|
-
export {
|
|
25
|
+
export { claudeBridgedPermission, codexBridgedPermission, geminiBridgedPermission, } from './tool-permissions.js';
|
|
26
|
+
/** @public */
|
|
27
|
+
export { claudeToolWiring, codexToolWiring, geminiToolWiring, opencodeToolWiring, piToolWiring, sessionToolWiring, } from './tool-wiring.js';
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region Transports
|
|
30
|
+
/** @public */
|
|
31
|
+
export { spawnOn } from './spawn.js';
|
|
32
|
+
/** @public */
|
|
33
|
+
export { createAcpLoopbackPair, loopbackMachine, loopbackTransport } from './transport-loopback.js';
|
|
26
34
|
/** @public */
|
|
27
35
|
export { ACP_PERMISSION_SCOPE, AcpPermissionPromptSchema, AcpPermissionReplySchema, acpPermissionDecisions, acpPermissionRequests, askUserForPermission, resolveAcpPermission, } from './permission-channel.js';
|
|
28
36
|
//#endregion
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serves a tool host over MCP streamable HTTP on the loopback interface.
|
|
3
|
+
*
|
|
4
|
+
* Needs `node:http`, so the tool bridge loads it on first use and the
|
|
5
|
+
* package's main entry stays runtime-neutral.
|
|
6
|
+
*
|
|
7
|
+
* Any local process can reach a loopback port, so every request must carry
|
|
8
|
+
* the server's bearer token, name it by its own host, and come from no web
|
|
9
|
+
* origin (the MCP spec's guard against DNS rebinding).
|
|
10
|
+
*/
|
|
11
|
+
import type { AcpToolHost } from '@noetic-tools/types';
|
|
12
|
+
/** Where an agent reaches a running tool server. */
|
|
13
|
+
export interface HttpToolServer {
|
|
14
|
+
/** Full endpoint URL, path included. */
|
|
15
|
+
readonly url: string;
|
|
16
|
+
/** Sent by the agent as `Authorization: Bearer <token>`. */
|
|
17
|
+
readonly token: string;
|
|
18
|
+
close(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Start a tool server on `127.0.0.1` at a random port and path. Each MCP
|
|
22
|
+
* session the agent opens gets its own protocol server over the same host.
|
|
23
|
+
*/
|
|
24
|
+
export declare function serveToolsOverHttp(getHost: () => AcpToolHost | undefined): Promise<HttpToolServer>;
|
package/dist/mcp/node.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serves a tool host over MCP streamable HTTP on the loopback interface.
|
|
3
|
+
*
|
|
4
|
+
* Needs `node:http`, so the tool bridge loads it on first use and the
|
|
5
|
+
* package's main entry stays runtime-neutral.
|
|
6
|
+
*
|
|
7
|
+
* Any local process can reach a loopback port, so every request must carry
|
|
8
|
+
* the server's bearer token, name it by its own host, and come from no web
|
|
9
|
+
* origin (the MCP spec's guard against DNS rebinding).
|
|
10
|
+
*/
|
|
11
|
+
import { randomUUID, timingSafeEqual } from 'node:crypto';
|
|
12
|
+
import { createServer } from 'node:http';
|
|
13
|
+
import { NodeStreamableHTTPServerTransport } from '@modelcontextprotocol/node';
|
|
14
|
+
import { createToolServer } from './server.js';
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region Request guard
|
|
17
|
+
function allowedHosts(port) {
|
|
18
|
+
return new Set([
|
|
19
|
+
`127.0.0.1:${port}`,
|
|
20
|
+
`localhost:${port}`,
|
|
21
|
+
]);
|
|
22
|
+
}
|
|
23
|
+
function pathOf(url) {
|
|
24
|
+
return new URL(url ?? '/', 'http://127.0.0.1').pathname;
|
|
25
|
+
}
|
|
26
|
+
function sameSecret(given, expected) {
|
|
27
|
+
const a = Buffer.from(given ?? '');
|
|
28
|
+
const b = Buffer.from(expected);
|
|
29
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
30
|
+
}
|
|
31
|
+
/** The status to refuse a request with, or `undefined` to let it through. */
|
|
32
|
+
function refusal(req, expect) {
|
|
33
|
+
if (pathOf(req.url) !== expect.path) {
|
|
34
|
+
return 404;
|
|
35
|
+
}
|
|
36
|
+
if (!sameSecret(req.headers.authorization, `Bearer ${expect.token}`)) {
|
|
37
|
+
return 401;
|
|
38
|
+
}
|
|
39
|
+
if (!req.headers.host || !expect.hosts.has(req.headers.host)) {
|
|
40
|
+
return 403;
|
|
41
|
+
}
|
|
42
|
+
if (req.headers.origin !== undefined) {
|
|
43
|
+
return 403;
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region Public API
|
|
49
|
+
/**
|
|
50
|
+
* Start a tool server on `127.0.0.1` at a random port and path. Each MCP
|
|
51
|
+
* session the agent opens gets its own protocol server over the same host.
|
|
52
|
+
*/
|
|
53
|
+
export async function serveToolsOverHttp(getHost) {
|
|
54
|
+
const token = randomUUID();
|
|
55
|
+
const path = `/mcp/${randomUUID()}`;
|
|
56
|
+
const sessions = new Map();
|
|
57
|
+
const open = async () => {
|
|
58
|
+
const transport = new NodeStreamableHTTPServerTransport({
|
|
59
|
+
sessionIdGenerator: randomUUID,
|
|
60
|
+
onsessioninitialized: (id) => {
|
|
61
|
+
sessions.set(id, transport);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
transport.onclose = () => {
|
|
65
|
+
if (transport.sessionId) {
|
|
66
|
+
sessions.delete(transport.sessionId);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
await createToolServer(getHost).connect(transport);
|
|
70
|
+
return transport;
|
|
71
|
+
};
|
|
72
|
+
let hosts = new Set();
|
|
73
|
+
const http = createServer(async (req, res) => {
|
|
74
|
+
const status = refusal(req, {
|
|
75
|
+
path,
|
|
76
|
+
token,
|
|
77
|
+
hosts,
|
|
78
|
+
});
|
|
79
|
+
if (status !== undefined) {
|
|
80
|
+
res.writeHead(status).end();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
await route(req, res);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
if (!res.headersSent) {
|
|
88
|
+
res.writeHead(500);
|
|
89
|
+
}
|
|
90
|
+
res.end();
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
/**
|
|
94
|
+
* A request naming a session goes to it. Only a POST may open a new one, and
|
|
95
|
+
* a transport the request did not initialize is closed at once.
|
|
96
|
+
*/
|
|
97
|
+
const route = async (req, res) => {
|
|
98
|
+
const sessionId = req.headers['mcp-session-id'];
|
|
99
|
+
if (typeof sessionId === 'string') {
|
|
100
|
+
const existing = sessions.get(sessionId);
|
|
101
|
+
if (!existing) {
|
|
102
|
+
res.writeHead(404).end();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
await existing.handleRequest(req, res);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (req.method !== 'POST') {
|
|
109
|
+
res.writeHead(405).end();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const transport = await open();
|
|
113
|
+
await transport.handleRequest(req, res);
|
|
114
|
+
if (!transport.sessionId) {
|
|
115
|
+
await transport.close();
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
await new Promise((resolve, reject) => {
|
|
119
|
+
http.once('error', reject);
|
|
120
|
+
http.listen(0, '127.0.0.1', () => {
|
|
121
|
+
http.off('error', reject);
|
|
122
|
+
resolve();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
const address = http.address();
|
|
126
|
+
if (address === null || typeof address === 'string') {
|
|
127
|
+
throw new Error('The tool server did not bind a TCP port.');
|
|
128
|
+
}
|
|
129
|
+
const port = address.port;
|
|
130
|
+
hosts = allowedHosts(port);
|
|
131
|
+
return {
|
|
132
|
+
url: `http://127.0.0.1:${port}${path}`,
|
|
133
|
+
token,
|
|
134
|
+
async close() {
|
|
135
|
+
for (const transport of sessions.values()) {
|
|
136
|
+
await transport.close().catch(() => undefined);
|
|
137
|
+
}
|
|
138
|
+
sessions.clear();
|
|
139
|
+
await new Promise((resolve) => {
|
|
140
|
+
http.close(() => resolve());
|
|
141
|
+
http.closeAllConnections();
|
|
142
|
+
});
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The MCP server an ACP agent connects to for its Noetic tools.
|
|
3
|
+
*
|
|
4
|
+
* It only translates: `tools/list` reads the host's listing and `tools/call`
|
|
5
|
+
* forwards to `host.call`, which runs the same pipeline a model's tool call
|
|
6
|
+
* does. Nothing here knows how a Noetic tool executes.
|
|
7
|
+
*/
|
|
8
|
+
import { Server } from '@modelcontextprotocol/server';
|
|
9
|
+
import type { AcpToolHost } from '@noetic-tools/types';
|
|
10
|
+
/** The agent's id for a call, read from the MCP request `_meta`. */
|
|
11
|
+
export declare function agentCallIdOf(meta: Record<string, unknown> | undefined): string | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Build an MCP server over the tool host. `getHost` is read on every request,
|
|
14
|
+
* so a host rebound between turns takes effect without reconnecting.
|
|
15
|
+
*/
|
|
16
|
+
export declare function createToolServer(getHost: () => AcpToolHost | undefined): Server;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The MCP server an ACP agent connects to for its Noetic tools.
|
|
3
|
+
*
|
|
4
|
+
* It only translates: `tools/list` reads the host's listing and `tools/call`
|
|
5
|
+
* forwards to `host.call`, which runs the same pipeline a model's tool call
|
|
6
|
+
* does. Nothing here knows how a Noetic tool executes.
|
|
7
|
+
*/
|
|
8
|
+
import { Server } from '@modelcontextprotocol/server';
|
|
9
|
+
//#region Agent hints
|
|
10
|
+
/**
|
|
11
|
+
* Claude Code reads these per-tool `_meta` keys. Without them it hides MCP
|
|
12
|
+
* tools behind a ToolSearch step and saves large results to a file, so the
|
|
13
|
+
* model would neither see the tools up front nor read their full output.
|
|
14
|
+
*/
|
|
15
|
+
const ALWAYS_LOAD = 'anthropic/alwaysLoad';
|
|
16
|
+
const MAX_RESULT_SIZE_CHARS = 'anthropic/maxResultSizeChars';
|
|
17
|
+
const UNBOUNDED_RESULT_CHARS = 1e9;
|
|
18
|
+
/**
|
|
19
|
+
* Keys under which agents put their own id for a call in the request `_meta`.
|
|
20
|
+
* Each equals the `toolCallId` of the agent's ACP `tool_call` for that call.
|
|
21
|
+
*/
|
|
22
|
+
const AGENT_CALL_ID_KEYS = [
|
|
23
|
+
'claudecode/toolUseId',
|
|
24
|
+
'callId',
|
|
25
|
+
];
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region Helpers
|
|
28
|
+
function toMcpTool(tool) {
|
|
29
|
+
return {
|
|
30
|
+
name: tool.name,
|
|
31
|
+
description: tool.description,
|
|
32
|
+
inputSchema: {
|
|
33
|
+
type: 'object',
|
|
34
|
+
...tool.inputSchema,
|
|
35
|
+
},
|
|
36
|
+
...(tool.title !== undefined
|
|
37
|
+
? {
|
|
38
|
+
title: tool.title,
|
|
39
|
+
}
|
|
40
|
+
: {}),
|
|
41
|
+
_meta: {
|
|
42
|
+
[ALWAYS_LOAD]: true,
|
|
43
|
+
[MAX_RESULT_SIZE_CHARS]: UNBOUNDED_RESULT_CHARS,
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** The agent's id for a call, read from the MCP request `_meta`. */
|
|
48
|
+
export function agentCallIdOf(meta) {
|
|
49
|
+
if (!meta) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
for (const key of AGENT_CALL_ID_KEYS) {
|
|
53
|
+
const value = meta[key];
|
|
54
|
+
if (typeof value === 'string' && value.length > 0) {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
function textResult(output, isError) {
|
|
61
|
+
return {
|
|
62
|
+
content: [
|
|
63
|
+
{
|
|
64
|
+
type: 'text',
|
|
65
|
+
text: output,
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
isError,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region Public API
|
|
73
|
+
/**
|
|
74
|
+
* Build an MCP server over the tool host. `getHost` is read on every request,
|
|
75
|
+
* so a host rebound between turns takes effect without reconnecting.
|
|
76
|
+
*/
|
|
77
|
+
export function createToolServer(getHost) {
|
|
78
|
+
const server = new Server({
|
|
79
|
+
name: 'noetic',
|
|
80
|
+
version: '1.0.0',
|
|
81
|
+
}, {
|
|
82
|
+
capabilities: {
|
|
83
|
+
tools: {},
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
server.setRequestHandler('tools/list', async () => ({
|
|
87
|
+
tools: (getHost()?.tools ?? []).map(toMcpTool),
|
|
88
|
+
}));
|
|
89
|
+
server.setRequestHandler('tools/call', async (request, ctx) => {
|
|
90
|
+
const host = getHost();
|
|
91
|
+
if (!host) {
|
|
92
|
+
return textResult(`Error: unknown tool '${request.params.name}'`, true);
|
|
93
|
+
}
|
|
94
|
+
const result = await host.call({
|
|
95
|
+
name: request.params.name,
|
|
96
|
+
args: request.params.arguments ?? {},
|
|
97
|
+
agentCallId: agentCallIdOf(request.params._meta),
|
|
98
|
+
signal: ctx.mcpReq.signal,
|
|
99
|
+
});
|
|
100
|
+
return textResult(result.output, result.isError);
|
|
101
|
+
});
|
|
102
|
+
return server;
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
@@ -74,8 +74,8 @@ export declare const acpPermissionDecisions: ExternalChannel<AcpPermissionReply>
|
|
|
74
74
|
export interface AskUserForPermissionOptions {
|
|
75
75
|
/**
|
|
76
76
|
* How long to wait for an answer, in milliseconds. On expiry the handler
|
|
77
|
-
* resolves with {@link onTimeout}
|
|
78
|
-
*
|
|
77
|
+
* resolves with {@link onTimeout}. With none, the request waits until it is
|
|
78
|
+
* answered or the turn is cancelled.
|
|
79
79
|
*/
|
|
80
80
|
timeout?: number;
|
|
81
81
|
/**
|