40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
/**
|
|
* MCP tools for rDocs (linked documents).
|
|
*
|
|
* Tools: rdocs_list_documents
|
|
*/
|
|
|
|
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
import { z } from "zod";
|
|
import type { SyncServer } from "../local-first/sync-server";
|
|
import { docsDocId } from "../../modules/rdocs/schemas";
|
|
import type { DocsDoc } from "../../modules/rdocs/schemas";
|
|
import { resolveAccess, accessDeniedResponse } from "./_auth";
|
|
|
|
export function registerDocsTools(server: McpServer, syncServer: SyncServer) {
|
|
server.tool(
|
|
"rdocs_list_documents",
|
|
"List linked documents in a space",
|
|
{
|
|
space: z.string().describe("Space slug"),
|
|
token: z.string().optional().describe("JWT auth token"),
|
|
search: z.string().optional().describe("Search in title"),
|
|
},
|
|
async ({ space, token, search }) => {
|
|
const access = await resolveAccess(token, space, false);
|
|
if (!access.allowed) return accessDeniedResponse(access.reason!);
|
|
|
|
const doc = syncServer.getDoc<DocsDoc>(docsDocId(space));
|
|
if (!doc) return { content: [{ type: "text", text: JSON.stringify({ error: "No docs data found" }) }] };
|
|
|
|
let documents = Object.values(doc.linkedDocuments || {});
|
|
if (search) {
|
|
const q = search.toLowerCase();
|
|
documents = documents.filter(d => d.title.toLowerCase().includes(q));
|
|
}
|
|
|
|
return { content: [{ type: "text", text: JSON.stringify(documents, null, 2) }] };
|
|
},
|
|
);
|
|
}
|