diff --git a/examples/system-monitor-server/server.ts b/examples/system-monitor-server/server.ts index 477a9ae..8ac7f7b 100644 --- a/examples/system-monitor-server/server.ts +++ b/examples/system-monitor-server/server.ts @@ -107,6 +107,30 @@ async function getMemoryStats(): Promise { }; } +async function getStats(): Promise { + const cpuSnapshots = getCpuSnapshots(); + const cpuInfo = os.cpus()[0]; + const memory = await getMemoryStats(); + const uptimeSeconds = os.uptime(); + + return { + cpu: { + cores: cpuSnapshots, + model: cpuInfo?.model ?? "Unknown", + count: os.cpus().length, + }, + memory, + system: { + hostname: os.hostname(), + platform: `${os.platform()} ${os.arch()}`, + arch: os.arch(), + uptime: uptimeSeconds, + uptimeFormatted: formatUptime(uptimeSeconds), + }, + timestamp: new Date().toISOString(), + }; +} + export function createServer(): McpServer { const server = new McpServer({ name: "System Monitor Server", @@ -128,28 +152,27 @@ export function createServer(): McpServer { _meta: { [RESOURCE_URI_META_KEY]: resourceUri }, }, async (): Promise => { - const cpuSnapshots = getCpuSnapshots(); - const cpuInfo = os.cpus()[0]; - const memory = await getMemoryStats(); - const uptimeSeconds = os.uptime(); - - const stats: SystemStats = { - cpu: { - cores: cpuSnapshots, - model: cpuInfo?.model ?? "Unknown", - count: os.cpus().length, - }, - memory, - system: { - hostname: os.hostname(), - platform: `${os.platform()} ${os.arch()}`, - arch: os.arch(), - uptime: uptimeSeconds, - uptimeFormatted: formatUptime(uptimeSeconds), - }, - timestamp: new Date().toISOString(), + const stats = await getStats(); + return { + content: [{ type: "text", text: JSON.stringify(stats) }], + structuredContent: stats, }; + }, + ); + // App-only tool for polling - used by the UI for periodic refresh + registerAppTool( + server, + "refresh-stats", + { + title: "Refresh Stats", + description: "Refresh system statistics (app-only, for polling)", + inputSchema: {}, + outputSchema: SystemStatsSchema.shape, + _meta: { ui: { visibility: ["app"] } }, + }, + async (): Promise => { + const stats = await getStats(); return { content: [{ type: "text", text: JSON.stringify(stats) }], structuredContent: stats, diff --git a/package-lock.json b/package-lock.json index a6cc8a4..66c9546 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5590,6 +5590,7 @@ "version": "4.53.3", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@types/estree": "1.0.8" },