feat(toolbox-langchain): Support per-invocation auth via RunnableConfig
#291
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a major enhancement to the
toolbox-langchain
package by adding support for dynamic, per-invocation authentication. This is achieved by readingauth_token_getters
from LangChain's standardRunnableConfig
, enablingToolboxTool
to be used safely and effectively in multi-user environments like LangGraph.Motivation
Currently, authentication tokens can only be provided to a
ToolboxTool
at initialization time, either viaToolboxClient.load_tool/load_toolset
or by callingtool.add_auth_token_getters()
on the tool instance. This static binding of credentials poses a significant challenge in modern agentic frameworks like LangGraph.Challenge
In LangGraph, a single graph containing tool instances is often created once and then shared across multiple users and requests. It is insecure and impractical to configure these shared tool instances with any single user's credentials. The required credentials must be provided dynamically, on a per-request basis.
Proposed Solution
This PR solves this problem by introducing a third, invocation-time method for providing auth. It leverages LangChain's idiomatic
RunnableConfig
as the vehicle for passing request-specific authentication, makingtoolbox-langchain
fully compatible with multi-tenant and shared-use patterns.Description of Changes
The core of this change lies in how the
ToolboxTool
handles an invocation:_arun
/_run
) is updated to accept theconfig: RunnableConfig
argument, which is standard in the LangChain.config["configurable"]["auth_token_getters"]
.auth_token_getters
are found in the config, the tool:a. Introspects its own authentication and authorization requirements (using the properties exposed in fix(toolbox-core): Expose authorization token requirements on
ToolboxTool
#294).b. Creates a temporary, in-memory copy of the underlying proxied
ToolboxTool
. This is critical, as it ensures the original shared tool instance is never mutated.auth_token_getters
from theconfig
are applied to this new, temporary copy of the tool using itsadd_auth_token_getters
method.This mechanism provides a thread-safe and secure way to handle user-specific credentials without affecting the shared state of the primary tool in the graph.
Usage Example