@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,45 @@
1
+ import { type Obj } from "../server-events/index.js";
2
+ import { type CacheHints } from "./protocol.js";
3
+ export declare const SKILLS_EXTENSION_ID = "io.modelcontextprotocol/skills";
4
+ export declare const MAX_SKILL_FILES = 512;
5
+ export declare const MAX_SKILL_BYTES: number;
6
+ export interface SkillFileInput {
7
+ readonly uri: string;
8
+ readonly mimeType?: string;
9
+ readonly text?: string;
10
+ readonly bytes?: Uint8Array;
11
+ }
12
+ export interface SkillInput {
13
+ readonly uri: string;
14
+ readonly name: string;
15
+ readonly description: string;
16
+ readonly files: readonly SkillFileInput[];
17
+ readonly resources?: "static" | "dynamic";
18
+ readonly allowedTools?: readonly string[];
19
+ readonly metadata?: Record<string, unknown>;
20
+ }
21
+ export interface SkillFileEntry {
22
+ readonly uri: string;
23
+ readonly size: number;
24
+ readonly sha256: string;
25
+ readonly mimeType?: string;
26
+ }
27
+ export interface SkillEntry {
28
+ readonly uri: string;
29
+ readonly name: string;
30
+ readonly description: string;
31
+ readonly files: readonly SkillFileEntry[];
32
+ readonly resources?: "static" | "dynamic";
33
+ readonly allowedTools?: readonly string[];
34
+ readonly metadata?: Record<string, unknown>;
35
+ }
36
+ /** Server-side skills publisher: manifests, hashes, and file contents. */
37
+ export declare class SkillsRegistry {
38
+ #private;
39
+ capabilities(): Obj;
40
+ register(input: SkillInput): SkillEntry;
41
+ list(cursor: unknown, cache?: CacheHints): Obj;
42
+ get(uri: string, cache?: CacheHints): Obj;
43
+ /** File contents for the host's resources/read fallback. */
44
+ read(uri: string): Obj | undefined;
45
+ }
@@ -0,0 +1,73 @@
1
+ import { createHash } from "node:crypto";
2
+ import { ServerError } from "../server-events/index.js";
3
+ import { complete, DEFAULT_CACHE_HINTS, paginate } from "./protocol.js";
4
+ export const SKILLS_EXTENSION_ID = "io.modelcontextprotocol/skills";
5
+ export const MAX_SKILL_FILES = 512;
6
+ export const MAX_SKILL_BYTES = 16 * 1024 * 1024;
7
+ /** Server-side skills publisher: manifests, hashes, and file contents. */
8
+ export class SkillsRegistry {
9
+ #skills = new Map();
10
+ #files = new Map();
11
+ capabilities() {
12
+ return { extensions: { [SKILLS_EXTENSION_ID]: {} } };
13
+ }
14
+ register(input) {
15
+ if (!input.uri || !input.name || !input.description)
16
+ throw new ServerError(-32602, "skill uri/name/description required");
17
+ if (input.files.length > MAX_SKILL_FILES)
18
+ throw new ServerError(-32602, "too many skill files");
19
+ let total = 0;
20
+ const files = [];
21
+ for (const file of input.files) {
22
+ const bytes = file.bytes ?? new TextEncoder().encode(file.text ?? "");
23
+ total += bytes.byteLength;
24
+ if (total > MAX_SKILL_BYTES)
25
+ throw new ServerError(-32602, "skill files too large");
26
+ const entry = {
27
+ uri: file.uri,
28
+ size: bytes.byteLength,
29
+ sha256: createHash("sha256").update(bytes).digest("hex"),
30
+ ...(file.mimeType !== undefined ? { mimeType: file.mimeType } : {}),
31
+ };
32
+ files.push(entry);
33
+ this.#files.set(file.uri, { bytes, ...(file.mimeType !== undefined ? { mimeType: file.mimeType } : {}) });
34
+ }
35
+ const entry = {
36
+ uri: input.uri,
37
+ name: input.name,
38
+ description: input.description,
39
+ files,
40
+ ...(input.resources !== undefined ? { resources: input.resources } : {}),
41
+ ...(input.allowedTools !== undefined ? { allowedTools: input.allowedTools } : {}),
42
+ ...(input.metadata !== undefined ? { metadata: input.metadata } : {}),
43
+ };
44
+ this.#skills.set(entry.uri, entry);
45
+ return entry;
46
+ }
47
+ list(cursor, cache = DEFAULT_CACHE_HINTS) {
48
+ const skills = [...this.#skills.values()].sort((a, b) => a.uri.localeCompare(b.uri));
49
+ const page = paginate(skills, cursor);
50
+ return complete({
51
+ skills: page.items,
52
+ ...(page.nextCursor !== undefined ? { nextCursor: page.nextCursor } : {}),
53
+ }, cache);
54
+ }
55
+ get(uri, cache = DEFAULT_CACHE_HINTS) {
56
+ const skill = this.#skills.get(uri);
57
+ if (!skill)
58
+ throw new ServerError(-32602, `unknown skill: ${uri}`);
59
+ return complete({ skill }, cache);
60
+ }
61
+ /** File contents for the host's resources/read fallback. */
62
+ read(uri) {
63
+ const file = this.#files.get(uri);
64
+ if (!file)
65
+ return undefined;
66
+ const text = new TextDecoder().decode(file.bytes);
67
+ return {
68
+ uri,
69
+ text,
70
+ ...(file.mimeType !== undefined ? { mimeType: file.mimeType } : {}),
71
+ };
72
+ }
73
+ }
@@ -0,0 +1,2 @@
1
+ /** RFC 6570 subset matcher for resource reads: {var} and {+var} expressions. */
2
+ export declare function matchUriTemplate(template: string, uri: string): Record<string, string> | undefined;
@@ -0,0 +1,27 @@
1
+ /** RFC 6570 subset matcher for resource reads: {var} and {+var} expressions. */
2
+ export function matchUriTemplate(template, uri) {
3
+ const vars = [];
4
+ let pattern = "";
5
+ for (let i = 0; i < template.length; i++) {
6
+ const ch = template[i];
7
+ if (ch === "{") {
8
+ const end = template.indexOf("}", i);
9
+ if (end < 0)
10
+ return undefined;
11
+ const expr = template.slice(i + 1, end);
12
+ if (/^[+]?[A-Za-z_][A-Za-z0-9_]*$/u.test(expr) === false)
13
+ return undefined;
14
+ const plus = expr.startsWith("+");
15
+ vars.push(plus ? expr.slice(1) : expr);
16
+ pattern += plus ? "(.+)" : "([^/]+)";
17
+ i = end;
18
+ }
19
+ else {
20
+ pattern += ch.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
21
+ }
22
+ }
23
+ const match = new RegExp(`^${pattern}$`, "u").exec(uri);
24
+ if (!match)
25
+ return undefined;
26
+ return Object.fromEntries(vars.map((name, i) => [name, decodeURIComponent(match[i + 1])]));
27
+ }
@@ -0,0 +1,40 @@
1
+ import { type Clock } from "../core/index.js";
2
+ import { type Obj } from "./types.js";
3
+ export interface ServerOccurrence {
4
+ readonly eventId: string;
5
+ readonly name: string;
6
+ readonly timestamp: string;
7
+ readonly data: unknown;
8
+ readonly cursor: string;
9
+ }
10
+ export interface PollPage {
11
+ readonly events: readonly ServerOccurrence[];
12
+ readonly cursor: string;
13
+ readonly truncated: boolean;
14
+ readonly hasMore: boolean;
15
+ }
16
+ export interface EventBusOptions {
17
+ /** Retained occurrences per name+arguments stream. Default 1000. */
18
+ readonly retain?: number;
19
+ readonly clock?: Clock;
20
+ readonly idGenerator?: () => string;
21
+ }
22
+ /**
23
+ * Named, argument-scoped event logs with opaque cursors and bounded retention.
24
+ * Transport-independent: EventsServer and TaskCenter publish into this bus;
25
+ * hosts can also publish directly for application events/notices.
26
+ */
27
+ export declare class EventBus {
28
+ #private;
29
+ constructor(opts?: EventBusOptions);
30
+ list(): Array<{
31
+ name: string;
32
+ arguments: Obj;
33
+ }>;
34
+ publish(name: string, data: unknown, args?: Obj): ServerOccurrence;
35
+ /** Current end cursor: subscribers starting here receive only new events. */
36
+ endCursor(name: string, args?: Obj): string;
37
+ poll(name: string, args: Obj, cursor: string | null, limit: number): PollPage;
38
+ /** Live subscription; returns an unsubscribe function. */
39
+ subscribe(name: string, args: Obj, fn: (event: ServerOccurrence) => void): () => void;
40
+ }
@@ -0,0 +1,84 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { systemClock } from "../core/index.js";
3
+ import { streamKey } from "./types.js";
4
+ /**
5
+ * Named, argument-scoped event logs with opaque cursors and bounded retention.
6
+ * Transport-independent: EventsServer and TaskCenter publish into this bus;
7
+ * hosts can also publish directly for application events/notices.
8
+ */
9
+ export class EventBus {
10
+ #retain;
11
+ #clock;
12
+ #idGenerator;
13
+ #logs = new Map();
14
+ #listeners = new Map();
15
+ constructor(opts = {}) {
16
+ this.#retain = opts.retain ?? 1000;
17
+ this.#clock = opts.clock ?? systemClock;
18
+ this.#idGenerator = opts.idGenerator ?? randomUUID;
19
+ }
20
+ list() {
21
+ return [...this.#logs.values()].map((log) => ({ name: log.name, arguments: log.args }));
22
+ }
23
+ #log(name, args) {
24
+ const key = streamKey(name, args);
25
+ let log = this.#logs.get(key);
26
+ if (!log) {
27
+ log = { name, args, seq: 0, events: [] };
28
+ this.#logs.set(key, log);
29
+ }
30
+ return log;
31
+ }
32
+ publish(name, data, args = {}) {
33
+ const log = this.#log(name, args);
34
+ log.seq += 1;
35
+ const event = {
36
+ eventId: this.#idGenerator(),
37
+ name,
38
+ timestamp: new Date(this.#clock.now()).toISOString(),
39
+ data,
40
+ cursor: String(log.seq),
41
+ };
42
+ log.events.push(event);
43
+ if (log.events.length > this.#retain)
44
+ log.events.splice(0, log.events.length - this.#retain);
45
+ for (const fn of this.#listeners.get(streamKey(name, args)) ?? [])
46
+ fn(event);
47
+ return event;
48
+ }
49
+ /** Current end cursor: subscribers starting here receive only new events. */
50
+ endCursor(name, args = {}) {
51
+ return String(this.#log(name, args).seq);
52
+ }
53
+ poll(name, args, cursor, limit) {
54
+ const log = this.#log(name, args);
55
+ const after = cursor === null ? 0 : Number.parseInt(cursor, 10);
56
+ if (!Number.isSafeInteger(after) || after < 0)
57
+ throw new Error("invalid events cursor");
58
+ const oldest = log.events.length > 0 ? Number.parseInt(log.events[0].cursor, 10) : log.seq + 1;
59
+ const truncated = after > 0 && after + 1 < oldest;
60
+ const matched = log.events.filter((e) => Number.parseInt(e.cursor, 10) > after);
61
+ const events = matched.slice(0, limit);
62
+ return {
63
+ events,
64
+ cursor: events.length > 0 ? events[events.length - 1].cursor : String(log.seq),
65
+ truncated,
66
+ hasMore: matched.length > events.length,
67
+ };
68
+ }
69
+ /** Live subscription; returns an unsubscribe function. */
70
+ subscribe(name, args, fn) {
71
+ const key = streamKey(name, args);
72
+ let set = this.#listeners.get(key);
73
+ if (!set) {
74
+ set = new Set();
75
+ this.#listeners.set(key, set);
76
+ }
77
+ set.add(fn);
78
+ return () => {
79
+ set.delete(fn);
80
+ if (set.size === 0)
81
+ this.#listeners.delete(key);
82
+ };
83
+ }
84
+ }
@@ -0,0 +1,36 @@
1
+ import { type Clock } from "../core/index.js";
2
+ import { EventBus } from "./bus.js";
3
+ import { type Obj, type PushChannel } from "./types.js";
4
+ export declare const EVENTS_EXTENSION_ID = "io.modelcontextprotocol/events";
5
+ export interface EventsServerOptions {
6
+ readonly bus?: EventBus;
7
+ /** Heartbeat interval for live streams. Default 15000ms. */
8
+ readonly heartbeatMs?: number;
9
+ /** Max occurrences returned by one events/poll. Default 100. */
10
+ readonly pageSize?: number;
11
+ /** Suggested delay for empty polls. Default 1000ms. */
12
+ readonly nextPollMs?: number;
13
+ readonly clock?: Clock;
14
+ }
15
+ /**
16
+ * Server side of io.modelcontextprotocol/events: events/list, events/poll,
17
+ * and live events/stream with active/event/heartbeat/terminated notices.
18
+ * Webhook delivery (events/subscribe with a URL) is intentionally out of scope.
19
+ */
20
+ export declare class EventsServer {
21
+ #private;
22
+ readonly bus: EventBus;
23
+ constructor(opts?: EventsServerOptions);
24
+ capabilities(): Obj;
25
+ list(): Obj;
26
+ poll(params: Obj): Obj;
27
+ /** Open a live stream on a host channel. Null cursor means "new events only". */
28
+ stream(channel: PushChannel, params: Obj): void;
29
+ /** Terminate every live stream, optionally only one name+arguments stream. */
30
+ terminate(name?: string, args?: Obj, error?: unknown): void;
31
+ }
32
+ export declare function readSubscription(params: Obj): {
33
+ name: string;
34
+ args: Obj;
35
+ cursor: string | null;
36
+ };
@@ -0,0 +1,108 @@
1
+ import { systemClock } from "../core/index.js";
2
+ import { EventBus } from "./bus.js";
3
+ import { notification, object, ServerError } from "./types.js";
4
+ export const EVENTS_EXTENSION_ID = "io.modelcontextprotocol/events";
5
+ /**
6
+ * Server side of io.modelcontextprotocol/events: events/list, events/poll,
7
+ * and live events/stream with active/event/heartbeat/terminated notices.
8
+ * Webhook delivery (events/subscribe with a URL) is intentionally out of scope.
9
+ */
10
+ export class EventsServer {
11
+ bus;
12
+ #heartbeatMs;
13
+ #pageSize;
14
+ #nextPollMs;
15
+ #clock;
16
+ #live = new Set();
17
+ constructor(opts = {}) {
18
+ this.bus = opts.bus ?? new EventBus();
19
+ this.#heartbeatMs = opts.heartbeatMs ?? 15_000;
20
+ this.#pageSize = opts.pageSize ?? 100;
21
+ this.#nextPollMs = opts.nextPollMs ?? 1000;
22
+ this.#clock = opts.clock ?? systemClock;
23
+ }
24
+ capabilities() {
25
+ return { extensions: { [EVENTS_EXTENSION_ID]: { revision: "2026-07-28", listChanged: true } } };
26
+ }
27
+ list() {
28
+ return { events: this.bus.list() };
29
+ }
30
+ poll(params) {
31
+ const { name, args, cursor } = readSubscription(params);
32
+ const page = this.bus.poll(name, args, cursor, this.#pageSize);
33
+ return {
34
+ events: page.events,
35
+ cursor: page.cursor,
36
+ ...(page.truncated ? { truncated: true } : {}),
37
+ ...(page.hasMore ? { hasMore: true } : {}),
38
+ nextPollMs: page.hasMore ? 0 : this.#nextPollMs,
39
+ };
40
+ }
41
+ /** Open a live stream on a host channel. Null cursor means "new events only". */
42
+ stream(channel, params) {
43
+ const { name, args, cursor } = readSubscription(params);
44
+ const live = {
45
+ channel,
46
+ name,
47
+ args,
48
+ cursor: cursor ?? this.bus.endCursor(name, args),
49
+ unsubscribe: () => { },
50
+ stopHeartbeat: () => { },
51
+ };
52
+ const send = (event) => {
53
+ live.cursor = event.cursor;
54
+ channel.notify(notification("notifications/events/event", { ...event }, channel.id));
55
+ };
56
+ const truncated = cursor !== null && this.bus.poll(name, args, cursor, 1).truncated;
57
+ channel.notify(notification("notifications/events/active", {
58
+ heartbeatMs: this.#heartbeatMs,
59
+ cursor: live.cursor,
60
+ ...(truncated ? { truncated: true } : {}),
61
+ }, channel.id));
62
+ if (cursor !== null) {
63
+ // Replay retained occurrences after the cursor, then continue live.
64
+ let page = this.bus.poll(name, args, cursor, this.#pageSize);
65
+ for (;;) {
66
+ for (const event of page.events)
67
+ send(event);
68
+ if (!page.hasMore)
69
+ break;
70
+ page = this.bus.poll(name, args, live.cursor, this.#pageSize);
71
+ }
72
+ }
73
+ live.unsubscribe = this.bus.subscribe(name, args, send);
74
+ const heartbeat = () => {
75
+ channel.notify(notification("notifications/events/heartbeat", { cursor: live.cursor }, channel.id));
76
+ live.stopHeartbeat = this.#clock.setTimeout(heartbeat, this.#heartbeatMs);
77
+ };
78
+ live.stopHeartbeat = this.#clock.setTimeout(heartbeat, this.#heartbeatMs);
79
+ this.#live.add(live);
80
+ channel.onClose(() => this.#close(live));
81
+ }
82
+ /** Terminate every live stream, optionally only one name+arguments stream. */
83
+ terminate(name, args = {}, error) {
84
+ for (const live of [...this.#live]) {
85
+ if (name !== undefined && (live.name !== name || live.args !== args))
86
+ continue;
87
+ live.channel.notify(notification("notifications/events/terminated", { ...(error !== undefined ? { error } : {}) }, live.channel.id));
88
+ this.#close(live);
89
+ }
90
+ }
91
+ #close(live) {
92
+ live.unsubscribe();
93
+ live.stopHeartbeat();
94
+ this.#live.delete(live);
95
+ }
96
+ }
97
+ export function readSubscription(params) {
98
+ if (typeof params.name !== "string" || params.name === "")
99
+ throw new ServerError(-32602, "events name required");
100
+ const args = params.arguments === undefined ? {} : params.arguments;
101
+ if (!object(args))
102
+ throw new ServerError(-32602, "events arguments must be an object");
103
+ const cursor = params.cursor;
104
+ if (cursor !== undefined && cursor !== null && typeof cursor !== "string") {
105
+ throw new ServerError(-32602, "events cursor must be a string or null");
106
+ }
107
+ return { name: params.name, args, cursor: cursor ?? null };
108
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./types.js";
2
+ export * from "./bus.js";
3
+ export * from "./events.js";
4
+ export * from "./subscriptions.js";
5
+ export * from "./tasks.js";
6
+ export * from "./notices.js";
@@ -0,0 +1,6 @@
1
+ export * from "./types.js";
2
+ export * from "./bus.js";
3
+ export * from "./events.js";
4
+ export * from "./subscriptions.js";
5
+ export * from "./tasks.js";
6
+ export * from "./notices.js";
@@ -0,0 +1,24 @@
1
+ import type { EventBus } from "./bus.js";
2
+ import { type Obj, type PushChannel } from "./types.js";
3
+ /**
4
+ * Notices are application events, not the deprecated MCP Logging feature.
5
+ * Publish them on a named event stream (default "notices") so clients receive
6
+ * them through the same push path as everything else.
7
+ */
8
+ export declare class Notices {
9
+ private readonly bus;
10
+ private readonly stream;
11
+ constructor(bus: EventBus, stream?: string);
12
+ info(message: string, data?: unknown): void;
13
+ warn(message: string, data?: unknown): void;
14
+ error(message: string, data?: unknown): void;
15
+ }
16
+ /** Request-scoped progress notice (not on the subscriptions/listen stream). */
17
+ export declare function sendProgress(channel: PushChannel, progress: {
18
+ readonly progressToken: string | number;
19
+ readonly progress: number;
20
+ readonly total?: number;
21
+ readonly message?: string;
22
+ }): void;
23
+ /** Request-scoped custom notice. Do not use for list_changed or events traffic. */
24
+ export declare function sendNotice(channel: PushChannel, method: string, params?: Obj): void;
@@ -0,0 +1,31 @@
1
+ import { notification } from "./types.js";
2
+ /**
3
+ * Notices are application events, not the deprecated MCP Logging feature.
4
+ * Publish them on a named event stream (default "notices") so clients receive
5
+ * them through the same push path as everything else.
6
+ */
7
+ export class Notices {
8
+ bus;
9
+ stream;
10
+ constructor(bus, stream = "notices") {
11
+ this.bus = bus;
12
+ this.stream = stream;
13
+ }
14
+ info(message, data) {
15
+ this.bus.publish(this.stream, { level: "info", message, ...(data !== undefined ? { data } : {}) });
16
+ }
17
+ warn(message, data) {
18
+ this.bus.publish(this.stream, { level: "warning", message, ...(data !== undefined ? { data } : {}) });
19
+ }
20
+ error(message, data) {
21
+ this.bus.publish(this.stream, { level: "error", message, ...(data !== undefined ? { data } : {}) });
22
+ }
23
+ }
24
+ /** Request-scoped progress notice (not on the subscriptions/listen stream). */
25
+ export function sendProgress(channel, progress) {
26
+ channel.notify(notification("notifications/progress", { ...progress }));
27
+ }
28
+ /** Request-scoped custom notice. Do not use for list_changed or events traffic. */
29
+ export function sendNotice(channel, method, params = {}) {
30
+ channel.notify(notification(method, params));
31
+ }
@@ -0,0 +1,33 @@
1
+ import { type Obj, type PushChannel } from "./types.js";
2
+ export interface SubscriptionCapabilities {
3
+ readonly tools?: {
4
+ readonly listChanged?: boolean;
5
+ };
6
+ readonly prompts?: {
7
+ readonly listChanged?: boolean;
8
+ };
9
+ readonly resources?: {
10
+ readonly listChanged?: boolean;
11
+ readonly subscribe?: boolean;
12
+ };
13
+ }
14
+ /**
15
+ * Standard MCP subscriptions/listen: capability-filtered grants, ack-first,
16
+ * list_changed and resource-updated fanout, and graceful stream completion.
17
+ * The host calls notify* when its own tools/prompts/resources change.
18
+ */
19
+ export declare class SubscriptionsServer {
20
+ #private;
21
+ constructor(opts?: {
22
+ readonly capabilities?: SubscriptionCapabilities;
23
+ });
24
+ capabilities(): Obj;
25
+ listen(channel: PushChannel, params: Obj): void;
26
+ /** Gracefully end a listen stream (server shutdown or filter replacement). */
27
+ end(channel: PushChannel): void;
28
+ notifyToolsChanged(): void;
29
+ notifyPromptsChanged(): void;
30
+ notifyResourcesChanged(): void;
31
+ notifyResourceUpdated(uri: string): void;
32
+ }
33
+ export declare function requireTaskId(params: Obj): string;
@@ -0,0 +1,77 @@
1
+ import { notification, object, ServerError } from "./types.js";
2
+ /**
3
+ * Standard MCP subscriptions/listen: capability-filtered grants, ack-first,
4
+ * list_changed and resource-updated fanout, and graceful stream completion.
5
+ * The host calls notify* when its own tools/prompts/resources change.
6
+ */
7
+ export class SubscriptionsServer {
8
+ #capabilities;
9
+ #listeners = new Map();
10
+ constructor(opts = {}) {
11
+ this.#capabilities = opts.capabilities ?? {};
12
+ }
13
+ capabilities() {
14
+ return { ...this.#capabilities };
15
+ }
16
+ listen(channel, params) {
17
+ const requested = object(params.notifications) ? params.notifications : {};
18
+ const granted = {};
19
+ const offered = {};
20
+ const kinds = ["tools", "prompts", "resources"];
21
+ for (const kind of kinds) {
22
+ const key = `${kind}ListChanged`;
23
+ if (requested[key] !== true)
24
+ continue;
25
+ const supported = this.#capabilities[kind]?.listChanged === true;
26
+ offered[key] = supported;
27
+ if (supported)
28
+ granted[key] = true;
29
+ }
30
+ if (Array.isArray(requested.resourceSubscriptions)) {
31
+ const uris = requested.resourceSubscriptions.filter((u) => typeof u === "string");
32
+ if (this.#capabilities.resources?.subscribe === true) {
33
+ granted.resourceSubscriptions = [...new Set(uris)];
34
+ offered.resourceSubscriptions = granted.resourceSubscriptions;
35
+ }
36
+ else {
37
+ offered.resourceSubscriptions = [];
38
+ }
39
+ }
40
+ this.#listeners.set(channel.id, { channel, granted });
41
+ // Ack is always the first message on a listen stream.
42
+ channel.notify(notification("notifications/subscriptions/acknowledged", { notifications: offered }, channel.id));
43
+ channel.onClose(() => this.#listeners.delete(channel.id));
44
+ }
45
+ /** Gracefully end a listen stream (server shutdown or filter replacement). */
46
+ end(channel) {
47
+ this.#listeners.delete(channel.id);
48
+ channel.finish({ resultType: "complete", _meta: { "io.modelcontextprotocol/subscriptionId": channel.id } });
49
+ }
50
+ notifyToolsChanged() {
51
+ this.#fanout("toolsListChanged", "notifications/tools/list_changed");
52
+ }
53
+ notifyPromptsChanged() {
54
+ this.#fanout("promptsListChanged", "notifications/prompts/list_changed");
55
+ }
56
+ notifyResourcesChanged() {
57
+ this.#fanout("resourcesListChanged", "notifications/resources/list_changed");
58
+ }
59
+ notifyResourceUpdated(uri) {
60
+ for (const listener of this.#listeners.values()) {
61
+ if (listener.granted.resourceSubscriptions?.includes(uri)) {
62
+ listener.channel.notify(notification("notifications/resources/updated", { uri }, listener.channel.id));
63
+ }
64
+ }
65
+ }
66
+ #fanout(grant, method) {
67
+ for (const listener of this.#listeners.values()) {
68
+ if (listener.granted[grant])
69
+ listener.channel.notify(notification(method, {}, listener.channel.id));
70
+ }
71
+ }
72
+ }
73
+ export function requireTaskId(params) {
74
+ if (typeof params.taskId !== "string" || params.taskId === "")
75
+ throw new ServerError(-32602, "taskId required");
76
+ return params.taskId;
77
+ }
@@ -0,0 +1,43 @@
1
+ import { type Clock } from "../core/index.js";
2
+ import type { EventBus } from "./bus.js";
3
+ import { type Obj, type PushChannel } from "./types.js";
4
+ export declare const TASKS_EXTENSION_ID = "io.modelcontextprotocol/tasks";
5
+ export type TaskStatus = "working" | "input_required" | "completed" | "failed" | "cancelled";
6
+ export interface TaskState extends Obj {
7
+ taskId: string;
8
+ status: TaskStatus;
9
+ ttlMs?: number;
10
+ pollIntervalMs?: number;
11
+ inputRequests?: Record<string, {
12
+ method: string;
13
+ params: Obj;
14
+ }>;
15
+ }
16
+ /**
17
+ * Optional async-task companion to the push center: task handles whose state
18
+ * changes are emitted as notifications/tasks and published on the "tasks"
19
+ * event stream. No tools framework — hosts decide which operations start tasks.
20
+ */
21
+ export declare class TaskCenter {
22
+ #private;
23
+ constructor(opts?: {
24
+ readonly bus?: EventBus;
25
+ readonly clock?: Clock;
26
+ });
27
+ capabilities(): Obj;
28
+ /** Attach a push channel (e.g. a subscriptions/listen channel) for notifications/tasks. */
29
+ attach(channel: PushChannel): void;
30
+ create(init?: {
31
+ taskId?: string;
32
+ ttlMs?: number;
33
+ pollIntervalMs?: number;
34
+ inputRequests?: TaskState["inputRequests"];
35
+ }): TaskState;
36
+ get(params: Obj): TaskState;
37
+ cancel(params: Obj): TaskState;
38
+ /** Client input for an input_required task; resumes work. */
39
+ update(params: Obj): TaskState;
40
+ requestInput(taskId: string, inputRequests: NonNullable<TaskState["inputRequests"]>): TaskState;
41
+ complete(taskId: string, result: Obj): TaskState;
42
+ fail(taskId: string, error: Obj): TaskState;
43
+ }