@jangmanj/mcp 0.0.0-stage → 0.13.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.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +337 -2
  3. package/adapters/pi/bridge.ts +721 -0
  4. package/adapters/pi/config.ts +364 -0
  5. package/adapters/pi/content-offloaded.ts +53 -0
  6. package/adapters/pi/content.ts +78 -0
  7. package/adapters/pi/diagnostics.ts +26 -0
  8. package/adapters/pi/elicitation.ts +133 -0
  9. package/adapters/pi/index.ts +210 -0
  10. package/adapters/pi/oauth.ts +219 -0
  11. package/adapters/pi/pi-api.ts +133 -0
  12. package/adapters/pi/resources.ts +177 -0
  13. package/adapters/pi/result-files.ts +32 -0
  14. package/adapters/pi/tasks.ts +49 -0
  15. package/dist/auth/client-credentials.d.ts +65 -0
  16. package/dist/auth/client-credentials.js +388 -0
  17. package/dist/auth/discovery.d.ts +28 -0
  18. package/dist/auth/discovery.js +125 -0
  19. package/dist/auth/index.d.ts +9 -0
  20. package/dist/auth/index.js +5 -0
  21. package/dist/auth/node-ports.d.ts +8 -0
  22. package/dist/auth/node-ports.js +67 -0
  23. package/dist/auth/protocol.d.ts +12 -0
  24. package/dist/auth/protocol.js +39 -0
  25. package/dist/auth/provider.d.ts +114 -0
  26. package/dist/auth/provider.js +407 -0
  27. package/dist/auth/safe-fetch.d.ts +14 -0
  28. package/dist/auth/safe-fetch.js +130 -0
  29. package/dist/client/events.d.ts +33 -0
  30. package/dist/client/events.js +84 -0
  31. package/dist/client/index.d.ts +65 -0
  32. package/dist/client/index.js +128 -0
  33. package/dist/core/auth-types.d.ts +36 -0
  34. package/dist/core/auth-types.js +11 -0
  35. package/dist/core/bearer-challenge.d.ts +3 -0
  36. package/dist/core/bearer-challenge.js +63 -0
  37. package/dist/core/cancel.d.ts +70 -0
  38. package/dist/core/cancel.js +177 -0
  39. package/dist/core/extension.d.ts +33 -0
  40. package/dist/core/extension.js +145 -0
  41. package/dist/core/index.d.ts +73 -0
  42. package/dist/core/index.js +129 -0
  43. package/dist/core/jsonrpc.d.ts +49 -0
  44. package/dist/core/jsonrpc.js +80 -0
  45. package/dist/core/lifecycle.d.ts +128 -0
  46. package/dist/core/lifecycle.js +251 -0
  47. package/dist/core/outcome.d.ts +98 -0
  48. package/dist/core/outcome.js +32 -0
  49. package/dist/core/ports.d.ts +144 -0
  50. package/dist/core/ports.js +67 -0
  51. package/dist/core/progress.d.ts +81 -0
  52. package/dist/core/progress.js +205 -0
  53. package/dist/core/request.d.ts +108 -0
  54. package/dist/core/request.js +578 -0
  55. package/dist/core/schema/index.d.ts +1 -0
  56. package/dist/core/schema/index.js +1 -0
  57. package/dist/core/schema/types.d.ts +4154 -0
  58. package/dist/core/schema/types.js +6 -0
  59. package/dist/core/telemetry.d.ts +25 -0
  60. package/dist/core/telemetry.js +37 -0
  61. package/dist/ext-events/index.d.ts +79 -0
  62. package/dist/ext-events/index.js +218 -0
  63. package/dist/ext-events/model.d.ts +10 -0
  64. package/dist/ext-events/model.js +6 -0
  65. package/dist/ext-events/push.d.ts +18 -0
  66. package/dist/ext-events/push.js +101 -0
  67. package/dist/ext-events/stream.d.ts +6 -0
  68. package/dist/ext-events/stream.js +60 -0
  69. package/dist/ext-server-cards/index.d.ts +107 -0
  70. package/dist/ext-server-cards/index.js +249 -0
  71. package/dist/ext-skills/cache.d.ts +20 -0
  72. package/dist/ext-skills/cache.js +101 -0
  73. package/dist/ext-skills/client.d.ts +82 -0
  74. package/dist/ext-skills/client.js +292 -0
  75. package/dist/ext-skills/extension.d.ts +16 -0
  76. package/dist/ext-skills/extension.js +29 -0
  77. package/dist/ext-skills/index.d.ts +4 -0
  78. package/dist/ext-skills/index.js +4 -0
  79. package/dist/ext-skills/verify.d.ts +57 -0
  80. package/dist/ext-skills/verify.js +168 -0
  81. package/dist/index.d.ts +1 -0
  82. package/dist/index.js +1 -0
  83. package/dist/mrtr-cache/cache.d.ts +51 -0
  84. package/dist/mrtr-cache/cache.js +295 -0
  85. package/dist/mrtr-cache/client.d.ts +24 -0
  86. package/dist/mrtr-cache/client.js +44 -0
  87. package/dist/mrtr-cache/common.d.ts +10 -0
  88. package/dist/mrtr-cache/common.js +9 -0
  89. package/dist/mrtr-cache/elicitation.d.ts +42 -0
  90. package/dist/mrtr-cache/elicitation.js +221 -0
  91. package/dist/mrtr-cache/index.d.ts +9 -0
  92. package/dist/mrtr-cache/index.js +8 -0
  93. package/dist/mrtr-cache/mrtr.d.ts +37 -0
  94. package/dist/mrtr-cache/mrtr.js +250 -0
  95. package/dist/mrtr-cache/refresh.d.ts +8 -0
  96. package/dist/mrtr-cache/refresh.js +30 -0
  97. package/dist/mrtr-cache/stream.d.ts +5 -0
  98. package/dist/mrtr-cache/stream.js +18 -0
  99. package/dist/mrtr-cache/subscriptions.d.ts +96 -0
  100. package/dist/mrtr-cache/subscriptions.js +288 -0
  101. package/dist/mrtr-cache/tasks.d.ts +103 -0
  102. package/dist/mrtr-cache/tasks.js +444 -0
  103. package/dist/ops/common.d.ts +18 -0
  104. package/dist/ops/common.js +39 -0
  105. package/dist/ops/completion.d.ts +44 -0
  106. package/dist/ops/completion.js +65 -0
  107. package/dist/ops/content.d.ts +40 -0
  108. package/dist/ops/content.js +117 -0
  109. package/dist/ops/icons.d.ts +29 -0
  110. package/dist/ops/icons.js +82 -0
  111. package/dist/ops/index.d.ts +30 -0
  112. package/dist/ops/index.js +28 -0
  113. package/dist/ops/pagination.d.ts +32 -0
  114. package/dist/ops/pagination.js +65 -0
  115. package/dist/ops/prompts.d.ts +32 -0
  116. package/dist/ops/prompts.js +68 -0
  117. package/dist/ops/resources.d.ts +42 -0
  118. package/dist/ops/resources.js +104 -0
  119. package/dist/ops/schema.d.ts +39 -0
  120. package/dist/ops/schema.js +163 -0
  121. package/dist/ops/tools.d.ts +88 -0
  122. package/dist/ops/tools.js +169 -0
  123. package/dist/ops/uri-template.d.ts +3 -0
  124. package/dist/ops/uri-template.js +101 -0
  125. package/dist/server/index.d.ts +5 -0
  126. package/dist/server/index.js +5 -0
  127. package/dist/server/mrtr.d.ts +38 -0
  128. package/dist/server/mrtr.js +99 -0
  129. package/dist/server/protocol.d.ts +21 -0
  130. package/dist/server/protocol.js +49 -0
  131. package/dist/server/server.d.ts +105 -0
  132. package/dist/server/server.js +383 -0
  133. package/dist/server/skills.d.ts +45 -0
  134. package/dist/server/skills.js +73 -0
  135. package/dist/server/uri.d.ts +2 -0
  136. package/dist/server/uri.js +27 -0
  137. package/dist/server-events/bus.d.ts +40 -0
  138. package/dist/server-events/bus.js +84 -0
  139. package/dist/server-events/events.d.ts +36 -0
  140. package/dist/server-events/events.js +108 -0
  141. package/dist/server-events/index.d.ts +6 -0
  142. package/dist/server-events/index.js +6 -0
  143. package/dist/server-events/notices.d.ts +24 -0
  144. package/dist/server-events/notices.js +31 -0
  145. package/dist/server-events/subscriptions.d.ts +33 -0
  146. package/dist/server-events/subscriptions.js +77 -0
  147. package/dist/server-events/tasks.d.ts +43 -0
  148. package/dist/server-events/tasks.js +101 -0
  149. package/dist/server-events/types.d.ts +25 -0
  150. package/dist/server-events/types.js +34 -0
  151. package/dist/server-transport/auth.d.ts +20 -0
  152. package/dist/server-transport/auth.js +29 -0
  153. package/dist/server-transport/channel.d.ts +17 -0
  154. package/dist/server-transport/channel.js +47 -0
  155. package/dist/server-transport/http.d.ts +19 -0
  156. package/dist/server-transport/http.js +209 -0
  157. package/dist/server-transport/index.d.ts +4 -0
  158. package/dist/server-transport/index.js +4 -0
  159. package/dist/server-transport/stdio.d.ts +16 -0
  160. package/dist/server-transport/stdio.js +83 -0
  161. package/dist/telemetry-otel/index.d.ts +64 -0
  162. package/dist/telemetry-otel/index.js +196 -0
  163. package/dist/testing/index.d.ts +36 -0
  164. package/dist/testing/index.js +140 -0
  165. package/dist/transport-http/headers.d.ts +24 -0
  166. package/dist/transport-http/headers.js +140 -0
  167. package/dist/transport-http/index.d.ts +3 -0
  168. package/dist/transport-http/index.js +3 -0
  169. package/dist/transport-http/sse.d.ts +33 -0
  170. package/dist/transport-http/sse.js +152 -0
  171. package/dist/transport-http/transport.d.ts +66 -0
  172. package/dist/transport-http/transport.js +854 -0
  173. package/dist/transport-stdio/framing.d.ts +50 -0
  174. package/dist/transport-stdio/framing.js +154 -0
  175. package/dist/transport-stdio/index.d.ts +4 -0
  176. package/dist/transport-stdio/index.js +4 -0
  177. package/dist/transport-stdio/stderr-ring.d.ts +27 -0
  178. package/dist/transport-stdio/stderr-ring.js +105 -0
  179. package/dist/transport-stdio/transport.d.ts +142 -0
  180. package/dist/transport-stdio/transport.js +743 -0
  181. package/dist/transport-stdio/write-queue.d.ts +48 -0
  182. package/dist/transport-stdio/write-queue.js +147 -0
  183. package/package.json +134 -4
