Skip to main content
Mastra 1.0 is available 🎉 Read announcement

ToolCallFilter

The ToolCallFilter is an input processor that filters out tool calls and their results from the message history before sending to the model. This is useful when you want to exclude specific tool interactions from context or remove all tool calls entirely.

Usage example
Direct link to Usage example

import { ToolCallFilter } from "@mastra/core/processors";

// Exclude all tool calls
const filterAll = new ToolCallFilter();

// Exclude specific tools by name
const filterSpecific = new ToolCallFilter({
exclude: ["searchDatabase", "sendEmail"],
});

Constructor parameters
Direct link to Constructor parameters

options?:

Options
Configuration options for the tool call filter

Options
Direct link to Options

exclude?:

string[]
List of specific tool names to exclude. If not provided or undefined, all tool calls are excluded

Returns
Direct link to Returns

id:

string
Processor identifier set to 'tool-call-filter'

name:

string
Processor display name set to 'ToolCallFilter'

processInput:

(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>
Processes input messages to filter out tool calls and their results based on configuration

Extended usage example
Direct link to Extended usage example

src/mastra/agents/filtered-agent.ts
import { Agent } from "@mastra/core/agent";
import { ToolCallFilter } from "@mastra/core/processors";

export const agent = new Agent({
name: "filtered-agent",
instructions: "You are a helpful assistant",
model: "openai:gpt-4o",
tools: {
searchDatabase,
sendEmail,
getWeather,
},
inputProcessors: [
// Filter out database search tool calls from context
// to reduce token usage while keeping other tool interactions
new ToolCallFilter({
exclude: ["searchDatabase"],
}),
],
});

Filtering all tool calls
Direct link to Filtering all tool calls

import { Agent } from "@mastra/core/agent";
import { ToolCallFilter } from "@mastra/core/processors";

export const agent = new Agent({
name: "no-tools-context-agent",
instructions: "You are a helpful assistant",
model: "openai:gpt-4o",
tools: {
searchDatabase,
sendEmail,
},
inputProcessors: [
// Remove all tool calls from the message history
// The agent can still use tools, but previous tool interactions
// won't be included in the context
new ToolCallFilter(),
],
});