@koreanhole/kandev 0.0.0-stage → 0.1.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/README.md +367 -2
- package/bin/codex-acp.js +16 -0
- package/bin/kandev-auto-sync.mjs +101 -0
- package/bin/kandev-config.js +53 -0
- package/bin/kandev.js +35 -0
- package/config/config.yaml +6 -0
- package/config/docker/projects/opencodex-cli.json +79 -0
- package/config/docker/projects/samsung-bus-tracker.json +91 -0
- package/config/gateways.json +14 -0
- package/config/pipeline.json +150 -0
- package/config/workflows/sdlc.yaml +1019 -0
- package/docker/Dockerfile +68 -0
- package/docker/Dockerfile.opencodex-cli +44 -0
- package/docker/Dockerfile.samsung-bus-tracker +75 -0
- package/package.json +61 -4
- package/scripts/agent-tools-docker.mjs +223 -0
- package/scripts/baseline.mjs +20 -0
- package/scripts/codex-medium +2 -0
- package/scripts/codex-stage +22 -0
- package/scripts/gate.mjs +909 -0
- package/scripts/gateways/docker.mjs +64 -0
- package/scripts/gateways/speckit.mjs +78 -0
- package/scripts/install-prerequisites.mjs +48 -0
- package/scripts/lib/artifacts.mjs +990 -0
- package/scripts/lib/bugfix.mjs +325 -0
- package/scripts/lib/checksum.mjs +166 -0
- package/scripts/lib/docker-workspace.mjs +1209 -0
- package/scripts/lib/gateways.mjs +79 -0
- package/scripts/lib/kandev.mjs +273 -0
- package/scripts/lib/mcp.mjs +111 -0
- package/scripts/lib/pipeline.mjs +548 -0
- package/scripts/lib/planning-replan.mjs +103 -0
- package/scripts/lib/resume.mjs +207 -0
- package/scripts/lib/review-history.mjs +260 -0
- package/scripts/lib/stage-boundary.mjs +146 -0
- package/scripts/lib/stage-model.mjs +23 -0
- package/scripts/lib/task-worktree.mjs +40 -0
- package/scripts/measure-tokens.mjs +121 -0
- package/scripts/planning-tools.mjs +166 -0
- package/scripts/replan-planning.mjs +82 -0
- package/scripts/review-digest.mjs +240 -0
- package/scripts/run-android-e2e.mjs +116 -0
- package/scripts/run-code-review.mjs +281 -0
- package/scripts/run-detached.mjs +163 -0
- package/scripts/run-doc-review.mjs +155 -0
- package/scripts/run-gateways.mjs +138 -0
- package/scripts/sync.mjs +588 -0
- package/scripts/verify-change.mjs +252 -0
- package/scripts/verify-node.mjs +288 -0
- package/scripts/verify.mjs +442 -0
- package/scripts/wait-status.mjs +88 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/** Gateway configuration and result contracts shared by the runner and the gate. */
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
function isObject(value) {
|
|
6
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function loadGateways(root) {
|
|
10
|
+
const file = path.join(path.resolve(root), "config", "gateways.json");
|
|
11
|
+
if (!fs.existsSync(file)) throw new Error(`Missing config/gateways.json`);
|
|
12
|
+
let data;
|
|
13
|
+
try {
|
|
14
|
+
data = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
15
|
+
} catch (err) {
|
|
16
|
+
throw new Error(`config/gateways.json is not valid JSON: ${err.message}`);
|
|
17
|
+
}
|
|
18
|
+
if (!Array.isArray(data?.gateways)) throw new Error("config/gateways.json must contain a gateways array");
|
|
19
|
+
const seen = new Set();
|
|
20
|
+
return data.gateways.map((entry, index) => {
|
|
21
|
+
if (!isObject(entry)) throw new Error(`gateways[${index}] must be an object`);
|
|
22
|
+
const id = typeof entry.id === "string" ? entry.id.trim() : "";
|
|
23
|
+
if (!id) throw new Error(`gateways[${index}] lacks id`);
|
|
24
|
+
if (seen.has(id)) throw new Error(`duplicate gateway id ${id}`);
|
|
25
|
+
seen.add(id);
|
|
26
|
+
if (typeof entry.module !== "string" || !entry.module.trim()) {
|
|
27
|
+
throw new Error(`gateway ${id} lacks module`);
|
|
28
|
+
}
|
|
29
|
+
if (entry.enabled !== undefined && typeof entry.enabled !== "boolean") {
|
|
30
|
+
throw new Error(`gateway ${id} enabled must be boolean`);
|
|
31
|
+
}
|
|
32
|
+
return { id, enabled: entry.enabled !== false, module: entry.module.trim() };
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function enabledGatewayIds(root) {
|
|
37
|
+
return loadGateways(root).filter((entry) => entry.enabled).map((entry) => entry.id);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function gatewayProblems(result, expectedIds = null) {
|
|
41
|
+
const problems = [];
|
|
42
|
+
if (!isObject(result)) return ["gateway result must be an object"];
|
|
43
|
+
if (!Array.isArray(result.gateways) || result.gateways.length === 0) {
|
|
44
|
+
problems.push("gateways must be a non-empty array");
|
|
45
|
+
return problems;
|
|
46
|
+
}
|
|
47
|
+
const seen = new Set();
|
|
48
|
+
for (const [index, gateway] of result.gateways.entries()) {
|
|
49
|
+
if (!isObject(gateway)) {
|
|
50
|
+
problems.push(`gateways[${index}] must be an object`);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const id = typeof gateway.id === "string" ? gateway.id.trim() : "";
|
|
54
|
+
if (!id) problems.push(`gateways[${index}] lacks id`);
|
|
55
|
+
else if (seen.has(id)) problems.push(`gateways contains duplicate id ${id}`);
|
|
56
|
+
else seen.add(id);
|
|
57
|
+
if (!["PASS", "FAIL"].includes(gateway.status)) {
|
|
58
|
+
problems.push(`gateway ${id || index} status must be PASS or FAIL`);
|
|
59
|
+
} else if (gateway.status !== "PASS") {
|
|
60
|
+
problems.push(`gateway ${id || index} did not pass`);
|
|
61
|
+
}
|
|
62
|
+
if (!Array.isArray(gateway.actions)) problems.push(`gateway ${id || index} actions must be an array`);
|
|
63
|
+
if (typeof gateway.version !== "string") problems.push(`gateway ${id || index} version must be a string`);
|
|
64
|
+
if (!Array.isArray(gateway.evidence)) problems.push(`gateway ${id || index} evidence must be an array`);
|
|
65
|
+
}
|
|
66
|
+
if (Array.isArray(expectedIds)) {
|
|
67
|
+
for (const id of expectedIds) {
|
|
68
|
+
if (!seen.has(id)) problems.push(`enabled gateway ${id} is missing from gateway.json`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return problems;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function validateGatewayConfigEntry(entry) {
|
|
75
|
+
const problems = [];
|
|
76
|
+
if (!entry || typeof entry.id !== "string" || !entry.id.trim()) problems.push("id missing");
|
|
77
|
+
if (!entry || typeof entry.module !== "string" || !entry.module.trim()) problems.push("module missing");
|
|
78
|
+
return problems;
|
|
79
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/** Typed helpers over the Kandev External MCP tools and read-only HTTP routes. */
|
|
2
|
+
import { McpError } from "./mcp.mjs";
|
|
3
|
+
|
|
4
|
+
export function baseUrl() {
|
|
5
|
+
return (process.env.KANDEV_URL || "http://127.0.0.1:38429").replace(/\/$/, "");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
async function http(path) {
|
|
9
|
+
const res = await fetch(`${baseUrl()}${path}`);
|
|
10
|
+
if (!res.ok) throw new McpError(`GET ${path} -> HTTP ${res.status}`);
|
|
11
|
+
return res.json();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function listWorkspaces(mcp) {
|
|
15
|
+
const res = await mcp.call("list_workspaces_kandev");
|
|
16
|
+
return res?.workspaces || [];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function findWorkspace(mcp, name) {
|
|
20
|
+
const workspaces = await listWorkspaces(mcp);
|
|
21
|
+
const found = workspaces.find((w) => w.name === name);
|
|
22
|
+
if (!found) {
|
|
23
|
+
throw new Error(`Workspace "${name}" not found (have: ${workspaces.map((w) => w.name).join(", ") || "none"})`);
|
|
24
|
+
}
|
|
25
|
+
return found;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function listAgents(mcp) {
|
|
29
|
+
const res = await mcp.call("list_agents_kandev");
|
|
30
|
+
return res?.agents || [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function findAgent(mcp, agentId) {
|
|
34
|
+
const agents = await listAgents(mcp);
|
|
35
|
+
const found = agents.find((a) => a.id === agentId || a.name === agentId);
|
|
36
|
+
if (!found) throw new Error(`Agent "${agentId}" not found (have: ${agents.map((a) => a.name).join(", ")})`);
|
|
37
|
+
return found;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function listProfiles(mcp, agentId) {
|
|
41
|
+
const res = await mcp.call("list_agent_profiles_kandev", { agent_id: agentId });
|
|
42
|
+
return res?.profiles || [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function createProfile(mcp, { agentId, name, model, reasoningEffort, autoApprove = true }) {
|
|
46
|
+
const args = {
|
|
47
|
+
agent_id: agentId,
|
|
48
|
+
name,
|
|
49
|
+
model,
|
|
50
|
+
auto_approve: autoApprove,
|
|
51
|
+
};
|
|
52
|
+
if (reasoningEffort) {
|
|
53
|
+
args.settings = { config_options: { reasoning_effort: reasoningEffort } };
|
|
54
|
+
}
|
|
55
|
+
return mcp.call("create_agent_profile_kandev", args);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function updateProfile(mcp, { profileId, name, model, autoApprove }) {
|
|
59
|
+
const args = { profile_id: profileId };
|
|
60
|
+
if (name !== undefined) args.name = name;
|
|
61
|
+
if (model !== undefined) args.model = model;
|
|
62
|
+
if (autoApprove !== undefined) args.auto_approve = autoApprove;
|
|
63
|
+
return mcp.call("update_agent_profile_kandev", args);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function updateSettingsWithFallback(mcp, { target, changes, prefixedChanges }) {
|
|
67
|
+
try {
|
|
68
|
+
return await mcp.call("update_settings_kandev", { target, changes });
|
|
69
|
+
} catch (err) {
|
|
70
|
+
if (!prefixedChanges) throw err;
|
|
71
|
+
return mcp.call("update_settings_kandev", { target, changes: prefixedChanges });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function updateProfileConfigOptions(mcp, { profileId, configOptions }) {
|
|
76
|
+
return updateSettingsWithFallback(mcp, {
|
|
77
|
+
target: { resource_type: "agent_profile", resource_id: profileId },
|
|
78
|
+
changes: { config_options: configOptions },
|
|
79
|
+
prefixedChanges: { "agent_profile.config_options": configOptions },
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function getWorkspaceDefaultProfiles(mcp, workspaceId) {
|
|
84
|
+
const res = await mcp.call("get_settings_kandev", {
|
|
85
|
+
target: { resource_type: "workspace", resource_id: workspaceId },
|
|
86
|
+
keys: ["workspace.default_agent_profile_id", "workspace.default_config_agent_profile_id"],
|
|
87
|
+
});
|
|
88
|
+
const values = res?.values || {};
|
|
89
|
+
return {
|
|
90
|
+
default_agent_profile_id: values.default_agent_profile_id || "",
|
|
91
|
+
default_config_agent_profile_id: values.default_config_agent_profile_id || "",
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function updateWorkspaceDefaultProfiles(
|
|
96
|
+
mcp,
|
|
97
|
+
{ workspaceId, defaultAgentProfileId, defaultConfigAgentProfileId },
|
|
98
|
+
) {
|
|
99
|
+
return updateSettingsWithFallback(mcp, {
|
|
100
|
+
target: { resource_type: "workspace", resource_id: workspaceId },
|
|
101
|
+
changes: {
|
|
102
|
+
default_agent_profile_id: defaultAgentProfileId,
|
|
103
|
+
default_config_agent_profile_id: defaultConfigAgentProfileId,
|
|
104
|
+
},
|
|
105
|
+
prefixedChanges: {
|
|
106
|
+
"workspace.default_agent_profile_id": defaultAgentProfileId,
|
|
107
|
+
"workspace.default_config_agent_profile_id": defaultConfigAgentProfileId,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export async function getMcpConfig(mcp, profileId) {
|
|
113
|
+
return mcp.call("get_mcp_config_kandev", { profile_id: profileId });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function updateMcpConfig(mcp, profileId, servers, enabled = true) {
|
|
117
|
+
return mcp.call("update_mcp_config_kandev", {
|
|
118
|
+
profile_id: profileId,
|
|
119
|
+
enabled,
|
|
120
|
+
servers,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function listWorkflows(mcp, workspaceId) {
|
|
125
|
+
const res = await mcp.call("list_workflows_kandev", { workspace_id: workspaceId });
|
|
126
|
+
return res?.workflows || [];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function listTasks(mcp, workflowId) {
|
|
130
|
+
const res = await mcp.call("list_tasks_kandev", { workflow_id: workflowId });
|
|
131
|
+
return res?.tasks || [];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function listToolNames(mcp) {
|
|
135
|
+
const tools = await mcp.listTools();
|
|
136
|
+
return tools.map((tool) => tool.name);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export async function listTaskSessions(mcp, { taskId }) {
|
|
140
|
+
const res = await mcp.call("list_task_sessions_kandev", { task_id: taskId });
|
|
141
|
+
return res?.sessions || res?.items || [];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function getTaskConversation(mcp, {
|
|
145
|
+
taskId,
|
|
146
|
+
sessionId,
|
|
147
|
+
limit = 1,
|
|
148
|
+
sort = "desc",
|
|
149
|
+
messageTypes = ["message", "tool_call"],
|
|
150
|
+
} = {}) {
|
|
151
|
+
const args = { task_id: taskId, limit, sort, message_types: messageTypes };
|
|
152
|
+
if (sessionId) args.session_id = sessionId;
|
|
153
|
+
const res = await mcp.call("get_task_conversation_kandev", args);
|
|
154
|
+
return res?.messages || res?.items || res || [];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export async function messageTask(mcp, { taskId, sessionId, prompt }) {
|
|
158
|
+
const args = { task_id: taskId, prompt };
|
|
159
|
+
if (sessionId) args.session_id = sessionId;
|
|
160
|
+
return mcp.call("message_task_kandev", args);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export async function spawnSession(mcp, { taskId, prompt, name }) {
|
|
164
|
+
const args = { task_id: taskId, prompt };
|
|
165
|
+
if (name) args.name = name;
|
|
166
|
+
return mcp.call("spawn_session_kandev", args);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export async function moveTask(mcp, { taskId, workflowId, stepId, entryOptions }) {
|
|
170
|
+
const args = {
|
|
171
|
+
task_id: taskId,
|
|
172
|
+
workflow_id: workflowId,
|
|
173
|
+
workflow_step_id: stepId,
|
|
174
|
+
};
|
|
175
|
+
if (entryOptions) args.entry_options = entryOptions;
|
|
176
|
+
return mcp.call("move_task_kandev", args);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export async function updateTaskState(mcp, { taskId, state }) {
|
|
180
|
+
return mcp.call("update_task_state_kandev", { task_id: taskId, state });
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export async function createWorkflow(mcp, { workspaceId, name, description }) {
|
|
184
|
+
return mcp.call("create_workflow_kandev", { workspace_id: workspaceId, name, description });
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export async function updateWorkflow(mcp, { workflowId, name, description }) {
|
|
188
|
+
return mcp.call("update_workflow_kandev", { workflow_id: workflowId, name, description });
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export async function listSteps(mcp, workflowId) {
|
|
192
|
+
const res = await mcp.call("list_workflow_steps_kandev", { workflow_id: workflowId });
|
|
193
|
+
return res?.steps || [];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export async function createStep(mcp, { workflowId, step, agentProfileId }) {
|
|
197
|
+
return mcp.call("create_workflow_step_kandev", {
|
|
198
|
+
workflow_id: workflowId,
|
|
199
|
+
name: step.name,
|
|
200
|
+
position: step.position,
|
|
201
|
+
color: step.color,
|
|
202
|
+
prompt: step.prompt,
|
|
203
|
+
events: step.events,
|
|
204
|
+
is_start_step: step.is_start_step,
|
|
205
|
+
show_in_command_panel: step.show_in_command_panel,
|
|
206
|
+
allow_manual_move: step.allow_manual_move,
|
|
207
|
+
auto_advance_requires_signal: step.auto_advance_requires_signal,
|
|
208
|
+
cancel_triggers_turn_complete: step.cancel_triggers_turn_complete,
|
|
209
|
+
complete_task_on_enter: step.complete_task_on_enter,
|
|
210
|
+
agent_profile_id: agentProfileId,
|
|
211
|
+
profile_session_start_policy: "new",
|
|
212
|
+
profile_session_end_policy: "complete",
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export async function updateStep(mcp, { stepId, step, agentProfileId }) {
|
|
217
|
+
return mcp.call("update_workflow_step_kandev", {
|
|
218
|
+
step_id: stepId,
|
|
219
|
+
name: step.name,
|
|
220
|
+
color: step.color,
|
|
221
|
+
prompt: step.prompt,
|
|
222
|
+
events: step.events,
|
|
223
|
+
is_start_step: step.is_start_step,
|
|
224
|
+
show_in_command_panel: step.show_in_command_panel,
|
|
225
|
+
allow_manual_move: step.allow_manual_move,
|
|
226
|
+
auto_advance_requires_signal: step.auto_advance_requires_signal,
|
|
227
|
+
cancel_triggers_turn_complete: step.cancel_triggers_turn_complete,
|
|
228
|
+
complete_task_on_enter: step.complete_task_on_enter,
|
|
229
|
+
agent_profile_id: agentProfileId,
|
|
230
|
+
profile_session_start_policy: "new",
|
|
231
|
+
profile_session_end_policy: "complete",
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export async function deleteStep(mcp, stepId) {
|
|
236
|
+
return mcp.call("delete_workflow_step_kandev", { step_id: stepId });
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export async function reorderSteps(mcp, workflowId, stepIds) {
|
|
240
|
+
return mcp.call("reorder_workflow_steps_kandev", { workflow_id: workflowId, step_ids: stepIds });
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export async function exportWorkflow(mcp, workflowId) {
|
|
244
|
+
return mcp.call("export_workflow_kandev", { workflow_id: workflowId });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export async function advertisedModels(agentName) {
|
|
248
|
+
const res = await http(`/api/v1/agent-models/${encodeURIComponent(agentName)}`);
|
|
249
|
+
const list = (res.models || []).map((m) => m.id);
|
|
250
|
+
if (res.discovery?.allows_custom_model) {
|
|
251
|
+
try {
|
|
252
|
+
const fs = await import("node:fs");
|
|
253
|
+
const path = await import("node:path");
|
|
254
|
+
const os = await import("node:os");
|
|
255
|
+
const catalogPath = path.join(os.homedir(), ".codex", "opencodex-catalog.json");
|
|
256
|
+
if (fs.existsSync(catalogPath)) {
|
|
257
|
+
const catalog = JSON.parse(fs.readFileSync(catalogPath, "utf8"));
|
|
258
|
+
for (const m of catalog.models || []) {
|
|
259
|
+
if (m.slug && !list.includes(m.slug)) {
|
|
260
|
+
list.push(m.slug);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
} catch {
|
|
265
|
+
// ignore
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return list;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export async function health() {
|
|
272
|
+
return http("/api/v1/workspaces");
|
|
273
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal Kandev External MCP client (streamable HTTP, legacy initialize).
|
|
3
|
+
* Loopback only; no credentials are sent by this repository.
|
|
4
|
+
*/
|
|
5
|
+
export class McpError extends Error {
|
|
6
|
+
constructor(message, cause) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "McpError";
|
|
9
|
+
this.cause = cause;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class McpClient {
|
|
14
|
+
constructor({ url = process.env.KANDEV_URL || "http://127.0.0.1:38429", timeoutMs = 30000 } = {}) {
|
|
15
|
+
this.base = `${url.replace(/\/$/, "")}/mcp`;
|
|
16
|
+
this.timeoutMs = timeoutMs;
|
|
17
|
+
this.sessionId = null;
|
|
18
|
+
this.nextId = 1;
|
|
19
|
+
this.writeCalls = 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async #post(payload) {
|
|
23
|
+
const controller = new AbortController();
|
|
24
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
25
|
+
try {
|
|
26
|
+
const headers = {
|
|
27
|
+
"Content-Type": "application/json",
|
|
28
|
+
Accept: "application/json, text/event-stream",
|
|
29
|
+
};
|
|
30
|
+
if (this.sessionId) headers["Mcp-Session-Id"] = this.sessionId;
|
|
31
|
+
const res = await fetch(this.base, {
|
|
32
|
+
method: "POST",
|
|
33
|
+
headers,
|
|
34
|
+
body: JSON.stringify(payload),
|
|
35
|
+
signal: controller.signal,
|
|
36
|
+
});
|
|
37
|
+
const sid = res.headers.get("mcp-session-id");
|
|
38
|
+
if (sid) this.sessionId = sid;
|
|
39
|
+
const text = await res.text();
|
|
40
|
+
if (!res.ok) {
|
|
41
|
+
throw new McpError(`MCP HTTP ${res.status}: ${text.slice(0, 400)}`);
|
|
42
|
+
}
|
|
43
|
+
if (!text.trim()) return null;
|
|
44
|
+
let raw = text;
|
|
45
|
+
if (raw.startsWith("event:") || raw.startsWith("data:")) {
|
|
46
|
+
const line = raw.split("\n").find((l) => l.startsWith("data:"));
|
|
47
|
+
raw = line ? line.slice(5).trim() : "";
|
|
48
|
+
}
|
|
49
|
+
if (!raw) return null;
|
|
50
|
+
const msg = JSON.parse(raw);
|
|
51
|
+
if (msg.error) throw new McpError(`MCP ${msg.error.message || JSON.stringify(msg.error)}`);
|
|
52
|
+
return msg.result;
|
|
53
|
+
} catch (err) {
|
|
54
|
+
if (err instanceof McpError) throw err;
|
|
55
|
+
if (err.name === "AbortError") throw new McpError(`MCP timeout after ${this.timeoutMs}ms`);
|
|
56
|
+
throw new McpError(`MCP request failed: ${err.message}`, err);
|
|
57
|
+
} finally {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async connect() {
|
|
63
|
+
await this.#post({
|
|
64
|
+
jsonrpc: "2.0",
|
|
65
|
+
id: 0,
|
|
66
|
+
method: "initialize",
|
|
67
|
+
params: {
|
|
68
|
+
protocolVersion: "2025-06-18",
|
|
69
|
+
capabilities: {},
|
|
70
|
+
clientInfo: { name: "kandev-workflow-config", version: "0.1.0" },
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
await this.#post({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async listTools() {
|
|
78
|
+
const result = await this.#post({
|
|
79
|
+
jsonrpc: "2.0",
|
|
80
|
+
id: this.nextId++,
|
|
81
|
+
method: "tools/list",
|
|
82
|
+
params: {},
|
|
83
|
+
});
|
|
84
|
+
return result?.tools || [];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async call(name, args = {}) {
|
|
88
|
+
const result = await this.#post({
|
|
89
|
+
jsonrpc: "2.0",
|
|
90
|
+
id: this.nextId++,
|
|
91
|
+
method: "tools/call",
|
|
92
|
+
params: { name, arguments: args },
|
|
93
|
+
});
|
|
94
|
+
this.writeCalls += 1;
|
|
95
|
+
if (result?.isError) {
|
|
96
|
+
const text = (result.content || []).map((c) => c.text).join("\n");
|
|
97
|
+
throw new McpError(`${name} failed: ${text.slice(0, 500)}`);
|
|
98
|
+
}
|
|
99
|
+
const text = (result?.content || []).filter((c) => c.type === "text").map((c) => c.text).join("\n");
|
|
100
|
+
if (!text) return result;
|
|
101
|
+
try {
|
|
102
|
+
return JSON.parse(text);
|
|
103
|
+
} catch {
|
|
104
|
+
return text;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export async function connect() {
|
|
110
|
+
return new McpClient().connect();
|
|
111
|
+
}
|