Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
Fix Carbon MCP quote-setup tool bugs
Reported by Anthan (via Brad) while using the Carbon MCP for quote setup. Nine items; #1 is client-side (not fixable here). The rest are generator, executor, classification, and service-behavior bugs. One PR.
Ground truth: scripts/generate-mcp.ts parses {module}.service.ts signatures
textually → tool-metadata.json; api+/mcp+/lib/direct-executor.ts runs tools
positionally and stamps auth via enrichWithAuthContext.
Root causes (confirmed)
- #2
quoteLinePricesshowstype:objectnotarray.buildToolSchema's multi-param branch special-casesstartsWith("{")→parseInlineObjectType, which swallows the trailing[].typeToJsonSchemaalready handles{...}[](array check precedes object check) — the branch just bypasses it. - #5
upsertQuoteLinedropsquantity(+ every field after the firsterrorMap). The depth counters (splitAtTopLevel/findTopLevelColon/splitObjectFields) treat the>in a=>arrow as a generic close, driving depth negative so no further top-level commas split. Affects EVERY validator with an arrow (errorMap, refine) before later fields — many tools. (externalNotes/internalNotesare not inquoteLineValidatorat all → out of scope; documented as a follow-up.) - #3
createdByNOT NULL but never injected into array elements.enrichWithAuthContextreturns arrays untouched (Array.isArray → return). - #9
items_upsertServiceparam is opaque{}. Type isapplyStorageAndShelfLifeRefines(itemValidator.merge(z.object({...})))— not a barez.object(, soparseValidatorFieldsreturns null → fallback{}. - #8 delete-and-reinsert classified WRITE (name-based
upsert*→WRITE). - #7
leadTime/discount/shippingsilently ignored: carry-overexisting?.X ?? p.X(recalc route depends on this to preserve stored values). - #4
quoteLine.quantity[]not synced to the rewritten price rows. - #6 new break defaults to
priceSource:"system"at raw unitPrice.
Changes
A. Generator (scripts/generate-mcp.ts)
- Depth counters ignore
=>: whench === ">"and prev char is=, don't decrement. Apply insplitAtTopLevel,findTopLevelColon,splitObjectFields. (Fixes #5 broadly.) buildToolSchemamulti-param branch: replace thestartsWith("{")special case withtypeToJsonSchema(param.typeStr)so{...}[]→ array. Do the same in the single-param inline branch, but when the resolved schema is an array wrap it underparam.name(can't flatten an array into top-level props). (Fixes #2.)- Add
CLASSIFICATION_OVERRIDES: Record<string,"READ"|"WRITE"|"DESTRUCTIVE">(mirrorsINJECT_AUTH_OVERRIDES);sales_upsertQuoteLinePrices → DESTRUCTIVE. Also addINJECT_AUTH_OVERRIDES['sales_upsertQuoteLinePrices'] = ['companyId','createdBy']so DESTRUCTIVE classification doesn't strip the per-row createdBy injection. (Fixes #8.) - Add
SCHEMA_OVERRIDES: Record<string, object>for tools whose param type the parser can't resolve; authoritems_upsertServicewith the real fields +_operation, and a description note:idis the Service ID (used as readableId); never passreadableIdWithRevision(generated column). (Fixes #9.) DESCRIPTION_OVERRIDESfor the two pricing tools describing destructive-by- omission + carry-over + explicit-set semantics.
B. Executor (api+/mcp+/lib/direct-executor.ts)
enrichWithAuthContext: whenvalueis an array, map each object element, injecting ONLYcompanyId+createdBy(neverupdatedBy— avoids spreading a column the element table may not have). Non-object elements pass through. (Fixes #3.)
C. Service (sales.service.ts rewriteQuoteLinePrices + input type)
- Make
leadTime/discountPercent/shippingCost/quantity... keep required except makeleadTime/discountPercent/shippingCostoptional onQuoteLinePriceInput; change carry-over top.X ?? existing?.X ?? default(explicit wins, omitted → carry). (#7) priceSource: p.priceSource ?? existing?.priceSource ?? "manual"(was"system") — a hand-set API price with no source is manual. Recalc still sets"system"explicitly. (#6)- After an EXPLICIT
quoteLinePricesrewrite (not the precision rebuild, not the empty no-op), syncquoteLine.quantityto the sorted distinct quantities of the inserted rows, inside the same trx. (#4)
D. Caller (x+/quote+/$quoteId.$lineId.recalculate-price.tsx)
- Stop passing
discountPercent:0, leadTime:0; omit them so carry-over preserves the stored values (now that explicit values win). Quantities unchanged → #4 sync is a no-op here.
E. Regenerate + verify
npx tsx scripts/generate-mcp.ts; eyeball the 3 tools; sanity-check that the=>fix only ADDED fields (no tool lost properties); totalTools unchanged.
F. Tests + docs
sales.utils/service test: explicit leadTime/discount wins; omitted carries; quantity sync; priceSource default manual. Generator unit checks for the=>and[]cases if a harness exists (else rely on metadata diff).- Sync
quote-discount-system.md, salesAGENTS.md,mcp-tools-reference.md.
Out of scope (documented)
- #1 MCP eviction (client/transport).
- #5 notes fields (not in the validator; separate rich-text write path).
Verify
pnpm exec turbo run typecheck --filter=erpcd apps/erp && pnpm exec vitest run app/modules/sales- generated
tool-metadata.jsondiff review