Skip to main content

ToolCallFilter

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.

使用示例
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"],
});

构造函数参数
Direct link to 构造函数参数

🌐 Constructor parameters

options?:

Options
Configuration options for the tool call filter

选项
Direct link to 选项

🌐 Options

exclude?:

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

返回
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

扩展使用示例
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"],
}),
],
});

过滤所有工具调用
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(),
],
});

🌐 Related