@cyanmycelium/mcp-broker-provider 0.1.1 → 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/README.md CHANGED
@@ -36,7 +36,7 @@ A multiplexed tunnel socket carries traffic for several providers at once, so ev
36
36
 
37
37
  Beyond the envelope it also covers:
38
38
 
39
- - `notifications/register`, sent as soon as the tunnel opens to claim a provider slot. Without it the broker only learns a provider name on its first real message, and an MCP client connecting in between is told the provider is not connected.
39
+ - `notifications/register`, sent as soon as the tunnel opens to claim a provider slot. Without it the broker only learns a provider name on its first real message, and an MCP client connecting in between is told the provider is not connected. It optionally carries `params: { aggregate: true }`, which also joins the broker's `_all` slot, see [Joining the `_all` aggregate slot](#joining-the-_all-aggregate-slot).
40
40
  - The tunnel error codes: `-32001` when the provider's credentials do not allow publishing on the requested slot, `-32000` when the slot is unavailable.
41
41
 
42
42
  When the broker refuses a slot, the transport surfaces it through `onError` rather than forwarding it. An id-less error frame handed to an MCP server would be classified as an unknown notification and dropped without a word, so the publisher would never learn it was refused.
@@ -45,10 +45,12 @@ Malformed frames decode to `undefined` rather than throwing: a tunnel socket is
45
45
 
46
46
  ## Transports
47
47
 
48
- | Transport | Use case |
49
- |---|---|
50
- | `MultiplexTransport` | Several servers published by one application. A single socket carries them all, keyed by slot name |
51
- | `DirectTransport` | One server, one socket, on `ws://<broker>/provider/<name>` |
48
+ | Transport | Endpoint it speaks to | Framing | Reconnects | Use case |
49
+ |---|---|---|---|---|
50
+ | `MultiplexTransport` | the shared multiplex base, `ws://<broker>/providers` | envelopes `{ provider, payload }` | yes, on the shared socket | Several servers published by one application. A single socket carries them all, keyed by slot name |
51
+ | `DirectTransport` | a slot-scoped path, `ws://<broker>/provider/<name>` | plain JSON-RPC frames | **no** | One server, one socket |
52
+
53
+ **The transport and the path are a pair, not a preference.** The broker decides framing from the endpoint the socket landed on, so a mismatch does not fail the handshake: the socket opens, looks healthy, and every frame is dropped on one side or the other with nothing logged by the broker. Both transports warn on the console when they spot the mismatch at connect time. And `ws://<broker>/providers/<name>`, the natural-looking blend of the two, is neither: the broker accepts it as a *client* connection on a slot of that name.
52
54
 
53
55
  ```ts
54
56
  import { McpServerBuilder } from "@cyanmycelium/mcp-core/server";
@@ -63,13 +65,57 @@ const server = new McpServerBuilder()
63
65
  await server.start();
64
66
  ```
65
67
 
66
- Transports created for the same tunnel URL share one WebSocket, whichever order they are opened in. Reconnection is handled by that shared socket, with exponential back-off and jitter; individual transports never reconnect on their own.
68
+ Transports created for the same tunnel URL share one WebSocket, whichever order they are opened in. Reconnection is handled by that shared socket, with exponential back-off and jitter, and individual transports never reconnect on their own. `DirectTransport` does not reconnect at all: when its socket closes it stays closed, and the application decides whether to call `connect()` again.
69
+
70
+ `server.start()` resolving means the transport reported itself open, not that the broker accepted the slot. A refusal arrives afterwards, and reaches you as an `onError` on the transport and a line on the console.
67
71
 
68
72
  `@cyanmycelium/mcp-core` is a peer dependency: the transports import its `IMessageTransport` type and nothing else at runtime, so your application keeps a single copy of it.
69
73
 
