@kkelly-offical/kkcode 0.1.2 → 0.1.6
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 +120 -178
- package/package.json +46 -46
- package/src/agent/agent.mjs +41 -0
- package/src/agent/prompt/frontend-designer.txt +58 -0
- package/src/agent/prompt/longagent-blueprint-agent.txt +83 -0
- package/src/agent/prompt/longagent-coding-agent.txt +37 -0
- package/src/agent/prompt/longagent-debugging-agent.txt +46 -0
- package/src/agent/prompt/longagent-preview-agent.txt +63 -0
- package/src/config/defaults.mjs +260 -195
- package/src/config/schema.mjs +71 -6
- package/src/core/constants.mjs +91 -46
- package/src/index.mjs +1 -1
- package/src/knowledge/frontend-aesthetics.txt +39 -0
- package/src/knowledge/loader.mjs +2 -1
- package/src/knowledge/tailwind.txt +12 -3
- package/src/mcp/client-http.mjs +141 -157
- package/src/mcp/client-sse.mjs +288 -286
- package/src/mcp/client-stdio.mjs +533 -451
- package/src/mcp/constants.mjs +2 -0
- package/src/mcp/registry.mjs +479 -394
- package/src/mcp/stdio-framing.mjs +133 -127
- package/src/mcp/tool-result.mjs +24 -0
- package/src/observability/index.mjs +42 -0
- package/src/observability/metrics.mjs +137 -0
- package/src/observability/tracer.mjs +137 -0
- package/src/orchestration/background-manager.mjs +372 -358
- package/src/orchestration/background-worker.mjs +305 -245
- package/src/orchestration/longagent-manager.mjs +171 -116
- package/src/orchestration/stage-scheduler.mjs +728 -489
- package/src/permission/exec-policy.mjs +9 -11
- package/src/provider/anthropic.mjs +1 -0
- package/src/provider/openai.mjs +340 -339
- package/src/provider/retry-policy.mjs +68 -68
- package/src/provider/router.mjs +241 -228
- package/src/provider/sse.mjs +104 -91
- package/src/repl.mjs +1 -1
- package/src/session/checkpoint.mjs +66 -3
- package/src/session/engine.mjs +227 -225
- package/src/session/longagent-4stage.mjs +460 -0
- package/src/session/longagent-hybrid.mjs +1081 -0
- package/src/session/longagent-plan.mjs +365 -329
- package/src/session/longagent-project-memory.mjs +53 -0
- package/src/session/longagent-scaffold.mjs +291 -100
- package/src/session/longagent-task-bus.mjs +54 -0
- package/src/session/longagent-utils.mjs +472 -0
- package/src/session/longagent.mjs +884 -1462
- package/src/session/project-context.mjs +30 -0
- package/src/session/store.mjs +510 -503
- package/src/session/task-validator.mjs +4 -3
- package/src/skill/builtin/design.mjs +76 -0
- package/src/skill/builtin/frontend.mjs +8 -0
- package/src/skill/registry.mjs +390 -336
- package/src/storage/ghost-commit-store.mjs +18 -8
- package/src/tool/executor.mjs +11 -0
- package/src/tool/git-auto.mjs +0 -19
- package/src/tool/registry.mjs +71 -37
- package/src/ui/activity-renderer.mjs +664 -410
- package/src/util/git.mjs +23 -0
package/src/core/constants.mjs
CHANGED
|
@@ -1,46 +1,91 @@
|
|
|
1
|
-
export const MODES = ["ask", "plan", "agent", "longagent"]
|
|
2
|
-
|
|
3
|
-
export const TOOL_STATUSES = ["running", "completed", "error", "cancelled"]
|
|
4
|
-
|
|
5
|
-
export const PERMISSION_DECISIONS = ["allow_once", "allow_session", "deny"]
|
|
6
|
-
|
|
7
|
-
export const DEFAULT_MAX_STEPS = 8
|
|
8
|
-
export const DEFAULT_REQUEST_TIMEOUT_MS = 120000
|
|
9
|
-
export const DEFAULT_RETRY_ATTEMPTS = 3
|
|
10
|
-
export const DEFAULT_LONGAGENT_RETRY_STORM_THRESHOLD = 3
|
|
11
|
-
export const DEFAULT_LONGAGENT_TOKEN_ALERT_THRESHOLD = 120000
|
|
12
|
-
|
|
13
|
-
export const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
1
|
+
export const MODES = ["ask", "plan", "agent", "longagent"]
|
|
2
|
+
|
|
3
|
+
export const TOOL_STATUSES = ["running", "completed", "error", "cancelled"]
|
|
4
|
+
|
|
5
|
+
export const PERMISSION_DECISIONS = ["allow_once", "allow_session", "deny"]
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_MAX_STEPS = 8
|
|
8
|
+
export const DEFAULT_REQUEST_TIMEOUT_MS = 120000
|
|
9
|
+
export const DEFAULT_RETRY_ATTEMPTS = 3
|
|
10
|
+
export const DEFAULT_LONGAGENT_RETRY_STORM_THRESHOLD = 3
|
|
11
|
+
export const DEFAULT_LONGAGENT_TOKEN_ALERT_THRESHOLD = 120000
|
|
12
|
+
|
|
13
|
+
export const LONGAGENT_4STAGE_STAGES = {
|
|
14
|
+
PREVIEW: "preview",
|
|
15
|
+
BLUEPRINT: "blueprint",
|
|
16
|
+
CODING: "coding",
|
|
17
|
+
DEBUGGING: "debugging"
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const EVENT_TYPES = {
|
|
21
|
+
TURN_START: "turn.start",
|
|
22
|
+
TURN_STEP_START: "turn.step.start",
|
|
23
|
+
TURN_STEP_FINISH: "turn.step.finish",
|
|
24
|
+
TURN_FINISH: "turn.finish",
|
|
25
|
+
TURN_ERROR: "turn.error",
|
|
26
|
+
TOOL_START: "tool.start",
|
|
27
|
+
TOOL_FINISH: "tool.finish",
|
|
28
|
+
TOOL_ERROR: "tool.error",
|
|
29
|
+
PERMISSION_ASKED: "permission.asked",
|
|
30
|
+
PERMISSION_DECIDED: "permission.decided",
|
|
31
|
+
REVIEW_DECISION: "review.decision",
|
|
32
|
+
MCP_HEALTH: "mcp.health",
|
|
33
|
+
MCP_REQUEST: "mcp.request",
|
|
34
|
+
MCP_RECONNECT: "mcp.reconnect",
|
|
35
|
+
MCP_CIRCUIT_OPEN: "mcp.circuit_open",
|
|
36
|
+
MCP_CIRCUIT_CLOSE: "mcp.circuit_close",
|
|
37
|
+
LONGAGENT_HEARTBEAT: "longagent.heartbeat",
|
|
38
|
+
LONGAGENT_ALERT: "longagent.alert",
|
|
39
|
+
LONGAGENT_PHASE_CHANGED: "longagent.phase.changed",
|
|
40
|
+
LONGAGENT_GATE_CHECKED: "longagent.gate.checked",
|
|
41
|
+
LONGAGENT_RECOVERY_ENTERED: "longagent.recovery.entered",
|
|
42
|
+
LONGAGENT_INTAKE_STARTED: "longagent.intake.started",
|
|
43
|
+
LONGAGENT_PLAN_FROZEN: "longagent.plan.frozen",
|
|
44
|
+
LONGAGENT_STAGE_STARTED: "longagent.stage.started",
|
|
45
|
+
LONGAGENT_STAGE_TASK_DISPATCHED: "longagent.stage.task.dispatched",
|
|
46
|
+
LONGAGENT_STAGE_TASK_FINISHED: "longagent.stage.task.finished",
|
|
47
|
+
LONGAGENT_STAGE_FINISHED: "longagent.stage.finished",
|
|
48
|
+
LONGAGENT_SCAFFOLD_COMPLETE: "longagent.scaffold.complete",
|
|
49
|
+
LONGAGENT_GIT_BRANCH_CREATED: "longagent.git.branch.created",
|
|
50
|
+
LONGAGENT_GIT_STAGE_COMMITTED: "longagent.git.stage.committed",
|
|
51
|
+
LONGAGENT_GIT_MERGED: "longagent.git.merged",
|
|
52
|
+
LONGAGENT_4STAGE_PREVIEW_START: "longagent.4stage.preview.start",
|
|
53
|
+
LONGAGENT_4STAGE_PREVIEW_COMPLETE: "longagent.4stage.preview.complete",
|
|
54
|
+
LONGAGENT_4STAGE_BLUEPRINT_START: "longagent.4stage.blueprint.start",
|
|
55
|
+
LONGAGENT_4STAGE_BLUEPRINT_COMPLETE: "longagent.4stage.blueprint.complete",
|
|
56
|
+
LONGAGENT_4STAGE_CODING_START: "longagent.4stage.coding.start",
|
|
57
|
+
LONGAGENT_4STAGE_CODING_COMPLETE: "longagent.4stage.coding.complete",
|
|
58
|
+
LONGAGENT_4STAGE_DEBUGGING_START: "longagent.4stage.debugging.start",
|
|
59
|
+
LONGAGENT_4STAGE_DEBUGGING_COMPLETE: "longagent.4stage.debugging.complete",
|
|
60
|
+
LONGAGENT_4STAGE_RETURN_TO_CODING: "longagent.4stage.return_to_coding",
|
|
61
|
+
LONGAGENT_HYBRID_PREVIEW_START: "longagent.hybrid.preview.start",
|
|
62
|
+
LONGAGENT_HYBRID_PREVIEW_COMPLETE: "longagent.hybrid.preview.complete",
|
|
63
|
+
LONGAGENT_HYBRID_BLUEPRINT_START: "longagent.hybrid.blueprint.start",
|
|
64
|
+
LONGAGENT_HYBRID_BLUEPRINT_COMPLETE: "longagent.hybrid.blueprint.complete",
|
|
65
|
+
LONGAGENT_HYBRID_DEBUGGING_START: "longagent.hybrid.debugging.start",
|
|
66
|
+
LONGAGENT_HYBRID_DEBUGGING_COMPLETE: "longagent.hybrid.debugging.complete",
|
|
67
|
+
LONGAGENT_HYBRID_RETURN_TO_CODING: "longagent.hybrid.return_to_coding",
|
|
68
|
+
LONGAGENT_HYBRID_BLUEPRINT_REVIEW: "longagent.hybrid.blueprint.review",
|
|
69
|
+
LONGAGENT_HYBRID_BLUEPRINT_VALIDATED: "longagent.hybrid.blueprint.validated",
|
|
70
|
+
LONGAGENT_HYBRID_CROSS_REVIEW: "longagent.hybrid.cross_review",
|
|
71
|
+
LONGAGENT_HYBRID_INCREMENTAL_GATE: "longagent.hybrid.incremental_gate",
|
|
72
|
+
LONGAGENT_HYBRID_CONTEXT_COMPRESSED: "longagent.hybrid.context_compressed",
|
|
73
|
+
LONGAGENT_HYBRID_BUDGET_WARNING: "longagent.hybrid.budget_warning",
|
|
74
|
+
LONGAGENT_HYBRID_CHECKPOINT_RESUMED: "longagent.hybrid.checkpoint_resumed",
|
|
75
|
+
LONGAGENT_HYBRID_REPLAN: "longagent.hybrid.replan",
|
|
76
|
+
LONGAGENT_HYBRID_MEMORY_LOADED: "longagent.hybrid.memory_loaded",
|
|
77
|
+
LONGAGENT_HYBRID_MEMORY_SAVED: "longagent.hybrid.memory_saved",
|
|
78
|
+
SESSION_COMPACTED: "session.compacted",
|
|
79
|
+
TURN_USAGE_UPDATE: "turn.usage.update",
|
|
80
|
+
STREAM_TEXT_START: "stream.text.start",
|
|
81
|
+
STREAM_THINKING_START: "stream.thinking.start",
|
|
82
|
+
LONGAGENT_DEGRADATION_APPLIED: "longagent.degradation.applied",
|
|
83
|
+
LONGAGENT_WRITE_LOOP_DETECTED: "longagent.write_loop.detected",
|
|
84
|
+
LONGAGENT_SEMANTIC_ERROR_REPEATED: "longagent.semantic_error.repeated",
|
|
85
|
+
LONGAGENT_PHASE_TIMEOUT: "longagent.phase.timeout",
|
|
86
|
+
LONGAGENT_GIT_CONFLICT_RESOLUTION: "longagent.git.conflict_resolution",
|
|
87
|
+
LONGAGENT_CHECKPOINT_CLEANED: "longagent.checkpoint.cleaned",
|
|
88
|
+
LONGAGENT_HYBRID_CHECKPOINT_INVALID: "longagent.hybrid.checkpoint_invalid",
|
|
89
|
+
LONGAGENT_STAGE_TASK_SKIPPED: "longagent.stage.task.skipped",
|
|
90
|
+
PROVIDER_FALLBACK: "provider.fallback"
|
|
91
|
+
}
|
package/src/index.mjs
CHANGED
|
@@ -54,7 +54,7 @@ async function main() {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
const program = new Command()
|
|
57
|
-
program.name("kkcode").description("kkcode CLI").version("0.1.
|
|
57
|
+
program.name("kkcode").description("kkcode CLI").version("0.1.6")
|
|
58
58
|
program.addCommand(createChatCommand())
|
|
59
59
|
program.addCommand(createThemeCommand())
|
|
60
60
|
program.addCommand(createUsageCommand())
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
<frontend_aesthetics>
|
|
2
|
+
## Frontend Design Quality Rules
|
|
3
|
+
|
|
4
|
+
When building frontend UI, avoid generic "AI-generated" aesthetics. Make creative, distinctive choices.
|
|
5
|
+
|
|
6
|
+
### Typography
|
|
7
|
+
- Avoid default fonts (Inter, Roboto, Arial, system-ui). Choose distinctive fonts.
|
|
8
|
+
- Use high contrast: pair display fonts with monospace, serif with geometric sans.
|
|
9
|
+
- Use extreme weight differences (200 vs 800) and 3x+ size jumps.
|
|
10
|
+
- Load fonts from Google Fonts via <link> or @import.
|
|
11
|
+
|
|
12
|
+
### Color
|
|
13
|
+
- Define ALL colors as CSS variables in :root.
|
|
14
|
+
- Use a dominant color with 1-2 sharp accents. Avoid evenly-distributed palettes.
|
|
15
|
+
- Draw from real palettes: IDE themes (Nord, Catppuccin, Dracula), nature, cultural aesthetics.
|
|
16
|
+
- AVOID: purple gradients on white, generic blue buttons, gray-on-gray cards.
|
|
17
|
+
|
|
18
|
+
### Motion
|
|
19
|
+
- One high-impact animation per page (staggered reveal on load).
|
|
20
|
+
- Micro-interactions: hover lift, focus glow, press feedback.
|
|
21
|
+
- Use CSS transitions/animations. Use animation-delay for staggered effects.
|
|
22
|
+
|
|
23
|
+
### Layout
|
|
24
|
+
- CSS Grid for page structure, Flexbox for components.
|
|
25
|
+
- Generous whitespace. Consistent spacing scale (4/8/12/16/24/32/48/64px).
|
|
26
|
+
- Mobile-first responsive design.
|
|
27
|
+
|
|
28
|
+
### Depth & Atmosphere
|
|
29
|
+
- Layered gradients, subtle patterns, backdrop-filter for glass effects.
|
|
30
|
+
- Elevation hierarchy via box-shadow (not just border).
|
|
31
|
+
- Noise/grain textures for premium feel.
|
|
32
|
+
|
|
33
|
+
### What NOT to do
|
|
34
|
+
- Cookie-cutter card grids with identical rounded corners
|
|
35
|
+
- Generic hero with centered text + gradient
|
|
36
|
+
- border-radius: 9999px on everything
|
|
37
|
+
- Gray placeholder text that looks like wireframe
|
|
38
|
+
- No visual rhythm (identical spacing everywhere)
|
|
39
|
+
</frontend_aesthetics>
|
package/src/knowledge/loader.mjs
CHANGED
|
@@ -58,7 +58,8 @@ const LANGUAGE_MAP = {
|
|
|
58
58
|
// Project type → scenario knowledge
|
|
59
59
|
const TYPE_MAP = {
|
|
60
60
|
backend: ["api-design.txt"],
|
|
61
|
-
fullstack: ["api-design.txt"],
|
|
61
|
+
fullstack: ["api-design.txt", "frontend-aesthetics.txt"],
|
|
62
|
+
frontend: ["frontend-aesthetics.txt"],
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
// Feature → knowledge file
|
|
@@ -4,7 +4,16 @@ Tailwind CSS conventions:
|
|
|
4
4
|
- Dark mode: dark: prefix with class or media strategy
|
|
5
5
|
- Component patterns: extract repeated patterns with @apply in CSS or component abstraction
|
|
6
6
|
- Spacing scale: p-4 = 1rem, m-2 = 0.5rem (4px base)
|
|
7
|
-
- Colors: bg-blue-500, text-gray-900, use project's color palette
|
|
7
|
+
- Colors: bg-blue-500, text-gray-900, use project's color palette from tailwind.config
|
|
8
8
|
- Layout: flex, grid, container mx-auto, gap utilities
|
|
9
|
-
- Customization: tailwind.config.js for theme extension
|
|
10
|
-
- Avoid: inline styles, arbitrary values when a utility exists
|
|
9
|
+
- Customization: tailwind.config.js for theme extension (colors, fonts, spacing)
|
|
10
|
+
- Avoid: inline styles, arbitrary values when a utility exists
|
|
11
|
+
|
|
12
|
+
Design quality with Tailwind:
|
|
13
|
+
- READ tailwind.config.js first — use the project's defined colors/fonts/spacing
|
|
14
|
+
- Prefer semantic color names (primary, accent, muted) over raw colors (blue-500)
|
|
15
|
+
- Use ring utilities for focus states (ring-2 ring-offset-2)
|
|
16
|
+
- Transition utilities: transition-all duration-200 ease-in-out for interactions
|
|
17
|
+
- Shadow scale: shadow-sm → shadow → shadow-md → shadow-lg for elevation
|
|
18
|
+
- Use group and group-hover for parent-child hover effects
|
|
19
|
+
- Animate: animate-pulse for loading, animate-spin for spinners
|
package/src/mcp/client-http.mjs
CHANGED
|
@@ -1,157 +1,141 @@
|
|
|
1
|
-
import { McpError } from "../core/errors.mjs"
|
|
2
|
-
import { EventBus } from "../core/events.mjs"
|
|
3
|
-
import { EVENT_TYPES } from "../core/constants.mjs"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (msg.includes("
|
|
15
|
-
if (
|
|
16
|
-
if (status && status >=
|
|
17
|
-
return "
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const result = await requestJson({
|
|
143
|
-
serverName,
|
|
144
|
-
method: "POST",
|
|
145
|
-
url: `${baseUrl}/tools/${encodeURIComponent(name)}`,
|
|
146
|
-
body: { args },
|
|
147
|
-
timeoutMs,
|
|
148
|
-
headers,
|
|
149
|
-
signal
|
|
150
|
-
})
|
|
151
|
-
return normalizeToolResult(result, serverName, name)
|
|
152
|
-
},
|
|
153
|
-
shutdown() {
|
|
154
|
-
// HTTP client is stateless — no persistent connections to clean up
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
1
|
+
import { McpError } from "../core/errors.mjs"
|
|
2
|
+
import { EventBus } from "../core/events.mjs"
|
|
3
|
+
import { EVENT_TYPES } from "../core/constants.mjs"
|
|
4
|
+
import { normalizeToolResult } from "./tool-result.mjs"
|
|
5
|
+
|
|
6
|
+
function timeoutSignal(ms, parentSignal = null) {
|
|
7
|
+
const own = AbortSignal.timeout(ms)
|
|
8
|
+
if (!parentSignal) return own
|
|
9
|
+
return AbortSignal.any([parentSignal, own])
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function classifyHttpError(error, status = null) {
|
|
13
|
+
const msg = String(error?.message || error || "")
|
|
14
|
+
if (msg.includes("AbortError") || msg.includes("timeout") || msg.includes("abort")) return "timeout"
|
|
15
|
+
if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) return "connection_refused"
|
|
16
|
+
if (status && status >= 500) return "server_crash"
|
|
17
|
+
if (status && status >= 400) return "bad_response"
|
|
18
|
+
return "unknown"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function requestJson({ serverName, method, url, body = null, timeoutMs = 10000, headers = {}, signal = null }) {
|
|
22
|
+
const action = method === "GET" ? url.split("/").pop() : body?.args ? "call_tool" : "request"
|
|
23
|
+
const startedAt = Date.now()
|
|
24
|
+
let status = null
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const res = await fetch(url, {
|
|
28
|
+
method,
|
|
29
|
+
headers: {
|
|
30
|
+
"content-type": "application/json",
|
|
31
|
+
...headers
|
|
32
|
+
},
|
|
33
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
34
|
+
signal: timeoutSignal(timeoutMs, signal)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
status = res.status
|
|
38
|
+
const elapsed = Date.now() - startedAt
|
|
39
|
+
|
|
40
|
+
EventBus.emit({
|
|
41
|
+
type: EVENT_TYPES.MCP_REQUEST,
|
|
42
|
+
payload: { server: serverName, action, method, elapsed, status }
|
|
43
|
+
}).catch(() => {})
|
|
44
|
+
|
|
45
|
+
if (!res.ok) {
|
|
46
|
+
const text = await res.text().catch(() => "")
|
|
47
|
+
const reason = classifyHttpError(null, status)
|
|
48
|
+
throw new McpError(
|
|
49
|
+
`mcp server "${serverName}" HTTP ${status} on ${method} ${url}: ${text.slice(0, 500)}`,
|
|
50
|
+
{ reason, server: serverName, action, phase: "request", statusCode: status }
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
return res.json().catch((parseErr) => {
|
|
54
|
+
const action = body?.args ? "call_tool" : "request"
|
|
55
|
+
EventBus.emit({
|
|
56
|
+
type: EVENT_TYPES.MCP_REQUEST,
|
|
57
|
+
payload: { server: serverName, action, warning: "malformed_json_response", detail: parseErr.message }
|
|
58
|
+
}).catch(() => {})
|
|
59
|
+
return {}
|
|
60
|
+
})
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (error instanceof McpError) throw error
|
|
63
|
+
const reason = classifyHttpError(error, status)
|
|
64
|
+
throw new McpError(
|
|
65
|
+
`mcp server "${serverName}" ${reason} on ${method} ${url}: ${error.message}`,
|
|
66
|
+
{ reason, server: serverName, action, phase: "request", statusCode: status }
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function createHttpMcpClient(serverName, config) {
|
|
72
|
+
const baseUrl = String(config.url || "").replace(/\/$/, "")
|
|
73
|
+
const timeoutMs = Number(config.timeout_ms || 10000)
|
|
74
|
+
const headers = config.headers || {}
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
serverName,
|
|
78
|
+
transport: "http",
|
|
79
|
+
async health() {
|
|
80
|
+
try {
|
|
81
|
+
await requestJson({ serverName, method: "GET", url: `${baseUrl}/health`, timeoutMs, headers })
|
|
82
|
+
return { ok: true }
|
|
83
|
+
} catch (error) {
|
|
84
|
+
return { ok: false, error: error.message, reason: error.reason || "unknown" }
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
async listTools() {
|
|
88
|
+
const out = await requestJson({ serverName, method: "GET", url: `${baseUrl}/tools`, timeoutMs, headers })
|
|
89
|
+
return Array.isArray(out?.tools) ? out.tools : []
|
|
90
|
+
},
|
|
91
|
+
async listPrompts() {
|
|
92
|
+
try {
|
|
93
|
+
const out = await requestJson({ serverName, method: "GET", url: `${baseUrl}/prompts`, timeoutMs, headers })
|
|
94
|
+
return Array.isArray(out?.prompts) ? out.prompts : []
|
|
95
|
+
} catch {
|
|
96
|
+
return []
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
async getPrompt(name, args = {}) {
|
|
100
|
+
return requestJson({
|
|
101
|
+
serverName,
|
|
102
|
+
method: "POST",
|
|
103
|
+
url: `${baseUrl}/prompts/${encodeURIComponent(name)}`,
|
|
104
|
+
body: { arguments: args },
|
|
105
|
+
timeoutMs,
|
|
106
|
+
headers
|
|
107
|
+
})
|
|
108
|
+
},
|
|
109
|
+
async listResources() {
|
|
110
|
+
try {
|
|
111
|
+
const out = await requestJson({ serverName, method: "GET", url: `${baseUrl}/resources`, timeoutMs, headers })
|
|
112
|
+
return Array.isArray(out?.resources) ? out.resources : []
|
|
113
|
+
} catch {
|
|
114
|
+
return []
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
async listTemplates() {
|
|
118
|
+
try {
|
|
119
|
+
const out = await requestJson({ serverName, method: "GET", url: `${baseUrl}/templates`, timeoutMs, headers })
|
|
120
|
+
return Array.isArray(out?.templates) ? out.templates : []
|
|
121
|
+
} catch {
|
|
122
|
+
return []
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
async callTool(name, args = {}, signal = null) {
|
|
126
|
+
const result = await requestJson({
|
|
127
|
+
serverName,
|
|
128
|
+
method: "POST",
|
|
129
|
+
url: `${baseUrl}/tools/${encodeURIComponent(name)}`,
|
|
130
|
+
body: { args },
|
|
131
|
+
timeoutMs,
|
|
132
|
+
headers,
|
|
133
|
+
signal
|
|
134
|
+
})
|
|
135
|
+
return normalizeToolResult(result, serverName, name)
|
|
136
|
+
},
|
|
137
|
+
shutdown() {
|
|
138
|
+
// HTTP client is stateless — no persistent connections to clean up
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|