@zackbart/connecta 0.6.0 → 0.7.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 +403 -0
- package/README.md +163 -308
- package/dist/auth/bearer.d.ts +4 -3
- package/dist/auth/bearer.d.ts.map +1 -1
- package/dist/auth/bearer.js +10 -8
- package/dist/auth/bearer.js.map +1 -1
- package/dist/auth/clerk.d.ts +8 -7
- package/dist/auth/clerk.d.ts.map +1 -1
- package/dist/auth/clerk.js +27 -8
- package/dist/auth/clerk.js.map +1 -1
- package/dist/connector-scope.d.ts +13 -0
- package/dist/connector-scope.d.ts.map +1 -0
- package/dist/connector-scope.js +35 -0
- package/dist/connector-scope.js.map +1 -0
- package/dist/connectors/api.d.ts +5 -5
- package/dist/connectors/api.d.ts.map +1 -1
- package/dist/connectors/remote-mcp.d.ts +3 -3
- package/dist/connectors/remote-mcp.d.ts.map +1 -1
- package/dist/connectors/remote-mcp.js +309 -10
- package/dist/connectors/remote-mcp.js.map +1 -1
- package/dist/credential-health.d.ts +20 -9
- package/dist/credential-health.d.ts.map +1 -1
- package/dist/credential-health.js +127 -63
- package/dist/credential-health.js.map +1 -1
- package/dist/credentials.d.ts +84 -1
- package/dist/credentials.d.ts.map +1 -1
- package/dist/credentials.js +109 -2
- package/dist/credentials.js.map +1 -1
- package/dist/index.d.ts +83 -82
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +101 -31
- package/dist/index.js.map +1 -1
- package/dist/meta-tools.d.ts +3 -3
- package/dist/meta-tools.d.ts.map +1 -1
- package/dist/meta-tools.js +16 -7
- package/dist/meta-tools.js.map +1 -1
- package/dist/registry.d.ts +3 -2
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +4 -3
- package/dist/registry.js.map +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +154 -52
- package/dist/server.js.map +1 -1
- package/dist/skills.js +2 -2
- package/dist/skills.js.map +1 -1
- package/dist/toolkits.js +1 -1
- package/dist/types.d.ts +51 -26
- package/dist/types.d.ts.map +1 -1
- package/dist/ui.d.ts +52 -21
- package/dist/ui.d.ts.map +1 -1
- package/dist/ui.js +665 -196
- package/dist/ui.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -2
- package/src/auth/bearer.ts +10 -8
- package/src/auth/clerk.ts +28 -9
- package/src/connector-scope.ts +41 -0
- package/src/connectors/api.ts +5 -5
- package/src/connectors/remote-mcp.ts +348 -25
- package/src/credential-health.ts +151 -71
- package/src/credentials.ts +166 -3
- package/src/index.ts +202 -113
- package/src/meta-tools.ts +22 -7
- package/src/registry.ts +4 -3
- package/src/server.ts +197 -71
- package/src/skills.ts +2 -2
- package/src/toolkits.ts +1 -1
- package/src/types.ts +51 -26
- package/src/ui.ts +703 -195
- package/src/version.ts +1 -1
|
@@ -26,9 +26,9 @@ export interface RemoteMcpOptions {
|
|
|
26
26
|
/**
|
|
27
27
|
* Max inline result size (bytes) for this connector's tools before
|
|
28
28
|
* call_tool/batch_call truncate and stash the full text for get_result
|
|
29
|
-
* paging. Overrides the deployment's `maxResultBytes`; omit to inherit
|
|
30
|
-
* Must be a whole number of bytes >= 1; anything else warns at startup
|
|
31
|
-
* is ignored.
|
|
29
|
+
* paging. Overrides the deployment's `calls.maxResultBytes`; omit to inherit
|
|
30
|
+
* it. Must be a whole number of bytes >= 1; anything else warns at startup
|
|
31
|
+
* and is ignored.
|
|
32
32
|
*/
|
|
33
33
|
maxResultBytes?: number;
|
|
34
34
|
/**
|
|
@@ -58,10 +58,147 @@ export interface RemoteMcpOptions {
|
|
|
58
58
|
_transportFactory?: (ctx: ConnectorContext) => Transport;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* How long a downstream gets to answer the session-termination DELETE before
|
|
63
|
+
* teardown stops waiting. Deliberately a fraction of the core's scope-close
|
|
64
|
+
* budget (`closeConnectorScope`), so the whole teardown still lands inside the
|
|
65
|
+
* window that budget makes safe on an edge runtime: whatever is still in flight
|
|
66
|
+
* when this expires is aborted by the close that immediately follows. What that
|
|
67
|
+
* costs is only the acknowledgement — the DELETE is headers-only and has long
|
|
68
|
+
* since gone out — so a slow provider still usually ends the session; it just
|
|
69
|
+
* does not get to tell us so.
|
|
70
|
+
*/
|
|
71
|
+
const TERMINATE_SESSION_BUDGET_MS = 50;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Ceiling on the tools one catalog refresh will accumulate while walking
|
|
75
|
+
* `tools/list`.
|
|
76
|
+
*
|
|
77
|
+
* This is the bound that matters, and pages are the wrong dimension to put it
|
|
78
|
+
* in: the *server* picks the page size, so a page ceiling is a tool ceiling
|
|
79
|
+
* multiplied by a number connecta can neither observe in advance nor control.
|
|
80
|
+
* At ten tools a page, 100 pages is 1,000 tools; at a hundred, 10,000 — and
|
|
81
|
+
* connecta's own large-catalog envelope is benchmarked to 100,000 (issue #82).
|
|
82
|
+
* A page ceiling low enough to be a real defense therefore sits *inside* the
|
|
83
|
+
* catalog sizes this product exists to serve. Worse, the common conformant
|
|
84
|
+
* idiom is to advertise a `nextCursor` whenever a page came back full and then
|
|
85
|
+
* serve one empty page to terminate, so a perfectly well-behaved 10,000-tool
|
|
86
|
+
* server paging at 100 spends 101 requests: bound the pages and its entire
|
|
87
|
+
* catalog fails, for doing nothing wrong.
|
|
88
|
+
*
|
|
89
|
+
* So the ceiling goes on accumulated tools — the thing actually held in memory
|
|
90
|
+
* — and it sits at the top of the benchmarked envelope rather than below it.
|
|
91
|
+
* Deliberately the same philosophy as issue #82's discovery-response bounds:
|
|
92
|
+
* cap the bytes a caller can be made to hold, not the number of round trips it
|
|
93
|
+
* took to get them.
|
|
94
|
+
*/
|
|
95
|
+
const MAX_TOOLS = 100_000;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Absolute backstop on `tools/list` pages in one refresh — a runaway guard, not
|
|
99
|
+
* the primary defense.
|
|
100
|
+
*
|
|
101
|
+
* The walk terminates on its own well before this: a cursor handed back twice
|
|
102
|
+
* is a definite loop, two consecutive pages that add no new tools are a server
|
|
103
|
+
* going nowhere, and MAX_TOOLS caps what any of it can accumulate. This exists
|
|
104
|
+
* only so the loop is finite even if a downstream somehow satisfies all three
|
|
105
|
+
* forever, because the caller's probe deadline abandons the *caller*, not the
|
|
106
|
+
* loop. Set high enough that no honest server reaches it.
|
|
107
|
+
*/
|
|
108
|
+
const MAX_TOOL_PAGES = 10_000;
|
|
109
|
+
|
|
110
|
+
/** One entry of the SDK's `tools/list` result, before it becomes a ToolDef. */
|
|
111
|
+
type ListedTool = Awaited<ReturnType<Client["listTools"]>>["tools"][number];
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Re-prime an SDK client's tool-metadata cache from the *full* walked catalog.
|
|
115
|
+
*
|
|
116
|
+
* `Client.listTools()` ends by calling its private `cacheToolMetadata`, which
|
|
117
|
+
* **clears** the output-schema validators and the task-support sets before
|
|
118
|
+
* repopulating them from the page it just received. Call it once per page —
|
|
119
|
+
* which walking the chain necessarily does — and the request-scoped client is
|
|
120
|
+
* left holding metadata for the *last* page alone. `callTool` then finds no
|
|
121
|
+
* validator for every earlier-page tool and silently skips both the "declared
|
|
122
|
+
* an outputSchema but returned no structuredContent" check and the
|
|
123
|
+
* structured-content validation, and finds no task requirement so a
|
|
124
|
+
* required-task tool is dispatched as a plain `tools/call`. Enforcement would
|
|
125
|
+
* depend on which page a tool happened to land on, which is not enforcement.
|
|
126
|
+
*
|
|
127
|
+
* So hand the whole aggregated list back deliberately, once, at the end. The
|
|
128
|
+
* SDK types the method `private`, hence the cast; the SDK version is pinned
|
|
129
|
+
* exactly and `test/remote-mcp-pagination.test.ts` asserts the method still
|
|
130
|
+
* exists, so a bump that renames it fails CI rather than quietly restoring the
|
|
131
|
+
* bug.
|
|
132
|
+
*/
|
|
133
|
+
function primeToolMetadata(client: Client, tools: ListedTool[]): void {
|
|
134
|
+
const prime = (
|
|
135
|
+
client as unknown as {
|
|
136
|
+
cacheToolMetadata?: (tools: ListedTool[]) => void;
|
|
137
|
+
}
|
|
138
|
+
).cacheToolMetadata;
|
|
139
|
+
if (typeof prime !== "function") return;
|
|
140
|
+
prime.call(client, tools);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* True for a result-parse failure caused by the page's `nextCursor` itself —
|
|
145
|
+
* in practice `nextCursor: null`, a very common JSON idiom for "no more pages"
|
|
146
|
+
* that the MCP schema does not accept (the chain ends on an *absent* cursor).
|
|
147
|
+
* Duck-typed rather than `instanceof ZodError`: the SDK may parse with its own
|
|
148
|
+
* zod instance, and cross-instance `instanceof` is a coin flip.
|
|
149
|
+
*/
|
|
150
|
+
function isCursorShapeError(err: unknown): boolean {
|
|
151
|
+
const issues = (err as { issues?: unknown } | null)?.issues;
|
|
152
|
+
return (
|
|
153
|
+
Array.isArray(issues) &&
|
|
154
|
+
issues.some((issue) => {
|
|
155
|
+
const path = (issue as { path?: unknown }).path;
|
|
156
|
+
return Array.isArray(path) && path[0] === "nextCursor";
|
|
157
|
+
})
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
61
161
|
function msg(err: unknown): string {
|
|
62
162
|
return err instanceof Error ? err.message : String(err);
|
|
63
163
|
}
|
|
64
164
|
|
|
165
|
+
/**
|
|
166
|
+
* End the downstream's session before the connection is torn down.
|
|
167
|
+
*
|
|
168
|
+
* `Client.close()` only unwinds our side — it aborts the transport's controller
|
|
169
|
+
* and fires `onclose`. Spec session termination is a separate DELETE carrying
|
|
170
|
+
* `Mcp-Session-Id`, and without it a stateful provider keeps the session alive
|
|
171
|
+
* until its own (often hour-long) timeout, which a periodic probe would then
|
|
172
|
+
* accumulate several of per connector.
|
|
173
|
+
*
|
|
174
|
+
* Ordering is load-bearing: the SDK sends that DELETE on the transport's
|
|
175
|
+
* AbortSignal, so calling this *after* close would abort the request on issue
|
|
176
|
+
* and silently do nothing. Everything else is best-effort — a transport with no
|
|
177
|
+
* `terminateSession` (a custom one, or an older SDK), a downstream that refuses
|
|
178
|
+
* (405 is a legal answer), errors, or never replies all fall through to the
|
|
179
|
+
* close with the session left to age out as it did before.
|
|
180
|
+
*/
|
|
181
|
+
async function terminateSession(transport: Transport): Promise<void> {
|
|
182
|
+
const terminate = (
|
|
183
|
+
transport as Transport & { terminateSession?: () => Promise<void> }
|
|
184
|
+
).terminateSession;
|
|
185
|
+
if (typeof terminate !== "function") return;
|
|
186
|
+
// The SDK issues no request at all when no `mcp-session-id` was captured, so
|
|
187
|
+
// a stateless downstream never sees a spurious DELETE.
|
|
188
|
+
const done = (async () => terminate.call(transport))().catch(() => {
|
|
189
|
+
// The session is being abandoned either way; a refusal changes nothing.
|
|
190
|
+
// Caught here rather than at the await below so a late rejection — one
|
|
191
|
+
// arriving after the budget expired — is still consumed.
|
|
192
|
+
});
|
|
193
|
+
await new Promise<void>((resolve) => {
|
|
194
|
+
const timer = setTimeout(resolve, TERMINATE_SESSION_BUDGET_MS);
|
|
195
|
+
done.then(() => {
|
|
196
|
+
clearTimeout(timer);
|
|
197
|
+
resolve();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
65
202
|
function isLoopbackHost(hostname: string): boolean {
|
|
66
203
|
return (
|
|
67
204
|
hostname === "localhost" ||
|
|
@@ -78,6 +215,12 @@ interface ConnectionState {
|
|
|
78
215
|
authRequired: boolean;
|
|
79
216
|
provider: KvOAuthProvider | null;
|
|
80
217
|
connectedGeneration: number | null;
|
|
218
|
+
/**
|
|
219
|
+
* One-way latch: set by closeScope and never cleared, so neither a late
|
|
220
|
+
* connect nor a `reset()` can cache a client into a scope that is already
|
|
221
|
+
* gone — that client would have no owner left to close it.
|
|
222
|
+
*/
|
|
223
|
+
closed: boolean;
|
|
81
224
|
}
|
|
82
225
|
|
|
83
226
|
/**
|
|
@@ -126,6 +269,31 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
|
|
|
126
269
|
{ cause },
|
|
127
270
|
);
|
|
128
271
|
|
|
272
|
+
const scopeEndedError = () =>
|
|
273
|
+
new Error(`Connector "${id}" scope ended during connection.`);
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* One `tools/list` request. The only thing wrapped here is the diagnosis of a
|
|
277
|
+
* `nextCursor` the MCP result schema refuses — overwhelmingly `null`, which
|
|
278
|
+
* plenty of servers use to mean "no more pages" but the spec spells as an
|
|
279
|
+
* *absent* cursor. The SDK surfaces that as a raw validation dump about a
|
|
280
|
+
* field the operator never sees; say which server broke which rule instead.
|
|
281
|
+
* Accepting `null` as end-of-chain outright is issue #99.
|
|
282
|
+
*/
|
|
283
|
+
const listPage = async (client: Client, cursor: string | undefined) => {
|
|
284
|
+
try {
|
|
285
|
+
return await client.listTools(
|
|
286
|
+
cursor === undefined ? undefined : { cursor },
|
|
287
|
+
);
|
|
288
|
+
} catch (err) {
|
|
289
|
+
if (!isCursorShapeError(err)) throw err;
|
|
290
|
+
throw new Error(
|
|
291
|
+
`Connector "${id}" returned a tools/list page whose nextCursor is neither a string nor absent (a null cursor is the usual culprit) — MCP ends pagination on an absent nextCursor, so this catalog cannot be walked.`,
|
|
292
|
+
{ cause: err },
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
129
297
|
const stateFor = (ctx: ConnectorContext): ConnectionState => {
|
|
130
298
|
const scope = ctx.requestScope ?? ctx;
|
|
131
299
|
let state = states.get(scope);
|
|
@@ -137,6 +305,7 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
|
|
|
137
305
|
authRequired: false,
|
|
138
306
|
provider: null,
|
|
139
307
|
connectedGeneration: null,
|
|
308
|
+
closed: false,
|
|
140
309
|
};
|
|
141
310
|
states.set(scope, state);
|
|
142
311
|
}
|
|
@@ -188,6 +357,7 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
|
|
|
188
357
|
state.connecting = null;
|
|
189
358
|
state.authRequired = false;
|
|
190
359
|
state.connectedGeneration = null;
|
|
360
|
+
// `closed` is deliberately not cleared — see ConnectionState.
|
|
191
361
|
};
|
|
192
362
|
|
|
193
363
|
const ensureConnected = async (
|
|
@@ -198,17 +368,18 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
|
|
|
198
368
|
// wiped credentials. This request's cached client still speaks the old
|
|
199
369
|
// token — drop it so the next connect runs against current state.
|
|
200
370
|
if (state.client && isOauth && state.connectedGeneration !== null) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
) {
|
|
371
|
+
const generation = await getProvider(ctx, state).generation();
|
|
372
|
+
if (state.closed) throw scopeEndedError();
|
|
373
|
+
if (generation !== state.connectedGeneration) {
|
|
205
374
|
reset(state);
|
|
206
375
|
}
|
|
207
376
|
}
|
|
377
|
+
if (state.closed) throw scopeEndedError();
|
|
208
378
|
if (state.client) return;
|
|
209
379
|
state.connecting ??= (async () => {
|
|
210
380
|
const provider = isOauth ? getProvider(ctx, state) : null;
|
|
211
381
|
const genAtStart = provider ? await provider.generation() : 0;
|
|
382
|
+
if (state.closed) throw scopeEndedError();
|
|
212
383
|
// Stamp the provider so any saveTokens/saveClientInformation the SDK fires
|
|
213
384
|
// during this connect (code exchange, DCR) — or during a later refresh on
|
|
214
385
|
// the resulting client — is dropped if a concurrent force bumps the
|
|
@@ -226,20 +397,46 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
|
|
|
226
397
|
state.transport = t;
|
|
227
398
|
try {
|
|
228
399
|
await c.connect(t);
|
|
400
|
+
// A probe deadline can end its scope while connect is still in flight.
|
|
401
|
+
// The transport is closed immediately by closeScope; if connect wins
|
|
402
|
+
// that race anyway, close the resulting client rather than resurrecting
|
|
403
|
+
// a session in the detached state object.
|
|
404
|
+
if (state.closed) {
|
|
405
|
+
try {
|
|
406
|
+
await c.close();
|
|
407
|
+
} catch {
|
|
408
|
+
// The scope has already been discarded either way.
|
|
409
|
+
}
|
|
410
|
+
throw scopeEndedError();
|
|
411
|
+
}
|
|
229
412
|
// A force re-auth that landed WHILE we were connecting wiped the creds
|
|
230
413
|
// this client just bound to. Discard it rather than cache a connection
|
|
231
414
|
// that resurrects the wiped-and-reauthorized connector from a stale
|
|
232
415
|
// isolate. Surfaces as auth_required — the connector genuinely needs
|
|
233
416
|
// re-consent now.
|
|
234
|
-
if (provider
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
417
|
+
if (provider) {
|
|
418
|
+
const generation = await provider.generation();
|
|
419
|
+
// closeScope can land while the generation read is pending, after
|
|
420
|
+
// connect succeeded but before this client is cached. Discard the
|
|
421
|
+
// client on that side of the await too.
|
|
422
|
+
if (state.closed) {
|
|
423
|
+
try {
|
|
424
|
+
await c.close();
|
|
425
|
+
} catch {
|
|
426
|
+
// The scope has already been discarded either way.
|
|
427
|
+
}
|
|
428
|
+
throw scopeEndedError();
|
|
429
|
+
}
|
|
430
|
+
if (generation !== genAtStart) {
|
|
431
|
+
try {
|
|
432
|
+
await c.close();
|
|
433
|
+
} catch {
|
|
434
|
+
// discarding either way
|
|
435
|
+
}
|
|
436
|
+
throw new UnauthorizedError(
|
|
437
|
+
"Connector was re-authorized during connect; reconnect required.",
|
|
438
|
+
);
|
|
239
439
|
}
|
|
240
|
-
throw new UnauthorizedError(
|
|
241
|
-
"Connector was re-authorized during connect; reconnect required.",
|
|
242
|
-
);
|
|
243
440
|
}
|
|
244
441
|
state.client = c;
|
|
245
442
|
state.connectedGeneration = genAtStart;
|
|
@@ -268,19 +465,113 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
|
|
|
268
465
|
maxResultBytes: opts.maxResultBytes,
|
|
269
466
|
usageGuide: opts.usageGuide,
|
|
270
467
|
|
|
468
|
+
// `tools/list` is cursor-paginated: the server chooses the page size and
|
|
469
|
+
// signals "there is more" with a `nextCursor`, which the SDK's
|
|
470
|
+
// Client.listTools() returns without following. Collect the whole chain
|
|
471
|
+
// here, because a half-collected catalog is indistinguishable from a small
|
|
472
|
+
// one — later-page tools would simply appear not to exist, unsearchable and
|
|
473
|
+
// unaddressable, with nothing anywhere saying why.
|
|
474
|
+
//
|
|
475
|
+
// All pages ride the one request-scoped client already connected above, and
|
|
476
|
+
// the accumulator is returned rather than stored: a cursor is opaque and
|
|
477
|
+
// session-bound, so nothing here may outlive this call.
|
|
271
478
|
async listTools(ctx) {
|
|
272
479
|
const state = stateFor(ctx);
|
|
273
480
|
await ensureConnected(ctx, state);
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
)
|
|
481
|
+
// Bind the client once so the whole walk provably rides one session — a
|
|
482
|
+
// cursor is only meaningful to the connection that issued it, and a
|
|
483
|
+
// re-read could in principle pick up a different one. It is NOT guarding
|
|
484
|
+
// against closeScope nulling state.client mid-loop: closeScope sets
|
|
485
|
+
// `closed` and nulls `client` in one synchronous run, and the loop
|
|
486
|
+
// re-checks `closed` before every page, so the nulled client is
|
|
487
|
+
// unreachable from here.
|
|
488
|
+
const client = state.client!;
|
|
489
|
+
// Raw SDK tools, not ToolDefs: the metadata re-prime below needs fields
|
|
490
|
+
// (task support) that a ToolDef deliberately does not carry.
|
|
491
|
+
const listed: ListedTool[] = [];
|
|
492
|
+
const names = new Set<string>();
|
|
493
|
+
const spent = new Set<string>();
|
|
494
|
+
let cursor: string | undefined;
|
|
495
|
+
/** Consecutive pages that advertised a successor but added nothing. */
|
|
496
|
+
let barren = 0;
|
|
497
|
+
let complete = false;
|
|
498
|
+
for (let page = 0; page < MAX_TOOL_PAGES; page++) {
|
|
499
|
+
// The scope can end between pages (probe timeout, teardown). Stop
|
|
500
|
+
// rather than keep paging into a transport that is being closed.
|
|
501
|
+
if (state.closed) throw scopeEndedError();
|
|
502
|
+
// Page one sends no params at all, so a non-paginated server sees
|
|
503
|
+
// exactly the request it saw before pagination existed.
|
|
504
|
+
const res = await listPage(client, cursor);
|
|
505
|
+
let added = 0;
|
|
506
|
+
for (const t of res.tools) {
|
|
507
|
+
// First page wins. An unstable cursor can serve the same tool on two
|
|
508
|
+
// pages — a failure mode that did not exist while only page one was
|
|
509
|
+
// read — and a duplicate would inflate `toolCount`, double the tool's
|
|
510
|
+
// `search_tools` row, and churn the registry's catalog-changed
|
|
511
|
+
// comparison into a persistence write on every refresh.
|
|
512
|
+
if (names.has(t.name)) continue;
|
|
513
|
+
names.add(t.name);
|
|
514
|
+
listed.push(t);
|
|
515
|
+
added++;
|
|
516
|
+
}
|
|
517
|
+
// Pagination ends when `nextCursor` is ABSENT — not when it is falsy.
|
|
518
|
+
// An empty string is a legal cursor and means "keep going"; `if
|
|
519
|
+
// (!next)` here would silently truncate that server's catalog.
|
|
520
|
+
const next = res.nextCursor;
|
|
521
|
+
if (typeof next !== "string") {
|
|
522
|
+
complete = true;
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
// A page that adds nothing and still claims a successor made no
|
|
526
|
+
// progress. Allow exactly one: the widespread idiom is to advertise a
|
|
527
|
+
// cursor whenever a page came back full and then serve one empty page
|
|
528
|
+
// to terminate, and that server is conformant. Two in a row is a
|
|
529
|
+
// downstream going nowhere, and this kills a "fresh cursor forever, no
|
|
530
|
+
// tools" adversary in a couple of round trips instead of thousands.
|
|
531
|
+
if (added === 0 && ++barren > 1) {
|
|
532
|
+
throw new Error(
|
|
533
|
+
`Connector "${id}" returned two consecutive tools/list pages that added no tools and still advertised another — the catalog is not advancing.`,
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
if (added > 0) barren = 0;
|
|
537
|
+
// A cursor handed back a second time is not a slow server, it is a
|
|
538
|
+
// loop. Fail now rather than walking it until a ceiling notices.
|
|
539
|
+
if (spent.has(next)) {
|
|
540
|
+
throw new Error(
|
|
541
|
+
`Connector "${id}" handed back a tools/list cursor it had already issued — the pagination chain loops.`,
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
// Checked here rather than on arrival: this bounds what a *walk* may
|
|
545
|
+
// accumulate, and a server that answers in one page was always free to
|
|
546
|
+
// send whatever it sends.
|
|
547
|
+
if (listed.length > MAX_TOOLS) {
|
|
548
|
+
throw new Error(
|
|
549
|
+
`Connector "${id}" advertised further tools/list pages past ${listed.length} tools, over the ${MAX_TOOLS}-tool ceiling one catalog refresh will collect.`,
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
// Opaque by contract: handed straight back, never parsed, rewritten,
|
|
553
|
+
// or persisted.
|
|
554
|
+
spent.add(next);
|
|
555
|
+
cursor = next;
|
|
556
|
+
}
|
|
557
|
+
// Fail the refresh outright. Returning what we have would publish a
|
|
558
|
+
// partial catalog that looks complete; throwing lets the registry keep
|
|
559
|
+
// serving the last complete one via its stale fallback.
|
|
560
|
+
if (!complete) {
|
|
561
|
+
throw new Error(
|
|
562
|
+
`Connector "${id}" kept advertising more tools/list pages after ${MAX_TOOL_PAGES} — refusing to page further.`,
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
// Repair what the per-page listTools calls left behind before any of
|
|
566
|
+
// these tools can be called. See primeToolMetadata.
|
|
567
|
+
primeToolMetadata(client, listed);
|
|
568
|
+
return listed.map((t) => ({
|
|
569
|
+
name: t.name,
|
|
570
|
+
description: t.description,
|
|
571
|
+
inputSchema: t.inputSchema as ToolDef["inputSchema"],
|
|
572
|
+
outputSchema: t.outputSchema as ToolDef["outputSchema"],
|
|
573
|
+
annotations: t.annotations as ToolDef["annotations"],
|
|
574
|
+
}));
|
|
284
575
|
},
|
|
285
576
|
|
|
286
577
|
async callTool(name, args, ctx) {
|
|
@@ -310,6 +601,38 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
|
|
|
310
601
|
}
|
|
311
602
|
},
|
|
312
603
|
|
|
604
|
+
async closeScope(ctx) {
|
|
605
|
+
const scope = ctx.requestScope ?? ctx;
|
|
606
|
+
const state = states.get(scope);
|
|
607
|
+
if (!state) return;
|
|
608
|
+
|
|
609
|
+
// Delete before awaiting: a duplicate teardown is a no-op, and no later
|
|
610
|
+
// lookup can reuse the state while its client is closing.
|
|
611
|
+
states.delete(scope);
|
|
612
|
+
state.closed = true;
|
|
613
|
+
const client = state.client;
|
|
614
|
+
const transport = state.transport;
|
|
615
|
+
state.client = null;
|
|
616
|
+
state.transport = null;
|
|
617
|
+
state.connecting = null;
|
|
618
|
+
state.authRequired = false;
|
|
619
|
+
state.connectedGeneration = null;
|
|
620
|
+
|
|
621
|
+
// Ask the downstream to drop its session first — closing only aborts our
|
|
622
|
+
// side, and the DELETE that frees the server's rides on the very
|
|
623
|
+
// AbortSignal the close is about to trip.
|
|
624
|
+
if (transport) await terminateSession(transport);
|
|
625
|
+
|
|
626
|
+
// Client.close() owns its connected transport. During an unfinished or
|
|
627
|
+
// failed connect there is no cached client yet, so close the transport
|
|
628
|
+
// directly to abort/release that half-open session.
|
|
629
|
+
if (client) {
|
|
630
|
+
await client.close();
|
|
631
|
+
} else {
|
|
632
|
+
await transport?.close();
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
|
|
313
636
|
async status(ctx): Promise<ConnectorStatus> {
|
|
314
637
|
const state = stateFor(ctx);
|
|
315
638
|
try {
|