|
| 1 | +import { readdir, readFile } from 'fs/promises' |
| 2 | +import { join } from 'path' |
| 3 | + |
| 4 | +// Markdown resources (e.g. the writing style guide) live as plain `.md` files in |
| 5 | +// `cms/resources/` so they stay versioned and browsable in Git. They are read at |
| 6 | +// runtime and exposed through the `getResources` MCP tool. |
| 7 | +// |
| 8 | +// `process.cwd()` is the cms project root both locally and in the Vercel serverless |
| 9 | +// function. The files are not statically reachable (we scan the directory at runtime), |
| 10 | +// so they are force-included into the function bundle via `outputFileTracingIncludes` |
| 11 | +// in `next.config.ts` — keep that entry in sync with this directory. |
| 12 | +const RESOURCES_DIR = join(process.cwd(), 'resources') |
| 13 | + |
| 14 | +// Only simple, lowercase-kebab slugs are valid. Enforced before building a file path |
| 15 | +// to prevent path traversal, since the slug originates from an MCP tool argument. |
| 16 | +const SLUG_PATTERN = /^[a-z0-9-]+$/ |
| 17 | + |
| 18 | +export type ResourceMeta = { |
| 19 | + slug: string |
| 20 | + title: string |
| 21 | + description: string |
| 22 | +} |
| 23 | + |
| 24 | +export type Resource = ResourceMeta & { |
| 25 | + content: string |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Minimal YAML front matter parser. Handles the simple `key: value` scalars we use |
| 30 | + * (`title`, `description`) — not nested structures. Returns the parsed key/value map |
| 31 | + * and the markdown body with the front matter block stripped. |
| 32 | + */ |
| 33 | +function parseFrontMatter(raw: string): { data: Record<string, string>; body: string } { |
| 34 | + const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/.exec(raw) |
| 35 | + if (!match) return { body: raw, data: {} } |
| 36 | + |
| 37 | + const data: Record<string, string> = {} |
| 38 | + for (const line of match[1].split(/\r?\n/)) { |
| 39 | + const separator = line.indexOf(':') |
| 40 | + if (separator === -1) continue |
| 41 | + const key = line.slice(0, separator).trim() |
| 42 | + if (!key) continue |
| 43 | + // Strip surrounding single/double quotes from the value. |
| 44 | + const value = line |
| 45 | + .slice(separator + 1) |
| 46 | + .trim() |
| 47 | + .replace(/^['"]|['"]$/g, '') |
| 48 | + data[key] = value |
| 49 | + } |
| 50 | + |
| 51 | + return { body: raw.slice(match[0].length), data } |
| 52 | +} |
| 53 | + |
| 54 | +function toMeta(slug: string, data: Record<string, string>): ResourceMeta { |
| 55 | + return { |
| 56 | + description: data.description ?? '', |
| 57 | + slug, |
| 58 | + title: data.title ?? slug, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** Lists all available resources with their front matter metadata. */ |
| 63 | +export async function listResources(): Promise<ResourceMeta[]> { |
| 64 | + let entries: string[] |
| 65 | + try { |
| 66 | + entries = await readdir(RESOURCES_DIR) |
| 67 | + } catch { |
| 68 | + return [] |
| 69 | + } |
| 70 | + |
| 71 | + const slugs = entries.filter((name) => name.endsWith('.md')).map((name) => name.slice(0, -3)) |
| 72 | + |
| 73 | + const resources = await Promise.all( |
| 74 | + slugs.map(async (slug) => { |
| 75 | + const raw = await readFile(join(RESOURCES_DIR, `${slug}.md`), 'utf-8') |
| 76 | + return toMeta(slug, parseFrontMatter(raw).data) |
| 77 | + }), |
| 78 | + ) |
| 79 | + |
| 80 | + return resources.sort((a, b) => a.slug.localeCompare(b.slug)) |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Reads a single resource by slug, returning its metadata and markdown body (front |
| 85 | + * matter stripped). Returns `null` for an invalid or unknown slug. |
| 86 | + */ |
| 87 | +export async function readResource(slug: string): Promise<Resource | null> { |
| 88 | + if (!SLUG_PATTERN.test(slug)) return null |
| 89 | + |
| 90 | + let raw: string |
| 91 | + try { |
| 92 | + raw = await readFile(join(RESOURCES_DIR, `${slug}.md`), 'utf-8') |
| 93 | + } catch { |
| 94 | + return null |
| 95 | + } |
| 96 | + |
| 97 | + const { body, data } = parseFrontMatter(raw) |
| 98 | + return { ...toMeta(slug, data), content: body.trim() } |
| 99 | +} |
0 commit comments