import { z } from 'zod';
import { PathResolver } from '../utils/path-resolver.js';
import { convertMdxToMarkdown } from '../utils/mdx-processor.js';
import { ToolResponse } from '../types/index.js';
/**
* Input schema for read_mdx tool
*/
export const readMdxInputSchema = z.object({
path: z.string().describe('Path to the MDX file relative to workspace root')
});
/**
* Output schema for read_mdx tool
*/
export const readMdxOutputSchema = z.object({
markdown: z.string().describe('The MDX file converted to Markdown'),
originalPath: z.string().describe('Original file path'),
resolvedPath: z.string().describe('Absolute resolved file path')
});
/**
* Read an MDX file and return its contents as Markdown
*
* @param input - Tool input with path parameter
* @param pathResolver - PathResolver instance
* @returns Tool response with markdown content
*/
export async function readMdxTool(
input: z.infer<typeof readMdxInputSchema>,
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
};
}
// Convert MDX to Markdown
const markdown = await convertMdxToMarkdown(resolvedPath);
const result = {
markdown,
originalPath: path,
resolvedPath
};
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 processing MDX file: ${errorMessage}`
}],
isError: true
};
}
}