Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ func (a *Agent) Tools(ctx context.Context) ([]tools.Tool, error) {

agentTools = append(agentTools, a.tools...)

// Deduplicate tools by name, keeping the first occurrence.
// Duplicate tool names cause provider API errors (e.g. Anthropic 400).
seen := make(map[string]struct{}, len(agentTools))
deduped := make([]tools.Tool, 0, len(agentTools))
for _, t := range agentTools {
if _, exists := seen[t.Name]; exists {
slog.Warn("Duplicate tool name; keeping first occurrence",
"agent", a.Name(), "tool", t.Name)
a.addToolWarning(fmt.Sprintf("duplicate tool %q: keeping first occurrence", t.Name))
continue
}
seen[t.Name] = struct{}{}
deduped = append(deduped, t)
}
agentTools = deduped

if a.addDescriptionParameter {
agentTools = tools.AddDescriptionParameter(agentTools)
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ func TestAgentTools(t *testing.T) {
wantToolCount: 0,
wantWarnings: 0,
},
{
name: "duplicate tool names are deduplicated",
toolsets: []tools.ToolSet{
newStubToolSet(nil, []tools.Tool{
{Name: "read_file", Parameters: map[string]any{}},
{Name: "write_file", Parameters: map[string]any{}},
}, nil),
newStubToolSet(nil, []tools.Tool{
{Name: "read_file", Parameters: map[string]any{}},
{Name: "shell", Parameters: map[string]any{}},
}, nil),
},
wantToolCount: 3,
wantWarnings: 1,
},
}

for _, tt := range tests {
Expand Down