agregado datos de log para el MCP
This commit is contained in:
146
mcp/index.js
146
mcp/index.js
@@ -5,11 +5,29 @@ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
|
// -------------------------------
|
||||||
|
// 🔧 VARIABLES Y HELPERS GLOBALES
|
||||||
|
// -------------------------------
|
||||||
const API_BASE_URL = process.env.PLANILLA_API_URL || "http://localhost:4000";
|
const API_BASE_URL = process.env.PLANILLA_API_URL || "http://localhost:4000";
|
||||||
const log = (...args) => console.log("[MCP]", ...args);
|
const log = (...args) => console.log("[MCP]", ...args);
|
||||||
|
const bootLog = (...args) => console.log("[BOOT]", ...args);
|
||||||
|
|
||||||
// 👀 Log de cada request al API
|
// Imprime detalles del runtime apenas carga el módulo
|
||||||
async function fetchJSON(path, options = {}) {
|
(function printBootInfo () {
|
||||||
|
bootLog("Iniciando Planilla MCP v0.1.0");
|
||||||
|
bootLog(`Node : ${process.version} (${process.platform}-${process.arch})`);
|
||||||
|
bootLog(`Working directory : ${process.cwd()}`);
|
||||||
|
bootLog(`PLANILLA_API_URL : ${API_BASE_URL}`);
|
||||||
|
bootLog(`PORT (HTTP) : ${process.env.PORT ?? "5000 (default)"}`);
|
||||||
|
bootLog(`Args : ${process.argv.slice(2).join(" ") || "<none>"}`);
|
||||||
|
const rss = (process.memoryUsage().rss / 1024 ** 2).toFixed(1);
|
||||||
|
bootLog(`Boot RSS Memory : ${rss} MB`);
|
||||||
|
})();
|
||||||
|
|
||||||
|
// --------------------------------
|
||||||
|
// ⛑️ FETCH HELPER CON LOGGING
|
||||||
|
// --------------------------------
|
||||||
|
async function fetchJSON (path, options = {}) {
|
||||||
const method = options.method || "GET";
|
const method = options.method || "GET";
|
||||||
console.log(`[API] ${method} ${API_BASE_URL}${path}`);
|
console.log(`[API] ${method} ${API_BASE_URL}${path}`);
|
||||||
const res = await fetch(`${API_BASE_URL}${path}`, {
|
const res = await fetch(`${API_BASE_URL}${path}`, {
|
||||||
@@ -23,8 +41,12 @@ async function fetchJSON(path, options = {}) {
|
|||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
function createServer() {
|
// -------------------------------
|
||||||
|
// 🏗️ CREACIÓN DEL MCP SERVER
|
||||||
|
// -------------------------------
|
||||||
|
function createServer () {
|
||||||
const server = new McpServer({ name: "planilla-mcp", version: "0.1.0" });
|
const server = new McpServer({ name: "planilla-mcp", version: "0.1.0" });
|
||||||
|
log("Servidor MCP instanciado");
|
||||||
|
|
||||||
// ----- Resources -----
|
// ----- Resources -----
|
||||||
server.resource("planilla-list", "planilla://list", async (uri) => {
|
server.resource("planilla-list", "planilla://list", async (uri) => {
|
||||||
@@ -48,21 +70,21 @@ function createServer() {
|
|||||||
"create-planilla",
|
"create-planilla",
|
||||||
"Crea una planilla",
|
"Crea una planilla",
|
||||||
{
|
{
|
||||||
empleado_id: z.number(),
|
empleado_id: z.number(),
|
||||||
fecha_desde: z.string(),
|
fecha_desde: z.string(),
|
||||||
fecha_hasta: z.string(),
|
fecha_hasta: z.string(),
|
||||||
titulo: z.string(),
|
titulo: z.string(),
|
||||||
total: z.number().optional(),
|
total: z.number().optional(),
|
||||||
estado: z.string().optional(),
|
estado: z.string().optional(),
|
||||||
fecha_anulado: z.string().optional(),
|
fecha_anulado:z.string().optional(),
|
||||||
creador_id: z.number().optional(),
|
creador_id: z.number().optional(),
|
||||||
anulador_id: z.number().optional(),
|
anulador_id: z.number().optional(),
|
||||||
},
|
},
|
||||||
async (params) => {
|
async (params) => {
|
||||||
log("Tool invocada", "create-planilla", params);
|
log("Tool invocada", "create-planilla", params);
|
||||||
const planilla = await fetchJSON("/api/planillas", {
|
const planilla = await fetchJSON("/api/planillas", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(params),
|
body: JSON.stringify(params),
|
||||||
});
|
});
|
||||||
return { content: [{ type: "text", text: JSON.stringify(planilla) }] };
|
return { content: [{ type: "text", text: JSON.stringify(planilla) }] };
|
||||||
}
|
}
|
||||||
@@ -72,21 +94,21 @@ function createServer() {
|
|||||||
"update-planilla",
|
"update-planilla",
|
||||||
"Actualiza una planilla existente",
|
"Actualiza una planilla existente",
|
||||||
{
|
{
|
||||||
id: z.number(),
|
id: z.number(),
|
||||||
empleado_id: z.number().optional(),
|
empleado_id: z.number().optional(),
|
||||||
fecha_desde: z.string().optional(),
|
fecha_desde: z.string().optional(),
|
||||||
fecha_hasta: z.string().optional(),
|
fecha_hasta: z.string().optional(),
|
||||||
titulo: z.string().optional(),
|
titulo: z.string().optional(),
|
||||||
total: z.number().optional(),
|
total: z.number().optional(),
|
||||||
estado: z.string().optional(),
|
estado: z.string().optional(),
|
||||||
fecha_anulado: z.string().optional(),
|
fecha_anulado:z.string().optional(),
|
||||||
anulador_id: z.number().optional(),
|
anulador_id: z.number().optional(),
|
||||||
},
|
},
|
||||||
async ({ id, ...updates }) => {
|
async ({ id, ...updates }) => {
|
||||||
log("Tool invocada", "update-planilla", { id, ...updates });
|
log("Tool invocada", "update-planilla", { id, ...updates });
|
||||||
const planilla = await fetchJSON(`/api/planillas/${id}`, {
|
const planilla = await fetchJSON(`/api/planillas/${id}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
body: JSON.stringify(updates),
|
body: JSON.stringify(updates),
|
||||||
});
|
});
|
||||||
return { content: [{ type: "text", text: JSON.stringify(planilla) }] };
|
return { content: [{ type: "text", text: JSON.stringify(planilla) }] };
|
||||||
}
|
}
|
||||||
@@ -103,52 +125,50 @@ function createServer() {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
server.tool(
|
||||||
|
"search-planillas",
|
||||||
|
"Busca planillas. `q` es un texto libre que matchea id, empleado_id, título o estado. Si no mandás ningún argumento devuelve los primeros 100 registros.",
|
||||||
|
{
|
||||||
|
q: z.string().optional(),
|
||||||
|
empleado_id: z.number().optional(),
|
||||||
|
estado: z.string().optional(),
|
||||||
|
titulo: z.string().optional(),
|
||||||
|
fecha_desde_desde: z.string().optional(),
|
||||||
|
fecha_desde_hasta: z.string().optional(),
|
||||||
|
fecha_hasta_desde: z.string().optional(),
|
||||||
|
fecha_hasta_hasta: z.string().optional(),
|
||||||
|
},
|
||||||
|
async (params) => {
|
||||||
|
log("Tool invocada", "search-planillas", params);
|
||||||
|
const qs = new URLSearchParams(
|
||||||
|
Object.entries(params).filter(([, v]) => v !== undefined).map(([k, v]) => [k, String(v)])
|
||||||
|
);
|
||||||
|
|
||||||
server.tool(
|
// 👇 Si no hay filtros, limit=100
|
||||||
"search-planillas",
|
if (qs.toString() === "") qs.append("limit", "100");
|
||||||
"Busca planillas. `q` es un texto libre que matchea id, empleado_id, título o estado. "
|
|
||||||
+ "Si no mandás ningún argumento devuelve los primeros 100 registros.",
|
|
||||||
{
|
|
||||||
q: z.string().optional(),
|
|
||||||
empleado_id: z.number().optional(),
|
|
||||||
estado: z.string().optional(),
|
|
||||||
titulo: z.string().optional(),
|
|
||||||
fecha_desde_desde: z.string().optional(),
|
|
||||||
fecha_desde_hasta: z.string().optional(),
|
|
||||||
fecha_hasta_desde: z.string().optional(),
|
|
||||||
fecha_hasta_hasta: z.string().optional(),
|
|
||||||
},
|
|
||||||
async (params) => {
|
|
||||||
log("Tool invocada", "search-planillas", params);
|
|
||||||
const qs = new URLSearchParams(
|
|
||||||
Object.entries(params).filter(([, v]) => v !== undefined).map(([k, v]) => [k, String(v)])
|
|
||||||
);
|
|
||||||
|
|
||||||
// 👇 Si no hay filtros, limit=100
|
|
||||||
if (qs.toString() === "") qs.append("limit", "100");
|
|
||||||
|
|
||||||
const planillas = await fetchJSON(`/api/planillas/search?${qs.toString()}`);
|
|
||||||
return { content: [{ type: "text", text: JSON.stringify(planillas) }] };
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
|
const planillas = await fetchJSON(`/api/planillas/search?${qs.toString()}`);
|
||||||
|
return { content: [{ type: "text", text: JSON.stringify(planillas) }] };
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --------------------------------
|
||||||
|
// 🚀 PUNTO DE ENTRADA
|
||||||
|
// --------------------------------
|
||||||
async function main() {
|
async function main () {
|
||||||
const useStdio = process.argv.includes("--stdio");
|
const useStdio = process.argv.includes("--stdio");
|
||||||
if (useStdio) {
|
if (useStdio) {
|
||||||
const server = createServer();
|
bootLog("Modo transporte: stdio");
|
||||||
|
const server = createServer();
|
||||||
const transport = new StdioServerTransport();
|
const transport = new StdioServerTransport();
|
||||||
await server.connect(transport);
|
await server.connect(transport);
|
||||||
console.log("MCP Planilla server listo (stdio)");
|
console.log("MCP Planilla server listo (stdio)");
|
||||||
} else {
|
} else {
|
||||||
const app = express();
|
bootLog("Modo transporte: HTTP streamable");
|
||||||
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// 🌐 Log de cada request HTTP entrante
|
// 🌐 Log de cada request HTTP entrante
|
||||||
@@ -161,7 +181,7 @@ async function main() {
|
|||||||
|
|
||||||
app.post("/mcp", async (req, res) => {
|
app.post("/mcp", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const server = createServer();
|
const server = createServer();
|
||||||
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
|
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
|
||||||
res.on("close", () => {
|
res.on("close", () => {
|
||||||
transport.close();
|
transport.close();
|
||||||
@@ -174,8 +194,8 @@ async function main() {
|
|||||||
if (!res.headersSent) {
|
if (!res.headersSent) {
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
error: { code: -32603, message: "Internal server error" },
|
error: { code: -32603, message: "Internal server error" },
|
||||||
id: null,
|
id: null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,8 +205,8 @@ async function main() {
|
|||||||
app[m]("/mcp", (_req, res) =>
|
app[m]("/mcp", (_req, res) =>
|
||||||
res.status(405).json({
|
res.status(405).json({
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
error: { code: -32000, message: "Method not allowed." },
|
error: { code: -32000, message: "Method not allowed." },
|
||||||
id: null,
|
id: null,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user