mirror of
https://github.com/modelcontextprotocol/servers.git
synced 2026-04-19 08:33:23 +02:00
[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:
@@ -1,6 +1,8 @@
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { registerEchoTool } from "./echo.js";
|
||||
import { registerAddTool } from "./add.js";
|
||||
import { registerToggleLoggingTool } from "./toggle-logging.js";
|
||||
import { registerToggleSubscriberUpdatesTool } from "./toggle-subscriber-updates.js";
|
||||
|
||||
/**
|
||||
* Register the tools with the MCP server.
|
||||
@@ -9,4 +11,6 @@ import { registerAddTool } from "./add.js";
|
||||
export const registerTools = (server: McpServer) => {
|
||||
registerEchoTool(server);
|
||||
registerAddTool(server);
|
||||
registerToggleLoggingTool(server);
|
||||
registerToggleSubscriberUpdatesTool(server);
|
||||
};
|
||||
|
||||
51
src/everything/tools/toggle-logging.ts
Normal file
51
src/everything/tools/toggle-logging.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
||||
import {
|
||||
beginSimulatedLogging,
|
||||
stopSimulatedLogging,
|
||||
} from "../server/logging.js";
|
||||
|
||||
const name = "toggle-logging";
|
||||
const config = {
|
||||
title: "Toggle Logging",
|
||||
description: "Toggles simulated logging on or off.",
|
||||
inputSchema: {},
|
||||
};
|
||||
|
||||
const clients: Set<string | undefined> = new Set<string | undefined>();
|
||||
|
||||
/**
|
||||
* Registers a tool that toggles simulated logging for a session on or off.
|
||||
*
|
||||
* This function allows the server to manage simulated logging for client sessions.
|
||||
* 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 it will be started.
|
||||
*
|
||||
* @param {McpServer} server - The server instance to which the tool is registered.
|
||||
* @returns {void}
|
||||
*/
|
||||
export const registerToggleLoggingTool = (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}` }],
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
40
src/everything/tools/toggle-subscriber-updates.ts
Normal file
40
src/everything/tools/toggle-subscriber-updates.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
||||
import {
|
||||
beginSimulatedResourceUpdates,
|
||||
stopSimulatedResourceUpdates,
|
||||
} from "../resources/subscriptions.js";
|
||||
|
||||
const name = "toggle-subscriber-updates";
|
||||
const config = {
|
||||
title: "Toggle Subscriber Updates",
|
||||
description: "Toggles simulated resource subscription updates on or off.",
|
||||
inputSchema: {},
|
||||
};
|
||||
|
||||
const clients: Set<string | undefined> = new Set<string | undefined>();
|
||||
|
||||
export const registerToggleSubscriberUpdatesTool = (server: McpServer) => {
|
||||
server.registerTool(
|
||||
name,
|
||||
config,
|
||||
async (_args, extra): Promise<CallToolResult> => {
|
||||
const sessionId = extra?.sessionId;
|
||||
|
||||
let response: string;
|
||||
if (clients.has(sessionId)) {
|
||||
stopSimulatedResourceUpdates(sessionId);
|
||||
clients.delete(sessionId);
|
||||
response = `Stopped simulated resource updates for session ${sessionId}`;
|
||||
} else {
|
||||
beginSimulatedResourceUpdates(server, sessionId);
|
||||
clients.add(sessionId);
|
||||
response = `Started simulated resource updated notifications for session ${sessionId} at a 5 second pace. Client will receive updates for any resources the it is subscribed to.`;
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{ type: "text", text: `${response}` }],
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user