@revealui/mcp 0.1.11 → 0.2.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/LICENSE.commercial +90 -0
- package/README.md +5 -13
- package/dist/adapters/db.d.ts +16 -7
- package/dist/adapters/db.d.ts.map +1 -1
- package/dist/adapters/db.js +6 -24
- package/dist/adapters/db.js.map +1 -1
- package/dist/client.d.ts +307 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +492 -0
- package/dist/client.js.map +1 -0
- package/dist/hypervisor.d.ts +18 -0
- package/dist/hypervisor.d.ts.map +1 -1
- package/dist/hypervisor.js +83 -4
- package/dist/hypervisor.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -2
- package/dist/index.js.map +1 -1
- package/dist/metering.d.ts +59 -0
- package/dist/metering.d.ts.map +1 -0
- package/dist/metering.js +25 -0
- package/dist/metering.js.map +1 -0
- package/dist/oauth.d.ts +171 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +292 -0
- package/dist/oauth.js.map +1 -0
- package/dist/remote-client.d.ts +86 -0
- package/dist/remote-client.d.ts.map +1 -0
- package/dist/remote-client.js +130 -0
- package/dist/remote-client.js.map +1 -0
- package/dist/servers/adapter.d.ts.map +1 -1
- package/dist/servers/adapter.js +4 -0
- package/dist/servers/adapter.js.map +1 -1
- package/dist/servers/factories/revealui-content.d.ts +85 -0
- package/dist/servers/factories/revealui-content.d.ts.map +1 -0
- package/dist/servers/factories/revealui-content.js +471 -0
- package/dist/servers/factories/revealui-content.js.map +1 -0
- package/dist/servers/revealui-content.d.ts +12 -18
- package/dist/servers/revealui-content.d.ts.map +1 -1
- package/dist/servers/revealui-content.js +14 -220
- package/dist/servers/revealui-content.js.map +1 -1
- package/dist/servers/revealui-memory.d.ts +24 -0
- package/dist/servers/revealui-memory.d.ts.map +1 -0
- package/dist/servers/revealui-memory.js +339 -0
- package/dist/servers/revealui-memory.js.map +1 -0
- package/dist/streamable-http.d.ts +72 -0
- package/dist/streamable-http.d.ts.map +1 -0
- package/dist/streamable-http.js +120 -0
- package/dist/streamable-http.js.map +1 -0
- package/package.json +21 -8
- package/dist/stores/postgres-idempotency.d.ts +0 -32
- package/dist/stores/postgres-idempotency.d.ts.map +0 -1
- package/dist/stores/postgres-idempotency.js +0 -63
- package/dist/stores/postgres-idempotency.js.map +0 -1
package/dist/client.js
ADDED
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Client wrapper for `@revealui/mcp`.
|
|
3
|
+
*
|
|
4
|
+
* Phase: Stage 0 of the MCP v1 plan (see `.jv/docs/mcp-productionization-scope.md`).
|
|
5
|
+
*
|
|
6
|
+
* Wraps `@modelcontextprotocol/sdk`'s `Client` with a RevealUI-shaped surface:
|
|
7
|
+
* transport selection, capability enforcement (method throws a typed error if
|
|
8
|
+
* the server doesn't advertise the feature), per-URI resource subscription
|
|
9
|
+
* fan-out, and application-layer handlers for the server-initiated primitives
|
|
10
|
+
* (sampling, elicitation, roots).
|
|
11
|
+
*
|
|
12
|
+
* Stage 0 completion as of PR-0.3:
|
|
13
|
+
* PR-0.1 — resources + prompts.
|
|
14
|
+
* PR-0.2 — sampling + elicitation + roots + completions.
|
|
15
|
+
* PR-0.3 — logging, progress, cancellation, generic notification routing,
|
|
16
|
+
* per-request options (signal + onProgress + timeout) threaded
|
|
17
|
+
* through every client-initiated call.
|
|
18
|
+
*
|
|
19
|
+
* The hypervisor (`./hypervisor.ts`) continues to speak its custom JSON-RPC
|
|
20
|
+
* for tool calls. Stage 1 migrates the hypervisor to route through this
|
|
21
|
+
* client so transport abstraction (Streamable HTTP) lands cleanly.
|
|
22
|
+
*/
|
|
23
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
24
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
25
|
+
import { StreamableHTTPClientTransport, } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
26
|
+
import { CreateMessageRequestSchema, ElicitRequestSchema, ListRootsRequestSchema, LoggingMessageNotificationSchema, ResourceUpdatedNotificationSchema, } from '@modelcontextprotocol/sdk/types.js';
|
|
27
|
+
/** Internal: translate our options to the SDK's native shape. */
|
|
28
|
+
function toSdkRequestOptions(options) {
|
|
29
|
+
if (!options)
|
|
30
|
+
return undefined;
|
|
31
|
+
const sdkOptions = {};
|
|
32
|
+
if (options.signal)
|
|
33
|
+
sdkOptions.signal = options.signal;
|
|
34
|
+
if (options.onProgress)
|
|
35
|
+
sdkOptions.onprogress = options.onProgress;
|
|
36
|
+
if (options.timeout !== undefined)
|
|
37
|
+
sdkOptions.timeout = options.timeout;
|
|
38
|
+
if (options.resetTimeoutOnProgress !== undefined) {
|
|
39
|
+
sdkOptions.resetTimeoutOnProgress = options.resetTimeoutOnProgress;
|
|
40
|
+
}
|
|
41
|
+
return sdkOptions;
|
|
42
|
+
}
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
// Errors
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
/**
|
|
47
|
+
* Thrown when a caller invokes a method that requires a server-side capability
|
|
48
|
+
* the server did NOT advertise during initialize. Signals a misconfiguration
|
|
49
|
+
* or a version mismatch — not a transient failure; do not retry.
|
|
50
|
+
*/
|
|
51
|
+
export class McpCapabilityError extends Error {
|
|
52
|
+
capability;
|
|
53
|
+
constructor(capability) {
|
|
54
|
+
super(`MCP server does not advertise the '${capability}' capability`);
|
|
55
|
+
this.name = 'McpCapabilityError';
|
|
56
|
+
this.capability = capability;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Thrown when a method is called before `connect()` has resolved.
|
|
61
|
+
*/
|
|
62
|
+
export class McpNotConnectedError extends Error {
|
|
63
|
+
constructor(method) {
|
|
64
|
+
super(`McpClient.${method}() called before connect()`);
|
|
65
|
+
this.name = 'McpNotConnectedError';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
// Capability auto-advertisement
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
/**
|
|
72
|
+
* Derive the client capability declaration from which handlers/providers the
|
|
73
|
+
* caller supplied. Only features we can actually service get advertised.
|
|
74
|
+
*
|
|
75
|
+
* Form-mode elicitation is advertised as the default when an elicitation
|
|
76
|
+
* handler is provided (matches the SDK's interpretation of `elicitation: {}`).
|
|
77
|
+
* URL-mode callers who want that surface explicitly can add it — future
|
|
78
|
+
* enhancement; today the handler is a single entry point.
|
|
79
|
+
*/
|
|
80
|
+
function buildCapabilities(options) {
|
|
81
|
+
const caps = {};
|
|
82
|
+
if (options.samplingHandler)
|
|
83
|
+
caps.sampling = {};
|
|
84
|
+
if (options.elicitationHandler)
|
|
85
|
+
caps.elicitation = {};
|
|
86
|
+
if (options.rootsProvider)
|
|
87
|
+
caps.roots = { listChanged: true };
|
|
88
|
+
return caps;
|
|
89
|
+
}
|
|
90
|
+
export class McpClient {
|
|
91
|
+
sdk;
|
|
92
|
+
options;
|
|
93
|
+
connected = false;
|
|
94
|
+
/**
|
|
95
|
+
* Tracks the Streamable HTTP transport when that kind is in use, so
|
|
96
|
+
* `finishAuth()` can delegate to it. Set by `createTransport`, cleared
|
|
97
|
+
* on `close()`.
|
|
98
|
+
*/
|
|
99
|
+
httpTransport;
|
|
100
|
+
resourceSubscribers = new Map();
|
|
101
|
+
listChangedHandlers = {
|
|
102
|
+
resources: new Set(),
|
|
103
|
+
prompts: new Set(),
|
|
104
|
+
tools: new Set(),
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Subscribers per notification schema. First `on(schema, handler)` call for
|
|
108
|
+
* a schema registers a single fan-out handler with the SDK; subsequent
|
|
109
|
+
* calls just add to the Set. Removing the last subscriber keeps the SDK
|
|
110
|
+
* handler registered (no public API to unregister by schema) — empty
|
|
111
|
+
* fan-out is a cheap no-op.
|
|
112
|
+
*/
|
|
113
|
+
notificationSubscribers = new Map();
|
|
114
|
+
constructor(options) {
|
|
115
|
+
this.options = options;
|
|
116
|
+
this.sdk = new Client(options.clientInfo, {
|
|
117
|
+
capabilities: buildCapabilities(options),
|
|
118
|
+
listChanged: {
|
|
119
|
+
resources: { onChanged: () => this.fireListChanged('resources') },
|
|
120
|
+
prompts: { onChanged: () => this.fireListChanged('prompts') },
|
|
121
|
+
tools: { onChanged: () => this.fireListChanged('tools') },
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
// Route resource-updated notifications through the generic fan-out so the
|
|
125
|
+
// same `on(ResourceUpdatedNotificationSchema, ...)` subscription API
|
|
126
|
+
// remains available without overwriting our per-URI subscriber dispatch.
|
|
127
|
+
this.on(ResourceUpdatedNotificationSchema, (notification) => {
|
|
128
|
+
const uri = notification.params.uri;
|
|
129
|
+
const subscribers = this.resourceSubscribers.get(uri);
|
|
130
|
+
if (!subscribers)
|
|
131
|
+
return;
|
|
132
|
+
for (const handler of subscribers) {
|
|
133
|
+
try {
|
|
134
|
+
handler({ uri });
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
// Swallow per-subscriber errors.
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
// Register server-initiated request handlers. Each is only wired when the
|
|
142
|
+
// caller supplied the corresponding handler/provider — that way the
|
|
143
|
+
// declared capability set matches what we can actually service.
|
|
144
|
+
if (options.samplingHandler) {
|
|
145
|
+
const handler = options.samplingHandler;
|
|
146
|
+
this.sdk.setRequestHandler(CreateMessageRequestSchema, (request) => handler(request.params));
|
|
147
|
+
}
|
|
148
|
+
if (options.elicitationHandler) {
|
|
149
|
+
const handler = options.elicitationHandler;
|
|
150
|
+
this.sdk.setRequestHandler(ElicitRequestSchema, (request) => handler(request.params));
|
|
151
|
+
}
|
|
152
|
+
if (options.rootsProvider) {
|
|
153
|
+
const provider = options.rootsProvider;
|
|
154
|
+
this.sdk.setRequestHandler(ListRootsRequestSchema, async () => ({
|
|
155
|
+
roots: await provider(),
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// -------------------------------------------------------------------------
|
|
160
|
+
// Lifecycle
|
|
161
|
+
// -------------------------------------------------------------------------
|
|
162
|
+
/**
|
|
163
|
+
* Connect the underlying transport and run MCP `initialize`. Idempotent:
|
|
164
|
+
* calling twice is a no-op on the second call.
|
|
165
|
+
*/
|
|
166
|
+
async connect() {
|
|
167
|
+
if (this.connected)
|
|
168
|
+
return;
|
|
169
|
+
const transport = this.createTransport();
|
|
170
|
+
await this.sdk.connect(transport);
|
|
171
|
+
this.connected = true;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Close the underlying transport and invalidate the client. Idempotent.
|
|
175
|
+
*/
|
|
176
|
+
async close() {
|
|
177
|
+
if (!this.connected)
|
|
178
|
+
return;
|
|
179
|
+
await this.sdk.close();
|
|
180
|
+
this.connected = false;
|
|
181
|
+
this.httpTransport = undefined;
|
|
182
|
+
this.resourceSubscribers.clear();
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Finalize an OAuth 2.1 authorization flow initiated by an `authProvider`.
|
|
186
|
+
*
|
|
187
|
+
* After `connect()` triggers `OAuthClientProvider.redirectToAuthorization`,
|
|
188
|
+
* the user completes consent at the authorization server and is redirected
|
|
189
|
+
* back to the caller's callback URL with a `code` query parameter. The
|
|
190
|
+
* caller passes that `code` here, which delegates to the Streamable HTTP
|
|
191
|
+
* transport's `finishAuth`: the SDK exchanges the code (with the stored
|
|
192
|
+
* PKCE verifier) at the token endpoint and persists the resulting token
|
|
193
|
+
* set via the provider's `saveTokens`. Retry `connect()` afterward.
|
|
194
|
+
*
|
|
195
|
+
* Throws if the client is not using the `streamable-http` transport or if
|
|
196
|
+
* `connect()` has not been called yet (the transport is constructed during
|
|
197
|
+
* `connect`).
|
|
198
|
+
*/
|
|
199
|
+
async finishAuth(authorizationCode) {
|
|
200
|
+
if (this.options.transport.kind !== 'streamable-http') {
|
|
201
|
+
throw new Error(`McpClient.finishAuth() requires the 'streamable-http' transport ` +
|
|
202
|
+
`(got '${this.options.transport.kind}')`);
|
|
203
|
+
}
|
|
204
|
+
if (!this.httpTransport) {
|
|
205
|
+
throw new McpNotConnectedError('finishAuth');
|
|
206
|
+
}
|
|
207
|
+
await this.httpTransport.finishAuth(authorizationCode);
|
|
208
|
+
}
|
|
209
|
+
/** Server capabilities returned by `initialize`. Undefined before connect. */
|
|
210
|
+
getServerCapabilities() {
|
|
211
|
+
return this.sdk.getServerCapabilities();
|
|
212
|
+
}
|
|
213
|
+
// -------------------------------------------------------------------------
|
|
214
|
+
// Resources
|
|
215
|
+
// -------------------------------------------------------------------------
|
|
216
|
+
async listResources(options) {
|
|
217
|
+
this.assertConnected('listResources');
|
|
218
|
+
this.requireCapability('resources');
|
|
219
|
+
const result = await this.sdk.listResources(undefined, toSdkRequestOptions(options));
|
|
220
|
+
return result.resources;
|
|
221
|
+
}
|
|
222
|
+
async readResource(uri, options) {
|
|
223
|
+
this.assertConnected('readResource');
|
|
224
|
+
this.requireCapability('resources');
|
|
225
|
+
const result = await this.sdk.readResource({ uri }, toSdkRequestOptions(options));
|
|
226
|
+
return result.contents;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Subscribe to updates for a single resource URI. The returned function
|
|
230
|
+
* removes this subscription (and unsubscribes on the wire once no
|
|
231
|
+
* subscribers remain for the URI).
|
|
232
|
+
*
|
|
233
|
+
* Requires the server to advertise `resources.subscribe`; throws
|
|
234
|
+
* `McpCapabilityError` otherwise.
|
|
235
|
+
*/
|
|
236
|
+
async subscribeResource(uri, handler, options) {
|
|
237
|
+
this.assertConnected('subscribeResource');
|
|
238
|
+
const caps = this.getServerCapabilities();
|
|
239
|
+
if (!caps?.resources)
|
|
240
|
+
throw new McpCapabilityError('resources');
|
|
241
|
+
if (!caps.resources.subscribe)
|
|
242
|
+
throw new McpCapabilityError('resources.subscribe');
|
|
243
|
+
let subscribers = this.resourceSubscribers.get(uri);
|
|
244
|
+
if (!subscribers) {
|
|
245
|
+
subscribers = new Set();
|
|
246
|
+
this.resourceSubscribers.set(uri, subscribers);
|
|
247
|
+
await this.sdk.subscribeResource({ uri }, toSdkRequestOptions(options));
|
|
248
|
+
}
|
|
249
|
+
subscribers.add(handler);
|
|
250
|
+
let disposed = false;
|
|
251
|
+
return async () => {
|
|
252
|
+
if (disposed)
|
|
253
|
+
return;
|
|
254
|
+
disposed = true;
|
|
255
|
+
const current = this.resourceSubscribers.get(uri);
|
|
256
|
+
if (!current)
|
|
257
|
+
return;
|
|
258
|
+
current.delete(handler);
|
|
259
|
+
if (current.size === 0) {
|
|
260
|
+
this.resourceSubscribers.delete(uri);
|
|
261
|
+
if (this.connected) {
|
|
262
|
+
await this.sdk.unsubscribeResource({ uri });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
// -------------------------------------------------------------------------
|
|
268
|
+
// Prompts
|
|
269
|
+
// -------------------------------------------------------------------------
|
|
270
|
+
// -------------------------------------------------------------------------
|
|
271
|
+
// Tools
|
|
272
|
+
// -------------------------------------------------------------------------
|
|
273
|
+
/**
|
|
274
|
+
* Enumerate tools the server exposes. Requires the server to advertise the
|
|
275
|
+
* `tools` capability.
|
|
276
|
+
*
|
|
277
|
+
* Returns the SDK's `Tool` shape unchanged — name, description, input JSON
|
|
278
|
+
* Schema (as `inputSchema`), and any spec-defined annotations.
|
|
279
|
+
*/
|
|
280
|
+
async listTools(options) {
|
|
281
|
+
this.assertConnected('listTools');
|
|
282
|
+
this.requireCapability('tools');
|
|
283
|
+
const result = await this.sdk.listTools(undefined, toSdkRequestOptions(options));
|
|
284
|
+
return result.tools;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Invoke a tool by name with the supplied structured arguments. Requires
|
|
288
|
+
* the server to advertise `tools`. Returns the full SDK `CallToolResult`
|
|
289
|
+
* (structured content + isError flag + optional `_meta`).
|
|
290
|
+
*
|
|
291
|
+
* Tool failures surface as `{ isError: true, content: [...] }` rather than
|
|
292
|
+
* thrown exceptions — the server is explicitly asked to communicate tool
|
|
293
|
+
* errors in-band per the MCP spec. Transport-level failures still throw.
|
|
294
|
+
*/
|
|
295
|
+
async callTool(name, args, options) {
|
|
296
|
+
this.assertConnected('callTool');
|
|
297
|
+
this.requireCapability('tools');
|
|
298
|
+
const params = { name };
|
|
299
|
+
if (args !== undefined)
|
|
300
|
+
params.arguments = args;
|
|
301
|
+
const result = await this.sdk.callTool(params, undefined, toSdkRequestOptions(options));
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
// -------------------------------------------------------------------------
|
|
305
|
+
// Prompts
|
|
306
|
+
// -------------------------------------------------------------------------
|
|
307
|
+
async listPrompts(options) {
|
|
308
|
+
this.assertConnected('listPrompts');
|
|
309
|
+
this.requireCapability('prompts');
|
|
310
|
+
const result = await this.sdk.listPrompts(undefined, toSdkRequestOptions(options));
|
|
311
|
+
return result.prompts;
|
|
312
|
+
}
|
|
313
|
+
async getPrompt(name, args, options) {
|
|
314
|
+
this.assertConnected('getPrompt');
|
|
315
|
+
this.requireCapability('prompts');
|
|
316
|
+
const params = args === undefined ? { name } : { name, arguments: args };
|
|
317
|
+
const result = await this.sdk.getPrompt(params, toSdkRequestOptions(options));
|
|
318
|
+
return {
|
|
319
|
+
description: result.description,
|
|
320
|
+
messages: result.messages,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
// -------------------------------------------------------------------------
|
|
324
|
+
// List-changed subscriptions
|
|
325
|
+
// -------------------------------------------------------------------------
|
|
326
|
+
onResourcesListChanged(handler) {
|
|
327
|
+
return this.addListChanged('resources', handler);
|
|
328
|
+
}
|
|
329
|
+
onPromptsListChanged(handler) {
|
|
330
|
+
return this.addListChanged('prompts', handler);
|
|
331
|
+
}
|
|
332
|
+
onToolsListChanged(handler) {
|
|
333
|
+
return this.addListChanged('tools', handler);
|
|
334
|
+
}
|
|
335
|
+
// -------------------------------------------------------------------------
|
|
336
|
+
// Roots
|
|
337
|
+
// -------------------------------------------------------------------------
|
|
338
|
+
/**
|
|
339
|
+
* Notify the server that the client's roots have changed. The server will
|
|
340
|
+
* typically re-fetch via `roots/list`, which routes to the `rootsProvider`
|
|
341
|
+
* passed at construction. No-op if the client wasn't constructed with a
|
|
342
|
+
* `rootsProvider` — advertising list-changed without a provider is
|
|
343
|
+
* nonsensical, so we fail fast.
|
|
344
|
+
*/
|
|
345
|
+
async notifyRootsListChanged() {
|
|
346
|
+
this.assertConnected('notifyRootsListChanged');
|
|
347
|
+
if (!this.options.rootsProvider) {
|
|
348
|
+
throw new Error('McpClient.notifyRootsListChanged() requires a rootsProvider at construction');
|
|
349
|
+
}
|
|
350
|
+
await this.sdk.sendRootsListChanged();
|
|
351
|
+
}
|
|
352
|
+
// -------------------------------------------------------------------------
|
|
353
|
+
// Completions
|
|
354
|
+
// -------------------------------------------------------------------------
|
|
355
|
+
/**
|
|
356
|
+
* Request argument completions from the server. The reference points at a
|
|
357
|
+
* prompt or resource-template; the server returns suggestion values for
|
|
358
|
+
* the named argument given the current partial value.
|
|
359
|
+
*/
|
|
360
|
+
async complete(reference, argument, options) {
|
|
361
|
+
this.assertConnected('complete');
|
|
362
|
+
this.requireCapability('completions');
|
|
363
|
+
const params = { ref: reference, argument };
|
|
364
|
+
const result = await this.sdk.complete(params, toSdkRequestOptions(options));
|
|
365
|
+
return result.completion;
|
|
366
|
+
}
|
|
367
|
+
// -------------------------------------------------------------------------
|
|
368
|
+
// Logging (PR-0.3)
|
|
369
|
+
// -------------------------------------------------------------------------
|
|
370
|
+
/**
|
|
371
|
+
* Set the minimum log level the server should emit. Requires the server to
|
|
372
|
+
* advertise the `logging` capability.
|
|
373
|
+
*/
|
|
374
|
+
async setLoggingLevel(level, options) {
|
|
375
|
+
this.assertConnected('setLoggingLevel');
|
|
376
|
+
this.requireCapability('logging');
|
|
377
|
+
await this.sdk.setLoggingLevel(level, toSdkRequestOptions(options));
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Subscribe to server-emitted log messages. Returns an unregister function.
|
|
381
|
+
*
|
|
382
|
+
* Implemented on top of the generic `on()` fan-out, so multiple subscribers
|
|
383
|
+
* coexist cleanly (admin UI, CLI logger, telemetry exporter, …).
|
|
384
|
+
*/
|
|
385
|
+
onLog(handler) {
|
|
386
|
+
return this.on(LoggingMessageNotificationSchema, (notification) => {
|
|
387
|
+
handler(notification.params);
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
// -------------------------------------------------------------------------
|
|
391
|
+
// Generic notification subscription (PR-0.3)
|
|
392
|
+
// -------------------------------------------------------------------------
|
|
393
|
+
/**
|
|
394
|
+
* Subscribe to arbitrary server notifications by zod schema. First
|
|
395
|
+
* subscription per schema installs a single SDK handler that fans out to
|
|
396
|
+
* every registered subscriber; later subscriptions join the existing fan.
|
|
397
|
+
* Returns an unregister function.
|
|
398
|
+
*
|
|
399
|
+
* Typically callers use the purpose-built subscribers (`onLog`,
|
|
400
|
+
* `onResourcesListChanged`, `subscribeResource`) rather than this. Use
|
|
401
|
+
* `on()` for schemas the client doesn't yet expose a named subscriber for.
|
|
402
|
+
*/
|
|
403
|
+
on(schema, handler) {
|
|
404
|
+
let subscribers = this.notificationSubscribers.get(schema);
|
|
405
|
+
if (!subscribers) {
|
|
406
|
+
subscribers = new Set();
|
|
407
|
+
this.notificationSubscribers.set(schema, subscribers);
|
|
408
|
+
const fanOut = (notification) => {
|
|
409
|
+
for (const h of subscribers ?? []) {
|
|
410
|
+
try {
|
|
411
|
+
h(notification);
|
|
412
|
+
}
|
|
413
|
+
catch {
|
|
414
|
+
// Swallow per-subscriber errors.
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
this.sdk.setNotificationHandler(schema, fanOut);
|
|
419
|
+
}
|
|
420
|
+
subscribers.add(handler);
|
|
421
|
+
return () => {
|
|
422
|
+
subscribers?.delete(handler);
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
// -------------------------------------------------------------------------
|
|
426
|
+
// Health
|
|
427
|
+
// -------------------------------------------------------------------------
|
|
428
|
+
async ping(options) {
|
|
429
|
+
this.assertConnected('ping');
|
|
430
|
+
await this.sdk.ping(toSdkRequestOptions(options));
|
|
431
|
+
}
|
|
432
|
+
// -------------------------------------------------------------------------
|
|
433
|
+
// Internals
|
|
434
|
+
// -------------------------------------------------------------------------
|
|
435
|
+
createTransport() {
|
|
436
|
+
const t = this.options.transport;
|
|
437
|
+
switch (t.kind) {
|
|
438
|
+
case 'stdio':
|
|
439
|
+
return new StdioClientTransport({
|
|
440
|
+
command: t.command,
|
|
441
|
+
args: t.args,
|
|
442
|
+
env: t.env,
|
|
443
|
+
cwd: t.cwd,
|
|
444
|
+
});
|
|
445
|
+
case 'custom':
|
|
446
|
+
return t.transport;
|
|
447
|
+
case 'streamable-http': {
|
|
448
|
+
const url = t.url instanceof URL ? t.url : new URL(t.url);
|
|
449
|
+
const sdkOptions = {};
|
|
450
|
+
if (t.requestInit)
|
|
451
|
+
sdkOptions.requestInit = t.requestInit;
|
|
452
|
+
if (t.fetch)
|
|
453
|
+
sdkOptions.fetch = t.fetch;
|
|
454
|
+
if (t.sessionId)
|
|
455
|
+
sdkOptions.sessionId = t.sessionId;
|
|
456
|
+
if (t.reconnectionOptions)
|
|
457
|
+
sdkOptions.reconnectionOptions = t.reconnectionOptions;
|
|
458
|
+
if (t.authProvider)
|
|
459
|
+
sdkOptions.authProvider = t.authProvider;
|
|
460
|
+
const transport = new StreamableHTTPClientTransport(url, sdkOptions);
|
|
461
|
+
this.httpTransport = transport;
|
|
462
|
+
return transport;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
assertConnected(method) {
|
|
467
|
+
if (!this.connected)
|
|
468
|
+
throw new McpNotConnectedError(method);
|
|
469
|
+
}
|
|
470
|
+
requireCapability(capability) {
|
|
471
|
+
const caps = this.getServerCapabilities();
|
|
472
|
+
if (!caps?.[capability])
|
|
473
|
+
throw new McpCapabilityError(capability);
|
|
474
|
+
}
|
|
475
|
+
addListChanged(channel, handler) {
|
|
476
|
+
this.listChangedHandlers[channel].add(handler);
|
|
477
|
+
return () => {
|
|
478
|
+
this.listChangedHandlers[channel].delete(handler);
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
fireListChanged(channel) {
|
|
482
|
+
for (const handler of this.listChangedHandlers[channel]) {
|
|
483
|
+
try {
|
|
484
|
+
handler();
|
|
485
|
+
}
|
|
486
|
+
catch {
|
|
487
|
+
// Swallow per-subscriber errors.
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EACL,6BAA6B,GAG9B,MAAM,oDAAoD,CAAC;AAI5D,OAAO,EAML,0BAA0B,EAG1B,mBAAmB,EAEnB,sBAAsB,EAGtB,gCAAgC,EAQhC,iCAAiC,GAIlC,MAAM,oCAAoC,CAAC;AAmI5C,iEAAiE;AACjE,SAAS,mBAAmB,CAAC,OAA2B;IACtD,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,MAAM,UAAU,GAAmB,EAAE,CAAC;IACtC,IAAI,OAAO,CAAC,MAAM;QAAE,UAAU,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IACvD,IAAI,OAAO,CAAC,UAAU;QAAE,UAAU,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IACnE,IAAI,OAAO,CAAC,OAAO,KAAK,SAA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AAE,MAAM,IAAI,oBAAoB,CAAC,MAAM,CAAC,CAAC;IAC9D,CAAC;IAEO,iBAAiB,CACvB,UAAyE;QAEzE,MAAM,IAAI,GAAG,IAAI,CAAC,qBAAqB,EAAE,CAAC;QAC1C,IAAI,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC;YAAE,MAAM,IAAI,kBAAkB,CAAC,UAAU,CAAC,CAAC;IACpE,CAAC;IAEO,cAAc,CAAC,OAA2B,EAAE,OAAmB;QACrE,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC/C,OAAO,GAAG,EAAE;YACV,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACpD,CAAC,CAAC;IACJ,CAAC;IAEO,eAAe,CAAC,OAA2B;QACjD,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,EAAE,CAAC;YACxD,IAAI,CAAC;gBACH,OAAO,EAAE,CAAC;YACZ,CAAC;YAAC,MAAM,CAAC;gBACP,iCAAiC;YACnC,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
package/dist/hypervisor.d.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* Wire format: newline-delimited JSON-RPC 2.0 (stdin/stdout)
|
|
15
15
|
*/
|
|
16
|
+
import type { McpMeterSink } from './metering.js';
|
|
16
17
|
export interface MCPServerConfig {
|
|
17
18
|
/** Unique name for this server (used in tool namespacing) */
|
|
18
19
|
name: string;
|
|
@@ -89,6 +90,7 @@ export declare class MCPHypervisor {
|
|
|
89
90
|
/** Tenant-scoped server instances: key = `${tenantId}:${serverName}` */
|
|
90
91
|
private tenantServers;
|
|
91
92
|
private credentialResolver;
|
|
93
|
+
private meterSink;
|
|
92
94
|
private requestCounter;
|
|
93
95
|
private pendingRequests;
|
|
94
96
|
private healthCheckTimer;
|
|
@@ -156,6 +158,22 @@ export declare class MCPHypervisor {
|
|
|
156
158
|
* Must be set before calling any tenant-scoped methods.
|
|
157
159
|
*/
|
|
158
160
|
setCredentialResolver(resolver: MCPCredentialResolver): void;
|
|
161
|
+
/**
|
|
162
|
+
* Install a consumer-wired metering sink. Fires exactly once per
|
|
163
|
+
* tool-call boundary (success or failure) with an `McpMeterEvent`.
|
|
164
|
+
* Sinks may be sync or async — invocation is fire-and-forget; sink
|
|
165
|
+
* errors are logged at warn and never propagate into the call path.
|
|
166
|
+
*
|
|
167
|
+
* See `./metering.ts` for the event shape. Pass `null` to disable.
|
|
168
|
+
*/
|
|
169
|
+
setUsageMeterSink(sink: McpMeterSink | null): void;
|
|
170
|
+
/**
|
|
171
|
+
* Fire the consumer-wired metering sink. Swallows errors so
|
|
172
|
+
* observability can never corrupt a successful tool call.
|
|
173
|
+
*
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
176
|
+
private emitMeter;
|
|
159
177
|
/**
|
|
160
178
|
* Start an MCP server with tenant-specific credentials.
|
|
161
179
|
* Each tenant gets its own isolated server process with credentials
|
package/dist/hypervisor.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hypervisor.d.ts","sourceRoot":"","sources":["../src/hypervisor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;
|
|
1
|
+
{"version":3,"file":"hypervisor.d.ts","sourceRoot":"","sources":["../src/hypervisor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,OAAO,KAAK,EAAiB,YAAY,EAAE,MAAM,eAAe,CAAC;AAMjE,MAAM,WAAW,eAAe;IAC9B,6DAA6D;IAC7D,IAAI,EAAE,MAAM,CAAC;IACb,8CAA8C;IAC9C,OAAO,EAAE,MAAM,CAAC;IAChB,+BAA+B;IAC/B,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,uCAAuC;IACvC,GAAG,CAAC,EAAE,MAAM,CAAC,MAA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