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

* Updated architecture.md

* Refactor/renamed static.ts to file.ts

* Refactor/renamed complex.ts to args.ts

* Refactor/renamed template.ts to templates.ts.

* In resource.ts,
  -  improved registerEmbeddedResourcePrompt to allow selection of blob or text resource type.

* In file.ts
  - refactor/renamed registerStaticResources to registerFileResources to highlight the fact that it is using files as resources.

* In args.ts
  - refactor/renamed registerComplexPrompt to registerArgumentsPrompt to highlight the fact that it is demonstrating prompt arguments.

* Updated inline documentation throughout
This commit is contained in:
cliffhall
2025-12-06 15:48:39 -05:00
parent 9084cd3a96
commit 7b2ff6b064
9 changed files with 168 additions and 62 deletions

View File

@@ -3,30 +3,32 @@ 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.
*
* Register static file resources
* - Each file in src/everything/docs is exposed as an individual static resource
* - URIs follow the pattern: "demo://static/docs/<filename>"
* - Markdown files are served as text/markdown; others as text/plain
* - Markdown (.md) files are served as mime type "text/markdown"
* - Text (.txt) files are served as mime type "text/plain"
* - JSON (.json) files are served as mime type "application/json"
*
* @param server
*/
export const registerStaticResources = (server: McpServer) => {
export const registerFileResources = (server: McpServer) => {
// Read the entries in the docs directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const docsDir = join(__dirname, "..", "docs");
let entries: string[] = [];
try {
entries = readdirSync(docsDir);
} catch (e) {
// If docs folder is missing or unreadable, just skip registration
// If docs/ folder is missing or unreadable, just skip registration
return;
}
// Register each file as a static resource
for (const name of entries) {
// Only process files, not directories
const fullPath = join(docsDir, name);
try {
const st = statSync(fullPath);
@@ -35,11 +37,13 @@ export const registerStaticResources = (server: McpServer) => {
continue;
}
// Prepare file resource info
const uri = `demo://resource/static/document/${encodeURIComponent(name)}`;
const mimeType = getMimeType(name);
const displayName = `Docs: ${name}`;
const description = `Static document file exposed from /docs: ${name}`;
// Register file resource
server.registerResource(
displayName,
uri,
@@ -60,6 +64,10 @@ export const registerStaticResources = (server: McpServer) => {
}
};
/**
* Get the mimetype based on filename
* @param fileName
*/
function getMimeType(fileName: string): string {
const lower = fileName.toLowerCase();
if (lower.endsWith(".md") || lower.endsWith(".markdown"))
@@ -69,6 +77,10 @@ function getMimeType(fileName: string): string {
return "text/plain";
}
/**
* Read a file or return an error message if it fails
* @param path
*/
function readFileSafe(path: string): string {
try {
return readFileSync(path, "utf-8");

View File

@@ -1,6 +1,6 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { registerResourceTemplates } from "./template.js";
import { registerStaticResources } from "./static.js";
import { registerResourceTemplates } from "./templates.js";
import { registerFileResources } from "./files.js";
/**
* Register the resources with the MCP server.
@@ -8,5 +8,5 @@ import { registerStaticResources } from "./static.js";
*/
export const registerResources = (server: McpServer) => {
registerResourceTemplates(server);
registerStaticResources(server);
registerFileResources(server);
};

View File

@@ -11,6 +11,7 @@ const blobUriTemplate: string = `${blobUriBase}/{index}`;
/**
* Create a dynamic text resource
* - Exposed for use by embedded resource prompt example
* @param uri
* @param index
*/
@@ -25,6 +26,7 @@ export const textResource = (uri: URL, index: number) => {
/**
* Create a dynamic blob resource
* - Exposed for use by embedded resource prompt example
* @param uri
* @param index
*/
@@ -42,14 +44,22 @@ export const blobResource = (uri: URL, index: number) => {
/**
* Create a dynamic text resource URI
* - Exposed for use by embedded resource prompt example
* @param index
*/
export const textResourceUri = (index: number) =>
new URL(`${textUriBase}/${index}`);
/**
* Create a dynamic blob resource URI
* - Exposed for use by embedded resource prompt example
* @param index
*/
export const blobResourceUri = (index: number) =>
new URL(`${blobUriBase}/${index}`);
/**
* Register resource templates with the MCP server.
*
* - Text and blob resources, dynamically generated from the URI {index} variable
* - Any finite integer is acceptable for the index variable
* - List resources method will not return these resources
@@ -63,6 +73,7 @@ export const textResourceUri = (index: number) =>
* @param server
*/
export const registerResourceTemplates = (server: McpServer) => {
// Parse the index from the URI
const parseIndex = (uri: URL, variables: Record<string, unknown>) => {
const uriError = `Unknown resource: ${uri.toString()}`;
if (
@@ -81,7 +92,7 @@ export const registerResourceTemplates = (server: McpServer) => {
}
};
// Text resource template registration
// Register the text resource template
server.registerResource(
"Dynamic Text Resource",
new ResourceTemplate(textUriTemplate, { list: undefined }),
@@ -98,7 +109,7 @@ export const registerResourceTemplates = (server: McpServer) => {
}
);
// Blob resource template registration
// Register the blob resource template
server.registerResource(
"Dynamic Blob Resource",
new ResourceTemplate(blobUriTemplate, { list: undefined }),