feat: fuck mcp, we'll just send the json

This commit is contained in:
2025-08-07 12:55:43 -07:00
parent e49137bb08
commit 0f058870a8
12 changed files with 19 additions and 399 deletions
-85
View File
@@ -1,85 +0,0 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
/**
* This script fetches our documentation from our repository,
* compiles it into an MCP format, and writes it to a JSON file.
* It is intended to run automatically as part of the build process.
*/
import fs from "node:fs/promises";
import path from "node:path";
import matter from "gray-matter";
import { promisify } from "node:util";
import { exec } from "node:child_process";
const execAsync = promisify(exec);
const docsDirectory = path.resolve(process.cwd(), "temp-docs");
const docsPath = path.resolve(process.cwd(), "temp-docs", "src", "content", "docs")
async function walk(directory: string): Promise<Array<string>> {
const dirents = await fs.readdir(directory, { withFileTypes: true });
const files = await Promise.all(
dirents.map(async (dirent) => {
const result = path.resolve(directory, dirent.name);
return dirent.isDirectory() ? await walk(result) : result;
})
);
return files.flat();
}
await execAsync(`git clone https://git.nhcarrigan.com/nhcarrigan/docs.git ${docsDirectory}`, {
cwd: process.cwd(),
stdio: "inherit",
});
const files = await walk(docsPath);
const markdownFiles = files.filter((f) => {
return f.endsWith(".md");
});
const results = await Promise.all(
markdownFiles.map(async (file) => {
const raw = await fs.readFile(file, "utf-8");
const { content, data } = matter(raw);
// Split content by header blocks (basic chunking)
const chunks = content.split(/^#+\s+/gm).map((chunk, index) => {
return {
content: chunk.trim(),
file: path.relative(docsDirectory, file),
id: `${path.relative(docsDirectory, file)}::${index}`,
metadata: data,
title:
index === 0 ? "(intro)" : chunk.split("\n")[0]?.trim() ?? "Unknown",
url: `https://docs.nhcarrigan.com/${path
.relative(docsPath, file)
.replace(/\.md$/, "").replace(/\/$/, "")}#${index === 0 ? "" : chunk.split("\n")[0]?.trim().toLowerCase().replace(/\s+/g, "-").replace(/\./g, "")}`,
};
});
return chunks;
})
);
const flat = results.flat();
const string = `/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
export const documentationData = ${JSON.stringify({ documents: flat }, null, 2)};
`;
await fs.writeFile(
path.resolve(process.cwd(), "src", "data", "docs.ts"),
string
);
await fs.rm(docsDirectory, { recursive: true, force: true });
+1 -4
View File
@@ -19,17 +19,14 @@
"@anthropic-ai/sdk": "0.56.0",
"@atproto/api": "0.15.26",
"@fastify/cors": "11.0.1",
"@modelcontextprotocol/sdk": "1.17.1",
"@nhcarrigan/logger": "1.0.0",
"@prisma/client": "6.11.1",
"fastify": "5.4.0",
"fastify-mcp-server": "0.4.1",
"gray-matter": "4.0.3",
"twitter-api-v2": "1.24.0"
},
"devDependencies": {
"@types/node": "24.0.10",
"prisma": "6.11.1",
"tsx": "4.20.3"
"prisma": "6.11.1"
}
}
-45
View File
@@ -5,11 +5,7 @@
*/
import cors from "@fastify/cors";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import fastify from "fastify";
// eslint-disable-next-line @typescript-eslint/naming-convention -- 'Tis a class.
import FastifyMcpServer, { getMcpDecorator } from "fastify-mcp-server";
import { documentationData } from "./data/docs.js";
import { corsHook } from "./hooks/cors.js";
import { ipHook } from "./hooks/ips.js";
import { announcementRoutes } from "./routes/announcement.js";
@@ -37,47 +33,6 @@ server.addHook("preHandler", ipHook);
server.register(baseRoutes);
server.register(announcementRoutes);
const mcp = new McpServer({
name: "nhcarrigan-mcp",
version: process.env.npm_package_version ?? "0.0.0",
});
// Define MCP tools
mcp.tool("docs", () => {
return {
content: documentationData.documents.map((document) => {
return {
text: JSON.stringify(document, null, 2),
type: "text",
};
}),
};
});
await server.register(FastifyMcpServer, {
endpoint: "/mcp",
server: mcp.server,
});
const mcpServer = getMcpDecorator(server);
const sessionManager = mcpServer.getSessionManager();
// Session created
sessionManager.on("sessionCreated", (sessionId: string) => {
void logger.log("debug", `New MCP session: ${sessionId}`);
});
// Session destroyed
sessionManager.on("sessionDestroyed", (sessionId: string) => {
void logger.log("debug", `MCP session ended: ${sessionId}`);
});
// Transport errors
sessionManager.on("transportError", (sessionId: string, error: Error) => {
void logger.error(`Error in session ${sessionId}:`, error);
});
server.listen({ port: 20_000 }, (error) => {
if (error) {
void logger.error("instantiate server", error);
+1 -1
View File
@@ -4,5 +4,5 @@
"rootDir": "./src",
"outDir": "./prod",
},
"exclude": ["./getDocs.ts"]
"exclude": ["../bot/getDocs.ts"]
}