@imenam/mcp-documentor 1.0.33 → 1.0.35

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.
@@ -1,10 +1,37 @@
1
1
  import { z } from "zod";
2
+ export declare const TrackedSectionSchema: z.ZodObject<{
3
+ anchors: z.ZodObject<{
4
+ top: z.ZodArray<z.ZodString>;
5
+ bottom: z.ZodArray<z.ZodString>;
6
+ }, z.core.$strip>;
7
+ sectionHash: z.ZodString;
8
+ }, z.core.$strip>;
9
+ export type TrackedSection = z.infer<typeof TrackedSectionSchema>;
2
10
  export declare const TrackedFileSchema: z.ZodObject<{
3
11
  path: z.ZodString;
4
12
  lastModifiedAt: z.ZodString;
5
13
  contentHash: z.ZodOptional<z.ZodString>;
14
+ sections: z.ZodOptional<z.ZodArray<z.ZodObject<{
15
+ anchors: z.ZodObject<{
16
+ top: z.ZodArray<z.ZodString>;
17
+ bottom: z.ZodArray<z.ZodString>;
18
+ }, z.core.$strip>;
19
+ sectionHash: z.ZodString;
20
+ }, z.core.$strip>>>;
6
21
  }, z.core.$strip>;
7
22
  export type TrackedFile = z.infer<typeof TrackedFileSchema>;
23
+ export declare const TrackedFileInputLineSchema: z.ZodObject<{
24
+ start: z.ZodNumber;
25
+ end: z.ZodNumber;
26
+ }, z.core.$strip>;
27
+ export declare const TrackedFileInputSchema: z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
28
+ path: z.ZodString;
29
+ lines: z.ZodArray<z.ZodObject<{
30
+ start: z.ZodNumber;
31
+ end: z.ZodNumber;
32
+ }, z.core.$strip>>;
33
+ }, z.core.$strip>]>;
34
+ export type TrackedFileInput = z.infer<typeof TrackedFileInputSchema>;
8
35
  export declare const DocSchema: z.ZodObject<{
9
36
  id: z.ZodString;
10
37
  title: z.ZodString;
@@ -13,6 +40,13 @@ export declare const DocSchema: z.ZodObject<{
13
40
  path: z.ZodString;
14
41
  lastModifiedAt: z.ZodString;
15
42
  contentHash: z.ZodOptional<z.ZodString>;
43
+ sections: z.ZodOptional<z.ZodArray<z.ZodObject<{
44
+ anchors: z.ZodObject<{
45
+ top: z.ZodArray<z.ZodString>;
46
+ bottom: z.ZodArray<z.ZodString>;
47
+ }, z.core.$strip>;
48
+ sectionHash: z.ZodString;
49
+ }, z.core.$strip>>>;
16
50
  }, z.core.$strip>>>;
17
51
  tags: z.ZodDefault<z.ZodArray<z.ZodString>>;
18
52
  folder: z.ZodOptional<z.ZodString>;
@@ -53,7 +87,13 @@ export type FreshnessReport = z.infer<typeof FreshnessReportSchema>;
53
87
  export declare const CreateDocArgsSchema: z.ZodObject<{
54
88
  title: z.ZodString;
55
89
  content: z.ZodString;
56
- tracked_files: z.ZodOptional<z.ZodArray<z.ZodString>>;
90
+ tracked_files: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
91
+ path: z.ZodString;
92
+ lines: z.ZodArray<z.ZodObject<{
93
+ start: z.ZodNumber;
94
+ end: z.ZodNumber;
95
+ }, z.core.$strip>>;
96
+ }, z.core.$strip>]>>>;
57
97
  tags: z.ZodOptional<z.ZodArray<z.ZodString>>;
58
98
  folder: z.ZodOptional<z.ZodString>;
59
99
  related_docs: z.ZodOptional<z.ZodArray<z.ZodString>>;
@@ -62,7 +102,13 @@ export declare const UpdateDocArgsSchema: z.ZodObject<{
62
102
  path: z.ZodString;
63
103
  title: z.ZodOptional<z.ZodString>;
64
104
  content: z.ZodOptional<z.ZodString>;
65
- tracked_files: z.ZodOptional<z.ZodArray<z.ZodString>>;
105
+ tracked_files: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
106
+ path: z.ZodString;
107
+ lines: z.ZodArray<z.ZodObject<{
108
+ start: z.ZodNumber;
109
+ end: z.ZodNumber;
110
+ }, z.core.$strip>>;
111
+ }, z.core.$strip>]>>>;
66
112
  tags: z.ZodOptional<z.ZodArray<z.ZodString>>;
