pi-charset 0.1.7 → 0.1.8
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/CHANGELOG.md +10 -0
- package/CHANGELOG.zh-CN.md +10 -0
- package/dist/diagnostics/incidents.js +436 -0
- package/dist/edit/diagnostics.js +27 -0
- package/dist/faults.js +67 -0
- package/dist/mcp/server.js +138 -45
- package/dist/operations.js +169 -35
- package/dist/setup/adapters.js +524 -0
- package/dist/version.js +1 -1
- package/docs/MCP-AND-MULTI-AGENT.md +17 -2
- package/docs/zh-CN/MCP-AND-MULTI-AGENT.md +17 -2
- package/package.json +1 -1
- package/src/diagnostics/incidents.ts +474 -0
- package/src/edit/diagnostics.ts +31 -0
- package/src/faults.ts +165 -0
- package/src/mcp/server.ts +191 -45
- package/src/operations.ts +178 -37
- package/src/setup/adapters.ts +613 -0
- package/src/version.ts +1 -1
- package/tools/pi-charset-cli.mjs +232 -22
- package/tools/pi-charset-mcp.mjs +3 -1
package/src/mcp/server.ts
CHANGED
|
@@ -6,10 +6,12 @@ import { once } from "node:events";
|
|
|
6
6
|
import { mkdir } from "node:fs/promises";
|
|
7
7
|
import * as path from "node:path";
|
|
8
8
|
import { findNearestConfig, getRuntimeDefaults } from "../config";
|
|
9
|
+
import { getIncidentRecorder, type IncidentRecorder } from "../diagnostics/incidents";
|
|
9
10
|
import { containsNul } from "../encoding/classify";
|
|
10
11
|
import { detectSupportedImageMimeType } from "../encoding/mime";
|
|
11
12
|
import { compactRangeDiff, executeApplyPatch } from "../edit/apply-patch-core";
|
|
12
13
|
import { EditError } from "../edit/diagnostics";
|
|
14
|
+
import { CharsetFault, isCharsetFault, REPORTABLE_FAULT_CODES, toMcpErrorEnvelope, type FaultMetadata } from "../faults";
|
|
13
15
|
import { formatUpdatedHashlines, planHashlineEdits, type HashlineOp } from "../edit/hashline";
|
|
14
16
|
import { planPatchEdits } from "../edit/patch";
|
|
15
17
|
import { resolveEditPath, resolveReadPathCompat } from "../edit/paths";
|
|
@@ -47,13 +49,70 @@ const MCP_INSTRUCTIONS =
|
|
|
47
49
|
"for all text file operations to preserve exact encodings (GBK/GB18030/Big5/Shift_JIS/EUC-KR/UTF-16/UTF-8), BOM, and CRLF/LF line endings. " +
|
|
48
50
|
"When multiple agents share a workspace, pass the `version` returned by charset_read as `expectedVersion` to charset_write/charset_edit.";
|
|
49
51
|
|
|
50
|
-
class InputValidationError extends
|
|
52
|
+
class InputValidationError extends CharsetFault {
|
|
51
53
|
constructor(message: string) {
|
|
52
|
-
super(`[INVALID_INPUT; not_applied] ${message}
|
|
54
|
+
super(`[INVALID_INPUT; not_applied] ${message}`, {
|
|
55
|
+
code: "INVALID_INPUT",
|
|
56
|
+
category: "input",
|
|
57
|
+
phase: "validate",
|
|
58
|
+
outcome: "not_applied",
|
|
59
|
+
retryable: false,
|
|
60
|
+
actionHint: "Fix the tool input parameters and retry.",
|
|
61
|
+
});
|
|
53
62
|
this.name = "InputValidationError";
|
|
54
63
|
}
|
|
55
64
|
}
|
|
56
65
|
|
|
66
|
+
function mapClientName(raw: unknown): string {
|
|
67
|
+
if (typeof raw !== "string") return "generic";
|
|
68
|
+
const s = raw.trim().toLowerCase();
|
|
69
|
+
if (s.includes("codex")) return "codex";
|
|
70
|
+
if (s.includes("antigravity-ide")) return "antigravity-ide";
|
|
71
|
+
if (s.includes("antigravity-app")) return "antigravity-app";
|
|
72
|
+
if (s.includes("antigravity") || s === "agy") return "agy";
|
|
73
|
+
if (s.includes("claude")) return "claude";
|
|
74
|
+
if (s.includes("cursor")) return "cursor";
|
|
75
|
+
if (s.includes("windsurf")) return "windsurf";
|
|
76
|
+
if (s === "pi") return "pi";
|
|
77
|
+
return "generic";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function toolToOperation(toolName: string): string {
|
|
81
|
+
switch (toolName) {
|
|
82
|
+
case "charset_read": return "read";
|
|
83
|
+
case "charset_write": return "write";
|
|
84
|
+
case "charset_edit": return "edit";
|
|
85
|
+
case "charset_apply_patch": return "apply_patch";
|
|
86
|
+
case "charset_grep": return "grep";
|
|
87
|
+
case "charset_check": return "check";
|
|
88
|
+
case "charset_fix": return "fix";
|
|
89
|
+
default: return "unknown";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function faultFromCaught(err: unknown): FaultMetadata {
|
|
94
|
+
if (isCharsetFault(err)) return err.fault;
|
|
95
|
+
if (err instanceof EditError) return err.toFaultMetadata();
|
|
96
|
+
const code = (err as NodeJS.ErrnoException | undefined)?.code;
|
|
97
|
+
if (code === "ENOENT") {
|
|
98
|
+
return { code: "NOT_FOUND", category: "io", phase: "detect", outcome: "not_applied", retryable: false, actionHint: "Check the file path and retry." };
|
|
99
|
+
}
|
|
100
|
+
if (code === "EACCES" || code === "EPERM") {
|
|
101
|
+
return { code: "PERMISSION_DENIED", category: "io", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Check file permissions and retry." };
|
|
102
|
+
}
|
|
103
|
+
if (code === "EBUSY") {
|
|
104
|
+
return { code: "LOCK_BUSY", category: "io", phase: "commit", retryable: true, outcome: "not_applied", actionHint: "Close external editors locking the file and retry." };
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
code: "INTERNAL",
|
|
108
|
+
category: "engine",
|
|
109
|
+
phase: "unknown",
|
|
110
|
+
outcome: "unknown",
|
|
111
|
+
retryable: false,
|
|
112
|
+
actionHint: "Re-read the target file to verify on-disk state; run `pi-charset feedback list` to inspect sanitized local diagnostics.",
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
57
116
|
function coercePathArg(args: Record<string, unknown>): string | undefined {
|
|
58
117
|
const raw = typeof args.path === "string" ? args.path : typeof args.file_path === "string" ? args.file_path : undefined;
|
|
59
118
|
return raw && raw.trim() ? raw : undefined;
|
|
@@ -136,6 +195,7 @@ export const MCP_TOOLS = [
|
|
|
136
195
|
name: "charset_read",
|
|
137
196
|
description:
|
|
138
197
|
"Read a text file preserving exact encoding/BOM/EOL (GBK, GB18030, Big5, Shift_JIS, EUC-KR, UTF-16, UTF-8) and return its SHA-1 version for safe edits. Supports outline=true, ranges=[[start,end],...], offset/limit, and hashlines=true. Output is strictly bounded.",
|
|
198
|
+
_meta: { "antigravity/alwaysLoad": true },
|
|
139
199
|
inputSchema: {
|
|
140
200
|
type: "object",
|
|
141
201
|
additionalProperties: false,
|
|
@@ -179,6 +239,7 @@ export const MCP_TOOLS = [
|
|
|
179
239
|
name: "charset_edit",
|
|
180
240
|
description:
|
|
181
241
|
"Edit a file atomically while preserving its exact encoding, BOM, line endings, and untouched bytes. Supply exactly one of: edits ([{oldText, newText}]), hashlines (anchored replace/delete/insert_after/insert_before), patch (unified/Codex/Aider diff), or wholeFile.",
|
|
242
|
+
_meta: { "antigravity/alwaysLoad": true },
|
|
182
243
|
inputSchema: {
|
|
183
244
|
type: "object",
|
|
184
245
|
additionalProperties: false,
|
|
@@ -228,6 +289,7 @@ export const MCP_TOOLS = [
|
|
|
228
289
|
name: "charset_grep",
|
|
229
290
|
description:
|
|
230
291
|
"Search files across UTF-8, GBK, GB18030, Big5, Shift_JIS, EUC-KR, EUC-JP, and UTF-16 using encoding-verified multi-pass ripgrep. Supports filesOnly=true and hashlines=true. Propagates ripgrep errors explicitly.",
|
|
292
|
+
_meta: { "antigravity/alwaysLoad": true },
|
|
231
293
|
inputSchema: {
|
|
232
294
|
type: "object",
|
|
233
295
|
additionalProperties: false,
|
|
@@ -314,7 +376,35 @@ function validateRpcEnvelope(
|
|
|
314
376
|
return { ok: true, req: obj as unknown as JsonRpcRequest };
|
|
315
377
|
}
|
|
316
378
|
|
|
317
|
-
export function createMcpDispatcher(
|
|
379
|
+
export function createMcpDispatcher(
|
|
380
|
+
defaultCwd: string,
|
|
381
|
+
scope: ReadScope = createReadScope(),
|
|
382
|
+
opts?: { client?: string; recorder?: IncidentRecorder },
|
|
383
|
+
) {
|
|
384
|
+
let activeClient = mapClientName(opts?.client);
|
|
385
|
+
const recordFault = (fault: FaultMetadata, operation: string) => {
|
|
386
|
+
if (!REPORTABLE_FAULT_CODES.has(fault.code)) return;
|
|
387
|
+
const rec = opts?.recorder ?? getIncidentRecorder(activeClient);
|
|
388
|
+
rec.record({
|
|
389
|
+
code: fault.code,
|
|
390
|
+
operation,
|
|
391
|
+
phase: fault.phase,
|
|
392
|
+
outcome: fault.outcome,
|
|
393
|
+
encoding: fault.encoding,
|
|
394
|
+
eol: fault.eol,
|
|
395
|
+
fileBytes: fault.fileBytes,
|
|
396
|
+
});
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const failTool = (message: string, fault: FaultMetadata, operation: string): ToolCallOutcome => {
|
|
400
|
+
recordFault(fault, operation);
|
|
401
|
+
return {
|
|
402
|
+
text: message,
|
|
403
|
+
isError: true,
|
|
404
|
+
structuredContent: toMcpErrorEnvelope(fault) as unknown as Record<string, unknown>,
|
|
405
|
+
};
|
|
406
|
+
};
|
|
407
|
+
|
|
318
408
|
const resolveCwd = (args: Record<string, unknown>) => {
|
|
319
409
|
const cwdArg = coerceOptionalString(args, "cwd");
|
|
320
410
|
return cwdArg && cwdArg.trim() ? path.resolve(defaultCwd, cwdArg) : defaultCwd;
|
|
@@ -326,7 +416,11 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
326
416
|
if (name === "charset_read") {
|
|
327
417
|
const pathArg = coercePathArg(args);
|
|
328
418
|
if (!pathArg) {
|
|
329
|
-
return
|
|
419
|
+
return failTool(
|
|
420
|
+
"[INVALID_INPUT; not_applied] path is required.",
|
|
421
|
+
{ code: "INVALID_INPUT", category: "input", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Pass path or file_path." },
|
|
422
|
+
"edit",
|
|
423
|
+
);
|
|
330
424
|
}
|
|
331
425
|
const outlineArg = coerceOptionalBoolean(args, "outline") ?? false;
|
|
332
426
|
const hashlinesOpt = coerceOptionalBoolean(args, "hashlines");
|
|
@@ -346,18 +440,18 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
346
440
|
);
|
|
347
441
|
const buf = await readOps.readFile(absPath);
|
|
348
442
|
if (detectSupportedImageMimeType(buf)) {
|
|
349
|
-
return
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
443
|
+
return failTool(
|
|
444
|
+
`[BINARY_IMAGE] ${absPath} (${buf.length}B) is an image file; use a binary/image reader.`,
|
|
445
|
+
{ code: "BINARY_FILE", category: "policy", phase: "detect", outcome: "not_applied", retryable: false, actionHint: "Use an image/binary reader for image files.", fileBytes: buf.length },
|
|
446
|
+
"read",
|
|
447
|
+
);
|
|
354
448
|
}
|
|
355
449
|
if (containsNul(buf)) {
|
|
356
|
-
return
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
450
|
+
return failTool(
|
|
451
|
+
`[BINARY_FILE] ${absPath} (${buf.length}B) contains NUL bytes and cannot be displayed as text.`,
|
|
452
|
+
{ code: "BINARY_FILE", category: "policy", phase: "detect", outcome: "not_applied", retryable: false, actionHint: "Binary files are refused by text tools.", fileBytes: buf.length },
|
|
453
|
+
"read",
|
|
454
|
+
);
|
|
361
455
|
}
|
|
362
456
|
if (scope.epoch === epoch && observed) scope.baselines.set(observed.key, observed.version);
|
|
363
457
|
|
|
@@ -420,7 +514,11 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
420
514
|
if (name === "charset_write") {
|
|
421
515
|
const pathArg = coercePathArg(args);
|
|
422
516
|
if (!pathArg || typeof args.content !== "string") {
|
|
423
|
-
return
|
|
517
|
+
return failTool(
|
|
518
|
+
"[INVALID_INPUT; not_applied] path and content are required.",
|
|
519
|
+
{ code: "INVALID_INPUT", category: "input", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Pass path (or file_path) and content." },
|
|
520
|
+
"write",
|
|
521
|
+
);
|
|
424
522
|
}
|
|
425
523
|
const expectedVersion = coerceOptionalString(args, "expectedVersion");
|
|
426
524
|
const absPath = resolveEditPath(pathArg, baseCwd);
|
|
@@ -436,7 +534,11 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
436
534
|
if (name === "charset_edit") {
|
|
437
535
|
const pathArg = coercePathArg(args);
|
|
438
536
|
if (!pathArg) {
|
|
439
|
-
return
|
|
537
|
+
return failTool(
|
|
538
|
+
"[INVALID_INPUT; not_applied] path is required.",
|
|
539
|
+
{ code: "INVALID_INPUT", category: "input", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Pass path or file_path." },
|
|
540
|
+
"edit",
|
|
541
|
+
);
|
|
440
542
|
}
|
|
441
543
|
const expectedVersion = coerceOptionalString(args, "expectedVersion");
|
|
442
544
|
const presentModes = [
|
|
@@ -446,10 +548,11 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
446
548
|
args.wholeFile !== undefined ? "wholeFile" : null,
|
|
447
549
|
].filter((m): m is "edits" | "hashlines" | "patch" | "wholeFile" => m !== null);
|
|
448
550
|
if (presentModes.length !== 1) {
|
|
449
|
-
return
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
551
|
+
return failTool(
|
|
552
|
+
"[INVALID_INPUT; not_applied] Supply exactly one of: edits, hashlines, patch, or wholeFile.",
|
|
553
|
+
{ code: "INVALID_INPUT", category: "input", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Pass exactly one edit payload: edits, hashlines, patch, or wholeFile." },
|
|
554
|
+
"edit",
|
|
555
|
+
);
|
|
453
556
|
}
|
|
454
557
|
const mode = presentModes[0];
|
|
455
558
|
const editsArg = mode === "edits" ? coerceArrayArg<EditReplacement>(args.edits, "edits", true) : undefined;
|
|
@@ -494,7 +597,11 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
494
597
|
|
|
495
598
|
if (name === "charset_apply_patch") {
|
|
496
599
|
if (typeof args.patch !== "string" || !args.patch.trim()) {
|
|
497
|
-
return
|
|
600
|
+
return failTool(
|
|
601
|
+
"[INVALID_INPUT; not_applied] patch is required.",
|
|
602
|
+
{ code: "INVALID_INPUT", category: "input", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Pass a non-empty patch string." },
|
|
603
|
+
"apply_patch",
|
|
604
|
+
);
|
|
498
605
|
}
|
|
499
606
|
const res = await executeApplyPatch(baseCwd, args.patch, scope, signal);
|
|
500
607
|
return {
|
|
@@ -513,7 +620,11 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
513
620
|
|
|
514
621
|
if (name === "charset_grep") {
|
|
515
622
|
if (typeof args.pattern !== "string" || !args.pattern) {
|
|
516
|
-
return
|
|
623
|
+
return failTool(
|
|
624
|
+
"[INVALID_INPUT; not_applied] pattern is required.",
|
|
625
|
+
{ code: "INVALID_INPUT", category: "input", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Pass a non-empty search pattern." },
|
|
626
|
+
"grep",
|
|
627
|
+
);
|
|
517
628
|
}
|
|
518
629
|
const ignoreCase = coerceOptionalBoolean(args, "ignoreCase") ?? false;
|
|
519
630
|
const literal = coerceOptionalBoolean(args, "literal") ?? false;
|
|
@@ -539,23 +650,21 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
539
650
|
signal,
|
|
540
651
|
);
|
|
541
652
|
if (!res) {
|
|
542
|
-
return
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
653
|
+
return failTool(
|
|
654
|
+
"[GREP_UNAVAILABLE] ripgrep (rg) was not found or search path is invalid.",
|
|
655
|
+
{ code: "SEARCH_ENGINE_FAILED", category: "compatibility", phase: "search", outcome: "not_applied", retryable: false, actionHint: "Run `pi-charset doctor` to verify ripgrep availability and search path." },
|
|
656
|
+
"grep",
|
|
657
|
+
);
|
|
547
658
|
}
|
|
548
659
|
if (res.status === "failed") {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
},
|
|
558
|
-
};
|
|
660
|
+
const isSyntax = /regex parse error/i.test(res.error ?? "");
|
|
661
|
+
return failTool(
|
|
662
|
+
res.text,
|
|
663
|
+
isSyntax
|
|
664
|
+
? { code: "INVALID_INPUT", category: "input", phase: "search", outcome: "not_applied", retryable: false, actionHint: "Fix the regular expression or pass literal=true." }
|
|
665
|
+
: { code: "SEARCH_ENGINE_FAILED", category: "compatibility", phase: "search", outcome: "not_applied", retryable: true, actionHint: "Check search pattern and ripgrep status via `pi-charset doctor`." },
|
|
666
|
+
"grep",
|
|
667
|
+
);
|
|
559
668
|
}
|
|
560
669
|
return {
|
|
561
670
|
text: res.text,
|
|
@@ -598,7 +707,11 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
598
707
|
if (action === "normalize_eol") {
|
|
599
708
|
const to = (toArg ?? "").toUpperCase();
|
|
600
709
|
if (!pathArg || (to !== "CRLF" && to !== "LF")) {
|
|
601
|
-
return
|
|
710
|
+
return failTool(
|
|
711
|
+
"[INVALID_INPUT; not_applied] normalize_eol requires path and to='CRLF'|'LF'.",
|
|
712
|
+
{ code: "INVALID_INPUT", category: "input", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Pass path and to='CRLF' or to='LF'." },
|
|
713
|
+
"fix",
|
|
714
|
+
);
|
|
602
715
|
}
|
|
603
716
|
const absPath = resolveEditPath(pathArg, baseCwd);
|
|
604
717
|
const res = await normalizeFileLineEndings(absPath, to, scope, dryRun);
|
|
@@ -610,7 +723,11 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
610
723
|
}
|
|
611
724
|
if (action === "convert") {
|
|
612
725
|
if (!pathArg || !toArg || !toArg.trim()) {
|
|
613
|
-
return
|
|
726
|
+
return failTool(
|
|
727
|
+
"[INVALID_INPUT; not_applied] convert requires path and to='<encoding>'.",
|
|
728
|
+
{ code: "INVALID_INPUT", category: "input", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Pass path and target encoding in to." },
|
|
729
|
+
"fix",
|
|
730
|
+
);
|
|
614
731
|
}
|
|
615
732
|
const absPath = resolveEditPath(pathArg, baseCwd);
|
|
616
733
|
const res = await convertFileEncoding(absPath, toArg, scope, { bom, dryRun });
|
|
@@ -636,7 +753,20 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
636
753
|
return {
|
|
637
754
|
text: res.reason,
|
|
638
755
|
isError: true,
|
|
639
|
-
structuredContent: {
|
|
756
|
+
structuredContent: {
|
|
757
|
+
...toMcpErrorEnvelope({
|
|
758
|
+
code: "CONFLICT",
|
|
759
|
+
category: "conflict",
|
|
760
|
+
phase: "rollback",
|
|
761
|
+
outcome: "not_applied",
|
|
762
|
+
retryable: false,
|
|
763
|
+
actionHint: "Inspect the file and session journal before retrying undo.",
|
|
764
|
+
}),
|
|
765
|
+
ok: false,
|
|
766
|
+
action: "undo",
|
|
767
|
+
dryRun,
|
|
768
|
+
reason: res.reason,
|
|
769
|
+
},
|
|
640
770
|
};
|
|
641
771
|
}
|
|
642
772
|
const items = res.entries ?? [{ entry: res.entry, action: res.action }];
|
|
@@ -663,10 +793,18 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
663
793
|
},
|
|
664
794
|
};
|
|
665
795
|
}
|
|
666
|
-
return
|
|
796
|
+
return failTool(
|
|
797
|
+
`[INVALID_INPUT; not_applied] Unknown action "${action}".`,
|
|
798
|
+
{ code: "INVALID_INPUT", category: "input", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Use action: normalize_eol, convert, auto_fix_eol, or undo." },
|
|
799
|
+
"fix",
|
|
800
|
+
);
|
|
667
801
|
}
|
|
668
802
|
|
|
669
|
-
return
|
|
803
|
+
return failTool(
|
|
804
|
+
`[INVALID_INPUT; not_applied] Unknown tool: ${name}`,
|
|
805
|
+
{ code: "INVALID_INPUT", category: "input", phase: "validate", outcome: "not_applied", retryable: false, actionHint: "Call tools/list to inspect available tool names." },
|
|
806
|
+
"unknown",
|
|
807
|
+
);
|
|
670
808
|
}
|
|
671
809
|
|
|
672
810
|
async function handleRequest(rawReq: unknown, signal?: AbortSignal): Promise<JsonRpcResponse | null> {
|
|
@@ -680,6 +818,10 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
680
818
|
return null;
|
|
681
819
|
}
|
|
682
820
|
if (req.method === "initialize") {
|
|
821
|
+
if (!opts?.client) {
|
|
822
|
+
const clientInfo = req.params?.clientInfo as Record<string, unknown> | undefined;
|
|
823
|
+
activeClient = mapClientName(clientInfo?.name);
|
|
824
|
+
}
|
|
683
825
|
const requested = typeof req.params?.protocolVersion === "string" ? req.params.protocolVersion : "2024-11-05";
|
|
684
826
|
const protocolVersion = SUPPORTED_PROTOCOLS.has(requested) ? requested : "2024-11-05";
|
|
685
827
|
return {
|
|
@@ -732,7 +874,9 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
732
874
|
},
|
|
733
875
|
};
|
|
734
876
|
} catch (err) {
|
|
735
|
-
const
|
|
877
|
+
const fault = faultFromCaught(err);
|
|
878
|
+
recordFault(fault, toolToOperation(toolName));
|
|
879
|
+
const msg = err instanceof EditError || err instanceof CharsetFault || err instanceof Error ? err.message : String(err);
|
|
736
880
|
return isNotification
|
|
737
881
|
? null
|
|
738
882
|
: {
|
|
@@ -740,6 +884,7 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
740
884
|
id: id ?? null,
|
|
741
885
|
result: {
|
|
742
886
|
content: [{ type: "text", text: msg }],
|
|
887
|
+
structuredContent: toMcpErrorEnvelope(fault),
|
|
743
888
|
isError: true,
|
|
744
889
|
},
|
|
745
890
|
};
|
|
@@ -757,8 +902,8 @@ export function createMcpDispatcher(defaultCwd: string, scope: ReadScope = creat
|
|
|
757
902
|
}
|
|
758
903
|
|
|
759
904
|
/** Run the MCP server over stdin/stdout (supports newline-delimited JSON-RPC and Content-Length framing, backpressure drain, cancellation, and error isolation). */
|
|
760
|
-
export function startMcpStdioServer(defaultCwd = process.cwd()): void {
|
|
761
|
-
const { handleRequest } = createMcpDispatcher(defaultCwd);
|
|
905
|
+
export function startMcpStdioServer(defaultCwd = process.cwd(), opts?: { client?: string }): void {
|
|
906
|
+
const { handleRequest } = createMcpDispatcher(defaultCwd, createReadScope(), { client: opts?.client });
|
|
762
907
|
let buf: Buffer = Buffer.alloc(0);
|
|
763
908
|
let useContentLength = false;
|
|
764
909
|
let chain = Promise.resolve();
|
|
@@ -916,6 +1061,7 @@ export function startMcpStdioServer(defaultCwd = process.cwd()): void {
|
|
|
916
1061
|
const rest = buf.toString("utf8").trim();
|
|
917
1062
|
if (rest) dispatchRaw(rest);
|
|
918
1063
|
void chain.finally(async () => {
|
|
1064
|
+
await getIncidentRecorder(mapClientName(opts?.client)).flush(500).catch(() => undefined);
|
|
919
1065
|
await flushStdout();
|
|
920
1066
|
process.exitCode = 0;
|
|
921
1067
|
});
|