@@ -0,0 +1,6 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * AUTO-GENERATED from packages/core/src/schema/schema.json (MCP 2026-07-28, tag 2bbace55).
4
+ * Do not edit manually; regenerate with `npm run gen:schema`.
5
+ */
6
+ export {};
@@ -0,0 +1,25 @@
1
+ export interface W3cTraceContext {
2
+ readonly traceparent: string;
3
+ readonly tracestate?: string;
4
+ readonly baggage?: string;
5
+ }
6
+ export interface TelemetrySpan {
7
+ setAttribute(key: string, value: string | number | boolean): void;
8
+ recordException(err: unknown): void;
9
+ end(status: {
10
+ ok: boolean;
11
+ errorKind?: string;
12
+ }): void;
13
+ }
14
+ export interface TelemetryPort {
15
+ activeContext(): W3cTraceContext | undefined;
16
+ startClientSpan(name: string, attrs: Record<string, string | number | boolean>): TelemetrySpan;
17
+ }
18
+ export declare function validateTraceContext(ctx: W3cTraceContext): {
19
+ valid: W3cTraceContext;
20
+ diagnostics: string[];
21
+ } | {
22
+ valid: undefined;
23
+ diagnostics: string[];
24
+ };
25
+ export declare const noopTelemetry: TelemetryPort;
@@ -0,0 +1,37 @@
1
+ const TRACEPARENT_RE = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/;
2
+ const ALL_ZERO_TRACE = "00000000000000000000000000000000";
3
+ const ALL_ZERO_SPAN = "0000000000000000";
4
+ export function validateTraceContext(ctx) {
5
+ const diagnostics = [];
6
+ const m = TRACEPARENT_RE.exec(ctx.traceparent);
7
+ if (!m || m[1] === ALL_ZERO_TRACE || m[2] === ALL_ZERO_SPAN) {
8
+ diagnostics.push("invalid-traceparent");
9
+ return { valid: undefined, diagnostics };
10
+ }
11
+ let tracestate = ctx.tracestate;
12
+ if (tracestate !== undefined && tracestate.length > 512) {
13
+ diagnostics.push("tracestate-truncated-or-dropped");
14
+ tracestate = undefined;
15
+ }
16
+ let baggage = ctx.baggage;
17
+ if (baggage !== undefined && baggage.length > 8192) {
18
+ diagnostics.push("baggage-dropped");
19
+ baggage = undefined;
20
+ }
21
+ return {
22
+ valid: {
23
+ traceparent: ctx.traceparent,
24
+ ...(tracestate !== undefined ? { tracestate } : {}),
25
+ ...(baggage !== undefined ? { baggage } : {}),
26
+ },
27
+ diagnostics,
28
+ };
29
+ }
30
+ export const noopTelemetry = {
31
+ activeContext: () => undefined,
32
+ startClientSpan: () => ({
33
+ setAttribute() { },
34
+ recordException() { },
35
+ end() { },
36
+ }),
37
+ };
@@ -0,0 +1,79 @@
1
+ import { type Clock, type ClientRequest, type Extension, type ExtensionCapabilities, type RequestOptions, type StreamHandle } from "../core/index.js";
2
+ import { type Obj, type Occurrence } from "./model.js";
3
+ import { type PushMessage } from "./push.js";
4
+ export type { PushMessage } from "./push.js";
5
+ export type { Obj, Occurrence } from "./model.js";
6
+ export { openEventsStream } from "./stream.js";
7
+ export declare const EVENTS_EXTENSION_ID = "io.modelcontextprotocol/events";
8
+ export declare class EventsExtension implements Extension {
9
+ private readonly opts;
10
+ readonly id = "io.modelcontextprotocol/events";
11
+ constructor(opts?: {
12
+ enabled?: boolean;
13
+ healthy?: boolean;
14
+ });
15
+ health(): "unavailable" | "working";
16
+ settings(): {} | undefined;
17
+ capabilities(isWorking?: boolean): ExtensionCapabilities;
18
+ }
19
+ export declare const createEventsExtension: (opts?: {
20
+ enabled?: boolean;
21
+ healthy?: boolean;
22
+ }) => EventsExtension;
23
+ export interface WebhookEndpointPort {
24
+ readonly url: string;
25
+ readonly secret: string;
26
+ isReady(): boolean;
27
+ }
28
+ export interface EventsOptions {
29
+ extension: EventsExtension;
30
+ peerCapabilities(): unknown;
31
+ request(method: string, params: Obj, options: RequestOptions): ClientRequest<Obj>;
32
+ openStream?(method: "events/stream", params: Obj, options: RequestOptions): StreamHandle;
33
+ clock?: Clock;
34
+ jitterMs?: () => number;
35
+ enableWebhook?: boolean;
36
+ endpoint?: WebhookEndpointPort;
37
+ onRefreshError?: (error: unknown) => void;
38
+ }
39
+ export declare class EventsClient {
40
+ private readonly opts;
41
+ readonly deliverySemantics: "at-least-once";
42
+ private readonly subs;
43
+ private seq;
44
+ private disposed;
45
+ private readonly clock;
46
+ constructor(opts: EventsOptions);
47
+ private capability;
48
+ private gate;
49
+ expectsListChanged(): boolean;
50
+ private rpc;
51
+ list(page?: string): Promise<Obj>;
52
+ subscription(name: string, args?: Obj): number;
53
+ subscriptions(): {
54
+ id: number;
55
+ name: string;
56
+ cursor: string | null;
57
+ }[];
58
+ private state;
59
+ private params;
60
+ poll(id: number): Promise<{
61
+ events: Occurrence[];
62
+ cursor: string | null;
63
+ truncated: boolean;
64
+ gap: boolean;
65
+ nextDelayMs: number;
66
+ }>;
67
+ push(id: number, deliver: (message: PushMessage) => void): {
68
+ close: () => void;
69
+ };
70
+ private endpoint;
71
+ subscribe(id: number): Promise<{
72
+ cursor: string | null;
73
+ gap: boolean;
74
+ truncated: boolean;
75
+ }>;
76
+ unsubscribe(id: number): Promise<void>;
77
+ forget(id: number): void;
78
+ dispose(): void;
79
+ }
@@ -0,0 +1,218 @@
1
+ import { systemClock, } from "../core/index.js";
2
+ import { object, cursor } from "./model.js";
3
+ import { watchStream } from "./push.js";
4
+ export { openEventsStream } from "./stream.js";
5
+ export const EVENTS_EXTENSION_ID = "io.modelcontextprotocol/events";
6
+ export class EventsExtension {
7
+ opts;
8
+ id = EVENTS_EXTENSION_ID;
9
+ constructor(opts = {}) {
10
+ this.opts = opts;
11
+ }
12
+ health() {
13
+ return this.opts.enabled === true && this.opts.healthy !== false
14
+ ? "working"
15
+ : "unavailable";
16
+ }
17
+ settings() {
18
+ return this.health() === "working" ? {} : undefined;
19
+ }
20
+ capabilities(isWorking = true) {
21
+ return isWorking && this.settings()
22
+ ? { extensions: { [this.id]: {} } }
23
+ : {};
24
+ }
25
+ }
26
+ export const createEventsExtension = (opts) => new EventsExtension(opts);
27
+ function occurrence(s, p) {
28
+ if (!object(p) ||
29
+ typeof p.eventId !== "string" ||
30
+ p.name !== s.name ||
31
+ typeof p.timestamp !== "string" ||
32
+ !Object.hasOwn(p, "data"))
33
+ throw new Error("Invalid Events occurrence");
34
+ const e = {
35
+ eventId: p.eventId,
36
+ name: s.name,
37
+ timestamp: p.timestamp,
38
+ data: p.data,
39
+ cursor: cursor(p.cursor),
40
+ };
41
+ if (s.seen.has(e.eventId))
42
+ return;
43
+ s.seen.add(e.eventId);
44
+ return e;
45
+ }
46
+ export class EventsClient {
47
+ opts;
48
+ deliverySemantics = "at-least-once";
49
+ subs = new Map();
50
+ seq = 0;
51
+ disposed = false;
52
+ clock;
53
+ constructor(opts) {
54
+ this.opts = opts;
55
+ this.clock = opts.clock ?? systemClock;
56
+ }
57
+ capability() {
58
+ const caps = this.opts.peerCapabilities();
59
+ const ext = object(caps) &&
60
+ object(caps.extensions) &&
61
+ caps.extensions[EVENTS_EXTENSION_ID];
62
+ return !this.disposed &&
63
+ this.opts.extension.health() === "working" &&
64
+ object(ext) &&
65
+ (ext.revision === undefined || ext.revision === "2026-07-28")
66
+ ? ext
67
+ : undefined;
68
+ }
69
+ gate() {
70
+ if (!this.capability())
71
+ throw new Error("Events unavailable");
72
+ }
73
+ expectsListChanged() {
74
+ return this.capability()?.listChanged === true;
75
+ }
76
+ async rpc(method, params) {
77
+ this.gate();
78
+ const out = await this.opts.request(method, params, typeof params.name === "string"
79
+ ? { headersHint: { name: params.name } }
80
+ : {});
81
+ if (out.kind !== "ok")
82
+ throw new Error(`Events request failed: ${out.kind}`, { cause: out });
83
+ return out.value;
84
+ }
85
+ list(page) {
86
+ return this.rpc("events/list", page === undefined ? {} : { cursor: page });
87
+ }
88
+ subscription(name, args = {}) {
89
+ if (this.disposed)
90
+ throw new Error("Events client disposed");
91
+ const id = ++this.seq;
92
+ this.subs.set(id, {
93
+ name,
94
+ args: structuredClone(args),
95
+ cursor: null,
96
+ seen: new Set(),
97
+ generation: 0,
98
+ });
99
+ return id;
100
+ }
101
+ subscriptions() {
102
+ return Array.from(this.subs, ([id, s]) => ({
103
+ id,
104
+ name: s.name,
105
+ cursor: s.cursor,
106
+ }));
107
+ }
108
+ state(id) {
109
+ const s = this.subs.get(id);
110
+ if (!s)
111
+ throw new Error("Unknown local subscription");
112
+ return s;
113
+ }
114
+ params(s) {
115
+ return {
116
+ name: s.name,
117
+ arguments: structuredClone(s.args),
118
+ cursor: s.cursor,
119
+ };
120
+ }
121
+ async poll(id) {
122
+ const s = this.state(id), r = await this.rpc("events/poll", this.params(s));
123
+ if (!Array.isArray(r.events))
124
+ throw new Error("Invalid Events poll result");
125
+ const events = r.events
126
+ .map((p) => occurrence(s, p))
127
+ .filter((e) => e !== undefined);
128
+ s.cursor = cursor(r.cursor);
129
+ return {
130
+ events,
131
+ cursor: s.cursor,
132
+ truncated: r.truncated === true,
133
+ gap: r.truncated === true,
134
+ nextDelayMs: r.hasMore === true
135
+ ? 0
136
+ : Math.max(1000, typeof r.nextPollMs === "number" ? r.nextPollMs : 1000),
137
+ };
138
+ }
139
+ push(id, deliver) {
140
+ this.gate();
141
+ const s = this.state(id), open = this.opts.openStream;
142
+ if (!open)
143
+ throw new Error("Live Events stream port required");
144
+ s.stop?.();
145
+ const stop = watchStream(() => {
146
+ this.gate();
147
+ return open("events/stream", this.params(s), {
148
+ headersHint: { name: s.name },
149
+ });
150
+ }, this.clock, this.opts.jitterMs ?? (() => 50 + Math.floor(Math.random() * 200)), (p) => {
151
+ s.cursor = cursor(p);
152
+ }, (p) => occurrence(s, p), deliver);
153
+ s.stop = stop;
154
+ return { close: stop };
155
+ }
156
+ endpoint() {
157
+ const ep = this.opts.endpoint;
158
+ if (this.opts.enableWebhook !== true || !ep?.isReady())
159
+ throw new Error("Webhook unavailable");
160
+ if (!ep.url.startsWith("https://") ||
161
+ new URL(ep.url).protocol !== "https:" ||
162
+ !/^whsec_(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)$/.test(ep.secret))
163
+ throw new Error("Invalid webhook endpoint");
164
+ return ep;
165
+ }
166
+ async subscribe(id) {
167
+ const ep = this.endpoint(), s = this.state(id);
168
+ if (s.url && s.url !== ep.url)
169
+ throw new Error("Webhook identity changed");
170
+ const generation = ++s.generation;
171
+ s.refresh?.();
172
+ const r = await this.rpc("events/subscribe", {
173
+ ...this.params(s),
174
+ delivery: { url: ep.url, secret: ep.secret },
175
+ });
176
+ if (s.generation === generation) {
177
+ s.url = ep.url;
178
+ s.cursor = cursor(r.cursor);
179
+ if (r.refreshBefore != null) {
180
+ const remaining = Date.parse(String(r.refreshBefore)) - this.clock.now();
181
+ if (!(remaining > 0))
182
+ throw new Error("Invalid webhook refresh deadline");
183
+ s.refresh = this.clock.setTimeout(() => {
184
+ void this.subscribe(id).catch((e) => this.opts.onRefreshError?.(e));
185
+ }, Math.min(2 ** 31 - 1, Math.floor(remaining * 0.8)));
186
+ }
187
+ }
188
+ return {
189
+ ...r,
190
+ cursor: cursor(r.cursor),
191
+ gap: r.truncated === true,
192
+ truncated: r.truncated === true,
193
+ };
194
+ }
195
+ async unsubscribe(id) {
196
+ const s = this.state(id), ep = this.endpoint();
197
+ ++s.generation;
198
+ s.refresh?.();
199
+ await this.rpc("events/unsubscribe", {
200
+ name: s.name,
201
+ arguments: structuredClone(s.args),
202
+ delivery: { url: s.url ?? ep.url },
203
+ });
204
+ this.forget(id);
205
+ }
206
+ forget(id) {
207
+ const s = this.state(id);
208
+ ++s.generation;
209
+ s.refresh?.();
210
+ s.stop?.();
211
+ this.subs.delete(id);
212
+ }
213
+ dispose() {
214
+ this.disposed = true;
215
+ for (const id of this.subs.keys())
216
+ this.forget(id);
217
+ }
218
+ }
@@ -0,0 +1,10 @@
1
+ export type Obj = Record<string, unknown>;
2
+ export declare const object: (x: unknown) => x is Obj;
3
+ export declare function cursor(x: unknown): string | null;
4
+ export interface Occurrence {
5
+ eventId: string;
6
+ name: string;
7
+ timestamp: string;
8
+ data: unknown;
9
+ cursor: string | null;
10
+ }
@@ -0,0 +1,6 @@
1
+ export const object = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
2
+ export function cursor(x) {
3
+ if (x == null || typeof x === "string")
4
+ return x ?? null;
5
+ throw new Error("Invalid Events cursor");
6
+ }
@@ -0,0 +1,18 @@
1
+ import type { Clock, StreamHandle, RequestId } from "../core/index.js";
2
+ import { type Occurrence } from "./model.js";
3
+ export type PushMessage = {
4
+ type: "active";
5
+ cursor: string | null;
6
+ truncated: boolean;
7
+ gap: boolean;
8
+ heartbeatMs: number;
9
+ } | {
10
+ type: "event";
11
+ subscriptionId: RequestId;
12
+ event: Occurrence;
13
+ } | {
14
+ type: "terminated" | "error";
15
+ error: unknown;
16
+ };
17
+ /** A stream owns one watchdog. Retiring its handle invalidates all late frames and completions. */
18
+ export declare function watchStream(open: () => StreamHandle, clock: Clock, jitter: () => number, advance: (c: unknown) => void, accept: (p: unknown) => Occurrence | undefined, deliver: (m: PushMessage) => void): () => void;
@@ -0,0 +1,101 @@
1
+ import { object, cursor } from "./model.js";
2
+ /** A stream owns one watchdog. Retiring its handle invalidates all late frames and completions. */
3
+ export function watchStream(open, clock, jitter, advance, accept, deliver) {
4
+ let live, stopped = false, timer;
5
+ const retire = () => {
6
+ timer?.();
7
+ const old = live;
8
+ live = undefined;
9
+ old?.close();
10
+ };
11
+ const stop = () => {
12
+ stopped = true;
13
+ retire();
14
+ };
15
+ function connect() {
16
+ if (stopped)
17
+ return;
18
+ let stream;
19
+ try {
20
+ stream = open();
21
+ }
22
+ catch (error) {
23
+ stop();
24
+ deliver({ type: "error", error });
25
+ return;
26
+ }
27
+ live = stream;
28
+ const reconnect = () => {
29
+ if (live !== stream)
30
+ return;
31
+ retire();
32
+ timer = clock.setTimeout(connect, Math.max(1, jitter()));
33
+ };
34
+ let heartbeat = 0;
35
+ const arm = () => {
36
+ timer?.();
37
+ timer = clock.setTimeout(reconnect, heartbeat * 2);
38
+ };
39
+ void stream.done.then(reconnect, reconnect);
40
+ void (async () => {
41
+ for await (const n of stream) {
42
+ if (live !== stream)
43
+ return;
44
+ const p = n.params ?? {}, id = object(p._meta)
45
+ ? p._meta["io.modelcontextprotocol/subscriptionId"]
46
+ : p.subscriptionId;
47
+ if (id !== stream.id)
48
+ continue;
49
+ if (!heartbeat && n.method !== "notifications/events/active")
50
+ throw new Error("Expected active as first Events notification");
51
+ switch (n.method) {
52
+ case "notifications/events/active": {
53
+ if (typeof p.heartbeatMs !== "number" ||
54
+ !Number.isFinite(p.heartbeatMs) ||
55
+ p.heartbeatMs <= 0)
56
+ throw new Error("Invalid Events heartbeat grant");
57
+ heartbeat = p.heartbeatMs;
58
+ advance(p.cursor);
59
+ arm();
60
+ deliver({
61
+ type: "active",
62
+ cursor: cursor(p.cursor),
63
+ gap: p.truncated === true,
64
+ truncated: p.truncated === true,
65
+ heartbeatMs: heartbeat,
66
+ });
67
+ break;
68
+ }
69
+ case "notifications/events/event": {
70
+ const event = accept(p);
71
+ advance(p.cursor);
72
+ arm();
73
+ if (event)
74
+ deliver({ type: "event", event, subscriptionId: stream.id });
75
+ break;
76
+ }
77
+ case "notifications/events/heartbeat":
78
+ advance(p.cursor);
79
+ arm();
80
+ break;
81
+ case "notifications/events/error":
82
+ arm();
83
+ deliver({ type: "error", error: p.error });
84
+ break;
85
+ case "notifications/events/terminated":
86
+ stop();
87
+ deliver({ type: "terminated", error: p.error });
88
+ return;
89
+ }
90
+ }
91
+ reconnect();
92
+ })().catch((error) => {
93
+ if (live === stream) {
94
+ deliver({ type: "error", error });
95
+ reconnect();
96
+ }
97
+ });
98
+ }
99
+ connect();
100
+ return stop;
101
+ }
@@ -0,0 +1,6 @@
1
+ import { type JsonRpcNotification, type IdGenerator, type RequestId, type Transport } from "../core/index.js";
2
+ import type { EventsOptions, Obj } from "./index.js";
3
+ /** Use the host's authenticated transport, shared request-id allocator and per-request metadata. */
4
+ export declare function openEventsStream(transport: Transport & {
5
+ registerSubscription?(id: RequestId, fn: (n: JsonRpcNotification) => void): () => void;
6
+ }, nextId: IdGenerator, meta: () => Obj): NonNullable<EventsOptions["openStream"]>;
@@ -0,0 +1,60 @@
1
+ import { NotificationQueue, } from "../core/index.js";
2
+ /** Use the host's authenticated transport, shared request-id allocator and per-request metadata. */
3
+ export function openEventsStream(transport, nextId, meta) {
4
+ return (method, params, opts) => {
5
+ const id = nextId(), controller = new AbortController();
6
+ const routed = transport.binding === "stdio" && transport.registerSubscription
7
+ ? new NotificationQueue()
8
+ : undefined;
9
+ const detach = routed
10
+ ? transport.registerSubscription(id, (n) => {
11
+ routed.push(n);
12
+ })
13
+ : undefined;
14
+ const cleanup = () => {
15
+ detach?.();
16
+ routed?.close();
17
+ };
18
+ let exchange;
19
+ try {
20
+ exchange = transport.start({ jsonrpc: "2.0", id, method, params: { ...params, _meta: meta() } }, { ...opts, signal: controller.signal, idempotent: true, timeoutMs: 0 });
21
+ }
22
+ catch (error) {
23
+ cleanup();
24
+ throw error;
25
+ }
26
+ const notifications = routed ?? exchange.notifications;
27
+ const info = {
28
+ requestId: id,
29
+ method,
30
+ binding: transport.binding,
31
+ attempts: 1,
32
+ };
33
+ return {
34
+ id,
35
+ close: () => {
36
+ cleanup();
37
+ controller.abort();
38
+ },
39
+ [Symbol.asyncIterator]: () => notifications[Symbol.asyncIterator](),
40
+ done: exchange.done.finally(cleanup).then((end) => {
41
+ if (end.kind === "failure")
42
+ return {
43
+ kind: "transport-failure",
44
+ failure: end.failure,
45
+ meta: info,
46
+ safeToRetry: true,
47
+ };
48
+ if (end.message.id !== id)
49
+ return {
50
+ kind: "protocol-violation",
51
+ detail: "Events stream id mismatch",
52
+ meta: info,
53
+ };
54
+ if ("error" in end.message)
55
+ return { kind: "jsonrpc-error", ...end.message.error, meta: info };
56
+ return { kind: "ok", value: end.message.result, meta: info };
57
+ }),
58
+ };
59
+ };
60
+ }
@@ -0,0 +1,107 @@
1
+ import type { Clock, DiscoverResult, Extension, FetchPort } from "../core/index.js";
2
+ /** Advisory MCP Server Cards (SEP-2127). Import has no side effects (EXT-REG-1). */
3
+ export declare const SERVER_CARDS_EXTENSION_ID = "io.modelcontextprotocol/server-card";
4
+ export declare const CORE_PROTOCOL_VERSION = "2026-07-28";
5
+ export interface ServerCardRemote {
6
+ readonly type: string;
7
+ readonly url: string;
8
+ readonly supportedVersions?: readonly string[];
9
+ readonly extensionsInfo?: unknown;
10
+ readonly [k: string]: unknown;
11
+ }
12
+ export interface ServerCard {
13
+ readonly $schema: string;
14
+ readonly name: string;
15
+ readonly version: string;
16
+ readonly description: string;
17
+ readonly title?: string;
18
+ readonly icons?: unknown;
19
+ readonly repository?: unknown;
20
+ readonly websiteUrl?: string;
21
+ readonly remotes?: readonly ServerCardRemote[];
22
+ readonly _meta?: Record<string, unknown>;
23
+ }
24
+ export interface ConnectionCandidate {
25
+ readonly url: string;
26
+ readonly supportedVersions: readonly string[];
27
+ readonly extensionsInfo?: unknown;
28
+ }
29
+ export interface ParsedServerCard {
30
+ readonly url: string;
31
+ readonly card: ServerCard;
32
+ readonly connectionCandidates: readonly ConnectionCandidate[];
33
+ /** Always empty: card metadata never becomes negotiated capability (EXT-card-9). */
34
+ readonly inferredCapabilities: Record<string, never>;
35
+ readonly ignoredFields: readonly string[];
36
+ }
37
+ export declare class ServerCardError extends Error {
38
+ readonly code: string;
39
+ constructor(code: string);
40
+ }
41
+ export interface ServerCardCacheEntry {
42
+ readonly parsed: ParsedServerCard;
43
+ readonly etag?: string;
44
+ readonly freshUntil: number;
45
+ }
46
+ export declare class ServerCardCache {
47
+ #private;
48
+ readonly clock: Clock;
49
+ constructor(clock?: Clock);
50
+ get(url: string): ServerCardCacheEntry | undefined;
51
+ set(url: string, e: ServerCardCacheEntry): void;
52
+ delete(url: string): void;
53
+ }
54
+ export interface DiscoverCardOptions {
55
+ readonly fetch?: FetchPort;
56
+ readonly allowInsecureDev?: boolean;
57
+ readonly headers?: Record<string, string>;
58
+ readonly cache?: ServerCardCache;
59
+ }
60
+ /** `<streamable-http base>/server-card` helper. */
61
+ export declare function canonicalServerCardUrl(streamableHttpUrl: string): string;
62
+ /** Validate untrusted JSON against the pinned ServerCard shape. */
63
+ export declare function parseServerCard(raw: unknown, url: string): ParsedServerCard;
64
+ /** Fetch an explicit card URL. Never opens an MCP session or calls server/discover (EXT-card-1, EXT-card-8). */
65
+ export declare function discoverCard(url: string, opts?: DiscoverCardOptions): Promise<ParsedServerCard>;
66
+ /** Fetch `/.well-known/ai-catalog.json` and resolve MCP server card entries (EXT-card-2). */
67
+ export declare function discoverFromDomain(domainOrOrigin: string, opts?: DiscoverCardOptions): Promise<ParsedServerCard[]>;
68
+ export interface Reconciliation {
69
+ readonly effective: DiscoverResult;
70
+ readonly mismatches: string[];
71
+ }
72
+ /** Live server/discover always wins over the advisory card (EXT-card-4). */
73
+ export declare function reconcileWithDiscover(card: ServerCard | ParsedServerCard, liveDiscover: DiscoverResult): Reconciliation;
74
+ export interface ServerCardsExtension extends Extension {
75
+ discoverCard(url: string, opts?: DiscoverCardOptions): Promise<ParsedServerCard>;
76
+ discoverFromDomain(domainOrOrigin: string, opts?: DiscoverCardOptions): Promise<ParsedServerCard[]>;
77
+ }
78
+ /** Advertises no client capability (EXT-card-8). */
79
+ export declare function createServerCardsExtension(defaults?: DiscoverCardOptions): ServerCardsExtension;
80
+ export declare const DEFAULT_SERVER_CARD_SCHEMA = "https://modelcontextprotocol.io/schemas/server-card.json";
81
+ export interface ServerCardInput {
82
+ readonly $schema?: string;
83
+ readonly name: string;
84
+ readonly version: string;
85
+ readonly description: string;
86
+ readonly title?: string;
87
+ readonly icons?: unknown;
88
+ readonly repository?: unknown;
89
+ readonly websiteUrl?: string;
90
+ readonly remotes?: readonly ServerCardRemote[];
91
+ readonly _meta?: Record<string, unknown>;
92
+ }
93
+ /**
94
+ * Build an advisory Server Card for a server you operate.
95
+ *
96
+ * Cards are discovery metadata only: tools/resources/prompts primitives are
97
+ * rejected so a card cannot become an inferred capability document. Live
98
+ * server/discover remains authoritative.
99
+ */
100
+ export declare function buildServerCard(input: ServerCardInput): ServerCard;
101
+ /** Wrap cards in the /.well-known/ai-catalog.json document shape. */
102
+ export declare function buildAiCatalog(cards: readonly ServerCard[]): {
103
+ entries: Array<{
104
+ type: "mcp-server-card";
105
+ data: ServerCard;
106
+ }>;
107
+ };