ya funcionando el mcp, vamos a continuar

This commit is contained in:
2025-06-03 16:09:43 -06:00
parent 1ecf990b9c
commit e3a39decda
385 changed files with 4671 additions and 55982 deletions

View File

@@ -1,3 +1,4 @@
// 🚨 Nuevos logs en puntos clave para saber qué se pide y cuándo
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
@@ -5,13 +6,15 @@ import express from "express";
import { z } from "zod";
const API_BASE_URL = process.env.PLANILLA_API_URL || "http://localhost:4000";
const log = (...args) => console.log("[MCP]", ...args);
// 👀 Log de cada request al API
async function fetchJSON(path, options = {}) {
const method = options.method || "GET";
console.log(`[API] ${method} ${API_BASE_URL}${path}`);
const res = await fetch(`${API_BASE_URL}${path}`, {
headers: {
'Content-Type': 'application/json'
},
...options
headers: { "Content-Type": "application/json" },
...options,
});
if (!res.ok) {
const txt = await res.text();
@@ -21,38 +24,26 @@ async function fetchJSON(path, options = {}) {
}
function createServer() {
const server = new McpServer({
name: "planilla-mcp",
version: "0.1.0",
});
const server = new McpServer({ name: "planilla-mcp", version: "0.1.0" });
// ----- Resources -----
// List all planillas
server.resource(
"planilla-list",
"planilla://list",
async (uri) => {
const planillas = await fetchJSON('/api/planillas');
return {
contents: [{ uri: uri.href, text: JSON.stringify(planillas) }]
};
}
);
server.resource("planilla-list", "planilla://list", async (uri) => {
log("Recurso solicitado", "planilla-list");
const planillas = await fetchJSON("/api/planillas");
return { contents: [{ uri: uri.href, text: JSON.stringify(planillas) }] };
});
// Get planilla by ID
server.resource(
"planilla",
new ResourceTemplate("planilla://{id}", { list: undefined }),
async (uri, { id }) => {
log("Recurso solicitado", `planilla ${id}`);
const planilla = await fetchJSON(`/api/planillas/${id}`);
return {
contents: [{ uri: uri.href, text: JSON.stringify(planilla) }]
};
return { contents: [{ uri: uri.href, text: JSON.stringify(planilla) }] };
}
);
// ----- Tools -----
// Create a new planilla
server.tool(
"create-planilla",
"Crea una planilla",
@@ -68,13 +59,15 @@ function createServer() {
anulador_id: z.number().optional(),
},
async (params) => {
const body = JSON.stringify(params);
const planilla = await fetchJSON('/api/planillas', { method: 'POST', body });
return { content: [{ type: 'text', text: JSON.stringify(planilla) }] };
log("Tool invocada", "create-planilla", params);
const planilla = await fetchJSON("/api/planillas", {
method: "POST",
body: JSON.stringify(params),
});
return { content: [{ type: "text", text: JSON.stringify(planilla) }] };
}
);
// Update planilla by ID
server.tool(
"update-planilla",
"Actualiza una planilla existente",
@@ -90,29 +83,63 @@ function createServer() {
anulador_id: z.number().optional(),
},
async ({ id, ...updates }) => {
const body = JSON.stringify(updates);
log("Tool invocada", "update-planilla", { id, ...updates });
const planilla = await fetchJSON(`/api/planillas/${id}`, {
method: 'PUT',
body,
method: "PUT",
body: JSON.stringify(updates),
});
return { content: [{ type: 'text', text: JSON.stringify(planilla) }] };
return { content: [{ type: "text", text: JSON.stringify(planilla) }] };
}
);
// Delete planilla by ID
server.tool(
"delete-planilla",
"Elimina una planilla",
{ id: z.number() },
async ({ id }) => {
await fetchJSON(`/api/planillas/${id}`, { method: 'DELETE' });
return { content: [{ type: 'text', text: `Planilla ${id} eliminada` }] };
log("Tool invocada", "delete-planilla", { id });
await fetchJSON(`/api/planillas/${id}`, { method: "DELETE" });
return { content: [{ type: "text", text: `Planilla ${id} eliminada` }] };
}
);
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)])
);
// 👇 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) }] };
}
);
return server;
}
async function main() {
const useStdio = process.argv.includes("--stdio");
if (useStdio) {
@@ -123,6 +150,13 @@ async function main() {
} else {
const app = express();
app.use(express.json());
// 🌐 Log de cada request HTTP entrante
app.use((req, _res, next) => {
console.log(`[HTTP] ${req.method} ${req.originalUrl}`);
next();
});
const port = process.env.PORT || 5000;
app.post("/mcp", async (req, res) => {
@@ -147,21 +181,15 @@ async function main() {
}
});
app.get("/mcp", async (_req, res) => {
res.writeHead(405).end(JSON.stringify({
jsonrpc: "2.0",
error: { code: -32000, message: "Method not allowed." },
id: null,
}));
});
app.delete("/mcp", async (_req, res) => {
res.writeHead(405).end(JSON.stringify({
jsonrpc: "2.0",
error: { code: -32000, message: "Method not allowed." },
id: null,
}));
});
["get", "delete"].forEach((m) =>
app[m]("/mcp", (_req, res) =>
res.status(405).json({
jsonrpc: "2.0",
error: { code: -32000, message: "Method not allowed." },
id: null,
})
)
);
app.listen(port, () => {
console.log(`MCP Planilla HTTP server listening on port ${port}`);