From eee9866ebba3a1da6e9b5b10b322edb2cf678e30 Mon Sep 17 00:00:00 2001 From: cliffhall Date: Fri, 5 Dec 2025 18:43:36 -0500 Subject: [PATCH] [WIP] Refactor everything server to be more modular and use recommended APIs. * Updated architecture.md * Refactor / renamed all addXPrompt, addXTool, and addXResource functions to registerX... * Added the add tool --- src/everything/docs/architecture.md | 14 ++++++++----- src/everything/prompts/completions.ts | 2 +- src/everything/prompts/complex.ts | 2 +- src/everything/prompts/index.ts | 12 +++++------ src/everything/prompts/simple.ts | 2 +- src/everything/resources/dynamic.ts | 2 +- src/everything/resources/index.ts | 8 +++---- src/everything/resources/static.ts | 2 +- src/everything/tools/add.ts | 30 +++++++++++++++++++++++++++ src/everything/tools/echo.ts | 2 +- src/everything/tools/index.ts | 6 ++++-- 11 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 src/everything/tools/add.ts diff --git a/src/everything/docs/architecture.md b/src/everything/docs/architecture.md index e2ae9903..2986aaf0 100644 --- a/src/everything/docs/architecture.md +++ b/src/everything/docs/architecture.md @@ -24,7 +24,8 @@ src/everything │ └── streamableHttp.ts ├── tools │ ├── index.ts -│ └── echo.ts +│ ├── echo.ts +│ └── add.ts ├── prompts │ ├── index.ts │ ├── simple.ts @@ -70,9 +71,11 @@ At `src/everything`: - tools/ - index.ts - - `registerTools(server)` orchestrator, currently delegates to `addToolEcho`. + - `registerTools(server)` orchestrator, currently delegates to `registerEchoTool` and `registerAddTool`. - echo.ts - Defines a minimal `echo` tool with a Zod input schema and returns `Echo: {message}`. + - add.ts + - Defines an `add` tool with a Zod input schema that sums two numbers `a` and `b` and returns the result. - prompts/ @@ -147,6 +150,7 @@ At `src/everything`: - Tools - `echo` (tools/echo.ts): Echoes the provided `message: string`. Uses Zod to validate inputs. + - `add` (tools/add.ts): Adds two numbers `a` and `b` and returns their sum. Uses Zod to validate inputs. - Prompts @@ -163,17 +167,17 @@ At `src/everything`: - Adding Tools - - Create a new file under `tools/` with your `addToolX(server)` function that registers the tool via `server.registerTool(...)`. + - Create a new file under `tools/` with your `registerXTool(server)` function that registers the tool via `server.registerTool(...)`. - Export and call it from `tools/index.ts` inside `registerTools(server)`. - Adding Prompts - - Create a new file under `prompts/` with your `addXPrompt(server)` function that registers the prompt via `server.registerPrompt(...)`. + - Create a new file under `prompts/` with your `registerXPrompt(server)` function that registers the prompt via `server.registerPrompt(...)`. - Export and call it from `prompts/index.ts` inside `registerPrompts(server)`. - Adding Resources - - Create a new file under `resources/` with your `addXResources(server)` function using `server.registerResource(...)` (optionally with `ResourceTemplate`). + - Create a new file under `resources/` with your `registerXResources(server)` function using `server.registerResource(...)` (optionally with `ResourceTemplate`). - Export and call it from `resources/index.ts` inside `registerResources(server)`. - Adding Transports diff --git a/src/everything/prompts/completions.ts b/src/everything/prompts/completions.ts index 058c35a9..23301ba8 100644 --- a/src/everything/prompts/completions.ts +++ b/src/everything/prompts/completions.ts @@ -2,7 +2,7 @@ import { z } from "zod"; import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { completable } from "@modelcontextprotocol/sdk/server/completable.js"; -export const addPromptWithCompletions = (server: McpServer) => { +export const registerPromptWithCompletions = (server: McpServer) => { const promptArgsSchema = { department: completable(z.string(), (value) => { return ["Engineering", "Sales", "Marketing", "Support"].filter((d) => diff --git a/src/everything/prompts/complex.ts b/src/everything/prompts/complex.ts index a0447a01..c0d38348 100644 --- a/src/everything/prompts/complex.ts +++ b/src/everything/prompts/complex.ts @@ -1,7 +1,7 @@ import { z } from "zod"; import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; -export const addComplexPrompt = (server: McpServer) => { +export const registerComplexPrompt = (server: McpServer) => { const promptArgsSchema = { city: z.string().describe("Name of the city"), state: z.string().describe("Name of the state").optional(), diff --git a/src/everything/prompts/index.ts b/src/everything/prompts/index.ts index c66d6fc6..51d1dc57 100644 --- a/src/everything/prompts/index.ts +++ b/src/everything/prompts/index.ts @@ -1,14 +1,14 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; -import { addSimplePrompt } from "./simple.js"; -import { addComplexPrompt } from "./complex.js"; -import { addPromptWithCompletions } from "./completions.js"; +import { registerSimplePrompt } from "./simple.js"; +import { registerComplexPrompt } from "./complex.js"; +import { registerPromptWithCompletions } from "./completions.js"; /** * Register the prompts with the MCP server. * @param server */ export const registerPrompts = (server: McpServer) => { - addSimplePrompt(server); - addComplexPrompt(server); - addPromptWithCompletions(server); + registerSimplePrompt(server); + registerComplexPrompt(server); + registerPromptWithCompletions(server); }; diff --git a/src/everything/prompts/simple.ts b/src/everything/prompts/simple.ts index c7561f0b..abd15e4c 100644 --- a/src/everything/prompts/simple.ts +++ b/src/everything/prompts/simple.ts @@ -1,6 +1,6 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; -export const addSimplePrompt = (server: McpServer) => { +export const registerSimplePrompt = (server: McpServer) => { server.registerPrompt( "simple-prompt", { diff --git a/src/everything/resources/dynamic.ts b/src/everything/resources/dynamic.ts index 05a052cf..143e2d24 100644 --- a/src/everything/resources/dynamic.ts +++ b/src/everything/resources/dynamic.ts @@ -18,7 +18,7 @@ import { * * @param server */ -export const addDynamicResources = (server: McpServer) => { +export const registerDynamicResources = (server: McpServer) => { const uriBase: string = "test://dynamic/resource"; const textUriBase: string = `${uriBase}/text`; const blobUriBase: string = `${uriBase}/blob`; diff --git a/src/everything/resources/index.ts b/src/everything/resources/index.ts index fbf9f328..481c9386 100644 --- a/src/everything/resources/index.ts +++ b/src/everything/resources/index.ts @@ -1,12 +1,12 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; -import { addDynamicResources } from "./dynamic.js"; -import { addStaticResources } from "./static.js"; +import { registerDynamicResources } from "./dynamic.js"; +import { registerStaticResources } from "./static.js"; /** * Register the resources with the MCP server. * @param server */ export const registerResources = (server: McpServer) => { - addDynamicResources(server); - addStaticResources(server); + registerDynamicResources(server); + registerStaticResources(server); }; diff --git a/src/everything/resources/static.ts b/src/everything/resources/static.ts index d2a8015d..16eb837f 100644 --- a/src/everything/resources/static.ts +++ b/src/everything/resources/static.ts @@ -15,7 +15,7 @@ const __dirname = dirname(__filename); * * @param server */ -export const addStaticResources = (server: McpServer) => { +export const registerStaticResources = (server: McpServer) => { const docsDir = join(__dirname, "..", "docs"); let entries: string[] = []; diff --git a/src/everything/tools/add.ts b/src/everything/tools/add.ts new file mode 100644 index 00000000..42bc049c --- /dev/null +++ b/src/everything/tools/add.ts @@ -0,0 +1,30 @@ +import { z } from "zod"; +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; + +const AddSchema = z.object({ + a: z.number().describe("First number"), + b: z.number().describe("Second number"), +}); + +const name = "add"; +const config = { + title: "Add Tool", + description: "Adds two numbers", + inputSchema: AddSchema, +}; + +export const registerAddTool = (server: McpServer) => { + server.registerTool(name, config, async (args): Promise => { + const validatedArgs = AddSchema.parse(args); + const sum = validatedArgs.a + validatedArgs.b; + return { + content: [ + { + type: "text", + text: `The sum of ${validatedArgs.a} and ${validatedArgs.b} is ${sum}.`, + }, + ], + }; + }); +}; diff --git a/src/everything/tools/echo.ts b/src/everything/tools/echo.ts index 87f351ef..37d9a426 100644 --- a/src/everything/tools/echo.ts +++ b/src/everything/tools/echo.ts @@ -13,7 +13,7 @@ const config = { inputSchema: EchoSchema, }; -export const addToolEcho = (server: McpServer) => { +export const registerEchoTool = (server: McpServer) => { server.registerTool(name, config, async (args): Promise => { const validatedArgs = EchoSchema.parse(args); return { diff --git a/src/everything/tools/index.ts b/src/everything/tools/index.ts index 380b7900..a365eaa4 100644 --- a/src/everything/tools/index.ts +++ b/src/everything/tools/index.ts @@ -1,10 +1,12 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; -import { addToolEcho } from "./echo.js"; +import { registerEchoTool } from "./echo.js"; +import { registerAddTool } from "./add.js"; /** * Register the tools with the MCP server. * @param server */ export const registerTools = (server: McpServer) => { - addToolEcho(server); + registerEchoTool(server); + registerAddTool(server); };