Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions examples/system-monitor-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ async function getMemoryStats(): Promise<MemoryStats> {
};
}

async function getStats(): Promise<SystemStats> {
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",
Expand All @@ -128,28 +152,27 @@ export function createServer(): McpServer {
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
},
async (): Promise<CallToolResult> => {
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<CallToolResult> => {
const stats = await getStats();
return {
content: [{ type: "text", text: JSON.stringify(stats) }],
structuredContent: stats,
Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading