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

* Updated architecture.md

* Refactor/renamed sampling-request.ts to get-sampling-request.ts

* In tools/index.ts
  - sorted presenation order
This commit is contained in:
cliffhall 2025-12-10 16:58:25 -05:00
commit 48bf94a728
4 changed files with 16 additions and 19 deletions

View file

@ -52,7 +52,7 @@ src/everything
│ ├── get-structured-content.ts
│ ├── get-sum.ts
│ ├── long-running-operation.ts
│ ├── sampling-request.ts
│ ├── get-sampling-request.ts
│ ├── toggle-logging.ts
│ └── toggle-subscriber-updates.ts
└── package.json
@ -104,6 +104,8 @@ At `src/everything`:
- Registers a `get-resource-links` tool that returns an intro `text` block followed by multiple `resource_link` items.
- get-resource-reference.ts
- Registers a `get-resource-reference` tool that returns a reference for a selected dynamic resource.
- get-sampling-request.ts
- Registers a `sampling-request` tool that sends a `sampling/createMessage` request to the client/LLM and returns the sampling result.
- get-structured-content.ts
- Registers a `get-structured-content` tool that demonstrates structuredContent block responses.
- get-sum.ts
@ -112,8 +114,6 @@ At `src/everything`:
- Registers a `get-tiny-image` tool, which returns a tiny PNG MCP logo as an `image` content item, along with surrounding descriptive `text` items.
- long-running-operation.ts
- Registers a `long-running-operation` tool that simulates a long-running task over a specified `duration` (seconds) and number of `steps`; emits `notifications/progress` updates when the client supplies a `progressToken`.
- sampling-request.ts
- Registers a `sampling-request` tool that sends a `sampling/createMessage` request to the client/LLM and returns the sampling result.
- toggle-logging.ts
- Registers a `toggle-logging` tool, which starts or stops simulated logging for the invoking session.
- toggle-subscriber-updates.ts
@ -198,11 +198,11 @@ At `src/everything`:
- `get-env` (tools/get-env.ts): Returns all environment variables from the running process as pretty-printed JSON text.
- `get-resource-links` (tools/get-resource-links.ts): Returns an intro `text` block followed by multiple `resource_link` items. For a requested `count` (1–10), alternates between dynamic Text and Blob resources using URIs from `resources/templates.ts`.
- `get-resource-reference` (tools/get-resource-reference.ts): Accepts `resourceType` (`text` or `blob`) and `resourceId` (positive integer). Returns a concrete `resource` content block (with its `uri`, `mimeType`, and data) with surrounding explanatory `text`.
- `get-sampling-request` (tools/get-sampling-request.ts): Issues a `sampling/createMessage` request to the client/LLM using provided `prompt` and optional generation controls; returns the LLM’s response payload.
- `get-structured-content` (tools/get-structured-content.ts): Demonstrates structured responses. Accepts `location` input and returns both backward‑compatible `content` (a `text` block containing JSON) and `structuredContent` validated by an `outputSchema` (temperature, conditions, humidity).
- `get-sum` (tools/get-sum.ts): For two numbers `a` and `b` calculates and returns their sum. Uses Zod to validate inputs.
- `get-tiny-image` (tools/get-tiny-image.ts): Returns a tiny PNG MCP logo as an `image` content item with brief descriptive text before and after.
- `long-running-operation` (tools/long-running-operation.ts): Simulates a multi-step operation over a given `duration` and number of `steps`; reports progress via `notifications/progress` when a `progressToken` is provided by the client.
- `sampling-request` (tools/sampling-request.ts): Issues a `sampling/createMessage` request to the client/LLM using provided `prompt` and optional generation controls; returns the LLM’s response payload.
- `toggle-logging` (tools/toggle-logging.ts): Starts or stops simulated, random‑leveled logging for the invoking session. Respects the client’s selected minimum logging level.
- `toggle-subscriber-updates` (tools/toggle-subscriber-updates.ts): Starts or stops simulated resource update notifications for URIs the invoking session has subscribed to.

View file

@ -0,0 +1,83 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import {
CallToolResult,
CreateMessageRequest,
CreateMessageResultSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
// Tool input schema
const GetSamplingRequestSchema = z.object({
prompt: z.string().describe("The prompt to send to the LLM"),
maxTokens: z
.number()
.default(100)
.describe("Maximum number of tokens to generate"),
});
// Tool configuration
const name = "get-sampling-request";
const config = {
title: "Get Sampling Request Tool",
description: "Server Sends the Client a Request for LLM Sampling",
inputSchema: GetSamplingRequestSchema,
};
/**
* Registers the 'get-sampling-request' tool within the provided McpServer instance.
*
* The registered tool performs the following operations:
* - Validates incoming arguments using `SampleLLMSchema`.
* - Constructs a `sampling/createMessage` request object using provided prompt and maximum tokens.
* - Sends the request to the server for sampling.
* - Formats and returns the sampling result content to the client.
*
* @param {McpServer} server - The instance of the MCP server where the tool
* will be registered.
*/
export const registerGetSamplingRequestTool = (server: McpServer) => {
server.registerTool(
name,
config,
async (args, extra): Promise<CallToolResult> => {
const validatedArgs = GetSamplingRequestSchema.parse(args);
const { prompt, maxTokens } = validatedArgs;
// Create the sampling request
const request: CreateMessageRequest = {
method: "sampling/createMessage",
params: {
messages: [
{
role: "user",
content: {
type: "text",
text: `Resource ${name} context: ${prompt}`,
},
},
],
systemPrompt: "You are a helpful test server.",
maxTokens,
temperature: 0.7,
includeContext: "thisServer",
},
};
// Send the sampling request to the client
const result = await extra.sendRequest(
request,
CreateMessageResultSchema
);
// Return the result to the client
return {
content: [
{
type: "text",
text: `LLM sampling result: \n${JSON.stringify(result, null, 2)}`,
},
],
};
}
);
};

View file

@ -12,7 +12,7 @@ const GetSumSchema = z.object({
const name = "get-sum";
const config = {
title: "Get Sum Tool",
description: "Gets the sum of two numbers",
description: "Returns the sum of two numbers",
inputSchema: GetSumSchema,
};

View file

@ -4,11 +4,11 @@ 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 { registerGetSamplingRequestTool } from "./get-sampling-request.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";
@ -22,11 +22,11 @@ export const registerTools = (server: McpServer) => {
registerGetEnvTool(server);
registerGetResourceLinksTool(server);
registerGetResourceReferenceTool(server);
registerGetSamplingRequestTool(server);
registerGetStructuredContentTool(server);
registerGetSumTool(server);
registerGetTinyImageTool(server);
registerLongRunningOperationTool(server);
registerSamplingRequestTool(server);
registerToggleLoggingTool(server);
registerToggleSubscriberUpdatesTool(server);
};