[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
1 changed files with 168 additions and 62 deletions
|
|
@ -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:`,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue