import { z } from 'zod';
import { PathResolver } from '../utils/path-resolver.js';
import { extractFrontmatter } from '../utils/mdx-processor.js';
import { ToolResponse } from '../types/index.js';
/**
* Input schema for get_mdx_frontmatter tool
*/
export const getFrontmatterInputSchema = z.object({
path: z.string().describe('Path to the MDX file relative to workspace root')
});
/**
* Output schema for get_mdx_frontmatter tool
*/
export const getFrontmatterOutputSchema = z.object({
frontmatter: z.record(z.string(), z.any()).nullable(),
hasFrontmatter: z.boolean()
});
/**
* Extract frontmatter from an MDX file
*
* @param input - Tool input with path parameter
* @param pathResolver - PathResolver instance
* @returns Tool response with frontmatter data
*/
export async function getFrontmatterTool(
input: z.infer<typeof getFrontmatterInputSchema>,
pathResolver: PathResolver
): Promise<ToolResponse> {
try {
const { path } = input;
// Resolve the path
const resolvedPath = pathResolver.resolveWorkspacePath(path);
// Validate path safety
if (!pathResolver.isPathSafe(path)) {
return {
content: [{
type: 'text',
text: `Error: Path is outside workspace root: ${path}`
}],
isError: true
};
}
// Validate file exists
if (!pathResolver.validateFilePath(path)) {
return {
content: [{
type: 'text',
text: `Error: File not found at path: ${path}`
}],
isError: true
};
}
// Extract frontmatter
const frontmatter = await extractFrontmatter(resolvedPath);
const result = {
frontmatter,
hasFrontmatter: frontmatter !== null
};
return {
content: [{
type: 'text',
text: JSON.stringify(result, null, 2)
}]
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
content: [{
type: 'text',
text: `Error extracting frontmatter: ${errorMessage}`
}],
isError: true
};
}
}