minicode-ai 0.9.26
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 +935 -0
- package/LICENSE +21 -0
- package/README.md +200 -0
- package/cli/args.ts +195 -0
- package/cli/auto-update.ts +108 -0
- package/cli/commands/auth.ts +166 -0
- package/cli/commands/config.ts +382 -0
- package/cli/commands/doctor.ts +128 -0
- package/cli/commands/exec.ts +179 -0
- package/cli/commands/mcp.ts +35 -0
- package/cli/commands/memory.ts +107 -0
- package/cli/commands/pricing.ts +100 -0
- package/cli/commands/providers.ts +162 -0
- package/cli/commands/sessions.ts +128 -0
- package/cli/commands/skills.ts +61 -0
- package/cli/commands/stats.ts +82 -0
- package/cli/commands.ts +317 -0
- package/cli/index.ts +539 -0
- package/cli/model-manager.ts +112 -0
- package/cli/provider-manager.ts +111 -0
- package/cli/repl.ts +734 -0
- package/cli/router.ts +124 -0
- package/cli/setup.ts +780 -0
- package/cli/wizard.ts +21 -0
- package/docs/ARCHITECTURE.html +1222 -0
- package/docs/README.md +109 -0
- package/docs/architecture.md +44 -0
- package/docs/changelog.md +3 -0
- package/package.json +91 -0
- package/src/agents/pool.ts +43 -0
- package/src/app/mentions.ts +50 -0
- package/src/app/provider-layer.ts +118 -0
- package/src/app/rag-layer.ts +105 -0
- package/src/app/session.ts +154 -0
- package/src/app/tool-layer.ts +56 -0
- package/src/config.ts +372 -0
- package/src/constants.ts +119 -0
- package/src/hooks/run.ts +89 -0
- package/src/lib/atomic-write.ts +88 -0
- package/src/lib/db-path.ts +36 -0
- package/src/lib/git-hardening.ts +91 -0
- package/src/lib/ignore.ts +58 -0
- package/src/lib/keystore.ts +202 -0
- package/src/lib/net.ts +70 -0
- package/src/lib/safe-open.ts +138 -0
- package/src/lib/session-id.ts +23 -0
- package/src/lib/trash.ts +38 -0
- package/src/lib/trusted-exec.ts +81 -0
- package/src/lsp/client.ts +395 -0
- package/src/mcp/client.ts +400 -0
- package/src/mcp/http-transport.ts +344 -0
- package/src/mcp/server.ts +538 -0
- package/src/mcp/transport.ts +150 -0
- package/src/memory/files.ts +159 -0
- package/src/memory/vector.ts +775 -0
- package/src/policy/allowlist.ts +67 -0
- package/src/policy/bash-guard.ts +465 -0
- package/src/policy/compaction.ts +271 -0
- package/src/policy/context.ts +161 -0
- package/src/policy/executor.ts +199 -0
- package/src/policy/jail.ts +127 -0
- package/src/policy/permission.ts +517 -0
- package/src/policy/pricing.ts +336 -0
- package/src/policy/ratelimit.ts +54 -0
- package/src/policy/sandbox-policy.ts +157 -0
- package/src/policy/scrub.ts +138 -0
- package/src/policy/update-check.ts +200 -0
- package/src/policy/usage.ts +292 -0
- package/src/policy/verifier.ts +181 -0
- package/src/providers/anthropic.ts +401 -0
- package/src/providers/auth-store.ts +139 -0
- package/src/providers/build.ts +136 -0
- package/src/providers/detect.ts +126 -0
- package/src/providers/effort.ts +165 -0
- package/src/providers/guards.ts +107 -0
- package/src/providers/oauth.ts +326 -0
- package/src/providers/presets.ts +122 -0
- package/src/providers/provision.ts +298 -0
- package/src/providers/responses.ts +371 -0
- package/src/providers/router.ts +247 -0
- package/src/repo/repomap.ts +470 -0
- package/src/sandbox/docker.ts +129 -0
- package/src/sandbox/os.ts +193 -0
- package/src/session/checkpoint.ts +543 -0
- package/src/session/journal.ts +1347 -0
- package/src/session/persistence.ts +446 -0
- package/src/session/shadow-git.ts +351 -0
- package/src/session/turn-marker.ts +118 -0
- package/src/skills/loader.ts +117 -0
- package/src/telemetry/trace.ts +283 -0
- package/src/tools/ask_user.ts +54 -0
- package/src/tools/bash.ts +400 -0
- package/src/tools/code_run.ts +96 -0
- package/src/tools/delete_file.ts +31 -0
- package/src/tools/edit.ts +204 -0
- package/src/tools/git.ts +301 -0
- package/src/tools/glob.ts +90 -0
- package/src/tools/grep.ts +281 -0
- package/src/tools/hashline.ts +66 -0
- package/src/tools/index.ts +103 -0
- package/src/tools/lsp.ts +346 -0
- package/src/tools/mcp_call.ts +163 -0
- package/src/tools/memory.ts +144 -0
- package/src/tools/move_file.ts +47 -0
- package/src/tools/patch.ts +86 -0
- package/src/tools/read_file.ts +130 -0
- package/src/tools/read_image.ts +80 -0
- package/src/tools/submit_result.ts +49 -0
- package/src/tools/task.ts +346 -0
- package/src/tools/todo.ts +201 -0
- package/src/tools/web_fetch.ts +161 -0
- package/src/tools/web_search.ts +156 -0
- package/src/tools/write_file.ts +42 -0
- package/src/ui/approval/prompt.ts +76 -0
- package/src/ui/assistant/simple.ts +568 -0
- package/src/ui/assistant/turn-status.ts +303 -0
- package/src/ui/contract.ts +55 -0
- package/src/ui/footer.ts +105 -0
- package/src/ui/input/input.ts +805 -0
- package/src/ui/input/prompt-engine.ts +606 -0
- package/src/ui/render/collapse.ts +84 -0
- package/src/ui/render/detail.ts +24 -0
- package/src/ui/render/diff.ts +181 -0
- package/src/ui/render/errors.ts +239 -0
- package/src/ui/render/format.ts +54 -0
- package/src/ui/render/highlight.ts +294 -0
- package/src/ui/render/markdown.ts +120 -0
- package/src/ui/render/money.ts +12 -0
- package/src/ui/render/reasoning.ts +20 -0
- package/src/ui/render/sanitize.ts +80 -0
- package/src/ui/render/table.ts +88 -0
- package/src/ui/render/theme.ts +339 -0
- package/src/ui/render/width.ts +256 -0
- package/src/ui/render/wrap.ts +120 -0
- package/src/ui/runtime/bus-debug.ts +83 -0
- package/src/ui/runtime/chrome.ts +301 -0
- package/src/ui/runtime/spinner.ts +77 -0
- package/src/ui/runtime/statusline.ts +227 -0
- package/src/ui/screens/model-manager.ts +434 -0
- package/src/ui/screens/overlay.ts +61 -0
- package/src/ui/screens/picker.ts +307 -0
- package/src/ui/screens/provider-manager.ts +450 -0
- package/src/ui/screens/wizard.ts +132 -0
- package/vendor/minicore/VENDOR.md +29 -0
- package/vendor/minicore/package.json +21 -0
- package/vendor/minicore/src/core/budget.ts +26 -0
- package/vendor/minicore/src/core/compact.ts +76 -0
- package/vendor/minicore/src/core/errors.ts +60 -0
- package/vendor/minicore/src/core/events.ts +74 -0
- package/vendor/minicore/src/core/executor.ts +139 -0
- package/vendor/minicore/src/core/history.ts +32 -0
- package/vendor/minicore/src/core/index.ts +42 -0
- package/vendor/minicore/src/core/loop.ts +344 -0
- package/vendor/minicore/src/core/permission.ts +24 -0
- package/vendor/minicore/src/core/provider.ts +39 -0
- package/vendor/minicore/src/core/recovery.ts +34 -0
- package/vendor/minicore/src/core/session.ts +327 -0
- package/vendor/minicore/src/core/snapshot.ts +142 -0
- package/vendor/minicore/src/core/tokens.ts +111 -0
- package/vendor/minicore/src/core/tool.ts +118 -0
- package/vendor/minicore/src/core/types.ts +50 -0
- package/vendor/minicore/src/providers/openai-compat.ts +366 -0
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import type { Tool, ToolContext } from "#minicore"
|
|
2
|
+
import { LIMITS } from "../constants.ts"
|
|
3
|
+
import { scrubSecrets } from "../policy/scrub.ts"
|
|
4
|
+
import { McpHttpTransport, type McpTransportLike } from "./http-transport.ts"
|
|
5
|
+
import { McpTransport } from "./transport.ts"
|
|
6
|
+
|
|
7
|
+
// MODEL KEPERCAYAAN MCP (baca sebelum mengubah file ini):
|
|
8
|
+
//
|
|
9
|
+
// Permission Minicode HANYA mengontrol pemanggilan (invocation): tool apa di
|
|
10
|
+
// server mana dengan argumen apa, satu approval per panggilan (di auto/ask;
|
|
11
|
+
// ditolak di readonly/plan/allowlist; bebas di allow-all).
|
|
12
|
+
//
|
|
13
|
+
// Server TIDAK dianggap trusted wrapper: capability di balik server
|
|
14
|
+
// (filesystem, network, process, state, API eksternal, kredensial) TIDAK
|
|
15
|
+
// dapat diketahui statis dan TIDAK diklasifikasikan — anggap setiap
|
|
16
|
+
// `tools/call` sebagai UNCLASSIFIED EXTERNAL CAPABILITY dengan efek arbitrer.
|
|
17
|
+
// Satu-satunya batas sisi-Minicode: validasi server-id, scrubSecrets +
|
|
18
|
+
// cap 100k pada hasil, dan SSRF guard untuk transport HTTP.
|
|
19
|
+
// Pembatalan parent menghentikan penungguan (dan request HTTP), tetapi TIDAK
|
|
20
|
+
// membunuh proses stdio server maupun membatalkan efek yang sudah terjadi.
|
|
21
|
+
|
|
22
|
+
export interface McpServerConfig {
|
|
23
|
+
id: string
|
|
24
|
+
/** stdio: perintah yang di-spawn. */
|
|
25
|
+
command?: string
|
|
26
|
+
args?: string[]
|
|
27
|
+
env?: Record<string, string>
|
|
28
|
+
/** Streamable HTTP endpoint. Bila ada, dipakai alih-alih stdio. */
|
|
29
|
+
url?: string
|
|
30
|
+
headers?: Record<string, string>
|
|
31
|
+
allowPrivateHost?: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface McpToolDef {
|
|
35
|
+
name: string
|
|
36
|
+
description?: string
|
|
37
|
+
inputSchema?: Record<string, unknown>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Resource yang di-expose server (spec: `resources/list`). */
|
|
41
|
+
export interface McpResourceDef {
|
|
42
|
+
uri: string
|
|
43
|
+
name?: string
|
|
44
|
+
description?: string
|
|
45
|
+
mimeType?: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Prompt template yang di-expose server (spec: `prompts/list`). */
|
|
49
|
+
export interface McpPromptDef {
|
|
50
|
+
name: string
|
|
51
|
+
description?: string
|
|
52
|
+
arguments?: { name: string; description?: string; required?: boolean }[]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const activeConnections = new Map<string, McpConnection>()
|
|
56
|
+
|
|
57
|
+
class McpConnection {
|
|
58
|
+
/** Transport konkret; tipe union agar `connect` yang berbeda tetap tersalur. */
|
|
59
|
+
private readonly http: McpHttpTransport | null
|
|
60
|
+
private readonly stdio: McpTransport | null
|
|
61
|
+
transport: McpTransportLike
|
|
62
|
+
tools: McpToolDef[] = []
|
|
63
|
+
resources: McpResourceDef[] = []
|
|
64
|
+
prompts: McpPromptDef[] = []
|
|
65
|
+
ready = false
|
|
66
|
+
|
|
67
|
+
constructor(public config: McpServerConfig) {
|
|
68
|
+
// Transport dipilih dari bentuk config: `url` → HTTP, else stdio.
|
|
69
|
+
if (config.url) {
|
|
70
|
+
this.http = new McpHttpTransport({
|
|
71
|
+
url: config.url,
|
|
72
|
+
headers: config.headers,
|
|
73
|
+
allowPrivateHost: config.allowPrivateHost,
|
|
74
|
+
})
|
|
75
|
+
this.stdio = null
|
|
76
|
+
this.transport = this.http
|
|
77
|
+
} else {
|
|
78
|
+
this.stdio = new McpTransport()
|
|
79
|
+
this.http = null
|
|
80
|
+
this.transport = this.stdio
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get kind(): "http" | "stdio" {
|
|
85
|
+
return this.http ? "http" : "stdio"
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async connect(): Promise<void> {
|
|
89
|
+
if (this.http) {
|
|
90
|
+
await this.http.connect()
|
|
91
|
+
} else {
|
|
92
|
+
if (!this.config.command) throw new Error("MCP config needs `command` or `url`")
|
|
93
|
+
await this.stdio!.connect(this.config.command, this.config.args ?? [], this.config.env ?? {})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let handshakeOk = false
|
|
97
|
+
// `server/discover` adalah ekstensi non-standar; hanya masuk akal untuk
|
|
98
|
+
// stdio lokal. Server HTTP mengikuti spec → langsung `initialize`.
|
|
99
|
+
if (this.kind === "stdio") {
|
|
100
|
+
try {
|
|
101
|
+
const disc = (await this.transport.request(
|
|
102
|
+
"server/discover",
|
|
103
|
+
{
|
|
104
|
+
_meta: {
|
|
105
|
+
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
|
|
106
|
+
"io.modelcontextprotocol/clientInfo": { name: "minicode", version: "0.1.0" },
|
|
107
|
+
"io.modelcontextprotocol/clientCapabilities": { tools: {} },
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
2_000,
|
|
111
|
+
)) as Record<string, unknown>
|
|
112
|
+
if (disc?.capabilities) handshakeOk = true
|
|
113
|
+
} catch {
|
|
114
|
+
// jatuh ke initialize di bawah
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (!handshakeOk) {
|
|
118
|
+
await this.transport.request(
|
|
119
|
+
"initialize",
|
|
120
|
+
{
|
|
121
|
+
protocolVersion: "2025-06-18",
|
|
122
|
+
// Deklarasikan seluruh kapabilitas yang kita KONSUMSI. Server memakai
|
|
123
|
+
// ini untuk memutuskan apa yang layak diiklankan; sebelumnya kita
|
|
124
|
+
// hanya menyebut `tools` sehingga server yang sopan tidak menawarkan
|
|
125
|
+
// resources/prompts sama sekali.
|
|
126
|
+
capabilities: { tools: {}, resources: {}, prompts: {} },
|
|
127
|
+
clientInfo: { name: "minicode", version: "0.1.0" },
|
|
128
|
+
},
|
|
129
|
+
LIMITS.MCP_HANDSHAKE_TIMEOUT_MS,
|
|
130
|
+
)
|
|
131
|
+
// wajib per spec: notification initialized setelah initialize
|
|
132
|
+
this.transport.notify("notifications/initialized", {})
|
|
133
|
+
handshakeOk = true
|
|
134
|
+
}
|
|
135
|
+
if (!handshakeOk) throw new Error("handshake failed")
|
|
136
|
+
|
|
137
|
+
const result = (await this.transport.request("tools/list", {})) as Record<string, unknown>
|
|
138
|
+
if (result && Array.isArray(result.tools)) this.tools = result.tools as McpToolDef[]
|
|
139
|
+
|
|
140
|
+
// Resources & prompts bersifat OPSIONAL di spec: banyak server tidak
|
|
141
|
+
// mengimplementasikannya dan membalas "method not found". Kegagalan di sini
|
|
142
|
+
// tidak boleh membatalkan koneksi yang tool-nya sudah berhasil dimuat.
|
|
143
|
+
this.resources = await this.listSafely<McpResourceDef>("resources/list", "resources")
|
|
144
|
+
this.prompts = await this.listSafely<McpPromptDef>("prompts/list", "prompts")
|
|
145
|
+
|
|
146
|
+
this.ready = true
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private async listSafely<T>(method: string, key: string): Promise<T[]> {
|
|
150
|
+
try {
|
|
151
|
+
const res = (await this.transport.request(method, {}, LIMITS.MCP_HANDSHAKE_TIMEOUT_MS)) as
|
|
152
|
+
| Record<string, unknown>
|
|
153
|
+
| undefined
|
|
154
|
+
const arr = res?.[key]
|
|
155
|
+
return Array.isArray(arr) ? (arr as T[]) : []
|
|
156
|
+
} catch {
|
|
157
|
+
return []
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Baca satu resource. Return teks gabungan dari semua `contents`. */
|
|
162
|
+
async readResource(uri: string, signal?: AbortSignal): Promise<string> {
|
|
163
|
+
const res = (await this.transport.request("resources/read", { uri }, undefined, signal)) as
|
|
164
|
+
| { contents?: unknown[] }
|
|
165
|
+
| undefined
|
|
166
|
+
const contents = Array.isArray(res?.contents) ? res.contents : []
|
|
167
|
+
if (contents.length === 0) return ""
|
|
168
|
+
const parts: string[] = []
|
|
169
|
+
for (const c of contents) {
|
|
170
|
+
const item = c as { text?: unknown; blob?: unknown; uri?: unknown; mimeType?: unknown }
|
|
171
|
+
if (typeof item.text === "string") {
|
|
172
|
+
parts.push(item.text)
|
|
173
|
+
} else if (typeof item.blob === "string") {
|
|
174
|
+
// Blob base64: jangan tumpahkan ribuan karakter base64 ke konteks model.
|
|
175
|
+
parts.push(
|
|
176
|
+
`[binary ${String(item.mimeType ?? "application/octet-stream")}, ${item.blob.length} base64 chars — skipped]`,
|
|
177
|
+
)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return parts.join("\n")
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Ambil prompt template yang sudah di-render server. */
|
|
184
|
+
async getPrompt(
|
|
185
|
+
name: string,
|
|
186
|
+
args: Record<string, unknown>,
|
|
187
|
+
signal?: AbortSignal,
|
|
188
|
+
): Promise<string> {
|
|
189
|
+
const res = (await this.transport.request(
|
|
190
|
+
"prompts/get",
|
|
191
|
+
{
|
|
192
|
+
name,
|
|
193
|
+
arguments: args,
|
|
194
|
+
},
|
|
195
|
+
undefined,
|
|
196
|
+
signal,
|
|
197
|
+
)) as { messages?: unknown[]; description?: string } | undefined
|
|
198
|
+
const msgs = Array.isArray(res?.messages) ? res.messages : []
|
|
199
|
+
const lines: string[] = []
|
|
200
|
+
if (res?.description) lines.push(`# ${res.description}`)
|
|
201
|
+
for (const m of msgs) {
|
|
202
|
+
const msg = m as { role?: unknown; content?: unknown }
|
|
203
|
+
const role = typeof msg.role === "string" ? msg.role : "user"
|
|
204
|
+
// content bisa objek tunggal {type,text} atau array blok
|
|
205
|
+
const blocks = Array.isArray(msg.content) ? msg.content : [msg.content]
|
|
206
|
+
const text = blocks
|
|
207
|
+
.map((b) => {
|
|
208
|
+
const blk = b as { type?: unknown; text?: unknown }
|
|
209
|
+
if (typeof blk?.text === "string") return blk.text
|
|
210
|
+
if (typeof b === "string") return b
|
|
211
|
+
return ""
|
|
212
|
+
})
|
|
213
|
+
.filter(Boolean)
|
|
214
|
+
.join("\n")
|
|
215
|
+
if (text) lines.push(`[${role}] ${text}`)
|
|
216
|
+
}
|
|
217
|
+
return lines.join("\n")
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async callTool(
|
|
221
|
+
name: string,
|
|
222
|
+
args: Record<string, unknown>,
|
|
223
|
+
signal?: AbortSignal,
|
|
224
|
+
): Promise<unknown> {
|
|
225
|
+
const result = (await this.transport.request(
|
|
226
|
+
"tools/call",
|
|
227
|
+
{
|
|
228
|
+
name,
|
|
229
|
+
arguments: args,
|
|
230
|
+
},
|
|
231
|
+
undefined,
|
|
232
|
+
signal,
|
|
233
|
+
)) as Record<string, unknown>
|
|
234
|
+
if (result && typeof result.isError === "boolean" && result.isError) {
|
|
235
|
+
const content = Array.isArray(result.content)
|
|
236
|
+
? result.content.map((c: any) => c.text ?? JSON.stringify(c)).join("\n")
|
|
237
|
+
: String(result.content)
|
|
238
|
+
throw new Error(`MCP tool ${name}: ${content}`)
|
|
239
|
+
}
|
|
240
|
+
return result
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
wrapTools(serverId: string): Tool[] {
|
|
244
|
+
return this.tools.map((t) => ({
|
|
245
|
+
name: `${serverId}.${t.name}`,
|
|
246
|
+
description: t.description ?? `MCP tool from ${serverId}`,
|
|
247
|
+
parameters: (t.inputSchema ?? {
|
|
248
|
+
type: "object",
|
|
249
|
+
properties: {},
|
|
250
|
+
additionalProperties: true,
|
|
251
|
+
}) as Tool["parameters"],
|
|
252
|
+
async execute(input: Record<string, unknown>, _ctx: ToolContext): Promise<string> {
|
|
253
|
+
const conn = activeConnections.get(serverId)
|
|
254
|
+
if (!conn) throw new Error(`MCP server ${serverId} not connected`)
|
|
255
|
+
_ctx.signal.throwIfAborted()
|
|
256
|
+
const result = await conn.callTool(t.name, input ?? {}, _ctx.signal)
|
|
257
|
+
const content = extractMcpText(result)
|
|
258
|
+
return capMcpText(content)
|
|
259
|
+
},
|
|
260
|
+
}))
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async close(): Promise<void> {
|
|
264
|
+
await this.transport.close()
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Normalisasi konten tool MCP (string | blok {text} campuran) → teks. */
|
|
269
|
+
export function extractMcpText(result: unknown): string {
|
|
270
|
+
if (result == null) return ""
|
|
271
|
+
const r = result as { content?: unknown }
|
|
272
|
+
if (!Array.isArray(r.content)) return String(result)
|
|
273
|
+
return r.content
|
|
274
|
+
.map((c) => {
|
|
275
|
+
if (typeof c === "string") return c
|
|
276
|
+
if (c && typeof c === "object" && typeof (c as { text?: unknown }).text === "string") {
|
|
277
|
+
return (c as { text: string }).text
|
|
278
|
+
}
|
|
279
|
+
return JSON.stringify(c)
|
|
280
|
+
})
|
|
281
|
+
.join("\n")
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Cap + penanda truncation MCP (temuan audit #02: slice 100k diam-diam
|
|
286
|
+
* tampak utuh). Scrub dulu agar pola rahasia di dekat batas tak lolos.
|
|
287
|
+
*/
|
|
288
|
+
export function capMcpText(content: string): string {
|
|
289
|
+
const clean = scrubSecrets(content)
|
|
290
|
+
const cap = LIMITS.MCP_OUTPUT_MAX_CHARS
|
|
291
|
+
return clean.length > cap
|
|
292
|
+
? `${clean.slice(0, cap)}\n… [mcp truncated: showing first ${cap} chars]`
|
|
293
|
+
: clean
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export async function connectAll(configs: McpServerConfig[]): Promise<Tool[]> {
|
|
297
|
+
// Paralel: server independen — sequential membuat startup membayar jumlah
|
|
298
|
+
// timeout handshake (3s/server). Urutan pesan stderr dipertahankan sesuai
|
|
299
|
+
// config agar log deterministik.
|
|
300
|
+
const settled = await Promise.allSettled(
|
|
301
|
+
configs.map(async (cfg) => {
|
|
302
|
+
if (activeConnections.has(cfg.id)) return { cfg, conn: null as McpConnection | null }
|
|
303
|
+
const conn = new McpConnection(cfg)
|
|
304
|
+
await conn.connect()
|
|
305
|
+
return { cfg, conn }
|
|
306
|
+
}),
|
|
307
|
+
)
|
|
308
|
+
const allTools: Tool[] = []
|
|
309
|
+
|
|
310
|
+
for (const s of settled) {
|
|
311
|
+
if (s.status === "rejected") {
|
|
312
|
+
const cfg = configs[settled.indexOf(s)]!
|
|
313
|
+
process.stderr.write(`[mcp] ${cfg.id} failed: ${(s.reason as Error)?.message ?? s.reason}\n`)
|
|
314
|
+
continue
|
|
315
|
+
}
|
|
316
|
+
const { cfg, conn } = s.value
|
|
317
|
+
if (!conn) {
|
|
318
|
+
process.stderr.write(`[mcp] ${cfg.id} already connected\n`)
|
|
319
|
+
continue
|
|
320
|
+
}
|
|
321
|
+
{
|
|
322
|
+
const tools = conn.wrapTools(cfg.id)
|
|
323
|
+
allTools.push(...tools)
|
|
324
|
+
activeConnections.set(cfg.id, conn)
|
|
325
|
+
const extra = [
|
|
326
|
+
conn.resources.length ? `${conn.resources.length} resources` : "",
|
|
327
|
+
conn.prompts.length ? `${conn.prompts.length} prompts` : "",
|
|
328
|
+
]
|
|
329
|
+
.filter(Boolean)
|
|
330
|
+
.join(", ")
|
|
331
|
+
process.stderr.write(
|
|
332
|
+
`[mcp] ${cfg.id} connected via ${conn.kind} (${tools.length} tools${extra ? `, ${extra}` : ""})\n`,
|
|
333
|
+
)
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return allTools
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export async function callMcpTool(
|
|
341
|
+
serverId: string,
|
|
342
|
+
toolName: string,
|
|
343
|
+
args: Record<string, unknown>,
|
|
344
|
+
signal?: AbortSignal,
|
|
345
|
+
): Promise<unknown> {
|
|
346
|
+
const conn = activeConnections.get(serverId)
|
|
347
|
+
if (!conn) throw new Error(`MCP server ${serverId} not connected`)
|
|
348
|
+
return conn.callTool(toolName, args, signal)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export async function readMcpResource(
|
|
352
|
+
serverId: string,
|
|
353
|
+
uri: string,
|
|
354
|
+
signal?: AbortSignal,
|
|
355
|
+
): Promise<string> {
|
|
356
|
+
const conn = activeConnections.get(serverId)
|
|
357
|
+
if (!conn) throw new Error(`MCP server ${serverId} not connected`)
|
|
358
|
+
return conn.readResource(uri, signal)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export async function getMcpPrompt(
|
|
362
|
+
serverId: string,
|
|
363
|
+
name: string,
|
|
364
|
+
args: Record<string, unknown>,
|
|
365
|
+
signal?: AbortSignal,
|
|
366
|
+
): Promise<string> {
|
|
367
|
+
const conn = activeConnections.get(serverId)
|
|
368
|
+
if (!conn) throw new Error(`MCP server ${serverId} not connected`)
|
|
369
|
+
return conn.getPrompt(name, args, signal)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Ringkasan seluruh kapabilitas server terhubung (dipakai `mcp_list`). */
|
|
373
|
+
export function mcpInventory(): {
|
|
374
|
+
id: string
|
|
375
|
+
kind: "http" | "stdio"
|
|
376
|
+
tools: McpToolDef[]
|
|
377
|
+
resources: McpResourceDef[]
|
|
378
|
+
prompts: McpPromptDef[]
|
|
379
|
+
}[] {
|
|
380
|
+
return [...activeConnections.entries()].map(([id, c]) => ({
|
|
381
|
+
id,
|
|
382
|
+
kind: c.kind,
|
|
383
|
+
tools: c.tools,
|
|
384
|
+
resources: c.resources,
|
|
385
|
+
prompts: c.prompts,
|
|
386
|
+
}))
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export async function closeAll(): Promise<void> {
|
|
390
|
+
for (const [id, conn] of activeConnections) {
|
|
391
|
+
try {
|
|
392
|
+
await conn.close()
|
|
393
|
+
} catch {}
|
|
394
|
+
activeConnections.delete(id)
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
export function getMcpServerIds(): string[] {
|
|
399
|
+
return [...activeConnections.keys()]
|
|
400
|
+
}
|