import { z } from "zod";
import { fetchPage, FumadocsError } from "../services/docs-fetcher.js";
import { COMPONENT_PATHS, searchDocsIndex } from "../data/docs-index.js";
export const getComponentSchema = {
component: z
.string()
.describe("Component name (e.g., 'accordion', 'tabs', 'codeblock', 'steps')"),
};
export type GetComponentParams = {
component: string;
};
export async function getComponent(params: GetComponentParams): Promise<string> {
const { component } = params;
if (!component || component.trim().length === 0) {
return "Error: Please provide a component name.";
}
const normalizedComponent = component.toLowerCase().trim();
// Try exact match first
let componentPath = COMPONENT_PATHS[normalizedComponent];
// If no exact match, try to find it
if (!componentPath) {
// Search for it in the docs index
const searchResults = searchDocsIndex(normalizedComponent, "ui");
const componentResult = searchResults.find(
(r) => r.path.includes("/components/") && r.title.toLowerCase().includes(normalizedComponent)
);
if (componentResult) {
componentPath = componentResult.path;
}
}
if (!componentPath) {
// List available components
const availableComponents = Object.keys(COMPONENT_PATHS).sort();
return [
`Component not found: ${component}`,
"",
"Available components:",
...availableComponents.map((c) => `- ${c}`),
"",
"Tips:",
"- Use the exact component name",
"- Try `search_docs` with your query to find related documentation",
].join("\n");
}
try {
const content = await fetchPage(componentPath);
const output: string[] = [
`# Component: ${component}`,
`Path: ${componentPath}`,
"",
"---",
"",
content,
"",
"---",
"",
"## Usage Tips",
"",
"1. Import the component from `fumadocs-ui/components`",
"2. Check the props table above for configuration options",
"3. See the examples for common usage patterns",
"",
`Source: https://fumadocs.vercel.app${componentPath}`,
];
return output.join("\n");
} catch (error) {
if (error instanceof FumadocsError) {
if (error.code === "PAGE_NOT_FOUND") {
return `Component documentation not found at: ${componentPath}`;
}
return `Error fetching component docs: ${error.message} (${error.code})`;
}
return `Unexpected error: ${error instanceof Error ? error.message : "Unknown error"}`;
}
}