fix(ai) - optimize crud tools (#21133)

- **Add delete many**, `delete_many_{object}` added alongside the
existing `delete_one_{object}`.
- **Uniformize naming**, crud module, type names, and MCP helper
constants renamed for consistency.
- **Optimize tool schema (learn phase)**
  - `find_many(_companies)`: **7 158 → 2 700 tokens**
  - `find_one(_company)`: **280 → 126 tokens**
  -  ....
- Main mechanism: `reused: 'ref'` (line 7 of
`to-tool-json-schema.util.ts`). Zod walks the schema tree, tracks which
Zod schema instances appear more than once, and emits each reused
instance exactly once in `$defs`, replacing all subsequent occurrences
with a `$ref`. Works because filter and value schemas are now extracted
as shared objects.

- **Optimize system prompt (tool catalog)**, DATABASE_CRUD section
restructured to list operation patterns (`find_many_{object}`, …) once +
objects once, instead of the full N×M cross-product of tool names.
- **Optimize execute_tool**, shared record-properties schema (same
`$defs` deduplication applies at call time); introduced `upsert_many`;
added `selectedFields` to `find_*` so the agent only fetches the fields
it needs.
This commit is contained in:
Etienne
2026-06-03 19:57:40 +02:00
committed by GitHub
parent 50c9b68e81
commit 15eaabdbc1
40 changed files with 1610 additions and 633 deletions
@@ -16,7 +16,7 @@ import {
const fileItemSchema = z
.object({
fileId: z.string().uuidv4(),
fileId: z.uuidv4(),
label: z.string(),
})
.strict();
@@ -273,7 +273,7 @@ describe('McpProtocolService', () => {
method: 'tools/call',
params: {
name: 'execute_tool',
arguments: { toolName: 'find_companies', arguments: {} },
arguments: { toolName: 'find_many_companies', arguments: {} },
},
id: '123',
};
@@ -19,8 +19,8 @@ export const buildMcpServerInstructions = (
``,
...(skillNames ? [`Available skills: ${skillNames}.`, ``] : []),
`CRUD tool name grammar — construct names directly without prior discovery:`,
` Read: find_{objects} | find_one_{object} | group_by_{objects}`,
` Write: create_{object} | create_many_{objects} | update_{object} | update_many_{objects} | delete_{object}`,
` Read: find_many_{objects} | find_one_{object} | group_by_{objects}`,
` Write: create_one_{object} | create_many_{objects} | update_one_{object} | update_many_{objects} | delete_one_{object} | delete_many_{objects} | upsert_many_{objects}. Use upsert_many_{objects} instead of update_many_{objects} when each record has its own individual data.`,
``,
`Non-CRUD tools — use learn_tools for schemas:`,
` ACTION: http_request | send_email | draft_email | navigate_app | code_interpreter | search_help_center`,
@@ -39,8 +39,8 @@ export const buildMcpServerInstructions = (
` ⚠️ Never call workflow, dashboard, or metadata tools without loading their skill first.`,
``,
`Route by intent:`,
` Named entity ("Acme company") → find_{objects} to resolve id first, then operate on id`,
` Retrieve records → find_{objects} (default limit: 10, always report total count)`,
` Named entity ("Acme company") → find_many_{objects} to resolve id first, then operate on id`,
` Retrieve records → find_many_{objects} (default limit: 10, always report total count)`,
` Single record by id → find_one_{object}`,
` Analytics / grouped metrics → group_by_{objects} (COUNT, SUM, AVG, MIN, MAX)`,
` Multiple metrics → run parallel group_by calls, merge results`,