@zincapp/mcp-server 1.7.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/tools/index.js
CHANGED
|
@@ -13,8 +13,10 @@ import { registerAuditEntities } from './audit-entities.js';
|
|
|
13
13
|
import { registerApplyCompletion } from './apply-completion.js';
|
|
14
14
|
import { registerProposeAuthorizations } from './propose-authorizations.js';
|
|
15
15
|
import { registerApplyAuthorization } from './apply-authorization.js';
|
|
16
|
+
import { registerLinkGestorAuthorizations } from './link-gestor-authorizations.js';
|
|
16
17
|
import { registerProposeNt } from './propose-nt.js';
|
|
17
18
|
import { registerLookupAuthorization } from './lookup-authorization.js';
|
|
19
|
+
import { registerNtFinalGestor } from './nt-final-gestor.js';
|
|
18
20
|
export function registerAllTools(server, client) {
|
|
19
21
|
registerSearchDocs(server, client);
|
|
20
22
|
registerReadDoc(server, client);
|
|
@@ -31,6 +33,8 @@ export function registerAllTools(server, client) {
|
|
|
31
33
|
registerApplyCompletion(server, client);
|
|
32
34
|
registerProposeAuthorizations(server, client);
|
|
33
35
|
registerApplyAuthorization(server, client);
|
|
36
|
+
registerLinkGestorAuthorizations(server, client);
|
|
34
37
|
registerProposeNt(server, client);
|
|
35
38
|
registerLookupAuthorization(server, client);
|
|
39
|
+
registerNtFinalGestor(server, client);
|
|
36
40
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { resolveElevation } from './elevation.js';
|
|
3
|
+
export function registerLinkGestorAuthorizations(server, client) {
|
|
4
|
+
server.tool('link_gestor_authorizations', 'Backfill the missing gestor authorization LINK on a year\'s waste removals — the fix for the ' +
|
|
5
|
+
'register\'s `autorizacion-gestor` gap. The importer created the gestor EntidadCentroLer rows ' +
|
|
6
|
+
'with an empty auth_id; this links each to the center\'s already-loaded gestor authorization ' +
|
|
7
|
+
'(G0x) using the SAME rule the register enforces (destino + hazard class). Without confirm it ' +
|
|
8
|
+
'is a DRY-RUN (shows what would be linked and what can\'t resolve); with confirm=true it writes ' +
|
|
9
|
+
'under your active operator elevation (company derived from it). Rows whose center has no ' +
|
|
10
|
+
'matching destino authorization are reported, never forced — load the G0x with ' +
|
|
11
|
+
'apply_authorization first. Read the dry-run before confirming.', {
|
|
12
|
+
year: z.number().describe('Calendar year, e.g. 2025'),
|
|
13
|
+
confirm: z.boolean().optional().describe('true to write; omit/false for a dry-run'),
|
|
14
|
+
}, async ({ year, confirm }) => {
|
|
15
|
+
const elev = await resolveElevation(client);
|
|
16
|
+
if ('content' in elev)
|
|
17
|
+
return elev;
|
|
18
|
+
const body = { year, confirm: confirm === true };
|
|
19
|
+
const res = confirm === true
|
|
20
|
+
? await client.post('/mwm/retirada/link-gestor-authorizations', body)
|
|
21
|
+
: await client.post('/mwm/retirada/link-gestor-authorizations', body);
|
|
22
|
+
if (confirm !== true) {
|
|
23
|
+
const d = res;
|
|
24
|
+
const sampleR = d.resoluble.slice(0, 15).map(r => ` - ${r.clientId}/${r.centerId} · LER ${r.codigoLer} → \`${r.authId}\``).join('\n');
|
|
25
|
+
const sampleU = d.unresoluble.slice(0, 15).map(u => ` - ${u.clientId}/${u.centerId} · LER ${u.codigoLer}: ${u.reason}`).join('\n');
|
|
26
|
+
const text = `# Dry-run · vincular autorización de gestor · ${d.year}\n` +
|
|
27
|
+
`${d.total} fila(s) EntidadCentroLer sin auth_id · ${d.resoluble.length} resolubles · ${d.unresoluble.length} sin resolver.\n\n` +
|
|
28
|
+
(d.resoluble.length ? `## Se vincularían (authId)\n${sampleR}${d.resoluble.length > 15 ? `\n … +${d.resoluble.length - 15} más` : ''}\n\n` : '') +
|
|
29
|
+
(d.unresoluble.length ? `## No resolubles (falta cargar la G0x)\n${sampleU}${d.unresoluble.length > 15 ? `\n … +${d.unresoluble.length - 15} más` : ''}\n\n` : '') +
|
|
30
|
+
`_Ejecuta con confirm=true para aplicar las ${d.resoluble.length} resolubles._`;
|
|
31
|
+
return { content: [{ type: 'text', text }] };
|
|
32
|
+
}
|
|
33
|
+
const r = res;
|
|
34
|
+
const failLines = r.failed.length ? '\n\n### Fallidas\n' + r.failed.map(f => `- ${f.clientId}/${f.centerId} LER ${f.codigoLer}: ${f.error}`).join('\n') : '';
|
|
35
|
+
const text = `# Aplicado · vincular autorización de gestor · ${r.year}\n` +
|
|
36
|
+
`${r.applied} vinculada(s)${r.skipped ? `, ${r.skipped} ya poblada(s)` : ''}${r.failed.length ? `, ${r.failed.length} con error` : ''}, bajo tu elevación.` +
|
|
37
|
+
(r.unresoluble.length ? `\n${r.unresoluble.length} sin resolver (falta cargar su G0x — usa apply_authorization).` : '') +
|
|
38
|
+
failLines +
|
|
39
|
+
`\n\n_Recuerda des-elevarte desde el portal web con un motivo cuando termines._`;
|
|
40
|
+
return { content: [{ type: 'text', text }] };
|
|
41
|
+
});
|
|
42
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
|
+
import type { ZincAppClient } from '../client.js';
|
|
3
|
+
/** Límite de tamaño de PDF, alineado con el importer (LapazImportEndpoint.kt:62 → 25 MB/PDF). */
|
|
4
|
+
export declare const MAX_PDF_BYTES: number;
|
|
5
|
+
/** Valida que los bytes son un PDF (`%PDF` magic) y no exceden el límite. Puro y exportado para test. */
|
|
6
|
+
export declare function validatePdf(bytes: Buffer): {
|
|
7
|
+
ok: true;
|
|
8
|
+
} | {
|
|
9
|
+
ok: false;
|
|
10
|
+
error: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function registerNtFinalGestor(server: McpServer, client: ZincAppClient): void;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { resolveElevation } from './elevation.js';
|
|
4
|
+
/** Límite de tamaño de PDF, alineado con el importer (LapazImportEndpoint.kt:62 → 25 MB/PDF). */
|
|
5
|
+
export const MAX_PDF_BYTES = 25 * 1024 * 1024;
|
|
6
|
+
/** Valida que los bytes son un PDF (`%PDF` magic) y no exceden el límite. Puro y exportado para test. */
|
|
7
|
+
export function validatePdf(bytes) {
|
|
8
|
+
if (bytes.length < 5 || bytes.subarray(0, 5).toString('latin1') !== '%PDF-') {
|
|
9
|
+
return { ok: false, error: 'El fichero no es un PDF (no empieza por "%PDF-").' };
|
|
10
|
+
}
|
|
11
|
+
if (bytes.length > MAX_PDF_BYTES) {
|
|
12
|
+
return { ok: false, error: `El PDF es demasiado grande (${bytes.length} bytes; máximo ${MAX_PDF_BYTES}, 25 MB).` };
|
|
13
|
+
}
|
|
14
|
+
return { ok: true };
|
|
15
|
+
}
|
|
16
|
+
/** Resuelve el base64 del PDF desde EXACTAMENTE uno de pdfPath | pdfBase64. */
|
|
17
|
+
function resolvePdfBase64(pdfPath, pdfBase64) {
|
|
18
|
+
const hasPath = typeof pdfPath === 'string' && pdfPath.length > 0;
|
|
19
|
+
const hasB64 = typeof pdfBase64 === 'string' && pdfBase64.length > 0;
|
|
20
|
+
if (hasPath === hasB64) {
|
|
21
|
+
return { ok: false, error: 'Aporta EXACTAMENTE uno de pdfPath o pdfBase64 (no ambos, no ninguno).' };
|
|
22
|
+
}
|
|
23
|
+
let bytes;
|
|
24
|
+
if (hasPath) {
|
|
25
|
+
try {
|
|
26
|
+
bytes = readFileSync(pdfPath);
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
return { ok: false, error: `No pude leer el PDF en '${pdfPath}': ${e.message}` };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
try {
|
|
34
|
+
bytes = Buffer.from(pdfBase64, 'base64');
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
return { ok: false, error: `pdfBase64 no es base64 válido: ${e.message}` };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const v = validatePdf(bytes);
|
|
41
|
+
if (!v.ok)
|
|
42
|
+
return v;
|
|
43
|
+
return { ok: true, base64: bytes.toString('base64') };
|
|
44
|
+
}
|
|
45
|
+
const errorContent = (text) => ({ content: [{ type: 'text', text }] });
|
|
46
|
+
export function registerNtFinalGestor(server, client) {
|
|
47
|
+
server.tool('preview_nt_final_gestor', 'DRY-RUN: extrae por IA el "Gestor final para la operación Dxx" del PDF de una Notificación Previa ' +
|
|
48
|
+
'(NT), lo resuelve contra las entidades/centros de tu tenant, valida las invariantes E3L ' +
|
|
49
|
+
'(operación en Table2Type + autorización por LER del centro), y devuelve una PROPUESTA opaca ' +
|
|
50
|
+
'(con proposalId) SIN escribir nada. Aporta el PDF con pdfPath (ruta local) o pdfBase64 ' +
|
|
51
|
+
'(exactamente uno). Revisa la propuesta y, si es correcta, aplícala con apply_nt_final_gestor. ' +
|
|
52
|
+
'Corre bajo tu elevación de operador (company derivada de ella).', {
|
|
53
|
+
ntNumero: z.string().describe('El número de la NT objetivo (p.ej. "NT30280008283420220064303")'),
|
|
54
|
+
pdfPath: z.string().optional().describe('Ruta local al PDF de la NT (alternativa a pdfBase64)'),
|
|
55
|
+
pdfBase64: z.string().optional().describe('El PDF de la NT en base64 (alternativa a pdfPath)'),
|
|
56
|
+
}, async ({ ntNumero, pdfPath, pdfBase64 }) => {
|
|
57
|
+
const pdf = resolvePdfBase64(pdfPath, pdfBase64);
|
|
58
|
+
if (!pdf.ok)
|
|
59
|
+
return errorContent(`# Error\n${pdf.error}`);
|
|
60
|
+
const elev = await resolveElevation(client);
|
|
61
|
+
if ('content' in elev)
|
|
62
|
+
return elev;
|
|
63
|
+
const p = await client.post('/mwm/retirada/preview-nt-final-gestor', { ntNumero, pdfBase64: pdf.base64 });
|
|
64
|
+
if (!p.resoluble) {
|
|
65
|
+
return errorContent(`# Propuesta · NT ${p.ntNumero} — NO RESOLUBLE\n\n` +
|
|
66
|
+
`**Razón:** ${p.razon ?? 'desconocida'}\n\n` +
|
|
67
|
+
`Terna actual en la NT: ${fmtTriple(p.ternaActual)}. No se ha persistido nada.`);
|
|
68
|
+
}
|
|
69
|
+
const text = `# Propuesta · NT ${p.ntNumero}\n\n` +
|
|
70
|
+
`- **proposalId:** \`${p.proposalId}\`\n` +
|
|
71
|
+
`- **Nº de NT en el documento:** ${p.ntNumeroDocumento ?? '—'}\n` +
|
|
72
|
+
`- **Gestor final propuesto:** ${fmtTriple(p.gestorFinal)}\n` +
|
|
73
|
+
`- **Terna actual en la NT:** ${fmtTriple(p.ternaActual)}\n\n` +
|
|
74
|
+
`Si es correcto, aplica con \`apply_nt_final_gestor({ proposalId: "${p.proposalId}" })\`. ` +
|
|
75
|
+
`La propuesta caduca (TTL) — aplícala pronto.`;
|
|
76
|
+
return { content: [{ type: 'text', text }] };
|
|
77
|
+
});
|
|
78
|
+
server.tool('apply_nt_final_gestor', 'Aplica una propuesta de gestor final previamente revisada (por proposalId de preview_nt_final_gestor). ' +
|
|
79
|
+
'NO re-ejecuta la IA ni re-transporta el PDF. Escribe la terna (clientId, centerId, operación) en ' +
|
|
80
|
+
'la NT bajo tu elevación de operador, por el framework (hooks + auditoría), con regla ' +
|
|
81
|
+
'complete-what\'s-missing (si la NT ya tiene un gestor final distinto → conflicto, no se ' +
|
|
82
|
+
'sobrescribe). Sólo aplica propuestas que hayas verificado.', {
|
|
83
|
+
proposalId: z.string().describe('El proposalId devuelto por preview_nt_final_gestor'),
|
|
84
|
+
}, async ({ proposalId }) => {
|
|
85
|
+
const elev = await resolveElevation(client);
|
|
86
|
+
if ('content' in elev)
|
|
87
|
+
return elev;
|
|
88
|
+
const r = await client.post('/mwm/retirada/apply-nt-final-gestor', { proposalId });
|
|
89
|
+
const titulo = r.outcome === 'applied'
|
|
90
|
+
? `# Aplicado · NT ${r.ntNumero}`
|
|
91
|
+
: `# Sin cambios · NT ${r.ntNumero}`;
|
|
92
|
+
const text = `${titulo}\n\n` +
|
|
93
|
+
`- **Resultado:** ${r.outcome}\n` +
|
|
94
|
+
`- **Gestor final:** ${fmtTriple(r.gestorFinal)}\n\n` +
|
|
95
|
+
`_Recuerda des-elevarte desde el portal web con un motivo cuando termines._`;
|
|
96
|
+
return { content: [{ type: 'text', text }] };
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function fmtTriple(t) {
|
|
100
|
+
if (!t || (t.clientId == null && t.centerId == null && t.operacion == null))
|
|
101
|
+
return '(vacía)';
|
|
102
|
+
return `entidad ${t.clientId ?? '—'} / centro ${t.centerId ?? '—'} · operación ${t.operacion ?? '—'}`;
|
|
103
|
+
}
|
package/package.json
CHANGED