[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

@@ -1,16 +1,25 @@
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
export const registerComplexPrompt = (server: McpServer) => {
/**
* 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(
"complex-prompt",
"args-prompt",
{
title: "Complex Prompt",
title: "Arguments Prompt",
description: "A prompt with two arguments, one required and one optional",
argsSchema: promptArgsSchema,
},

View File

@@ -2,36 +2,54 @@ import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.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) => {
// Prompt arguments
const promptArgsSchema = {
department: completable(z.string(), (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));
department: completable(
z.string().describe("Choose the department."),
(value) => {
return ["Engineering", "Sales", "Marketing", "Support"].filter((d) =>
d.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(
"completable-prompt",
{
title: "Team Management",
description: "Choose a team member to lead their specific department.",
description: "First argument choice narrows values for second argument.",
argsSchema: promptArgsSchema,
},
async ({ department, name }) => ({
({ department, name }) => ({
messages: [
{
role: "user",

View File

@@ -1,16 +1,17 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { registerSimplePrompt } from "./simple.js";
import { registerComplexPrompt } from "./complex.js";
import { registerArgumentsPrompt } from "./args.js";
import { registerPromptWithCompletions } from "./completions.js";
import { registerEmbeddedResourcePrompt } from "./resource.js";
/**
* Register the prompts with the MCP server.
*
* @param server
*/
export const registerPrompts = (server: McpServer) => {
registerSimplePrompt(server);
registerComplexPrompt(server);
registerArgumentsPrompt(server);
registerPromptWithCompletions(server);
registerEmbeddedResourcePrompt(server);
};

View File

@@ -1,15 +1,47 @@
import { z } from "zod";
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) => {
// 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
// Resource types
const BLOB_TYPE = "Blob";
const TEXT_TYPE = "Text";
const resourceTypes = [BLOB_TYPE, TEXT_TYPE];
// Prompt arguments
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(
"resource-prompt",
{
@@ -18,6 +50,15 @@ export const registerEmbeddedResourcePrompt = (server: McpServer) => {
argsSchema: promptArgsSchema,
},
(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);
if (!Number.isFinite(resourceId) || !Number.isInteger(resourceId)) {
throw new Error(
@@ -25,8 +66,15 @@ export const registerEmbeddedResourcePrompt = (server: McpServer) => {
);
}
const uri = textResourceUri(resourceId);
const resource = textResource(uri, resourceId);
// Get resource based on the resource type
const uri =
resourceType === TEXT_TYPE
? textResourceUri(resourceId)
: blobResourceUri(resourceId);
const resource =
resourceType === TEXT_TYPE
? textResource(uri, resourceId)
: blobResource(uri, resourceId);
return {
messages: [
@@ -34,7 +82,7 @@ export const registerEmbeddedResourcePrompt = (server: McpServer) => {
role: "user",
content: {
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:`,
},
},
{

View File

@@ -1,6 +1,13 @@
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) => {
// Register the prompt
server.registerPrompt(
"simple-prompt",
{