Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit 6496d41

Browse files
authored
Lorenze/better env vars setup enterprise tools (#343)
* refactor: remove token validation from EnterpriseActionKitToolAdapter and CrewaiEnterpriseTools This commit simplifies the initialization of the EnterpriseActionKitToolAdapter and CrewaiEnterpriseTools by removing the explicit validation for the enterprise action token. The token can now be set to None without raising an error, allowing for more flexible usage. * added loggers for monitoring * fixed typo * fix: enhance token handling in EnterpriseActionKitToolAdapter and CrewaiEnterpriseTools This commit improves the handling of the enterprise action token by allowing it to be fetched from environment variables if not provided. It adds checks to ensure the token is set before making API requests, enhancing robustness and flexibility. * removed redundancy * test: add new test for environment token fallback in CrewaiEnterpriseTools This update introduces a new test case to verify that the environment token is used when no token is provided during the initialization of CrewaiEnterpriseTools. Additionally, minor formatting adjustments were made to existing assertions for consistency. * test: update environment token test to clear environment variables This change modifies the test for CrewaiEnterpriseTools to ensure that the environment variables are cleared before setting the test token. This ensures a clean test environment and prevents potential interference from other tests. * drop redundancy
1 parent 923c7b0 commit 6496d41

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

crewai_tools/adapters/enterprise_adapter.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import os
2+
import json
13
import requests
2-
from pydantic import Field, create_model
34
from typing import List, Any, Dict, Optional
4-
import json
5+
from pydantic import Field, create_model
56
from crewai.tools import BaseTool
67

78
# DEFAULTS
@@ -146,6 +147,14 @@ def tools(self) -> List[BaseTool]:
146147
def _fetch_actions(self):
147148
"""Fetch available actions from the API."""
148149
try:
150+
if (
151+
self.enterprise_action_token is None
152+
or self.enterprise_action_token == ""
153+
):
154+
self.enterprise_action_token = os.environ.get(
155+
"CREWAI_ENTERPRISE_TOOLS_TOKEN"
156+
)
157+
149158
actions_url = f"{self.enterprise_action_kit_project_url}/{self.enterprise_action_kit_project_id}/actions"
150159
headers = {"Authorization": f"Bearer {self.enterprise_action_token}"}
151160
params = {"format": "json_schema"}

crewai_tools/tools/crewai_enterprise_tools/crewai_enterprise_tools.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ def CrewaiEnterpriseTools(
3131
Returns:
3232
A ToolCollection of BaseTool instances for enterprise actions
3333
"""
34-
if enterprise_token is None:
34+
35+
if enterprise_token is None or enterprise_token == "":
3536
enterprise_token = os.environ.get("CREWAI_ENTERPRISE_TOOLS_TOKEN")
36-
logger.warning("No enterprise token provided")
37+
if not enterprise_token:
38+
logger.warning("No enterprise token provided")
3739

3840
adapter_kwargs = {"enterprise_action_token": enterprise_token}
3941

tests/tools/crewai_enterprise_tools_test.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def setUp(self):
1515
self._create_mock_tool("tool3", "Tool 3 Description"),
1616
]
1717
self.adapter_patcher = patch(
18-
'crewai_tools.tools.crewai_enterprise_tools.crewai_enterprise_tools.EnterpriseActionKitToolAdapter'
18+
"crewai_tools.tools.crewai_enterprise_tools.crewai_enterprise_tools.EnterpriseActionKitToolAdapter"
1919
)
2020
self.MockAdapter = self.adapter_patcher.start()
2121

@@ -55,16 +55,21 @@ def test_uses_provided_parameters(self):
5555
CrewaiEnterpriseTools(
5656
enterprise_token="test-token",
5757
enterprise_action_kit_project_id="project-id",
58-
enterprise_action_kit_project_url="project-url"
58+
enterprise_action_kit_project_url="project-url",
5959
)
6060

6161
self.MockAdapter.assert_called_once_with(
6262
enterprise_action_token="test-token",
6363
enterprise_action_kit_project_id="project-id",
64-
enterprise_action_kit_project_url="project-url"
64+
enterprise_action_kit_project_url="project-url",
6565
)
6666

6767
@patch.dict(os.environ, {"CREWAI_ENTERPRISE_TOOLS_TOKEN": "env-token"})
6868
def test_uses_environment_token(self):
6969
CrewaiEnterpriseTools()
70-
self.MockAdapter.assert_called_once_with(enterprise_action_token="env-token")
70+
self.MockAdapter.assert_called_once_with(enterprise_action_token="env-token")
71+
72+
@patch.dict(os.environ, {"CREWAI_ENTERPRISE_TOOLS_TOKEN": "env-token"})
73+
def test_uses_environment_token_when_no_token_provided(self):
74+
CrewaiEnterpriseTools(enterprise_token="")
75+
self.MockAdapter.assert_called_once_with(enterprise_action_token="env-token")

0 commit comments

Comments
 (0)