@casys/mcp-server 0.22.0 → 0.24.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/README.md +24 -34
- package/mod.ts +46 -3
- package/package.json +1 -1
- package/src/auth/middleware.ts +36 -7
- package/src/client-auth/token-store/memory-store.ts +11 -6
- package/src/http/body.ts +72 -0
- package/src/http/request-guards.ts +137 -0
- package/src/http/wire.ts +41 -0
- package/src/mcp-app.ts +1946 -640
- package/src/middleware/types.ts +33 -0
- package/src/mrtr/capability-check.ts +303 -0
- package/src/mrtr/mod.ts +39 -0
- package/src/mrtr/request-state.ts +435 -0
- package/src/mrtr/types.ts +93 -0
- package/src/observability/metrics.ts +0 -42
- package/src/subscriptions/subscription-registry.ts +558 -0
- package/src/tasks/mod.ts +33 -0
- package/src/tasks/task-store.ts +1014 -0
- package/src/transport/request-headers.ts +661 -0
- package/src/types.ts +143 -24
- package/src/validation/schema-validator.ts +56 -0
- package/src/sampling/sampling-bridge.ts +0 -193
package/src/types.ts
CHANGED
|
@@ -80,8 +80,9 @@ export interface McpAppOptions {
|
|
|
80
80
|
validateSchema?: boolean;
|
|
81
81
|
|
|
82
82
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
83
|
+
* @deprecated No-op as of 0.24.0. The SamplingBridge was removed in the
|
|
84
|
+
* MCP 2026-07-28 spec drop. Passing this option has zero effect and emits
|
|
85
|
+
* a console warning at startup. Will be removed in 0.25.0.
|
|
85
86
|
*/
|
|
86
87
|
enableSampling?: boolean;
|
|
87
88
|
|
|
@@ -92,8 +93,9 @@ export interface McpAppOptions {
|
|
|
92
93
|
instructions?: string;
|
|
93
94
|
|
|
94
95
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
96
|
+
* @deprecated No-op as of 0.24.0. The SamplingBridge was removed in the
|
|
97
|
+
* MCP 2026-07-28 spec drop. Passing this option has zero effect and emits
|
|
98
|
+
* a console warning at startup. Will be removed in 0.25.0.
|
|
97
99
|
*/
|
|
98
100
|
samplingClient?: SamplingClient;
|
|
99
101
|
|
|
@@ -144,20 +146,106 @@ export interface McpAppOptions {
|
|
|
144
146
|
expectResources?: boolean;
|
|
145
147
|
|
|
146
148
|
/**
|
|
147
|
-
* HTTP transport mode
|
|
149
|
+
* HTTP transport mode. Only `"stateless"` is supported as of spec 2026-07-28.
|
|
148
150
|
*
|
|
149
|
-
* -
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
151
|
+
* Per-request transport: no handshake, no `Mcp-Session-Id`.
|
|
152
|
+
* `protocolVersion` is read from
|
|
153
|
+
* `params._meta["io.modelcontextprotocol/protocolVersion"]` on every
|
|
154
|
+
* request. `GET /mcp` returns 405; use `subscriptions/listen` instead.
|
|
155
|
+
*/
|
|
156
|
+
transport?: "stateless";
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Cache hints emitted on list and read results (spec 2026-07-28,
|
|
160
|
+
* `CacheableResult`, SEP-2549).
|
|
161
|
+
*
|
|
162
|
+
* `ttlMs` and `cacheScope` are **required** fields of those results, so the
|
|
163
|
+
* framework always emits them. The defaults are deliberately inert:
|
|
164
|
+
*
|
|
165
|
+
* - `ttlMs: 0` — "revalidate every time", the HTTP `max-age=0` equivalent.
|
|
166
|
+
* Caching is a per-deployment decision: a server whose tool list is built
|
|
167
|
+
* from a live database must not have a stale window chosen for it. Opt in
|
|
168
|
+
* with a real value.
|
|
169
|
+
* - `cacheScope: "private"` — shared intermediaries must not cache the
|
|
170
|
+
* response. Anything else would be unsafe by default here, since results can
|
|
171
|
+
* be scoped to the authenticated principal or tenant. Use `"public"` only for
|
|
172
|
+
* a server whose lists are genuinely identical for every caller.
|
|
173
|
+
*/
|
|
174
|
+
cache?: {
|
|
175
|
+
/** Freshness window in milliseconds. Default `0`. */
|
|
176
|
+
ttlMs?: number;
|
|
177
|
+
/** Whether shared caches may store the response. Default `"private"`. */
|
|
178
|
+
scope?: "public" | "private";
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Authorization key a task is bound to, derived from the request that created it.
|
|
183
|
+
*
|
|
184
|
+
* A task can only be read, answered or cancelled by a caller whose key matches.
|
|
185
|
+
* The default is the authenticated subject, which is right for a
|
|
186
|
+
* single-authority deployment and **not enough** for anything that authorizes on
|
|
187
|
+
* more than identity: a tenant-scoped server where the same subject may act for
|
|
188
|
+
* several tenants needs the tenant in the key, or a task created under one
|
|
189
|
+
* tenant stays reachable from another.
|
|
190
|
+
*
|
|
191
|
+
* The framework cannot derive that itself — tenancy lives in a consumer's own
|
|
192
|
+
* middleware — so it is a hook rather than a guess. Return a stable string; it is
|
|
193
|
+
* compared verbatim.
|
|
194
|
+
*
|
|
195
|
+
* **It must derive everything from its two arguments.** `tasks/*` requests do
|
|
196
|
+
* not run the middleware pipeline, so anything a middleware injects into the
|
|
197
|
+
* pipeline context — the built-in tenant middleware's `tenantId`, for instance —
|
|
198
|
+
* is NOT visible here. Read the header, or resolve it yourself; the hook may be
|
|
199
|
+
* async precisely so a lookup is possible. A hook that expects pipeline state
|
|
200
|
+
* silently sees `undefined` on every call, which collapses every caller onto one
|
|
201
|
+
* key.
|
|
202
|
+
*
|
|
203
|
+
* Returning an empty or non-string value is refused rather than accepted, since
|
|
204
|
+
* that is what the obvious implementation yields when the value it reads is
|
|
205
|
+
* absent, and accepting it would share tasks between callers.
|
|
206
|
+
*
|
|
207
|
+
* Called on task creation and on every `tasks/get` / `tasks/update` /
|
|
208
|
+
* `tasks/cancel`. It must return the same key for the same caller across those
|
|
209
|
+
* calls, or the creator stops being able to reach its own task (fail-closed).
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* taskOwnerKey: (authInfo, request) =>
|
|
214
|
+
* `${authInfo?.subject}@${request.headers.get("x-tenant-id")}`
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
taskOwnerKey?: (
|
|
218
|
+
authInfo: import("./auth/types.js").AuthInfo | undefined,
|
|
219
|
+
request: Request,
|
|
220
|
+
) => string | Promise<string>;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Multi Round-Trip Requests (spec 2026-07-28, SEP-2322).
|
|
224
|
+
*
|
|
225
|
+
* Configures how `requestState` is protected when a tool handler asks the
|
|
226
|
+
* client for input mid-call. Only consulted on the stateless transport with a
|
|
227
|
+
* negotiated `2026-07-28`: MRTR replaces the server-initiated request pattern
|
|
228
|
+
* that earlier revisions still use, so it has no meaning on the legacy path.
|
|
156
229
|
*
|
|
157
|
-
*
|
|
230
|
+
* Without `signingKey`, `requestState` travels unprotected and the server logs
|
|
231
|
+
* a warning at startup. That is only acceptable when tampering with it can
|
|
232
|
+
* cause nothing worse than the request failing.
|
|
158
233
|
*/
|
|
159
|
-
|
|
160
|
-
|
|
234
|
+
mrtr?: import("./mrtr/mod.js").MrtrOptions;
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Protocol extensions this server declares (spec 2026-07-28).
|
|
238
|
+
*
|
|
239
|
+
* Keyed by reverse-DNS extension identifier — e.g.
|
|
240
|
+
* `{ "io.modelcontextprotocol/tasks": {} }`. Surfaced under
|
|
241
|
+
* `capabilities.extensions` on `server/discover` and `initialize`, and only
|
|
242
|
+
* when non-empty: an empty object would assert "no extensions supported",
|
|
243
|
+
* which is a claim the framework cannot make on a consumer's behalf.
|
|
244
|
+
*
|
|
245
|
+
* Declaring an extension here does not implement it — it advertises what the
|
|
246
|
+
* consumer has wired up.
|
|
247
|
+
*/
|
|
248
|
+
extensions?: Readonly<Record<string, unknown>>;
|
|
161
249
|
}
|
|
162
250
|
|
|
163
251
|
// ============================================
|
|
@@ -473,6 +561,38 @@ export interface ToolHandlerContext {
|
|
|
473
561
|
readonly authInfo?: import("./auth/types.js").AuthInfo;
|
|
474
562
|
readonly clientInfo?: Implementation;
|
|
475
563
|
readonly clientCapabilities?: ClientCapabilities;
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Answers to a previous `InputRequiredResult`, keyed by the ids the server
|
|
567
|
+
* assigned (spec 2026-07-28 MRTR).
|
|
568
|
+
*
|
|
569
|
+
* Present only on a retry. Read from `params.inputResponses` — a sibling of
|
|
570
|
+
* `_meta`, not a member of it, unlike every other per-request field. Getting
|
|
571
|
+
* that wrong yields a silent `undefined` even when the client sent them.
|
|
572
|
+
*/
|
|
573
|
+
readonly inputResponses?: Record<string, unknown>;
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* True when the client echoed a `requestState` that passed integrity
|
|
577
|
+
* verification, so the handler may trust this is a legitimate retry of its own
|
|
578
|
+
* earlier request.
|
|
579
|
+
*
|
|
580
|
+
* The token carries no application state — it binds the principal, method,
|
|
581
|
+
* argument digest and expiry. Since the digest guarantees the arguments are
|
|
582
|
+
* byte-identical to the original call, a handler reconstructs its context from
|
|
583
|
+
* those arguments rather than from the token.
|
|
584
|
+
*/
|
|
585
|
+
readonly retryVerified?: boolean;
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Log level requested for this call (spec 2026-07-28), read from
|
|
589
|
+
* `params._meta["io.modelcontextprotocol/logLevel"]`.
|
|
590
|
+
*
|
|
591
|
+
* `undefined` means the client requested no logging. A handler **MUST NOT**
|
|
592
|
+
* emit `notifications/message` in that case — the level is not a filter you
|
|
593
|
+
* may default, it is the client's opt-in.
|
|
594
|
+
*/
|
|
595
|
+
readonly logLevel?: import("./middleware/types.js").McpLogLevel;
|
|
476
596
|
}
|
|
477
597
|
|
|
478
598
|
export type ToolHandler = (
|
|
@@ -507,10 +627,11 @@ export type ToolErrorMapper = (
|
|
|
507
627
|
) => string | null;
|
|
508
628
|
|
|
509
629
|
/**
|
|
510
|
-
* Sampling client interface
|
|
511
|
-
*
|
|
630
|
+
* Sampling client interface — no-op as of 0.24.0. The SamplingBridge was
|
|
631
|
+
* removed with MCP 2026-07-28. This type is kept for source compatibility
|
|
632
|
+
* only and will be removed in 0.25.0.
|
|
512
633
|
*
|
|
513
|
-
* @deprecated
|
|
634
|
+
* @deprecated No-op as of 0.24.0. Will be removed in 0.25.0.
|
|
514
635
|
*/
|
|
515
636
|
export interface SamplingClient {
|
|
516
637
|
/**
|
|
@@ -522,10 +643,9 @@ export interface SamplingClient {
|
|
|
522
643
|
}
|
|
523
644
|
|
|
524
645
|
/**
|
|
525
|
-
* Parameters for sampling request
|
|
526
|
-
* Compatible with MCP sampling protocol
|
|
646
|
+
* Parameters for sampling request — no-op as of 0.24.0.
|
|
527
647
|
*
|
|
528
|
-
* @deprecated
|
|
648
|
+
* @deprecated No-op as of 0.24.0. Will be removed in 0.25.0.
|
|
529
649
|
*/
|
|
530
650
|
export interface SamplingParams {
|
|
531
651
|
messages: Array<{ role: "user" | "assistant"; content: string }>;
|
|
@@ -545,10 +665,9 @@ export interface SamplingParams {
|
|
|
545
665
|
}
|
|
546
666
|
|
|
547
667
|
/**
|
|
548
|
-
* Result from sampling request
|
|
549
|
-
* Compatible with MCP sampling protocol
|
|
668
|
+
* Result from sampling request — no-op as of 0.24.0.
|
|
550
669
|
*
|
|
551
|
-
* @deprecated
|
|
670
|
+
* @deprecated No-op as of 0.24.0. Will be removed in 0.25.0.
|
|
552
671
|
*/
|
|
553
672
|
export interface SamplingResult {
|
|
554
673
|
content: Array<{
|
|
@@ -28,6 +28,16 @@ interface AjvValidateFunction {
|
|
|
28
28
|
errors?: AjvErrorObject[] | null;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Count with its noun, agreeing in number.
|
|
33
|
+
*
|
|
34
|
+
* A bound of 1 is a legitimate schema (`minItems: 1`), and "must have at least
|
|
35
|
+
* 1 items" reads as a bug in the server rather than a problem with the input.
|
|
36
|
+
*/
|
|
37
|
+
function plural(n: number, singular: string, plural = `${singular}s`): string {
|
|
38
|
+
return `${n} ${n === 1 ? singular : plural}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
31
41
|
/**
|
|
32
42
|
* Validation error with formatted message
|
|
33
43
|
*/
|
|
@@ -205,6 +215,52 @@ export class SchemaValidator {
|
|
|
205
215
|
expected = param.pattern;
|
|
206
216
|
break;
|
|
207
217
|
|
|
218
|
+
// Collection bounds. ajv enforces these already — what was missing is a
|
|
219
|
+
// message shaped like the others, with a structured `expected`. Without
|
|
220
|
+
// it these fell through to `default` and surfaced ajv's bare prose
|
|
221
|
+
// ("must NOT have more than 8 items"), which states the limit but names
|
|
222
|
+
// neither the property nor an `expected` a caller can act on.
|
|
223
|
+
//
|
|
224
|
+
// `expected` says "items"/"properties" rather than "length": it is read
|
|
225
|
+
// on its own as a recovery hint, where "length <= 8" is indistinguishable
|
|
226
|
+
// from the character bound that minLength/maxLength above produce.
|
|
227
|
+
case "minItems":
|
|
228
|
+
message = `Property ${path} must have at least ${
|
|
229
|
+
plural(param.limit, "item")
|
|
230
|
+
}`;
|
|
231
|
+
expected = `items >= ${param.limit}`;
|
|
232
|
+
break;
|
|
233
|
+
|
|
234
|
+
case "maxItems":
|
|
235
|
+
message = `Property ${path} must have at most ${
|
|
236
|
+
plural(param.limit, "item")
|
|
237
|
+
}`;
|
|
238
|
+
expected = `items <= ${param.limit}`;
|
|
239
|
+
break;
|
|
240
|
+
|
|
241
|
+
case "uniqueItems":
|
|
242
|
+
// ajv reports the two colliding array indices, zero-based, and in
|
|
243
|
+
// either order depending on how it optimised the comparison. The
|
|
244
|
+
// indices are not what is equal — the items at them are.
|
|
245
|
+
message =
|
|
246
|
+
`Property ${path} must not contain duplicate items (items at indices ${param.j} and ${param.i} are equal)`;
|
|
247
|
+
expected = "unique items";
|
|
248
|
+
break;
|
|
249
|
+
|
|
250
|
+
case "minProperties":
|
|
251
|
+
message = `Property ${path} must have at least ${
|
|
252
|
+
plural(param.limit, "property", "properties")
|
|
253
|
+
}`;
|
|
254
|
+
expected = `properties >= ${param.limit}`;
|
|
255
|
+
break;
|
|
256
|
+
|
|
257
|
+
case "maxProperties":
|
|
258
|
+
message = `Property ${path} must have at most ${
|
|
259
|
+
plural(param.limit, "property", "properties")
|
|
260
|
+
}`;
|
|
261
|
+
expected = `properties <= ${param.limit}`;
|
|
262
|
+
break;
|
|
263
|
+
|
|
208
264
|
case "additionalProperties":
|
|
209
265
|
message = `Unknown property: ${param.additionalProperty}`;
|
|
210
266
|
break;
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sampling Bridge for Bidirectional LLM Communication
|
|
3
|
-
*
|
|
4
|
-
* Enables MCP servers to delegate complex tasks back to the client's LLM
|
|
5
|
-
* via the sampling protocol. Manages pending requests with timeout and
|
|
6
|
-
* response routing.
|
|
7
|
-
*
|
|
8
|
-
* @module lib/server/sampling-bridge
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type {
|
|
12
|
-
PromiseResolver,
|
|
13
|
-
SamplingClient,
|
|
14
|
-
SamplingParams,
|
|
15
|
-
SamplingResult,
|
|
16
|
-
} from "../types.js";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* SamplingBridge manages bidirectional sampling requests
|
|
20
|
-
*
|
|
21
|
-
* Features:
|
|
22
|
-
* - Request/response correlation via unique IDs
|
|
23
|
-
* - Automatic timeout for pending requests (60s default)
|
|
24
|
-
* - Promise-based async/await API
|
|
25
|
-
* - Support for both Anthropic and OpenAI sampling
|
|
26
|
-
*
|
|
27
|
-
* @deprecated Deprecated (MCP 2026-07-28). Removal after 2027-07-28. Prefer the explicit handle-based pattern.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```typescript
|
|
31
|
-
* const bridge = new SamplingBridge(samplingClient);
|
|
32
|
-
*
|
|
33
|
-
* // Delegate task to LLM
|
|
34
|
-
* const result = await bridge.requestSampling({
|
|
35
|
-
* messages: [{ role: 'user', content: 'Analyze this data...' }]
|
|
36
|
-
* });
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
export class SamplingBridge {
|
|
40
|
-
private pendingRequests = new Map<number, PromiseResolver<SamplingResult>>();
|
|
41
|
-
private nextId = 1;
|
|
42
|
-
private samplingClient: SamplingClient;
|
|
43
|
-
private defaultTimeout = 60000; // 60 seconds
|
|
44
|
-
|
|
45
|
-
constructor(client: SamplingClient, options?: { timeout?: number }) {
|
|
46
|
-
this.samplingClient = client;
|
|
47
|
-
if (options?.timeout) {
|
|
48
|
-
this.defaultTimeout = options.timeout;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Request sampling from the client's LLM
|
|
54
|
-
*
|
|
55
|
-
* @param params - Sampling parameters (messages, model preferences, etc.)
|
|
56
|
-
* @param timeout - Override default timeout (ms)
|
|
57
|
-
* @returns Promise that resolves with sampling result
|
|
58
|
-
* @throws {Error} If request times out or client fails
|
|
59
|
-
*/
|
|
60
|
-
async requestSampling(
|
|
61
|
-
params: SamplingParams,
|
|
62
|
-
timeout?: number,
|
|
63
|
-
): Promise<SamplingResult> {
|
|
64
|
-
const id = this.nextId++;
|
|
65
|
-
const timeoutMs = timeout ?? this.defaultTimeout;
|
|
66
|
-
|
|
67
|
-
// Create timeout promise for race condition
|
|
68
|
-
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
|
69
|
-
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
70
|
-
timeoutId = setTimeout(() => {
|
|
71
|
-
this.pendingRequests.delete(id);
|
|
72
|
-
reject(
|
|
73
|
-
new Error(`Sampling request ${id} timed out after ${timeoutMs}ms`),
|
|
74
|
-
);
|
|
75
|
-
}, timeoutMs);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// Track pending request for potential external response handling
|
|
79
|
-
const resultPromise = new Promise<SamplingResult>((resolve, reject) => {
|
|
80
|
-
this.pendingRequests.set(id, { resolve, reject });
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
// Race between client response and timeout
|
|
85
|
-
// Also race with external response via handleResponse()
|
|
86
|
-
const clientPromise = this.samplingClient.createMessage(params);
|
|
87
|
-
const result = await Promise.race([
|
|
88
|
-
clientPromise.then((r) => {
|
|
89
|
-
// Resolve the pending request tracker as well
|
|
90
|
-
const pending = this.pendingRequests.get(id);
|
|
91
|
-
if (pending) {
|
|
92
|
-
pending.resolve(r);
|
|
93
|
-
}
|
|
94
|
-
return r;
|
|
95
|
-
}),
|
|
96
|
-
timeoutPromise,
|
|
97
|
-
resultPromise, // Allow external handleResponse() to resolve
|
|
98
|
-
]);
|
|
99
|
-
return result;
|
|
100
|
-
} catch (error) {
|
|
101
|
-
// Clean up pending request on error
|
|
102
|
-
this.pendingRequests.delete(id);
|
|
103
|
-
throw error;
|
|
104
|
-
} finally {
|
|
105
|
-
// Always clear timeout to prevent memory leak
|
|
106
|
-
if (timeoutId) {
|
|
107
|
-
clearTimeout(timeoutId);
|
|
108
|
-
}
|
|
109
|
-
// Clean up pending request
|
|
110
|
-
this.pendingRequests.delete(id);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Handle sampling response from client (for bidirectional stdio)
|
|
116
|
-
*
|
|
117
|
-
* Used when MCP server receives sampling responses via stdin.
|
|
118
|
-
* Resolves the corresponding pending promise.
|
|
119
|
-
*
|
|
120
|
-
* @param id - Request ID from original sampling request
|
|
121
|
-
* @param result - Sampling result from client
|
|
122
|
-
*/
|
|
123
|
-
handleResponse(id: number, result: SamplingResult): void {
|
|
124
|
-
const pending = this.pendingRequests.get(id);
|
|
125
|
-
|
|
126
|
-
if (!pending) {
|
|
127
|
-
console.error(
|
|
128
|
-
`[SamplingBridge] Received response for unknown request: ${id}`,
|
|
129
|
-
);
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
this.pendingRequests.delete(id);
|
|
134
|
-
pending.resolve(result);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Handle sampling error from client
|
|
139
|
-
*
|
|
140
|
-
* @param id - Request ID
|
|
141
|
-
* @param error - Error from client
|
|
142
|
-
*/
|
|
143
|
-
handleError(id: number, error: Error): void {
|
|
144
|
-
const pending = this.pendingRequests.get(id);
|
|
145
|
-
|
|
146
|
-
if (!pending) {
|
|
147
|
-
console.error(
|
|
148
|
-
`[SamplingBridge] Received error for unknown request: ${id}`,
|
|
149
|
-
);
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
this.pendingRequests.delete(id);
|
|
154
|
-
pending.reject(error);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Get count of pending sampling requests
|
|
159
|
-
*/
|
|
160
|
-
getPendingCount(): number {
|
|
161
|
-
return this.pendingRequests.size;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Cancel all pending requests (useful for shutdown)
|
|
166
|
-
*/
|
|
167
|
-
cancelAll(): void {
|
|
168
|
-
for (const [id, pending] of this.pendingRequests.entries()) {
|
|
169
|
-
pending.reject(
|
|
170
|
-
new Error(`Sampling request ${id} cancelled (server shutdown)`),
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
this.pendingRequests.clear();
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Get sampling client for direct access
|
|
178
|
-
*/
|
|
179
|
-
getClient(): SamplingClient {
|
|
180
|
-
return this.samplingClient;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Alias for requestSampling() - implements SamplingClient interface
|
|
185
|
-
* This allows the bridge to be used as a drop-in replacement for SamplingClient
|
|
186
|
-
*
|
|
187
|
-
* @param params - Sampling parameters
|
|
188
|
-
* @returns Sampling result
|
|
189
|
-
*/
|
|
190
|
-
createMessage(params: SamplingParams): Promise<SamplingResult> {
|
|
191
|
-
return this.requestSampling(params);
|
|
192
|
-
}
|
|
193
|
-
}
|