Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(chat): a misspelled action name finds the right action (#16390)

Hazem Adelb2d0a6dc0c50
diff --git a/packages/server/api/src/app/mcp/tools/ap-get-piece-props.ts b/packages/server/api/src/app/mcp/tools/ap-get-piece-props.ts
index 1b3d57e089b..7fefac4fa2d 100644
--- a/packages/server/api/src/app/mcp/tools/ap-get-piece-props.ts
+++ b/packages/server/api/src/app/mcp/tools/ap-get-piece-props.ts
@@ -23,14 +23,9 @@ export const apGetPiecePropsTool = (mcp: ProjectScopedMcpServer, log: FastifyBas
const platformId = await mcpUtils.resolvePlatformId({ mcp, log })
const projectId = mcpUtils.isProjectScoped(mcp) ? mcp.projectId : undefined
- const lookup = await mcpUtils.lookupPieceComponent({
- pieceName,
- componentName: actionOrTriggerName,
- componentType: type,
- projectId,
- platformId,
- log,
- })
+ const lookupByName = (componentName: string) => mcpUtils.lookupPieceComponent({ pieceName, componentName, componentType: type, projectId, platformId, log })
+ const firstLookup = await lookupByName(actionOrTriggerName)
+ const lookup = isNil(firstLookup.closestName) ? firstLookup : await lookupByName(firstLookup.closestName)
if (lookup.error) {
return lookup.error
}
@@ -63,7 +58,7 @@ export const apGetPiecePropsTool = (mcp: ProjectScopedMcpServer, log: FastifyBas
componentProps: component.props,
pieceName: normalized,
pieceVersion: piece.version,
- actionOrTriggerName,
+ actionOrTriggerName: component.name,
auth,
flowId,
providedInput: providedInput ?? {},
@@ -136,7 +131,7 @@ export const apGetPiecePropsTool = (mcp: ProjectScopedMcpServer, log: FastifyBas
: '\nNo required inputs.'
const exampleSection = `\n\n🧪 Example input (fill the placeholders, then pass to ap_execute_action):\n${JSON.stringify(exampleInput, null, 2)}`
return {
- content: [{ type: 'text', text: `✅ ${label} schema for "${normalized}/${actionOrTriggerName}":${descLine}${aiHintLine}${idempotentLine}${requiredLine}\n${JSON.stringify(textResult, null, 2)}${outputSection}${exampleSection}` }],
+ content: [{ type: 'text', text: `✅ ${label} schema for "${normalized}/${component.name}":${descLine}${aiHintLine}${idempotentLine}${requiredLine}\n${JSON.stringify(textResult, null, 2)}${outputSection}${exampleSection}` }],
structuredContent: structured,
}
}
diff --git a/packages/server/api/src/app/mcp/tools/mcp-utils.ts b/packages/server/api/src/app/mcp/tools/mcp-utils.ts
index 38aadc29b93..58c4e9e98bd 100644
--- a/packages/server/api/src/app/mcp/tools/mcp-utils.ts
+++ b/packages/server/api/src/app/mcp/tools/mcp-utils.ts
@@ -1,4 +1,4 @@
-import { applyFunctionToValuesSync, isNil, isObject, tryCatch } from '@activepieces/core-utils'
+import { applyFunctionToValuesSync, isNil, isObject, spreadIfDefined, tryCatch } from '@activepieces/core-utils'
import { AiMetadata, OutputSchema, OutputSchemaField, PieceMetadataModel, PiecePropertyMap, PropertyType } from '@activepieces/pieces-framework'
import { BranchOperator, EngineResponse, EngineResponseStatus, flowStructureUtil, McpServerType, McpToolResult, ProjectScopedMcpServer, PropertyExecutionType, PropertySettings, singleValueConditions, WorkerJobType } from '@activepieces/shared'
import type { BranchedAction, Step } from '@activepieces/shared'
@@ -433,13 +433,26 @@ async function lookupPieceComponent({ pieceName, componentName, componentType, p
const component = componentMap[componentName]
if (isNil(component)) {
const available = Object.keys(componentMap)
- const suggestion = available.find((name) => name.includes(componentName))
- const hint = suggestion ? ` Did you mean "${suggestion}"?` : ''
- return { error: { content: [{ type: 'text', text: `❌ ${label} "${componentName}" not found in "${normalized}".${hint} Available: ${available.join(', ')}` }], isError: true } }
+ const closestName = closestComponentName({ guess: componentName, available })
+ const hint = isNil(closestName) ? '' : ` Did you mean "${closestName}"?`
+ return { error: { content: [{ type: 'text', text: `❌ ${label} "${componentName}" not found in "${normalized}".${hint} Available: ${available.join(', ')}` }], isError: true }, ...spreadIfDefined('closestName', closestName) }
}
return { piece, component, pieceName: normalized }
}
+function closestComponentName({ guess, available }: { guess: string, available: string[] }): string | undefined {
+ const wanted = comparableName(guess)
+ if (wanted.length === 0) {
+ return undefined
+ }
+ const matches = available.filter((name) => comparableName(name).endsWith(wanted))
+ return matches.length === 1 ? matches[0] : undefined
+}
+
+function comparableName(name: string): string {
+ return name.toLowerCase().replace(/[^a-z0-9]/g, '').replace(/s$/, '')
+}
+
function findResolvableProps({ props, componentProps, auth, providedInput }: FindResolvablePropsParams): PropSummary[] {
return props.filter(prop => {
const propDef = componentProps[prop.name]
@@ -956,8 +969,8 @@ type LookupPieceComponentParams = {
}
type LookupPieceComponentResult =
- | { piece: PieceMetadataModel, component: { props: PiecePropertyMap, requireAuth: boolean, name: string, displayName: string, description: string, outputSchema?: OutputSchema, aiMetadata?: AiMetadata, sampleData?: unknown }, pieceName: string, error?: never }
- | { error: McpToolResult, piece?: never, component?: never, pieceName?: never }
+ | { piece: PieceMetadataModel, component: { props: PiecePropertyMap, requireAuth: boolean, name: string, displayName: string, description: string, outputSchema?: OutputSchema, aiMetadata?: AiMetadata, sampleData?: unknown }, pieceName: string, error?: never, closestName?: never }
+ | { error: McpToolResult, closestName?: string, piece?: never, component?: never, pieceName?: never }
type ResolveFolderResult =
| { folderId: string | undefined, folderName: string | undefined, error?: never }
diff --git a/packages/server/api/test/integration/ce/mcp/mcp-tools.test.ts b/packages/server/api/test/integration/ce/mcp/mcp-tools.test.ts
index af411f6fb29..3dc55d6f653 100644
--- a/packages/server/api/test/integration/ce/mcp/mcp-tools.test.ts
+++ b/packages/server/api/test/integration/ce/mcp/mcp-tools.test.ts
@@ -423,6 +423,22 @@ describe('MCP Tools integration', () => {
expect(text(result)).toContain('subject')
})
+ it('11b. ap_get_piece_props — a guessed name that clearly means one action returns that action', async () => {
+ const ctx = await createTestContext(app)
+ const mcp = makeMcp(ctx.project.id)
+
+ const resolved = await apGetPiecePropsTool(mcp, mockLog).execute({ pieceName: '@activepieces/piece-test-email', actionOrTriggerName: 'sendEmail', type: 'action' })
+ const unclear = await apGetPiecePropsTool(mcp, mockLog).execute({ pieceName: '@activepieces/piece-test-email', actionOrTriggerName: 'send', type: 'action' })
+ const ambiguous = await apGetPiecePropsTool(mcp, mockLog).execute({ pieceName: '@activepieces/piece-test-email', actionOrTriggerName: 'email', type: 'trigger' })
+ const blank = await apGetPiecePropsTool(mcp, mockLog).execute({ pieceName: '@activepieces/piece-test-array', actionOrTriggerName: '__', type: 'action' })
+
+ expect(text(resolved)).toContain('✅')
+ expect(text(resolved)).toContain('/send_email')
+ expect(text(unclear)).toContain('❌')
+ expect(text(ambiguous)).toContain('❌')
+ expect(text(blank)).toContain('❌')
+ })
+
it('12. ap_get_piece_props — returns error for non-existent piece', async () => {
const ctx = await createTestContext(app)
const mcp = makeMcp(ctx.project.id)