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

Adding the annotated message tool

* Updated architecture.md

* Added annotated-message.ts
  - registers a tool that returns a message with annotations on different content types

* In package.json
  - updated TS SDK version
This commit is contained in:
cliffhall
2025-12-09 12:17:08 -05:00
parent 2ce87b168c
commit 0bf6c6911d
5 changed files with 150 additions and 18 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 AnnotatedMessageSchema = 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 = "annotated-message";
const config = {
title: "Annotated Message Tool",
description: "Demonstrates how annotations can be used to provide metadata about content.",
inputSchema: AnnotatedMessageSchema,
};
/**
* 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 registerAnnotatedMessageTool = (server: McpServer) => {
server.registerTool(name, config, async (args): Promise<CallToolResult> => {
const { messageType, includeImage } = AnnotatedMessageSchema.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

@@ -7,6 +7,7 @@ import { registerPrintEnvTool } from "./print-env.js";
import { registerSamplingRequestTool } from "./sampling-request.js";
import { registerToggleLoggingTool } from "./toggle-logging.js";
import { registerToggleSubscriberUpdatesTool } from "./toggle-subscriber-updates.js";
import {registerAnnotatedMessageTool} from "./annotated-message.js";
/**
* Register the tools with the MCP server.
@@ -14,6 +15,7 @@ import { registerToggleSubscriberUpdatesTool } from "./toggle-subscriber-updates
*/
export const registerTools = (server: McpServer) => {
registerAddTool(server);
registerAnnotatedMessageTool(server);
registerEchoTool(server);
registerGetTinyImageTool(server);
registerLongRunningOperationTool(server);