Skip to content

Commit ea63405

Browse files
authored
fix(workflow-code): let Mothership add child workflow, MCP and saved custom tools in source (#10722)
Copilot authoring refused the labels the tool picker derives from a binding (a child workflow's Workflow/workflow_executor, an MCP tool's name and id), which childWorkflow().asTool() and mcp().tool() write, and a saved custom tool's empty params, so every such tool Mothership added through source failed at apply although check passed. Authoring now accepts a label only when the binding's own definition derives it, and materialize writes a saved custom tool as its id alone, as the picker does. check also hints the fix for TS2448 (a block named above its declaration), the type error Mothership hit most while editing source.
1 parent 5d912ca commit ea63405

6 files changed

Lines changed: 96 additions & 2 deletions

File tree

‎apps/sim/lib/workflows/code/hand-written.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { generateRegistryCorpus } from '@/lib/workflows/code/corpus'
2727
import { CORPUS_RESOURCES } from '@/lib/workflows/code/corpus/fixtures.test-helpers'
2828
import { createMaterializeHooks } from '@/lib/workflows/code/materialize-hooks'
2929
import { typeCheckAgainstSdk, writeSdkTypes } from '@/lib/workflows/code/sdk-typecheck.test-helpers'
30+
import { validateToolBindingAuthoring } from '@/lib/workflows/tool-input/authoring'
3031

3132
const hooks = createMaterializeHooks()
3233

@@ -396,3 +397,30 @@ export default workflow('Nightly', {}, () => {
396397
expect(typeCheck('every-tool', entry.text)).toBe('')
397398
}, 120_000)
398399
})
400+
401+
describe('an agent tool added in source', () => {
402+
it.each([
403+
['a child workflow', "childWorkflow('enrich').asTool()"],
404+
['an MCP tool', "mcp('docs').tool('search_docs')"],
405+
['a saved custom tool', "customTool('lookup')"],
406+
])('stores %s in a shape the copilot authoring check accepts', (_, form) => {
407+
const text = `${HEADER}import { agent, childWorkflow, customTool, mcp, trigger, workflow } from '@sim/workflow-sdk'
408+
409+
export default workflow('Tools', () => {
410+
const start = trigger.start('Start', { inputFormat: {} })
411+
const helper = agent('Helper', { model: 'claude-sonnet-4-6', messages: [{ role: 'user', content: 'hi' }], tools: [${form}] })
412+
start.to(helper)
413+
})
414+
`
415+
const compiled = compile(text)
416+
if (!compiled.workflow) throw new Error(compiled.diagnostics.map((d) => d.message).join('; '))
417+
const stored = materialize(compiled.workflow, null, {
418+
catalog,
419+
hooks,
420+
resources: RESOURCES,
421+
lockFreshness: 'none',
422+
}).state
423+
const helper = Object.values(stored.blocks).find((block) => block.type === 'agent')
424+
expect(validateToolBindingAuthoring('agent', helper?.subBlocks.tools?.value)).toBeUndefined()
425+
})
426+
})

‎apps/sim/lib/workflows/tool-input/authoring.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ describe('tool binding authoring contract', () => {
2727
).toContain('read-only')
2828
})
2929

