[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:
parent
9084cd3a96
commit
7b2ff6b064
9 changed files with 168 additions and 62 deletions
|
|
@ -29,13 +29,13 @@ src/everything
|
||||||
├── prompts
|
├── prompts
|
||||||
│ ├── index.ts
|
│ ├── index.ts
|
||||||
│ ├── simple.ts
|
│ ├── simple.ts
|
||||||
│ ├── complex.ts
|
│ ├── args.ts
|
||||||
│ ├── completions.ts
|
│ ├── completions.ts
|
||||||
│ └── resource.ts
|
│ └── resource.ts
|
||||||
├── resources
|
├── resources
|
||||||
│ ├── index.ts
|
│ ├── index.ts
|
||||||
│ ├── template.ts
|
│ ├── templates.ts
|
||||||
│ └── static.ts
|
│ └── files.ts
|
||||||
├── docs
|
├── docs
|
||||||
│ ├── server-instructions.md
|
│ ├── server-instructions.md
|
||||||
│ └── architecture.md
|
│ └── architecture.md
|
||||||
|
|
@ -84,26 +84,26 @@ At `src/everything`:
|
||||||
- `registerPrompts(server)` orchestrator; delegates to individual prompt registrations.
|
- `registerPrompts(server)` orchestrator; delegates to individual prompt registrations.
|
||||||
- simple.ts
|
- simple.ts
|
||||||
- Registers `simple-prompt`: a prompt with no arguments that returns a single user message.
|
- Registers `simple-prompt`: a prompt with no arguments that returns a single user message.
|
||||||
- complex.ts
|
- args.ts
|
||||||
- Registers `complex-prompt`: a prompt with two arguments (`city` required, `state` optional) used to compose a message.
|
- Registers `args-prompt`: a prompt with two arguments (`city` required, `state` optional) used to compose a message.
|
||||||
- completions.ts
|
- completions.ts
|
||||||
- Registers `completable-prompt`: a prompt whose arguments support server-driven completions using the SDK’s `completable(...)` helper (e.g., completing `department` and context-aware `name`).
|
- Registers `completable-prompt`: a prompt whose arguments support server-driven completions using the SDK’s `completable(...)` helper (e.g., completing `department` and context-aware `name`).
|
||||||
- resource.ts
|
- resource.ts
|
||||||
- Exposes `registerEmbeddedResourcePrompt(server)` which registers `resource-prompt` — a prompt that accepts `resourceId` and embeds a dynamically generated text resource within the returned messages. Internally reuses helpers from `resources/template.ts`.
|
- Exposes `registerEmbeddedResourcePrompt(server)` which registers `resource-prompt` — a prompt that accepts `resourceType` ("Text" or "Blob") and `resourceId` (integer), and embeds a dynamically generated resource of the requested type within the returned messages. Internally reuses helpers from `resources/templates.ts`.
|
||||||
|
|
||||||
- resources/
|
- resources/
|
||||||
|
|
||||||
- index.ts
|
- index.ts
|
||||||
- `registerResources(server)` orchestrator; delegates to template‑based dynamic resources and static resources by calling `registerResourceTemplates(server)` and `registerStaticResources(server)`.
|
- `registerResources(server)` orchestrator; delegates to template‑based dynamic resources and static file-based resources by calling `registerResourceTemplates(server)` and `registerFileResources(server)`.
|
||||||
- template.ts
|
- templates.ts
|
||||||
- Registers two dynamic, template‑driven resources using `ResourceTemplate`:
|
- Registers two dynamic, template‑driven resources using `ResourceTemplate`:
|
||||||
- Text: `demo://resource/dynamic/text/{index}` (MIME: `text/plain`)
|
- Text: `demo://resource/dynamic/text/{index}` (MIME: `text/plain`)
|
||||||
- Blob: `demo://resource/dynamic/blob/{index}` (MIME: `application/octet-stream`, Base64 payload)
|
- Blob: `demo://resource/dynamic/blob/{index}` (MIME: `application/octet-stream`, Base64 payload)
|
||||||
- The `{index}` path variable must be a finite integer. Content is generated on demand with a timestamp.
|
- The `{index}` path variable must be a finite integer. Content is generated on demand with a timestamp.
|
||||||
- Exposes helpers `textResource(uri, index)` and `textResourceUri(index)` so other modules can construct and embed text resources directly (e.g., from prompts).
|
- Exposes helpers `textResource(uri, index)`, `textResourceUri(index)`, `blobResource(uri, index)`, and `blobResourceUri(index)` so other modules can construct and embed dynamic resources directly (e.g., from prompts).
|
||||||
- static.ts
|
- files.ts
|
||||||
- Registers static resources for each file in the `docs/` folder.
|
- Registers static file-based resources for each file in the `docs/` folder.
|
||||||
- URIs follow the pattern: `demo://static/docs/<filename>`.
|
- URIs follow the pattern: `demo://resource/static/document/<filename>`.
|
||||||
- Serves markdown files as `text/markdown`, `.txt` as `text/plain`, `.json` as `application/json`, others default to `text/plain`.
|
- Serves markdown files as `text/markdown`, `.txt` as `text/plain`, `.json` as `application/json`, others default to `text/plain`.
|
||||||
|
|
||||||
- docs/
|
- docs/
|
||||||
|
|
@ -159,14 +159,14 @@ At `src/everything`:
|
||||||
- Prompts
|
- Prompts
|
||||||
|
|
||||||
- `simple-prompt` (prompts/simple.ts): No-argument prompt that returns a static user message.
|
- `simple-prompt` (prompts/simple.ts): No-argument prompt that returns a static user message.
|
||||||
- `complex-prompt` (prompts/complex.ts): Two-argument prompt with `city` (required) and `state` (optional) used to compose a question.
|
- `args-prompt` (prompts/args.ts): Two-argument prompt with `city` (required) and `state` (optional) used to compose a question.
|
||||||
- `completable-prompt` (prompts/completions.ts): Demonstrates argument auto-completions with the SDK’s `completable` helper; `department` completions drive context-aware `name` suggestions.
|
- `completable-prompt` (prompts/completions.ts): Demonstrates argument auto-completions with the SDK’s `completable` helper; `department` completions drive context-aware `name` suggestions.
|
||||||
- `resource-prompt` (prompts/resource.ts): Accepts `resourceId` (string convertible to integer) and returns messages that include an embedded dynamic text resource generated via `resources/template.ts`.
|
- `resource-prompt` (prompts/resource.ts): Accepts `resourceType` ("Text" or "Blob") and `resourceId` (string convertible to integer) and returns messages that include an embedded dynamic resource of the selected type generated via `resources/templates.ts`.
|
||||||
|
|
||||||
- Resources
|
- Resources
|
||||||
- Dynamic Text: `demo://resource/dynamic/text/{index}` (content generated on the fly)
|
- Dynamic Text: `demo://resource/dynamic/text/{index}` (content generated on the fly)
|
||||||
- Dynamic Blob: `demo://resource/dynamic/blob/{index}` (base64 payload generated on the fly)
|
- Dynamic Blob: `demo://resource/dynamic/blob/{index}` (base64 payload generated on the fly)
|
||||||
- Static Docs: `demo://static/docs/<filename>` (serves files from `src/everything/docs/` as static resources)
|
- Static Docs: `demo://resource/static/document/<filename>` (serves files from `src/everything/docs/` as static file-based resources)
|
||||||
|
|
||||||
## Extension Points
|
## Extension Points
|
||||||
|
|
||||||
|
|
|
||||||
41
src/everything/prompts/args.ts
Normal file
41
src/everything/prompts/args.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
import { z } from "zod";
|
||||||
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a prompt with arguments
|
||||||
|
* - Two arguments, one required and one optional
|
||||||
|
* - Combines argument values in the returned prompt
|
||||||
|
*
|
||||||
|
* @param server
|
||||||
|
*/
|
||||||
|
export const registerArgumentsPrompt = (server: McpServer) => {
|
||||||
|
// Prompt arguments
|
||||||
|
const promptArgsSchema = {
|
||||||
|
city: z.string().describe("Name of the city"),
|
||||||
|
state: z.string().describe("Name of the state").optional(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Register the prompt
|
||||||
|
server.registerPrompt(
|
||||||
|
"args-prompt",
|
||||||
|
{
|
||||||
|
title: "Arguments Prompt",
|
||||||
|
description: "A prompt with two arguments, one required and one optional",
|
||||||
|
argsSchema: promptArgsSchema,
|
||||||
|
},
|
||||||
|
(args) => {
|
||||||
|
const location = `${args?.city}${args?.state ? `, ${args?.state}` : ""}`;
|
||||||
|
return {
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: {
|
||||||
|
type: "text",
|
||||||
|
text: `What's weather in ${location}?`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
@ -2,36 +2,54 @@ import { z } from "zod";
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
import { completable } from "@modelcontextprotocol/sdk/server/completable.js";
|
import { completable } from "@modelcontextprotocol/sdk/server/completable.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a prompt with completable arguments
|
||||||
|
* - Two required arguments, both with completion handlers
|
||||||
|
* - First argument value will be included in context for second argument
|
||||||
|
* - Allows second argument to depend on the first argument value
|
||||||
|
*
|
||||||
|
* @param server
|
||||||
|
*/
|
||||||
export const registerPromptWithCompletions = (server: McpServer) => {
|
export const registerPromptWithCompletions = (server: McpServer) => {
|
||||||
|
// Prompt arguments
|
||||||
const promptArgsSchema = {
|
const promptArgsSchema = {
|
||||||
department: completable(z.string(), (value) => {
|
department: completable(
|
||||||
return ["Engineering", "Sales", "Marketing", "Support"].filter((d) =>
|
z.string().describe("Choose the department."),
|
||||||
d.startsWith(value)
|
(value) => {
|
||||||
);
|
return ["Engineering", "Sales", "Marketing", "Support"].filter((d) =>
|
||||||
}),
|
d.startsWith(value)
|
||||||
name: completable(z.string(), (value, context) => {
|
);
|
||||||
const department = context?.arguments?.["department"];
|
|
||||||
if (department === "Engineering") {
|
|
||||||
return ["Alice", "Bob", "Charlie"].filter((n) => n.startsWith(value));
|
|
||||||
} else if (department === "Sales") {
|
|
||||||
return ["David", "Eve", "Frank"].filter((n) => n.startsWith(value));
|
|
||||||
} else if (department === "Marketing") {
|
|
||||||
return ["Grace", "Henry", "Iris"].filter((n) => n.startsWith(value));
|
|
||||||
} else if (department === "Support") {
|
|
||||||
return ["John", "Kim", "Lee"].filter((n) => n.startsWith(value));
|
|
||||||
}
|
}
|
||||||
return [];
|
),
|
||||||
}),
|
name: completable(
|
||||||
|
z
|
||||||
|
.string()
|
||||||
|
.describe("Choose a team member to lead the selected department."),
|
||||||
|
(value, context) => {
|
||||||
|
const department = context?.arguments?.["department"];
|
||||||
|
if (department === "Engineering") {
|
||||||
|
return ["Alice", "Bob", "Charlie"].filter((n) => n.startsWith(value));
|
||||||
|
} else if (department === "Sales") {
|
||||||
|
return ["David", "Eve", "Frank"].filter((n) => n.startsWith(value));
|
||||||
|
} else if (department === "Marketing") {
|
||||||
|
return ["Grace", "Henry", "Iris"].filter((n) => n.startsWith(value));
|
||||||
|
} else if (department === "Support") {
|
||||||
|
return ["John", "Kim", "Lee"].filter((n) => n.startsWith(value));
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Register the prompt
|
||||||
server.registerPrompt(
|
server.registerPrompt(
|
||||||
"completable-prompt",
|
"completable-prompt",
|
||||||
{
|
{
|
||||||
title: "Team Management",
|
title: "Team Management",
|
||||||
description: "Choose a team member to lead their specific department.",
|
description: "First argument choice narrows values for second argument.",
|
||||||
argsSchema: promptArgsSchema,
|
argsSchema: promptArgsSchema,
|
||||||
},
|
},
|
||||||
async ({ department, name }) => ({
|
({ department, name }) => ({
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: "user",
|
role: "user",
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,17 @@
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
import { registerSimplePrompt } from "./simple.js";
|
import { registerSimplePrompt } from "./simple.js";
|
||||||
import { registerComplexPrompt } from "./complex.js";
|
import { registerArgumentsPrompt } from "./args.js";
|
||||||
import { registerPromptWithCompletions } from "./completions.js";
|
import { registerPromptWithCompletions } from "./completions.js";
|
||||||
import { registerEmbeddedResourcePrompt } from "./resource.js";
|
import { registerEmbeddedResourcePrompt } from "./resource.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the prompts with the MCP server.
|
* Register the prompts with the MCP server.
|
||||||
|
*
|
||||||
* @param server
|
* @param server
|
||||||
*/
|
*/
|
||||||
export const registerPrompts = (server: McpServer) => {
|
export const registerPrompts = (server: McpServer) => {
|
||||||
registerSimplePrompt(server);
|
registerSimplePrompt(server);
|
||||||
registerComplexPrompt(server);
|
registerArgumentsPrompt(server);
|
||||||
registerPromptWithCompletions(server);
|
registerPromptWithCompletions(server);
|
||||||
registerEmbeddedResourcePrompt(server);
|
registerEmbeddedResourcePrompt(server);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,47 @@
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
import { textResource, textResourceUri } from "../resources/template.js";
|
import { completable } from "@modelcontextprotocol/sdk/server/completable.js";
|
||||||
|
import {
|
||||||
|
textResource,
|
||||||
|
textResourceUri,
|
||||||
|
blobResourceUri,
|
||||||
|
blobResource,
|
||||||
|
} from "../resources/templates.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a prompt with an embedded resource reference
|
||||||
|
* - Takes a resource type and id
|
||||||
|
* - Returns the corresponding dynamically created resource
|
||||||
|
*
|
||||||
|
* @param server
|
||||||
|
*/
|
||||||
export const registerEmbeddedResourcePrompt = (server: McpServer) => {
|
export const registerEmbeddedResourcePrompt = (server: McpServer) => {
|
||||||
// NOTE: Currently, prompt arguments can only be strings since type is not field of PromptArgument
|
// Resource types
|
||||||
// Consequently, we must define it as a string and convert the argument to number before using it
|
const BLOB_TYPE = "Blob";
|
||||||
// https://modelcontextprotocol.io/specification/2025-11-25/schema#promptargument
|
const TEXT_TYPE = "Text";
|
||||||
|
const resourceTypes = [BLOB_TYPE, TEXT_TYPE];
|
||||||
|
|
||||||
|
// Prompt arguments
|
||||||
const promptArgsSchema = {
|
const promptArgsSchema = {
|
||||||
resourceId: z.string().describe("ID of the text resource to fetch"),
|
resourceType: completable(
|
||||||
|
z.string().describe("Type of resource to fetch"),
|
||||||
|
(value: string) => {
|
||||||
|
return [TEXT_TYPE, BLOB_TYPE].filter((t) => t.startsWith(value));
|
||||||
|
}
|
||||||
|
),
|
||||||
|
// NOTE: Currently, prompt arguments can only be strings since type is not field of PromptArgument
|
||||||
|
// Consequently, we must define it as a string and convert the argument to number before using it
|
||||||
|
// https://modelcontextprotocol.io/specification/2025-11-25/schema#promptargument
|
||||||
|
resourceId: completable(
|
||||||
|
z.string().describe("ID of the text resource to fetch"),
|
||||||
|
(value: string) => {
|
||||||
|
const resourceId = Number(value);
|
||||||
|
return Number.isInteger(resourceId) ? [value] : [];
|
||||||
|
}
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Register the prompt
|
||||||
server.registerPrompt(
|
server.registerPrompt(
|
||||||
"resource-prompt",
|
"resource-prompt",
|
||||||
{
|
{
|
||||||
|
|
@ -18,6 +50,15 @@ export const registerEmbeddedResourcePrompt = (server: McpServer) => {
|
||||||
argsSchema: promptArgsSchema,
|
argsSchema: promptArgsSchema,
|
||||||
},
|
},
|
||||||
(args) => {
|
(args) => {
|
||||||
|
// Validate resource type argument
|
||||||
|
const { resourceType } = args;
|
||||||
|
if (!resourceTypes.includes(resourceType)) {
|
||||||
|
throw new Error(
|
||||||
|
`Invalid resourceType: ${args?.resourceType}. Must be ${TEXT_TYPE} or ${BLOB_TYPE}.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate resourceId argument
|
||||||
const resourceId = Number(args?.resourceId);
|
const resourceId = Number(args?.resourceId);
|
||||||
if (!Number.isFinite(resourceId) || !Number.isInteger(resourceId)) {
|
if (!Number.isFinite(resourceId) || !Number.isInteger(resourceId)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
|
@ -25,8 +66,15 @@ export const registerEmbeddedResourcePrompt = (server: McpServer) => {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const uri = textResourceUri(resourceId);
|
// Get resource based on the resource type
|
||||||
const resource = textResource(uri, resourceId);
|
const uri =
|
||||||
|
resourceType === TEXT_TYPE
|
||||||
|
? textResourceUri(resourceId)
|
||||||
|
: blobResourceUri(resourceId);
|
||||||
|
const resource =
|
||||||
|
resourceType === TEXT_TYPE
|
||||||
|
? textResource(uri, resourceId)
|
||||||
|
: blobResource(uri, resourceId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
messages: [
|
messages: [
|
||||||
|
|
@ -34,7 +82,7 @@ export const registerEmbeddedResourcePrompt = (server: McpServer) => {
|
||||||
role: "user",
|
role: "user",
|
||||||
content: {
|
content: {
|
||||||
type: "text",
|
type: "text",
|
||||||
text: `This prompt includes the text resource with id: ${resourceId}. Please analyze the following resource:`,
|
text: `This prompt includes the ${resourceType} resource with id: ${resourceId}. Please analyze the following resource:`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,13 @@
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a simple prompt with no arguments
|
||||||
|
* - Returns the fixed text of the prompt with no modifications
|
||||||
|
*
|
||||||
|
* @param server
|
||||||
|
*/
|
||||||
export const registerSimplePrompt = (server: McpServer) => {
|
export const registerSimplePrompt = (server: McpServer) => {
|
||||||
|
// Register the prompt
|
||||||
server.registerPrompt(
|
server.registerPrompt(
|
||||||
"simple-prompt",
|
"simple-prompt",
|
||||||
{
|
{
|
||||||
|
|
|
||||||
90
src/everything/resources/files.ts
Normal file
90
src/everything/resources/files.ts
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
import { dirname, join } from "path";
|
||||||
|
import { fileURLToPath } from "url";
|
||||||
|
import { readdirSync, readFileSync, statSync } from "fs";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 (.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 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
|
||||||
|
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);
|
||||||
|
if (!st.isFile()) continue;
|
||||||
|
} catch {
|
||||||
|
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,
|
||||||
|
{ mimeType, description },
|
||||||
|
async (uri) => {
|
||||||
|
const text = readFileSafe(fullPath);
|
||||||
|
return {
|
||||||
|
contents: [
|
||||||
|
{
|
||||||
|
uri: uri.toString(),
|
||||||
|
mimeType,
|
||||||
|
text,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the mimetype based on filename
|
||||||
|
* @param fileName
|
||||||
|
*/
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a file or return an error message if it fails
|
||||||
|
* @param path
|
||||||
|
*/
|
||||||
|
function readFileSafe(path: string): string {
|
||||||
|
try {
|
||||||
|
return readFileSync(path, "utf-8");
|
||||||
|
} catch (e) {
|
||||||
|
return `Error reading file: ${path}. ${e}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
import { registerResourceTemplates } from "./template.js";
|
import { registerResourceTemplates } from "./templates.js";
|
||||||
import { registerStaticResources } from "./static.js";
|
import { registerFileResources } from "./files.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the resources with the MCP server.
|
* Register the resources with the MCP server.
|
||||||
|
|
@ -8,5 +8,5 @@ import { registerStaticResources } from "./static.js";
|
||||||
*/
|
*/
|
||||||
export const registerResources = (server: McpServer) => {
|
export const registerResources = (server: McpServer) => {
|
||||||
registerResourceTemplates(server);
|
registerResourceTemplates(server);
|
||||||
registerStaticResources(server);
|
registerFileResources(server);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
128
src/everything/resources/templates.ts
Normal file
128
src/everything/resources/templates.ts
Normal file
|
|
@ -0,0 +1,128 @@
|
||||||
|
import {
|
||||||
|
McpServer,
|
||||||
|
ResourceTemplate,
|
||||||
|
} from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
|
||||||
|
const uriBase: string = "demo://resource/dynamic";
|
||||||
|
const textUriBase: string = `${uriBase}/text`;
|
||||||
|
const blobUriBase: string = `${uriBase}/blob`;
|
||||||
|
const textUriTemplate: string = `${textUriBase}/{index}`;
|
||||||
|
const blobUriTemplate: string = `${blobUriBase}/{index}`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a dynamic text resource
|
||||||
|
* - Exposed for use by embedded resource prompt example
|
||||||
|
* @param uri
|
||||||
|
* @param index
|
||||||
|
*/
|
||||||
|
export const textResource = (uri: URL, index: number) => {
|
||||||
|
const timestamp = new Date().toLocaleTimeString();
|
||||||
|
return {
|
||||||
|
uri: uri.toString(),
|
||||||
|
mimeType: "text/plain",
|
||||||
|
text: `Resource ${index}: This is a plaintext resource created at ${timestamp}`,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a dynamic blob resource
|
||||||
|
* - Exposed for use by embedded resource prompt example
|
||||||
|
* @param uri
|
||||||
|
* @param index
|
||||||
|
*/
|
||||||
|
export const blobResource = (uri: URL, index: number) => {
|
||||||
|
const timestamp = new Date().toLocaleTimeString();
|
||||||
|
const resourceText = Buffer.from(
|
||||||
|
`Resource ${index}: This is a base64 blob created at ${timestamp}`
|
||||||
|
).toString("base64");
|
||||||
|
return {
|
||||||
|
uri: uri.toString(),
|
||||||
|
mimeType: "text/plain",
|
||||||
|
text: resourceText,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
* - These are only accessible via template URIs
|
||||||
|
* - Both blob and text resources:
|
||||||
|
* - have content that is dynamically generated, including a timestamp
|
||||||
|
* - have different template URIs
|
||||||
|
* - Blob: "demo://resource/dynamic/blob/{index}"
|
||||||
|
* - Text: "demo://resource/dynamic/text/{index}"
|
||||||
|
*
|
||||||
|
* @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 (
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Register the text resource template
|
||||||
|
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: [textResource(uri, index)],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Register the blob resource template
|
||||||
|
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);
|
||||||
|
return {
|
||||||
|
contents: [blobResource(uri, index)],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue