project-context-map-mcp 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/src/claude.js ADDED
@@ -0,0 +1,86 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ export const SKILL_MARKDOWN = `---
5
+ name: project-context-map
6
+ description: Use this skill when working in a large repository and you want Claude Code to avoid reading too many files. First consult the project context map or MCP tools, identify the most relevant files for the task, and only then inspect or edit code.
7
+ ---
8
+
9
+ # Project Context Map
10
+
11
+ Use this skill when a task involves code changes, debugging, or feature work in a medium or large repository.
12
+
13
+ ## Goal
14
+
15
+ Reduce token usage and speed up edits by using the project context map before opening source files.
16
+
17
+ ## Workflow
18
+
19
+ 1. Start with the MCP if it is available.
20
+ 2. Call \`read_project_context\` to understand the current repo memory in \`.project.md\`.
21
+ 3. Call \`query_topology\` with the user request.
22
+ 4. Call \`read_files_sparse\` with only the returned file paths.
23
+ 5. Expand to neighboring files only if the first pass is insufficient.
24
+ 6. Use \`write_and_log\` when you want MCP-managed writes with a \`.project.md\` Change Log entry.
25
+ 7. After edits, call \`refresh_project_map\` so \`.project-map.json\`, \`.project-map.md\`, and \`.project.md\` stay current.
26
+
27
+ ## Fallback
28
+
29
+ If the MCP server is not available:
30
+
31
+ 1. Look for \`.project.md\`, \`.project-map.json\`, and \`.project-map.md\` in the project root.
32
+ 2. Use those files to identify relevant modules and files.
33
+ 3. Only after that, inspect code files directly.
34
+
35
+ ## Rules
36
+
37
+ - Do not begin by scanning the whole repository.
38
+ - Prefer targeted reads over broad tree walks.
39
+ - Use recent git changes as a signal when ranking files.
40
+ - Treat the project map as the primary onboarding artifact for the repo.
41
+ `;
42
+
43
+ export function buildMcpConfig({
44
+ projectRoot = ".",
45
+ serverName = "project-context-map",
46
+ command = "project-context-map-mcp"
47
+ } = {}) {
48
+ return {
49
+ mcpServers: {
50
+ [serverName]: {
51
+ command,
52
+ args: ["serve", "--project-root", projectRoot],
53
+ env: {}
54
+ }
55
+ }
56
+ };
57
+ }
58
+
59
+ export function writeClaudeSkill(projectRoot) {
60
+ const targetDir = path.join(projectRoot, ".claude", "skills", "project-context-map");
61
+ const targetPath = path.join(targetDir, "SKILL.md");
62
+ fs.mkdirSync(targetDir, { recursive: true });
63
+ fs.writeFileSync(targetPath, `${SKILL_MARKDOWN}\n`, "utf8");
64
+ return targetPath;
65
+ }
66
+
67
+ export function writeMcpConfig(projectRoot, options = {}) {
68
+ const targetPath = path.join(projectRoot, ".mcp.json");
69
+ const generated = buildMcpConfig(options);
70
+ let current = {};
71
+
72
+ if (fs.existsSync(targetPath)) {
73
+ current = JSON.parse(fs.readFileSync(targetPath, "utf8"));
74
+ }
75
+
76
+ const config = {
77
+ ...current,
78
+ mcpServers: {
79
+ ...(current.mcpServers || {}),
80
+ ...generated.mcpServers
81
+ }
82
+ };
83
+
84
+ fs.writeFileSync(targetPath, `${JSON.stringify(config, null, 2)}\n`, "utf8");
85
+ return targetPath;
86
+ }
package/src/cli.js ADDED
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { installGitHooks } from "../scripts/install-git-hooks.js";
4
+ import { refreshMapCli } from "../scripts/refresh-map.js";
5
+ import { buildMcpConfig, writeClaudeSkill, writeMcpConfig } from "./claude.js";
6
+ import { startServer } from "./server.js";
7
+
8
+ function parseArgs(argv) {
9
+ const [command = "serve", ...rest] = argv;
10
+ const options = {};
11
+
12
+ for (let index = 0; index < rest.length; index += 1) {
13
+ const value = rest[index];
14
+ if (!value.startsWith("--")) {
15
+ continue;
16
+ }
17
+
18
+ const key = value.slice(2);
19
+ const next = rest[index + 1];
20
+
21
+ if (!next || next.startsWith("--")) {
22
+ options[key] = true;
23
+ continue;
24
+ }
25
+
26
+ options[key] = next;
27
+ index += 1;
28
+ }
29
+
30
+ return { command, options };
31
+ }
32
+
33
+ function printHelp() {
34
+ process.stdout.write(`project-context-map-mcp
35
+
36
+ Commands:
37
+ serve [--project-root <path>]
38
+ refresh [--project-root <path>]
39
+ install-hooks [--project-root <path>]
40
+ configure-claude [--project-root <path>] [--server-name <name>] [--command <cmd>]
41
+ print-mcp-config [--project-root <path>] [--server-name <name>] [--command <cmd>]
42
+ help
43
+ `);
44
+ }
45
+
46
+ const { command, options } = parseArgs(process.argv.slice(2));
47
+ const projectRoot = options["project-root"] || process.cwd();
48
+
49
+ if (command === "help" || command === "--help" || command === "-h") {
50
+ printHelp();
51
+ process.exit(0);
52
+ }
53
+
54
+ if (command === "serve") {
55
+ if (options["project-root"]) {
56
+ process.env.PROJECT_ROOT = projectRoot;
57
+ }
58
+ await startServer();
59
+ process.exit(0);
60
+ }
61
+
62
+ if (command === "refresh") {
63
+ refreshMapCli(projectRoot);
64
+ process.exit(0);
65
+ }
66
+
67
+ if (command === "install-hooks") {
68
+ installGitHooks(projectRoot);
69
+ process.exit(0);
70
+ }
71
+
72
+ if (command === "configure-claude") {
73
+ const skillPath = writeClaudeSkill(projectRoot);
74
+ const configPath = writeMcpConfig(projectRoot, {
75
+ projectRoot: ".",
76
+ serverName: options["server-name"] || "project-context-map",
77
+ command: options.command || "project-context-map-mcp"
78
+ });
79
+
80
+ process.stdout.write(
81
+ `${JSON.stringify(
82
+ {
83
+ message: "Claude Code skill and .mcp.json created.",
84
+ skillPath,
85
+ configPath
86
+ },
87
+ null,
88
+ 2
89
+ )}\n`
90
+ );
91
+ process.exit(0);
92
+ }
93
+
94
+ if (command === "print-mcp-config") {
95
+ process.stdout.write(
96
+ `${JSON.stringify(
97
+ buildMcpConfig({
98
+ projectRoot: options["project-root"] || ".",
99
+ serverName: options["server-name"] || "project-context-map",
100
+ command: options.command || "project-context-map-mcp"
101
+ }),
102
+ null,
103
+ 2
104
+ )}\n`
105
+ );
106
+ process.exit(0);
107
+ }
108
+
109
+ printHelp();
110
+ process.exit(1);
package/src/git.js ADDED
@@ -0,0 +1,96 @@
1
+ import { execFileSync } from "node:child_process";
2
+
3
+ function runGit(projectRoot, args) {
4
+ return execFileSync("git", ["-C", projectRoot, ...args], {
5
+ encoding: "utf8",
6
+ stdio: ["ignore", "pipe", "ignore"]
7
+ }).trim();
8
+ }
9
+
10
+ export function isGitRepository(projectRoot) {
11
+ try {
12
+ return runGit(projectRoot, ["rev-parse", "--is-inside-work-tree"]) === "true";
13
+ } catch {
14
+ return false;
15
+ }
16
+ }
17
+
18
+ export function getRecentGitChanges(projectRoot, days = 5) {
19
+ if (!isGitRepository(projectRoot)) {
20
+ return {
21
+ isGitRepo: false,
22
+ days,
23
+ commits: [],
24
+ hotspots: []
25
+ };
26
+ }
27
+
28
+ const output = runGit(projectRoot, [
29
+ "log",
30
+ `--since=${days} days ago`,
31
+ "--date=short",
32
+ "--pretty=format:__COMMIT__%n%H%n%ad%n%s",
33
+ "--name-only"
34
+ ]);
35
+
36
+ if (!output) {
37
+ return {
38
+ isGitRepo: true,
39
+ days,
40
+ commits: [],
41
+ hotspots: []
42
+ };
43
+ }
44
+
45
+ const blocks = output
46
+ .split("__COMMIT__")
47
+ .map((block) => block.trim())
48
+ .filter(Boolean);
49
+
50
+ const hotspotCounter = new Map();
51
+ const commits = blocks.map((block) => {
52
+ const lines = block
53
+ .split(/\r?\n/u)
54
+ .map((line) => line.trim())
55
+ .filter(Boolean);
56
+
57
+ const [hash, date, summary, ...files] = lines;
58
+
59
+ for (const filePath of files) {
60
+ hotspotCounter.set(filePath, (hotspotCounter.get(filePath) || 0) + 1);
61
+ }
62
+
63
+ return {
64
+ hash,
65
+ date,
66
+ summary,
67
+ files
68
+ };
69
+ });
70
+
71
+ const hotspots = [...hotspotCounter.entries()]
72
+ .map(([path, count]) => ({ path, count }))
73
+ .sort((left, right) => right.count - left.count || left.path.localeCompare(right.path))
74
+ .slice(0, 25);
75
+
76
+ return {
77
+ isGitRepo: true,
78
+ days,
79
+ commits,
80
+ hotspots
81
+ };
82
+ }
83
+
84
+ export function getLastChangedReasonMap(gitChanges) {
85
+ const result = new Map();
86
+
87
+ for (const commit of gitChanges?.commits || []) {
88
+ for (const filePath of commit.files || []) {
89
+ if (!result.has(filePath)) {
90
+ result.set(filePath, commit.summary);
91
+ }
92
+ }
93
+ }
94
+
95
+ return result;
96
+ }