74
+ ## Joining the `_all` aggregate slot
75
+
76
+ The broker publishes an aggregate slot, `_all`, which exposes every opted-in provider's tools and prompts through one MCP connection. Membership is opt-in, because `_all` is a confidentiality boundary: a provider that does not ask for it stays reachable only on its own slot.
77
+
78
+ ```ts
79
+ import { DirectTransport, MultiplexTransport } from "@cyanmycelium/mcp-broker-provider";
80
+
81
+ // One socket per server
82
+ const direct = new DirectTransport("ws://localhost:3000/provider/scene-1", { aggregate: true });
83
+
84
+ // Or on the shared tunnel
85
+ const shared = MultiplexTransport.create("scene-1", "ws://localhost:3000/providers", { aggregate: true });
86
+ ```
87
+
88
+ Either form sends the registration notification with `params: { aggregate: true }` as its first frame:
89
+
90
+ ```json
91
+ { "jsonrpc": "2.0", "method": "notifications/register", "params": { "aggregate": true } }
92
+ ```
93
+
94
+ `DirectTransport` sends it verbatim, since the slot-scoped path carries plain JSON-RPC and the broker already knows the slot name from the URL. `MultiplexTransport` sends it inside the usual envelope, as `{ "provider": "scene-1", "payload": { ... } }`.
95
+
96
+ **Wire the message handler before you connect.** The broker runs `initialize` against a newly aggregated provider immediately, and a provider that does not answer is dropped from `_all` silently. Handing the transport to an MCP server does this for you, since the server assigns `onMessage` before calling `connect()`. Assigning it yourself, after connecting, loses the handshake and the provider never appears in the aggregate.
97
+
98
+ ## Diagnostics
99
+
100
+ This stack used to fail quietly. The transports now name what went wrong, on the console, because a browser-hosted provider has nowhere else to report:
101
+
102
+ - A frame written before the socket is open is queued (64 frames, oldest dropped first with a warning) and flushed on open, rather than discarded. That window covers the whole reconnect back-off, up to 30 seconds.
103
+ - A close with a code other than `1000` is reported through `onError` with the code and the broker's own reason, before `onClose`. A `1008` is a policy refusal: the slot is already connected, is reserved, or provider authentication rejected it.
104
+ - An incoming frame that is not an envelope, or one for a slot this socket does not publish, is logged with the likely cause and the fix. Repeats are sampled (the first in full, then one in fifty) so a mismatched tunnel cannot flood the console.
105
+
106
+ ## In a browser
107
+
108
+ The transports are written for the browser, but the npm path needs a bundler: neither this package nor `@cyanmycelium/mcp-core` ships a UMD build or declares a `browser` field, so a bare `<script>` tag will not load them. Any bundler works; there is nothing to configure beyond resolving the two packages.
109
+
110
+ Import only the isomorphic entry points. `@cyanmycelium/mcp-core/server` and `@cyanmycelium/mcp-core/client` run in a browser; `@cyanmycelium/mcp-core/node` does not, and pulling it in is the usual cause of a build that fails on Node built-ins.
111
+
112
+ If you would rather not add a build step, the broker ships a dependency-free ES module that speaks the same tunnel, `web/js/lib/broker-tunnel.js` (inside the `@cyanmycelium/mcp-broker` package, at `node/packages/broker/web` in the repository). It is the zero-build alternative, not a replacement: it carries no MCP server implementation.
113
+
114
+ One limitation worth knowing before you deploy: if the broker is configured with provider authentication, a browser-hosted provider cannot connect. The broker reads its credential from the `X-Provider-Token` or `Authorization` header of the upgrade request, and the browser `WebSocket` constructor cannot set headers, so the handshake is refused with a 401 the page sees only as a generic error. A browser provider needs provider auth off, or an authenticating reverse proxy in front of the broker.
115
+
70
116
  ## Status
71
117
 
72
- `0.1.0` ships the protocol module and both transports. They still exist in `@cyanmycelium/mcp-core@0.4.x` as well, and are removed there in `0.5.0`, migrate your imports before upgrading.
118
+ This package is the only home of the tunnel transports. They also shipped in `@cyanmycelium/mcp-core@0.4.x`, were removed there in `0.5.0`, and are absent from `0.7.x` and `1.x`, so migrate those imports here before upgrading `mcp-core`.
73
119
 
74
120
  The protocol is shared with the broker, and later with the consumer side. Import it through the `./protocol` subpath rather than the package root, so it can move to a package of its own one day without touching your call sites.
75
121
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,28 @@
1
- export { TUNNEL_REGISTER_METHOD, TunnelEnvelope, TunnelError, TunnelErrorCode, TunnelErrorCodes, decodeEnvelope, encodeEnvelope, encodeEnvelopeMessage, encodeErrorEnvelope, encodeRegisterEnvelope, envelopeFrame, tunnelErrorOf } from './protocol/index.js';
1
+ export { ITunnelRegisterOptions, TUNNEL_REGISTER_METHOD, TunnelEnvelope, TunnelError, TunnelErrorCode, TunnelErrorCodes, decodeEnvelope, encodeEnvelope, encodeEnvelopeMessage, encodeErrorEnvelope, encodeRegisterEnvelope, encodeRegisterFrame, envelopeFrame, tunnelErrorOf } from './protocol/index.js';
2
2
  import { IMessageTransport } from '@cyanmycelium/mcp-core';
3
3
 
