Claus Ibsen created CAMEL-27630:
-----------------------------------

             Summary: camel-langchain4j-agent: exclude headers not trimmed, MCP 
OAuth token not refreshed, docs
                 Key: CAMEL-27630
                 URL: https://issues.apache.org/jira/browse/CAMEL-27630
             Project: Camel
          Issue Type: Bug
          Components: camel-langchain4j-agent
            Reporter: Claus Ibsen
             Fix For: 4.24.0


Found while improving the option descriptions of camel-langchain4j-agent for 
CAMEL-23832 (PR https://github.com/apache/camel/pull/30967).
These are separate problems of the component; the descriptions PR only 
documents the current behavior.

h4. 1. The exclude headers do not trim the names, so a list with spaces 
excludes nothing
{{LangChain4jAgentProducer.createCamelToolProvider()}} splits the {{tags}} 
option and the {{CamelLangChain4jAgentExcludeTags}} header with 
{{ToolsTagsHelper.splitTags()}}, which only calls {{split(",")}}. It then 
compares the parts without trimming them. {{discoverAiToolsByName()}} trims the 
tags later, so the spaces matter only for the exclusion.
With {{tags=a, b}} and the header {{b}}, the remaining tag is {{" b"}}, so it 
is not excluded, and the tools of {{b}} are still offered to the model. With 
{{tags=a,b}} and the header {{a, b}}, {{b}} is not excluded either.
{{createComposedToolProvider()}} has the same problem with 
{{CamelLangChain4jAgentExcludeMcpServers}}: with the header {{x, y}}, the MCP 
client with key {{y}} is not excluded.
The user sees no error. The model is still offered the tools that were meant to 
be excluded.
Proposed change: trim each part in {{ToolsTagsHelper.splitTags()}} and drop the 
empty parts. Every caller then gets the same result.

h4. 2. The OAuth token of an MCP server is fetched only once and never refreshed
With {{mcpServer.<name>.oauthProfile}}, 
{{LangChain4jMcpServerDefinition.buildTransport()}} calls 
{{OAuthHelper.resolveOAuthToken()}} once. It puts the token in a fixed 
{{Authorization: Bearer}} header ({{customHeaders(Map.of(...))}}) of the 
{{StreamableHttpMcpTransport}}. {{LangChain4jAgentProducer.doStart()}} builds 
the clients only when the producer starts.
When the token expires, every later call to the MCP server fails with an 
authentication error, until the route or the producer is restarted.
Proposed change: supply the header through a supplier that asks the OAuth 
profile for a valid token on each request, if the transport supports one, or 
rebuild the client when the token expires.

h4. 3. The CamelLangChain4jAgentResponseModel header is documented but never set
{{Headers.RESPONSE_MODEL}} ({{CamelLangChain4jAgentResponseModel}}) is listed 
in the component JSON and in the docs ({{ai-observability.adoc}} names it too). 
Neither {{LangChain4jAgentProducer.populateResultHeaders()}} nor any other code 
sets it, because the LangChain4j {{Result}} does not return the model name of 
the response.
Proposed change: remove the header from {{Headers}}, or deprecate it, or set it 
from the last {{ChatResponse}} if the agent can observe it, for example through 
the model listeners that are already used at observability level detailed.

h4. 4. The docs of withCompensateOnToolErrors describe another feature
The Javadoc of {{AgentConfiguration.withCompensateOnToolErrors()}} in 
camel-langchain4j-agent-api says it compensates "by sending the error back to 
the LLM for recovery". The table in {{langchain4j-agent-component.adoc}} says 
"Sends tool errors back to the LLM so it can recover instead of failing the 
exchange".
In LangChain4j 1.24.0, {{compensateOnToolErrors}} makes {{ToolService}} run the 
compensating actions (methods annotated with {{@CompensateFor}}) of the tools 
when a tool fails ({{CompensationReason.TOOL_EXECUTION_FAILED}}). Sending tool 
errors back to the model is done by {{withToolExecutionErrorHandler(...)}}.
Proposed change: correct the Javadoc and the table row. The option description 
was corrected in the PR above.

_Claude Code on behalf of davsclaus_



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to