@jangmanj/mcp 0.0.0-stage → 0.12.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +337 -2
- package/adapters/pi/HOST-NOTES.md +65 -0
- package/adapters/pi/bridge.ts +721 -0
- package/adapters/pi/config.ts +364 -0
- package/adapters/pi/content-offloaded.ts +53 -0
- package/adapters/pi/content.ts +78 -0
- package/adapters/pi/diagnostics.ts +26 -0
- package/adapters/pi/elicitation.ts +133 -0
- package/adapters/pi/index.ts +210 -0
- package/adapters/pi/oauth.ts +219 -0
- package/adapters/pi/pi-api.ts +133 -0
- package/adapters/pi/resources.ts +177 -0
- package/adapters/pi/result-files.ts +32 -0
- package/adapters/pi/tasks.ts +49 -0
- package/dist/auth/client-credentials.d.ts +65 -0
- package/dist/auth/client-credentials.js +388 -0
- package/dist/auth/discovery.d.ts +28 -0
- package/dist/auth/discovery.js +125 -0
- package/dist/auth/index.d.ts +9 -0
- package/dist/auth/index.js +5 -0
- package/dist/auth/node-ports.d.ts +8 -0
- package/dist/auth/node-ports.js +67 -0
- package/dist/auth/protocol.d.ts +12 -0
- package/dist/auth/protocol.js +39 -0
- package/dist/auth/provider.d.ts +114 -0
- package/dist/auth/provider.js +407 -0
- package/dist/auth/safe-fetch.d.ts +14 -0
- package/dist/auth/safe-fetch.js +130 -0
- package/dist/client/events.d.ts +33 -0
- package/dist/client/events.js +84 -0
- package/dist/client/index.d.ts +65 -0
- package/dist/client/index.js +128 -0
- package/dist/core/auth-types.d.ts +36 -0
- package/dist/core/auth-types.js +11 -0
- package/dist/core/bearer-challenge.d.ts +3 -0
- package/dist/core/bearer-challenge.js +63 -0
- package/dist/core/cancel.d.ts +70 -0
- package/dist/core/cancel.js +177 -0
- package/dist/core/extension.d.ts +33 -0
- package/dist/core/extension.js +145 -0
- package/dist/core/index.d.ts +73 -0
- package/dist/core/index.js +129 -0
- package/dist/core/jsonrpc.d.ts +49 -0
- package/dist/core/jsonrpc.js +80 -0
- package/dist/core/lifecycle.d.ts +128 -0
- package/dist/core/lifecycle.js +251 -0
- package/dist/core/outcome.d.ts +98 -0
- package/dist/core/outcome.js +32 -0
- package/dist/core/ports.d.ts +144 -0
- package/dist/core/ports.js +67 -0
- package/dist/core/progress.d.ts +81 -0
- package/dist/core/progress.js +205 -0
- package/dist/core/request.d.ts +108 -0
- package/dist/core/request.js +578 -0
- package/dist/core/schema/index.d.ts +1 -0
- package/dist/core/schema/index.js +1 -0
- package/dist/core/schema/types.d.ts +4154 -0
- package/dist/core/schema/types.js +6 -0
- package/dist/core/telemetry.d.ts +25 -0
- package/dist/core/telemetry.js +37 -0
- package/dist/ext-events/index.d.ts +79 -0
- package/dist/ext-events/index.js +218 -0
- package/dist/ext-events/model.d.ts +10 -0
- package/dist/ext-events/model.js +6 -0
- package/dist/ext-events/push.d.ts +18 -0
- package/dist/ext-events/push.js +101 -0
- package/dist/ext-events/stream.d.ts +6 -0
- package/dist/ext-events/stream.js +60 -0
- package/dist/ext-server-cards/index.d.ts +107 -0
- package/dist/ext-server-cards/index.js +249 -0
- package/dist/ext-skills/cache.d.ts +20 -0
- package/dist/ext-skills/cache.js +101 -0
- package/dist/ext-skills/client.d.ts +82 -0
- package/dist/ext-skills/client.js +292 -0
- package/dist/ext-skills/extension.d.ts +16 -0
- package/dist/ext-skills/extension.js +29 -0
- package/dist/ext-skills/index.d.ts +4 -0
- package/dist/ext-skills/index.js +4 -0
- package/dist/ext-skills/verify.d.ts +57 -0
- package/dist/ext-skills/verify.js +168 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mrtr-cache/cache.d.ts +51 -0
- package/dist/mrtr-cache/cache.js +295 -0
- package/dist/mrtr-cache/client.d.ts +24 -0
- package/dist/mrtr-cache/client.js +44 -0
- package/dist/mrtr-cache/common.d.ts +10 -0
- package/dist/mrtr-cache/common.js +9 -0
- package/dist/mrtr-cache/elicitation.d.ts +42 -0
- package/dist/mrtr-cache/elicitation.js +221 -0
- package/dist/mrtr-cache/index.d.ts +9 -0
- package/dist/mrtr-cache/index.js +8 -0
- package/dist/mrtr-cache/mrtr.d.ts +37 -0
- package/dist/mrtr-cache/mrtr.js +250 -0
- package/dist/mrtr-cache/refresh.d.ts +8 -0
- package/dist/mrtr-cache/refresh.js +30 -0
- package/dist/mrtr-cache/stream.d.ts +5 -0
- package/dist/mrtr-cache/stream.js +18 -0
- package/dist/mrtr-cache/subscriptions.d.ts +96 -0
- package/dist/mrtr-cache/subscriptions.js +288 -0
- package/dist/mrtr-cache/tasks.d.ts +103 -0
- package/dist/mrtr-cache/tasks.js +444 -0
- package/dist/ops/common.d.ts +18 -0
- package/dist/ops/common.js +39 -0
- package/dist/ops/completion.d.ts +44 -0
- package/dist/ops/completion.js +65 -0
- package/dist/ops/content.d.ts +40 -0
- package/dist/ops/content.js +117 -0
- package/dist/ops/icons.d.ts +29 -0
- package/dist/ops/icons.js +82 -0
- package/dist/ops/index.d.ts +30 -0
- package/dist/ops/index.js +28 -0
- package/dist/ops/pagination.d.ts +32 -0
- package/dist/ops/pagination.js +65 -0
- package/dist/ops/prompts.d.ts +32 -0
- package/dist/ops/prompts.js +68 -0
- package/dist/ops/resources.d.ts +42 -0
- package/dist/ops/resources.js +104 -0
- package/dist/ops/schema.d.ts +39 -0
- package/dist/ops/schema.js +163 -0
- package/dist/ops/tools.d.ts +88 -0
- package/dist/ops/tools.js +169 -0
- package/dist/ops/uri-template.d.ts +3 -0
- package/dist/ops/uri-template.js +101 -0
- package/dist/server/index.d.ts +5 -0
- package/dist/server/index.js +5 -0
- package/dist/server/mrtr.d.ts +38 -0
- package/dist/server/mrtr.js +99 -0
- package/dist/server/protocol.d.ts +21 -0
- package/dist/server/protocol.js +49 -0
- package/dist/server/server.d.ts +105 -0
- package/dist/server/server.js +383 -0
- package/dist/server/skills.d.ts +45 -0
- package/dist/server/skills.js +73 -0
- package/dist/server/uri.d.ts +2 -0
- package/dist/server/uri.js +27 -0
- package/dist/server-events/bus.d.ts +40 -0
- package/dist/server-events/bus.js +84 -0
- package/dist/server-events/events.d.ts +36 -0
- package/dist/server-events/events.js +108 -0
- package/dist/server-events/index.d.ts +6 -0
- package/dist/server-events/index.js +6 -0
- package/dist/server-events/notices.d.ts +24 -0
- package/dist/server-events/notices.js +31 -0
- package/dist/server-events/subscriptions.d.ts +33 -0
- package/dist/server-events/subscriptions.js +77 -0
- package/dist/server-events/tasks.d.ts +43 -0
- package/dist/server-events/tasks.js +101 -0
- package/dist/server-events/types.d.ts +25 -0
- package/dist/server-events/types.js +34 -0
- package/dist/server-transport/auth.d.ts +20 -0
- package/dist/server-transport/auth.js +29 -0
- package/dist/server-transport/channel.d.ts +17 -0
- package/dist/server-transport/channel.js +47 -0
- package/dist/server-transport/http.d.ts +19 -0
- package/dist/server-transport/http.js +209 -0
- package/dist/server-transport/index.d.ts +4 -0
- package/dist/server-transport/index.js +4 -0
- package/dist/server-transport/stdio.d.ts +16 -0
- package/dist/server-transport/stdio.js +83 -0
- package/dist/telemetry-otel/index.d.ts +64 -0
- package/dist/telemetry-otel/index.js +196 -0
- package/dist/testing/index.d.ts +36 -0
- package/dist/testing/index.js +140 -0
- package/dist/transport-http/headers.d.ts +24 -0
- package/dist/transport-http/headers.js +140 -0
- package/dist/transport-http/index.d.ts +3 -0
- package/dist/transport-http/index.js +3 -0
- package/dist/transport-http/sse.d.ts +33 -0
- package/dist/transport-http/sse.js +152 -0
- package/dist/transport-http/transport.d.ts +66 -0
- package/dist/transport-http/transport.js +854 -0
- package/dist/transport-stdio/framing.d.ts +50 -0
- package/dist/transport-stdio/framing.js +154 -0
- package/dist/transport-stdio/index.d.ts +4 -0
- package/dist/transport-stdio/index.js +4 -0
- package/dist/transport-stdio/stderr-ring.d.ts +27 -0
- package/dist/transport-stdio/stderr-ring.js +105 -0
- package/dist/transport-stdio/transport.d.ts +142 -0
- package/dist/transport-stdio/transport.js +743 -0
- package/dist/transport-stdio/write-queue.d.ts +48 -0
- package/dist/transport-stdio/write-queue.js +147 -0
- package/package.json +134 -4
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type JsonRpcNotification, type JsonRpcRequest } from "../core/index.js";
|
|
2
|
+
export declare const DEFAULT_MAX_LINE_BYTES: number;
|
|
3
|
+
/**
|
|
4
|
+
* Validates that an outbound notification is a valid JSON-RPC 2.0 notification
|
|
5
|
+
* (never a response envelope, WIRE-DIR-1 / STDIO-STDIN-1).
|
|
6
|
+
*/
|
|
7
|
+
export declare function assertOutboundNotification(msg: unknown): asserts msg is JsonRpcNotification;
|
|
8
|
+
/**
|
|
9
|
+
* Encodes an outbound JSON-RPC request or notification as a single UTF-8 line
|
|
10
|
+
* terminated by `\n`, asserting no embedded raw newline or carriage return (STDIO-FRAME-1).
|
|
11
|
+
*/
|
|
12
|
+
export declare function encodeStdioMessage(msg: JsonRpcRequest | JsonRpcNotification): string;
|
|
13
|
+
export type LineSplitterEvent = {
|
|
14
|
+
readonly kind: "line";
|
|
15
|
+
readonly text: string;
|
|
16
|
+
readonly byteLength: number;
|
|
17
|
+
} | {
|
|
18
|
+
readonly kind: "invalid-utf8";
|
|
19
|
+
readonly byteLength: number;
|
|
20
|
+
readonly preview: string;
|
|
21
|
+
} | {
|
|
22
|
+
readonly kind: "oversize";
|
|
23
|
+
readonly byteLength: number;
|
|
24
|
+
readonly maxLineBytes: number;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Incremental byte-safe newline-delimited UTF-8 splitter (STDIO-FRAME-2, STDIO-LIMIT-1).
|
|
28
|
+
* Handles multi-byte UTF-8 sequences split across arbitrary chunk boundaries, trims
|
|
29
|
+
* trailing `\r` from `\r\n`, detects invalid UTF-8 (EDGE-11), and enforces `maxLineBytes`.
|
|
30
|
+
*/
|
|
31
|
+
export declare class LineSplitter {
|
|
32
|
+
private readonly maxLineBytes;
|
|
33
|
+
private readonly decoder;
|
|
34
|
+
private chunks;
|
|
35
|
+
private bufferedBytes;
|
|
36
|
+
private failedOversize;
|
|
37
|
+
constructor(maxLineBytes?: number);
|
|
38
|
+
get pendingBytes(): number;
|
|
39
|
+
push(chunk: Uint8Array): LineSplitterEvent[];
|
|
40
|
+
/**
|
|
41
|
+
* Returns any buffered partial line bytes on EOF as a lossy UTF-8 preview (for crash diagnostics).
|
|
42
|
+
*/
|
|
43
|
+
flushRemainderPreview(maxPreviewBytes?: number): {
|
|
44
|
+
readonly byteLength: number;
|
|
45
|
+
readonly preview: string;
|
|
46
|
+
} | undefined;
|
|
47
|
+
reset(): void;
|
|
48
|
+
private concatWith;
|
|
49
|
+
private decodeLine;
|
|
50
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { assertOutboundRequest, JSONRPC_VERSION, } from "../core/index.js";
|
|
2
|
+
export const DEFAULT_MAX_LINE_BYTES = 16 * 1024 * 1024; // 16 MiB (STDIO-LIMIT-1)
|
|
3
|
+
/**
|
|
4
|
+
* Validates that an outbound notification is a valid JSON-RPC 2.0 notification
|
|
5
|
+
* (never a response envelope, WIRE-DIR-1 / STDIO-STDIN-1).
|
|
6
|
+
*/
|
|
7
|
+
export function assertOutboundNotification(msg) {
|
|
8
|
+
if (typeof msg !== "object" || msg === null || Array.isArray(msg)) {
|
|
9
|
+
throw new TypeError("Outbound JSON-RPC notification must be an object");
|
|
10
|
+
}
|
|
11
|
+
const rec = msg;
|
|
12
|
+
if ("result" in rec || "error" in rec) {
|
|
13
|
+
throw new TypeError("Clients MUST NOT send JSON-RPC response envelopes (WIRE-DIR-1)");
|
|
14
|
+
}
|
|
15
|
+
if ("id" in rec) {
|
|
16
|
+
throw new TypeError("Outbound JSON-RPC notification must not carry an id");
|
|
17
|
+
}
|
|
18
|
+
if (rec["jsonrpc"] !== JSONRPC_VERSION) {
|
|
19
|
+
throw new TypeError("Outbound notification jsonrpc must be '2.0'");
|
|
20
|
+
}
|
|
21
|
+
if (typeof rec["method"] !== "string" || rec["method"].length === 0) {
|
|
22
|
+
throw new TypeError("Outbound notification method must be a non-empty string");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Encodes an outbound JSON-RPC request or notification as a single UTF-8 line
|
|
27
|
+
* terminated by `\n`, asserting no embedded raw newline or carriage return (STDIO-FRAME-1).
|
|
28
|
+
*/
|
|
29
|
+
export function encodeStdioMessage(msg) {
|
|
30
|
+
if ("id" in msg) {
|
|
31
|
+
assertOutboundRequest(msg);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
assertOutboundNotification(msg);
|
|
35
|
+
}
|
|
36
|
+
const json = JSON.stringify(msg);
|
|
37
|
+
if (json.includes("\n") || json.includes("\r")) {
|
|
38
|
+
throw new Error("Encoded JSON-RPC message must not contain embedded CR/LF (STDIO-FRAME-1)");
|
|
39
|
+
}
|
|
40
|
+
return `${json}\n`;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Incremental byte-safe newline-delimited UTF-8 splitter (STDIO-FRAME-2, STDIO-LIMIT-1).
|
|
44
|
+
* Handles multi-byte UTF-8 sequences split across arbitrary chunk boundaries, trims
|
|
45
|
+
* trailing `\r` from `\r\n`, detects invalid UTF-8 (EDGE-11), and enforces `maxLineBytes`.
|
|
46
|
+
*/
|
|
47
|
+
export class LineSplitter {
|
|
48
|
+
maxLineBytes;
|
|
49
|
+
decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
50
|
+
chunks = [];
|
|
51
|
+
bufferedBytes = 0;
|
|
52
|
+
failedOversize = false;
|
|
53
|
+
constructor(maxLineBytes = DEFAULT_MAX_LINE_BYTES) {
|
|
54
|
+
if (!Number.isInteger(maxLineBytes) || maxLineBytes <= 0) {
|
|
55
|
+
throw new RangeError("maxLineBytes must be a positive integer");
|
|
56
|
+
}
|
|
57
|
+
this.maxLineBytes = maxLineBytes;
|
|
58
|
+
}
|
|
59
|
+
get pendingBytes() {
|
|
60
|
+
return this.bufferedBytes;
|
|
61
|
+
}
|
|
62
|
+
push(chunk) {
|
|
63
|
+
if (this.failedOversize || chunk.byteLength === 0) {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
const events = [];
|
|
67
|
+
let start = 0;
|
|
68
|
+
for (let i = 0; i < chunk.byteLength; i++) {
|
|
69
|
+
if (chunk[i] === 0x0a) {
|
|
70
|
+
// Complete line from start..i (excluding 0x0a)
|
|
71
|
+
const slice = chunk.subarray(start, i);
|
|
72
|
+
const lineByteLength = this.bufferedBytes + slice.byteLength;
|
|
73
|
+
if (lineByteLength > this.maxLineBytes) {
|
|
74
|
+
this.failedOversize = true;
|
|
75
|
+
this.chunks = [];
|
|
76
|
+
this.bufferedBytes = 0;
|
|
77
|
+
events.push({ kind: "oversize", byteLength: lineByteLength, maxLineBytes: this.maxLineBytes });
|
|
78
|
+
return events;
|
|
79
|
+
}
|
|
80
|
+
const combined = this.concatWith(slice, lineByteLength);
|
|
81
|
+
this.chunks = [];
|
|
82
|
+
this.bufferedBytes = 0;
|
|
83
|
+
events.push(this.decodeLine(combined));
|
|
84
|
+
start = i + 1;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (start < chunk.byteLength) {
|
|
88
|
+
const tail = chunk.subarray(start);
|
|
89
|
+
const nextTotal = this.bufferedBytes + tail.byteLength;
|
|
90
|
+
if (nextTotal > this.maxLineBytes) {
|
|
91
|
+
this.failedOversize = true;
|
|
92
|
+
this.chunks = [];
|
|
93
|
+
this.bufferedBytes = 0;
|
|
94
|
+
events.push({ kind: "oversize", byteLength: nextTotal, maxLineBytes: this.maxLineBytes });
|
|
95
|
+
return events;
|
|
96
|
+
}
|
|
97
|
+
// Copy tail to avoid retaining oversized parent slabs
|
|
98
|
+
const copy = new Uint8Array(tail.byteLength);
|
|
99
|
+
copy.set(tail);
|
|
100
|
+
this.chunks.push(copy);
|
|
101
|
+
this.bufferedBytes = nextTotal;
|
|
102
|
+
}
|
|
103
|
+
return events;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Returns any buffered partial line bytes on EOF as a lossy UTF-8 preview (for crash diagnostics).
|
|
107
|
+
*/
|
|
108
|
+
flushRemainderPreview(maxPreviewBytes = 256) {
|
|
109
|
+
if (this.bufferedBytes === 0)
|
|
110
|
+
return undefined;
|
|
111
|
+
const total = this.bufferedBytes;
|
|
112
|
+
const combined = this.concatWith(new Uint8Array(0), total);
|
|
113
|
+
this.chunks = [];
|
|
114
|
+
this.bufferedBytes = 0;
|
|
115
|
+
const sliced = combined.byteLength > maxPreviewBytes ? combined.subarray(0, maxPreviewBytes) : combined;
|
|
116
|
+
const preview = new TextDecoder("utf-8", { fatal: false }).decode(sliced);
|
|
117
|
+
return { byteLength: total, preview };
|
|
118
|
+
}
|
|
119
|
+
reset() {
|
|
120
|
+
this.chunks = [];
|
|
121
|
+
this.bufferedBytes = 0;
|
|
122
|
+
this.failedOversize = false;
|
|
123
|
+
}
|
|
124
|
+
concatWith(tail, totalLength) {
|
|
125
|
+
if (this.chunks.length === 0) {
|
|
126
|
+
return tail;
|
|
127
|
+
}
|
|
128
|
+
const out = new Uint8Array(totalLength);
|
|
129
|
+
let offset = 0;
|
|
130
|
+
for (const c of this.chunks) {
|
|
131
|
+
out.set(c, offset);
|
|
132
|
+
offset += c.byteLength;
|
|
133
|
+
}
|
|
134
|
+
if (tail.byteLength > 0) {
|
|
135
|
+
out.set(tail, offset);
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
decodeLine(raw) {
|
|
140
|
+
let view = raw;
|
|
141
|
+
if (view.byteLength > 0 && view[view.byteLength - 1] === 0x0d) {
|
|
142
|
+
view = view.subarray(0, view.byteLength - 1);
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
const text = this.decoder.decode(view);
|
|
146
|
+
return { kind: "line", text, byteLength: raw.byteLength };
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
const sliced = view.byteLength > 256 ? view.subarray(0, 256) : view;
|
|
150
|
+
const preview = new TextDecoder("utf-8", { fatal: false }).decode(sliced);
|
|
151
|
+
return { kind: "invalid-utf8", byteLength: raw.byteLength, preview };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare const DEFAULT_STDERR_MAX_BYTES: number;
|
|
2
|
+
export declare const DEFAULT_STDERR_MAX_LINE_BYTES: number;
|
|
3
|
+
/**
|
|
4
|
+
* Bounded stderr ring buffer with per-line cap (STDIO-STDERR-1, STDIO-STDERR-2).
|
|
5
|
+
* Never treats stderr as a protocol error.
|
|
6
|
+
*/
|
|
7
|
+
export declare class StderrRingBuffer {
|
|
8
|
+
private readonly maxTotalBytes;
|
|
9
|
+
private readonly maxLineBytes;
|
|
10
|
+
private readonly onLine?;
|
|
11
|
+
private readonly decoder;
|
|
12
|
+
private readonly entries;
|
|
13
|
+
private totalBytes;
|
|
14
|
+
private pendingChunks;
|
|
15
|
+
private pendingBytes;
|
|
16
|
+
private truncated;
|
|
17
|
+
constructor(opts?: {
|
|
18
|
+
readonly maxTotalBytes?: number;
|
|
19
|
+
readonly maxLineBytes?: number;
|
|
20
|
+
readonly onLine?: (line: string) => void;
|
|
21
|
+
});
|
|
22
|
+
push(chunk: Uint8Array): void;
|
|
23
|
+
flush(): void;
|
|
24
|
+
snapshot(): readonly string[];
|
|
25
|
+
get byteLength(): number;
|
|
26
|
+
private emitBufferedLine;
|
|
27
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
export const DEFAULT_STDERR_MAX_BYTES = 64 * 1024; // 64 KiB
|
|
2
|
+
export const DEFAULT_STDERR_MAX_LINE_BYTES = 4 * 1024; // 4 KiB
|
|
3
|
+
/**
|
|
4
|
+
* Bounded stderr ring buffer with per-line cap (STDIO-STDERR-1, STDIO-STDERR-2).
|
|
5
|
+
* Never treats stderr as a protocol error.
|
|
6
|
+
*/
|
|
7
|
+
export class StderrRingBuffer {
|
|
8
|
+
maxTotalBytes;
|
|
9
|
+
maxLineBytes;
|
|
10
|
+
onLine;
|
|
11
|
+
decoder = new TextDecoder("utf-8", { fatal: false });
|
|
12
|
+
entries = [];
|
|
13
|
+
totalBytes = 0;
|
|
14
|
+
pendingChunks = [];
|
|
15
|
+
pendingBytes = 0;
|
|
16
|
+
truncated = false;
|
|
17
|
+
constructor(opts) {
|
|
18
|
+
this.maxTotalBytes = opts?.maxTotalBytes ?? DEFAULT_STDERR_MAX_BYTES;
|
|
19
|
+
this.maxLineBytes = opts?.maxLineBytes ?? DEFAULT_STDERR_MAX_LINE_BYTES;
|
|
20
|
+
if (!Number.isSafeInteger(this.maxTotalBytes) || this.maxTotalBytes < 0 ||
|
|
21
|
+
!Number.isSafeInteger(this.maxLineBytes) || this.maxLineBytes < 1) {
|
|
22
|
+
throw new RangeError("stderr limits require a nonnegative total and positive line cap");
|
|
23
|
+
}
|
|
24
|
+
if (opts?.onLine) {
|
|
25
|
+
this.onLine = opts.onLine;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
push(chunk) {
|
|
29
|
+
if (chunk.byteLength === 0)
|
|
30
|
+
return;
|
|
31
|
+
let start = 0;
|
|
32
|
+
for (let i = 0; i < chunk.byteLength; i++) {
|
|
33
|
+
if (chunk[i] === 0x0a) {
|
|
34
|
+
const part = chunk.subarray(start, i);
|
|
35
|
+
this.emitBufferedLine(part);
|
|
36
|
+
start = i + 1;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (start < chunk.byteLength) {
|
|
40
|
+
const tail = chunk.subarray(start);
|
|
41
|
+
this.truncated ||= this.pendingBytes + tail.byteLength > this.maxLineBytes;
|
|
42
|
+
if (this.pendingBytes < this.maxLineBytes) {
|
|
43
|
+
const take = Math.min(tail.byteLength, this.maxLineBytes - this.pendingBytes);
|
|
44
|
+
const copy = new Uint8Array(take);
|
|
45
|
+
copy.set(tail.subarray(0, take));
|
|
46
|
+
this.pendingChunks.push(copy);
|
|
47
|
+
this.pendingBytes += take;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
flush() {
|
|
52
|
+
if (this.pendingBytes > 0) {
|
|
53
|
+
this.emitBufferedLine(new Uint8Array(0));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
snapshot() {
|
|
57
|
+
return this.entries.map((e) => e.line);
|
|
58
|
+
}
|
|
59
|
+
get byteLength() {
|
|
60
|
+
return this.totalBytes;
|
|
61
|
+
}
|
|
62
|
+
emitBufferedLine(tail) {
|
|
63
|
+
const cap = this.maxLineBytes;
|
|
64
|
+
const rawTotal = this.pendingBytes + tail.byteLength;
|
|
65
|
+
const truncated = this.truncated || rawTotal > cap;
|
|
66
|
+
this.truncated = false;
|
|
67
|
+
const takeLen = Math.min(rawTotal, cap);
|
|
68
|
+
const buf = new Uint8Array(takeLen);
|
|
69
|
+
let offset = 0;
|
|
70
|
+
for (const c of this.pendingChunks) {
|
|
71
|
+
const n = Math.min(c.byteLength, takeLen - offset);
|
|
72
|
+
if (n <= 0)
|
|
73
|
+
break;
|
|
74
|
+
buf.set(c.subarray(0, n), offset);
|
|
75
|
+
offset += n;
|
|
76
|
+
}
|
|
77
|
+
if (offset < takeLen && tail.byteLength > 0) {
|
|
78
|
+
const n = Math.min(tail.byteLength, takeLen - offset);
|
|
79
|
+
buf.set(tail.subarray(0, n), offset);
|
|
80
|
+
offset += n;
|
|
81
|
+
}
|
|
82
|
+
this.pendingChunks = [];
|
|
83
|
+
this.pendingBytes = 0;
|
|
84
|
+
let view = buf;
|
|
85
|
+
if (!truncated && view.byteLength > 0 && view[view.byteLength - 1] === 0x0d) {
|
|
86
|
+
view = view.subarray(0, view.byteLength - 1);
|
|
87
|
+
}
|
|
88
|
+
let text = this.decoder.decode(view, { stream: truncated });
|
|
89
|
+
this.decoder.decode(); // Drop any partial code point at the clipping boundary.
|
|
90
|
+
if (Buffer.byteLength(text, "utf8") > cap) {
|
|
91
|
+
// Lossy decoding of malformed UTF-8 can expand the retained bytes.
|
|
92
|
+
const bytes = Buffer.from(text, "utf8");
|
|
93
|
+
text = this.decoder.decode(bytes.subarray(0, cap), { stream: true });
|
|
94
|
+
this.decoder.decode();
|
|
95
|
+
}
|
|
96
|
+
const storedBytes = Math.max(1, Buffer.byteLength(text, "utf8"));
|
|
97
|
+
this.entries.push({ line: text, bytes: storedBytes });
|
|
98
|
+
this.totalBytes += storedBytes;
|
|
99
|
+
while (this.totalBytes > this.maxTotalBytes && this.entries.length > 0) {
|
|
100
|
+
const evicted = this.entries.shift();
|
|
101
|
+
this.totalBytes -= evicted.bytes;
|
|
102
|
+
}
|
|
103
|
+
this.onLine?.(text);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { isReadOnlyMethod, type Clock, type Exchange, type JsonRpcNotification, type JsonRpcRequest, type RequestId, type Transport, type TransportCall, type TransportDiagnostic } from "../core/index.js";
|
|
2
|
+
import { type StdioWritable } from "./write-queue.js";
|
|
3
|
+
export declare const META_PROGRESS_TOKEN: "io.modelcontextprotocol/progressToken";
|
|
4
|
+
export declare const META_SUBSCRIPTION_ID: "io.modelcontextprotocol/subscriptionId";
|
|
5
|
+
export declare const DEFAULT_MAX_NOISE_LINES = 100;
|
|
6
|
+
export declare const DEFAULT_MAX_NOISE_BYTES: number;
|
|
7
|
+
export declare const DEFAULT_GRACE_MS = 2000;
|
|
8
|
+
export declare const DEFAULT_TERM_MS = 2000;
|
|
9
|
+
export declare const DEFAULT_MAX_RESTARTS = 3;
|
|
10
|
+
export declare const DEFAULT_RESTART_WINDOW_MS = 60000;
|
|
11
|
+
export declare const DEFAULT_RESTART_BACKOFF_MS = 100;
|
|
12
|
+
export { isReadOnlyMethod };
|
|
13
|
+
export type StdioTransportState = "idle" | "spawning" | "ready" | "closing" | "closed" | "failed";
|
|
14
|
+
export interface StdioRestartPolicy {
|
|
15
|
+
readonly policy: "on-demand" | "auto";
|
|
16
|
+
readonly maxRestarts?: number;
|
|
17
|
+
readonly windowMs?: number;
|
|
18
|
+
readonly backoffMs?: number;
|
|
19
|
+
}
|
|
20
|
+
export interface StdioSpawnOptions {
|
|
21
|
+
readonly stdio: ["pipe", "pipe", "pipe"];
|
|
22
|
+
readonly shell: false;
|
|
23
|
+
readonly windowsHide: true;
|
|
24
|
+
readonly cwd?: string;
|
|
25
|
+
readonly env: Record<string, string>;
|
|
26
|
+
}
|
|
27
|
+
export interface StdioReadableStream {
|
|
28
|
+
on(event: "data", listener: (chunk: Uint8Array) => void): unknown;
|
|
29
|
+
on(event: "error", listener: (err: Error) => void): unknown;
|
|
30
|
+
once(event: "end", listener: () => void): unknown;
|
|
31
|
+
}
|
|
32
|
+
export interface StdioWritableStream extends StdioWritable {
|
|
33
|
+
end(): unknown;
|
|
34
|
+
on(event: "error", listener: (err: Error) => void): unknown;
|
|
35
|
+
}
|
|
36
|
+
export interface StdioChildProcess {
|
|
37
|
+
readonly pid?: number | undefined;
|
|
38
|
+
readonly exitCode: number | null;
|
|
39
|
+
readonly signalCode: string | null;
|
|
40
|
+
readonly stdin: StdioWritableStream | null;
|
|
41
|
+
readonly stdout: StdioReadableStream | null;
|
|
42
|
+
readonly stderr: StdioReadableStream | null;
|
|
43
|
+
once(event: "spawn", listener: () => void): unknown;
|
|
44
|
+
once(event: "close", listener: (code: number | null, signal: string | null) => void): unknown;
|
|
45
|
+
on(event: "error", listener: (err: Error & {
|
|
46
|
+
code?: string;
|
|
47
|
+
}) => void): unknown;
|
|
48
|
+
kill(signal?: "SIGTERM" | "SIGKILL"): boolean;
|
|
49
|
+
}
|
|
50
|
+
export type StdioSpawnFn = (command: string, args: readonly string[], options: StdioSpawnOptions) => StdioChildProcess;
|
|
51
|
+
export interface StdioTransportOptions {
|
|
52
|
+
readonly command: string;
|
|
53
|
+
readonly args?: readonly string[];
|
|
54
|
+
readonly cwd?: string;
|
|
55
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
56
|
+
/** Explicit allow-list of keys to inherit from process.env; defaults to minimal PATH resolution keys. */
|
|
57
|
+
readonly inheritEnv?: readonly string[];
|
|
58
|
+
readonly maxLineBytes?: number;
|
|
59
|
+
readonly maxNoiseLines?: number;
|
|
60
|
+
readonly maxNoiseBytes?: number;
|
|
61
|
+
readonly stderrMaxBytes?: number;
|
|
62
|
+
readonly stderrMaxLineBytes?: number;
|
|
63
|
+
readonly onStderr?: (line: string) => void;
|
|
64
|
+
readonly graceMs?: number;
|
|
65
|
+
readonly termMs?: number;
|
|
66
|
+
readonly restart?: StdioRestartPolicy;
|
|
67
|
+
readonly clock?: Clock;
|
|
68
|
+
readonly maxDiagnostics?: number;
|
|
69
|
+
/** Optional spawn override for unit testing launch parameters (STDIO-LAUNCH-1). */
|
|
70
|
+
readonly spawnFn?: StdioSpawnFn;
|
|
71
|
+
}
|
|
72
|
+
export declare class StdioTransport implements Transport {
|
|
73
|
+
readonly binding: "stdio";
|
|
74
|
+
private readonly command;
|
|
75
|
+
private readonly args;
|
|
76
|
+
private readonly cwd?;
|
|
77
|
+
private readonly childEnv;
|
|
78
|
+
private readonly maxLineBytes;
|
|
79
|
+
private readonly maxNoiseLines;
|
|
80
|
+
private readonly maxNoiseBytes;
|
|
81
|
+
private readonly graceMs;
|
|
82
|
+
private readonly termMs;
|
|
83
|
+
private readonly restartPolicy;
|
|
84
|
+
private readonly maxRestarts;
|
|
85
|
+
private readonly restartWindowMs;
|
|
86
|
+
private readonly restartBackoffMs;
|
|
87
|
+
private readonly clock;
|
|
88
|
+
private readonly spawnFn;
|
|
89
|
+
private readonly diagChannel;
|
|
90
|
+
private readonly stderrRing;
|
|
91
|
+
private readonly writeQueue;
|
|
92
|
+
private readonly splitter;
|
|
93
|
+
private readonly pending;
|
|
94
|
+
private readonly progressMap;
|
|
95
|
+
private readonly subscriptionListeners;
|
|
96
|
+
private readonly settledIds;
|
|
97
|
+
private readonly settledIdSet;
|
|
98
|
+
private _state;
|
|
99
|
+
private run;
|
|
100
|
+
private closing;
|
|
101
|
+
private hasSpawnedOnce;
|
|
102
|
+
private readonly restartTimestamps;
|
|
103
|
+
private cancelRestartTimer;
|
|
104
|
+
private noiseLines;
|
|
105
|
+
private noiseBytes;
|
|
106
|
+
private fatalFailure;
|
|
107
|
+
constructor(opts: StdioTransportOptions);
|
|
108
|
+
get state(): StdioTransportState;
|
|
109
|
+
get pid(): number | undefined;
|
|
110
|
+
get diagnostics(): AsyncIterable<TransportDiagnostic>;
|
|
111
|
+
stderrSnapshot(): readonly string[];
|
|
112
|
+
diagnosticsSnapshot(): readonly TransportDiagnostic[];
|
|
113
|
+
/**
|
|
114
|
+
* Registers an external listener for notifications matching `io.modelcontextprotocol/subscriptionId` (STDIO-ROUTE-2).
|
|
115
|
+
*/
|
|
116
|
+
registerSubscription(subscriptionId: RequestId, listener: (n: JsonRpcNotification) => void): () => void;
|
|
117
|
+
start(msg: JsonRpcRequest, ctx: TransportCall): Exchange;
|
|
118
|
+
notify(msg: JsonRpcNotification, ctx: TransportCall): Promise<void>;
|
|
119
|
+
close(opts?: {
|
|
120
|
+
readonly graceMs?: number;
|
|
121
|
+
readonly termMs?: number;
|
|
122
|
+
}): Promise<void>;
|
|
123
|
+
private closeTransport;
|
|
124
|
+
private ensureStarted;
|
|
125
|
+
private handleStdoutChunk;
|
|
126
|
+
private handleInboundLine;
|
|
127
|
+
private routeNotification;
|
|
128
|
+
private recordNoise;
|
|
129
|
+
private handleRequestAbort;
|
|
130
|
+
private disconnect;
|
|
131
|
+
private handleUnexpectedClose;
|
|
132
|
+
private failTransportAndKill;
|
|
133
|
+
private retireChild;
|
|
134
|
+
private stopChild;
|
|
135
|
+
private shutdownChildLadder;
|
|
136
|
+
private settlePending;
|
|
137
|
+
private settleAllPending;
|
|
138
|
+
private deliveryForWriteState;
|
|
139
|
+
private recordSettledId;
|
|
140
|
+
private pruneRestartWindow;
|
|
141
|
+
private emitDiag;
|
|
142
|
+
}
|