4
+ /** Options accepted by {@link DirectTransport}. */
5
+ interface IDirectTransportOptions {
6
+ /**
7
+ * Join the broker's `_all` aggregate slot as well as this provider's own
8
+ * slot, by sending the registration notification
9
+ * `{"jsonrpc":"2.0","method":"notifications/register","params":{"aggregate":true}}`
10
+ * as the first frame on the socket.
11
+ *
12
+ * Opt-in on purpose: `_all` exposes this provider's tools and prompts to
13
+ * every client of the aggregate slot, so a provider that does not ask for it
14
+ * stays reachable only on its own slot.
15
+ *
16
+ * ORDERING: the broker runs `initialize` against a newly aggregated provider
17
+ * immediately, and drops it from `_all` without a word if the handshake times
18
+ * out. The frame therefore goes out on `open`, never from the constructor, so
19
+ * assign `onMessage` (or hand this transport to an MCP server, which assigns
20
+ * it for you) *before* calling {@link DirectTransport.connect}. Connecting
21
+ * first and wiring the handler afterwards loses the broker's `initialize` and
22
+ * the provider silently never appears in `_all`.
23
+ */
24
+ aggregate?: boolean;
25
+ }
4
26
  /**
5
27
  * 1:1 WebSocket transport, wraps a single `WebSocket` connection to a broker
6
28
  * provider slot, typically `ws://<broker>/provider/<name>`.
@@ -9,16 +31,25 @@ import { IMessageTransport } from '@cyanmycelium/mcp-core';
9
31
  * through the same broker, prefer {@link MultiplexTransport}, which shares a
10
32
  * single socket between them.
11
33
  *
34
+ * This transport does **not** reconnect: when the socket closes, it stays
35
+ * closed until the application calls {@link connect} again. Only
36
+ * {@link MultiplexTransport}'s shared socket reconnects on its own.
37
+ *
12
38
  * Call {@link connect} after setting the event callbacks to open the socket.
13
39
  */
14
40
  declare class DirectTransport implements IMessageTransport {
15
41
  private readonly _wsUrl;
42
+ private readonly _aggregate;
43
+ private readonly _pending;
44
+ /** Throttles the "wrote to a closed transport" line, which repeats per frame. */
45
+ private readonly _afterCloseNotice;
16
46
  private _ws;
47
+ private _closed;
17
48
  onMessage: ((data: string) => void) | null;
18
49
  onOpen: (() => void) | null;
19
50
  onClose: (() => void) | null;
20
51
  onError: ((error: Error) => void) | null;
21
- constructor(wsUrl: string);
52
+ constructor(wsUrl: string, options?: IDirectTransportOptions);
22
53
  get isOpen(): boolean;
23
54
  /**
24
55
  * Opens the WebSocket connection and wires its events to the transport
@@ -27,6 +58,23 @@ declare class DirectTransport implements IMessageTransport {
27
58
  connect(): void;
28
59
  send(data: string): void;
29
60
  close(): void;
61
+ /**
62
+ * Sends the registration notification when the caller asked for a specific
63
+ * aggregate membership. The slot name is not in the frame: on the
64
+ * slot-scoped path the broker takes it from the URL at connect time.
65
+ */
66
+ private _sendRegistration;
67
+ /** Writes out everything queued while the socket was connecting. */
68
+ private _flush;
69
+ /**
70
+ * Builds the message for a close worth reporting, or `undefined` when there
71
+ * is nothing to say.
72
+ *
73
+ * A code of 1000 is a normal close and stays silent. An environment that
74
+ * calls `onclose` with no event at all leaves nothing to distinguish a
75
+ * refusal from a clean shutdown, so that stays silent too.
76
+ */
77
+ private _closeError;
30
78
  }
31
79
 
32
80
  /**
@@ -42,26 +90,75 @@ declare class MultiplexSocket {
42
90
  private static readonly _instances;
43
91
  private readonly _wsUrl;
44
92
  private readonly _transports;
93
+ /** Aggregate opt-in per slot, absent when the caller did not express one. */
94
+ private readonly _aggregates;
95
+ /** Frames written while the socket is connecting, reconnecting or backing off. */
96
+ private readonly _pending;
45
97
  private _ws;
46
98
  private _reconnectAttempts;
99
+ private _reconnectTimer;
47
100
  private _stopped;
101
+ /** Set once the last transport left and this instance gave up its URL. */
102
+ private _dead;
103
+ /** The URL guard is about the URL, not the socket, so it is said once. */
104
+ private _pathWarned;
105
+ /** Throttles the "wrote to a closed tunnel" line, which repeats per frame. */
106
+ private readonly _afterStopNotice;
48
107
  private constructor();
49
108
  /** Returns (or creates) the singleton socket for a given tunnel URL. */
