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

Adding simulated logging and refactoring subscriptions to not need to track transports

* Updated architecture.md

* In server/index.ts
  - remove import of Transport
  - import beginSimulatedLogging and stopSimulatedLogging
  - in clientConnected()
    - change argument to sessionId? instead of transport
    - add call to beginSimulatedLogging
    - send server and sessionId to beginSimulatedResourceUpdates and beginSimulatedLogging
  - in cleanup()
    - add call to stopSimulatedLogging passing sessionId

* Added server/logging.ts
  - Initialize logsUpdateIntervals to Map session ID to the interval for sending logging messages to the client
  - in beginSimulatedLogging()
    - create an array of logging meesages, customized with the sessionId if present
    - if the interval for the sessionId hasn't been set, create one, calling server.sendLoggingMessage with a random message to the client each time the interval elapses
  - in stopSimulatedLogging()
    - if a logging interval exists for the sessionId, clear it and remove it

* 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 sse.ts and streamableHttp.ts
  - when calling clientConnected, pass sessionId instead of transport

* In stdio.ts,
- when calling clientConnected, pass nothing instead of transport

* In subscriptions.ts
  - updated inline doc
This commit is contained in:
cliffhall
2025-12-07 19:32:18 -05:00
parent 8559fbd5a4
commit 16ed05957c
8 changed files with 130 additions and 57 deletions

View File

@@ -2,7 +2,6 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { dirname, join } from "path";
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import {
setSubscriptionHandlers,
beginSimulatedResourceUpdates,
@@ -11,6 +10,7 @@ import {
import { registerTools } from "../tools/index.js";
import { registerResources } from "../resources/index.js";
import { registerPrompts } from "../prompts/index.js";
import { beginSimulatedLogging, stopSimulatedLogging } from "./logging.js";
// Everything Server factory
export const createServer = () => {
@@ -51,12 +51,15 @@ export const createServer = () => {
return {
server,
clientConnected: (transport: Transport) => {
beginSimulatedResourceUpdates(transport);
// TODO simulated logging
// 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);
stopSimulatedLogging(sessionId);
},
};
};