[WIP] Refactor everything server to be more modular and use recommended APIs.

* Adding static resources, move server instructions to
the new docs folder, and add code formatting

* Add docs folder

* Add docs/architecture.md which describes the architecture of the project thus far.

* Refactor moved instructions.md to docs/server-instructions.md

* Add resources/static.ts
  - in addStaticResources()
    - read the file entries from the docs folder
    - register each file as a resource (no template), with a readResource function that reads the file and returns it in a contents block with the appropriate mime type and contents
  - getMimeType helper function gets the mime type for a filename
  - readSafe helper function reads the file synchronously as utf-8 or returns an error string

* Add resources/index.ts
  - import addStaticResources
  - export registerResources function
  - in registerResources()
    - call addStaticResources

* In package.json
  - add prettier devDependency
  - add prettier:check script
  - add prettier:fix script
  - in build script, copy docs folder to dist

* All other changes were prettier formatting
This commit is contained in:
cliffhall
2025-12-05 13:26:08 -05:00
parent 8845118d61
commit 1c64b36c78
17 changed files with 904 additions and 527 deletions

View File

@@ -1,4 +1,7 @@
import {McpServer, ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
import {
McpServer,
ResourceTemplate,
} from "@modelcontextprotocol/sdk/server/mcp.js";
/**
* Register dynamic resources with the MCP server.
@@ -16,84 +19,104 @@ import {McpServer, ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.
* @param server
*/
export const addDynamicResources = (server: McpServer) => {
const uriBase: string = "test://dynamic/resource";
const textUri: string = `${uriBase}/text/{index}`
const blobUri: string = `${uriBase}/blob/{index}`
const uriBase: string = "test://dynamic/resource";
const textUriBase: string = `${uriBase}/text`;
const blobUriBase: string = `${uriBase}/blob`;
const textUriTemplate: string = `${textUriBase}/{index}`;
const blobUriTemplate: string = `${blobUriBase}/{index}`;
// Format a GMT timestamp like "7:30AM GMT on November 3"
const formatGmtTimestamp = () => {
const d = new Date();
const h24 = d.getUTCHours();
const minutes = d.getUTCMinutes();
const ampm = h24 >= 12 ? "PM" : "AM";
let h12 = h24 % 12;
if (h12 === 0) h12 = 12;
const mm = String(minutes).padStart(2, "0");
const months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const monthName = months[d.getUTCMonth()];
const day = d.getUTCDate();
return `${h12}:${mm}${ampm} GMT on ${monthName} ${day}`;
};
// Format a GMT timestamp like "7:30AM GMT on November 3"
const formatGmtTimestamp = () => {
const d = new Date();
const h24 = d.getUTCHours();
const minutes = d.getUTCMinutes();
const ampm = h24 >= 12 ? "PM" : "AM";
let h12 = h24 % 12;
if (h12 === 0) h12 = 12;
const mm = String(minutes).padStart(2, "0");
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const monthName = months[d.getUTCMonth()];
const day = d.getUTCDate();
return `${h12}:${mm}${ampm} GMT on ${monthName} ${day}`;
};
const parseIndex = (uri: URL, variables: Record<string, unknown>) => {
const uriError = `Unknown resource: ${uri}`;
if (uri.toString() !== textUri && uri.toString() !== blobUri) {
throw new Error(uriError);
}
const idxStr = String((variables as any).index ?? "");
const idx = Number(idxStr);
if (Number.isFinite(idx) && Number.isInteger(idx)) {
return idx;
} else {
throw new Error(uriError);
}
};
const parseIndex = (uri: URL, variables: Record<string, unknown>) => {
const uriError = `Unknown resource: ${uri.toString()}`;
if (
uri.toString().startsWith(textUriBase) &&
uri.toString().startsWith(blobUriBase)
) {
throw new Error(uriError);
} else {
const idxStr = String((variables as any).index ?? "");
const idx = Number(idxStr);
if (Number.isFinite(idx) && Number.isInteger(idx)) {
return idx;
} else {
throw new Error(uriError);
}
}
};
// Text resource registration
server.registerResource(
"Dynamic Text Resource",
new ResourceTemplate(textUri, { list: undefined }),
{
// Text resource registration
server.registerResource(
"Dynamic Text Resource",
new ResourceTemplate(textUriTemplate, { list: undefined }),
{
mimeType: "text/plain",
description:
"Plaintext dynamic resource fabricated from the {index} variable, which must be an integer.",
},
async (uri, variables) => {
const index = parseIndex(uri, variables);
return {
contents: [
{
uri: uri.toString(),
mimeType: "text/plain",
description: "Plaintext dynamic resource fabricated from the {index} variable, which must be an integer.",
},
async (uri, variables) => {
const index = parseIndex(uri, variables);
return {
contents: [
{
uri: uri.toString(),
mimeType: "text/plain",
text: `Resource ${index}: This is a plaintext resource created at ${formatGmtTimestamp()}`,
},
],
};
}
);
text: `Resource ${index}: This is a plaintext resource created at ${formatGmtTimestamp()}`,
},
],
};
}
);
// Blob resource registration
server.registerResource(
"Dynamic Blob Resource",
new ResourceTemplate(blobUri, { list: undefined }),
{
// Blob resource registration
server.registerResource(
"Dynamic Blob Resource",
new ResourceTemplate(blobUriTemplate, { list: undefined }),
{
mimeType: "application/octet-stream",
description:
"Binary (base64) dynamic resource fabricated from the {index} variable, which must be an integer.",
},
async (uri, variables) => {
const index = parseIndex(uri, variables);
const buffer = Buffer.from(
`Resource ${index}: This is a base64 blob created at ${formatGmtTimestamp()}`
);
return {
contents: [
{
uri: uri.toString(),
mimeType: "application/octet-stream",
description: "Binary (base64) dynamic resource fabricated from the {index} variable, which must be an integer.",
},
async (uri, variables) => {
const index = parseIndex(uri, variables);
const buffer = Buffer.from(`Resource ${index}: This is a base64 blob created at ${formatGmtTimestamp()}`);
return {
contents: [
{
uri: uri.toString(),
mimeType: "application/octet-stream",
blob: buffer.toString("base64"),
},
],
};
}
);
blob: buffer.toString("base64"),
},
],
};
}
);
};

View File

@@ -1,11 +1,12 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { addDynamicResources } from "./dynamic.js";
import { addStaticResources } from "./static.js";
/**
* Register the resources with the MCP server.
* @param server
*/
export const registerResources = (server: McpServer) => {
addDynamicResources(server);
addDynamicResources(server);
addStaticResources(server);
};

View File

@@ -0,0 +1,78 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { readdirSync, readFileSync, statSync } from "fs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* Register static resources for each file in the docs folder.
*
* - Each file in src/everything/docs is exposed as an individual static resource
* - URIs follow the pattern: "test://static/docs/<filename>"
* - Markdown files are served as text/markdown; others as text/plain
*
* @param server
*/
export const addStaticResources = (server: McpServer) => {
const docsDir = join(__dirname, "..", "docs");
let entries: string[] = [];
try {
entries = readdirSync(docsDir);
} catch (e) {
// If docs folder is missing or unreadable, just skip registration
return;
}
for (const name of entries) {
const fullPath = join(docsDir, name);
try {
const st = statSync(fullPath);
if (!st.isFile()) continue;
} catch {
continue;
}
const uri = `test://static/docs/${encodeURIComponent(name)}`;
const mimeType = getMimeType(name);
const displayName = `Docs: ${name}`;
const description = `Static documentation file exposed from /docs: ${name}`;
server.registerResource(
displayName,
uri,
{ mimeType, description },
async (uri) => {
const text = readFileSafe(fullPath);
return {
contents: [
{
uri: uri.toString(),
mimeType,
text,
},
],
};
}
);
}
};
function getMimeType(fileName: string): string {
const lower = fileName.toLowerCase();
if (lower.endsWith(".md") || lower.endsWith(".markdown"))
return "text/markdown";
if (lower.endsWith(".txt")) return "text/plain";
if (lower.endsWith(".json")) return "application/json";
return "text/plain";
}
function readFileSafe(path: string): string {
try {
return readFileSync(path, "utf-8");
} catch (e) {
return `Error reading file: ${path}. ${e}`;
}
}