ISSUE DESCRIPTION Anthropic rejects duplicate websearch tool names When LibreChat web…
- User
ISSUE DESCRIPTION
Anthropic rejects duplicate web_search tool names
When LibreChat web search and Anthropic-native web search are both enabled, expose one
web_searchtool and prefer LibreChat's definition. Do not affect unrelated tools or other providers.Interface
No new public interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /LibreChat, current file: none) bash-$
Assistant
Let me explore the repository structure to understand the codebase.
Assistant
Let me explore the structure more to find the Anthropic-related code.
Assistant
Now let me understand how the LibreChat web search tool is defined and how tools flow through the system.
Assistant
The issue is about the Anthropic provider. When LibreChat's web_search tool is enabled AND Anthropic-native web search is enabled, both would expose a tool named "web_search". Anthropic rejects duplicate tool names. So we need to make sure that when LibreChat's web_search tool is enabled, we don't also add the Anthropic-native web_search tool.
Let me look at how tools are passed to the Anthropic client. The
getLLMConfigfunction inllm.tscreates thetoolsarray with the Anthropic-native web_search tool. The LibreChat tools come from elsewhere. Let me understand the flow better.Let me look at how the Anthropic client handles tools - where do the LibreChat tools get added vs the native web_search.
Assistant
The
getLLMConfigcreates atoolsarray containing the Anthropic-native web_search tool. These tools get returned. Now the question is where do the LibreChat tools get merged in. Let me look at how the tools array is used in the Anthropic client.Assistant
Let me find where the initialize result tools are used and how LibreChat tools merge with them.
Assistant
Let me trace where the initialize function result is used and how LibreChat tools get merged with native tools.
Assistant
Let me look at the custom endpoint client that uses the providers config.
Assistant
Let me look at the agents initialize.js which uses the providers. That's likely where the tools get merged.
Assistant
This is the agents endpoint. Let me look at the
@librechat/apipackage which has the main client logic.Assistant
Let me find where the initialize result's
toolsarray gets merged with the LibreChat tools in the actual client.Assistant
Let me look at the openai config.ts where it references the anthropic result tools - this is for custom OpenAI-compatible endpoints that use Anthropic models.
Assistant
Let me find where the initialize result's tools get consumed and merged with LibreChat tools.
Assistant
Let me look at how the providers' initialize function is called in the main (non-agents) flow.
Assistant
Hmm, so the only BaseClient subclass is the agents client. Let me check how the main API flow works. Let me look at the api server index.js and how it routes. Actually, maybe the main flow uses the agents endpoint. Let me look at the routes.
Assistant
Let me look at the messages route to find where the initialize result is used.
Assistant
The main flow seems to be through the agents endpoint. Let me look at the agents client.js where tools are combined.
Assistant
Let me look at the agents load.ts and how tools get combined with native tools from getLLMConfig.
Assistant
So
Tools.web_searchis the LibreChat web search tool name. Let me find whereresult.tools(the native web_search from initialize) gets merged with the LibreChat tools in the agents flow.Assistant
Now let me trace how
options.tools(native tools) andstructuredTools(LibreChat tools) are set in initializeAgent.Assistant
Let me find where
options.toolscomes from in initializeAgent - look at the function signature.Assistant
Let me look at where
structuredToolsis set in initializeAgent.Assistant
Let me understand how the Anthropic-native web search gets enabled vs the LibreChat web_search tool. Let me look at the llm.spec.ts tests.
Assistant
Let me look at how the Anthropic-native web search gets enabled vs the LibreChat tool. Let me check the agent settings/schema.
Assistant
So
web_search?: booleanis in the preset/conversation schema. This is the model parameter that enables Anthropic-native web search (or Google/OpenAI native).Now, the LibreChat web_search tool is
Tools.web_searchwhich is a string constant. Let me confirmTools.web_search === 'web_search'.Assistant
Let me look at the concatenation code in initialize.ts more carefully and the surrounding context.
Assistant
Let me look at the agents tools.ts to understand how the LibreChat web_search tool is constructed as a GenericTool.
Assistant
Let me look at how the LibreChat web_search tool gets loaded as a structured tool.
Assistant
Let me look at the createSearchTool to confirm the LibreChat web_search tool's name.
Assistant
Confirmed. Both produce a tool named
web_search. Now let me look at the GenericTool type and how the native tool vs structured tools are shaped.Assistant
Now let me check how the final
toolsarray is used in run.ts (passed to the Anthropic model).Assistant
Let me look at how the initialized agent's tools are bound to the model in the graph/run flow.
Assistant
Let me check the Providers enum to reference ANTHROPIC correctly.
Assistant
Let me look at the existing tests for initialize.ts to understand test patterns and confirm the concatenation behavior.
Assistant
Let me look at the createMocks function and the test setup.
Assistant
Let me check what's imported in initialize.ts, particularly
Tools.Assistant
Let me write a reproduction test to confirm the issue. First, let me look at an existing test that exercises the
options.toolsconcatenation path.Assistant
Now let me create a reproduction test to confirm the duplicate web_search issue. Let me first check how tests are run.
Assistant
Let me create a temporary reproduction test to confirm the duplicate web_search issue.
Assistant
The
librechat-data-providermodule isn't found in this context. Let me check the moduleNameMapper and how existing tests resolve it. The existing initialize.test.ts usesimport { EModelEndpoint } from 'librechat-data-provider';. Let me check if there's a moduleNameMapper or if it's resolved via node_modules. Let me check the node_modules.Assistant
Let me run the existing initialize.test.ts to confirm the test environment works.
Assistant
The existing tests also fail because
librechat-data-providerisn't resolvable. This is a monorepo - let me check the build setup.Assistant
The data-provider package has no
distdirectory. Let me check if it has a build. Thelibrechat-data-providersymlink points topackages/data-providerwhich only hassrc(no dist). So the main field of its package.json probably points to a dist that doesn't exist. Let me check the package.json main/module.Assistant
Now let me also build data-schemas which the api package depends on, then run my reproduction test.
Assistant
I've confirmed the issue: when both are enabled, the final
toolsarray contains TWO tools namedweb_search. Now let me implement the fix ininitialize.ts.Assistant
Now let me implement the fix:
Assistant
Let me use the
editcommand instead.Assistant
Now let me run the reproduction test again to confirm the fix works.
Assistant
The fix works. Now let me check edge cases. Let me look at
getProviderConfigto understand how custom Anthropic-compatible endpoints resolve their provider, to ensure my fix covers the right cases.Assistant
My fix correctly targets the native Anthropic endpoint (custom Anthropic-compatible endpoints resolve to
Providers.OPENAI, so they're unaffected). Now let me run the existing tests to make sure nothing breaks.Assistant
All existing initialize tests pass. Now let me run the Anthropic llm.spec.ts tests to make sure the native config logic is unaffected.
Assistant
Let me update my reproduction test to cover edge cases and verify the fix is robust.
Assistant
All edge cases pass. Now let me remove the temporary repro test file and verify the TypeScript compiles.
Assistant
TypeScript compiles cleanly. Let me run a broader set of agents tests to ensure nothing is broken.
Assistant
All agent tests pass. Let me also run the endpoints tests to make sure nothing is broken.
Assistant
All endpoint tests pass. Let me verify there are no other Anthropic tool-binding paths that bypass the initialize.ts concatenation (e.g., title/summarization flows).
Assistant
The title flow uses a separate
client.titleConvopath that doesn't bind tools, so no duplicate concern there. The main chat flow goes throughinitializeAgentwhere my fix lives. Let me review the final diff.Assistant
I notice the build steps I ran modified
package-lock.jsonandpackage.json(added anallowScriptsblock). These are unrelated to my fix and should be reverted so only my source change is submitted.Assistant
The diff is now clean — only the source change to
initialize.ts. Let me verify there are no untracked build artifacts that might interfere.Assistant
Let me check the Bedrock provider to understand if it also exposes Anthropic-native web search (to ensure my scoping is correct).
Assistant
Bedrock doesn't add a native web_search tool, confirming my scoping to
Providers.ANTHROPICis correct. Let me run the relevant tests one final time and lint the changed file.Assistant
All tests pass and lint is clean. Let me verify the api server-side agents tests aren't affected by checking whether they use the real
initializeAgentor mock it.Assistant
The api server tests use
jest.requireActual('@librechat/api')against the built dist. Let me check whether the api package has a built dist that would need rebuilding for the change to take effect, and rebuild it so the api server tests reflect the fix.Assistant
There's no
distdirectory for the api package (thelsreturned nothing). Let me check whether the api package dist exists at all. The api server tests usejest.requireActual('@librechat/api'). If there's no dist, how does it resolve? Let me check how@librechat/apiis linked.Actually wait, the
ls packages/api/distreturned nothing (empty), meaning no dist directory exists. So how do the api server tests resolve@librechat/api? Let me check the symlink.Assistant
So
@librechat/apisymlinks topackages/apiwhich has nodist. Let me check how the api server resolves it and run an api server agents test to see if it's affected by my change.Assistant
No moduleNameMapper for
@librechat/api. Let me check the full package.json exports of the api package.Assistant
The api package exports point to
./dist/index.jswhich doesn't exist. Let me try running an api server agents test to see whether it resolves and whether my change affects it.Assistant
The api server tests need to run from the
api/directory. Let me check the api test setup and run the relevant test from there.Assistant
The api server tests can't resolve
@librechat/apibecause the dist isn't built (pre-existing environment limitation). Let me build the api package so I can verify the api server tests pass with my change.Assistant
The api package built successfully. Let me verify the built dist contains my fix and run the api server agents tests.
Assistant
The built dist contains my fix. Now let me run the api server agents tests to verify they pass.
Assistant
All api server agents tests pass. Let me run a few more relevant api server tests (ToolService and agents client) to be thorough.
Assistant
All tests pass. Let me do a final review of the changes to be submitted.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.