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.
MCP: explicit _operation for ambiguous upsert tools
Branch: fix/mcp-upsert-operation-flag
Problem
Carbon's upsert* service functions pick insert vs update by testing
if ("createdBy" in payload). The web app is safe because the "new" route and the
"edit" route each hard-code their intent (createdBy: userId vs updatedBy: userId).
MCP has one tool per service function, so it cannot know the intent. Today
enrichWithAuthContext (apps/erp/app/routes/api+/mcp+/lib/direct-executor.ts:57)
stamps createdBy onto every payload for any tool whose injectAuth contains it —
262 of 1431 tools. Consequence: an edit issued through MCP always takes the insert
branch, so it either duplicates or fails on the primary key instead of updating.
Rejected alternatives (and why):
- Decide on
idpresence — breaks entities whose id is a human-typed code.partValidator(apps/erp/app/modules/items/items.models.ts:824) requiresidon create, soitems_upsertPartwould become a no-op update. Confirmed create call sites that pass bothcreatedByand anid:apps/erp/app/routes/api+/sales-rfq.$rfqId.map-lines.ts:99,apps/erp/app/routes/api+/purchase-invoice.$invoiceId.map-lines.ts:94,apps/erp/app/routes/x+/part+/new.tsx:36,x+/tool+/new.tsx:35,x+/material+/new.tsx:35,x+/consumable+/new.tsx:35,x+/service+/new.tsx:35,x+/items+/methods+/operation.new.tsx:41. - Decide on
createdAtpresence — only works if the caller echoes back a whole row it read; a model editing one field won't. - A new marker column / field on the service payload — would require touching all affected service signatures and stripping the field before every write.
Approach
Add an MCP-only argument _operation: "create" | "update", required, on the tools
that genuinely branch. The executor reads it, removes it from the payload, and stamps
createdBy or updatedBy accordingly. No service, validator, or table changes.
Scope — which tools get the flag
Only tools whose service body literally contains if ("createdBy" in …). Measured
today: 88 tools (of the 191 upsert*-named tools, of the 262 createdBy-injecting
tools). Detection is done in the generator by inspecting the parsed function body, so
the set stays correct as services change — it is not a hand-maintained list.
Deliberately excluded:
- The 71
insert|create|copy|generate|duplicate|addtools — always create. Asking the model "create or update?" only introduces a wrong answer that could skipcreatedByand hit a NOT NULL violation. - The ~103
upsert*tools that decide some other way (Postgres.upsert(),"id" in, or always-insert). They still needcreatedBy; changing their injection could break writes that work today.
Changes
1. scripts/generate-mcp.ts
- New helper
usesCreatedByDiscriminator(serviceContent, funcName): locate the function body (reusefindMatchingBrace) and test for"createdBy" in. - New
ToolMetadatafieldoperationArg?: true, set when that helper is true andinjectAuthincludescreatedBy. - When
operationArgis set, add to the emitted schema:
and add_operation: { type: "string", enum: ["create", "update"], description: "Whether this call creates a new record or updates an existing one." }"_operation"to the schema'srequiredarray. - Regenerate
apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json(npx tsx scripts/generate-mcp.ts). Never hand-edit that file.
2. apps/erp/app/routes/api+/mcp+/lib/direct-executor.ts
- Immediately after
normalizedArgsis derived, lift the flag out and drop it from the object that will be passed on:
then build the working args withoutconst operation = normalizedArgs?._operation as "create" | "update" | undefined;_operation. This must happen before the branches at lines ~164 and ~187, which hand the args object to the service as-is — a leftover_operationwould reach Supabase as a nonexistent column. - If the tool has
operationArgandoperationis missing or not one of the two values, return{ success: false, error: "..." }without calling the service. enrichWithAuthContext(value, context, fields, operation):operation === "update"→ stampupdatedBy(andcompanyId), nevercreatedBy.operation === "create"or tool has nooperationArg→ today's behaviour exactly.- Keep the existing
!("createdBy" in enriched)guard so a caller-supplied value wins.
3. Nested-payload note
_operation arrives at the top level of the tool arguments, while the payload may be
nested under a parameter name (line ~168). Lifting it out at the top, as above, covers
both the nested and the flat shapes.
Verification
npx tsx scripts/generate-mcp.ts→ diff shows_operationon ~88 tools and on no others;totalToolsunchanged at 1431.pnpm exec turbo run typecheck --filter=erp(package is namederp, not@carbon/erp).pnpm exec biome checkon the touched files.- Manual MCP calls against a running dev stack:
sales_upsertCustomerStatuswith_operation: "update"+ an existing id → row is updated,createdByunchanged,updatedByset.sales_upsertCustomerStatuswith_operation: "create"→ new row as before.items_upsertPartwith_operation: "create"and a human-typedid→ still creates.- A
..._insert*tool → unchanged, no_operationin its schema.
Out of scope
- Changing the
if ("createdBy" in …)convention in the 88 services. - An existence lookup as a cross-check on a wrong
_operation. Worth revisiting if the model gets it wrong in practice; a wrong value fails loudly today (PK conflict or a zero-row update), which is already better than the silent misroute.