Files
windmill/frontend/src/lib/components/flows/content/FlowModuleWrapper.svelte
T
centdix 1afa36ceeb feat(aiagent): allow mcp as tools (#6790)
* draft mcp client

* testing

* fix

* cleaning

* mcp resource in inputtransforms

* cleaning

* big cleaning

* cleaning

* no arc

* add utils file

* refactor tools

* add mcp actions

* draft frontend

* send arguments from backend

* better frontend

* cleaning

* use token for auth

* add logo

* rm

* fix

* fix

* chore: refactor mcp for ai agents (#6829)

* Add Tool enum for AIAgent with backward compatibility

- Created Tool enum that can be either Windmill (FlowModule) or Mcp (resource reference)
- Created McpToolRef struct to hold MCP resource path
- Implemented custom Deserialize for Tool with backward compatibility:
  - New format: {type: 'windmill'|'mcp', ...}
  - Old format: FlowModule objects (automatically wrapped in Tool::Windmill)
- Updated AIAgent to use Vec<Tool> instead of Vec<FlowModule>
- Updated FlowValue::traverse_leafs to handle Tool enum
- Backward compatible: old flows with Vec<FlowModule> will deserialize correctly

* Refactor AI executor to process Tool enum instead of extracting MCP from input_transforms

- Separate Windmill tools and MCP resource paths from tools list
- Process Windmill FlowModules into Tool definitions
- Load MCP tools from resource paths in Tool::Mcp variants
- Remove old logic that extracted mcp_resources from input_transforms
- Import FlowModule, remove unused InputTransform
- Fix type issues: use .as_str() for path and handle Option<bool> properly

* handle in args

* mcp as flowmodule

* frontend

* config for mcp

* simplify logic

* fix ai executor logic

* cleaning

* clean frontend

* fix

* better resource picker

* fix and styling

* add endpoint to fetch tools

* apply tool filtering

* fix name validation

* better ui

* use cache

* fix

* fix merge

* refactor: Separate MCP tools from FlowModule in AIAgent

- Add new AgentTool, ToolValue, and McpToolValue types
- Update AIAgent to use Vec<AgentTool> instead of Vec<FlowModule>
- Implement From traits for clean conversion between AgentTool and FlowModule
- Add backward compatibility via custom deserializer for AgentTool
- Simplify resolve_module logic by reusing existing resolve_modules function
- Update traverse_leafs to handle AgentTool structure

This refactoring separates MCP tools from FlowModule tools, making the
type system clearer and eliminating the need to treat MCP servers as
a special case of FlowModule.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* refactor: Update ai_executor and worker_lockfiles for AgentTool

- Update ai_executor.rs to handle new AgentTool structure
  - Separate MCP tools from FlowModule tools using ToolValue enum
  - Convert AgentTool to FlowModule for backward compatibility
  - Add imports for AgentTool and ToolValue types

- Update worker_lockfiles.rs for lazy loading optimization
  - Convert AgentTool <-> FlowModule in insert_flow_modules
  - Preserve lazy loading for FlowModule tools via modules_node
  - Keep MCP tools inline (lightweight, no need for lazy loading)
  - Maintain backward compatibility with existing flows

This enables the lazy loading optimization for FlowModule tools while
keeping MCP tools inline, balancing performance and simplicity.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* cleaning

* adapt frontend

* cleaning

* cleaning

* type fix

* cleaning

* fix back comp

* move mcp button position

* nit

* cleaning

* fix nested removal

* cleaning

* opti

* fix chat markdown display

* fix chat messages layout

* fix back comp frontend

* fix deserializer

* nit

* simpler serializer

* use if else

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-21 15:28:52 +00:00

310 lines
9.9 KiB
Svelte

<script lang="ts">
import FlowModuleWrapper from './FlowModuleWrapper.svelte'
import { type FlowModule } from '$lib/gen'
import { getContext } from 'svelte'
import type { FlowEditorContext } from '../types'
import FlowLoop from './FlowLoop.svelte'
import FlowModuleComponent from './FlowModuleComponent.svelte'
import FlowBranchAllWrapper from './FlowBranchAllWrapper.svelte'
import FlowBranchOneWrapper from './FlowBranchOneWrapper.svelte'
import {
createInlineScriptModule,
pickFlow,
pickScript
} from '$lib/components/flows/flowStateUtils.svelte'
import FlowInputs from './FlowInputs.svelte'
import { Alert } from '$lib/components/common'
import FlowInputsFlow from './FlowInputsFlow.svelte'
import FlowBranchesAllWrapper from './FlowBranchesAllWrapper.svelte'
import FlowBranchesOneWrapper from './FlowBranchesOneWrapper.svelte'
import FlowWhileLoop from './FlowWhileLoop.svelte'
import type { TriggerContext } from '$lib/components/triggers'
import { formatCron } from '$lib/utils'
import AgentToolWrapper from './AgentToolWrapper.svelte'
const { selectedId, flowStateStore } = getContext<FlowEditorContext>('FlowEditorContext')
const { triggersState, triggersCount } = getContext<TriggerContext>('TriggerContext')
let scriptKind: 'script' | 'trigger' | 'approval' = $state('script')
let scriptTemplate: 'pgsql' | 'mysql' | 'script' | 'docker' | 'powershell' = $state('script')
// These pointers are used to easily access previewArgs of parent module, and previous module
interface Props {
flowModule: FlowModule
noEditor?: boolean
enableAi?: boolean
savedModule?: FlowModule | undefined
// Pointer to parent module, only defined within Branches or Loops.
parentModule?: FlowModule | undefined
// Pointer to previous module, for easy access to testing results
previousModule?: FlowModule | undefined
forceTestTab?: Record<string, boolean>
highlightArg?: Record<string, string | undefined>
isAgentTool?: boolean
}
let {
flowModule = $bindable(),
noEditor = false,
enableAi = false,
savedModule = undefined,
parentModule = $bindable(),
previousModule = undefined,
forceTestTab,
highlightArg,
isAgentTool = false
}: Props = $props()
function initializePrimaryScheduleForTriggerScript(module: FlowModule) {
const primaryIndex = triggersState.triggers.findIndex((t) => t.isPrimary)
if (primaryIndex === -1) {
const primaryCfg = {
summary: 'Scheduled poll of flow',
args: {},
schedule: formatCron('0 */15 * * *'),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
enabled: true,
is_flow: true
}
triggersState.addDraftTrigger(triggersCount, 'schedule', undefined, primaryCfg)
} else if (triggersState.triggers[primaryIndex].draftConfig) {
//If there is a primary schedule draft update it
const newCfg = { ...triggersState.triggers[primaryIndex].draftConfig }
let updated = false
if (!newCfg.schedule) {
newCfg.schedule = formatCron('0 */15 * * *')
updated = true
}
if (!newCfg.enabled) {
newCfg.enabled = true
updated = true
}
if (updated) {
triggersState.triggers[primaryIndex].draftConfig = newCfg
}
}
module.stop_after_if = {
expr: 'result == undefined || Array.isArray(result) && result.length == 0',
skip_if_stopped: true
}
}
async function createModuleFromScript(
path: string,
summary: string,
kind: string,
hash: string | undefined
) {
const [module, state] = await pickScript(path, summary, flowModule.id, hash)
if (kind == 'approval') {
module.suspend = { required_events: 1, timeout: 1800 }
}
if (kind == 'trigger') {
initializePrimaryScheduleForTriggerScript(module)
}
flowModule = module
flowStateStore.val[module.id] = state
}
</script>
{#if flowModule.id === $selectedId}
{#if flowModule.value.type === 'forloopflow'}
<FlowLoop {noEditor} bind:mod={flowModule} {parentModule} {previousModule} {enableAi} />
{:else if flowModule.value.type === 'whileloopflow'}
<FlowWhileLoop {noEditor} bind:mod={flowModule} {previousModule} {parentModule} />
{:else if flowModule.value.type === 'branchone'}
<FlowBranchesOneWrapper {noEditor} {previousModule} {parentModule} bind:flowModule {enableAi} />
{:else if flowModule.value.type === 'branchall'}
<FlowBranchesAllWrapper {noEditor} {previousModule} {parentModule} bind:flowModule />
{:else if flowModule.value.type === 'identity'}
{#if $selectedId == 'failure'}
<div class="p-4">
<Alert type="info" title="Error handlers are triggered upon non recovered errors">
If defined, the error handler will take the error as input.
</Alert>
</div>
{:else if $selectedId == 'preprocessor'}
<div class="p-4">
<Alert
type="info"
title="Preprocessor is called when the flow is triggered by API or email"
>
It prepares arguments for the flow. Besides request arguments, the preprocessor receives a
`wm_trigger` argument with trigger details.
</Alert>
</div>
{/if}
{#if flowModule.value.flow}
<FlowInputsFlow
on:pick={async ({ detail }) => {
const { path, summary } = detail
const [module, state] = await pickFlow(path, summary, flowModule.id)
flowModule = module
flowStateStore.val[module.id] = state
}}
/>
{:else}
<FlowInputs
{noEditor}
summary={flowModule.summary}
shouldDisableTriggerScripts={parentModule !== undefined ||
previousModule !== undefined ||
$selectedId == 'failure' ||
$selectedId == 'preprocessor'}
on:pick={async ({ detail }) => {
const { path, summary, kind, hash } = detail
createModuleFromScript(path, summary, kind, hash)
}}
on:new={async ({ detail }) => {
const { language, kind, subkind, summary } = detail
const [module, state] = await createInlineScriptModule(
language,
kind,
subkind,
flowModule.id,
summary
)
scriptKind = kind
scriptTemplate = subkind
if (kind == 'trigger') {
initializePrimaryScheduleForTriggerScript(module)
}
if (kind == 'approval') {
module.suspend = { required_events: 1, timeout: 1800 }
}
flowModule = module
flowStateStore.val[module.id] = state
}}
failureModule={$selectedId === 'failure'}
preprocessorModule={$selectedId === 'preprocessor'}
/>
{/if}
{:else if flowModule.value.type === 'rawscript' || flowModule.value.type === 'script' || flowModule.value.type === 'flow' || flowModule.value.type === 'aiagent'}
<FlowModuleComponent
{noEditor}
bind:flowModule
{parentModule}
{previousModule}
failureModule={$selectedId === 'failure'}
preprocessorModule={$selectedId === 'preprocessor'}
{scriptKind}
{scriptTemplate}
{enableAi}
{savedModule}
forceTestTab={forceTestTab?.[flowModule.id]}
highlightArg={highlightArg?.[flowModule.id]}
{isAgentTool}
/>
{/if}
{:else if flowModule.value.type === 'forloopflow' || flowModule.value.type == 'whileloopflow'}
{#each flowModule.value.modules as _, index (index)}
<FlowModuleWrapper
{noEditor}
bind:flowModule={flowModule.value.modules[index]}
bind:parentModule={flowModule}
previousModule={flowModule.value.modules[index - 1]}
savedModule={savedModule?.value.type === 'forloopflow' ||
savedModule?.value.type === 'whileloopflow'
? savedModule.value.modules[index]
: undefined}
{enableAi}
{forceTestTab}
{highlightArg}
/>
{/each}
{:else if flowModule.value.type === 'branchone'}
{#if $selectedId === `${flowModule?.id}-branch-default`}
<div class="p-2">
<h3 class="mb-4">Default branch</h3>
Nothing to configure, this is the default branch if none of the predicates are met.
</div>
{:else}
{#each flowModule.value.default as _, index}
<FlowModuleWrapper
{noEditor}
bind:flowModule={flowModule.value.default[index]}
bind:parentModule={flowModule}
previousModule={flowModule.value.default[index - 1]}
savedModule={savedModule?.value.type === 'branchone'
? savedModule.value.default[index]
: undefined}
{enableAi}
{forceTestTab}
{highlightArg}
/>
{/each}
{/if}
{#each flowModule.value.branches as branch, branchIndex (branchIndex)}
{#if $selectedId === `${flowModule?.id}-branch-${branchIndex}`}
<FlowBranchOneWrapper
{noEditor}
bind:branch={flowModule.value.branches[branchIndex]}
parentModule={flowModule}
{previousModule}
{enableAi}
/>
{:else}
{#each branch.modules as _, index}
<FlowModuleWrapper
{noEditor}
bind:flowModule={flowModule.value.branches[branchIndex].modules[index]}
bind:parentModule={flowModule}
previousModule={flowModule.value.branches[branchIndex].modules[index - 1]}
savedModule={savedModule?.value.type === 'branchone'
? savedModule.value.branches[branchIndex]?.modules[index]
: undefined}
{enableAi}
{forceTestTab}
{highlightArg}
/>
{/each}
{/if}
{/each}
{:else if flowModule.value.type === 'branchall'}
{#each flowModule.value.branches as branch, branchIndex (branchIndex)}
{#if $selectedId === `${flowModule?.id}-branch-${branchIndex}`}
<FlowBranchAllWrapper {noEditor} bind:branch={flowModule.value.branches[branchIndex]} />
{:else}
{#each branch.modules as _, index}
<FlowModuleWrapper
{noEditor}
bind:flowModule={flowModule.value.branches[branchIndex].modules[index]}
bind:parentModule={flowModule}
previousModule={flowModule.value.branches[branchIndex].modules[index - 1]}
{enableAi}
savedModule={savedModule?.value.type === 'branchall'
? savedModule.value.branches[branchIndex]?.modules[index]
: undefined}
{forceTestTab}
{highlightArg}
/>
{/each}
{/if}
{/each}
{:else if flowModule.value.type === 'aiagent'}
{@const toolIndex = flowModule.value.tools.findIndex((t) => t.id === $selectedId)}
{#if toolIndex !== -1}
<AgentToolWrapper
{noEditor}
bind:tool={flowModule.value.tools[toolIndex]}
parentModule={flowModule}
{previousModule}
{enableAi}
{forceTestTab}
{highlightArg}
/>
{/if}
{/if}