[WIP] Refactor everything server to be more modular and use recommended APIs.
* Updated docs * Refactor/renamed toggle-logging.ts to toggle-simulated-logging.ts - refactor/renamed registerToggleLoggingTool to registerToggleSimulatedLoggingTool
This commit is contained in:
parent
19e588bdcb
commit
f561f70002
5 changed files with 15 additions and 18 deletions
|
|
@ -19,7 +19,7 @@
|
|||
- `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.
|
||||
- `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-simulated-logging` (tools/toggle-simulated-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.
|
||||
- `trigger-sampling-request` (tools/trigger-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.
|
||||
|
||||
|
|
@ -47,5 +47,5 @@
|
|||
## Simulated Logging
|
||||
|
||||
- Simulated logging is available but off by default.
|
||||
- Use the `toggle-logging` tool to start/stop periodic log messages of varying levels (debug, info, notice, warning, error, critical, alert, emergency) per session.
|
||||
- Use the `toggle-simulated-logging` tool to start/stop periodic log messages of varying levels (debug, info, notice, warning, error, critical, alert, emergency) per session.
|
||||
- Clients can control the minimum level they receive via the standard MCP `logging/setLevel` request.
|
||||
|
|
|
|||
|
|
@ -30,5 +30,5 @@ Each client manages its own resource subscriptions and receives notifications on
|
|||
### Module: `server/logging.ts`
|
||||
|
||||
- Periodically sends randomized log messages at different levels. Messages can include the session ID for clarity during demos.
|
||||
- Started/stopped on demand via the `toggle-logging` tool, which calls `beginSimulatedLogging(server, sessionId?)` and `stopSimulatedLogging(sessionId?)`. Note that transport disconnect triggers `cleanup()` which also stops any active intervals.
|
||||
- Started/stopped on demand via the `toggle-simulated-logging` tool, which calls `beginSimulatedLogging(server, sessionId?)` and `stopSimulatedLogging(sessionId?)`. Note that transport disconnect triggers `cleanup()` which also stops any active intervals.
|
||||
- Uses `server.sendLoggingMessage({ level, data }, sessionId?)` so that the client’s configured minimum logging level is respected by the SDK.
|
||||
|
|
|
|||
|
|
@ -29,8 +29,7 @@ src/everything
|
|||
├── server
|
||||
│ ├── index.ts
|
||||
│ ├── logging.ts
|
||||
│ ├── roots.ts
|
||||
│ └── everything.ts
|
||||
│ └── roots.ts
|
||||
├── tools
|
||||
│ ├── index.ts
|
||||
│ ├── echo.ts
|
||||
|
|
@ -44,7 +43,7 @@ src/everything
|
|||
│ ├── get-tiny-image.ts
|
||||
│ ├── gzip-file-as-resource.ts
|
||||
│ ├── long-running-operation.ts
|
||||
│ ├── toggle-logging.ts
|
||||
│ ├── toggle-simulated-logging.ts
|
||||
│ ├── toggle-subscriber-updates.ts
|
||||
│ ├── trigger-elicitation-request.ts
|
||||
│ └── trigger-sampling-request.ts
|
||||
|
|
@ -113,11 +112,9 @@ src/everything
|
|||
- `index.ts`
|
||||
- Server factory that creates an `McpServer` with declared capabilities, loads server instructions, and registers tools, prompts, and resources.
|
||||
- Sets resource subscription handlers via `setSubscriptionHandlers(server)`.
|
||||
- Exposes `{ server, cleanup }` to the chosen transport. Cleanup stops any running intervals in the server when the transport disconencts.
|
||||
- Exposes `{ server, cleanup }` to the chosen transport. Cleanup stops any running intervals in the server when the transport disconnects.
|
||||
- `logging.ts`
|
||||
- Implements simulated logging. Periodically sends randomized log messages at various levels to the connected client session. Started/stopped on demand via a dedicated tool.
|
||||
- `everything.ts`
|
||||
- A full “reference/monolith” implementation demonstrating most MCP features. Not the default path used by the transports in this package.
|
||||
|
||||
### `tools/`
|
||||
|
||||
|
|
@ -156,8 +153,8 @@ 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`.
|
||||
- `toggle-logging.ts`
|
||||
- Registers a `toggle-logging` tool, which starts or stops simulated logging for the invoking session.
|
||||
- `toggle-simulated-logging.ts`
|
||||
- Registers a `toggle-simulated-logging` tool, which starts or stops simulated logging for the invoking session.
|
||||
- `toggle-subscriber-updates.ts`
|
||||
- Registers a `toggle-subscriber-updates` tool, which starts or stops simulated resource subscription update checks for the invoking session.
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import { registerGetSumTool } from "./get-sum.js";
|
|||
import { registerGetTinyImageTool } from "./get-tiny-image.js";
|
||||
import { registerGZipFileAsResourceTool } from "./gzip-file-as-resource.js";
|
||||
import { registerLongRunningOperationTool } from "./long-running-operation.js";
|
||||
import { registerToggleLoggingTool } from "./toggle-logging.js";
|
||||
import { registerToggleSimulatedLoggingTool } from "./toggle-simulated-logging.js";
|
||||
import { registerToggleSubscriberUpdatesTool } from "./toggle-subscriber-updates.js";
|
||||
import { registerTriggerElicitationRequestTool } from "./trigger-elicitation-request.js";
|
||||
import { registerTriggerSamplingRequestTool } from "./trigger-sampling-request.js";
|
||||
|
|
@ -31,7 +31,7 @@ export const registerTools = (server: McpServer) => {
|
|||
registerGetTinyImageTool(server);
|
||||
registerGZipFileAsResourceTool(server);
|
||||
registerLongRunningOperationTool(server);
|
||||
registerToggleLoggingTool(server);
|
||||
registerToggleSimulatedLoggingTool(server);
|
||||
registerToggleSubscriberUpdatesTool(server);
|
||||
registerTriggerElicitationRequestTool(server);
|
||||
registerTriggerSamplingRequestTool(server);
|
||||
|
|
|
|||
54
src/everything/tools/toggle-simulated-logging.ts
Normal file
54
src/everything/tools/toggle-simulated-logging.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
||||
import {
|
||||
beginSimulatedLogging,
|
||||
stopSimulatedLogging,
|
||||
} from "../server/logging.js";
|
||||
|
||||
// Tool configuration
|
||||
const name = "toggle-simulated-logging";
|
||||
const config = {
|
||||
title: "Toggle Simulated Logging",
|
||||
description: "Toggles simulated, random-leveled logging on or off.",
|
||||
inputSchema: {},
|
||||
};
|
||||
|
||||
// Track enabled clients by session id
|
||||
const clients: Set<string | undefined> = new Set<string | undefined>();
|
||||
|
||||
/**
|
||||
* Registers the `toggle-simulated-logging` tool with the provided MCP server.
|
||||
*
|
||||
* The registered tool enables or disables the sending of periodic, random-leveled
|
||||
* logging messages the connected client.
|
||||
*
|
||||
* When invoked, it either starts or stops simulated logging based on the session's
|
||||
* current state. If logging for the specified session is active, it will be stopped;
|
||||
* if it is inactive, logging will be started.
|
||||
*
|
||||
* @param {McpServer} server - The server instance to which the tool is registered.
|
||||
*/
|
||||
export const registerToggleSimulatedLoggingTool = (server: McpServer) => {
|
||||
server.registerTool(
|
||||
name,
|
||||
config,
|
||||
async (_args, extra): Promise<CallToolResult> => {
|
||||
const sessionId = extra?.sessionId;
|
||||
|
||||
let response: string;
|
||||
if (clients.has(sessionId)) {
|
||||
stopSimulatedLogging(sessionId);
|
||||
clients.delete(sessionId);
|
||||
response = `Stopped simulated logging for session ${sessionId}`;
|
||||
} else {
|
||||
beginSimulatedLogging(server, sessionId);
|
||||
clients.add(sessionId);
|
||||
response = `Started simulated, random-leveled logging for session ${sessionId} at a 5 second pace. Client's selected logging level will be respected. If an interval elapses and the message to be sent is below the selected level, it will not be sent. Thus at higher chosen logging levels, messages should arrive further apart. `;
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{ type: "text", text: `${response}` }],
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
Loading…
Reference in a new issue