dap-mcp-server 0.1.5
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/.claude/settings.local.json +31 -0
- package/.forgejo/workflows/publish.yml +63 -0
- package/CLAUDE.md +71 -0
- package/LICENSE +21 -0
- package/README.md +105 -0
- package/SKILL.md +62 -0
- package/build/adapters/builtin.d.ts +24 -0
- package/build/adapters/builtin.js +46 -0
- package/build/adapters/builtin.js.map +1 -0
- package/build/adapters/detect.d.ts +23 -0
- package/build/adapters/detect.js +36 -0
- package/build/adapters/detect.js.map +1 -0
- package/build/adapters/registry.d.ts +39 -0
- package/build/adapters/registry.js +79 -0
- package/build/adapters/registry.js.map +1 -0
- package/build/dap/dap-client.d.ts +46 -0
- package/build/dap/dap-client.js +190 -0
- package/build/dap/dap-client.js.map +1 -0
- package/build/dap/protocol.d.ts +32 -0
- package/build/dap/protocol.js +71 -0
- package/build/dap/protocol.js.map +1 -0
- package/build/dap/ring-buffer.d.ts +35 -0
- package/build/dap/ring-buffer.js +57 -0
- package/build/dap/ring-buffer.js.map +1 -0
- package/build/dap/types.d.ts +87 -0
- package/build/dap/types.js +25 -0
- package/build/dap/types.js.map +1 -0
- package/build/index.d.ts +24 -0
- package/build/index.js +30 -0
- package/build/index.js.map +1 -0
- package/build/server.d.ts +24 -0
- package/build/server.js +735 -0
- package/build/server.js.map +1 -0
- package/build/session/session-manager.d.ts +32 -0
- package/build/session/session-manager.js +60 -0
- package/build/session/session-manager.js.map +1 -0
- package/build/session/session.d.ts +85 -0
- package/build/session/session.js +374 -0
- package/build/session/session.js.map +1 -0
- package/docs/plans/2026-03-05-dap-wrapper-mcp-design.md +326 -0
- package/docs/plans/2026-03-05-dap-wrapper-mcp-implementation.md +2815 -0
- package/package.json +43 -0
- package/src/adapters/builtin.ts +47 -0
- package/src/adapters/detect.test.ts +36 -0
- package/src/adapters/detect.ts +36 -0
- package/src/adapters/registry.test.ts +73 -0
- package/src/adapters/registry.ts +86 -0
- package/src/dap/dap-client.test.ts +87 -0
- package/src/dap/dap-client.ts +216 -0
- package/src/dap/protocol.test.ts +82 -0
- package/src/dap/protocol.ts +78 -0
- package/src/dap/ring-buffer.test.ts +82 -0
- package/src/dap/ring-buffer.ts +64 -0
- package/src/dap/types.ts +92 -0
- package/src/index.ts +30 -0
- package/src/integration/debug-lifecycle.test.ts +107 -0
- package/src/server.ts +866 -0
- package/src/session/session-manager.test.ts +74 -0
- package/src/session/session-manager.ts +67 -0
- package/src/session/session.test.ts +84 -0
- package/src/session/session.ts +435 -0
- package/src/test-fixtures/crash-adapter.js +23 -0
- package/src/test-fixtures/mock-adapter.js +142 -0
- package/src/test-fixtures/silent-adapter.js +24 -0
- package/tsconfig.json +16 -0
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dap-mcp-server",
|
|
3
|
+
"version": "0.1.5",
|
|
4
|
+
"description": "MCP server wrapping the Debug Adapter Protocol for AI-driven debugging",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"LLM",
|
|
7
|
+
"MCP",
|
|
8
|
+
"DAP"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "ssh://git@forgejo.professioneit.com/ProfessioneIT/dap-mcp-server.git"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Ivan Iraci",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "build/index.js",
|
|
18
|
+
"types": "./build/index.d.ts",
|
|
19
|
+
"bin": {
|
|
20
|
+
"dap-mcp-server": "build/index.js"
|
|
21
|
+
},
|
|
22
|
+
"directories": {
|
|
23
|
+
"doc": "docs"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc",
|
|
27
|
+
"dev": "tsc --watch",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:watch": "vitest",
|
|
30
|
+
"start": "node build/index.js",
|
|
31
|
+
"typecheck": "tsc --noEmit"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@modelcontextprotocol/sdk": "^1.27.1",
|
|
35
|
+
"@vscode/debugprotocol": "^1.68.0",
|
|
36
|
+
"zod": "^4.3.6"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^25.3.3",
|
|
40
|
+
"typescript": "^5.9.3",
|
|
41
|
+
"vitest": "^4.0.18"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
|
12
|
+
* all copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { AdapterConfig } from "../dap/types.js";
|
|
24
|
+
|
|
25
|
+
export const BUILTIN_ADAPTERS: AdapterConfig[] = [
|
|
26
|
+
{
|
|
27
|
+
type: "python",
|
|
28
|
+
runtime: "python3",
|
|
29
|
+
program: "-m",
|
|
30
|
+
args: ["debugpy.adapter"],
|
|
31
|
+
installHint: "pip install debugpy",
|
|
32
|
+
},
|
|
33
|
+
// node adapter removed — no reliable universal path to js-debug-dap.
|
|
34
|
+
// Configure via configure_adapter if needed.
|
|
35
|
+
{
|
|
36
|
+
type: "go",
|
|
37
|
+
program: "dlv",
|
|
38
|
+
args: ["dap"],
|
|
39
|
+
installHint: "go install github.com/go-delve/delve/cmd/dlv@latest",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
type: "cppdbg",
|
|
43
|
+
program: "lldb-dap",
|
|
44
|
+
args: [],
|
|
45
|
+
installHint: "apt install lldb (Debian/Ubuntu) or brew install llvm (macOS)",
|
|
46
|
+
},
|
|
47
|
+
];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
|
12
|
+
* all copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { describe, it, expect } from "vitest";
|
|
24
|
+
import { checkAdapterAvailable } from "./detect.js";
|
|
25
|
+
|
|
26
|
+
describe("checkAdapterAvailable", () => {
|
|
27
|
+
it("detects 'node' as available", async () => {
|
|
28
|
+
const result = await checkAdapterAvailable("node");
|
|
29
|
+
expect(result).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("detects non-existent binary as unavailable", async () => {
|
|
33
|
+
const result = await checkAdapterAvailable("definitely-not-a-real-binary-xyz");
|
|
34
|
+
expect(result).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
|
12
|
+
* all copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { execFile } from "node:child_process";
|
|
24
|
+
import { promisify } from "node:util";
|
|
25
|
+
|
|
26
|
+
const execFileAsync = promisify(execFile);
|
|
27
|
+
|
|
28
|
+
export async function checkAdapterAvailable(command: string): Promise<boolean> {
|
|
29
|
+
try {
|
|
30
|
+
const whichCmd = process.platform === "win32" ? "where" : "which";
|
|
31
|
+
await execFileAsync(whichCmd, [command]);
|
|
32
|
+
return true;
|
|
33
|
+
} catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
|
12
|
+
* all copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
24
|
+
import { AdapterRegistry } from "./registry.js";
|
|
25
|
+
import fs from "node:fs/promises";
|
|
26
|
+
import os from "node:os";
|
|
27
|
+
import path from "node:path";
|
|
28
|
+
|
|
29
|
+
describe("AdapterRegistry", () => {
|
|
30
|
+
let tmpDir: string;
|
|
31
|
+
let registry: AdapterRegistry;
|
|
32
|
+
|
|
33
|
+
beforeEach(async () => {
|
|
34
|
+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "dap-mcp-test-"));
|
|
35
|
+
registry = new AdapterRegistry(tmpDir, tmpDir);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
afterEach(async () => {
|
|
39
|
+
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("returns built-in adapters", () => {
|
|
43
|
+
const adapters = registry.listAll();
|
|
44
|
+
expect(adapters.some((a) => a.type === "python")).toBe(true);
|
|
45
|
+
expect(adapters.some((a) => a.type === "go")).toBe(true);
|
|
46
|
+
// node adapter removed — no reliable universal path; configure via configure_adapter
|
|
47
|
+
expect(adapters.some((a) => a.type === "node")).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("registers a custom adapter", () => {
|
|
51
|
+
registry.register({ type: "ruby", program: "/usr/bin/rdbg", args: ["--open", "--dap"] });
|
|
52
|
+
const ruby = registry.get("ruby");
|
|
53
|
+
expect(ruby).toBeDefined();
|
|
54
|
+
expect(ruby!.program).toBe("/usr/bin/rdbg");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("project-local overrides built-in", () => {
|
|
58
|
+
registry.register({ type: "python", program: "/custom/debugpy" });
|
|
59
|
+
const python = registry.get("python");
|
|
60
|
+
expect(python!.program).toBe("/custom/debugpy");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("saves and loads config from disk", async () => {
|
|
64
|
+
registry.register({ type: "ruby", program: "/usr/bin/rdbg" });
|
|
65
|
+
await registry.save("project");
|
|
66
|
+
|
|
67
|
+
const registry2 = new AdapterRegistry(tmpDir, tmpDir);
|
|
68
|
+
await registry2.load();
|
|
69
|
+
const ruby = registry2.get("ruby");
|
|
70
|
+
expect(ruby).toBeDefined();
|
|
71
|
+
expect(ruby!.program).toBe("/usr/bin/rdbg");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
|
12
|
+
* all copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { AdapterConfig } from "../dap/types.js";
|
|
24
|
+
import { BUILTIN_ADAPTERS } from "./builtin.js";
|
|
25
|
+
import fs from "node:fs/promises";
|
|
26
|
+
import path from "node:path";
|
|
27
|
+
|
|
28
|
+
export class AdapterRegistry {
|
|
29
|
+
private builtins: Map<string, AdapterConfig>;
|
|
30
|
+
private global: Map<string, AdapterConfig> = new Map();
|
|
31
|
+
private project: Map<string, AdapterConfig> = new Map();
|
|
32
|
+
private globalDir: string;
|
|
33
|
+
private projectDir: string;
|
|
34
|
+
|
|
35
|
+
constructor(globalDir: string, projectDir: string) {
|
|
36
|
+
this.globalDir = globalDir;
|
|
37
|
+
this.projectDir = projectDir;
|
|
38
|
+
this.builtins = new Map(BUILTIN_ADAPTERS.map((a) => [a.type, a]));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get(type: string): AdapterConfig | undefined {
|
|
42
|
+
return this.project.get(type) ?? this.global.get(type) ?? this.builtins.get(type);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
register(config: AdapterConfig, scope: "global" | "project" = "project"): void {
|
|
46
|
+
const map = scope === "global" ? this.global : this.project;
|
|
47
|
+
map.set(config.type, config);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
listAll(): Array<AdapterConfig & { source: "builtin" | "global" | "project" }> {
|
|
51
|
+
const result = new Map<string, AdapterConfig & { source: "builtin" | "global" | "project" }>();
|
|
52
|
+
for (const [type, config] of this.builtins) {
|
|
53
|
+
result.set(type, { ...config, source: "builtin" });
|
|
54
|
+
}
|
|
55
|
+
for (const [type, config] of this.global) {
|
|
56
|
+
result.set(type, { ...config, source: "global" });
|
|
57
|
+
}
|
|
58
|
+
for (const [type, config] of this.project) {
|
|
59
|
+
result.set(type, { ...config, source: "project" });
|
|
60
|
+
}
|
|
61
|
+
return Array.from(result.values());
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async load(): Promise<void> {
|
|
65
|
+
this.global = await this.loadFile(path.join(this.globalDir, "adapters.json"));
|
|
66
|
+
this.project = await this.loadFile(path.join(this.projectDir, "adapters.json"));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async save(scope: "global" | "project"): Promise<void> {
|
|
70
|
+
const dir = scope === "global" ? this.globalDir : this.projectDir;
|
|
71
|
+
const map = scope === "global" ? this.global : this.project;
|
|
72
|
+
await fs.mkdir(dir, { recursive: true });
|
|
73
|
+
const data = Object.fromEntries(map);
|
|
74
|
+
await fs.writeFile(path.join(dir, "adapters.json"), JSON.stringify(data, null, 2));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private async loadFile(filePath: string): Promise<Map<string, AdapterConfig>> {
|
|
78
|
+
try {
|
|
79
|
+
const data = await fs.readFile(filePath, "utf-8");
|
|
80
|
+
const parsed = JSON.parse(data) as Record<string, AdapterConfig>;
|
|
81
|
+
return new Map(Object.entries(parsed));
|
|
82
|
+
} catch {
|
|
83
|
+
return new Map();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
|
12
|
+
* all copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { describe, it, expect, afterEach } from "vitest";
|
|
24
|
+
import { DAPClient, DAPClientError } from "./dap-client.js";
|
|
25
|
+
import path from "node:path";
|
|
26
|
+
import { fileURLToPath } from "node:url";
|
|
27
|
+
|
|
28
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
29
|
+
|
|
30
|
+
describe("DAPClient", () => {
|
|
31
|
+
let client: DAPClient | undefined;
|
|
32
|
+
|
|
33
|
+
afterEach(async () => {
|
|
34
|
+
if (client) {
|
|
35
|
+
try { await client.dispose(); } catch { /* ignore */ }
|
|
36
|
+
client = undefined;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("spawns adapter and sends initialize request", async () => {
|
|
41
|
+
client = new DAPClient("node", path.join(__dirname, "../test-fixtures/mock-adapter.js"));
|
|
42
|
+
await client.spawn();
|
|
43
|
+
const response = await client.sendRequest("initialize", {
|
|
44
|
+
adapterID: "test",
|
|
45
|
+
linesStartAt1: true,
|
|
46
|
+
columnsStartAt1: true,
|
|
47
|
+
pathFormat: "path",
|
|
48
|
+
});
|
|
49
|
+
expect(response.success).toBe(true);
|
|
50
|
+
expect(response.command).toBe("initialize");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("receives events from adapter", async () => {
|
|
54
|
+
client = new DAPClient("node", path.join(__dirname, "../test-fixtures/mock-adapter.js"));
|
|
55
|
+
await client.spawn();
|
|
56
|
+
|
|
57
|
+
const eventPromise = new Promise<any>((resolve) => {
|
|
58
|
+
client!.on("initialized", resolve);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
await client.sendRequest("initialize", {
|
|
62
|
+
adapterID: "test",
|
|
63
|
+
linesStartAt1: true,
|
|
64
|
+
columnsStartAt1: true,
|
|
65
|
+
pathFormat: "path",
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const event = await eventPromise;
|
|
69
|
+
expect(event.event).toBe("initialized");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("rejects on adapter exit during request", async () => {
|
|
73
|
+
client = new DAPClient("node", path.join(__dirname, "../test-fixtures/crash-adapter.js"));
|
|
74
|
+
await client.spawn();
|
|
75
|
+
await expect(
|
|
76
|
+
client.sendRequest("initialize", { adapterID: "test" })
|
|
77
|
+
).rejects.toThrow();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("times out if adapter does not respond", async () => {
|
|
81
|
+
client = new DAPClient("node", path.join(__dirname, "../test-fixtures/silent-adapter.js"));
|
|
82
|
+
await client.spawn();
|
|
83
|
+
await expect(
|
|
84
|
+
client.sendRequest("initialize", { adapterID: "test" }, 500)
|
|
85
|
+
).rejects.toThrow(/timeout/i);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
|
12
|
+
* all copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { spawn, ChildProcess } from "node:child_process";
|
|
24
|
+
import { EventEmitter } from "node:events";
|
|
25
|
+
import { DebugProtocol } from "@vscode/debugprotocol";
|
|
26
|
+
import { encodeMessage, MessageParser } from "./protocol.js";
|
|
27
|
+
|
|
28
|
+
export class DAPClientError extends Error {
|
|
29
|
+
constructor(message: string) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = "DAPClientError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface PendingRequest {
|
|
36
|
+
resolve: (response: DebugProtocol.Response) => void;
|
|
37
|
+
reject: (error: Error) => void;
|
|
38
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class DAPClient extends EventEmitter {
|
|
42
|
+
private runtime: string;
|
|
43
|
+
private executable: string;
|
|
44
|
+
private args: string[];
|
|
45
|
+
private process?: ChildProcess;
|
|
46
|
+
private parser?: MessageParser;
|
|
47
|
+
private seq = 1;
|
|
48
|
+
private pending = new Map<number, PendingRequest>();
|
|
49
|
+
private stderrOutput: string[] = [];
|
|
50
|
+
private _exited = false;
|
|
51
|
+
private _exitCode?: number;
|
|
52
|
+
|
|
53
|
+
constructor(runtime: string, executable: string, args: string[] = []) {
|
|
54
|
+
super();
|
|
55
|
+
this.runtime = runtime;
|
|
56
|
+
this.executable = executable;
|
|
57
|
+
this.args = args;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async spawn(): Promise<void> {
|
|
61
|
+
return new Promise<void>((resolve, reject) => {
|
|
62
|
+
const runtimeArgs = this.runtime
|
|
63
|
+
? [this.executable, ...this.args]
|
|
64
|
+
: this.args;
|
|
65
|
+
const cmd = this.runtime || this.executable;
|
|
66
|
+
|
|
67
|
+
this.process = spawn(cmd, runtimeArgs, {
|
|
68
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
this.parser = new MessageParser((msg) => this.handleMessage(msg));
|
|
72
|
+
|
|
73
|
+
this.process.stdout!.on("data", (data: Buffer) => {
|
|
74
|
+
this.parser!.feed(data);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
this.process.stderr!.on("data", (data: Buffer) => {
|
|
78
|
+
this.stderrOutput.push(data.toString());
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Suppress EPIPE and other stream errors on stdin; the exit handler handles cleanup
|
|
82
|
+
this.process.stdin!.on("error", () => {
|
|
83
|
+
// ignore — exit event will reject pending requests
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
this.process.on("error", (err) => {
|
|
87
|
+
reject(new DAPClientError(`Failed to spawn adapter: ${err.message}`));
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
this.process.on("exit", (code, signal) => {
|
|
91
|
+
this._exited = true;
|
|
92
|
+
this._exitCode = code ?? undefined;
|
|
93
|
+
for (const [, req] of this.pending) {
|
|
94
|
+
if (req.timer) clearTimeout(req.timer);
|
|
95
|
+
req.reject(
|
|
96
|
+
new DAPClientError(
|
|
97
|
+
`Adapter exited (code=${code}, signal=${signal}). stderr: ${this.stderrOutput.join("")}`
|
|
98
|
+
)
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
this.pending.clear();
|
|
102
|
+
this.emit("exit", code, signal);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Resolve once the process is spawned (use flag to prevent double-resolve)
|
|
106
|
+
let resolved = false;
|
|
107
|
+
this.process.stdout!.once("readable", () => {
|
|
108
|
+
if (!resolved) { resolved = true; resolve(); }
|
|
109
|
+
});
|
|
110
|
+
setTimeout(() => {
|
|
111
|
+
if (!resolved) { resolved = true; resolve(); }
|
|
112
|
+
}, 100);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async sendRequest(
|
|
117
|
+
command: string,
|
|
118
|
+
args?: Record<string, unknown>,
|
|
119
|
+
timeout = 10000
|
|
120
|
+
): Promise<DebugProtocol.Response> {
|
|
121
|
+
if (this._exited) {
|
|
122
|
+
throw new DAPClientError(
|
|
123
|
+
`Adapter has exited (code=${this._exitCode}). stderr: ${this.stderrOutput.join("")}`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
if (!this.process?.stdin?.writable) {
|
|
127
|
+
throw new DAPClientError("Adapter stdin is not writable");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const seq = this.seq++;
|
|
131
|
+
const request: DebugProtocol.Request = {
|
|
132
|
+
seq,
|
|
133
|
+
type: "request",
|
|
134
|
+
command,
|
|
135
|
+
arguments: args,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return new Promise<DebugProtocol.Response>((resolve, reject) => {
|
|
139
|
+
const pending: PendingRequest = { resolve, reject };
|
|
140
|
+
if (timeout > 0) {
|
|
141
|
+
pending.timer = setTimeout(() => {
|
|
142
|
+
this.pending.delete(seq);
|
|
143
|
+
reject(new DAPClientError(`Request '${command}' timeout after ${timeout}ms`));
|
|
144
|
+
}, timeout);
|
|
145
|
+
}
|
|
146
|
+
this.pending.set(seq, pending);
|
|
147
|
+
try {
|
|
148
|
+
this.process!.stdin!.write(encodeMessage(request), (err) => {
|
|
149
|
+
if (err) {
|
|
150
|
+
// Write failed (e.g. EPIPE); clean up and reject
|
|
151
|
+
const p = this.pending.get(seq);
|
|
152
|
+
if (p) {
|
|
153
|
+
if (p.timer) clearTimeout(p.timer);
|
|
154
|
+
this.pending.delete(seq);
|
|
155
|
+
p.reject(new DAPClientError(`Write to adapter failed: ${err.message}`));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
} catch (err) {
|
|
160
|
+
this.pending.delete(seq);
|
|
161
|
+
if (pending.timer) clearTimeout(pending.timer);
|
|
162
|
+
reject(new DAPClientError(`Write to adapter failed: ${(err as Error).message}`));
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private handleMessage(msg: DebugProtocol.ProtocolMessage): void {
|
|
168
|
+
if (msg.type === "response") {
|
|
169
|
+
const response = msg as DebugProtocol.Response;
|
|
170
|
+
const pending = this.pending.get(response.request_seq);
|
|
171
|
+
if (pending) {
|
|
172
|
+
if (pending.timer) clearTimeout(pending.timer);
|
|
173
|
+
this.pending.delete(response.request_seq);
|
|
174
|
+
if (response.success) {
|
|
175
|
+
pending.resolve(response);
|
|
176
|
+
} else {
|
|
177
|
+
pending.reject(
|
|
178
|
+
new DAPClientError(
|
|
179
|
+
`DAP error on '${response.command}': ${response.message || "unknown error"}`
|
|
180
|
+
)
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
} else if (msg.type === "event") {
|
|
185
|
+
const event = msg as DebugProtocol.Event;
|
|
186
|
+
this.emit(event.event, event);
|
|
187
|
+
this.emit("event", event);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
get exited(): boolean {
|
|
192
|
+
return this._exited;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
get stderr(): string {
|
|
196
|
+
return this.stderrOutput.join("");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async dispose(): Promise<void> {
|
|
200
|
+
if (this.process && !this._exited) {
|
|
201
|
+
this.process.stdin?.end();
|
|
202
|
+
this.process.kill("SIGTERM");
|
|
203
|
+
await new Promise<void>((resolve) => {
|
|
204
|
+
const timer = setTimeout(() => {
|
|
205
|
+
this.process?.kill("SIGKILL");
|
|
206
|
+
resolve();
|
|
207
|
+
}, 3000);
|
|
208
|
+
this.process!.on("exit", () => {
|
|
209
|
+
clearTimeout(timer);
|
|
210
|
+
resolve();
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
this.removeAllListeners();
|
|
215
|
+
}
|
|
216
|
+
}
|