@reclaimprotocol/agent 0.1.34 → 0.1.35
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/README.md +3 -3
- package/lib/mcp/api/operations.gen.js +340 -150
- package/lib/mcp/instructions.js +3 -3
- package/lib/mcp/start.js +8 -7
- package/lib/mcp/tools/agent/build.js +2 -0
- package/lib/mcp/tools/agent/check-attestor.d.ts +5 -0
- package/lib/mcp/tools/agent/check-attestor.js +37 -0
- package/lib/mcp/tools/agent/live-view.js +42 -18
- package/lib/mcp/tools/agent/proof.d.ts +12 -0
- package/lib/mcp/tools/agent/proof.js +32 -0
- package/lib/mcp/tools/agent/shared.d.ts +2 -1
- package/lib/mcp/tools/agent/shared.js +28 -24
- package/lib/mcp/tools/agent/start-integration.js +5 -4
- package/lib/mcp/tools/verification/build.d.ts +3 -1
- package/lib/mcp/tools/verification/build.js +10 -2
- package/lib/mcp/tools/verification/callback-listener.d.ts +51 -0
- package/lib/mcp/tools/verification/callback-listener.js +163 -0
- package/lib/mcp/tools/verification/index.d.ts +2 -0
- package/lib/mcp/tools/verification/index.js +2 -0
- package/lib/mcp/tools/verification/proxy-callback-events.d.ts +59 -0
- package/lib/mcp/tools/verification/proxy-callback-events.js +300 -0
- package/lib/mcp/tools/verification/read-callback-events.d.ts +3 -0
- package/lib/mcp/tools/verification/read-callback-events.js +135 -0
- package/lib/mcp/tools/verification/result.d.ts +6 -0
- package/lib/mcp/tools/verification/result.js +1 -1
- package/lib/proof/check-attestor.d.ts +67 -0
- package/lib/proof/check-attestor.js +156 -0
- package/package.json +2 -2
- package/skill/SKILL.builder.md +7 -1
- package/skill/SKILL.common.md +21 -9
- package/skill/builder-mode.md +17 -12
- package/skill/start-integration.md +36 -11
- package/skill/verification-client-integration.md +15 -9
- package/src/mcp/api/operations.gen.ts +344 -150
- package/src/mcp/instructions.ts +3 -3
- package/src/mcp/start.ts +9 -7
- package/src/mcp/tools/agent/build.ts +2 -0
- package/src/mcp/tools/agent/check-attestor.ts +49 -0
- package/src/mcp/tools/agent/live-view.ts +42 -18
- package/src/mcp/tools/agent/proof.ts +55 -0
- package/src/mcp/tools/agent/shared.ts +33 -23
- package/src/mcp/tools/agent/start-integration.ts +5 -4
- package/src/mcp/tools/verification/build.ts +10 -2
- package/src/mcp/tools/verification/callback-listener.ts +203 -0
- package/src/mcp/tools/verification/index.ts +2 -0
- package/src/mcp/tools/verification/proxy-callback-events.ts +374 -0
- package/src/mcp/tools/verification/read-callback-events.ts +160 -0
- package/src/mcp/tools/verification/result.ts +1 -1
- package/src/proof/check-attestor.ts +209 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { badRequest } from '@hapi/boom'
|
|
2
|
+
import type { ReclaimClient } from '@reclaimprotocol/client/api'
|
|
3
|
+
import assert from 'node:assert'
|
|
4
|
+
import { setTimeout as delay } from 'node:timers/promises'
|
|
5
|
+
import { LOGGER } from '../../../logger.ts'
|
|
6
|
+
import { defineTool, type RegisteredTool } from '../../server.ts'
|
|
7
|
+
import {
|
|
8
|
+
type CallbackDeps,
|
|
9
|
+
type CallbackEvent,
|
|
10
|
+
ensureListener,
|
|
11
|
+
type ListenerInfo,
|
|
12
|
+
orgClient,
|
|
13
|
+
} from './callback-listener.ts'
|
|
14
|
+
import { resolveOrgSecret } from './result.ts'
|
|
15
|
+
|
|
16
|
+
const POLL_MS = 2_000
|
|
17
|
+
const MAX_BACKOFF_MS = 30_000
|
|
18
|
+
const FORWARD_TIMEOUT_MS = 10_000
|
|
19
|
+
const BATCH = 50
|
|
20
|
+
const RECENT = 10
|
|
21
|
+
/** Loopback addresses, in the order tried. Dev servers often bind only one. */
|
|
22
|
+
const HOSTS = ['127.0.0.1', '[::1]']
|
|
23
|
+
|
|
24
|
+
interface ProxyArgs {
|
|
25
|
+
port?: number
|
|
26
|
+
path?: string
|
|
27
|
+
stop?: boolean
|
|
28
|
+
orgToken?: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface Target {
|
|
32
|
+
port: number
|
|
33
|
+
path: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type Recent = {
|
|
37
|
+
id: string
|
|
38
|
+
sessionId: string
|
|
39
|
+
event: string
|
|
40
|
+
status: 'delivered' | 'failed'
|
|
41
|
+
httpStatus?: number
|
|
42
|
+
at: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ProxyDeps extends CallbackDeps {
|
|
46
|
+
/** Delivers an event to the app. Defaults to global `fetch`. */
|
|
47
|
+
forwardFetch?: typeof globalThis.fetch
|
|
48
|
+
/** Waits between polls. Resolves early, without error, on abort. */
|
|
49
|
+
sleep?: (ms: number, signal: AbortSignal) => Promise<void>
|
|
50
|
+
registry?: ForwarderRegistry
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Polls the org's pending pull events and POSTs each envelope to a local
|
|
55
|
+
* route, like `stripe listen --forward-to`. 2xx responses are acknowledged;
|
|
56
|
+
* anything else (redirects included) stays pending and is retried with
|
|
57
|
+
* exponential backoff. After a failure the batch stops, so events reach the
|
|
58
|
+
* app in order.
|
|
59
|
+
*/
|
|
60
|
+
class Forwarder {
|
|
61
|
+
target: Target
|
|
62
|
+
listener: ListenerInfo
|
|
63
|
+
#client: ReclaimClient
|
|
64
|
+
#deps: ProxyDeps
|
|
65
|
+
#abort = new AbortController()
|
|
66
|
+
#forwarded = 0
|
|
67
|
+
#failed = 0
|
|
68
|
+
#lastError: string | undefined
|
|
69
|
+
#recent: Recent[] = []
|
|
70
|
+
/** Delivered to the app but not yet acknowledged; never POSTed twice. */
|
|
71
|
+
#unacked = new Set<string>()
|
|
72
|
+
#host = HOSTS[0]
|
|
73
|
+
#running: Promise<void>
|
|
74
|
+
|
|
75
|
+
constructor(
|
|
76
|
+
client: ReclaimClient,
|
|
77
|
+
listener: ListenerInfo,
|
|
78
|
+
target: Target,
|
|
79
|
+
deps: ProxyDeps,
|
|
80
|
+
) {
|
|
81
|
+
this.#client = client
|
|
82
|
+
this.listener = listener
|
|
83
|
+
this.target = target
|
|
84
|
+
this.#deps = deps
|
|
85
|
+
this.#running = this.#loop()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
status() {
|
|
89
|
+
return {
|
|
90
|
+
forwarding: !this.#abort.signal.aborted,
|
|
91
|
+
target: targetUrl(this.target, HOSTS[0]),
|
|
92
|
+
listener: this.listener,
|
|
93
|
+
forwarded: this.#forwarded,
|
|
94
|
+
failed: this.#failed,
|
|
95
|
+
...(this.#lastError ? { lastError: this.#lastError } : {}),
|
|
96
|
+
recent: [...this.#recent],
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async stop() {
|
|
101
|
+
this.#abort.abort()
|
|
102
|
+
await this.#running
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async #loop() {
|
|
106
|
+
const sleep = this.#deps.sleep || sleepFor
|
|
107
|
+
const signal = this.#abort.signal
|
|
108
|
+
let backoff = 0
|
|
109
|
+
while(!signal.aborted) {
|
|
110
|
+
let wait = POLL_MS
|
|
111
|
+
try {
|
|
112
|
+
const { ok, count } = await this.#tick()
|
|
113
|
+
backoff = ok ? 0 : nextBackoff(backoff)
|
|
114
|
+
wait = ok ? (count >= BATCH ? 0 : POLL_MS) : backoff
|
|
115
|
+
} catch(err) {
|
|
116
|
+
this.#lastError = err instanceof Error ? err.message : String(err)
|
|
117
|
+
LOGGER.warn({ err }, 'callback proxy poll failed')
|
|
118
|
+
backoff = nextBackoff(backoff)
|
|
119
|
+
wait = backoff
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if(wait && !signal.aborted) {
|
|
123
|
+
await sleep(wait, signal)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async #tick() {
|
|
129
|
+
const { data } = await this.#client.call('ListCallbackEvents', {
|
|
130
|
+
query: { pending: true, limit: BATCH },
|
|
131
|
+
})
|
|
132
|
+
const ack: string[] = []
|
|
133
|
+
let ok = true
|
|
134
|
+
for(const item of data.items) {
|
|
135
|
+
if(this.#abort.signal.aborted) {
|
|
136
|
+
break
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if(this.#unacked.has(item.id) || await this.#forward(item)) {
|
|
140
|
+
ack.push(item.id)
|
|
141
|
+
} else {
|
|
142
|
+
// Stop the batch so later events don't overtake the failed one.
|
|
143
|
+
ok = false
|
|
144
|
+
break
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if(ack.length) {
|
|
149
|
+
for(const id of ack) {
|
|
150
|
+
this.#unacked.add(id)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
await this.#client.call('AcknowledgeCallbackEvents', {
|
|
154
|
+
body: { ids: ack },
|
|
155
|
+
})
|
|
156
|
+
for(const id of ack) {
|
|
157
|
+
this.#unacked.delete(id)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return { ok, count: data.items.length }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async #forward(item: CallbackEvent) {
|
|
165
|
+
const doFetch = this.#deps.forwardFetch || globalThis.fetch
|
|
166
|
+
const record = (status: Recent['status'], httpStatus?: number) => {
|
|
167
|
+
this.#recent.push({
|
|
168
|
+
id: item.id,
|
|
169
|
+
sessionId: item.sessionId,
|
|
170
|
+
event: item.event,
|
|
171
|
+
status,
|
|
172
|
+
...(httpStatus === undefined ? {} : { httpStatus }),
|
|
173
|
+
at: new Date().toISOString(),
|
|
174
|
+
})
|
|
175
|
+
this.#recent.splice(0, this.#recent.length - RECENT)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const hosts = [this.#host, ...HOSTS.filter((h) => h !== this.#host)]
|
|
179
|
+
for(const host of hosts) {
|
|
180
|
+
try {
|
|
181
|
+
const res = await doFetch(targetUrl(this.target, host), {
|
|
182
|
+
method: 'POST',
|
|
183
|
+
headers: { 'Content-Type': 'application/json' },
|
|
184
|
+
body: JSON.stringify(item.envelope),
|
|
185
|
+
signal: AbortSignal.timeout(FORWARD_TIMEOUT_MS),
|
|
186
|
+
// Never follow a redirect: it could re-POST the signed result
|
|
187
|
+
// to a host outside this machine.
|
|
188
|
+
redirect: 'manual',
|
|
189
|
+
})
|
|
190
|
+
this.#host = host
|
|
191
|
+
if(res.ok) {
|
|
192
|
+
this.#forwarded += 1
|
|
193
|
+
record('delivered', res.status)
|
|
194
|
+
return true
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
this.#failed += 1
|
|
198
|
+
const url = targetUrl(this.target, host)
|
|
199
|
+
this.#lastError = res.status >= 300 && res.status < 400
|
|
200
|
+
? `${url} redirected (HTTP ${res.status}). Redirects are not `
|
|
201
|
+
+ 'followed; call again with the redirect target\'s `path`.'
|
|
202
|
+
: `${url} returned HTTP ${res.status}`
|
|
203
|
+
record('failed', res.status)
|
|
204
|
+
return false
|
|
205
|
+
} catch(err) {
|
|
206
|
+
this.#lastError = err instanceof Error ? err.message : String(err)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// No loopback address accepted the connection.
|
|
211
|
+
this.#failed += 1
|
|
212
|
+
record('failed')
|
|
213
|
+
return false
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The forwarders of one MCP process, one per org key. */
|
|
218
|
+
export class ForwarderRegistry {
|
|
219
|
+
#forwarders = new Map<string, Forwarder>()
|
|
220
|
+
|
|
221
|
+
get(secret: string) {
|
|
222
|
+
return this.#forwarders.get(secret)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
set(secret: string, forwarder: Forwarder) {
|
|
226
|
+
this.#forwarders.set(secret, forwarder)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async stop(secret: string) {
|
|
230
|
+
const forwarder = this.#forwarders.get(secret)
|
|
231
|
+
this.#forwarders.delete(secret)
|
|
232
|
+
await forwarder?.stop()
|
|
233
|
+
return forwarder
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async stopAll() {
|
|
237
|
+
const all = [...this.#forwarders.values()]
|
|
238
|
+
this.#forwarders.clear()
|
|
239
|
+
await Promise.all(all.map((f) => f.stop()))
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const defaultRegistry = new ForwarderRegistry()
|
|
244
|
+
|
|
245
|
+
/** Stops every forwarder. Called when the MCP server shuts down. */
|
|
246
|
+
export function stopAllForwarders() {
|
|
247
|
+
return defaultRegistry.stopAll()
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function proxyCallbackEventsTool(deps: ProxyDeps = {}): RegisteredTool {
|
|
251
|
+
const registry = deps.registry || defaultRegistry
|
|
252
|
+
return defineTool<ProxyArgs>(
|
|
253
|
+
{
|
|
254
|
+
name: 'proxy_callback_events',
|
|
255
|
+
description:
|
|
256
|
+
'Forwards the events Reclaim sends to the org\'s pull listener '
|
|
257
|
+
+ 'to a local route, like `stripe listen --forward-to`. Call it '
|
|
258
|
+
+ 'when the app\'s callback route runs locally. It POSTs each '
|
|
259
|
+
+ 'envelope as JSON to `http://127.0.0.1:<port><path>` (loopback '
|
|
260
|
+
+ 'only; also tries `[::1]`) in order, acknowledges 2xx '
|
|
261
|
+
+ 'responses, and retries anything else, redirects included, '
|
|
262
|
+
+ 'with backoff. Creates the listener on first use, so it is not '
|
|
263
|
+
+ 'read-only. One forwarder runs per org: call again to read '
|
|
264
|
+
+ 'status, or with another `port` or `path` to retarget. '
|
|
265
|
+
+ '`stop: true` stops forwarding only. Forwarding lasts only '
|
|
266
|
+
+ 'while this MCP server runs; production needs '
|
|
267
|
+
+ '`create_org_callback_subscription`. Returns `{ forwarding, '
|
|
268
|
+
+ 'target, listener, forwarded, failed, lastError?, recent }`; '
|
|
269
|
+
+ '`listener.expiresAt` is the lease end, and `recent` lists the '
|
|
270
|
+
+ 'last events with `status` and `httpStatus`.',
|
|
271
|
+
inputSchema: {
|
|
272
|
+
type: 'object',
|
|
273
|
+
properties: {
|
|
274
|
+
port: {
|
|
275
|
+
type: 'integer',
|
|
276
|
+
minimum: 1,
|
|
277
|
+
maximum: 65535,
|
|
278
|
+
description: 'The local port the app listens on.',
|
|
279
|
+
},
|
|
280
|
+
path: {
|
|
281
|
+
type: 'string',
|
|
282
|
+
pattern: '^/[^\\s#\\\\]*$',
|
|
283
|
+
maxLength: 500,
|
|
284
|
+
description:
|
|
285
|
+
'The callback route, starting with `/`. Defaults to `/`.',
|
|
286
|
+
},
|
|
287
|
+
stop: {
|
|
288
|
+
type: 'boolean',
|
|
289
|
+
description: 'Stop forwarding. `port` is not needed.',
|
|
290
|
+
},
|
|
291
|
+
orgToken: {
|
|
292
|
+
type: 'string',
|
|
293
|
+
pattern: '^rorg_',
|
|
294
|
+
maxLength: 200,
|
|
295
|
+
description: 'The organization API key (`rorg_…`). Defaults to '
|
|
296
|
+
+ 'the server\'s `RECLAIM_ORG_SECRET`, then the key saved by '
|
|
297
|
+
+ '`issue_org_token`. Never echo it back to the user.',
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
annotations: {
|
|
302
|
+
readOnlyHint: false,
|
|
303
|
+
idempotentHint: true,
|
|
304
|
+
openWorldHint: true,
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
async(args) => {
|
|
308
|
+
const secret = resolveOrgSecret(args.orgToken)
|
|
309
|
+
if(args.stop) {
|
|
310
|
+
const stopped = await registry.stop(secret)
|
|
311
|
+
return {
|
|
312
|
+
...(stopped?.status() || { forwarding: false }),
|
|
313
|
+
_notes: 'Forwarding stopped. The listener stays alive until the '
|
|
314
|
+
+ 'MCP server exits, so `read_callback_events` still works.',
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const existing = registry.get(secret)
|
|
319
|
+
if(args.port === undefined) {
|
|
320
|
+
assert(
|
|
321
|
+
existing,
|
|
322
|
+
badRequest('Pass `port`, the local port the app listens on.'),
|
|
323
|
+
)
|
|
324
|
+
existing.listener = await ensureListener(secret, deps)
|
|
325
|
+
return existing.status()
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const target = validateTarget(args.port, args.path ?? '/')
|
|
329
|
+
if(existing) {
|
|
330
|
+
existing.target = target
|
|
331
|
+
existing.listener = await ensureListener(secret, deps)
|
|
332
|
+
return existing.status()
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const client = orgClient(secret, deps)
|
|
336
|
+
const listener = await ensureListener(secret, deps)
|
|
337
|
+
const forwarder = new Forwarder(client, listener, target, deps)
|
|
338
|
+
registry.set(secret, forwarder)
|
|
339
|
+
return forwarder.status()
|
|
340
|
+
},
|
|
341
|
+
)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** Rejects anything that is not a path on a loopback port. */
|
|
345
|
+
export function validateTarget(port: number, path: string): Target {
|
|
346
|
+
assert(
|
|
347
|
+
Number.isInteger(port) && port >= 1 && port <= 65535,
|
|
348
|
+
badRequest('`port` must be an integer from 1 to 65535.'),
|
|
349
|
+
)
|
|
350
|
+
assert(
|
|
351
|
+
path.length <= 500 && /^\/[^\s#\\]*$/.test(path),
|
|
352
|
+
badRequest(
|
|
353
|
+
'`path` must start with "/" and contain no host, space, or "#".',
|
|
354
|
+
),
|
|
355
|
+
)
|
|
356
|
+
const url = new URL(targetUrl({ port, path }, HOSTS[0]))
|
|
357
|
+
assert(
|
|
358
|
+
url.hostname === HOSTS[0] && url.port === String(port),
|
|
359
|
+
badRequest('`path` must not change the target host.'),
|
|
360
|
+
)
|
|
361
|
+
return { port, path }
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function targetUrl(target: Target, host: string) {
|
|
365
|
+
return `http://${host}:${target.port}${target.path}`
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async function sleepFor(ms: number, signal: AbortSignal) {
|
|
369
|
+
await delay(ms, undefined, { signal }).catch(() => undefined)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function nextBackoff(current: number) {
|
|
373
|
+
return Math.min(current ? current * 2 : POLL_MS, MAX_BACKOFF_MS)
|
|
374
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { decryptCallback, ReclaimVerification } from '@reclaimprotocol/client'
|
|
2
|
+
import { apiUrl } from '../../../consts.ts'
|
|
3
|
+
import { defineTool, type RegisteredTool } from '../../server.ts'
|
|
4
|
+
import { resolveDecryptionKey } from '../authenticate/decrypt-key.ts'
|
|
5
|
+
import {
|
|
6
|
+
type CallbackDeps,
|
|
7
|
+
type CallbackEvent,
|
|
8
|
+
ensureListener,
|
|
9
|
+
orgClient,
|
|
10
|
+
} from './callback-listener.ts'
|
|
11
|
+
import { resolveOrgSecret } from './result.ts'
|
|
12
|
+
|
|
13
|
+
interface ReadArgs {
|
|
14
|
+
sessionId?: string
|
|
15
|
+
after?: string
|
|
16
|
+
limit?: number
|
|
17
|
+
pending?: boolean
|
|
18
|
+
orgToken?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Events whose `data` is the signed result (encrypted or plaintext JWS). */
|
|
22
|
+
const RESULT_EVENTS = new Set([
|
|
23
|
+
'verification_success',
|
|
24
|
+
'verification_rejected',
|
|
25
|
+
'verification_error',
|
|
26
|
+
'verification_cancelled',
|
|
27
|
+
])
|
|
28
|
+
|
|
29
|
+
export function readCallbackEventsTool(
|
|
30
|
+
deps: CallbackDeps = {},
|
|
31
|
+
): RegisteredTool {
|
|
32
|
+
return defineTool<ReadArgs>(
|
|
33
|
+
{
|
|
34
|
+
name: 'read_callback_events',
|
|
35
|
+
description:
|
|
36
|
+
'Reads the events Reclaim sent to the organization\'s pull '
|
|
37
|
+
+ 'listener, and decodes them: each result event is decrypted and '
|
|
38
|
+
+ 'signature-verified into `result`, and each lifecycle event '
|
|
39
|
+
+ 'returns its `eventData`. Call it to try a provider without an '
|
|
40
|
+
+ 'app or to check what Reclaim sent for a session. Creates the '
|
|
41
|
+
+ 'listener on first use, so it is not read-only. Filter with '
|
|
42
|
+
+ '`sessionId`, or pass the previous `next` as `after` to read on. '
|
|
43
|
+
+ 'Items with a lower sequence can appear late, so for a complete '
|
|
44
|
+
+ 'read use `pending: true`. Returns `{ listener, items, next }`. '
|
|
45
|
+
+ 'Encrypted results need the `RECLAIM_DECRYPT_KEY` that matches '
|
|
46
|
+
+ 'the org keypair. To deliver events to your app\'s callback '
|
|
47
|
+
+ 'route, call `proxy_callback_events`.',
|
|
48
|
+
inputSchema: {
|
|
49
|
+
type: 'object',
|
|
50
|
+
properties: {
|
|
51
|
+
sessionId: {
|
|
52
|
+
type: 'string',
|
|
53
|
+
format: 'uuid',
|
|
54
|
+
description: 'Only events of this verification session.',
|
|
55
|
+
},
|
|
56
|
+
after: {
|
|
57
|
+
type: 'string',
|
|
58
|
+
pattern: '^[0-9]{1,19}$',
|
|
59
|
+
description: 'The `next` value of a previous call.',
|
|
60
|
+
},
|
|
61
|
+
limit: {
|
|
62
|
+
type: 'integer',
|
|
63
|
+
minimum: 1,
|
|
64
|
+
maximum: 100,
|
|
65
|
+
description: 'Events to return. Defaults to 50.',
|
|
66
|
+
},
|
|
67
|
+
pending: {
|
|
68
|
+
type: 'boolean',
|
|
69
|
+
description:
|
|
70
|
+
'Only events not yet acknowledged. `proxy_callback_events` '
|
|
71
|
+
+ 'acknowledges the events it delivers.',
|
|
72
|
+
},
|
|
73
|
+
orgToken: {
|
|
74
|
+
type: 'string',
|
|
75
|
+
pattern: '^rorg_',
|
|
76
|
+
maxLength: 200,
|
|
77
|
+
description: 'The organization API key (`rorg_…`). Defaults to '
|
|
78
|
+
+ 'the server\'s `RECLAIM_ORG_SECRET`, then the key saved by '
|
|
79
|
+
+ '`issue_org_token`. Never echo it back to the user.',
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
annotations: {
|
|
84
|
+
readOnlyHint: false,
|
|
85
|
+
idempotentHint: true,
|
|
86
|
+
openWorldHint: true,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
async(args) => {
|
|
90
|
+
const secret = resolveOrgSecret(args.orgToken)
|
|
91
|
+
const client = orgClient(secret, deps)
|
|
92
|
+
const listener = await ensureListener(secret, deps)
|
|
93
|
+
const { data } = await client.call('ListCallbackEvents', {
|
|
94
|
+
query: {
|
|
95
|
+
after: args.after,
|
|
96
|
+
limit: args.limit,
|
|
97
|
+
sessionId: args.sessionId,
|
|
98
|
+
pending: args.pending,
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
const hasResults = data.items.some((e) => RESULT_EVENTS.has(e.event))
|
|
103
|
+
const key = hasResults ? await resolveDecryptionKey() : undefined
|
|
104
|
+
const reclaim = ReclaimVerification.create({
|
|
105
|
+
baseUrl: apiUrl(),
|
|
106
|
+
orgSecret: secret,
|
|
107
|
+
fetch: deps.fetch,
|
|
108
|
+
})
|
|
109
|
+
const items = []
|
|
110
|
+
for(const item of data.items) {
|
|
111
|
+
items.push(await decode(item))
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
listener,
|
|
116
|
+
items,
|
|
117
|
+
next: data.next,
|
|
118
|
+
...(listener.created
|
|
119
|
+
? {
|
|
120
|
+
_notes: 'Created the pull listener; only sessions created from '
|
|
121
|
+
+ 'now on are recorded. It works only while this MCP server '
|
|
122
|
+
+ 'runs.',
|
|
123
|
+
}
|
|
124
|
+
: {}),
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function decode(item: CallbackEvent) {
|
|
128
|
+
const base = {
|
|
129
|
+
id: item.id,
|
|
130
|
+
sessionId: item.sessionId,
|
|
131
|
+
event: item.event,
|
|
132
|
+
deliveryStatus: item.deliveryStatus,
|
|
133
|
+
createdAt: item.createdAt,
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
if(!RESULT_EVENTS.has(item.event)) {
|
|
137
|
+
const received = await reclaim.results.receive(item.envelope)
|
|
138
|
+
return received.kind === 'event'
|
|
139
|
+
? { ...base, eventData: received.eventData }
|
|
140
|
+
: base
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const jws = await decryptCallback(key, item.envelope)
|
|
144
|
+
const outcome = await reclaim.results.verify(jws, {
|
|
145
|
+
reclaimSessionId: item.sessionId,
|
|
146
|
+
})
|
|
147
|
+
return {
|
|
148
|
+
...base,
|
|
149
|
+
result: { payload: outcome.payload, proofs: outcome.proofs },
|
|
150
|
+
}
|
|
151
|
+
} catch(err) {
|
|
152
|
+
const message = err instanceof Error ? err.message : String(err)
|
|
153
|
+
return RESULT_EVENTS.has(item.event)
|
|
154
|
+
? { ...base, resultError: message }
|
|
155
|
+
: { ...base, eventError: message }
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
)
|
|
160
|
+
}
|
|
@@ -32,7 +32,7 @@ const RESULT_EVENTS = new Set([
|
|
|
32
32
|
* single per-org bearer secret (`rorg_…`), not an eth signature: the call's
|
|
33
33
|
* `orgToken`, else `RECLAIM_ORG_SECRET`.
|
|
34
34
|
*/
|
|
35
|
-
function resolveOrgSecret(orgToken?: string): string {
|
|
35
|
+
export function resolveOrgSecret(orgToken?: string): string {
|
|
36
36
|
const secret = orgToken?.trim() || orgSecret()?.trim() || savedOrgKey()
|
|
37
37
|
assert(
|
|
38
38
|
secret,
|