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

For tools where we seek to get some response from the server, renamed as an action, e.g., "Get Sum" rather than "Add" or "Get Annotated Message" rather than "Annotated Message", so that it's clear what the intent of the tool is in a quick review.

* Updated architecture.md

* Refactor/renamed add.ts to get-sum.ts
* Refactor/renamed annotated-message.ts to get-annotated-message.ts
* In tools/index.ts
  - sorted presentation order
This commit is contained in:
cliffhall 2025-12-10 16:08:41 -05:00
commit f759d9eaa1
3 changed files with 29 additions and 29 deletions

View file

@ -0,0 +1,89 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
import { MCP_TINY_IMAGE } from "./get-tiny-image.js";
// Tool input schema
const GetAnnotatedMessageSchema = z.object({
messageType: z
.enum(["error", "success", "debug"])
.describe("Type of message to demonstrate different annotation patterns"),
includeImage: z
.boolean()
.default(false)
.describe("Whether to include an example image"),
});
// Tool configuration
const name = "get-annotated-message";
const config = {
title: "Get Annotated Message Tool",
description:
"Demonstrates how annotations can be used to provide metadata about content.",
inputSchema: GetAnnotatedMessageSchema,
};
/**
* Registers the 'annotated-message' tool with the provided McpServer instance.
*
* The registered tool generates and sends messages with specific types, such as error,
* success, or debug, carrying associated annotations like priority level and intended
* audience.
*
* Optionally, it can include an annotated image in the response.
*
* @function
* @param {McpServer} server - The MCP server instance where the Annotated Message Tool is to be registered.
*/
export const registerGetAnnotatedMessageTool = (server: McpServer) => {
server.registerTool(name, config, async (args): Promise<CallToolResult> => {
const { messageType, includeImage } = GetAnnotatedMessageSchema.parse(args);
const content: CallToolResult["content"] = [];
// Main message with different priorities/audiences based on type
if (messageType === "error") {
content.push({
type: "text",
text: "Error: Operation failed",
annotations: {
priority: 1.0, // Errors are highest priority
audience: ["user", "assistant"], // Both need to know about errors
},
});
} else if (messageType === "success") {
content.push({
type: "text",
text: "Operation completed successfully",
annotations: {
priority: 0.7, // Success messages are important but not critical
audience: ["user"], // Success mainly for user consumption
},
});
} else if (messageType === "debug") {
content.push({
type: "text",
text: "Debug: Cache hit ratio 0.95, latency 150ms",
annotations: {
priority: 0.3, // Debug info is low priority
audience: ["assistant"], // Technical details for assistant
},
});
}
// Optional image with its own annotations
if (includeImage) {
content.push({
type: "image",
data: MCP_TINY_IMAGE,
mimeType: "image/png",
annotations: {
priority: 0.5,
audience: ["user"], // Images primarily for user visualization
},
});
}
return { content };
});
};

View file

@ -0,0 +1,45 @@
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
// Tool input schema
const GetSumSchema = z.object({
a: z.number().describe("First number"),
b: z.number().describe("Second number"),
});
// Tool configuration
const name = "get-sum";
const config = {
title: "Get Sum Tool",
description: "Gets the sum of two numbers",
inputSchema: GetSumSchema,
};
/**
* Registers the 'get-sum' tool with the provided McpServer instance.
**
* The registered tool processes input arguments, validates them using a predefined schema,
* calculates the sum of two numeric values, and returns the result in a content block.
*
* Expects input arguments to conform to a specific schema that includes two numeric properties, `a` and `b`.
* Validation is performed to ensure the input adheres to the expected structure before calculating the sum.
*
* The result is returned as a Promise resolving to an object containing the computed sum in a text format.
*
* @param {McpServer} server - The server instance where the sum tool will be registered.
*/
export const registerGetSumTool = (server: McpServer) => {
server.registerTool(name, config, async (args): Promise<CallToolResult> => {
const validatedArgs = GetSumSchema.parse(args);
const sum = validatedArgs.a + validatedArgs.b;
return {
content: [
{
type: "text",
text: `The sum of ${validatedArgs.a} and ${validatedArgs.b} is ${sum}.`,
},
],
};
});
};

View file

@ -1,29 +1,29 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { registerAddTool } from "./add.js";
import { registerAnnotatedMessageTool } from "./annotated-message.js";
import { registerGetAnnotatedMessageTool } from "./get-annotated-message.js";
import { registerEchoTool } from "./echo.js";
import { registerGetEnvTool } from "./get-env.js";
import { registerGetResourceLinksTool } from "./get-resource-links.js";
import { registerGetResourceReferenceTool } from "./get-resource-reference.js";
import { registerGetStructuredContentTool } from "./get-structured-content.js";
import { registerGetSumTool } from "./get-sum.js";
import { registerGetTinyImageTool } from "./get-tiny-image.js";
import { registerLongRunningOperationTool } from "./long-running-operation.js";
import { registerSamplingRequestTool } from "./sampling-request.js";
import { registerToggleLoggingTool } from "./toggle-logging.js";
import { registerToggleSubscriberUpdatesTool } from "./toggle-subscriber-updates.js";
import { registerGetStructuredContentTool } from "./get-structured-content.js";
/**
* Register the tools with the MCP server.
* @param server
*/
export const registerTools = (server: McpServer) => {
registerAddTool(server);
registerAnnotatedMessageTool(server);
registerEchoTool(server);
registerGetAnnotatedMessageTool(server);
registerGetEnvTool(server);
registerGetResourceLinksTool(server);
registerGetResourceReferenceTool(server);
registerGetStructuredContentTool(server);
registerGetSumTool(server);
registerGetTinyImageTool(server);
registerLongRunningOperationTool(server);
registerSamplingRequestTool(server);