67
113
  related_docs: z.ZodOptional<z.ZodArray<z.ZodString>>;
68
114
  }, z.core.$strip>;
@@ -1,9 +1,28 @@
1
1
  import { z } from "zod";
2
+ export const TrackedSectionSchema = z.object({
3
+ anchors: z.object({
4
+ top: z.array(z.string()),
5
+ bottom: z.array(z.string()),
6
+ }),
7
+ sectionHash: z.string(),
8
+ });
2
9
  export const TrackedFileSchema = z.object({
3
10
  path: z.string(),
4
11
  lastModifiedAt: z.string(),
5
12
  contentHash: z.string().optional(),
6
- });
13
+ sections: z.array(TrackedSectionSchema).optional(),
14
+ });
15
+ export const TrackedFileInputLineSchema = z.object({
16
+ start: z.number().int().positive(),
17
+ end: z.number().int().positive(),
18
+ });
19
+ export const TrackedFileInputSchema = z.union([
20
+ z.string(),
21
+ z.object({
22
+ path: z.string(),
23
+ lines: z.array(TrackedFileInputLineSchema).min(1),
24
+ }),
25
+ ]);
7
26
  export const DocSchema = z.object({
8
27
  id: z.string(),
9
28
  title: z.string(),
@@ -25,7 +44,7 @@ export const FreshnessReportSchema = z.object({
25
44
  export const CreateDocArgsSchema = z.object({
26
45
  title: z.string().describe("Title of the documentation entry"),
27
46
  content: z.string().describe("Documentation content in markdown"),
28
- tracked_files: z.array(z.string()).optional().describe("File paths to track for staleness detection"),
47
+ tracked_files: z.array(TrackedFileInputSchema).optional().describe("Files to track for staleness detection. Each entry is either a plain path string (whole-file tracking) or an object { path, lines: [{ start, end }, ...] } for granular section tracking. Lines are 1-indexed and inclusive."),
29
48
  tags: z.array(z.string()).optional().describe("Tags for categorization"),
30
49
  folder: z.string().optional().describe("Folder path for organization, e.g. 'frontend/components'"),
31
50
  related_docs: z.array(z.string()).optional().describe("IDs of related documentation entries"),
@@ -34,7 +53,7 @@ export const UpdateDocArgsSchema = z.object({
34
53
  path: z.string().describe("Path of the documentation entry to update, e.g. 'backend/auth/authentication' or 'authentication' for root docs"),
35
54
  title: z.string().optional().describe("Title of the documentation entry"),
36
55
  content: z.string().optional().describe("New content (replaces existing)"),
37
- tracked_files: z.array(z.string()).optional().describe("New list of file paths (replaces existing)"),
56
+ tracked_files: z.array(TrackedFileInputSchema).optional().describe("New list of tracked files (replaces existing). Each entry is either a plain path string (whole-file tracking) or an object { path, lines: [{ start, end }, ...] } for granular section tracking. Lines are 1-indexed and inclusive."),
38
57
  tags: z.array(z.string()).optional().describe("New tags (replaces existing)"),
39
58
  related_docs: z.array(z.string()).optional().describe("Paths of related documentation entries (replaces existing)"),
40
59
  });
@@ -0,0 +1,40 @@
1
+ import type { TrackedSection } from "../../shared/schemas/doc.schema.js";
2
+ /**
3
+ * Reads a file and snapshots a section defined by 1-indexed [start, end] line numbers.
4
+ * Extracts top/bottom anchor lines and computes a hash of the section content.
5
+ * The `lines` input is intentionally not persisted — only anchors and sectionHash are stored.
6
+ */
7
+ export declare function snapshotSection(absolutePath: string, start: number, end: number): TrackedSection;
8
+ type FindResult = {
9
+ found: false;
10
+ } | {
11
+ found: true;
12
+ currentHash: string;
13
+ lines: {
14
+ start: number;
15
+ end: number;
16
+ };
17
+ };
18
+ /**
19
+ * Locates a tracked section in the current file content using its stored anchors,
20
+ * then hashes the found content for comparison with the stored sectionHash.
21
+ * Also returns the current 1-indexed line range where the section was found,
22
+ * so callers can display it without storing it.
23
+ *
24
+ * Returns { found: false } if the anchors cannot be located — which is treated as stale,
25
+ * since the surrounding context has changed enough to invalidate the documentation.
26
+ */
27
+ export declare function findAndHashSection(absolutePath: string, section: TrackedSection): FindResult;
28
+ type RelocateResult = {
29
+ found: false;
30
+ } | {
31
+ found: true;
32
+ refreshed: TrackedSection;
33
+ };
34
+ /**
35
+ * Locates a section by its current anchors, then refreshes its sectionHash to match
36
+ * the current file content. Used by refresh_file_snapshot to acknowledge changes.
37
+ * Anchors are kept as-is (they were found successfully); only sectionHash is updated.
38
+ */
39
+ export declare function relocateAndRefreshSection(absolutePath: string, section: TrackedSection): RelocateResult;
40
+ export {};
@@ -0,0 +1,99 @@
1
+ import crypto from "crypto";
2
+ import fs from "fs";
3
+ const ANCHOR_SIZE = 3;
4
+ /**
5
+ * Reads a file and snapshots a section defined by 1-indexed [start, end] line numbers.
6
+ * Extracts top/bottom anchor lines and computes a hash of the section content.
7
+ * The `lines` input is intentionally not persisted — only anchors and sectionHash are stored.
8
+ */
9
+ export function snapshotSection(absolutePath, start, end) {
10
+ const fileContent = fs.readFileSync(absolutePath, "utf-8");
11
+ const allLines = fileContent.split("\n");
12
+ const startIdx = Math.max(0, start - 1);
13
+ const endIdx = Math.min(allLines.length - 1, end - 1);
14
+ const sectionLines = allLines.slice(startIdx, endIdx + 1);
15
+ let top;
16
+ let bottom;
17
+ if (sectionLines.length < ANCHOR_SIZE * 2) {
18
+ // Short section: all lines become the top anchor; bottom is empty (section = top anchor)
19
+ top = [...sectionLines];
20
+ bottom = [];
21
+ }
22
+ else {
23
+ top = sectionLines.slice(0, ANCHOR_SIZE);
24
+ bottom = sectionLines.slice(sectionLines.length - ANCHOR_SIZE);
25
+ }
26
+ const sectionHash = crypto.createHash("sha256").update(sectionLines.join("\n")).digest("hex");
27
+ return { anchors: { top, bottom }, sectionHash };
28
+ }
29
+ /**
30
+ * Locates a tracked section in the current file content using its stored anchors,
31
+ * then hashes the found content for comparison with the stored sectionHash.
32
+ * Also returns the current 1-indexed line range where the section was found,
33
+ * so callers can display it without storing it.
34
+ *
35
+ * Returns { found: false } if the anchors cannot be located — which is treated as stale,
36
+ * since the surrounding context has changed enough to invalidate the documentation.
37
+ */
38
+ export function findAndHashSection(absolutePath, section) {
39
+ const fileContent = fs.readFileSync(absolutePath, "utf-8");
40
+ const fileLines = fileContent.split("\n");
41
+ const { top, bottom } = section.anchors;
42
+ if (bottom.length === 0) {
43
+ // Short-section path: the top anchor IS the full section content
44
+ for (let i = 0; i <= fileLines.length - top.length; i++) {
45
+ if (top.every((line, j) => fileLines[i + j] === line)) {
46
+ const sectionLines = fileLines.slice(i, i + top.length);
47
+ const currentHash = crypto.createHash("sha256").update(sectionLines.join("\n")).digest("hex");
48
+ return { found: true, currentHash, lines: { start: i + 1, end: i + top.length } };
49
+ }
50
+ }
51
+ return { found: false };
52
+ }
53
+ // Normal path: locate top anchor, then search for bottom anchor below it
54
+ for (let i = 0; i <= fileLines.length - top.length; i++) {
55
+ if (!top.every((line, j) => fileLines[i + j] === line))
56
+ continue;
57
+ const searchStart = i + top.length;
58
+ for (let k = searchStart; k <= fileLines.length - bottom.length; k++) {
59
+ if (!bottom.every((line, j) => fileLines[k + j] === line))
60
+ continue;
61
+ const sectionLines = fileLines.slice(i, k + bottom.length);
62
+ const currentHash = crypto.createHash("sha256").update(sectionLines.join("\n")).digest("hex");
63
+ return { found: true, currentHash, lines: { start: i + 1, end: k + bottom.length } };
64
+ }
65
+ }
66
+ return { found: false };
67
+ }
68
+ /**
69
+ * Locates a section by its current anchors, then refreshes its sectionHash to match
70
+ * the current file content. Used by refresh_file_snapshot to acknowledge changes.
71
+ * Anchors are kept as-is (they were found successfully); only sectionHash is updated.
72
+ */
73
+ export function relocateAndRefreshSection(absolutePath, section) {
74
+ const fileContent = fs.readFileSync(absolutePath, "utf-8");
75
+ const fileLines = fileContent.split("\n");
76
+ const { top, bottom } = section.anchors;
77
+ if (bottom.length === 0) {
78
+ for (let i = 0; i <= fileLines.length - top.length; i++) {
79
+ if (top.every((line, j) => fileLines[i + j] === line)) {
80
+ const sectionHash = crypto.createHash("sha256").update(fileLines.slice(i, i + top.length).join("\n")).digest("hex");
81
+ return { found: true, refreshed: { anchors: section.anchors, sectionHash } };
82
+ }
83
+ }
84
+ return { found: false };
85
+ }
86
+ for (let i = 0; i <= fileLines.length - top.length; i++) {
87
+ if (!top.every((line, j) => fileLines[i + j] === line))
88
+ continue;
89
+ const searchStart = i + top.length;
90
+ for (let k = searchStart; k <= fileLines.length - bottom.length; k++) {
91
+ if (!bottom.every((line, j) => fileLines[k + j] === line))
92
+ continue;
93
+ const sectionLines = fileLines.slice(i, k + bottom.length);
94
+ const sectionHash = crypto.createHash("sha256").update(sectionLines.join("\n")).digest("hex");
95
+ return { found: true, refreshed: { anchors: section.anchors, sectionHash } };
96
+ }
97
+ }
98
+ return { found: false };
99
+ }
package/dist/src/index.js CHANGED
@@ -33,6 +33,7 @@ import { HistoryStore } from "./core/history-store.js";
33
33
  import { listDocVersions } from "./tools/list-doc-versions.js";
34
34
  import { getDocVersion } from "./tools/get-doc-version.js";
35
35
  import { restoreDocVersion } from "./tools/restore-doc-version.js";
36
+ import { reindexEmbeddings } from "./tools/reindex-embeddings.js";
36
37
  const __filename = fileURLToPath(import.meta.url);
37
38
  const __dirname = path.dirname(__filename);
38
39
  function findProjectRoot(startDir) {
@@ -449,8 +450,33 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
449
450
  content: { type: "string", description: "Documentation content in markdown. To link to another document inline, use the syntax [text](doc://target-doc-id) — the viewer will navigate to that document on click. To link to a heading within the same document, use [text](#heading-slug). Use check_links to validate both types. For code blocks, always specify a language identifier for syntax highlighting, e.g. ```typescript, ```python, ```bash, ```json, ```yaml, ```sql." },
450
451
  tracked_files: {
451
452
  type: "array",
452
- items: { type: "string" },
453
- description: "File paths to track for staleness detection",
453
+ items: {
454
+ oneOf: [
455
+ { type: "string", description: "Plain file path for whole-file tracking" },
456
+ {
457
+ type: "object",
458
+ properties: {
459
+ path: { type: "string", description: "File path to track" },
460
+ lines: {
461
+ type: "array",
462
+ items: {
463
+ type: "object",
464
+ properties: {
465
+ start: { type: "integer", minimum: 1, description: "First line of the section (1-indexed, inclusive)" },
466
+ end: { type: "integer", minimum: 1, description: "Last line of the section (1-indexed, inclusive)" },
467
+ },
468
+ required: ["start", "end"],
469
+ },
470
+ minItems: 1,
471
+ description: "One or more line ranges to track within the file",
472
+ },
473
+ },
474
+ required: ["path", "lines"],
475
+ description: "Granular section tracking: only the specified line ranges are monitored",
476
+ },
477
+ ],
478
+ },
479
+ description: "Files to track for staleness detection. Use a plain path string for whole-file tracking, or { path, lines: [{ start, end }] } to track specific sections only (reduces false positives when unrelated parts of the file change).",
454
480
  },
455
481
  tags: {
456
482
  type: "array",
@@ -503,8 +529,33 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
503
529
  content: { type: "string", description: "New content (replaces existing). Supports [text](doc://target-doc-path) to link inline to other documents, and [text](#heading-slug) to link to headings within this document. For code blocks, always specify a language identifier for syntax highlighting, e.g. ```typescript, ```python, ```bash, ```json, ```yaml, ```sql." },
504
530
  tracked_files: {
505
531
  type: "array",
506
- items: { type: "string" },
507
- description: "New list of file paths (replaces existing)",
532
+ items: {
533
+ oneOf: [
534
+ { type: "string", description: "Plain file path for whole-file tracking" },
535
+ {
536
+ type: "object",
537
+ properties: {
538
+ path: { type: "string", description: "File path to track" },
539
+ lines: {
540
+ type: "array",
541
+ items: {
542
+ type: "object",
543
+ properties: {
544
+ start: { type: "integer", minimum: 1, description: "First line of the section (1-indexed, inclusive)" },
545
+ end: { type: "integer", minimum: 1, description: "Last line of the section (1-indexed, inclusive)" },
546
+ },
547
+ required: ["start", "end"],
548
+ },
549
+ minItems: 1,
550
+ description: "One or more line ranges to track within the file",
551
+ },
552
+ },
553
+ required: ["path", "lines"],
554
+ description: "Granular section tracking: only the specified line ranges are monitored",
555
+ },
556
+ ],
557
+ },
558
+ description: "New list of tracked files (replaces existing). Use a plain path string for whole-file tracking, or { path, lines: [{ start, end }] } to track specific sections only (reduces false positives when unrelated parts of the file change).",
508
559
  },
509
560
  tags: {
510
561
  type: "array",
@@ -709,6 +760,18 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
709
760
  required: ["path", "version"],
710
761
  },
711
762
  },
763
+ {
764
+ name: "reindex_embeddings",
765
+ description: "Generate missing embeddings for all documentation entries in a project (or all projects). Useful when importing existing documentation that was created outside the MCP. Use force: true to regenerate embeddings for all docs, including those already indexed.",
766
+ inputSchema: {
767
+ type: "object",
768
+ properties: {
769
+ project: { type: "string", description: "Project to reindex (processes all projects if omitted)" },
770
+ force: { type: "boolean", description: "If true, regenerates embeddings for all docs even if they are already indexed (default: false)" },
771
+ },
772
+ required: [],
773
+ },
774
+ },
712
775
  ],
713
776
  };
714
777
  });
@@ -754,6 +817,8 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
754
817
  return await getDocVersion(historyStore, storageEngine, args);
755
818
  case "restore_doc_version":
756
819
  return await restoreDocVersion(historyStore, storageEngine, embeddingEngine, args);
820
+ case "reindex_embeddings":
821
+ return await reindexEmbeddings(storageEngine, embeddingEngine, embeddingStore, args);
757
822
  default:
758
823
  throw new Error(`Tool not found: ${name}`);
759
824
  }
@@ -2,6 +2,7 @@ import crypto from "crypto";
2
2
  import fs from "fs";
3
3
  import path from "path";
4
4
  import { CheckFreshnessArgsSchema } from "../../shared/schemas/doc.schema.js";
5
+ import { findAndHashSection } from "../core/section-tracker.js";
5
6
  export function computeFileHash(absolutePath) {
6
7
  return crypto.createHash("sha256").update(fs.readFileSync(absolutePath)).digest("hex");
7
8
  }
@@ -18,7 +19,23 @@ export function computeFreshness(trackedFiles) {
18
19
  missing.push(tracked.path);
19
20
  continue;
20
21
  }
21
- if (tracked.contentHash) {
22
+ if (tracked.sections && tracked.sections.length > 0) {
23
+ let sectionStale = false;
24
+ for (const section of tracked.sections) {
25
+ const result = findAndHashSection(absolutePath, section);
26
+ if (!result.found || result.currentHash !== section.sectionHash) {
27
+ sectionStale = true;
28
+ break;
29
+ }
30
+ }
31
+ if (sectionStale) {
32
+ modified.push(tracked.path);
33
+ }
34
+ else {
35
+ unchanged.push(tracked.path);
36
+ }
37
+ }
38
+ else if (tracked.contentHash) {
22
39
  const currentHash = computeFileHash(absolutePath);
23
40
  if (currentHash !== tracked.contentHash) {
24
41
  modified.push(tracked.path);
@@ -2,6 +2,7 @@ import fs from "fs";
2
2
  import path from "path";
3
3
  import { CreateDocArgsSchema } from "../../shared/schemas/doc.schema.js";
4
4
  import { computeFileHash } from "./check-freshness.js";
5
+ import { snapshotSection } from "../core/section-tracker.js";
5
6
  function slugify(title) {
6
7
  return title
7
8
  .normalize("NFD")
@@ -10,17 +11,24 @@ function slugify(title) {
10
11
  .replace(/[^a-z0-9]+/g, "-")
11
12
  .replace(/^-|-$/g, "");
12
13
  }
13
- function snapshotTrackedFiles(filePaths) {
14
+ function snapshotTrackedFiles(inputs) {
14
15
  const tracked = [];
15
- for (const fp of filePaths) {
16
+ for (const input of inputs) {
17
+ const fp = typeof input === "string" ? input : input.path;
16
18
  const absolutePath = path.resolve(process.cwd(), fp);
17
- let mtime = new Date().toISOString();
18
- let contentHash;
19
- if (fs.existsSync(absolutePath)) {
20
- mtime = fs.statSync(absolutePath).mtime.toISOString();
21
- contentHash = computeFileHash(absolutePath);
19
+ const mtime = fs.existsSync(absolutePath)
20
+ ? fs.statSync(absolutePath).mtime.toISOString()
21
+ : new Date().toISOString();
22
+ if (typeof input !== "string" && input.lines.length > 0 && fs.existsSync(absolutePath)) {
23
+ const sections = input.lines
24
+ .filter(({ start, end }) => start <= end)
25
+ .map(({ start, end }) => snapshotSection(absolutePath, start, end));
26
+ tracked.push({ path: absolutePath, lastModifiedAt: mtime, sections });
27
+ }
28
+ else {
29
+ const contentHash = fs.existsSync(absolutePath) ? computeFileHash(absolutePath) : undefined;
30
+ tracked.push({ path: absolutePath, lastModifiedAt: mtime, ...(contentHash !== undefined && { contentHash }) });
22
31
  }
23
- tracked.push({ path: absolutePath, lastModifiedAt: mtime, ...(contentHash !== undefined && { contentHash }) });
24
32
  }
25
33
  return tracked;
26
34
  }
@@ -1,5 +1,6 @@
1
1
  import { GetDocMetadataArgsSchema } from "../../shared/schemas/doc.schema.js";
2
2
  import { computeFreshness } from "./check-freshness.js";
3
+ import { findAndHashSection } from "../core/section-tracker.js";
3
4
  export async function getDocMetadata(storageEngine, args) {
4
5
  try {
5
6
  const validated = GetDocMetadataArgsSchema.parse(args);
@@ -24,9 +25,20 @@ export async function getDocMetadata(storageEngine, args) {
24
25
  text += `**Freshness:** ${report.status}\n`;
25
26
  if (doc.tracked_files.length > 0) {
26
27
  text += `\nTracked files (${doc.tracked_files.length} total):\n`;
27
- text += doc.tracked_files
28
- .map((f) => ` ${fileStatusIcon(f.path)} — ${f.path}\n Snapshot: ${f.lastModifiedAt}`)
29
- .join("\n");
28
+ text += doc.tracked_files.map((f) => {
29
+ let entry = ` ${fileStatusIcon(f.path)} — ${f.path}\n Snapshot: ${f.lastModifiedAt}`;
30
+ if (f.sections && f.sections.length > 0) {
31
+ const sectionLines = f.sections.map((section, idx) => {
32
+ const result = findAndHashSection(f.path, section);
33
+ const location = result.found
34
+ ? `lines ${result.lines.start}–${result.lines.end}`
35
+ : "section introuvable (ancres modifiées)";
36
+ return ` - Section ${idx + 1}: ${location}`;
37
+ });
38
+ entry += `\n Sections (position actuelle):\n${sectionLines.join("\n")}`;
39
+ }
40
+ return entry;
41
+ }).join("\n");
30
42
  }
31
43
  else {
32
44
  text += "\nNo tracked files.";
@@ -1,5 +1,6 @@
1
1
  import { GetDocArgsSchema } from "../../shared/schemas/doc.schema.js";
2
2
  import { computeFreshness } from "./check-freshness.js";
3
+ import { findAndHashSection } from "../core/section-tracker.js";
3
4
  export async function getDoc(storageEngine, args) {
4
5
  try {
5
6
  const validated = GetDocArgsSchema.parse(args);
@@ -9,7 +10,19 @@ export async function getDoc(storageEngine, args) {
9
10
  }
10
11
  const { doc, project: resolvedProject } = resolved;
11
12
  const trackedFilesText = doc.tracked_files.length > 0
12
- ? doc.tracked_files.map((f) => ` - ${f.path} (snapshot: ${f.lastModifiedAt})`).join("\n")
13
+ ? doc.tracked_files.map((f) => {
14
+ if (f.sections && f.sections.length > 0) {
15
+ const sectionLines = f.sections.map((section, idx) => {
16
+ const result = findAndHashSection(f.path, section);
17
+ const location = result.found
18
+ ? `lines ${result.lines.start}–${result.lines.end}`
19
+ : "section introuvable";
20
+ return ` - Section ${idx + 1}: ${location}`;
21
+ });
22
+ return ` - ${f.path} (snapshot: ${f.lastModifiedAt})\n${sectionLines.join("\n")}`;
23
+ }
24
+ return ` - ${f.path} (snapshot: ${f.lastModifiedAt})`;
25
+ }).join("\n")
13
26
  : " (none)";
14
27
  const freshnessReport = computeFreshness(doc.tracked_files);
15
28
  const stalenessBanner = freshnessReport.status === "stale"
@@ -1,6 +1,7 @@
1
1
  import fs from "fs";
2
2
  import { RefreshFileSnapshotArgsSchema } from "../../shared/schemas/doc.schema.js";
3
3
  import { computeFileHash } from "./check-freshness.js";
4
+ import { relocateAndRefreshSection } from "../core/section-tracker.js";
4
5
  export async function refreshFileSnapshot(storageEngine, historyStore, args) {
5
6
  try {
6
7
  const validated = RefreshFileSnapshotArgsSchema.parse(args);
@@ -25,7 +26,16 @@ export async function refreshFileSnapshot(storageEngine, historyStore, args) {
25
26
  continue;
26
27
  }
27
28
  tracked.lastModifiedAt = fs.statSync(tracked.path).mtime.toISOString();
28
- tracked.contentHash = computeFileHash(tracked.path);
29
+ if (tracked.sections && tracked.sections.length > 0) {
30
+ const refreshedSections = tracked.sections.map((section) => {
31
+ const result = relocateAndRefreshSection(tracked.path, section);
32
+ return result.found ? result.refreshed : section;
33
+ });
34
+ tracked.sections = refreshedSections;
35
+ }
36
+ else {
37
+ tracked.contentHash = computeFileHash(tracked.path);
38
+ }
29
39
  refreshed.push(tracked.path);
30
40
  }
31
41
  await storageEngine.saveDoc(doc, resolvedProject);
@@ -0,0 +1,16 @@
1
+ import type { StorageEngine } from "../core/storage-engine.js";
2
+ import type { EmbeddingEngine } from "../core/embedding-engine.js";
3
+ import type { EmbeddingStore } from "../core/embedding-store.js";
4
+ export declare function reindexEmbeddings(storageEngine: StorageEngine, embeddingEngine: EmbeddingEngine, embeddingStore: EmbeddingStore, args: unknown): Promise<{
5
+ isError: boolean;
6
+ content: {
7
+ type: string;
8
+ text: string;
9
+ }[];
10
+ } | {
11
+ content: {
12
+ type: string;
13
+ text: string;
14
+ }[];
15
+ isError?: undefined;
16
+ }>;
@@ -0,0 +1,83 @@
1
+ import { z } from "zod";
2
+ const ReindexEmbeddingsArgsSchema = z.object({
3
+ project: z.string().optional(),
4
+ force: z.boolean().optional().default(false),
5
+ });
6
+ export async function reindexEmbeddings(storageEngine, embeddingEngine, embeddingStore, args) {
7
+ try {
8
+ if (!embeddingEngine.isConfigured()) {
9
+ return {
10
+ isError: true,
11
+ content: [
12
+ {
13
+ type: "text",
14
+ text: "OpenAI API key is not configured. Set MCP_DOCUMENTOR_OPENAI_API_KEY in your .env file to enable embedding generation.",
15
+ },
16
+ ],
17
+ };
18
+ }
19
+ const validated = ReindexEmbeddingsArgsSchema.parse(args);
20
+ const config = storageEngine.getConfig();
21
+ const projectsToProcess = validated.project
22
+ ? [validated.project]
23
+ : config.projects;
24
+ const summary = [];
25
+ for (const project of projectsToProcess) {
26
+ const docs = await storageEngine.listDocs(project);
27
+ if (docs.length === 0) {
28
+ summary.push({ project, indexed: 0, skipped: 0, failed: 0 });
29
+ continue;
30
+ }
31
+ const existingEmbeddings = embeddingStore.loadAll(project);
32
+ let indexed = 0;
33
+ let skipped = 0;
34
+ let failed = 0;
35
+ for (const docSummary of docs) {
36
+ const alreadyIndexed = existingEmbeddings.has(docSummary.id);
37
+ if (alreadyIndexed && !validated.force) {
38
+ skipped++;
39
+ continue;
40
+ }
41
+ const doc = await storageEngine.getDoc(docSummary.id, project);
42
+ if (!doc) {
43
+ failed++;
44
+ continue;
45
+ }
46
+ const embedding = await embeddingEngine.generateEmbedding(doc.content);
47
+ if (embedding) {
48
+ await embeddingEngine.saveEmbedding(doc.id, embedding, project);
49
+ indexed++;
50
+ }
51
+ else {
52
+ failed++;
53
+ }
54
+ }
55
+ summary.push({ project, indexed, skipped, failed });
56
+ }
57
+ const lines = summary.map((s) => `• [${s.project}] ${s.indexed} indexed, ${s.skipped} skipped (already indexed), ${s.failed} failed`);
58
+ const totalIndexed = summary.reduce((acc, s) => acc + s.indexed, 0);
59
+ const totalSkipped = summary.reduce((acc, s) => acc + s.skipped, 0);
60
+ const totalFailed = summary.reduce((acc, s) => acc + s.failed, 0);
61
+ const resultText = [
62
+ `Reindex complete across ${summary.length} project(s):`,
63
+ ...lines,
64
+ "",
65
+ `Total: ${totalIndexed} indexed, ${totalSkipped} skipped, ${totalFailed} failed.`,
66
+ totalSkipped > 0 && !validated.force
67
+ ? "Tip: use force: true to regenerate all embeddings including existing ones."
68
+ : "",
69
+ ]
70
+ .filter((l) => l !== "")
71
+ .join("\n");
72
+ return {
73
+ content: [{ type: "text", text: resultText }],
74
+ };
75
+ }
76
+ catch (err) {
77
+ const message = err instanceof Error ? err.message : String(err);
78
+ return {
79
+ isError: true,
80
+ content: [{ type: "text", text: `Error during reindex: ${message}` }],
81
+ };
82
+ }
83
+ }
@@ -2,17 +2,25 @@ import fs from "fs";
2
2
  import path from "path";
3
3
  import { UpdateDocArgsSchema } from "../../shared/schemas/doc.schema.js";
4
4
  import { computeFileHash } from "./check-freshness.js";
5
- function snapshotTrackedFiles(filePaths) {
5
+ import { snapshotSection } from "../core/section-tracker.js";
6
+ function snapshotTrackedFiles(inputs) {
6
7
  const tracked = [];
7
- for (const fp of filePaths) {
8
+ for (const input of inputs) {
9
+ const fp = typeof input === "string" ? input : input.path;
8
10
  const absolutePath = path.resolve(process.cwd(), fp);
9
- let mtime = new Date().toISOString();
10
- let contentHash;
11
- if (fs.existsSync(absolutePath)) {
12
- mtime = fs.statSync(absolutePath).mtime.toISOString();
13
- contentHash = computeFileHash(absolutePath);
11
+ const mtime = fs.existsSync(absolutePath)
12
+ ? fs.statSync(absolutePath).mtime.toISOString()
13
+ : new Date().toISOString();
14
+ if (typeof input !== "string" && input.lines.length > 0 && fs.existsSync(absolutePath)) {
15
+ const sections = input.lines
16
+ .filter(({ start, end }) => start <= end)
17
+ .map(({ start, end }) => snapshotSection(absolutePath, start, end));
18
+ tracked.push({ path: absolutePath, lastModifiedAt: mtime, sections });
19
+ }
20
+ else {
21
+ const contentHash = fs.existsSync(absolutePath) ? computeFileHash(absolutePath) : undefined;
22
+ tracked.push({ path: absolutePath, lastModifiedAt: mtime, ...(contentHash !== undefined && { contentHash }) });
14
23
  }
15
- tracked.push({ path: absolutePath, lastModifiedAt: mtime, ...(contentHash !== undefined && { contentHash }) });
16
24
  }
17
25
  return tracked;
18
26
  }
@@ -1 +1 @@
1
- export declare const APP_VERSION = "1.0.33";
1
+ export declare const APP_VERSION = "1.0.35";
@@ -1,2 +1,2 @@
1
1
  // Auto-generated version file - do not edit manually
2
- export const APP_VERSION = '1.0.33';
2
+ export const APP_VERSION = '1.0.35';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imenam/mcp-documentor",
3
- "version": "1.0.33",
3
+ "version": "1.0.35",
4
4
  "main": "dist/src/index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",