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

Added print-env, and sampling-request tools

* Updated architecture.md

* In tools/index.ts
  - import registerPrintEnvTool and registerSamplingRequestTool
  - in registerTools
    - call registerPrintEnvTool and registerSamplingRequestTool

* Added tools/print-env.ts
  - registers a tool that takes no args and returns the environment variables

* Added tools/sampling-request
  - registers a tool that
    - takes prompt and maxTokens args
    - sends client a sampling request
    - returns the client response in the result
This commit is contained in:
cliffhall
2025-12-08 19:14:06 -05:00
parent 1df8623bcc
commit 0f3e27ef87
4 changed files with 139 additions and 7 deletions

View File

@@ -1,18 +1,22 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { registerEchoTool } from "./echo.js";
import { registerAddTool } from "./add.js";
import { registerEchoTool } from "./echo.js";
import { registerLongRunningOperationTool } from "./long-running-operation.js";
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 { registerLongRunningOperationTool } from "./long-running-operation.js";
/**
* Register the tools with the MCP server.
* @param server
*/
export const registerTools = (server: McpServer) => {
registerEchoTool(server);
registerAddTool(server);
registerEchoTool(server);
registerLongRunningOperationTool(server);
registerPrintEnvTool(server);
registerSamplingRequestTool(server);
registerToggleLoggingTool(server);
registerToggleSubscriberUpdatesTool(server);
registerLongRunningOperationTool(server);
};

View File

@@ -0,0 +1,32 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
// Tool configuration
const name = "print-env";
const config = {
title: "Print Environment Tool",
description:
"Prints all environment variables, helpful for debugging MCP server configuration",
inputSchema: {},
};
/**
* Registers the Echo Tool with the given MCP server. This tool, when invoked,
* retrieves and returns the environment variables of the current process
* as a JSON-formatted string encapsulated in a text response.
*
* @param {McpServer} server - The MCP server instance where the Echo Tool is to be registered.
* @returns {void}
*/
export const registerPrintEnvTool = (server: McpServer) => {
server.registerTool(name, config, async (args): Promise<CallToolResult> => {
return {
content: [
{
type: "text",
text: JSON.stringify(process.env, null, 2),
},
],
};
});
};

View File

@@ -0,0 +1,87 @@
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 SampleLLMSchema = 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 = "sampling-request";
const config = {
title: "Sampling Request Tool",
description: "Sends the Client a Request for LLM Sampling",
inputSchema: SampleLLMSchema,
};
/**
* Registers a sampling request tool within the given MCP server.
*
* This tool allows the server to handle sampling requests by parsing input
* arguments, generating a sampling request for an LLM, and returning the
* result to the client.
*
* The registered tool performs the following operations:
* - Validates incoming arguments using `SampleLLMSchema`.
* - Constructs a 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 registerSamplingRequestTool = (server: McpServer) => {
server.registerTool(
name,
config,
async (args, extra): Promise<CallToolResult> => {
const validatedArgs = SampleLLMSchema.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)}`,
},
],
};
}
);
};