@krisada-m/ksd-intelligence 0.0.0-stage → 1.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/.antigravity-plugin/plugin.json +20 -0
- package/LICENSE +21 -0
- package/README.md +195 -2
- package/agy-skills/erosiano-review/SKILL.md +17 -0
- package/dist/adapters/agy/skills.js +252 -0
- package/dist/adapters/cli/check-changed.js +3 -0
- package/dist/adapters/cli/main.js +1013 -0
- package/dist/adapters/cli/policy-bin.js +10 -0
- package/dist/adapters/cli/review-verdict.js +3 -0
- package/dist/adapters/omp/extension.js +884 -0
- package/dist/adapters/omp/skills.js +130 -0
- package/dist/adapters/ui/hud.js +389 -0
- package/dist/adapters/ui/tree.js +471 -0
- package/dist/core/audit/index.js +2 -0
- package/dist/core/audit/log.js +505 -0
- package/dist/core/audit/summary.js +188 -0
- package/dist/core/changes/changes.js +591 -0
- package/dist/core/changes/git.js +266 -0
- package/dist/core/changes/plan.js +109 -0
- package/dist/core/changes/render.js +356 -0
- package/dist/core/engine/build.js +178 -0
- package/dist/core/engine/child.js +389 -0
- package/dist/core/engine/host.js +206 -0
- package/dist/core/engine/lock.js +133 -0
- package/dist/core/engine/protocol.js +43 -0
- package/dist/core/engine/runtime.js +44 -0
- package/dist/core/engine/status.js +452 -0
- package/dist/core/engine/typescript.js +194 -0
- package/dist/core/engine/workspace.js +72 -0
- package/dist/core/graph/graph.js +127 -0
- package/dist/core/graph/limits.js +16 -0
- package/dist/core/impact/query.js +1292 -0
- package/dist/core/impact/render.js +590 -0
- package/dist/core/impact/session.js +694 -0
- package/dist/core/indexing/declarations.js +455 -0
- package/dist/core/indexing/differential.js +345 -0
- package/dist/core/indexing/doctor.js +120 -0
- package/dist/core/indexing/indexer.js +811 -0
- package/dist/core/indexing/keys.js +451 -0
- package/dist/core/indexing/lexical.js +212 -0
- package/dist/core/indexing/packages.js +57 -0
- package/dist/core/indexing/program.js +171 -0
- package/dist/core/indexing/resolve.js +1004 -0
- package/dist/core/indexing/stamp.js +52 -0
- package/dist/core/indexing/surface.js +335 -0
- package/dist/core/indexing/ui-progress.js +37 -0
- package/dist/core/indexing/verify.js +76 -0
- package/dist/core/kernel/config.js +383 -0
- package/dist/core/kernel/glob.js +105 -0
- package/dist/core/kernel/guards.js +4 -0
- package/dist/core/kernel/types.js +3 -0
- package/dist/core/kernel/verdict.js +142 -0
- package/dist/core/knowledge/anchors.js +139 -0
- package/dist/core/knowledge/knowledge.js +95 -0
- package/dist/core/knowledge/learning.js +102 -0
- package/dist/core/knowledge/render.js +76 -0
- package/dist/core/knowledge/store.js +109 -0
- package/dist/core/knowledge/yaml.js +87 -0
- package/dist/core/search/lexical.js +382 -0
- package/dist/core/search/render.js +98 -0
- package/dist/core/search/segments.js +118 -0
- package/dist/core/verify/parse.js +127 -0
- package/dist/core/verify/render.js +67 -0
- package/dist/core/verify/report.js +40 -0
- package/dist/core/verify/revision.js +79 -0
- package/dist/core/verify/run.js +117 -0
- package/dist/core/verify/store.js +66 -0
- package/dist/core/verify/verify.js +173 -0
- package/dist/core/workspace/init.js +267 -0
- package/dist/core/workspace/probe.js +56 -0
- package/dist/core/workspace/scan.js +420 -0
- package/dist/engineering/baseline.js +337 -0
- package/dist/engineering/change-scope.js +500 -0
- package/dist/engineering/check-changed.js +40 -0
- package/dist/engineering/config.js +129 -0
- package/dist/engineering/dist/policy.d.ts +17 -0
- package/dist/engineering/dist/policy.js +150 -0
- package/dist/engineering/dist/runtime.d.ts +40 -0
- package/dist/engineering/dist/runtime.js +77 -0
- package/dist/engineering/dist/sdlc.d.ts +7 -0
- package/dist/engineering/dist/sdlc.js +27 -0
- package/dist/engineering/eslint-rules/index.js +107 -0
- package/dist/engineering/index.d.ts +7 -0
- package/dist/engineering/index.js +319 -0
- package/dist/engineering/intelligence.js +44 -0
- package/dist/engineering/package.json +36 -0
- package/dist/engineering/policy.js +2 -0
- package/dist/engineering/practice-tool.js +72 -0
- package/dist/engineering/practices.js +283 -0
- package/dist/engineering/practices.json +2453 -0
- package/dist/engineering/review-verdict.js +262 -0
- package/dist/engineering/sdlc.js +2 -0
- package/dist/index.js +20 -0
- package/dist/storage/records.js +245 -0
- package/dist/storage/sqlite.js +314 -0
- package/dist/workflow/catalog.js +69 -0
- package/dist/workflow/classify.js +55 -0
- package/dist/workflow/loader.js +140 -0
- package/dist/workflow/packs.js +469 -0
- package/dist/workflow/registry.js +359 -0
- package/dist/workflow/render.js +61 -0
- package/dist/workflow/router.js +141 -0
- package/dist/workflow/sources.js +229 -0
- package/dist/workflow/types.js +44 -0
- package/dist/workflow/updates.js +532 -0
- package/engineering.lock.json +94 -0
- package/package.json +81 -4
- package/rules/engineering.md +20 -0
- package/scripts/agy-mcp.mjs +27 -0
- package/scripts/mcp/engineering.mjs +91 -0
- package/scripts/mcp/stdio.mjs +304 -0
- package/scripts/mcp/tools.mjs +491 -0
- package/skills/ksd-intelligence/SKILL.md +54 -0
- package/skills/matt-sdlc/SKILL.md +42 -0
- package/skills/osmani-quality/SKILL.md +38 -0
- package/skills/poteto-mode/SKILL.md +40 -0
- package/skills/pstack-analysis/SKILL.md +36 -0
- package/skills.lock.json +71 -0
- package/skills.registry.json +181 -0
package/package.json
CHANGED
|
@@ -1,6 +1,83 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krisada-m/ksd-intelligence",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Local-first engineering and code intelligence for OMP and AGY",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Krisada M. (https://github.com/Krisada-M)",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/Krisada-M/ksd-intelligence.git"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/Krisada-M/ksd-intelligence#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/Krisada-M/ksd-intelligence/issues"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public",
|
|
18
|
+
"provenance": true
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/",
|
|
22
|
+
"scripts/agy-mcp.mjs",
|
|
23
|
+
"scripts/mcp/",
|
|
24
|
+
"skills/",
|
|
25
|
+
"skills.lock.json",
|
|
26
|
+
"skills.registry.json",
|
|
27
|
+
"LICENSE",
|
|
28
|
+
"README.md",
|
|
29
|
+
"engineering.lock.json",
|
|
30
|
+
".antigravity-plugin/",
|
|
31
|
+
"rules/",
|
|
32
|
+
"agy-skills/"
|
|
33
|
+
],
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=22.5"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc -p tsconfig.json && node scripts/copy-policy-to-dist.mjs",
|
|
39
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tests/tsconfig.json --noEmit",
|
|
40
|
+
"test": "npm run build && tsc -p tests/tsconfig.json && node scripts/copy-policy-to-dist.mjs --test-tree && node --import ./tests/policy/helpers/isolated-home.js --test dist-test/tests/core/*.test.js dist-test/tests/workflow/*.test.js tests/policy/*.test.js tests/packaging/*.test.js",
|
|
41
|
+
"check:dist": "npm run build && git diff --exit-code dist/ && node scripts/check-dist-untracked.mjs",
|
|
42
|
+
"verify:skills": "node scripts/verify-unified-skills.mjs",
|
|
43
|
+
"lint": "eslint .",
|
|
44
|
+
"lint:fix": "eslint --fix .",
|
|
45
|
+
"format:check": "prettier --check .",
|
|
46
|
+
"format": "prettier --write .",
|
|
47
|
+
"prepare": "husky || true",
|
|
48
|
+
"verify:engineering": "node scripts/engineering-bundle.mjs"
|
|
49
|
+
},
|
|
50
|
+
"bin": {
|
|
51
|
+
"ei": "dist/adapters/cli/main.js",
|
|
52
|
+
"ksd-check-changed": "dist/adapters/cli/check-changed.js",
|
|
53
|
+
"ksd-review-verdict": "dist/adapters/cli/review-verdict.js",
|
|
54
|
+
"erosiano-baseline": "dist/engineering/baseline.js",
|
|
55
|
+
"erosiano-practices": "dist/engineering/practices.js"
|
|
56
|
+
},
|
|
57
|
+
"exports": {
|
|
58
|
+
".": "./dist/adapters/omp/extension.js",
|
|
59
|
+
"./eslint": "./dist/engineering/eslint-rules/index.js"
|
|
60
|
+
},
|
|
61
|
+
"omp": {
|
|
62
|
+
"extensions": [
|
|
63
|
+
"./dist/adapters/omp/extension.js"
|
|
64
|
+
]
|
|
65
|
+
},
|
|
66
|
+
"pi": {
|
|
67
|
+
"extensions": [
|
|
68
|
+
"./dist/adapters/omp/extension.js"
|
|
69
|
+
]
|
|
70
|
+
},
|
|
71
|
+
"eiRuntime": {
|
|
72
|
+
"typescript": "5.9.3"
|
|
73
|
+
},
|
|
74
|
+
"devDependencies": {
|
|
75
|
+
"@types/node": "^22.20.5",
|
|
76
|
+
"eslint": "10.13.0",
|
|
77
|
+
"eslint-config-prettier": "^10.1.8",
|
|
78
|
+
"husky": "^9.1.7",
|
|
79
|
+
"prettier": "^3.10.0",
|
|
80
|
+
"typescript": "^5.9.0",
|
|
81
|
+
"typescript-eslint": "8.72.1"
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
trigger: always_on
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
<!-- Generated from the pinned erosiano-dev balanced policy. Do not edit. -->
|
|
6
|
+
|
|
7
|
+
erosiano-dev personal style. Explicit user requirements and current repo conventions take precedence.
|
|
8
|
+
Balanced Clean Code: prefer behavior-preserving guards/early returns; nesting findings are advisory.
|
|
9
|
+
- No nested ternaries; use if, named helper or lookup map.
|
|
10
|
+
- One blank before return after 2+ preceding statements in the same block; optional after one, none for first-statement return. One blank between adjacent implementations, not TS overloads.
|
|
11
|
+
- Prefer typed domain results (discriminated unions); throw IO/infrastructure errors, catch at boundary. Preserve project error contracts and causes.
|
|
12
|
+
- No any, `as` casts except `as const`, or non-null `!`; type guards, explicit exported return types.
|
|
13
|
+
- Extract helpers for reuse or clearer callers, not length. Comments: one line explaining why, not what.
|
|
14
|
+
- Before substantial changes, establish a scoped project baseline: instructions, native gates, maintained code/tests. Distinguish current, legacy and unknown; refresh stale evidence. Preserve architecture, contracts, validation, accessibility; no unrelated refactors. Never copy insecure legacy behavior.
|
|
15
|
+
- Select applicable practices after baseline via permitted resolver/catalog; never inject entire catalog.
|
|
16
|
+
- Ponytail owns YAGNI/simplification when present; do not duplicate it or invoke its review automatically.
|
|
17
|
+
- Risky auth/authorization, payments, DB schema/queries/RLS, destructive data, secrets, concurrency: prove normal-user denial (cross-tenant/other owner/unauthenticated); preserve idempotency/replay protection, check constraints/locks/transactions/migration compatibility. Never run real payments or destructive shared-DB checks.
|
|
18
|
+
- Validate runtime boundaries, handle promise rejection/exhaustive states. Never weaken types, security or tests, invent APIs or fake production success.
|
|
19
|
+
- Never claim a check passed without running it.
|
|
20
|
+
Enforcement=balanced: native gates authoritative; deterministic only if adopted. For SDLC review, use the erosiano-review skill.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createStdioServer, resolveMcpRoot } from './mcp/stdio.mjs';
|
|
3
|
+
|
|
4
|
+
const argv = process.argv.slice(2);
|
|
5
|
+
let supplied;
|
|
6
|
+
let workflow = false;
|
|
7
|
+
for (let i = 0; i < argv.length; i++) {
|
|
8
|
+
if (argv[i] === '--root' && i + 1 < argv.length && supplied === undefined) {
|
|
9
|
+
supplied = argv[++i];
|
|
10
|
+
} else if (argv[i] === '--workflow') {
|
|
11
|
+
workflow = true;
|
|
12
|
+
} else {
|
|
13
|
+
console.error(
|
|
14
|
+
'usage: node scripts/agy-mcp.mjs --root /absolute/path/to/repository [--workflow]',
|
|
15
|
+
);
|
|
16
|
+
process.exit(2);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const resolved = await resolveMcpRoot(supplied, process.env);
|
|
20
|
+
if (!resolved.ok) {
|
|
21
|
+
console.error(resolved.message);
|
|
22
|
+
process.exit(2);
|
|
23
|
+
}
|
|
24
|
+
const env = workflow ? { ...process.env, KSD_MCP_WORKFLOW: '1' } : process.env;
|
|
25
|
+
// AGY may spawn MCP servers from its application directory rather than the workspace.
|
|
26
|
+
// Root is explicit and frozen for the entire process; tools cannot override it.
|
|
27
|
+
createStdioServer({ root: resolved.root, env });
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// AGY/stdio adapter to the pinned engineering runtime. No OMP lifecycle, subprocess or root override.
|
|
2
|
+
import { isAbsolute } from 'node:path';
|
|
3
|
+
import { collectBaselineEvidence } from '../../dist/engineering/baseline.js';
|
|
4
|
+
import { resolvePracticeTool } from '../../dist/engineering/practice-tool.js';
|
|
5
|
+
|
|
6
|
+
const schema = (properties, required) => ({
|
|
7
|
+
type: 'object',
|
|
8
|
+
properties,
|
|
9
|
+
required,
|
|
10
|
+
additionalProperties: false,
|
|
11
|
+
});
|
|
12
|
+
const annotations = { readOnlyHint: true, destructiveHint: false, openWorldHint: false };
|
|
13
|
+
|
|
14
|
+
export const ENGINEERING_TOOLS = Object.freeze([
|
|
15
|
+
{
|
|
16
|
+
name: 'erosiano_baseline',
|
|
17
|
+
description:
|
|
18
|
+
'Capture bounded sampled file hashes for a scope under the configured workspace. Supply relative scope and optional caller/test/config paths. Does not establish a complete semantic baseline or execute Git.',
|
|
19
|
+
inputSchema: schema(
|
|
20
|
+
{
|
|
21
|
+
scope: { type: 'string', minLength: 1, maxLength: 1024 },
|
|
22
|
+
additionalPaths: {
|
|
23
|
+
type: 'array',
|
|
24
|
+
maxItems: 24,
|
|
25
|
+
items: { type: 'string', minLength: 1, maxLength: 1024 },
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
['scope'],
|
|
29
|
+
),
|
|
30
|
+
annotations,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: 'erosiano_resolve_practices',
|
|
34
|
+
description:
|
|
35
|
+
'Resolve engineering practices from bounded supplied context. Fresh policy output requires a current baseline fingerprint and observed path provenance. Uses the fixed server root. Does not authenticate interpretations or run native checks.',
|
|
36
|
+
inputSchema: schema(
|
|
37
|
+
{
|
|
38
|
+
context: { type: 'string', maxLength: 65536 },
|
|
39
|
+
group: { type: 'string', maxLength: 64 },
|
|
40
|
+
format: { type: 'string', enum: ['decisions', 'policy'] },
|
|
41
|
+
},
|
|
42
|
+
['context'],
|
|
43
|
+
),
|
|
44
|
+
annotations,
|
|
45
|
+
},
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
function error(text) {
|
|
49
|
+
return { content: [{ type: 'text', text }], isError: true };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function validPath(path) {
|
|
53
|
+
return typeof path === 'string' && path.length > 0 && path.length <= 1024 && !isAbsolute(path);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function callEngineering(root, name, args, signal) {
|
|
57
|
+
if (signal?.aborted) return error('Cancelled');
|
|
58
|
+
if (!args || typeof args !== 'object' || Array.isArray(args)) {
|
|
59
|
+
return error('INVALID_INPUT: expected an object');
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
if (name === 'erosiano_baseline') {
|
|
63
|
+
if (
|
|
64
|
+
Object.keys(args).some((key) => !['scope', 'additionalPaths'].includes(key)) ||
|
|
65
|
+
!validPath(args.scope) ||
|
|
66
|
+
(args.additionalPaths !== undefined &&
|
|
67
|
+
(!Array.isArray(args.additionalPaths) ||
|
|
68
|
+
args.additionalPaths.length > 24 ||
|
|
69
|
+
!args.additionalPaths.every(validPath)))
|
|
70
|
+
) {
|
|
71
|
+
return error('INVALID_INPUT: relative scoped paths only; fixed server root');
|
|
72
|
+
}
|
|
73
|
+
const record = collectBaselineEvidence({
|
|
74
|
+
root,
|
|
75
|
+
scope: args.scope,
|
|
76
|
+
additionalPaths: args.additionalPaths ?? [],
|
|
77
|
+
});
|
|
78
|
+
const text = JSON.stringify(record);
|
|
79
|
+
if (text.length > 65536) return error('NOT_ASSESSED: narrow the sampled evidence scope');
|
|
80
|
+
return { content: [{ type: 'text', text }] };
|
|
81
|
+
}
|
|
82
|
+
if (name !== 'erosiano_resolve_practices') return error('TOOL_UNAVAILABLE');
|
|
83
|
+
if (Object.keys(args).some((key) => !['context', 'group', 'format'].includes(key))) {
|
|
84
|
+
return error('INVALID_INPUT: unknown argument; fixed server root');
|
|
85
|
+
}
|
|
86
|
+
const result = resolvePracticeTool(args, { root });
|
|
87
|
+
return { content: result.content, ...(result.isError ? { isError: true } : {}) };
|
|
88
|
+
} catch {
|
|
89
|
+
return error('NOT_ASSESSED: invalid or inaccessible scoped engineering evidence');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { realpathSync, statSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute } from 'node:path';
|
|
3
|
+
import { loadWorkspace } from '../../dist/core/engine/workspace.js';
|
|
4
|
+
import { createTools } from './tools.mjs';
|
|
5
|
+
|
|
6
|
+
export async function resolveMcpRoot(supplied, env = process.env) {
|
|
7
|
+
const rootPath = supplied ?? env.KSD_MCP_ROOT;
|
|
8
|
+
if (!rootPath || !isAbsolute(rootPath)) {
|
|
9
|
+
return {
|
|
10
|
+
ok: false,
|
|
11
|
+
message: 'KSD MCP requires --root or KSD_MCP_ROOT set to an absolute repository directory',
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
let root;
|
|
15
|
+
try {
|
|
16
|
+
root = realpathSync(rootPath);
|
|
17
|
+
if (!statSync(root).isDirectory()) throw new Error('not a directory');
|
|
18
|
+
} catch {
|
|
19
|
+
return { ok: false, message: 'KSD MCP root is not an accessible directory' };
|
|
20
|
+
}
|
|
21
|
+
// KSD's workspace resolver can walk up to a parent git/config root. Refuse implicit
|
|
22
|
+
// expansion beyond the root explicitly authorized in the MCP configuration.
|
|
23
|
+
try {
|
|
24
|
+
const workspace = await loadWorkspace(root);
|
|
25
|
+
if (workspace.root !== root) throw new Error('subdirectory is not the workspace root');
|
|
26
|
+
} catch {
|
|
27
|
+
return {
|
|
28
|
+
ok: false,
|
|
29
|
+
message: 'KSD MCP root must be the exact accessible KSD workspace root',
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return { ok: true, root };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const MODERN = '2026-07-28';
|
|
36
|
+
const LEGACY = '2025-11-25';
|
|
37
|
+
const MAX_FRAME_BYTES = 1024 * 1024;
|
|
38
|
+
const MAX_INFLIGHT = 8;
|
|
39
|
+
const SERVER_INFO = { name: 'ksd-intelligence', version: '0.2.0' };
|
|
40
|
+
|
|
41
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
42
|
+
const validId = (value) =>
|
|
43
|
+
typeof value === 'string' || (typeof value === 'number' && Number.isSafeInteger(value));
|
|
44
|
+
const idKey = (id) => typeof id + ':' + String(id);
|
|
45
|
+
const serverMeta = () => ({ 'io.modelcontextprotocol/serverInfo': SERVER_INFO });
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A deliberately small local stdio transport, no listening TCP port or third-party runtime.
|
|
49
|
+
* Supports the 2025-11-25 handshake and stateless MCP 2026-07-28 request envelope.
|
|
50
|
+
* Only JSON-RPC envelopes may reach stdout; all failures are bounded.
|
|
51
|
+
*/
|
|
52
|
+
export function createStdioServer({
|
|
53
|
+
root,
|
|
54
|
+
env = process.env,
|
|
55
|
+
input = process.stdin,
|
|
56
|
+
output = process.stdout,
|
|
57
|
+
toolset,
|
|
58
|
+
}) {
|
|
59
|
+
const tools = toolset ?? createTools({ root, env });
|
|
60
|
+
const active = new Map();
|
|
61
|
+
let legacyInitialized = false;
|
|
62
|
+
let legacyHello = false;
|
|
63
|
+
let buffered = Buffer.alloc(0);
|
|
64
|
+
let discarding = false;
|
|
65
|
+
let closed = false;
|
|
66
|
+
|
|
67
|
+
function send(frame) {
|
|
68
|
+
if (!closed) output.write(JSON.stringify(frame) + '\n');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function error(id, code, message, data) {
|
|
72
|
+
send({
|
|
73
|
+
jsonrpc: '2.0',
|
|
74
|
+
id,
|
|
75
|
+
error: { code, message, ...(data === undefined ? {} : { data }) },
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function reply(id, value, modern) {
|
|
80
|
+
const result = modern ? { ...value, resultType: 'complete', _meta: serverMeta() } : value;
|
|
81
|
+
send({ jsonrpc: '2.0', id, result });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function requestVersion(message) {
|
|
85
|
+
const meta =
|
|
86
|
+
isObject(message.params) && isObject(message.params._meta) ? message.params._meta : {};
|
|
87
|
+
return meta['io.modelcontextprotocol/protocolVersion'];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function cancel(message) {
|
|
91
|
+
const key =
|
|
92
|
+
isObject(message.params) && validId(message.params.requestId)
|
|
93
|
+
? idKey(message.params.requestId)
|
|
94
|
+
: null;
|
|
95
|
+
if (key === null) return;
|
|
96
|
+
const pending = active.get(key);
|
|
97
|
+
if (pending) pending.controller.abort();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function onMessage(message) {
|
|
101
|
+
if (
|
|
102
|
+
!isObject(message) ||
|
|
103
|
+
message.jsonrpc !== '2.0' ||
|
|
104
|
+
typeof message.method !== 'string' ||
|
|
105
|
+
(Object.hasOwn(message, 'id') && !validId(message.id))
|
|
106
|
+
) {
|
|
107
|
+
error(null, -32600, 'Invalid JSON-RPC request');
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const isNotification = !Object.hasOwn(message, 'id');
|
|
111
|
+
if (isNotification) {
|
|
112
|
+
if (message.method === 'notifications/cancelled') cancel(message);
|
|
113
|
+
if (message.method === 'notifications/initialized' && legacyHello) {
|
|
114
|
+
legacyInitialized = true;
|
|
115
|
+
}
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const id = message.id;
|
|
119
|
+
const version = requestVersion(message);
|
|
120
|
+
const modern = version === MODERN;
|
|
121
|
+
|
|
122
|
+
if (version !== undefined && version !== MODERN) {
|
|
123
|
+
error(id, -32022, 'Unsupported MCP protocol version', { supported: [MODERN] });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (message.method === 'initialize' && !modern) {
|
|
128
|
+
if (!isObject(message.params)) {
|
|
129
|
+
error(id, -32602, 'initialize params must be an object');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
legacyInitialized = false;
|
|
133
|
+
legacyHello = true;
|
|
134
|
+
reply(
|
|
135
|
+
id,
|
|
136
|
+
{
|
|
137
|
+
protocolVersion: LEGACY,
|
|
138
|
+
capabilities: { tools: {} },
|
|
139
|
+
serverInfo: SERVER_INFO,
|
|
140
|
+
instructions:
|
|
141
|
+
'Local read-only code intelligence. Configure an explicit repository root. Never treat partial impact evidence as safe.',
|
|
142
|
+
},
|
|
143
|
+
false,
|
|
144
|
+
);
|
|
145
|
+
// A separate initialized notification completes the handshake.
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (message.method === 'server/discover') {
|
|
150
|
+
if (!modern) {
|
|
151
|
+
error(id, -32601, 'Method not found');
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
reply(
|
|
155
|
+
id,
|
|
156
|
+
{
|
|
157
|
+
supportedVersions: [MODERN],
|
|
158
|
+
capabilities: { tools: {} },
|
|
159
|
+
ttlMs: 0,
|
|
160
|
+
cacheScope: 'private',
|
|
161
|
+
},
|
|
162
|
+
true,
|
|
163
|
+
);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!modern && !legacyInitialized) {
|
|
168
|
+
error(id, -32000, 'MCP server is not initialized');
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (message.method === 'ping' && !modern) {
|
|
172
|
+
reply(id, {}, false);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (message.method === 'tools/list') {
|
|
176
|
+
if (
|
|
177
|
+
message.params !== undefined &&
|
|
178
|
+
(!isObject(message.params) ||
|
|
179
|
+
(message.params.cursor !== undefined && message.params.cursor !== null))
|
|
180
|
+
) {
|
|
181
|
+
error(id, -32602, 'Invalid tools/list params');
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
reply(
|
|
185
|
+
id,
|
|
186
|
+
{
|
|
187
|
+
tools: tools.definitions,
|
|
188
|
+
...(modern ? { ttlMs: 0, cacheScope: 'private' } : {}),
|
|
189
|
+
},
|
|
190
|
+
modern,
|
|
191
|
+
);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (message.method !== 'tools/call') {
|
|
195
|
+
error(id, -32601, 'Method not found');
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (!isObject(message.params) || typeof message.params.name !== 'string') {
|
|
199
|
+
error(id, -32602, 'tools/call requires a tool name');
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const key = idKey(id);
|
|
203
|
+
if (active.has(key)) {
|
|
204
|
+
error(id, -32600, 'Duplicate request id');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (active.size >= MAX_INFLIGHT) {
|
|
208
|
+
error(id, -32000, 'Too many concurrent tool calls');
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const controller = new AbortController();
|
|
212
|
+
active.set(key, { controller });
|
|
213
|
+
const progressToken = isObject(message.params._meta)
|
|
214
|
+
? message.params._meta.progressToken
|
|
215
|
+
: undefined;
|
|
216
|
+
let progress = 0;
|
|
217
|
+
try {
|
|
218
|
+
const value = await tools.call(
|
|
219
|
+
message.params.name,
|
|
220
|
+
message.params.arguments ?? {},
|
|
221
|
+
controller.signal,
|
|
222
|
+
(text) => {
|
|
223
|
+
if (controller.signal.aborted || !validId(progressToken)) return;
|
|
224
|
+
progress += 1;
|
|
225
|
+
send({
|
|
226
|
+
jsonrpc: '2.0',
|
|
227
|
+
method: 'notifications/progress',
|
|
228
|
+
params: { progressToken, progress, message: String(text).slice(0, 500) },
|
|
229
|
+
});
|
|
230
|
+
},
|
|
231
|
+
);
|
|
232
|
+
if (!controller.signal.aborted) reply(id, value, modern);
|
|
233
|
+
} catch {
|
|
234
|
+
if (!controller.signal.aborted) {
|
|
235
|
+
reply(
|
|
236
|
+
id,
|
|
237
|
+
{ content: [{ type: 'text', text: 'KSD_FAILED: tool execution failed' }], isError: true },
|
|
238
|
+
modern,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
} finally {
|
|
242
|
+
active.delete(key);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function onLine(raw) {
|
|
247
|
+
if (raw.length === 0) return;
|
|
248
|
+
let message;
|
|
249
|
+
try {
|
|
250
|
+
message = JSON.parse(raw.toString('utf8').replace(/\r$/, ''));
|
|
251
|
+
} catch {
|
|
252
|
+
error(null, -32700, 'Parse error');
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
void onMessage(message).catch(() => {
|
|
256
|
+
const id = isObject(message) && validId(message.id) ? message.id : null;
|
|
257
|
+
error(id, -32603, 'Internal error');
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function onChunk(inputChunk) {
|
|
262
|
+
const chunk = Buffer.isBuffer(inputChunk) ? inputChunk : Buffer.from(inputChunk);
|
|
263
|
+
let start = 0;
|
|
264
|
+
while (start < chunk.length) {
|
|
265
|
+
const newline = chunk.indexOf(10, start);
|
|
266
|
+
const end = newline < 0 ? chunk.length : newline;
|
|
267
|
+
if (!discarding) {
|
|
268
|
+
const segment = chunk.subarray(start, end);
|
|
269
|
+
if (buffered.length + segment.length > MAX_FRAME_BYTES) {
|
|
270
|
+
buffered = Buffer.alloc(0);
|
|
271
|
+
discarding = true;
|
|
272
|
+
error(null, -32700, 'MCP frame exceeds 1 MiB');
|
|
273
|
+
} else {
|
|
274
|
+
buffered = Buffer.concat([buffered, segment]);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (newline < 0) return;
|
|
278
|
+
if (!discarding) onLine(buffered);
|
|
279
|
+
buffered = Buffer.alloc(0);
|
|
280
|
+
discarding = false;
|
|
281
|
+
start = newline + 1;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
let resolveDone;
|
|
286
|
+
const done = new Promise((resolve) => {
|
|
287
|
+
resolveDone = resolve;
|
|
288
|
+
});
|
|
289
|
+
function onEnd() {
|
|
290
|
+
closed = true;
|
|
291
|
+
input.off('data', onChunk);
|
|
292
|
+
for (const { controller } of active.values()) controller.abort();
|
|
293
|
+
resolveDone();
|
|
294
|
+
}
|
|
295
|
+
input.on('data', onChunk);
|
|
296
|
+
input.once('end', onEnd);
|
|
297
|
+
return {
|
|
298
|
+
done,
|
|
299
|
+
close: () => {
|
|
300
|
+
input.off('end', onEnd);
|
|
301
|
+
onEnd();
|
|
302
|
+
},
|
|
303
|
+
};
|
|
304
|
+
}
|