30+
it('accepts the labels the picker derives from a binding, and no other label', () => {
31+
const child = { type: 'workflow_input', params: { workflowId: 'wf-1' } }
32+
const picked = [
33+
{ ...child, title: 'Workflow', toolId: 'workflow_executor' },
34+
{ ...mcp, title: 'search_docs', toolId: 'mcp-server-1-search_docs' },
35+
]
36+
expect(validateToolBindingAuthoring('agent', picked)).toBeUndefined()
37+
expect(validateToolBindingAuthoring('agent', [{ ...child, title: 'Billing' }])).toContain(
38+
'read-only'
39+
)
40+
expect(validateToolBindingAuthoring('agent', [{ ...mcp, title: 'delete_all' }])).toContain(
41+
'read-only'
42+
)
43+
})
44+
3045
it('does not grant an existing label to a different operation or an extra binding', () => {
3146
const saved = [{ ...integration, title: 'Old UI label' }]
3247
expect(

‎apps/sim/lib/workflows/tool-input/authoring.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
v2AgentMcpServerAdvancedSchema,
99
v2AgentMcpToolSchema,
1010
} from '@/lib/api/contracts/v2/workflows'
11+
import { createMcpToolId } from '@/lib/mcp/shared'
1112

1213
/** Attachment identity comes from its definition, never an author-supplied alias. */
1314
const identityFields = ['title', 'name', 'toolId', 'functionName'] as const
@@ -60,6 +61,23 @@ export function getToolBindingAuthoringSchema(blockType: string) {
6061
return undefined
6162
}
6263

64+
type IdentityField = (typeof identityFields)[number]
65+
66+
/**
67+
* The labels the editor's tool picker derives from a binding's own definition (tool-input.tsx): a
68+
* child workflow tool's fixed title and tool id, and an MCP tool's name and the id built from it.
69+
* They name nothing the binding does not, so writing them is not an override.
70+
*/
71+
function definitionLabels(tool: Record<string, unknown>): Partial<Record<IdentityField, string>> {
72+
if (tool.type === 'workflow_input') return { title: 'Workflow', toolId: 'workflow_executor' }
73+
const params = isPlainRecord(tool.params) ? tool.params : {}
74+
const { serverId, toolName } = params
75+
if (tool.type === 'mcp' && typeof serverId === 'string' && typeof toolName === 'string') {
76+
return { title: toolName, toolId: createMcpToolId(serverId, toolName) }
77+
}
78+
return {}
79+
}
80+
6381
function bindingIdentity(tool: Record<string, unknown>): unknown[] {
6482
const params = isPlainRecord(tool.params) ? tool.params : {}
6583
return [tool.type, tool.operation, tool.customToolId, params.serverId, params.toolName]
@@ -92,8 +110,11 @@ export function validateToolBindingAuthoring(
92110
)
93111
const saved = savedIndex === -1 ? undefined : remaining.splice(savedIndex, 1)[0]
94112
const isInline = tool.type === 'custom-tool' && !tool.customToolId
113+
const derived = definitionLabels(tool)
95114
const carried = identityFields.filter(
96-
(key) => saved && tool[key] === saved[key] && !(isInline && key === 'title')
115+
(key) =>
116+
(tool[key] !== undefined && tool[key] === derived[key]) ||
117+
(saved && tool[key] === saved[key] && !(isInline && key === 'title'))
97118
)
98119
const candidate = omit(tool, carried)
99120
if (

‎packages/sim-cli/src/commands/protocol/source-types.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,14 @@ export async function typeCheck(
472472
const { file, line, column, message } = error
473473
const source = sources.get(file)
474474
if (asWritten.has(errorKey(error))) continue
475+
const usedEarly = USED_BEFORE_DECLARATION.exec(message)
475476
findings.push({
476477
code: 'E_TYPE',
477478
severity: 'error',
478479
message,
480+
...(usedEarly && {
481+
hint: `Move \`const ${usedEarly[1]}\` above this line: a block can be named only after it is declared. When two blocks read each other, write one side as its raw reference, ref('blockname.path').`,
482+
}),
479483
file: file.slice(scope.length),
480484
line,
481485
column,
@@ -487,6 +491,10 @@ export async function typeCheck(
487491
return { checked: true, findings }
488492
}
489493

494+
/** tsc's error on a `const` named above its declaration: the block's variable. */
495+
const USED_BEFORE_DECLARATION =
496+
/^TS2448: Block-scoped variable '([^']+)' used before its declaration/
497+
490498
/** tsc's excess-property error: the property, and the object type it is missing from. */
491499
const UNKNOWN_PROPERTY = /^TS2353: .*'([^']+)' does not exist in type '(.*)'\.?$/
492500

‎packages/sim-cli/src/commands/protocol/workspace-source-get.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,26 @@ describe('the type check of workspaces source check', () => {
13051305
expect(codes).not.toContain('E_TYPE:7')
13061306
})
13071307

1308+
it('names the move for a block wired above its declaration', async () => {
1309+
serve({ 'tables/leads.ts': LEADS })
1310+
await get()
1311+
writeInserting(
1312+
'leads',
1313+
" save.to(done)\n const done = response('Done', { body: { email: start.email } })"
1314+
)
1315+
1316+
await runWorkspaceSource('check', dir)
1317+
1318+
expect(result().diagnostics).toContainEqual(
1319+
expect.objectContaining({
1320+
code: 'E_TYPE',
1321+
line: 7,
1322+
message: expect.stringMatching(/^TS2448: /),
1323+
hint: expect.stringContaining('Move `const done` above this line'),
1324+
})
1325+
)
1326+
})
1327+
13081328
it('reports only its own folder from a workflow folder, files relative to it', async () => {
13091329
serve({ 'tables/leads.ts': LEADS })
13101330
await get()

‎packages/workflow-code/src/materialize/params.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ function encodeTool(
106106
const { shape } = toolParamDeclaration(context.catalog, tool.type, key)
107107
setOwn(params, key, encodeToolParam(unliftValue(value, context, location), shape))
108108
}
109-
stored.params = params
109+
// The picker writes a saved custom tool as its id alone, with no params.
110+
const savedCustomTool = tool.customTool?.ref !== undefined && !tool.customTool.snapshot
111+
if (!(savedCustomTool && Object.keys(params).length === 0)) stored.params = params
110112
const binding = tool.toolId === undefined ? mcpBinding(tool.type, params) : null
111113
// Canonicalize drops the toolId the binding derives, since it embeds the server's id.
112114
if (binding) stored.toolId = mcpToolId(binding.serverId, binding.toolName)

0 commit comments

Comments
 (0)