The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(mcp): make the discovery guidance consistent across every surface (#26558)
Etienne7a38fad8cde2
diff --git a/packages/twenty-docs/user-guide/ai/capabilities/mcp.mdx b/packages/twenty-docs/user-guide/ai/capabilities/mcp.mdxindex 9f770fc8da8..12e61e8bc2a 100644--- a/packages/twenty-docs/user-guide/ai/capabilities/mcp.mdx+++ b/packages/twenty-docs/user-guide/ai/capabilities/mcp.mdx@@ -104,6 +104,10 @@ Once connected, the MCP server exposes tools that mirror the Twenty API. The recYou don't need to remember tool names. Ask your AI assistant what it can do and it will call `learn_tools` automatically.+Record operation names follow a grammar, so an assistant can build them without a discovery call: `find_many_companies`, `find_one_person`, `update_one_opportunity`. If a name is wrong, `learn_tools` reports it under `notFound` along with the closest matching names.++For anything else, `get_tool_catalog` browses the remaining tools one category at a time. It is a fallback: an unfiltered call returns hundreds of operations and most assistants will truncate the response.+## PermissionsMCP connections inherit the permissions of the authenticated user (OAuth) or the role assigned to the API key. To restrict what the MCP server can do:diff --git a/packages/twenty-server/src/engine/api/mcp/utils/__tests__/build-mcp-server-instructions.util.spec.ts b/packages/twenty-server/src/engine/api/mcp/utils/__tests__/build-mcp-server-instructions.util.spec.tsindex 65224f8b17a..d8ecbfc2a28 100644--- a/packages/twenty-server/src/engine/api/mcp/utils/__tests__/build-mcp-server-instructions.util.spec.ts+++ b/packages/twenty-server/src/engine/api/mcp/utils/__tests__/build-mcp-server-instructions.util.spec.ts@@ -1,4 +1,12 @@+import { LIST_OBJECT_METADATA_NAMES_TOOL_NAME } from 'src/engine/api/mcp/tools/list-object-metadata-names.tool';+import { LIST_SKILLS_TOOL_NAME } from 'src/engine/api/mcp/tools/list-skills.tool';import { buildMcpServerInstructions } from 'src/engine/api/mcp/utils/build-mcp-server-instructions.util';+import {+ EXECUTE_TOOL_TOOL_NAME,+ LEARN_TOOLS_TOOL_NAME,+ LOAD_SKILL_TOOL_NAME,+} from 'src/engine/core-modules/tool-provider/tools';+import { GET_TOOL_CATALOG_TOOL_NAME } from 'src/engine/core-modules/tool-provider/tools/get-tool-catalog.tool';const getActionLine = (instructions: string): string =>instructions.split('\n').find((line) => line.includes('ACTION:')) ?? '';@@ -49,6 +57,41 @@ describe('buildMcpServerInstructions', () => {expect(withoutHttp).not.toContain('http_request is ONLY for external');});+ it('should document every meta-tool the MCP server exposes', () => {+ const instructions = buildMcpServerInstructions({+ objectNames: 'companies',+ actionToolNames: ['send_email'],+ });++ for (const toolName of [+ EXECUTE_TOOL_TOOL_NAME,+ LEARN_TOOLS_TOOL_NAME,+ LOAD_SKILL_TOOL_NAME,+ LIST_OBJECT_METADATA_NAMES_TOOL_NAME,+ LIST_SKILLS_TOOL_NAME,+ GET_TOOL_CATALOG_TOOL_NAME,+ ]) {+ expect(instructions).toContain(toolName);+ }+ });++ it('should route an unverified tool name to learn_tools, not to the catalog', () => {+ const instructions = buildMcpServerInstructions({+ objectNames: 'companies',+ actionToolNames: ['send_email'],+ });++ expect(instructions).toContain(+ 'unknown names come back under notFound with the closest matching names',+ );+ expect(instructions).toContain(+ `Never call ${GET_TOOL_CATALOG_TOOL_NAME} just to check a name`,+ );+ expect(instructions).toContain(+ `${GET_TOOL_CATALOG_TOOL_NAME} with ONE category`,+ );+ });+it('should omit the skills line when the workspace has no skills', () => {const instructions = buildMcpServerInstructions({objectNames: 'companies',diff --git a/packages/twenty-server/src/engine/api/mcp/utils/build-mcp-server-instructions.util.ts b/packages/twenty-server/src/engine/api/mcp/utils/build-mcp-server-instructions.util.tsindex f97e7ca3ad8..801ea47765e 100644--- a/packages/twenty-server/src/engine/api/mcp/utils/build-mcp-server-instructions.util.ts+++ b/packages/twenty-server/src/engine/api/mcp/utils/build-mcp-server-instructions.util.ts@@ -25,11 +25,15 @@ export const buildMcpServerInstructions = ({` execute_tool(toolName, arguments) — execute any CRUD or action tool by name`,` learn_tools(toolNames) — fetch input schemas before calling tools; pass ALL needed tool names in one call, not one call per tool`,` load_skills(skillNames) — load step-by-step instructions for complex tasks`,+ ` list_object_metadata_names() — list this workspace's object names`,+ ` list_skills() — list the available skill names`,+ ` get_tool_catalog(categories) — fallback discovery, only when you do not know which tool exists. Pass ONE category, never call it unfiltered`,``,...(skillNames ? [`Available skills: ${skillNames}.`, ``] : []),`CRUD tool name grammar — construct names directly without prior discovery:`,` 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.`,+ ` Not sure a constructed name exists? Pass it to learn_tools — unknown names come back under notFound with the closest matching names. Never call get_tool_catalog just to check a name.`,``,`Non-CRUD tools — use learn_tools for schemas:`,` ACTION: ${actionToolNames.join(' | ')}`,@@ -40,6 +44,7 @@ export const buildMcpServerInstructions = ({` WEBHOOK: list/create/update/delete_webhook`,` NAVIGATION: list/create/update/delete_navigation_menu_item`,` LOGIC_FUNCTION: app_{function_name} — workspace-specific; use list_logic_function_tools to discover`,+ ` Don't know which tool exists at all? get_tool_catalog with ONE category from the list above, then learn_tools, then execute_tool.`,``,`Skills vs Tools:`,` Skills = documentation (load_skills) — teach HOW to do something, correct schemas and patterns`,diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/services/tool-registry.service.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/services/tool-registry.service.tsindex b3f149a6896..9c7283640e7 100644--- a/packages/twenty-server/src/engine/core-modules/tool-provider/services/tool-registry.service.ts+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/services/tool-registry.service.ts@@ -329,7 +329,7 @@ export class ToolRegistryService {return {success: false,message: `Tool "${toolName}" not found`,- error: `Tool "${toolName}" not found.${suggestionHint} Use learn_tools to discover available tools.`,+ error: `Tool "${toolName}" not found.${suggestionHint} Pass your best candidate name to learn_tools to confirm it before executing.`,};}diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/tools/execute-tool.tool.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/tools/execute-tool.tool.tsindex 292e6c9d584..e6325f205db 100644--- a/packages/twenty-server/src/engine/core-modules/tool-provider/tools/execute-tool.tool.ts+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/tools/execute-tool.tool.ts@@ -9,7 +9,7 @@ import { type ToolOutput } from 'src/engine/core-modules/tool/types/tool-output.export const EXECUTE_TOOL_TOOL_NAME = 'execute_tool';const executeToolInputZodSchema = z.object({- toolName: z.string().describe('Exact tool name. Do not guess.'),+ toolName: z.string().describe('Tool name, as confirmed by learn_tools.'),arguments: z.record(z.string(), z.unknown()).describe('Arguments matching the schema returned by learn_tools.'),@@ -61,7 +61,7 @@ export const createExecuteToolTool = (return {success: false,message: `Tool "${toolName}" is not available`,- error: `Tool "${toolName}" is not available in this context. Use get_tool_catalog to discover available tools.`,+ error: `Tool "${toolName}" is not available in this context and cannot be called here. Do not retry it.`,};}diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/tools/get-tool-catalog.tool.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/tools/get-tool-catalog.tool.tsindex d6c9a01791a..965c57e53e5 100644--- a/packages/twenty-server/src/engine/core-modules/tool-provider/tools/get-tool-catalog.tool.ts+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/tools/get-tool-catalog.tool.ts@@ -39,7 +39,7 @@ export const createGetToolCatalogTool = (},) => ({description:- 'STEP 1: Start here. Browse available tools by category. Returns tool names and descriptions. You MUST call this before using learn_tools or execute_tool — do not guess tool names.',+ 'Fallback discovery — use only when you do not know which tool exists. You do not need this for record operations: build the name from the grammar ({operation}_{object}, e.g. find_many_people) and pass it straight to learn_tools, which returns the closest matching names when a name is wrong. Pass ONE category to keep the response small.',inputSchema: getToolCatalogInputSchema,execute: async (parameters: GetToolCatalogInput,diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/tools/learn-tools.tool.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/tools/learn-tools.tool.tsindex 431d7904aa4..a3dfeb9d167 100644--- a/packages/twenty-server/src/engine/core-modules/tool-provider/tools/learn-tools.tool.ts+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/tools/learn-tools.tool.ts@@ -15,7 +15,7 @@ export const learnToolsInputSchema = z.object({toolNames: z.array(z.string()).describe(- 'Exact tool names. Do not guess tool names. Pass every tool you need to learn in this single array — do not make separate learn_tools calls per tool.',+ 'Tool names to learn. A name you built from the CRUD grammar is fine: unknown names come back under notFound with the closest matching names. Pass every tool you need to learn in this single array — do not make separate learn_tools calls per tool.',),aspects: z.array(learnToolsAspectSchema)@@ -54,7 +54,7 @@ export const createLearnToolsTool = (options?: LearnToolsOptions,) => ({description:- 'Get input schemas for tools. Pass all the tool names you need in a single call (toolNames accepts an array) rather than calling learn_tools once per tool. Call this with exact tool names to learn the required arguments before calling execute_tool.',+ 'Get input schemas for tools, and confirm a tool name exists. Pass all the tool names you need in a single call (toolNames accepts an array) rather than calling learn_tools once per tool. A name you built from the CRUD grammar is safe to pass: unknown names come back under notFound with the closest matching names, so you never need a catalog call just to check a name.',inputSchema: learnToolsInputSchema,execute: async (parameters: LearnToolsInput): Promise<LearnToolsResult> => {const { toolNames, aspects } = parameters;diff --git a/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/twenty-mcp-helper.const.ts b/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/twenty-mcp-helper.const.tsindex f0ed866de35..e53429bb4ef 100644--- a/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/twenty-mcp-helper.const.ts+++ b/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/twenty-mcp-helper.const.ts@@ -15,7 +15,8 @@ class TwentyMCP:Two categories of tools exist behind /mcp:- MCP-native: execute_tool, learn_tools, load_skills,- search_help_center. These are the 4 surfaces exposed directly.+ search_help_center, get_tool_catalog, list_object_metadata_names,+ list_skills. These are the 7 surfaces exposed directly.- Workspace catalog: 250+ CRUD / view / workflow / dashboard toolslike find_many_companies, create_one_person, update_one_opportunity. These are