@jangmanj/mcp-client 0.5.0 → 0.6.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 +35 -9
- package/dist/client/events.d.ts +33 -0
- package/dist/client/events.js +84 -0
- package/dist/client/index.d.ts +11 -0
- package/dist/client/index.js +30 -1
- package/dist/core/index.d.ts +7 -1
- package/dist/core/index.js +18 -0
- package/dist/testing/index.d.ts +4 -2
- package/dist/testing/index.js +46 -7
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -10,27 +10,51 @@ npm install @jangmanj/mcp-client
|
|
|
10
10
|
|
|
11
11
|
## Quickstart
|
|
12
12
|
|
|
13
|
+
Push first: subscribe to events and react as they arrive. Pull (tools, resources) is
|
|
14
|
+
there when you need it.
|
|
15
|
+
|
|
13
16
|
```ts
|
|
14
17
|
import { connect } from "@jangmanj/mcp-client/client";
|
|
15
18
|
|
|
16
19
|
const connection = await connect({
|
|
17
|
-
stdio: { command: "npx", args: ["-y", "
|
|
18
|
-
reconnectOnDrop: true,
|
|
20
|
+
stdio: { command: "npx", args: ["-y", "your-mcp-server"] },
|
|
21
|
+
reconnectOnDrop: true,
|
|
19
22
|
});
|
|
20
23
|
|
|
21
24
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
25
|
+
// Events extension: live occurrences over events/stream (heartbeat + reconnect).
|
|
26
|
+
const mail = await connection.events.subscribe("mail", { folder: "inbox" });
|
|
27
|
+
for await (const event of mail) {
|
|
28
|
+
console.log(event.eventId, event.data);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Standard MCP subscriptions: tools/prompts/resources list_changed, resource updates.
|
|
32
|
+
const changes = connection.listen({ tools: true });
|
|
33
|
+
await changes.acknowledged;
|
|
34
|
+
for await (const change of changes.events) {
|
|
35
|
+
console.log(change.method); // e.g. notifications/tools/list_changed -> re-list tools
|
|
36
|
+
}
|
|
26
37
|
} finally {
|
|
27
38
|
await connection.close();
|
|
28
39
|
}
|
|
29
40
|
```
|
|
30
41
|
|
|
31
|
-
`
|
|
32
|
-
|
|
33
|
-
`connection.
|
|
42
|
+
`connection.events` uses the optional `io.modelcontextprotocol/events` extension and
|
|
43
|
+
calls `server/discover` lazily to check support; `subscription.poll()` is available
|
|
44
|
+
when you explicitly want pull. `connection.listen()` is the spec-standard
|
|
45
|
+
`subscriptions/listen` stream.
|
|
46
|
+
|
|
47
|
+
Tools work the same connection, without `Outcome` matching:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
const tools = await connection.tools.list();
|
|
51
|
+
const result = await connection.tools.call("list_allowed_directories");
|
|
52
|
+
console.log(result.content);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`tools.list()` / `tools.call()` return values or throw `McpOutcomeError`. For the full
|
|
56
|
+
protocol surface (MRTR, tasks, uncertain writes), use `connection.ops` directly and
|
|
57
|
+
match on `Outcome.kind`.
|
|
34
58
|
|
|
35
59
|
HTTP servers use `http:` instead of `stdio:`:
|
|
36
60
|
|
|
@@ -80,6 +104,8 @@ const connection = await connect({
|
|
|
80
104
|
```
|
|
81
105
|
|
|
82
106
|
Throw `FakeJsonRpcError(code, message)` from a handler to simulate protocol errors.
|
|
107
|
+
`transport.push(notification, requestId?)` injects notifications into active request
|
|
108
|
+
streams, so you can test pushed events and `list_changed` without a live server.
|
|
83
109
|
|
|
84
110
|
## Low-level API
|
|
85
111
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Push-first events facade over @mcp-client/ext-events.
|
|
3
|
+
*
|
|
4
|
+
* subscribe() returns an AsyncIterable of occurrences backed by the live
|
|
5
|
+
* events/stream (with heartbeat + reconnect); poll() is available explicitly
|
|
6
|
+
* for servers or callers that prefer pull.
|
|
7
|
+
*/
|
|
8
|
+
import { type McpClient } from "../core/index.js";
|
|
9
|
+
import { EventsClient, type Obj, type Occurrence } from "../ext-events/index.js";
|
|
10
|
+
export type EventsPollResult = Awaited<ReturnType<EventsClient["poll"]>>;
|
|
11
|
+
export interface EventSubscription extends AsyncIterable<Occurrence> {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
/** Explicit pull; the async iterable itself is push-based. */
|
|
14
|
+
poll(): Promise<EventsPollResult>;
|
|
15
|
+
close(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export interface Events {
|
|
18
|
+
/** List event streams offered by the server. */
|
|
19
|
+
list(): Promise<Obj>;
|
|
20
|
+
/** Subscribe to a named event stream; iterate to receive pushed occurrences. */
|
|
21
|
+
subscribe(name: string, args?: Obj): Promise<EventSubscription>;
|
|
22
|
+
}
|
|
23
|
+
export declare class EventsFacade implements Events {
|
|
24
|
+
#private;
|
|
25
|
+
private readonly connection;
|
|
26
|
+
constructor(connection: {
|
|
27
|
+
readonly client: McpClient;
|
|
28
|
+
});
|
|
29
|
+
setCapabilities(capabilities: unknown): void;
|
|
30
|
+
list(): Promise<Obj>;
|
|
31
|
+
subscribe(name: string, args?: Obj): Promise<EventSubscription>;
|
|
32
|
+
dispose(): void;
|
|
33
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Push-first events facade over @mcp-client/ext-events.
|
|
3
|
+
*
|
|
4
|
+
* subscribe() returns an AsyncIterable of occurrences backed by the live
|
|
5
|
+
* events/stream (with heartbeat + reconnect); poll() is available explicitly
|
|
6
|
+
* for servers or callers that prefer pull.
|
|
7
|
+
*/
|
|
8
|
+
import { NotificationQueue } from "../core/index.js";
|
|
9
|
+
import { createEventsExtension, EventsClient, } from "../ext-events/index.js";
|
|
10
|
+
class EventSubscriptionImpl {
|
|
11
|
+
events;
|
|
12
|
+
id;
|
|
13
|
+
name;
|
|
14
|
+
#queue = new NotificationQueue();
|
|
15
|
+
#handle;
|
|
16
|
+
#closed = false;
|
|
17
|
+
constructor(events, id, name) {
|
|
18
|
+
this.events = events;
|
|
19
|
+
this.id = id;
|
|
20
|
+
this.name = name;
|
|
21
|
+
}
|
|
22
|
+
poll() {
|
|
23
|
+
return this.events.poll(this.id);
|
|
24
|
+
}
|
|
25
|
+
#start() {
|
|
26
|
+
if (this.#handle || this.#closed)
|
|
27
|
+
return;
|
|
28
|
+
this.#handle = this.events.push(this.id, (message) => {
|
|
29
|
+
if (message.type === "event")
|
|
30
|
+
this.#queue.push(message.event);
|
|
31
|
+
else if (message.type === "terminated" || message.type === "error")
|
|
32
|
+
this.#queue.close();
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
[Symbol.asyncIterator]() {
|
|
36
|
+
this.#start();
|
|
37
|
+
return this.#queue[Symbol.asyncIterator]();
|
|
38
|
+
}
|
|
39
|
+
async close() {
|
|
40
|
+
if (this.#closed)
|
|
41
|
+
return;
|
|
42
|
+
this.#closed = true;
|
|
43
|
+
this.#handle?.close();
|
|
44
|
+
this.events.forget(this.id);
|
|
45
|
+
this.#queue.close();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export class EventsFacade {
|
|
49
|
+
connection;
|
|
50
|
+
#events;
|
|
51
|
+
#capabilities;
|
|
52
|
+
constructor(connection) {
|
|
53
|
+
this.connection = connection;
|
|
54
|
+
}
|
|
55
|
+
setCapabilities(capabilities) {
|
|
56
|
+
this.#capabilities = capabilities;
|
|
57
|
+
}
|
|
58
|
+
async #ensure() {
|
|
59
|
+
if (this.#events)
|
|
60
|
+
return this.#events;
|
|
61
|
+
if (this.#capabilities === undefined) {
|
|
62
|
+
const discovered = await this.connection.client.discover();
|
|
63
|
+
this.#capabilities = discovered.kind === "ok" ? discovered.value.capabilities : null;
|
|
64
|
+
}
|
|
65
|
+
this.#events = new EventsClient({
|
|
66
|
+
extension: createEventsExtension({ enabled: true }),
|
|
67
|
+
peerCapabilities: () => this.#capabilities,
|
|
68
|
+
request: (method, params, opts) => this.connection.client.request(method, params, opts),
|
|
69
|
+
openStream: (method, params, opts) => this.connection.client.openStream(method, params, opts),
|
|
70
|
+
});
|
|
71
|
+
return this.#events;
|
|
72
|
+
}
|
|
73
|
+
async list() {
|
|
74
|
+
return (await this.#ensure()).list();
|
|
75
|
+
}
|
|
76
|
+
async subscribe(name, args = {}) {
|
|
77
|
+
const events = await this.#ensure();
|
|
78
|
+
return new EventSubscriptionImpl(events, events.subscription(name, args), name);
|
|
79
|
+
}
|
|
80
|
+
dispose() {
|
|
81
|
+
this.#events?.dispose();
|
|
82
|
+
this.#events = undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
package/dist/client/index.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ import { McpClient, type McpClientOptions, type ServerRef, type Transport } from
|
|
|
10
10
|
import { type Ops, type ToolCallOptions, type ToolCallResult, type ToolEntry } from "../ops/index.js";
|
|
11
11
|
import { type HttpTransportOptions } from "../transport-http/index.js";
|
|
12
12
|
import { type StdioTransportOptions } from "../transport-stdio/index.js";
|
|
13
|
+
import { type ListenFilter, type Subscription } from "../mrtr-cache/index.js";
|
|
14
|
+
import { type Events } from "./events.js";
|
|
13
15
|
export interface ConnectBaseOptions {
|
|
14
16
|
/** Server identity; defaults to the command or URL. */
|
|
15
17
|
readonly server?: ServerRef;
|
|
@@ -18,6 +20,8 @@ export interface ConnectBaseOptions {
|
|
|
18
20
|
readonly timeoutMs?: number;
|
|
19
21
|
/** Probe server/discover during connect and throw on failure. Default false. */
|
|
20
22
|
readonly discover?: boolean;
|
|
23
|
+
/** Declare the events extension and expose connection.events. Default true. */
|
|
24
|
+
readonly events?: boolean;
|
|
21
25
|
/**
|
|
22
26
|
* Rebuild the connection and retry once after a dropped connection
|
|
23
27
|
* (connect/closed/stream-broken/process-exit). Requires a transport factory
|
|
@@ -42,6 +46,10 @@ export interface Connection {
|
|
|
42
46
|
readonly client: McpClient;
|
|
43
47
|
readonly ops: Ops;
|
|
44
48
|
readonly tools: SimpleTools;
|
|
49
|
+
/** Push-based event subscriptions (events extension). */
|
|
50
|
+
readonly events: Events;
|
|
51
|
+
/** Standard MCP subscriptions/listen (list_changed, resource updates). */
|
|
52
|
+
listen(filter: ListenFilter): Subscription;
|
|
45
53
|
/** Tear down and rebuild the underlying client (new transport). */
|
|
46
54
|
reconnect(): Promise<void>;
|
|
47
55
|
close(opts?: {
|
|
@@ -52,3 +60,6 @@ export interface Connection {
|
|
|
52
60
|
export declare function connect(options: ConnectOptions): Promise<Connection>;
|
|
53
61
|
export { McpOutcomeError, unwrapOutcome } from "../core/index.js";
|
|
54
62
|
export type { ToolCallOptions, ToolCallResult, ToolEntry } from "../ops/index.js";
|
|
63
|
+
export type { Events, EventSubscription, EventsPollResult } from "./events.js";
|
|
64
|
+
export type { Occurrence } from "../ext-events/index.js";
|
|
65
|
+
export type { ListenFilter, Subscription, ChangeEvent, Acknowledgment } from "../mrtr-cache/index.js";
|
package/dist/client/index.js
CHANGED
|
@@ -10,13 +10,21 @@ import { McpClient, unwrapOutcome, } from "../core/index.js";
|
|
|
10
10
|
import { createOps, } from "../ops/index.js";
|
|
11
11
|
import { HttpTransport } from "../transport-http/index.js";
|
|
12
12
|
import { StdioTransport } from "../transport-stdio/index.js";
|
|
13
|
+
import { createEventsExtension } from "../ext-events/index.js";
|
|
14
|
+
import { SubscriptionManager, } from "../mrtr-cache/index.js";
|
|
15
|
+
import { EventsFacade } from "./events.js";
|
|
13
16
|
const RETRY_KINDS = new Set(["connect", "closed", "stream-broken", "process-exit"]);
|
|
14
17
|
class ConnectionImpl {
|
|
15
18
|
#current;
|
|
16
19
|
#options;
|
|
20
|
+
events;
|
|
21
|
+
#binding = "http";
|
|
22
|
+
#manager;
|
|
23
|
+
#subscriptions = new Set();
|
|
17
24
|
constructor(options) {
|
|
18
25
|
this.#options = options;
|
|
19
26
|
this.#current = this.#build();
|
|
27
|
+
this.events = new EventsFacade(this);
|
|
20
28
|
}
|
|
21
29
|
get client() {
|
|
22
30
|
return this.#current.client;
|
|
@@ -33,8 +41,20 @@ class ConnectionImpl {
|
|
|
33
41
|
return new StdioTransport(this.#options.stdio);
|
|
34
42
|
return new HttpTransport(this.#options.http);
|
|
35
43
|
}
|
|
44
|
+
listen(filter) {
|
|
45
|
+
this.#manager ??= new SubscriptionManager({
|
|
46
|
+
open: (method, params) => this.client.openStream(method, params),
|
|
47
|
+
binding: this.#binding,
|
|
48
|
+
serverRef: this.client.server.key,
|
|
49
|
+
reconnect: "on-abrupt",
|
|
50
|
+
});
|
|
51
|
+
const subscription = this.#manager.listen(filter);
|
|
52
|
+
this.#subscriptions.add(subscription);
|
|
53
|
+
return subscription;
|
|
54
|
+
}
|
|
36
55
|
#build() {
|
|
37
56
|
const transport = this.#transport();
|
|
57
|
+
this.#binding = transport.binding === "stdio" ? "stdio" : "http";
|
|
38
58
|
const server = this.#options.server ??
|
|
39
59
|
("stdio" in this.#options
|
|
40
60
|
? { key: this.#options.stdio.command }
|
|
@@ -44,6 +64,9 @@ class ConnectionImpl {
|
|
|
44
64
|
const client = new McpClient({
|
|
45
65
|
transport,
|
|
46
66
|
server,
|
|
67
|
+
...(this.#options.events === false
|
|
68
|
+
? {}
|
|
69
|
+
: { extensions: [createEventsExtension({ enabled: true })] }),
|
|
47
70
|
...(this.#options.clientInfo !== undefined ? { clientInfo: this.#options.clientInfo } : {}),
|
|
48
71
|
...(this.#options.timeoutMs !== undefined
|
|
49
72
|
? { defaults: { timeoutMs: this.#options.timeoutMs } }
|
|
@@ -54,6 +77,7 @@ class ConnectionImpl {
|
|
|
54
77
|
async reconnect() {
|
|
55
78
|
const previous = this.#current;
|
|
56
79
|
this.#current = this.#build();
|
|
80
|
+
this.#manager?.reconnected();
|
|
57
81
|
try {
|
|
58
82
|
await previous.client.close({ graceMs: 500 });
|
|
59
83
|
}
|
|
@@ -62,6 +86,10 @@ class ConnectionImpl {
|
|
|
62
86
|
}
|
|
63
87
|
}
|
|
64
88
|
async close(opts) {
|
|
89
|
+
this.events.dispose();
|
|
90
|
+
for (const subscription of this.#subscriptions)
|
|
91
|
+
subscription.close();
|
|
92
|
+
this.#subscriptions.clear();
|
|
65
93
|
await this.#current.client.close(opts);
|
|
66
94
|
}
|
|
67
95
|
tools = {
|
|
@@ -87,7 +115,8 @@ export async function connect(options) {
|
|
|
87
115
|
const connection = new ConnectionImpl(options);
|
|
88
116
|
if (options.discover) {
|
|
89
117
|
try {
|
|
90
|
-
unwrapOutcome(await connection.client.discover());
|
|
118
|
+
const discovered = unwrapOutcome(await connection.client.discover());
|
|
119
|
+
connection.events.setCapabilities(discovered.capabilities);
|
|
91
120
|
}
|
|
92
121
|
catch (error) {
|
|
93
122
|
await connection.close().catch(() => { });
|
package/dist/core/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { type Disposable, type Extension } from "./extension.js";
|
|
|
2
2
|
import { type IdGenerator } from "./jsonrpc.js";
|
|
3
3
|
import { type EraState, type LifecyclePolicyOptions, type ProbeOptions } from "./lifecycle.js";
|
|
4
4
|
import type { PortRegistry, Transport } from "./ports.js";
|
|
5
|
-
import { type ClientRequest, type Limits, type NotifyOptions, type RequestOptions } from "./request.js";
|
|
5
|
+
import { type ClientRequest, type Limits, type NotifyOptions, type RequestOptions, type StreamHandle } from "./request.js";
|
|
6
6
|
import type { DiscoverResult } from "./schema/index.js";
|
|
7
7
|
export * as McpSchema from "./schema/index.js";
|
|
8
8
|
export type { DiscoverResult } from "./schema/index.js";
|
|
@@ -50,6 +50,12 @@ export declare class McpClient {
|
|
|
50
50
|
constructor(opts: McpClientOptions);
|
|
51
51
|
request<T = Record<string, unknown>>(method: string, params?: Record<string, unknown>, opts?: RequestOptions): ClientRequest<T>;
|
|
52
52
|
notify(method: string, params?: Record<string, unknown>, opts?: NotifyOptions): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Open a long-lived request stream (subscriptions/listen, events/stream).
|
|
55
|
+
* Uses the same per-request metadata, id allocation, and lifecycle handling
|
|
56
|
+
* as request(); no default request timeout applies to streams.
|
|
57
|
+
*/
|
|
58
|
+
openStream(method: string, params?: Record<string, unknown>, opts?: RequestOptions): StreamHandle;
|
|
53
59
|
discover(opts?: RequestOptions): ClientRequest<DiscoverResult>;
|
|
54
60
|
/**
|
|
55
61
|
* Probes the server era with server/discover once per server key and caches the verdict in
|
package/dist/core/index.js
CHANGED
|
@@ -61,6 +61,24 @@ export class McpClient {
|
|
|
61
61
|
notify(method, params = {}, opts) {
|
|
62
62
|
return this.manager.notify(method, params, opts);
|
|
63
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Open a long-lived request stream (subscriptions/listen, events/stream).
|
|
66
|
+
* Uses the same per-request metadata, id allocation, and lifecycle handling
|
|
67
|
+
* as request(); no default request timeout applies to streams.
|
|
68
|
+
*/
|
|
69
|
+
openStream(method, params = {}, opts = {}) {
|
|
70
|
+
const req = this.manager.request(method, params, {
|
|
71
|
+
...opts,
|
|
72
|
+
stream: true,
|
|
73
|
+
idempotent: opts.idempotent ?? true,
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
id: req.id,
|
|
77
|
+
close: () => req.cancel(),
|
|
78
|
+
done: Promise.resolve(req),
|
|
79
|
+
[Symbol.asyncIterator]: () => req.notifications[Symbol.asyncIterator](),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
64
82
|
discover(opts) {
|
|
65
83
|
return this.manager.request("server/discover", {}, { ...opts, idempotent: true });
|
|
66
84
|
}
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* });
|
|
8
8
|
* const connection = await connect({ transport });
|
|
9
9
|
*/
|
|
10
|
-
import type
|
|
10
|
+
import { type Exchange, type JsonRpcNotification, type JsonRpcRequest, type RequestId, type Transport, type TransportCall, type TransportDiagnostic } from "../core/index.js";
|
|
11
11
|
/** Throw from a handler to return a JSON-RPC error instead of a result. */
|
|
12
12
|
export declare class FakeJsonRpcError extends Error {
|
|
13
13
|
readonly code: number;
|
|
@@ -19,7 +19,9 @@ export declare class InMemoryTransport implements Transport {
|
|
|
19
19
|
#private;
|
|
20
20
|
readonly binding = "in-memory";
|
|
21
21
|
constructor(handler: InMemoryHandler);
|
|
22
|
-
|
|
22
|
+
/** Push a notification into one active request stream (or all of them). */
|
|
23
|
+
push(notification: JsonRpcNotification, id?: RequestId): boolean;
|
|
24
|
+
start(msg: JsonRpcRequest, ctx: TransportCall): Exchange;
|
|
23
25
|
notify(_msg: JsonRpcNotification, _ctx: TransportCall): Promise<void>;
|
|
24
26
|
close(): Promise<void>;
|
|
25
27
|
get diagnostics(): AsyncIterable<TransportDiagnostic>;
|
package/dist/testing/index.js
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Consumer test utilities: an in-memory Transport and a tiny scriptable MCP
|
|
3
|
+
* tool server. No processes, sockets, or fixtures required.
|
|
4
|
+
*
|
|
5
|
+
* const transport = createToolServer({
|
|
6
|
+
* echo: { handler: (args) => ({ content: [{ type: "text", text: String(args.message) }] }) },
|
|
7
|
+
* });
|
|
8
|
+
* const connection = await connect({ transport });
|
|
9
|
+
*/
|
|
10
|
+
import { NotificationQueue, } from "../core/index.js";
|
|
1
11
|
/** Throw from a handler to return a JSON-RPC error instead of a result. */
|
|
2
12
|
export class FakeJsonRpcError extends Error {
|
|
3
13
|
code;
|
|
@@ -9,20 +19,43 @@ export class FakeJsonRpcError extends Error {
|
|
|
9
19
|
this.name = "FakeJsonRpcError";
|
|
10
20
|
}
|
|
11
21
|
}
|
|
12
|
-
async function* emptyNotifications() {
|
|
13
|
-
// no notifications in the in-memory transport
|
|
14
|
-
}
|
|
15
22
|
async function* emptyDiagnostics() {
|
|
16
23
|
// no diagnostics in the in-memory transport
|
|
17
24
|
}
|
|
18
25
|
export class InMemoryTransport {
|
|
19
26
|
binding = "in-memory";
|
|
20
27
|
#handler;
|
|
28
|
+
#streams = new Map();
|
|
29
|
+
#closers = new Map();
|
|
21
30
|
constructor(handler) {
|
|
22
31
|
this.#handler = handler;
|
|
23
32
|
}
|
|
24
|
-
|
|
25
|
-
|
|
33
|
+
/** Push a notification into one active request stream (or all of them). */
|
|
34
|
+
push(notification, id) {
|
|
35
|
+
if (id !== undefined)
|
|
36
|
+
return this.#streams.get(id)?.push(notification) ?? false;
|
|
37
|
+
let delivered = false;
|
|
38
|
+
for (const queue of this.#streams.values())
|
|
39
|
+
delivered = queue.push(notification) || delivered;
|
|
40
|
+
return delivered;
|
|
41
|
+
}
|
|
42
|
+
start(msg, ctx) {
|
|
43
|
+
const notifications = new NotificationQueue();
|
|
44
|
+
this.#streams.set(msg.id, notifications);
|
|
45
|
+
const aborted = () => ({
|
|
46
|
+
kind: "failure",
|
|
47
|
+
failure: { kind: "closed", message: "aborted" },
|
|
48
|
+
delivery: "delivered-no-response",
|
|
49
|
+
});
|
|
50
|
+
const onAbort = new Promise((resolve) => {
|
|
51
|
+
const finish = () => resolve(aborted());
|
|
52
|
+
this.#closers.set(msg.id, finish);
|
|
53
|
+
if (ctx.signal.aborted)
|
|
54
|
+
finish();
|
|
55
|
+
else
|
|
56
|
+
ctx.signal.addEventListener("abort", finish, { once: true });
|
|
57
|
+
});
|
|
58
|
+
const work = (async () => {
|
|
26
59
|
try {
|
|
27
60
|
const result = await this.#handler(msg);
|
|
28
61
|
return {
|
|
@@ -51,13 +84,19 @@ export class InMemoryTransport {
|
|
|
51
84
|
};
|
|
52
85
|
}
|
|
53
86
|
})();
|
|
54
|
-
|
|
87
|
+
const done = Promise.race([work, onAbort]).finally(() => {
|
|
88
|
+
this.#streams.delete(msg.id);
|
|
89
|
+
this.#closers.delete(msg.id);
|
|
90
|
+
notifications.close();
|
|
91
|
+
});
|
|
92
|
+
return { notifications, done };
|
|
55
93
|
}
|
|
56
94
|
async notify(_msg, _ctx) {
|
|
57
95
|
// notifications are accepted and ignored
|
|
58
96
|
}
|
|
59
97
|
async close() {
|
|
60
|
-
|
|
98
|
+
for (const finish of [...this.#closers.values()])
|
|
99
|
+
finish();
|
|
61
100
|
}
|
|
62
101
|
get diagnostics() {
|
|
63
102
|
return emptyDiagnostics();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jangmanj/mcp-client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "MCP 2026-07-28 client library: core, transports, auth, ops, MRTR/cache, OTEL telemetry, and extensions.",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "MCP 2026-07-28 client library: push events/subscriptions, core, transports, auth, ops, MRTR/cache, OTEL telemetry, and extensions.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "JangMan-J",
|
|
7
7
|
"keywords": [
|