executeJs Reference

The JavaScript that runs in the visitor's browser when an AI agent calls your tool.

Execution context

  • Runs synchronously or asynchronously in the browser's JavaScript engine
  • Has full access to document, window, and all browser APIs
  • Runs with the same permissions as the page's own scripts
  • No Node.js APIs (require, fs, process are not available)

The args object

args contains the parameters passed by the AI agent, typed according to your input schema.

// Tool with inputSchema: { productId: string, quantity: number }
// args = { productId: "shoe-01", quantity: 2 }
const product = document.querySelector(`[data-id="${args.productId}"]`)

Return value

Return a plain object. The AI agent receives this as the tool's result.

// Success
return { success: true, cartCount: 3 }
 
// Failure (agent-visible, not an exception)
return { success: false, error: "Product not found" }

Throwing an exception also works — the agent receives { error: "message" }.

Async support

executeJs supports async/await:

const res = await fetch('/api/cart/add', {
  method: 'POST',
  body: JSON.stringify({ productId: args.productId })
})
return await res.json()

Examples

Click a button:

const btn = document.querySelector('#checkout-btn')
if (!btn) return { success: false, error: 'Button not found' }
btn.click()
return { success: true }

Fill and submit a form:

document.querySelector('#search-input').value = args.query
document.querySelector('#search-form').submit()
return { success: true, query: args.query }

Read data from the page:

const items = [...document.querySelectorAll('.product-card')].map(el => ({
  id: el.dataset.id,
  name: el.querySelector('.name')?.textContent?.trim(),
  price: el.querySelector('.price')?.textContent?.trim(),
}))
return { results: items, total: items.length }

Call an internal API:

const res = await fetch('/api/wishlist', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ productId: args.productId })
})
if (!res.ok) return { success: false, error: `API error ${res.status}` }
return await res.json()

Security linting

The editor warns about dangerous patterns in real time. Full rule list:

PatternSeverityReason
eval(ErrorExecutes arbitrary strings
new Function(ErrorSame risk as eval
document.cookieErrorExposes session tokens
document.write(ErrorCan overwrite the page
input[type=password]ErrorPassword field scraping
setTimeout/setInterval with stringErrorString callbacks = eval
__proto__ / prototype[ErrorPrototype pollution
localStorageWarningMay contain sensitive data
sessionStorageWarningMay contain sensitive data
innerHTML =WarningXSS risk with unsanitised args
window.location =WarningRedirect may confuse agent
fetch(external URL)WarningPossible data exfiltration
XMLHttpRequestWarningPrefer fetch
navigator.geolocationWarningLocation access
postMessage(WarningCross-origin messaging