How to Run Workflows in Node.js with @toolpal/sdk
Install @toolpal/sdk, pin workflows into your project lockfile, and execute them safely from Node.js or TypeScript — including secrets, file outputs, private workflow auth, and why SDK scoping differs from MCP.
On this page
Prerequisites
Toolpal workflows (also called tools in the MCP and SDK APIs) run directly on your machine inside a permission-restricted Node.js child process — no hosted proxy and no extra runtime to manage.
- Node.js 22.15 or newer (Node 25+ or 26 LTS is recommended for native
--allow-netnetwork isolation). - An existing Node.js or TypeScript project using ESM (
"type": "module"inpackage.json). - No account required for public workflows — you only need to sign in when calling your own private workflows.
1. Install the SDK and pin a workflow
Install @toolpal/sdk as a project dependency, then pin the workflow you want to call with npx toolpal add. Here we'll use @toolpal/github-repo to fetch live GitHub repository stats, languages, and recent releases:
npm install @toolpal/sdk
npx toolpal add @toolpal/github-repoWhen you run toolpal add, the CLI downloads the tarball from toolpal.dev, verifies its Ed25519 registry signature and SHA-256 integrity hash, prints its permissions, and writes two files in your project root:
toolpal.json— declares which workflows your app is allowed to execute, plus optional env mappings and sandbox policies.toolpal.lock— pins exact versions and cryptographic hashes so every teammate and CI run executes the exact same bytes.
Commit both toolpal.json and toolpal.lock to Git. On another machine or in CI, restore all pinned workflows in one command:
npx toolpal install --frozen-lockfile2. Run your first workflow
Import the default toolpal client from @toolpal/sdk and call execute(). Toolpal automatically finds the nearest toolpal.json / toolpal.lock walking up from process.cwd(), validates your input against the workflow's JSON Schema, runs the sandboxed child process, and validates the returned output:
// index.ts (or index.js with "type": "module")
import { toolpal } from "@toolpal/sdk";
interface RepoSummary {
fullName: string;
url: string;
description?: string;
stars: number;
forks: number;
languages: Array<{ name: string; percent: number }>;
}
const repo = await toolpal.execute<RepoSummary>({
tool: "@toolpal/github-repo",
input: { repo: "facebook/react" },
timeoutMs: 15_000,
});
console.log(`${repo.fullName}: ${repo.stars.toLocaleString()} stars`);Idle workflow processes are automatically unref'd, so short scripts exit cleanly as soon as your code finishes. You don't even need internet access for offline workflows once pinned — everything runs from your local store.
Why SDK needs toolpal add (SDK vs MCP)
| Surface | Where workflows live | How you add them |
|---|---|---|
@toolpal/sdk | Your project directory (toolpal.json + toolpal.lock) | npx toolpal add @scope/name |
toolpal mcp | Your Toolpal account (~/.toolpal/account/) | Add button on toolpal.dev, toolpal add --account, or MCP install_tool |
The SDK refuses to execute any workflow that isn't pinned in your project (throwing ToolpalError with code NOT_ADDED) so untrusted user input or LLM tool calls can never trigger silent code downloads at runtime.
Running in ephemeral scripts or containers
If you're writing a one-off script or serverless job without a checked-in lockfile, you can explicitly install a workflow in code using createToolpal({ lock: false }):
import { createToolpal } from "@toolpal/sdk";
const client = createToolpal({ lock: false });
// Explicitly downloads, verifies signature + hash, and allows execution for this instance
await client.install("@toolpal/github-repo");
const repo = await client.execute({
tool: "@toolpal/github-repo",
input: { repo: "facebook/react" },
});For production apps, always prefer committing toolpal.json and toolpal.lock so version upgrades and permission changes go through code review.
3. Pass secrets and configure toolpal.json
Sandboxed workflows start with an empty environment — they cannot read your process.env unless you explicitly pass variables, and even then Toolpal only forwards keys declared in the workflow's permissions.env.
You can pass secrets per call:
const repo = await toolpal.execute({
tool: "@toolpal/github-repo",
input: { repo: "facebook/react" },
env: { GITHUB_TOKEN: process.env.GITHUB_TOKEN },
});Or map them once in toolpal.json alongside project-wide sandbox guardrails (timeouts, memory limits, and process isolation):
{
"tools": {
"@toolpal/github-repo": "1.0"
},
"env": {
"@toolpal/github-repo": {
"GITHUB_TOKEN": { "fromEnv": "MY_GITHUB_TOKEN" }
}
},
"policy": {
"maxTimeoutMs": 30000,
"maxMemoryMb": 256,
"isolation": "warm"
}
}Set policy.isolation to "per-call" if you handle multi-tenant requests and want a brand-new OS child process and temp folder on every execution.
4. Work with input and output files
Workflows that accept or generate files (such as @toolpal/qr-code or @toolpal/csv-insights) use Toolpal's FilePath() fields. Toolpal copies input files into a private call folder so workflows never touch your originals, and copies generated files into outputDir without overwriting existing files:
npx toolpal add @toolpal/qr-codeimport { toolpal } from "@toolpal/sdk";
const result = await toolpal.execute<{ image: string; version: number; bytes: number }>({
tool: "@toolpal/qr-code",
input: { text: "https://toolpal.dev", format: "png" },
outputDir: "./out",
});
console.log("Saved QR code to:", result.image);5. Authenticate for private workflows
Public workflows need zero authentication. If you publish private workflows under your own scope, authenticate using any of three methods (checked in this order):
| Priority | Method | Best for |
|---|---|---|
| 1 (highest) | createToolpal({ token }) | Multi-tenant servers or injecting secrets from a vault. |
| 2 | TOOLPAL_TOKEN | CI/CD pipelines, Docker containers, and hosted backends. |
| 3 | npx toolpal login | Local development (saves your token in ~/.toolpal/config.json). |
Create a dedicated API token in Settings → API tokens (or via npx toolpal token create --name prod) and pass it to createToolpal or your environment:
import { createToolpal } from "@toolpal/sdk";
const client = createToolpal({
token: process.env.TOOLPAL_TOKEN,
});
// After running: TOOLPAL_TOKEN=tp_... npx toolpal add @your-login/private-workflow
const out = await client.execute({
tool: "@your-login/private-workflow",
input: { query: "Q4 revenue" },
});Once a private workflow is installed into your project's local store (node_modules/.toolpal), executing it at runtime doesn't even hit the network — your token is only needed during toolpal add / toolpal install.
6. Error handling, hooks, and server shutdown
Every runtime failure throws a typed ToolpalError with a machine-readable code (such as NOT_ADDED, INPUT_INVALID, OUTPUT_INVALID, TIMEOUT, or TOOL_ERROR). You can also inspect schemas dynamically with describe(), attach beforeCall / afterCall hooks (great for audit logging or prompt-injection filtering), and close warm worker pools cleanly on shutdown:
import { createToolpal, ToolpalError } from "@toolpal/sdk";
const client = createToolpal({
onLog: (entry) => console.error(`[${entry.tool ?? "toolpal"}] ${entry.level}: ${entry.message}`),
hooks: {
beforeCall: ({ tool, input }) => {
console.log("Calling", tool, input);
return input;
},
afterCall: ({ output }) => output,
},
});
try {
// Inspect input/output JSON Schemas and permission summary programmatically
const info = await client.describe("@toolpal/github-repo");
console.log("Permissions:", info.permissionSummary);
const data = await client.execute({
tool: "@toolpal/github-repo",
input: { repo: "facebook/react" },
});
console.log(data);
} catch (err) {
if (err instanceof ToolpalError) {
console.error(`Toolpal failed [${err.code}]: ${err.message}`);
} else {
throw err;
}
} finally {
// Recommended in long-running HTTP servers / graceful SIGTERM handlers
await client.close();
}Ready to build your own workflow and call it from this exact SDK (or any MCP client)? Read the companion guide on building and publishing your first workflow, or browse existing workflows in the registry.