[DEV] Tool _webmcp_server-info: estado del servidor MCP
Muestra version, commit git, host, puerto, canales activos, clientes conectados, registry (tools/prompts/resources), uptime y requests pendientes. Solo modo dev.
This commit is contained in:
@@ -2,7 +2,7 @@ import * as fs from 'fs/promises';
|
||||
import {WebSocketServer, WebSocket} from 'ws';
|
||||
import {createServer} from 'http';
|
||||
import {parse} from 'url';
|
||||
import {fork} from 'child_process';
|
||||
import {fork, execSync} from 'child_process';
|
||||
import {runMcpServer} from './server.js';
|
||||
import {
|
||||
clearTokens,
|
||||
@@ -97,6 +97,17 @@ const toolsRegistry = {};
|
||||
const promptsRegistry = {};
|
||||
const resourcesRegistry = {};
|
||||
|
||||
// Server start timestamp for uptime calculation
|
||||
const serverStartTime = Date.now();
|
||||
|
||||
// Git commit hash (read once at startup)
|
||||
let gitCommit = 'unknown';
|
||||
try {
|
||||
gitCommit = execSync('git rev-parse --short HEAD', { stdio: ['pipe', 'pipe', 'ignore'] }).toString().trim();
|
||||
} catch (e) {
|
||||
// Not in a git repo or git not available
|
||||
}
|
||||
|
||||
// Request counter for unique IDs
|
||||
let requestIdCounter = 1;
|
||||
|
||||
@@ -371,6 +382,10 @@ wss.on('connection', (ws, req) => {
|
||||
handleGetClientInfo(ws, clientChannel, data);
|
||||
break;
|
||||
|
||||
case 'getServerInfo':
|
||||
handleGetServerInfo(ws, data);
|
||||
break;
|
||||
|
||||
case 'clearRegistry':
|
||||
handleClearRegistry(ws, data);
|
||||
break;
|
||||
@@ -875,6 +890,51 @@ function handleGetClientInfo(ws, callerChannel, data) {
|
||||
}));
|
||||
}
|
||||
|
||||
// Handle getServerInfo request - return server status and stats
|
||||
function handleGetServerInfo(ws, data) {
|
||||
const { id } = data;
|
||||
|
||||
const uptimeMs = Date.now() - serverStartTime;
|
||||
const uptimeSec = Math.floor(uptimeMs / 1000);
|
||||
const hours = Math.floor(uptimeSec / 3600);
|
||||
const minutes = Math.floor((uptimeSec % 3600) / 60);
|
||||
const seconds = uptimeSec % 60;
|
||||
|
||||
const channelsList = {};
|
||||
for (const [channelPath, channelClients] of Object.entries(channels)) {
|
||||
channelsList[channelPath] = channelClients.size;
|
||||
}
|
||||
|
||||
const info = {
|
||||
server: `@nucleoriofrio/webmcp v0.2.0`,
|
||||
commit: gitCommit,
|
||||
host: HOST,
|
||||
port: CONFIG.port,
|
||||
dev: !!CONFIG.dev,
|
||||
uptime: `${hours}h ${minutes}m ${seconds}s`,
|
||||
uptimeMs,
|
||||
channels: channelsList,
|
||||
totalClients: Object.values(channels).reduce((sum, ch) => sum + ch.size, 0),
|
||||
registry: {
|
||||
tools: Object.keys(toolsRegistry).length,
|
||||
prompts: Object.keys(promptsRegistry).length,
|
||||
resources: Object.keys(resourcesRegistry).length,
|
||||
},
|
||||
toolsList: Object.entries(toolsRegistry).map(([id, info]) => ({
|
||||
id,
|
||||
name: info.originalName,
|
||||
channel: info.channel,
|
||||
})),
|
||||
pendingRequests: Object.keys(pendingRequests).length,
|
||||
};
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
id,
|
||||
type: 'toolResponse',
|
||||
result: { content: [{ type: 'text', text: JSON.stringify(info, null, 2) }] }
|
||||
}));
|
||||
}
|
||||
|
||||
// Handle clearing all registries
|
||||
function handleClearRegistry(ws, data) {
|
||||
const {id} = data;
|
||||
|
||||
Reference in New Issue
Block a user