Skip to content

feat: Add support for checking tool-level auth before tool invocation. #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2025
Merged
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
20 changes: 19 additions & 1 deletion src/toolbox_langchain/async_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,15 @@ def __validate_auth(self, strict: bool = True) -> None:
PermissionError: If strict is True and any required authentication
sources are not registered.
"""
is_authenticated: bool = not self.__schema.authRequired
params_missing_auth: list[str] = []

# Check tool for at least 1 required auth source
for src in self.__schema.authRequired:
if src in self.__auth_tokens:
is_authenticated = True
break

# Check each parameter for at least 1 required auth source
for param in self.__auth_params:
if not param.authSources:
Expand All @@ -210,9 +217,20 @@ def __validate_auth(self, strict: bool = True) -> None:
if not has_auth:
params_missing_auth.append(param.name)

messages: list[str] = []

if not is_authenticated:
messages.append(
f"Tool {self.__name} requires authentication, but no valid authentication sources are registered. Please register the required sources before use."
)

if params_missing_auth:
message = f"Parameter(s) `{', '.join(params_missing_auth)}` of tool {self.__name} require authentication, but no valid authentication sources are registered. Please register the required sources before use."
messages.append(
f"Parameter(s) `{', '.join(params_missing_auth)}` of tool {self.__name} require authentication, but no valid authentication sources are registered. Please register the required sources before use."
)

if messages:
message = "\n\n".join(messages)
if strict:
raise PermissionError(message)
warn(message)
Expand Down
1 change: 1 addition & 0 deletions src/toolbox_langchain/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ToolSchema(BaseModel):

description: str
parameters: list[ParameterSchema]
authRequired: list[str] = []


class ManifestSchema(BaseModel):
Expand Down