diff --git a/src/everything/docs/architecture.md b/src/everything/docs/architecture.md index 97778c27..2ff474f8 100644 --- a/src/everything/docs/architecture.md +++ b/src/everything/docs/architecture.md @@ -84,14 +84,20 @@ At `src/everything`: - index.ts - `registerTools(server)` orchestrator; delegates to basic tools and control tools. + - add.ts + - Defines an `add` tool with a Zod input schema that sums two numbers `a` and `b` and returns the result. - 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. + - long-running-operation.ts + - Defines `long-running-operation`: simulates a long-running task over a specified `duration` (seconds) and number of `steps`; emits `notifications/progress` updates when the client supplies a `progressToken`. + - print-env.ts + - Defines `print-env`: returns the current process environment variables as formatted JSON text; useful for debugging configuration. - toggle-logging.ts - Defines `toggle-logging`: starts/stops simulated logging for the invoking session. - toggle-subscriber-updates.ts - Defines `toggle-subscriber-updates`: starts/stops simulated resource subscription update checks for the invoking session. + - sampling-request.ts + - Defines `sampling-request`: sends a `sampling/createMessage` request to the client/LLM and returns the sampling result. - prompts/ @@ -167,8 +173,11 @@ 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. + - `echo` (tools/echo.ts): Echoes the provided `message: string`. Uses Zod to validate inputs. + - `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. + - `print-env` (tools/print-env.ts): Returns all environment variables from the running process as pretty-printed JSON text. + - `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. diff --git a/src/everything/tools/index.ts b/src/everything/tools/index.ts index ab12e5f4..8e550bf0 100644 --- a/src/everything/tools/index.ts +++ b/src/everything/tools/index.ts @@ -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); }; diff --git a/src/everything/tools/print-env.ts b/src/everything/tools/print-env.ts new file mode 100644 index 00000000..5f7f4e0d --- /dev/null +++ b/src/everything/tools/print-env.ts @@ -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 => { + return { + content: [ + { + type: "text", + text: JSON.stringify(process.env, null, 2), + }, + ], + }; + }); +}; diff --git a/src/everything/tools/sampling-request.ts b/src/everything/tools/sampling-request.ts new file mode 100644 index 00000000..155e717e --- /dev/null +++ b/src/everything/tools/sampling-request.ts @@ -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 => { + 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)}`, + }, + ], + }; + } + ); +};