50
109
  static getOrCreate(wsUrl: string): MultiplexSocket;
51
110
  get isOpen(): boolean;
52
- register(name: string, transport: MultiplexTransport): void;
111
+ register(name: string, transport: MultiplexTransport, aggregate?: boolean): void;
53
112
  unregister(name: string): void;
113
+ /**
114
+ * Brings a torn-down instance back into service when a transport that
115
+ * captured it is reactivated.
116
+ *
117
+ * Reclaims the URL when nothing else holds it. When another instance already
118
+ * does, the two would race for the same slot names, so this says exactly that
119
+ * rather than letting the loser fail with a refusal nobody reads.
120
+ */
121
+ private _revive;
54
122
  /**
55
123
  * Claims the slot for `name` so the broker eagerly creates its provider
56
124
  * state before any MCP client connects. Without it the broker only learns
57
125
  * about a provider on its first real message, and a client connecting in
58
126
  * between is told the provider is not connected.
127
+ *
128
+ * Carries the aggregate opt-in when the caller expressed one, which is why
129
+ * it must go out before any traffic: the broker runs `initialize` against a
130
+ * newly aggregated provider straight away.
59
131
  */
60
132
  private _announceProvider;
61
133
  send(provider: string, data: string): void;
62
134
  private _connect;
135
+ /** Writes out everything queued while the socket was down. */
136
+ private _flush;
63
137
  private _routeIncoming;
64
138
  private _scheduleReconnect;
139
+ /** Disarms a pending reconnect, so nothing opens a socket behind our back. */
140
+ private _cancelReconnect;
141
+ }
142
+ /** Options accepted by {@link MultiplexTransport.create}. */
143
+ interface IMultiplexTransportOptions {
144
+ /**
145
+ * Join the broker's `_all` aggregate slot as well as this provider's own
146
+ * slot, by carrying `params: { aggregate: true }` on the registration
147
+ * notification the shared socket sends when it claims the slot.
148
+ *
149
+ * Opt-in on purpose: `_all` exposes this provider's tools and prompts to
150
+ * every client of the aggregate slot, so a provider that does not ask for it
151
+ * stays reachable only on its own slot.
152
+ *
153
+ * ORDERING: the broker runs `initialize` against a newly aggregated provider
154
+ * immediately, and drops it from `_all` without a word if the handshake times
155
+ * out. The registration goes out from {@link MultiplexTransport.connect}, so
156
+ * assign `onMessage` (or hand this transport to an MCP server, which assigns
157
+ * it for you) *before* connecting. Connecting first and wiring the handler
158
+ * afterwards loses the broker's `initialize`, and the provider silently never
159
+ * appears in `_all`.
160
+ */
161
+ aggregate?: boolean;
65
162
  }
66
163
  /**
67
164
  * A transport that multiplexes multiple MCP servers over a single shared
@@ -77,19 +174,24 @@ declare class MultiplexSocket {
77
174
  declare class MultiplexTransport implements IMessageTransport {
78
175
  private readonly _name;
79
176
  private readonly _socket;
177
+ private readonly _aggregate;
80
178
  private _registered;
81
179
  onMessage: ((data: string) => void) | null;
82
180
  onOpen: (() => void) | null;
83
181
  onClose: (() => void) | null;
84
182
  onError: ((error: Error) => void) | null;
85
- constructor(name: string, socket: MultiplexSocket);
183
+ constructor(name: string, socket: MultiplexSocket, options?: IMultiplexTransportOptions);
86
184
  /**
87
185
  * Convenience factory: creates a {@link MultiplexTransport} backed by a
88
186
  * shared {@link MultiplexSocket} for the given tunnel URL.
89
187
  *
90
188
  * Transports targeting the same `wsUrl` automatically share one WebSocket.
189
+ *
190
+ * @param wsUrl The broker's shared multiplex base, `ws://<broker>/providers`.
191
+ * A slot-scoped `/provider/<name>` URL belongs to
192
+ * {@link DirectTransport} and is warned about on connect.
91
193
  */
92
- static create(name: string, wsUrl: string): MultiplexTransport;
194
+ static create(name: string, wsUrl: string, options?: IMultiplexTransportOptions): MultiplexTransport;
93
195
  get isOpen(): boolean;
94
196
  /**
95
197
  * Registers this transport with the shared socket.
@@ -109,4 +211,4 @@ declare class MultiplexTransport implements IMessageTransport {
109
211
  close(): void;
110
212
  }
111
213
 
112
- export { DirectTransport, MultiplexTransport };
214
+ export { DirectTransport, type IDirectTransportOptions, type IMultiplexTransportOptions, MultiplexTransport };