
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
@loopstack/custom-tool-example-module
Advanced tools
A complete example demonstrating how to implement and use a custom tool in a workflow
A module for the Loopstack AI automation framework.
This module provides a complete example demonstrating how to implement and use custom tools in a Loopstack workflow.
Custom tools are the building blocks of Loopstack automations. This module serves as a hands-on reference for developers learning how to extend Loopstack with their own functionality.
By exploring this example, you'll understand:
This is a great starting point before building your own custom tools.
See SETUP.md for installation and setup instructions.
A simple tool that maintains internal state across calls using the @Tool decorator and ToolInterface:
@Tool({
config: {
description: 'Counter tool.',
},
})
export class CounterTool implements ToolInterface {
count: number = 0;
async execute(): Promise<ToolResult> {
this.count++;
return Promise.resolve({
data: this.count,
});
}
}
A tool that accepts typed arguments via @Input and uses NestJS dependency injection for services:
@Tool({
config: {
description: 'Math tool calculating the sum of two arguments by using an injected service.',
},
})
export class MathSumTool implements ToolInterface {
@Inject()
private mathService: MathService;
@Input({
schema: z
.object({
a: z.number(),
b: z.number(),
})
.strict(),
})
args: MathSumArgs;
async execute(args: MathSumArgs): Promise<ToolResult<number>> {
const sum = this.mathService.sum(args.a, args.b);
return Promise.resolve({
data: sum,
});
}
}
The injected MathService is a standard NestJS injectable:
@Injectable()
export class MathService {
public sum(a: number, b: number) {
return a + b;
}
}
The workflow class declares input, state, output, tools, and helpers:
@Workflow({
configFile: __dirname + '/custom-tool-example.workflow.yaml',
})
export class CustomToolExampleWorkflow {
@InjectTool() private counterTool: CounterTool;
@InjectTool() private createChatMessage: CreateChatMessage;
@InjectTool() private mathTool: MathSumTool;
@Input({
schema: z
.object({
a: z.number().default(1),
b: z.number().default(2),
})
.strict(),
})
args: { a: number; b: number };
@State({
schema: z
.object({
total: z.number().optional(),
count1: z.number().optional(),
count2: z.number().optional(),
count3: z.number().optional(),
})
.strict(),
})
state: { total?: number; count1?: number; count2?: number; count3?: number };
@Output()
result() {
return { total: this.state.total };
}
@DefineHelper()
sum(a: number, b: number) {
return a + b;
}
}
Call custom tools and save their results to state using assign:
- id: calculation
tool: mathTool
args:
a: ${{ args.a }}
b: ${{ args.b }}
assign:
total: ${{ result.data }}
Reference workflow arguments with args.<name> and state with state.<name>:
- tool: createChatMessage
args:
role: 'assistant'
content: |
Tool calculation result:
{{ args.a }} + {{ args.b }} = {{ state.total }}
Call workflow helpers in templates:
- tool: createChatMessage
args:
role: 'assistant'
content: |
Alternatively, using workflow getter function:
{{ args.a }} + {{ args.b }} = {{ sum args.a args.b }}
The counter tool increments on each call, demonstrating stateful tools:
- id: count1
tool: counterTool
assign:
count1: ${{ result.data }}
- id: count2
tool: counterTool
assign:
count2: ${{ result.data }}
- id: count3
tool: counterTool
assign:
count3: ${{ result.data }}
- tool: createChatMessage
args:
role: 'assistant'
content: |
Counter tool should count:
{{ state.count1 }}, {{ state.count2 }}, {{ state.count3 }}
The @Output() decorator defines the data returned when the workflow completes:
@Output()
result() {
return { total: this.state.total };
}
This workflow uses the following Loopstack modules:
@loopstack/core - Core framework functionality@loopstack/create-chat-message-tool - Provides CreateChatMessage toolAuthor: Jakob Klippel
License: Apache-2.0
FAQs
A complete example demonstrating how to implement and use a custom tool in a workflow
The npm package @loopstack/custom-tool-example-module receives a total of 261 weekly downloads. As such, @loopstack/custom-tool-example-module popularity was classified as not popular.
We found that @loopstack/custom-tool-example-module demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.