44 lines
8.2 KiB
TypeScript
44 lines
8.2 KiB
TypeScript
import type {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
import type {Principal} from '@/lib/purchasing/types';
|
|
import {registerPurchasingTool} from './tool-contract';
|
|
import {CreatePurchaseOrderSchema,UpdatePurchaseOrderSchema,AddPurchaseOrderItemSchema,UpdatePurchaseOrderItemSchema,VerifyPurchaseOrderSchema,mutatePurchaseOrder} from '@/lib/purchasing/purchase-orders';
|
|
import {SupplierGuidanceSchema,mutateSupplierGuidance} from '@/lib/purchasing/supplier-guidance';
|
|
import {CandidateOperationSchema,EntityResolutionSchema,ItemResolutionSchema,resolveDelivery,processDeliveryCandidate} from '@/lib/purchasing/delivery-matching';
|
|
import {McpPostDeliveryLineSchema,ReceiptCorrectionSchema,postDeliveryLine,correctDeliveryReceipt} from '@/lib/purchasing/delivery-receipts';
|
|
import {ProcessBatchSchema,enqueueDeliveryJob} from '@/lib/purchasing/jobs';
|
|
import {setDeliveryVerification} from '@/lib/purchasing/verification';
|
|
import {PostDeliverySchema,postDelivery} from '@/lib/purchasing/delivery-posting';
|
|
import {UploadPurchasingSchema,uploadPurchasingDocument} from '@/lib/purchasing/uploads';
|
|
import {CreateDeliveryCandidateSchema,UpdateDeliveryCandidateSchema,DeliveryGroupingSchema,mutateDeliveryCandidate} from '@/lib/purchasing/delivery-candidates';
|
|
import {SetAsideSchema,RestoreSchema,setAsideDocument,restoreDocument} from '@/lib/purchasing/document-disposition';
|
|
import {EditDocumentSchema,editDocument} from '@/lib/purchasing/document-edit';
|
|
import {RequestSiteDecisionSchema,AnswerSiteDecisionSchema,requestSiteDecision,answerSiteDecision} from '@/lib/purchasing/site-decision';
|
|
import {MoveToPurchaseOrdersSchema,moveToPurchaseOrders} from '@/lib/purchasing/move-to-purchase-orders';
|
|
import {RequestDeletionSchema,requestDeletion} from '@/lib/purchasing/deletion-requests';
|
|
import {ReassignReceiptSchema,reassignReceipt} from '@/lib/purchasing/receipt-reassign';
|
|
import {MergeSuppliersSchema,mergeSuppliers} from '@/lib/purchasing/supplier-merge';
|
|
export function registerPurchasingWrites(server:McpServer,principal:Principal):void {
|
|
for(const name of ['upload_purchase_order','upload_delivery_documents'] as const)registerPurchasingTool(server,principal,{name,description:'Upload one immutable PDF or image source; retry using the same operationId.',schema:UploadPurchasingSchema,permission:'write',handler:(ctx,input)=>uploadPurchasingDocument(ctx,name,input)});
|
|
for(const [name,schema] of [['create_delivery_candidate',CreateDeliveryCandidateSchema],['update_delivery_candidate',UpdateDeliveryCandidateSchema],['set_delivery_grouping',DeliveryGroupingSchema]] as const)registerPurchasingTool(server,principal,{name,description:'Edit receiving candidates or grouping under the batch lease, preserving posted evidence.',schema,permission:'write',handler:(ctx,input)=>mutateDeliveryCandidate(ctx,name,input)});
|
|
for(const [name,schema] of [['resolve_delivery_entities',EntityResolutionSchema],['resolve_delivery_item',ItemResolutionSchema]] as const)registerPurchasingTool(server,principal,{name,description:'Resolve receiving entities or PO lines with ownership checks; does not book a receipt.',schema,permission:'write',handler:(ctx,input)=>resolveDelivery(ctx,name,input)});
|
|
for(const [name,description,schema,handler] of [['set_aside_document','Set a delivery document aside with a typed reason (quotation, duplicate, purchase_order_document, not_a_delivery, wrong_site, other). Reversible with restore_document.',SetAsideSchema,setAsideDocument],['restore_document','Restore a document that was set aside.',RestoreSchema,restoreDocument]] as const)registerPurchasingTool(server,principal,{name,description,schema,permission:'write',handler});
|
|
registerPurchasingTool(server,principal,{name:'edit_document',description:"Correct a delivery document's number, identity references, order references, type, date, supplier or site. Clears the preview; process the slip again afterwards.",schema:EditDocumentSchema,permission:'write',handler:editDocument});
|
|
for(const [name,description,schema,handler] of [['request_site_decision','Ask the site a plain-language question about a delivery document you cannot decide (for example an unclear quantity or a quantity beyond the PO). The document waits in Needs attention with your question until someone answers it.',RequestSiteDecisionSchema,requestSiteDecision],['answer_site_decision',"Record the site's answer to an open site-decision question.",AnswerSiteDecisionSchema,answerSiteDecision]] as const)registerPurchasingTool(server,principal,{name,description,schema,permission:'write',handler});
|
|
registerPurchasingTool(server,principal,{name:'move_to_purchase_orders',description:'Move a purchase-order photo that was uploaded as a delivery document into purchase orders, optionally with its lines, and set the delivery document aside.',schema:MoveToPurchaseOrdersSchema,permission:'write',handler:moveToPurchaseOrders});
|
|
for(const [name,description,schema,handler] of [['process_delivery_candidate','Generate a fresh matching preview without posting.',CandidateOperationSchema,processDeliveryCandidate],['post_delivery_line','Post one allocation with explicit confirmation and a current preview hash.',McpPostDeliveryLineSchema,postDeliveryLine],['correct_delivery_receipt','Correct an eligible legacy receipt with expected quantities and audit history.',ReceiptCorrectionSchema,correctDeliveryReceipt]] as const)registerPurchasingTool(server,principal,{name,description,schema,permission:'write',handler});
|
|
registerPurchasingTool(server,principal,{name:'post_delivery',description:'Post a whole delivery slip in one call. Plan every delivered slip line by its line number (group several slip lines for one PO line as a kit); NIL lines close automatically. Commits line by line and is safe to retry with the same operationId.',schema:PostDeliverySchema,permission:'write',handler:postDelivery});
|
|
registerPurchasingTool(server,principal,{name:'process_delivery_batch',description:'Queue durable source reading and matching; no automatic receipt posting. Poll get_mcp_operation.',schema:ProcessBatchSchema,permission:'write',handler:enqueueDeliveryJob});
|
|
const orderTools=[
|
|
['create_purchase_order','Create a manual order and validated lines atomically.',CreatePurchaseOrderSchema],
|
|
['update_purchase_order','Update permitted header fields of an unverified order.',UpdatePurchaseOrderSchema],
|
|
['add_purchase_order_item','Add a line to an unverified order and recompute totals.',AddPurchaseOrderItemSchema],
|
|
['update_purchase_order_item','Edit an order line, validating membership and totals.',UpdatePurchaseOrderItemSchema],
|
|
['set_purchase_order_verification','Explicitly verify or unverify an order.',VerifyPurchaseOrderSchema],
|
|
] as const;
|
|
for(const [name,description,schema] of orderTools)registerPurchasingTool(server,principal,{name,description,schema,permission:'write',handler:(ctx,input)=>mutatePurchaseOrder(ctx,name,input)});
|
|
registerPurchasingTool(server,principal,{name:'set_delivery_verification',description:'Explicitly verify or unverify a delivery receipt.',schema:VerifyPurchaseOrderSchema,permission:'write',handler:setDeliveryVerification});
|
|
registerPurchasingTool(server,principal,{name:'merge_suppliers',description:'Merge a duplicate supplier into the one to keep: repoints its orders, documents and aliases, renames it, and flags it for deletion.',schema:MergeSuppliersSchema,permission:'write',handler:mergeSuppliers});
|
|
registerPurchasingTool(server,principal,{name:'reassign_receipt',description:"Fix a wrong booking: move a posted receipt's lines to the right PO line, or the whole receipt to the right PO (same site and supplier), and/or correct the received quantity. Give the quantity you expect is booked now so a concurrent change is caught.",schema:ReassignReceiptSchema,permission:'write',handler:reassignReceipt});
|
|
registerPurchasingTool(server,principal,{name:'request_deletion',description:'Flag a record that should be deleted, with the reason. Agents never delete; a person reviews the flag and deletes it.',schema:RequestDeletionSchema,permission:'write',handler:requestDeletion});
|
|
registerPurchasingTool(server,principal,{name:'update_supplier_guidance',description:'Create or update supplier reconciliation guidance, preserving other rules.',schema:SupplierGuidanceSchema,permission:'write',handler:mutateSupplierGuidance});
|
|
}
|