@abide/bridge 0.7.3 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/package.json +1 -1
- package/src/bridge.ts +2 -1
- package/src/callTool.ts +304 -0
- package/src/claudeTurn.ts +3 -0
- package/src/grant.ts +69 -0
- package/src/serveTurns.ts +70 -12
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @abide/bridge
|
|
2
2
|
|
|
3
|
+
## 0.8.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 0d00892: the bridge makes a call of one of the visitor's MCP tools with no Claude Code run, speaking MCP to the server itself — a stdio server started for the call in an empty directory, or an http server sent only its configured headers — under the same `--tools` grant as a turn and only where `--allowedTools` approves it, each call beside every other (RULEBOOK 44.94-44.97)
|
|
8
|
+
|
|
9
|
+
## 0.7.4
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- e24354b: the native app finds claude on the login shell's PATH when opened from the Finder — Bun.which and Bun.spawn read the PATH the process started with, so localStands passes the current PATH and a turn's spawn hands the child the current env; the 44.77 test now starts with no claude on PATH and assigns it after, as runNative does ([`26a24cb`](https://github.com/briancray/abide/commit/26a24cbd98ebc248dce33a17fb0f5a28521247d4))
|
|
14
|
+
|
|
3
15
|
## 0.7.3
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/package.json
CHANGED
package/src/bridge.ts
CHANGED
|
@@ -54,8 +54,9 @@ export async function bridge(options: {
|
|
|
54
54
|
options.silenceMilliseconds ?? SILENCE_MILLISECONDS
|
|
55
55
|
// What cuts the stream being read, where it went silent.
|
|
56
56
|
let dialed = new AbortController()
|
|
57
|
+
// 44.96 — `calls` says, as it pairs, that this bridge makes a call of one tool with no model.
|
|
57
58
|
let stream = await fetch(
|
|
58
|
-
`${site}${BRIDGE_PATH}?code=${encodeURIComponent(options.code)}`,
|
|
59
|
+
`${site}${BRIDGE_PATH}?code=${encodeURIComponent(options.code)}&calls=1`,
|
|
59
60
|
{
|
|
60
61
|
headers: { accept: 'text/event-stream' },
|
|
61
62
|
signal: dialed.signal,
|
package/src/callTool.ts
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
// ONE MCP TOOL OF THIS MACHINE, CALLED WITH NO MODEL (44.95): the bridge speaks MCP to the visitor's
|
|
2
|
+
// server itself — the server as 44.58 finds it, started as Claude Code would start it — and hands back
|
|
3
|
+
// what the tool answered. A server is started for the one call and ended with it.
|
|
4
|
+
|
|
5
|
+
import { mkdtemp, rm } from 'node:fs/promises' // bun has no temporary directory or recursive remove of its own
|
|
6
|
+
import { tmpdir } from 'node:os' // bun exposes no temporary directory of its own
|
|
7
|
+
|
|
8
|
+
import type { AssistantContent, ToolAnswer } from '@abide/abide/server'
|
|
9
|
+
|
|
10
|
+
import { readLines } from './readLines.ts'
|
|
11
|
+
|
|
12
|
+
const PROTOCOL_VERSION = '2025-06-18'
|
|
13
|
+
|
|
14
|
+
/** A server as an MCP config's `mcpServers` holds it: a command run over stdio, or an http address. */
|
|
15
|
+
type ServerConfiguration = {
|
|
16
|
+
type?: string
|
|
17
|
+
command?: string
|
|
18
|
+
args?: string[]
|
|
19
|
+
env?: Record<string, string>
|
|
20
|
+
url?: string
|
|
21
|
+
headers?: Record<string, string>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type RpcMessage = {
|
|
25
|
+
id?: number | string
|
|
26
|
+
method?: string
|
|
27
|
+
result?: unknown
|
|
28
|
+
error?: { message?: string }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** One server spoken to: a request it answers, a notification it is sent, and the end of it. */
|
|
32
|
+
type Session = {
|
|
33
|
+
send(message: {
|
|
34
|
+
id?: number
|
|
35
|
+
method: string
|
|
36
|
+
params?: unknown
|
|
37
|
+
}): Promise<RpcMessage | undefined>
|
|
38
|
+
close(): Promise<void>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type McpPiece = {
|
|
42
|
+
type: string
|
|
43
|
+
text?: string
|
|
44
|
+
data?: string
|
|
45
|
+
mimeType?: string
|
|
46
|
+
resource?: { text?: string }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* What `tool` of `server` answered `input`, its server named `name` in what it says.
|
|
51
|
+
*
|
|
52
|
+
* Throws where the server cannot be started or reached, or fails the protocol before the call.
|
|
53
|
+
*/
|
|
54
|
+
export async function callTool(
|
|
55
|
+
name: string,
|
|
56
|
+
server: unknown,
|
|
57
|
+
tool: string,
|
|
58
|
+
input: unknown,
|
|
59
|
+
signal: AbortSignal,
|
|
60
|
+
): Promise<ToolAnswer> {
|
|
61
|
+
const configuration = server as ServerConfiguration
|
|
62
|
+
const type =
|
|
63
|
+
configuration.type ??
|
|
64
|
+
(configuration.command === undefined ? undefined : 'stdio')
|
|
65
|
+
let session: Session
|
|
66
|
+
if (type === 'stdio' && configuration.command !== undefined)
|
|
67
|
+
session = await stdioSession(name, configuration, signal)
|
|
68
|
+
else if (type === 'http' && configuration.url !== undefined)
|
|
69
|
+
session = httpSession(name, configuration, signal)
|
|
70
|
+
else
|
|
71
|
+
throw new Error(
|
|
72
|
+
`The ${name} MCP server is reached over ${type ?? 'nothing this bridge knows'}, and a call reaches a server over stdio or http.`,
|
|
73
|
+
)
|
|
74
|
+
try {
|
|
75
|
+
const opened = await session.send({
|
|
76
|
+
id: 1,
|
|
77
|
+
method: 'initialize',
|
|
78
|
+
params: {
|
|
79
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
80
|
+
capabilities: {},
|
|
81
|
+
clientInfo: { name: 'abide-bridge', version: '1' },
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
if (opened?.error !== undefined)
|
|
85
|
+
throw new Error(
|
|
86
|
+
`The ${name} MCP server refused to start a session: ${opened.error.message}`,
|
|
87
|
+
)
|
|
88
|
+
await session.send({ method: 'notifications/initialized' })
|
|
89
|
+
const called = await session.send({
|
|
90
|
+
id: 2,
|
|
91
|
+
method: 'tools/call',
|
|
92
|
+
params: { name: tool, arguments: input ?? {} },
|
|
93
|
+
})
|
|
94
|
+
// A call the server refused is the tool's answer, failed, as Claude Code would show it.
|
|
95
|
+
if (called?.error !== undefined)
|
|
96
|
+
return {
|
|
97
|
+
content: [
|
|
98
|
+
{ type: 'text', text: called.error.message ?? 'failed' },
|
|
99
|
+
],
|
|
100
|
+
failed: true,
|
|
101
|
+
}
|
|
102
|
+
return answerOf(called?.result)
|
|
103
|
+
} finally {
|
|
104
|
+
await session.close()
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** An MCP tool result as a `ToolAnswer`: text and images as they are, anything else as its text or JSON. */
|
|
109
|
+
function answerOf(result: unknown): ToolAnswer {
|
|
110
|
+
const { content, structuredContent, isError } = (result ?? {}) as {
|
|
111
|
+
content?: McpPiece[]
|
|
112
|
+
structuredContent?: unknown
|
|
113
|
+
isError?: boolean
|
|
114
|
+
}
|
|
115
|
+
const pieces: AssistantContent[] = []
|
|
116
|
+
for (const piece of content ?? [])
|
|
117
|
+
if (piece.type === 'text')
|
|
118
|
+
pieces.push({ type: 'text', text: piece.text ?? '' })
|
|
119
|
+
else if (
|
|
120
|
+
piece.type === 'image' &&
|
|
121
|
+
piece.data !== undefined &&
|
|
122
|
+
piece.mimeType !== undefined
|
|
123
|
+
)
|
|
124
|
+
pieces.push({
|
|
125
|
+
type: 'image',
|
|
126
|
+
data: piece.data,
|
|
127
|
+
mimeType: piece.mimeType,
|
|
128
|
+
})
|
|
129
|
+
else
|
|
130
|
+
pieces.push({
|
|
131
|
+
type: 'text',
|
|
132
|
+
text: piece.resource?.text ?? JSON.stringify(piece),
|
|
133
|
+
})
|
|
134
|
+
if (pieces.length === 0 && structuredContent !== undefined)
|
|
135
|
+
pieces.push({ type: 'text', text: JSON.stringify(structuredContent) })
|
|
136
|
+
return { content: pieces, failed: isError === true }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A server run as its `command`, in an empty directory removed when it ends, with this process's
|
|
141
|
+
* variables and its own `env` — as Claude Code runs one (44.15) — spoken to a line of JSON at a time.
|
|
142
|
+
*/
|
|
143
|
+
async function stdioSession(
|
|
144
|
+
name: string,
|
|
145
|
+
configuration: ServerConfiguration,
|
|
146
|
+
signal: AbortSignal,
|
|
147
|
+
): Promise<Session> {
|
|
148
|
+
const directory = await mkdtemp(`${tmpdir()}/abide-bridge-`)
|
|
149
|
+
let child: Bun.Subprocess<'pipe', 'pipe', 'pipe'>
|
|
150
|
+
try {
|
|
151
|
+
child = Bun.spawn({
|
|
152
|
+
cmd: [
|
|
153
|
+
configuration.command as string,
|
|
154
|
+
...(configuration.args ?? []),
|
|
155
|
+
],
|
|
156
|
+
cwd: directory,
|
|
157
|
+
env: { ...Bun.env, ...configuration.env },
|
|
158
|
+
stdin: 'pipe',
|
|
159
|
+
stdout: 'pipe',
|
|
160
|
+
stderr: 'pipe',
|
|
161
|
+
signal,
|
|
162
|
+
})
|
|
163
|
+
} catch (thrown) {
|
|
164
|
+
await rm(directory, { recursive: true, force: true })
|
|
165
|
+
throw new Error(
|
|
166
|
+
`The ${name} MCP server could not be started: ${thrown instanceof Error ? thrown.message : String(thrown)}`,
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
const stderr = new Response(child.stderr).text()
|
|
170
|
+
const waiting = new Map<number | string, (message: RpcMessage) => void>()
|
|
171
|
+
const ended = (async () => {
|
|
172
|
+
for await (const line of readLines(child.stdout)) {
|
|
173
|
+
let message: RpcMessage
|
|
174
|
+
try {
|
|
175
|
+
message = JSON.parse(line) as RpcMessage
|
|
176
|
+
} catch {
|
|
177
|
+
// A line that is not JSON is the server's own chatter.
|
|
178
|
+
continue
|
|
179
|
+
}
|
|
180
|
+
if (message.method === undefined) {
|
|
181
|
+
if (message.id !== undefined) waiting.get(message.id)?.(message)
|
|
182
|
+
continue
|
|
183
|
+
}
|
|
184
|
+
// A request of the server's — a root, a sample, a question — is one no model is here to answer.
|
|
185
|
+
if (message.id !== undefined)
|
|
186
|
+
child.stdin.write(
|
|
187
|
+
`${JSON.stringify(
|
|
188
|
+
message.method === 'ping'
|
|
189
|
+
? { jsonrpc: '2.0', id: message.id, result: {} }
|
|
190
|
+
: {
|
|
191
|
+
jsonrpc: '2.0',
|
|
192
|
+
id: message.id,
|
|
193
|
+
error: {
|
|
194
|
+
code: -32601,
|
|
195
|
+
message:
|
|
196
|
+
'abide-bridge answers no request of a server.',
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
)}\n`,
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
})()
|
|
203
|
+
return {
|
|
204
|
+
async send(message) {
|
|
205
|
+
const answered =
|
|
206
|
+
message.id === undefined
|
|
207
|
+
? undefined
|
|
208
|
+
: new Promise<RpcMessage | undefined>((resolve) => {
|
|
209
|
+
waiting.set(message.id as number, resolve)
|
|
210
|
+
void ended.then(() => resolve(undefined))
|
|
211
|
+
})
|
|
212
|
+
child.stdin.write(
|
|
213
|
+
`${JSON.stringify({ jsonrpc: '2.0', ...message })}\n`,
|
|
214
|
+
)
|
|
215
|
+
await child.stdin.flush()
|
|
216
|
+
if (answered === undefined) return undefined
|
|
217
|
+
const reply = await answered
|
|
218
|
+
if (reply === undefined)
|
|
219
|
+
throw new Error(
|
|
220
|
+
signal.aborted
|
|
221
|
+
? 'The call was ended.'
|
|
222
|
+
: `The ${name} MCP server exited ${await child.exited} before it answered ${message.method}.\n${(await stderr).slice(-2000)}`,
|
|
223
|
+
)
|
|
224
|
+
return reply
|
|
225
|
+
},
|
|
226
|
+
async close() {
|
|
227
|
+
child.kill()
|
|
228
|
+
await child.exited
|
|
229
|
+
await rm(directory, { recursive: true, force: true })
|
|
230
|
+
},
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** A server at an http address, each message posted, its answer read as JSON or from an event stream. */
|
|
235
|
+
function httpSession(
|
|
236
|
+
name: string,
|
|
237
|
+
configuration: ServerConfiguration,
|
|
238
|
+
signal: AbortSignal,
|
|
239
|
+
): Session {
|
|
240
|
+
let sessionId: string | null = null
|
|
241
|
+
return {
|
|
242
|
+
async send(message) {
|
|
243
|
+
const headers: Record<string, string> = {
|
|
244
|
+
...configuration.headers,
|
|
245
|
+
'content-type': 'application/json',
|
|
246
|
+
accept: 'application/json, text/event-stream',
|
|
247
|
+
'mcp-protocol-version': PROTOCOL_VERSION,
|
|
248
|
+
}
|
|
249
|
+
if (sessionId !== null) headers['mcp-session-id'] = sessionId
|
|
250
|
+
const answer = await fetch(configuration.url as string, {
|
|
251
|
+
method: 'POST',
|
|
252
|
+
headers,
|
|
253
|
+
body: JSON.stringify({ jsonrpc: '2.0', ...message }),
|
|
254
|
+
signal,
|
|
255
|
+
})
|
|
256
|
+
sessionId = answer.headers.get('mcp-session-id') ?? sessionId
|
|
257
|
+
if (answer.status === 401 || answer.status === 403)
|
|
258
|
+
throw new Error(
|
|
259
|
+
`The ${name} MCP server asks for a sign-in (${answer.status}): a call carries only the headers its configuration names, and none Claude Code signed in with.`,
|
|
260
|
+
)
|
|
261
|
+
if (!answer.ok)
|
|
262
|
+
throw new Error(
|
|
263
|
+
`The ${name} MCP server answered ${message.method} ${answer.status}: ${(await answer.text()).slice(0, 2000)}`,
|
|
264
|
+
)
|
|
265
|
+
if (message.id === undefined || answer.body === null) {
|
|
266
|
+
await answer.body?.cancel()
|
|
267
|
+
return undefined
|
|
268
|
+
}
|
|
269
|
+
if (
|
|
270
|
+
!(answer.headers.get('content-type') ?? '').includes(
|
|
271
|
+
'text/event-stream',
|
|
272
|
+
)
|
|
273
|
+
)
|
|
274
|
+
return (await answer.json()) as RpcMessage
|
|
275
|
+
let data = ''
|
|
276
|
+
for await (const line of readLines(answer.body)) {
|
|
277
|
+
if (line.startsWith('data:')) {
|
|
278
|
+
data += `${data === '' ? '' : '\n'}${line.slice(5).trimStart()}`
|
|
279
|
+
continue
|
|
280
|
+
}
|
|
281
|
+
if (line !== '' || data === '') continue
|
|
282
|
+
const event = JSON.parse(data) as RpcMessage
|
|
283
|
+
data = ''
|
|
284
|
+
if (event.method === undefined && event.id === message.id)
|
|
285
|
+
return event
|
|
286
|
+
}
|
|
287
|
+
throw new Error(
|
|
288
|
+
`The ${name} MCP server ended its answer to ${message.method} without one.`,
|
|
289
|
+
)
|
|
290
|
+
},
|
|
291
|
+
async close() {
|
|
292
|
+
if (sessionId === null) return
|
|
293
|
+
await fetch(configuration.url as string, {
|
|
294
|
+
method: 'DELETE',
|
|
295
|
+
headers: {
|
|
296
|
+
...configuration.headers,
|
|
297
|
+
'mcp-session-id': sessionId,
|
|
298
|
+
},
|
|
299
|
+
})
|
|
300
|
+
.then((answer) => answer.body?.cancel())
|
|
301
|
+
.catch(() => {})
|
|
302
|
+
},
|
|
303
|
+
}
|
|
304
|
+
}
|
package/src/claudeTurn.ts
CHANGED
|
@@ -240,6 +240,9 @@ export async function claudeTurn(
|
|
|
240
240
|
String(turn.maxTurns),
|
|
241
241
|
],
|
|
242
242
|
cwd: directory,
|
|
243
|
+
// The environment as it is NOW: without one, bun finds `claude` on the `PATH` the process
|
|
244
|
+
// started with and hands the child that, where a native app assigns the login shell's after.
|
|
245
|
+
env: { ...Bun.env },
|
|
243
246
|
stdin: 'pipe',
|
|
244
247
|
stdout: 'pipe',
|
|
245
248
|
stderr: 'pipe',
|
package/src/grant.ts
CHANGED
|
@@ -218,3 +218,72 @@ export function withheldNotices(grant: Grant): string[] {
|
|
|
218
218
|
)
|
|
219
219
|
return notices
|
|
220
220
|
}
|
|
221
|
+
|
|
222
|
+
/** Whether `values` of a tools flag name `tool` of `server`, the server's every tool, or the server. */
|
|
223
|
+
function namesTool(
|
|
224
|
+
values: readonly string[] | undefined,
|
|
225
|
+
tool: string,
|
|
226
|
+
server: string,
|
|
227
|
+
): boolean {
|
|
228
|
+
const named = grantedOf(values)
|
|
229
|
+
return (
|
|
230
|
+
named.has(tool) ||
|
|
231
|
+
named.has(`mcp__${server}`) ||
|
|
232
|
+
named.has(`mcp__${server}__*`)
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* How a call of `tool` reaches this machine for a visitor who passed `claudeArgs` — the server it is
|
|
238
|
+
* of, that server's configuration and the tool's name on it — or why it is refused: it is not an MCP
|
|
239
|
+
* tool, a turn whose `tools` named it alone would not be given it (44.29), the visitor's
|
|
240
|
+
* `--allowedTools` does not approve it, or their `--disallowedTools` names it. 44.94.
|
|
241
|
+
*
|
|
242
|
+
* Throws where an `--mcp-config` they named cannot be read.
|
|
243
|
+
*/
|
|
244
|
+
export async function callGrant(
|
|
245
|
+
tool: string,
|
|
246
|
+
claudeArgs: readonly string[],
|
|
247
|
+
full: boolean,
|
|
248
|
+
): Promise<
|
|
249
|
+
| {
|
|
250
|
+
server: string
|
|
251
|
+
configuration: unknown
|
|
252
|
+
name: string
|
|
253
|
+
refused: undefined
|
|
254
|
+
}
|
|
255
|
+
| { refused: string }
|
|
256
|
+
> {
|
|
257
|
+
const server = serverOf(tool)
|
|
258
|
+
const prefix = `mcp__${server}__`
|
|
259
|
+
if (
|
|
260
|
+
server === undefined ||
|
|
261
|
+
!tool.startsWith(prefix) ||
|
|
262
|
+
tool.length === prefix.length
|
|
263
|
+
)
|
|
264
|
+
return {
|
|
265
|
+
refused: `A call is of one MCP tool, named mcp__<server>__<tool>, and ${tool} is not one.`,
|
|
266
|
+
}
|
|
267
|
+
const { grant, args } = await grantFor([tool], claudeArgs, full)
|
|
268
|
+
if (grant.mcp.length === 0)
|
|
269
|
+
return { refused: withheldNotices(grant).join(' ') }
|
|
270
|
+
// 44.82 — a full bridge's options as the visitor gave them.
|
|
271
|
+
const given = full ? [...claudeArgs] : args
|
|
272
|
+
const allowed = takeFlag(given, ['--allowedTools', '--allowed-tools'])
|
|
273
|
+
const denied = takeFlag(given, ['--disallowedTools', '--disallowed-tools'])
|
|
274
|
+
if (namesTool(denied, tool, server))
|
|
275
|
+
return {
|
|
276
|
+
refused: `The site called ${tool}, which this bridge’s --disallowedTools names.`,
|
|
277
|
+
}
|
|
278
|
+
// A turn asks what auto mode would not approve; a call has no model to judge it, so it asks nothing.
|
|
279
|
+
if (!namesTool(allowed, tool, server))
|
|
280
|
+
return {
|
|
281
|
+
refused: `The site called ${tool} directly, which this bridge does only for a tool you approved. Add \`-- --allowedTools ${tool}\` to allow it.`,
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
server,
|
|
285
|
+
configuration: grant.servers[server],
|
|
286
|
+
name: tool.slice(prefix.length),
|
|
287
|
+
refused: undefined,
|
|
288
|
+
}
|
|
289
|
+
}
|
package/src/serveTurns.ts
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
// WHAT A BRIDGE DOES WITH WHAT A SITE SENDS IT, apart from how it is sent: each turn run with this
|
|
2
2
|
// machine's Claude Code, each MCP request and permission prompt it makes reported for the site to
|
|
3
|
-
// answer, and each reply handed back to it. `bridge.ts` carries it over a server-sent stream and posts; the native app's own
|
|
3
|
+
// answer, and each reply handed back to it — and each call of one MCP tool, made with no model (44.93). `bridge.ts` carries it over a server-sent stream and posts; the native app's own
|
|
4
4
|
// bridge (44.77) carries it in its own process — one core, so a frame either learns is one both learn.
|
|
5
5
|
|
|
6
|
-
import type { ProviderEvent } from '@abide/abide/server'
|
|
6
|
+
import type { ProviderEvent, ToolAnswer } from '@abide/abide/server'
|
|
7
7
|
|
|
8
|
+
import { callTool } from './callTool.ts'
|
|
8
9
|
import { claudeTurn, type TurnRequest } from './claudeTurn.ts'
|
|
9
|
-
import { PERMISSION_SERVER } from './grant.ts'
|
|
10
|
+
import { callGrant, PERMISSION_SERVER } from './grant.ts'
|
|
10
11
|
|
|
11
12
|
// 44.24 — how long a relayed MCP request waits for the site's reply.
|
|
12
13
|
const REPLY_MILLISECONDS = 120_000
|
|
13
14
|
|
|
14
|
-
/** What a site
|
|
15
|
+
/** What a site asks of one call: the MCP tool, as `mcp__<server>__<tool>`, and its input. 44.93. */
|
|
16
|
+
export type CallRequest = { id: string; name: string; input: unknown }
|
|
17
|
+
|
|
18
|
+
/** What a site sends a bridge about its turns and calls; a cancel ends either. */
|
|
15
19
|
export type TurnFrame =
|
|
16
20
|
| ({ type: 'turn' } & TurnRequest)
|
|
21
|
+
| ({ type: 'call' } & CallRequest)
|
|
17
22
|
| {
|
|
18
23
|
type: 'reply'
|
|
19
24
|
turn: string
|
|
@@ -42,6 +47,8 @@ export type TurnReport =
|
|
|
42
47
|
}
|
|
43
48
|
| { type: 'end'; turn: string }
|
|
44
49
|
| { type: 'fail'; turn: string; message: string }
|
|
50
|
+
| { type: 'answer'; turn: string; answer: ToolAnswer }
|
|
51
|
+
| { type: 'refused'; turn: string; message: string }
|
|
45
52
|
|
|
46
53
|
/** A relayed MCP request the site has yet to reply to. */
|
|
47
54
|
type Waiting = {
|
|
@@ -118,15 +125,16 @@ function permissionServer(method: string, params: unknown): unknown {
|
|
|
118
125
|
* Serves the turns `site` sends, reporting each through `report`.
|
|
119
126
|
*
|
|
120
127
|
* `claudeArgs` answers what each turn runs `claude` with after the protocol's own flags — the visitor's,
|
|
121
|
-
* or for the native app's own bridge, the app's `onPairing` arguments in the turn's request (44.78)
|
|
128
|
+
* or for the native app's own bridge, the app's `onPairing` arguments in the turn's request (44.78) —
|
|
129
|
+
* and what each call is granted by (44.94).
|
|
122
130
|
* `full` gives a turn everything `claude` has, as the visitor running the native app has (44.78,
|
|
123
131
|
* 44.82); otherwise the visitor's `--tools` in `claudeArgs` narrows what a turn asks for (44.29).
|
|
124
|
-
* `beforeTurn` runs ahead of each turn.
|
|
132
|
+
* `beforeTurn` runs ahead of each turn and call.
|
|
125
133
|
*/
|
|
126
134
|
export function serveTurns(options: {
|
|
127
135
|
site: string
|
|
128
136
|
claude: string
|
|
129
|
-
claudeArgs: (
|
|
137
|
+
claudeArgs: (asked: { id: string }) => readonly string[]
|
|
130
138
|
full: boolean
|
|
131
139
|
report: (report: TurnReport) => void
|
|
132
140
|
say: (line: string) => void
|
|
@@ -249,15 +257,17 @@ export function serveTurns(options: {
|
|
|
249
257
|
},
|
|
250
258
|
})
|
|
251
259
|
|
|
260
|
+
// A cancel or a signal aborts with no reason of its own, and ends the turn as asked.
|
|
261
|
+
const missedBy = (controller: AbortController): string | undefined =>
|
|
262
|
+
controller.signal.reason instanceof MissedTurn
|
|
263
|
+
? controller.signal.reason.message
|
|
264
|
+
: undefined
|
|
265
|
+
|
|
252
266
|
const run = async (turn: TurnRequest): Promise<void> => {
|
|
253
267
|
const controller = new AbortController()
|
|
254
268
|
turns.set(turn.id, controller)
|
|
255
269
|
await options.beforeTurn()
|
|
256
|
-
|
|
257
|
-
const missed = (): string | undefined =>
|
|
258
|
-
controller.signal.reason instanceof MissedTurn
|
|
259
|
-
? controller.signal.reason.message
|
|
260
|
-
: undefined
|
|
270
|
+
const missed = (): string | undefined => missedBy(controller)
|
|
261
271
|
try {
|
|
262
272
|
await claudeTurn(turn, {
|
|
263
273
|
claude: options.claude,
|
|
@@ -288,12 +298,60 @@ export function serveTurns(options: {
|
|
|
288
298
|
}
|
|
289
299
|
}
|
|
290
300
|
|
|
301
|
+
// 44.93, 44.97 — A CALL IS MADE AS IT ARRIVES, beside every other call and turn, by the grant a turn
|
|
302
|
+
// naming its tool alone would have (44.94), on the visitor's server started for it alone (44.95).
|
|
303
|
+
const call = async (asked: CallRequest): Promise<void> => {
|
|
304
|
+
const controller = new AbortController()
|
|
305
|
+
turns.set(asked.id, controller)
|
|
306
|
+
try {
|
|
307
|
+
await options.beforeTurn()
|
|
308
|
+
const granted = await callGrant(
|
|
309
|
+
asked.name,
|
|
310
|
+
options.claudeArgs(asked),
|
|
311
|
+
options.full,
|
|
312
|
+
)
|
|
313
|
+
if (granted.refused !== undefined) {
|
|
314
|
+
options.say(granted.refused)
|
|
315
|
+
report({
|
|
316
|
+
type: 'refused',
|
|
317
|
+
turn: asked.id,
|
|
318
|
+
message: granted.refused,
|
|
319
|
+
})
|
|
320
|
+
return
|
|
321
|
+
}
|
|
322
|
+
const answer = await callTool(
|
|
323
|
+
granted.server,
|
|
324
|
+
granted.configuration,
|
|
325
|
+
granted.name,
|
|
326
|
+
asked.input,
|
|
327
|
+
controller.signal,
|
|
328
|
+
)
|
|
329
|
+
const message = missedBy(controller)
|
|
330
|
+
report(
|
|
331
|
+
message === undefined
|
|
332
|
+
? { type: 'answer', turn: asked.id, answer }
|
|
333
|
+
: { type: 'fail', turn: asked.id, message },
|
|
334
|
+
)
|
|
335
|
+
} catch (thrown) {
|
|
336
|
+
report({
|
|
337
|
+
type: 'fail',
|
|
338
|
+
turn: asked.id,
|
|
339
|
+
message:
|
|
340
|
+
missedBy(controller) ??
|
|
341
|
+
(thrown instanceof Error ? thrown.message : String(thrown)),
|
|
342
|
+
})
|
|
343
|
+
} finally {
|
|
344
|
+
turns.delete(asked.id)
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
291
348
|
const cancelAll = (): void => {
|
|
292
349
|
for (const controller of turns.values()) controller.abort()
|
|
293
350
|
}
|
|
294
351
|
return {
|
|
295
352
|
take(frame) {
|
|
296
353
|
if (frame.type === 'turn') void run(frame)
|
|
354
|
+
else if (frame.type === 'call') void call(frame)
|
|
297
355
|
else if (frame.type === 'reply') {
|
|
298
356
|
// A reply answers an MCP request, and a permit a prompt: neither stands in for the other.
|
|
299
357
|
if (waiting.get(frame.id)?.method !== 'permission')
|