fumadocs-core
Version:
The React.js library for building a documentation website
54 lines (53 loc) • 1.63 kB
JavaScript
import { z } from "zod";
// src/mcp.ts
/**
* Register a `search` tool, backed by a search server from `fumadocs-core/search/server`.
*/
function registerSearchTool(mcp, server) {
mcp.registerTool("search", {
title: "Search Docs",
description: "Search docs pages with a query",
inputSchema: z.object({
query: z.string(),
locale: z.string().optional()
})
}, async ({ query, locale }) => ({ content: [{
type: "text",
text: JSON.stringify(await server.search(query, { locale }))
}] }));
}
/**
* Register `list_pages` and `get_page` tools, backed by a source and its `llms()` renderer.
*/
function registerSourceTools(mcp, source, llms) {
mcp.registerTool("list_pages", {
title: "List Pages",
description: "List all docs pages with their pathnames (URLs)",
inputSchema: z.object({})
}, async () => ({ content: [{
type: "text",
text: await llms.index()
}] }));
mcp.registerTool("get_page", {
title: "Get Page",
description: "Get the Markdown content of a docs page by its pathname (URL)",
inputSchema: z.object({ url: z.string() })
}, async ({ url }) => {
const loader = typeof source === "function" ? await source() : source;
const pathname = url.replace(/[?#].*$/, "");
const page = loader.getPageByUrl(pathname);
const text = page ? await llms.page(page) : pathname.endsWith(".md") ? await (await llms.serve(pathname))?.text() : void 0;
if (text === void 0) return {
content: [{
type: "text",
text: `page not found: ${url}`
}],
isError: true
};
return { content: [{
type: "text",
text
}] };
});
}
export { registerSearchTool, registerSourceTools };