Custom tools enable users to extend Sim Studio's functionality by writing JavaScript or Python code that executes within workflows. These tools integrate seamlessly with AI agents, allowing LLMs to discover and invoke user-defined functions as needed during workflow execution.
Custom tools are user-defined functions that can be stored in the database or defined inline within a block. They consist of:
The following diagram bridges the user-facing "Natural Language Space" where tools are defined to the "Code Entity Space" where they are executed.
Tool Execution Data Flow
Sources: apps/sim/tools/index.ts1-34 apps/sim/app/api/function/execute/route.ts1-21 apps/sim/executor/variables/resolvers/block.ts22-47
Sim Studio provides a multi-tier execution environment for custom tools, prioritizing speed while maintaining security.
For standard JavaScript code without external imports, the system uses a high-performance isolated Node.js VM.
executeInIsolatedVM which handles local sandboxed execution apps/sim/app/api/function/execute/route.ts7fetch that validates URLs against SSRF patterns using validateUrlWithDNS and secureFetchWithPinnedIP apps/sim/tools/index.ts8-10isolated-vm-worker.cjs to execute code in a separate process apps/sim/lib/execution/isolated-vm-worker.cjs1-4For Python code or JavaScript code containing import statements, the system delegates to E2B (External 2 Business) sandboxes.
CodeLanguage.Python or CodeLanguage.JavaScript apps/sim/app/api/function/execute/route.ts8extractJavaScriptImports to parse and extract imports from user code to determine if sandboxing is required apps/sim/app/api/function/execute/route.ts41-107Sources: apps/sim/app/api/function/execute/route.ts6-15 apps/sim/tools/index.ts8-12 apps/sim/app/api/function/execute/route.ts41-107
Custom tools are defined using the ToolConfig interface. When a user creates a "Function" block, it leverages the function_execute tool.
Parameters are defined with metadata that controls UI rendering and LLM visibility:
validateRequiredParametersAfterMerge before execution apps/sim/tools/index.ts33-34OutputSchema defined for the tool apps/sim/executor/variables/resolvers/block.ts10-11Code within custom tools can reference workflow state using specific syntaxes:
{{VAR_NAME}}: Workflow variables and environment variables apps/sim/app/api/function/execute/route.ts13-15BlockResolver which maps block names to their execution outputs apps/sim/executor/variables/resolvers/block.ts22-35Variable Resolution Architecture
Sources: apps/sim/executor/variables/resolvers/block.ts22-76 apps/sim/app/api/function/execute/route.ts9-15 apps/sim/tools/index.ts33-34
The /api/function/execute route serves as the central entry point for all custom code execution.
| Step | Component | Action |
|---|---|---|
| 1 | checkInternalAuth | Validates the request via internal JWT or session apps/sim/app/api/function/execute/route.ts3 |
| 2 | extractJavaScriptImports | Parses JS code to see if it requires E2B (external modules) apps/sim/app/api/function/execute/route.ts41 |
| 3 | executeInIsolatedVM | Executes standard JS in pooled worker processes apps/sim/app/api/function/execute/route.ts7 |
| 4 | executeInE2B | Executes Python or JS with imports in a secure remote sandbox apps/sim/app/api/function/execute/route.ts6 |
| 5 | extractEnhancedError | Maps VM/Sandbox stack traces back to user-provided code line numbers apps/sim/app/api/function/execute/route.ts125-204 |
Sources: apps/sim/app/api/function/execute/route.ts1-204 apps/sim/tools/index.ts1-20
To prevent VM escape and SSRF attacks, the system implements several layers of validation.
The secureFetchWithPinnedIP and validateUrlWithDNS functions ensure that code cannot access internal network resources or cloud metadata services apps/sim/tools/index.ts8-10
process, require, and module are explicitly set to undefined within the VM context to prevent access to the host system apps/sim/app/api/function/execute/route.test.ts82-85this.constructor.constructor patterns apps/sim/app/api/function/execute/route.test.ts72-76getHostedKeyRateLimiter to prevent abuse apps/sim/tools/index.ts6-10Sources: apps/sim/tools/index.ts8-10 apps/sim/app/api/function/execute/route.test.ts72-235 apps/sim/lib/execution/isolated-vm-worker.cjs146-200
Custom tools can securely use platform-provided API keys without exposing them to the user code directly.
This logic ensures that tools like serper_search or perplexity_chat can function out-of-the-box while maintaining strict billing and quota controls apps/sim/tools/index.ts49-137
Sources: apps/sim/tools/index.ts49-137 apps/sim/tools/index.test.ts171-178
Refresh this wiki