minicode-ai 0.26.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/args.ts +120 -16
- package/cli/commands/acp.ts +38 -28
- package/cli/commands/auth.ts +17 -17
- package/cli/commands/config.ts +65 -66
- package/cli/commands/daemon.ts +44 -16
- package/cli/commands/doctor.ts +11 -12
- package/cli/commands/exec.ts +35 -73
- package/cli/commands/mcp.ts +25 -8
- package/cli/commands/memory.ts +16 -24
- package/cli/commands/openwork.ts +6 -2
- package/cli/commands/pricing.ts +14 -12
- package/cli/commands/providers.ts +36 -57
- package/cli/commands/sessions.ts +24 -18
- package/cli/commands/skills.ts +33 -18
- package/cli/commands/stats.ts +15 -30
- package/cli/commands.ts +96 -46
- package/cli/context-command-run.ts +171 -0
- package/cli/context-command.ts +87 -0
- package/cli/index.ts +96 -98
- package/cli/model-manager.ts +7 -2
- package/cli/provider-manager.ts +33 -18
- package/cli/router.ts +41 -12
- package/cli/setup.ts +246 -123
- package/cli/tui.ts +311 -109
- package/docs/TERMINAL_CONTRACT.md +254 -69
- package/docs/UI_RENDER_PIPELINE.md +9 -1
- package/docs/agents.md +2 -2
- package/docs/architecture.md +3 -3
- package/docs/choosing-mode.md +7 -9
- package/docs/cli.md +3 -5
- package/docs/config.md +16 -0
- package/docs/environment.md +3 -3
- package/docs/policy-sandbox.md +3 -3
- package/docs/repl.md +3 -2
- package/docs/tools.md +1 -1
- package/package.json +1 -1
- package/src/app/mentions.ts +60 -13
- package/src/app/rag-layer.ts +17 -20
- package/src/app/session.ts +119 -66
- package/src/app/tool-layer.ts +12 -17
- package/src/config.ts +64 -104
- package/src/constants.ts +23 -9
- package/src/daemon/capability.ts +8 -1
- package/src/daemon/control.ts +3 -2
- package/src/daemon/server.ts +57 -0
- package/src/hooks/run.ts +73 -41
- package/src/lib/db-path.ts +47 -0
- package/src/lib/process-tree.ts +62 -0
- package/src/lib/public-http.ts +88 -23
- package/src/lib/trusted-exec.ts +48 -1
- package/src/lsp/client.ts +62 -17
- package/src/mcp/client.ts +85 -38
- package/src/mcp/http-transport.ts +28 -16
- package/src/mcp/transport.ts +42 -6
- package/src/memory/files.ts +164 -134
- package/src/memory/vector.ts +371 -420
- package/src/policy/approval-binding.ts +122 -45
- package/src/policy/bash-guard.ts +18 -7
- package/src/policy/compaction.ts +51 -18
- package/src/policy/context-window.ts +109 -0
- package/src/policy/context.ts +78 -37
- package/src/policy/executor.ts +17 -7
- package/src/policy/microcompact.ts +4 -3
- package/src/policy/no-progress.ts +103 -22
- package/src/policy/permission.ts +36 -12
- package/src/policy/tool-contract.ts +74 -416
- package/src/policy/usage.ts +24 -2
- package/src/policy/verifier.ts +50 -2
- package/src/policy/work-mode.ts +28 -54
- package/src/presentation/adapter.ts +24 -16
- package/src/providers/guards.ts +41 -2
- package/src/providers/provision.ts +6 -60
- package/src/providers/responses.ts +1 -0
- package/src/repo/repomap.ts +180 -23
- package/src/runtime/execution-journal.ts +4 -29
- package/src/sandbox/docker.ts +58 -34
- package/src/sandbox/mask.ts +157 -0
- package/src/sandbox/os.ts +107 -73
- package/src/session/authority.ts +29 -10
- package/src/session/context-projection.ts +6 -5
- package/src/session/open-work.ts +26 -3
- package/src/session/persistence.ts +25 -58
- package/src/session/verification.ts +170 -20
- package/src/skills/loader.ts +31 -16
- package/src/task/production-scheduler.ts +13 -1
- package/src/task/store.ts +20 -37
- package/src/telemetry/trace.ts +27 -0
- package/src/tools/ask_user.ts +1 -13
- package/src/tools/bash.ts +27 -92
- package/src/tools/code_run.ts +1 -1
- package/src/tools/context.ts +133 -0
- package/src/tools/delete_file.ts +3 -2
- package/src/tools/edit.ts +37 -72
- package/src/tools/git.ts +86 -9
- package/src/tools/glob.ts +26 -51
- package/src/tools/grep.ts +67 -57
- package/src/tools/hashline.ts +4 -0
- package/src/tools/lsp.ts +11 -8
- package/src/tools/mcp_call.ts +9 -6
- package/src/tools/memory.ts +56 -33
- package/src/tools/move_file.ts +36 -10
- package/src/tools/patch.ts +13 -21
- package/src/tools/read_file.ts +18 -25
- package/src/tools/read_image.ts +35 -33
- package/src/tools/submit_result.ts +29 -8
- package/src/tools/task.ts +74 -17
- package/src/tools/todo.ts +181 -74
- package/src/tools/web_fetch.ts +42 -2
- package/src/tools/web_search.ts +47 -8
- package/src/ui/approval/prompt.ts +206 -47
- package/src/ui/assistant/turn-status.ts +14 -5
- package/src/ui/footer.ts +4 -4
- package/src/ui/i18n/en.ts +59 -23
- package/src/ui/i18n/id.ts +88 -51
- package/src/ui/input/input.ts +4 -3
- package/src/ui/input/prompt-engine.ts +35 -5
- package/src/ui/render/activity-panel.ts +4 -2
- package/src/ui/render/hint.ts +7 -4
- package/src/ui/render/list.ts +32 -0
- package/src/ui/render/markdown.ts +6 -3
- package/src/ui/render/notice.ts +19 -0
- package/src/ui/render/table-grid.ts +7 -8
- package/src/ui/render/theme.ts +113 -39
- package/src/ui/runtime/screen.ts +74 -10
- package/src/ui/runtime/spinner.ts +19 -6
- package/src/ui/screens/dialog.ts +19 -1
- package/src/ui/screens/form.ts +80 -35
- package/src/ui/screens/model-manager.ts +88 -62
- package/src/ui/screens/output-window.ts +263 -0
- package/src/ui/screens/picker.ts +94 -81
- package/src/ui/screens/provider-manager.ts +100 -69
- package/src/ui/screens/wizard.ts +117 -28
- package/src/ui/tui/app.ts +196 -33
- package/src/ui/tui/transcript.ts +154 -25
- package/vendor/minicore/src/core/executor.ts +2 -0
- package/vendor/minicore/src/core/loop.ts +17 -6
- package/vendor/minicore/src/core/session.ts +3 -0
- package/vendor/minicore/src/core/tool.ts +1 -0
- package/vendor/minicore/src/providers/openai-compat.ts +28 -19
package/src/mcp/client.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "
|
|
|
3
3
|
import { dirname, resolve } from "node:path"
|
|
4
4
|
import type { Tool, ToolContext } from "#minicore"
|
|
5
5
|
import { LIMITS } from "../constants.ts"
|
|
6
|
-
import { scrubSecrets } from "../policy/scrub.ts"
|
|
6
|
+
import { fenceUntrustedToolOutput, isAlreadyFenced, scrubSecrets } from "../policy/scrub.ts"
|
|
7
7
|
import { McpHttpTransport, type McpTransportLike } from "./http-transport.ts"
|
|
8
8
|
import { McpTransport } from "./transport.ts"
|
|
9
9
|
|
|
@@ -67,6 +67,28 @@ function sortObjectKeys(obj: Record<string, unknown>): Record<string, unknown> {
|
|
|
67
67
|
return out
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Normalisasi `inputSchema` dari server MCP agar provider LLM yang ketat
|
|
72
|
+
* (OpenAI/Gemini) tidak menolak tool saat server mengirim skema kosong atau
|
|
73
|
+
* tanpa `type: "object"`/`properties`. Tidak menyentuh objek asli agar pin SHA-256 utuh.
|
|
74
|
+
*/
|
|
75
|
+
export function normalizeMcpInputSchema(schema: unknown): Tool["parameters"] {
|
|
76
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
|
|
77
|
+
return { type: "object", properties: {}, additionalProperties: true }
|
|
78
|
+
}
|
|
79
|
+
const obj = schema as Record<string, unknown>
|
|
80
|
+
const typeVal = typeof obj.type === "string" && obj.type ? obj.type : "object"
|
|
81
|
+
if (typeVal === "object" && (!obj.properties || typeof obj.properties !== "object")) {
|
|
82
|
+
return {
|
|
83
|
+
...obj,
|
|
84
|
+
type: "object",
|
|
85
|
+
properties: {},
|
|
86
|
+
...(obj.additionalProperties === undefined ? { additionalProperties: true } : {}),
|
|
87
|
+
} as Tool["parameters"]
|
|
88
|
+
}
|
|
89
|
+
return { ...obj, type: typeVal } as Tool["parameters"]
|
|
90
|
+
}
|
|
91
|
+
|
|
70
92
|
/** P2.3: sleep yang dapat dibatalkan oleh signal sesi/koneksi. */
|
|
71
93
|
function abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
72
94
|
return new Promise((resolve, reject) => {
|
|
@@ -316,7 +338,9 @@ class McpConnection {
|
|
|
316
338
|
this.stdio = null
|
|
317
339
|
return this.http
|
|
318
340
|
}
|
|
319
|
-
|
|
341
|
+
// Beri ruang frame stdio 8 MiB agar screenshot/snapshot browser MCP tidak
|
|
342
|
+
// memicu SIGKILL pada server (uji unit McpTransport() tetap pakai default 1 MiB).
|
|
343
|
+
this.stdio = new McpTransport({ maxFrameBytes: LIMITS.MCP_HTTP_BODY_MAX_BYTES })
|
|
320
344
|
this.http = null
|
|
321
345
|
return this.stdio
|
|
322
346
|
}
|
|
@@ -380,27 +404,10 @@ class McpConnection {
|
|
|
380
404
|
}
|
|
381
405
|
|
|
382
406
|
let handshakeOk = false
|
|
383
|
-
// `
|
|
384
|
-
//
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
const disc = (await this.transport.request(
|
|
388
|
-
"server/discover",
|
|
389
|
-
{
|
|
390
|
-
_meta: {
|
|
391
|
-
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
|
|
392
|
-
"io.modelcontextprotocol/clientInfo": { name: "minicode", version: "0.1.0" },
|
|
393
|
-
"io.modelcontextprotocol/clientCapabilities": { tools: {} },
|
|
394
|
-
},
|
|
395
|
-
},
|
|
396
|
-
2_000,
|
|
397
|
-
)) as Record<string, unknown>
|
|
398
|
-
if (disc?.capabilities) handshakeOk = true
|
|
399
|
-
} catch {
|
|
400
|
-
// jatuh ke initialize di bawah
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
if (!handshakeOk) {
|
|
407
|
+
// Standar MCP mewajibkan `initialize` sebagai pesan pertama; mengirim
|
|
408
|
+
// `server/discover` lebih dulu membuang 2 dtk timeout dan membuat server
|
|
409
|
+
// ketat (Playwright/Puppeteer/SDK resmi) menutup koneksi.
|
|
410
|
+
try {
|
|
404
411
|
await this.transport.request(
|
|
405
412
|
"initialize",
|
|
406
413
|
{
|
|
@@ -417,6 +424,32 @@ class McpConnection {
|
|
|
417
424
|
// wajib per spec: notification initialized setelah initialize
|
|
418
425
|
this.transport.notify("notifications/initialized", {})
|
|
419
426
|
handshakeOk = true
|
|
427
|
+
} catch (initErr) {
|
|
428
|
+
const msg = (initErr as Error)?.message ?? ""
|
|
429
|
+
if (
|
|
430
|
+
this.kind === "stdio" &&
|
|
431
|
+
!this.transport.isClosed &&
|
|
432
|
+
/method not found|-32601/i.test(msg)
|
|
433
|
+
) {
|
|
434
|
+
try {
|
|
435
|
+
const disc = (await this.transport.request(
|
|
436
|
+
"server/discover",
|
|
437
|
+
{
|
|
438
|
+
_meta: {
|
|
439
|
+
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
|
|
440
|
+
"io.modelcontextprotocol/clientInfo": { name: "minicode", version: "0.1.0" },
|
|
441
|
+
"io.modelcontextprotocol/clientCapabilities": { tools: {} },
|
|
442
|
+
},
|
|
443
|
+
},
|
|
444
|
+
2_000,
|
|
445
|
+
)) as Record<string, unknown>
|
|
446
|
+
if (disc?.capabilities) handshakeOk = true
|
|
447
|
+
} catch {
|
|
448
|
+
throw initErr
|
|
449
|
+
}
|
|
450
|
+
} else {
|
|
451
|
+
throw initErr
|
|
452
|
+
}
|
|
420
453
|
}
|
|
421
454
|
if (!handshakeOk) throw new Error("handshake failed")
|
|
422
455
|
|
|
@@ -579,11 +612,7 @@ class McpConnection {
|
|
|
579
612
|
return {
|
|
580
613
|
name: `${serverId}.${t.name}`,
|
|
581
614
|
description: t.description ?? `MCP tool from ${serverId}`,
|
|
582
|
-
parameters: (t.inputSchema
|
|
583
|
-
type: "object",
|
|
584
|
-
properties: {},
|
|
585
|
-
additionalProperties: true,
|
|
586
|
-
}) as Tool["parameters"],
|
|
615
|
+
parameters: normalizeMcpInputSchema(t.inputSchema),
|
|
587
616
|
async execute(input: Record<string, unknown>, _ctx: ToolContext): Promise<string> {
|
|
588
617
|
// P2.3: pastikan koneksi hidup; reconnect bounded bila transport
|
|
589
618
|
// mati. Discovery dijalankan ulang, jadi verifikasi memakai definisi
|
|
@@ -603,7 +632,10 @@ class McpConnection {
|
|
|
603
632
|
_ctx.signal.throwIfAborted()
|
|
604
633
|
const result = await conn.callTool(t.name, input ?? {}, _ctx.signal)
|
|
605
634
|
const content = extractMcpText(result)
|
|
606
|
-
|
|
635
|
+
const capped = capMcpText(content)
|
|
636
|
+
return isAlreadyFenced(capped)
|
|
637
|
+
? capped
|
|
638
|
+
: fenceUntrustedToolOutput(`${serverId}.${t.name}`, capped)
|
|
607
639
|
},
|
|
608
640
|
}
|
|
609
641
|
})
|
|
@@ -629,7 +661,7 @@ async function ensureConnection(serverId: string, signal?: AbortSignal): Promise
|
|
|
629
661
|
return conn
|
|
630
662
|
}
|
|
631
663
|
|
|
632
|
-
/** Normalisasi konten tool MCP (string | blok {text}
|
|
664
|
+
/** Normalisasi konten tool MCP (string | blok {text} | image | resource) → teks. */
|
|
633
665
|
export function extractMcpText(result: unknown): string {
|
|
634
666
|
if (result == null) return ""
|
|
635
667
|
const r = result as { content?: unknown }
|
|
@@ -637,8 +669,26 @@ export function extractMcpText(result: unknown): string {
|
|
|
637
669
|
return r.content
|
|
638
670
|
.map((c) => {
|
|
639
671
|
if (typeof c === "string") return c
|
|
640
|
-
if (c && typeof c === "object"
|
|
641
|
-
|
|
672
|
+
if (c && typeof c === "object") {
|
|
673
|
+
const obj = c as {
|
|
674
|
+
type?: unknown
|
|
675
|
+
text?: unknown
|
|
676
|
+
data?: unknown
|
|
677
|
+
mimeType?: unknown
|
|
678
|
+
resource?: { text?: unknown; blob?: unknown; mimeType?: unknown; uri?: unknown }
|
|
679
|
+
}
|
|
680
|
+
if (typeof obj.text === "string") return obj.text
|
|
681
|
+
// Blok image/audio (mis. screenshot Playwright MCP): jangan serialize base64
|
|
682
|
+
// raksasa menjadi JSON teks yang lalu dipotong tengah di 16 KB.
|
|
683
|
+
if ((obj.type === "image" || obj.type === "audio") && typeof obj.data === "string") {
|
|
684
|
+
return `[${obj.type} ${String(obj.mimeType ?? "application/octet-stream")}, ${obj.data.length} base64 chars — binary skipped]`
|
|
685
|
+
}
|
|
686
|
+
if (obj.type === "resource" && obj.resource) {
|
|
687
|
+
if (typeof obj.resource.text === "string") return obj.resource.text
|
|
688
|
+
if (typeof obj.resource.blob === "string") {
|
|
689
|
+
return `[binary ${String(obj.resource.mimeType ?? "application/octet-stream")}, ${obj.resource.blob.length} base64 chars — skipped]`
|
|
690
|
+
}
|
|
691
|
+
}
|
|
642
692
|
}
|
|
643
693
|
return JSON.stringify(c)
|
|
644
694
|
})
|
|
@@ -717,8 +767,7 @@ export async function callMcpTool(
|
|
|
717
767
|
args: Record<string, unknown>,
|
|
718
768
|
signal?: AbortSignal,
|
|
719
769
|
): Promise<unknown> {
|
|
720
|
-
const conn =
|
|
721
|
-
if (!conn) throw new Error(`MCP server ${serverId} not connected`)
|
|
770
|
+
const conn = await ensureConnection(serverId, signal)
|
|
722
771
|
// F8.2: verify skema ulang saat pemanggilan. Tool yang skemanya berubah
|
|
723
772
|
// tanpa persetujuan ditolak fail-closed sebelum request dikirim ke server.
|
|
724
773
|
const tool = conn.tools.find((t) => t.name === toolName)
|
|
@@ -735,8 +784,7 @@ export async function readMcpResource(
|
|
|
735
784
|
uri: string,
|
|
736
785
|
signal?: AbortSignal,
|
|
737
786
|
): Promise<string> {
|
|
738
|
-
const conn =
|
|
739
|
-
if (!conn) throw new Error(`MCP server ${serverId} not connected`)
|
|
787
|
+
const conn = await ensureConnection(serverId, signal)
|
|
740
788
|
return conn.readResource(uri, signal)
|
|
741
789
|
}
|
|
742
790
|
|
|
@@ -746,8 +794,7 @@ export async function getMcpPrompt(
|
|
|
746
794
|
args: Record<string, unknown>,
|
|
747
795
|
signal?: AbortSignal,
|
|
748
796
|
): Promise<string> {
|
|
749
|
-
const conn =
|
|
750
|
-
if (!conn) throw new Error(`MCP server ${serverId} not connected`)
|
|
797
|
+
const conn = await ensureConnection(serverId, signal)
|
|
751
798
|
return conn.getPrompt(name, args, signal)
|
|
752
799
|
}
|
|
753
800
|
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
|
|
21
21
|
import { LIMITS } from "../constants.ts"
|
|
22
22
|
import { isPrivateHostWithDns } from "../lib/net.ts"
|
|
23
|
+
import { publicHttp } from "../lib/public-http.ts"
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* Kemampuan transport yang dipakai `McpConnection` setelah koneksi terbuka.
|
|
@@ -160,17 +161,21 @@ export class McpHttpTransport implements McpTransportLike {
|
|
|
160
161
|
else signal?.addEventListener("abort", onAbort, { once: true })
|
|
161
162
|
let res: Response
|
|
162
163
|
try {
|
|
163
|
-
res = await
|
|
164
|
+
res = await publicHttp.request(this.url, {
|
|
164
165
|
method: "POST",
|
|
165
166
|
headers: this.headers("application/json, text/event-stream"),
|
|
166
167
|
body,
|
|
167
168
|
signal: controller.signal,
|
|
168
|
-
|
|
169
|
+
allowPrivateHost: this.allowPrivate,
|
|
169
170
|
})
|
|
170
171
|
} catch (e) {
|
|
171
172
|
clearTimeout(timer)
|
|
172
173
|
signal?.removeEventListener("abort", onAbort)
|
|
173
174
|
const msg = (e as Error).message
|
|
175
|
+
if (msg.startsWith("blocked private host:")) {
|
|
176
|
+
this.blockedPrivate = true
|
|
177
|
+
throw new Error(`MCP http: private host rejected: ${this.url.hostname}`)
|
|
178
|
+
}
|
|
174
179
|
throw new Error(
|
|
175
180
|
`MCP http: ${method} failed: ${msg === "timeout" ? `timeout ${timeoutMs}ms` : msg}`,
|
|
176
181
|
)
|
|
@@ -222,13 +227,16 @@ export class McpHttpTransport implements McpTransportLike {
|
|
|
222
227
|
// request()/connect()) alih-alih mengirim lalu diam.
|
|
223
228
|
void (async () => {
|
|
224
229
|
if (!(await this.checkHost())) return
|
|
225
|
-
await
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
230
|
+
await publicHttp
|
|
231
|
+
.request(this.url, {
|
|
232
|
+
method: "POST",
|
|
233
|
+
headers: this.headers("application/json, text/event-stream"),
|
|
234
|
+
body: JSON.stringify({ jsonrpc: "2.0", method, params }),
|
|
235
|
+
allowPrivateHost: this.allowPrivate,
|
|
236
|
+
signal: AbortSignal.timeout(Math.min(LIMITS.MCP_HANDSHAKE_TIMEOUT_MS, 3_000)),
|
|
237
|
+
})
|
|
238
|
+
.then((r) => r.body?.cancel())
|
|
239
|
+
.catch(() => {})
|
|
232
240
|
})()
|
|
233
241
|
}
|
|
234
242
|
|
|
@@ -239,11 +247,15 @@ export class McpHttpTransport implements McpTransportLike {
|
|
|
239
247
|
// itu bukan kegagalan bagi kita. Audit F7: cleanup TIDAK boleh jadi jalur
|
|
240
248
|
// egress tanpa validasi host (blockedPrivate = host terbukti privat).
|
|
241
249
|
if (this.sessionId && !this.blockedPrivate && (await this.checkHost())) {
|
|
242
|
-
await
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
250
|
+
await publicHttp
|
|
251
|
+
.request(this.url, {
|
|
252
|
+
method: "DELETE",
|
|
253
|
+
headers: this.headers("application/json"),
|
|
254
|
+
allowPrivateHost: this.allowPrivate,
|
|
255
|
+
signal: AbortSignal.timeout(Math.min(LIMITS.MCP_HANDSHAKE_TIMEOUT_MS, 3_000)),
|
|
256
|
+
})
|
|
257
|
+
.then((r) => r.body?.cancel())
|
|
258
|
+
.catch(() => {})
|
|
247
259
|
}
|
|
248
260
|
}
|
|
249
261
|
}
|
|
@@ -299,7 +311,7 @@ export async function readSseResponse(
|
|
|
299
311
|
const { done, value } = await reader.read()
|
|
300
312
|
if (done) break
|
|
301
313
|
total += value.byteLength
|
|
302
|
-
if (total > LIMITS.
|
|
314
|
+
if (total > LIMITS.MCP_HTTP_BODY_MAX_BYTES) {
|
|
303
315
|
throw new Error("MCP http: aliran SSE melewati batas ukuran")
|
|
304
316
|
}
|
|
305
317
|
buffer += decoder.decode(value, { stream: true })
|
|
@@ -377,7 +389,7 @@ async function readCapped(res: Response): Promise<string> {
|
|
|
377
389
|
const { done, value } = await reader.read()
|
|
378
390
|
if (done) break
|
|
379
391
|
out += decoder.decode(value, { stream: true })
|
|
380
|
-
if (out.length > LIMITS.
|
|
392
|
+
if (out.length > LIMITS.MCP_HTTP_BODY_MAX_BYTES) {
|
|
381
393
|
throw new Error("MCP http: balasan melewati batas ukuran")
|
|
382
394
|
}
|
|
383
395
|
}
|
package/src/mcp/transport.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
import { type ChildProcess, spawn } from "node:child_process"
|
|
1
|
+
import { type ChildProcess, spawn, spawnSync } from "node:child_process"
|
|
2
2
|
import { LIMITS } from "../constants.ts"
|
|
3
|
+
import { resolveSpawnTarget, resolveTrustedExecutable } from "../lib/trusted-exec.ts"
|
|
3
4
|
import { sanitizeSpawnEnv } from "../policy/scrub.ts"
|
|
4
5
|
|
|
5
6
|
export class McpTransport {
|
|
7
|
+
private readonly maxFrameBytes: number
|
|
8
|
+
constructor(opts: { maxFrameBytes?: number } = {}) {
|
|
9
|
+
this.maxFrameBytes = opts.maxFrameBytes ?? LIMITS.MCP_STDIO_FRAME_MAX_BYTES
|
|
10
|
+
}
|
|
6
11
|
private proc: ChildProcess | null = null
|
|
12
|
+
private winCmdTree = false
|
|
7
13
|
private stdoutFrame = Buffer.alloc(0)
|
|
8
14
|
private pending = new Map<
|
|
9
15
|
string | number,
|
|
@@ -28,11 +34,14 @@ export class McpTransport {
|
|
|
28
34
|
async connect(command: string, args: string[], env: Record<string, string> = {}): Promise<void> {
|
|
29
35
|
if (this.proc) throw new Error("transport already connected")
|
|
30
36
|
|
|
31
|
-
|
|
37
|
+
const target = resolveSpawnTarget(command, args)
|
|
38
|
+
this.winCmdTree = target.windowsVerbatimArguments === true
|
|
39
|
+
this.proc = spawn(target.command, target.args, {
|
|
32
40
|
stdio: ["pipe", "pipe", "pipe"],
|
|
33
41
|
// env kredensial di-strip; env eksplisit config server menang setelahnya
|
|
34
42
|
env: sanitizeSpawnEnv(process.env, env),
|
|
35
43
|
signal: this.killSignal.signal,
|
|
44
|
+
...(target.windowsVerbatimArguments ? { windowsVerbatimArguments: true } : {}),
|
|
36
45
|
})
|
|
37
46
|
|
|
38
47
|
this.proc.on("error", (err) => this.failAll(err))
|
|
@@ -101,6 +110,22 @@ export class McpTransport {
|
|
|
101
110
|
try {
|
|
102
111
|
this.proc?.stdin?.end()
|
|
103
112
|
} catch {}
|
|
113
|
+
// Untuk shim .cmd/.bat di Windows, bunuh seluruh subtree SEBELUM killSignal.abort()
|
|
114
|
+
// karena abort() hanya menghentikan wrapper cmd.exe dan meninggalkan anak (node/Chromium).
|
|
115
|
+
if (
|
|
116
|
+
this.winCmdTree &&
|
|
117
|
+
process.platform === "win32" &&
|
|
118
|
+
this.proc?.pid !== undefined &&
|
|
119
|
+
this.proc.exitCode === null
|
|
120
|
+
) {
|
|
121
|
+
try {
|
|
122
|
+
spawnSync(
|
|
123
|
+
resolveTrustedExecutable("taskkill"),
|
|
124
|
+
["/pid", String(this.proc.pid), "/T", "/F"],
|
|
125
|
+
{ stdio: "ignore", env: sanitizeSpawnEnv(process.env) },
|
|
126
|
+
)
|
|
127
|
+
} catch {}
|
|
128
|
+
}
|
|
104
129
|
this.killSignal.abort()
|
|
105
130
|
if (this.proc) {
|
|
106
131
|
const p = this.proc
|
|
@@ -108,7 +133,18 @@ export class McpTransport {
|
|
|
108
133
|
await new Promise<void>((r) => {
|
|
109
134
|
const t = setTimeout(() => {
|
|
110
135
|
try {
|
|
111
|
-
|
|
136
|
+
if (process.platform === "win32" && p.pid !== undefined) {
|
|
137
|
+
spawnSync(
|
|
138
|
+
resolveTrustedExecutable("taskkill"),
|
|
139
|
+
["/pid", String(p.pid), "/T", "/F"],
|
|
140
|
+
{
|
|
141
|
+
stdio: "ignore",
|
|
142
|
+
env: sanitizeSpawnEnv(process.env),
|
|
143
|
+
},
|
|
144
|
+
)
|
|
145
|
+
} else {
|
|
146
|
+
p.kill("SIGKILL")
|
|
147
|
+
}
|
|
112
148
|
} catch {}
|
|
113
149
|
r()
|
|
114
150
|
}, 2_000)
|
|
@@ -172,7 +208,7 @@ export class McpTransport {
|
|
|
172
208
|
for (let i = 0; i < chunk.length; i++) {
|
|
173
209
|
if (chunk[i] !== 0x0a) continue
|
|
174
210
|
const part = chunk.subarray(start, i)
|
|
175
|
-
if (this.stdoutFrame.length + part.length >
|
|
211
|
+
if (this.stdoutFrame.length + part.length > this.maxFrameBytes) {
|
|
176
212
|
this.rejectOversizedFrame(this.stdoutFrame.length + part.length)
|
|
177
213
|
return
|
|
178
214
|
}
|
|
@@ -184,7 +220,7 @@ export class McpTransport {
|
|
|
184
220
|
}
|
|
185
221
|
if (start < chunk.length) {
|
|
186
222
|
const remainder = chunk.subarray(start)
|
|
187
|
-
if (this.stdoutFrame.length + remainder.length >
|
|
223
|
+
if (this.stdoutFrame.length + remainder.length > this.maxFrameBytes) {
|
|
188
224
|
this.rejectOversizedFrame(this.stdoutFrame.length + remainder.length)
|
|
189
225
|
return
|
|
190
226
|
}
|
|
@@ -214,7 +250,7 @@ export class McpTransport {
|
|
|
214
250
|
|
|
215
251
|
private rejectOversizedFrame(size: number): void {
|
|
216
252
|
const error = new Error(
|
|
217
|
-
`MCP stdio frame exceeds ${
|
|
253
|
+
`MCP stdio frame exceeds ${this.maxFrameBytes} bytes (at least ${size})`,
|
|
218
254
|
)
|
|
219
255
|
this.closed = true
|
|
220
256
|
this.stdoutFrame = Buffer.alloc(0)
|