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

Added tools to toggle simulated logging and resource updates on and off rather than have them start immediately upon connection

* Updated architecture.md

* In server/index.ts
  - remove import of beginSimulatedResourceUpdates and beginSimulatedLogging
  - remove clientConnected from createServer factory result

* In tools/index.ts
  - import registerToggleLoggingTool and registerToggleSubscriberUpdatesTool
  - in registerTools
    - call registerToggleLoggingTool and registerToggleSubscriberUpdatesTool

* In logging.ts
  - in beginSimulatedLogging
    - refactor extract inline interval callback into function sendSimulatedLoggingMessage
  - call sendSimulatedLoggingMessage right away to send the first message
  - supply sendSimulatedLoggingMessage as interval callback

* In subscriptions.ts
  - remove import of Transport
  - remove transports map
  - in beginSimulatedResourceUpdates()
    - change arguments to server and sessionId
    - check for the subsUpdateInterval for the session
    - remove all transport storage and interaction
    - instead use the server to send the notification
 - in stopSimulatedResourceUpdates()
   - remove management of transports map

* In stdio.ts, sse.ts, and streamableHttp.ts
  - remove destructure and calling of clientConnected

* Added tools/toggle-logging.ts
  - registers a tool that
    - takes no arguments
    - tracks clients that have been enabled by session id in a set
    - if client isn't enabled,
       - calls beginSimulatedLogging
       - adds session id to client set
    - else
      - calls stopSimulatedLogging
      - deletes session id from client set
    - returns a message explaining what was done including what to expect when logging is enabled

 * Added tools/toggle-subscriber-updates.ts
   - registers a tool that
     - takes no arguments
     - tracks clients that have been enabled by session id in a set
     - if client isn't enabled,
        - calls beginSimulatedResourceUpdates
        - adds session id to client set
     - else
       - calls stopSimulatedResourceUpdates
       - deletes session id from client set
     - returns a message explaining what was done including what to expect when logging is enabled
This commit is contained in:
cliffhall
2025-12-08 17:13:42 -05:00
parent 16ed05957c
commit 346c29a086
10 changed files with 182 additions and 62 deletions

View File

@@ -4,13 +4,12 @@ import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import {
setSubscriptionHandlers,
beginSimulatedResourceUpdates,
stopSimulatedResourceUpdates
stopSimulatedResourceUpdates,
} from "../resources/subscriptions.js";
import { registerTools } from "../tools/index.js";
import { registerResources } from "../resources/index.js";
import { registerPrompts } from "../prompts/index.js";
import { beginSimulatedLogging, stopSimulatedLogging } from "./logging.js";
import { stopSimulatedLogging } from "./logging.js";
// Everything Server factory
export const createServer = () => {
@@ -49,17 +48,13 @@ export const createServer = () => {
// Set resource subscription handlers
setSubscriptionHandlers(server);
// Return server instance and cleanup function
return {
server,
// When the client connects, begin simulated resource updates and logging
clientConnected: (sessionId?: string) => {
beginSimulatedResourceUpdates(server, sessionId);
beginSimulatedLogging(server, sessionId);
},
// When the client disconnects, stop simulated resource updates and logging
cleanup: (sessionId?: string) => {
stopSimulatedResourceUpdates(sessionId);
// Stop any simulated logging or resource updates that may have been initiated.
stopSimulatedLogging(sessionId);
stopSimulatedResourceUpdates(sessionId);
},
};
};