---
title: "Functions"
description: "Sandboxed JavaScript or Python snippets agents, workflows, and widgets can call."
canonical_url: "https://docs.vettero.com/build/functions"
---
# Functions

> Sandboxed JavaScript or Python snippets agents, workflows, and widgets can call.

**Functions** are reusable, sandboxed code snippets in your workspace. You write **JavaScript** or **Python**, define inputs and outputs, bind workspace resources, and return a result agents, workflows, widgets, and MCP can call as a tool.

Use them to encapsulate scoring, transforms, lookups, or multi-step logic behind a clear name — without giving the sandbox raw network access.

## When to use

- Encapsulate business logic that agents and workflows should call as a tool
- Compose queries, agents, workflows, blocks, and other functions behind one schema
- Keep sandboxed code free of raw network access or arbitrary imports

The sandbox cannot call the network or load packages on its own. Bind queries, blocks, agents, workflows, or other functions and call them through `vt`.

## Create a function

Open **Build → Functions** for a workspace-wide function, or open an agent’s builder and use the **Functions** panel for one scoped to that agent.

1. Click **Create Function** (or **Build With AI** to draft it in [Studio](https://docs.vettero.com/build/studio)).
2. Enter a **Name**, optional **Description**, and **Language** (**JavaScript** or **Python**).
3. Click **Create** — Vettero opens the function detail page.

Language is fixed after create. Workspace functions appear under **Global** in the agent tools picker; agent-scoped ones appear under **Local**.

## Open the editor

From the function detail page, click **Edit**. Use **Save** when you are done. You can also open **Build With AI** to change the function in Studio.

<table>
<thead>
  <tr>
    <th>
      Section
    </th>
    
    <th>
      What you configure
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        Name
      </strong>
      
       / <strong>
        Description
      </strong>
    </td>
    
    <td>
      Identity; the description becomes tool help text for agents and MCP
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Input schema
      </strong>
    </td>
    
    <td>
      JSON Schema for the <code>
        args
      </code>
      
       object callers pass in
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Output schema
      </strong>
    </td>
    
    <td>
      JSON Schema for the value your code returns
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Bound resources
      </strong>
    </td>
    
    <td>
      Queries, functions, agents, workflows, or blocks the code may call
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Code
      </strong>
    </td>
    
    <td>
      Function body only — the runtime wraps it for you
    </td>
  </tr>
</tbody>
</table>

In the code panel, the chrome shows the wrapper (`async function name(args) { … }` or `async def name(args):`). You only write the body.

## Run a function

Open the function editor and click **Run**. Fill in the arguments from the input schema. Vettero saves that input on the function so the next run starts from the same values.

**Run** executes the saved function and shows the output in the dialog. If you still have unsaved edits, Vettero saves them first. Agents, workflows, and widgets do not use this sample input — they pass their own arguments.

## Bind resources

Click **Add** under **Bound resources**. Each binding needs a unique **key**, a **type**, and the target resource.

<table>
<thead>
  <tr>
    <th>
      Type
    </th>
    
    <th>
      What you can bind
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        Query
      </strong>
    </td>
    
    <td>
      Any workspace <a href="https://docs.vettero.com/build/queries">
        query
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Function
      </strong>
    </td>
    
    <td>
      Any other workspace function (max nesting depth <strong>
        5
      </strong>
      
      )
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Agent
      </strong>
    </td>
    
    <td>
      Must use the <strong>
        general
      </strong>
      
       channel
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Workflow
      </strong>
    </td>
    
    <td>
      Must use the <strong>
        general
      </strong>
      
       channel
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Block
      </strong>
    </td>
    
    <td>
      A tool-capable block; pick a <strong>
        Service
      </strong>
      
       when the block is service-scoped
    </td>
  </tr>
</tbody>
</table>

Only keys you list here are callable from code. A function cannot bind itself.

## Write the code

Call bound resources through the injected `vt` helper. Pass a plain object as input (or omit it for `{}`).

<table>
<thead>
  <tr>
    <th>
      Method
    </th>
    
    <th>
      What it does
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        await vt.runQuery(key, input?)
      </code>
    </td>
    
    <td>
      Runs a bound query (typically <code>
        { rows, total }
      </code>
      
      )
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        await vt.runFunction(key, input?)
      </code>
    </td>
    
    <td>
      Runs a nested function (max depth 5)
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        await vt.runWorkflow(key, input?)
      </code>
    </td>
    
    <td>
      Awaits a workflow run; throws if it fails or waits for human approval
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        await vt.runAgent(key, input?)
      </code>
    </td>
    
    <td>
      Awaits an agent run; throws if it fails or waits for human approval
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        await vt.runBlock(key, input?)
      </code>
    </td>
    
    <td>
      Runs a bound block; human-in-the-loop interrupts are not supported
    </td>
  </tr>
</tbody>
</table>

### JavaScript example

```js
const { rows, total } = await vt.runQuery('orders', { status: args.status })
return { count: rows?.length ?? 0, total }
```

### Python example

```python
query = await vt.runQuery('orders', { 'status': args['status'] })
rows = query['rows'] if query else []
return { 'count': len(rows), 'total': query.get('total') if query else 0 }
```

## Attach to an agent

In the agent builder, open **Config → Tools → Manage Tools → Function**. Pick a function.

You can also type `#` in the agent **Task** and select the function — that mentions it in the prompt and attaches it as a tool.

## Limits

- JavaScript: no `import` / `require` / network in the isolate — use bound resources via `vt`. About **10MB** memory; about **3s** with no resources; up to about **5 minutes** when resources are bound.
- Python: about **15s** with no resources; up to about **5 minutes** when resources are bound.

## Where functions are used

<table>
<thead>
  <tr>
    <th>
      Place
    </th>
    
    <th>
      How
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        <a href="https://docs.vettero.com/build/agents">
          Agents
        </a>
      </strong>
    </td>
    
    <td>
      Tools → <strong>
        Function
      </strong>
      
      , or <code>
        #
      </code>
      
       mention in the Task
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        <a href="https://docs.vettero.com/configuration/mcp-servers">
          MCP Servers
        </a>
      </strong>
    </td>
    
    <td>
      List functions on an MCP server; each becomes an MCP tool
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        <a href="https://docs.vettero.com/build/widgets">
          Widgets
        </a>
      </strong>
    </td>
    
    <td>
      Bind as a resource or job and call from the widget
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Other functions
      </strong>
    </td>
    
    <td>
      Bind as a <strong>
        Function
      </strong>
      
       resource and call <code>
        vt.runFunction
      </code>
    </td>
  </tr>
</tbody>
</table>


## Sitemap

See the full [sitemap](https://docs.vettero.com/sitemap.md) for all pages.
