111 lines
3.6 KiB
JavaScript
111 lines
3.6 KiB
JavaScript
import { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
import { z } from "zod";
|
|
import { fetchJSON } from "../lib/api.js";
|
|
|
|
const log = (...args) => console.log("[MCP]", ...args);
|
|
|
|
export default function registerPlanillas(server) {
|
|
// ----- Resources -----
|
|
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) }] };
|
|
});
|
|
|
|
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) }] };
|
|
}
|
|
);
|
|
|
|
// ----- Tools -----
|
|
server.tool(
|
|
"create-planilla",
|
|
"Crea una planilla",
|
|
{
|
|
empleado_id: z.number(),
|
|
fecha_desde: z.string(),
|
|
fecha_hasta: z.string(),
|
|
titulo: z.string(),
|
|
total: z.number().optional(),
|
|
estado: z.string().optional(),
|
|
fecha_anulado: z.string().optional(),
|
|
creador_id: z.number().optional(),
|
|
anulador_id: z.number().optional(),
|
|
},
|
|
async (params) => {
|
|
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) }] };
|
|
}
|
|
);
|
|
|
|
server.tool(
|
|
"update-planilla",
|
|
"Actualiza una planilla existente",
|
|
{
|
|
id: z.number(),
|
|
empleado_id: z.number().optional(),
|
|
fecha_desde: z.string().optional(),
|
|
fecha_hasta: z.string().optional(),
|
|
titulo: z.string().optional(),
|
|
total: z.number().optional(),
|
|
estado: z.string().optional(),
|
|
fecha_anulado: z.string().optional(),
|
|
anulador_id: z.number().optional(),
|
|
},
|
|
async ({ id, ...updates }) => {
|
|
log("Tool invocada", "update-planilla", { id, ...updates });
|
|
const planilla = await fetchJSON(`/api/planillas/${id}`, {
|
|
method: "PUT",
|
|
body: JSON.stringify(updates),
|
|
});
|
|
return { content: [{ type: "text", text: JSON.stringify(planilla) }] };
|
|
}
|
|
);
|
|
|
|
server.tool(
|
|
"delete-planilla",
|
|
"Elimina una planilla",
|
|
{ id: z.number() },
|
|
async ({ id }) => {
|
|
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)])
|
|
);
|
|
if (qs.toString() === "") qs.append("limit", "100");
|
|
const planillas = await fetchJSON(`/api/planillas/search?${qs.toString()}`);
|
|
return { content: [{ type: "text", text: JSON.stringify(planillas) }] };
|
|
}
|
|
);
|
|
}
|