agno 2.3.14__py3-none-any.whl → 2.3.16__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
agno/os/auth.py CHANGED
@@ -1,3 +1,4 @@
1
+ from os import getenv
1
2
  from typing import List, Set
2
3
 
3
4
  from fastapi import Depends, HTTPException, Request
@@ -10,18 +11,43 @@ from agno.os.settings import AgnoAPISettings
10
11
  security = HTTPBearer(auto_error=False)
11
12
 
12
13
 
14
+ def _is_jwt_configured() -> bool:
15
+ """Check if JWT authentication is configured via environment variables.
16
+
17
+ This covers cases where JWT middleware is set up manually (not via authorization=True).
18
+ """
19
+ return bool(getenv("JWT_VERIFICATION_KEY") or getenv("JWT_JWKS_FILE"))
20
+
21
+
13
22
  def get_authentication_dependency(settings: AgnoAPISettings):
14
23
  """
15
24
  Create an authentication dependency function for FastAPI routes.
16
25
 
26
+ This handles security key authentication (OS_SECURITY_KEY).
27
+ When JWT authorization is enabled (via authorization=True, JWT environment variables,
28
+ or manually added JWT middleware), this dependency is skipped as JWT middleware
29
+ handles authentication.
30
+
17
31
  Args:
18
- settings: The API settings containing the security key
32
+ settings: The API settings containing the security key and authorization flag
19
33
 
20
34
  Returns:
21
35
  A dependency function that can be used with FastAPI's Depends()
22
36
  """
23
37
 
24
- async def auth_dependency(credentials: HTTPAuthorizationCredentials = Depends(security)) -> bool:
38
+ async def auth_dependency(request: Request, credentials: HTTPAuthorizationCredentials = Depends(security)) -> bool:
39
+ # If JWT authorization is enabled via settings (authorization=True on AgentOS)
40
+ if settings and settings.authorization_enabled:
41
+ return True
42
+
43
+ # Check if JWT middleware has already handled authentication
44
+ if getattr(request.state, "authenticated", False):
45
+ return True
46
+
47
+ # Also skip if JWT is configured via environment variables
48
+ if _is_jwt_configured():
49
+ return True
50
+
25
51
  # If no security key is set, skip authentication entirely
26
52
  if not settings or not settings.os_security_key:
27
53
  return True
@@ -45,13 +71,24 @@ def validate_websocket_token(token: str, settings: AgnoAPISettings) -> bool:
45
71
  """
46
72
  Validate a bearer token for WebSocket authentication (legacy os_security_key method).
47
73
 
74
+ When JWT authorization is enabled (via authorization=True or JWT environment variables),
75
+ this validation is skipped as JWT middleware handles authentication.
76
+
48
77
  Args:
49
78
  token: The bearer token to validate
50
- settings: The API settings containing the security key
79
+ settings: The API settings containing the security key and authorization flag
51
80
 
52
81
  Returns:
53
82
  True if the token is valid or authentication is disabled, False otherwise
54
83
  """
84
+ # If JWT authorization is enabled, skip security key validation
85
+ if settings and settings.authorization_enabled:
86
+ return True
87
+
88
+ # Also skip if JWT is configured via environment variables (manual JWT middleware setup)
89
+ if _is_jwt_configured():
90
+ return True
91
+
55
92
  # If no security key is set, skip authentication entirely
56
93
  if not settings or not settings.os_security_key:
57
94
  return True
agno/os/router.py CHANGED
@@ -1,16 +1,11 @@
1
- from typing import TYPE_CHECKING, List, Optional, Union, cast
1
+ from typing import TYPE_CHECKING, List, Union, cast
2
2
 
3
3
  from fastapi import (
4
4
  APIRouter,
5
5
  Depends,
6
- HTTPException,
7
6
  )
8
- from fastapi.responses import JSONResponse
9
- from packaging import version
10
7
 
11
8
  from agno.agent.agent import Agent
12
- from agno.db.base import AsyncBaseDb
13
- from agno.db.migrations.manager import MigrationManager
14
9
  from agno.os.auth import get_authentication_dependency
15
10
  from agno.os.schema import (
16
11
  AgentSummaryResponse,
@@ -26,9 +21,6 @@ from agno.os.schema import (
26
21
  WorkflowSummaryResponse,
27
22
  )
28
23
  from agno.os.settings import AgnoAPISettings
29
- from agno.os.utils import (
30
- get_db,
31
- )
32
24
  from agno.team.team import Team
33
25
 
34
26
  if TYPE_CHECKING:
@@ -207,52 +199,4 @@ def get_base_router(
207
199
 
208
200
  return list(unique_models.values())
209
201
 
210
- # -- Database Migration routes ---
211
- @router.post(
212
- "/databases/{db_id}/migrate",
213
- tags=["Database"],
214
- operation_id="migrate_database",
215
- summary="Migrate Database",
216
- description=(
217
- "Migrate the given database schema to the given target version. "
218
- "If a target version is not provided, the database will be migrated to the latest version. "
219
- ),
220
- responses={
221
- 200: {
222
- "description": "Database migrated successfully",
223
- "content": {
224
- "application/json": {
225
- "example": {"message": "Database migrated successfully to version 3.0.0"},
226
- }
227
- },
228
- },
229
- 404: {"description": "Database not found", "model": NotFoundResponse},
230
- 500: {"description": "Failed to migrate database", "model": InternalServerErrorResponse},
231
- },
232
- )
233
- async def migrate_database(db_id: str, target_version: Optional[str] = None):
234
- db = await get_db(os.dbs, db_id)
235
- if not db:
236
- raise HTTPException(status_code=404, detail="Database not found")
237
-
238
- if target_version:
239
- # Use the session table as proxy for the database schema version
240
- if isinstance(db, AsyncBaseDb):
241
- current_version = await db.get_latest_schema_version(db.session_table_name)
242
- else:
243
- current_version = db.get_latest_schema_version(db.session_table_name)
244
-
245
- if version.parse(target_version) > version.parse(current_version): # type: ignore
246
- MigrationManager(db).up(target_version) # type: ignore
247
- else:
248
- MigrationManager(db).down(target_version) # type: ignore
249
-
250
- # If the target version is not provided, migrate to the latest version
251
- else:
252
- MigrationManager(db).up() # type: ignore
253
-
254
- return JSONResponse(
255
- content={"message": f"Database migrated successfully to version {target_version}"}, status_code=200
256
- )
257
-
258
202
  return router
@@ -0,0 +1,150 @@
1
+ from typing import TYPE_CHECKING, Optional
2
+
3
+ from fastapi import (
4
+ APIRouter,
5
+ Depends,
6
+ HTTPException,
7
+ )
8
+ from fastapi.responses import JSONResponse
9
+ from packaging import version
10
+
11
+ from agno.db.base import AsyncBaseDb
12
+ from agno.db.migrations.manager import MigrationManager
13
+ from agno.os.auth import get_authentication_dependency
14
+ from agno.os.schema import (
15
+ BadRequestResponse,
16
+ InternalServerErrorResponse,
17
+ NotFoundResponse,
18
+ UnauthenticatedResponse,
19
+ ValidationErrorResponse,
20
+ )
21
+ from agno.os.settings import AgnoAPISettings
22
+ from agno.os.utils import (
23
+ get_db,
24
+ )
25
+
26
+ if TYPE_CHECKING:
27
+ from agno.os.app import AgentOS
28
+
29
+
30
+ def get_database_router(
31
+ os: "AgentOS",
32
+ settings: AgnoAPISettings = AgnoAPISettings(),
33
+ ) -> APIRouter:
34
+ """Create the database router with comprehensive OpenAPI documentation."""
35
+ router = APIRouter(
36
+ dependencies=[Depends(get_authentication_dependency(settings))],
37
+ responses={
38
+ 400: {"description": "Bad Request", "model": BadRequestResponse},
39
+ 401: {"description": "Unauthorized", "model": UnauthenticatedResponse},
40
+ 404: {"description": "Not Found", "model": NotFoundResponse},
41
+ 422: {"description": "Validation Error", "model": ValidationErrorResponse},
42
+ 500: {"description": "Internal Server Error", "model": InternalServerErrorResponse},
43
+ },
44
+ )
45
+
46
+ async def _migrate_single_db(db, target_version: Optional[str] = None) -> None:
47
+ """Migrate a single database."""
48
+ if target_version:
49
+ # Use the session table as proxy for the database schema version
50
+ if isinstance(db, AsyncBaseDb):
51
+ current_version = await db.get_latest_schema_version(db.session_table_name)
52
+ else:
53
+ current_version = db.get_latest_schema_version(db.session_table_name)
54
+
55
+ if version.parse(target_version) > version.parse(current_version): # type: ignore
56
+ await MigrationManager(db).up(target_version) # type: ignore
57
+ else:
58
+ await MigrationManager(db).down(target_version) # type: ignore
59
+ else:
60
+ # If the target version is not provided, migrate to the latest version
61
+ await MigrationManager(db).up() # type: ignore
62
+
63
+ @router.post(
64
+ "/databases/all/migrate",
65
+ tags=["Database"],
66
+ operation_id="migrate_all_databases",
67
+ summary="Migrate All Databases",
68
+ description=(
69
+ "Migrate all database schemas to the given target version. "
70
+ "If a target version is not provided, all databases will be migrated to the latest version."
71
+ ),
72
+ responses={
73
+ 200: {
74
+ "description": "All databases migrated successfully",
75
+ "content": {
76
+ "application/json": {
77
+ "example": {"message": "All databases migrated successfully to version 3.0.0"},
78
+ }
79
+ },
80
+ },
81
+ 500: {"description": "Failed to migrate databases", "model": InternalServerErrorResponse},
82
+ },
83
+ )
84
+ async def migrate_all_databases(target_version: Optional[str] = None):
85
+ """Migrate all databases."""
86
+ all_dbs = {db.id: db for db_id, dbs in os.dbs.items() for db in dbs}
87
+ failed_dbs: dict[str, str] = {}
88
+
89
+ for db_id, db in all_dbs.items():
90
+ try:
91
+ await _migrate_single_db(db, target_version)
92
+ except Exception as e:
93
+ failed_dbs[db_id] = str(e)
94
+
95
+ version_msg = f"version {target_version}" if target_version else "latest version"
96
+ migrated_count = len(all_dbs) - len(failed_dbs)
97
+
98
+ if failed_dbs:
99
+ return JSONResponse(
100
+ content={
101
+ "message": f"Migrated {migrated_count}/{len(all_dbs)} databases to {version_msg}",
102
+ "failed": failed_dbs,
103
+ },
104
+ status_code=207, # Multi-Status
105
+ )
106
+
107
+ return JSONResponse(
108
+ content={"message": f"All databases migrated successfully to {version_msg}"}, status_code=200
109
+ )
110
+
111
+ @router.post(
112
+ "/databases/{db_id}/migrate",
113
+ tags=["Database"],
114
+ operation_id="migrate_database",
115
+ summary="Migrate Database",
116
+ description=(
117
+ "Migrate the given database schema to the given target version. "
118
+ "If a target version is not provided, the database will be migrated to the latest version."
119
+ ),
120
+ responses={
121
+ 200: {
122
+ "description": "Database migrated successfully",
123
+ "content": {
124
+ "application/json": {
125
+ "example": {"message": "Database migrated successfully to version 3.0.0"},
126
+ }
127
+ },
128
+ },
129
+ 404: {"description": "Database not found", "model": NotFoundResponse},
130
+ 500: {"description": "Failed to migrate database", "model": InternalServerErrorResponse},
131
+ },
132
+ )
133
+ async def migrate_database(db_id: str, target_version: Optional[str] = None):
134
+ db = await get_db(os.dbs, db_id)
135
+ if not db:
136
+ raise HTTPException(status_code=404, detail="Database not found")
137
+
138
+ try:
139
+ await _migrate_single_db(db, target_version)
140
+
141
+ version_msg = f"version {target_version}" if target_version else "latest version"
142
+ return JSONResponse(
143
+ content={"message": f"Database migrated successfully to {version_msg}"}, status_code=200
144
+ )
145
+ except HTTPException:
146
+ raise
147
+ except Exception as e:
148
+ raise HTTPException(status_code=500, detail=f"Failed to migrate database: {str(e)}")
149
+
150
+ return router
agno/os/settings.py CHANGED
@@ -20,6 +20,9 @@ class AgnoAPISettings(BaseSettings):
20
20
  # Authentication settings
21
21
  os_security_key: Optional[str] = Field(default=None, description="Bearer token for API authentication")
22
22
 
23
+ # Authorization flag - when True, JWT middleware handles auth and security key validation is skipped
24
+ authorization_enabled: bool = Field(default=False, description="Whether JWT authorization is enabled")
25
+
23
26
  # Cors origin list to allow requests from.
24
27
  # This list is set using the set_cors_origin_list validator
25
28
  cors_origin_list: Optional[List[str]] = Field(default=None, validate_default=True)
agno/run/agent.py CHANGED
@@ -153,6 +153,7 @@ class RunEvent(str, Enum):
153
153
 
154
154
  tool_call_started = "ToolCallStarted"
155
155
  tool_call_completed = "ToolCallCompleted"
156
+ tool_call_error = "ToolCallError"
156
157
 
157
158
  reasoning_started = "ReasoningStarted"
158
159
  reasoning_step = "ReasoningStep"
@@ -405,6 +406,13 @@ class ToolCallCompletedEvent(BaseAgentRunEvent):
405
406
  audio: Optional[List[Audio]] = None # Audio produced by the tool call
406
407
 
407
408
 
409
+ @dataclass
410
+ class ToolCallErrorEvent(BaseAgentRunEvent):
411
+ event: str = RunEvent.tool_call_error.value
412
+ tool: Optional[ToolExecution] = None
413
+ error: Optional[str] = None
414
+
415
+
408
416
  @dataclass
409
417
  class ParserModelResponseStartedEvent(BaseAgentRunEvent):
410
418
  event: str = RunEvent.parser_model_response_started.value
@@ -459,6 +467,7 @@ RunOutputEvent = Union[
459
467
  SessionSummaryCompletedEvent,
460
468
  ToolCallStartedEvent,
461
469
  ToolCallCompletedEvent,
470
+ ToolCallErrorEvent,
462
471
  ParserModelResponseStartedEvent,
463
472
  ParserModelResponseCompletedEvent,
464
473
  OutputModelResponseStartedEvent,
@@ -492,6 +501,7 @@ RUN_EVENT_TYPE_REGISTRY = {
492
501
  RunEvent.session_summary_completed.value: SessionSummaryCompletedEvent,
493
502
  RunEvent.tool_call_started.value: ToolCallStartedEvent,
494
503
  RunEvent.tool_call_completed.value: ToolCallCompletedEvent,
504
+ RunEvent.tool_call_error.value: ToolCallErrorEvent,
495
505
  RunEvent.parser_model_response_started.value: ParserModelResponseStartedEvent,
496
506
  RunEvent.parser_model_response_completed.value: ParserModelResponseCompletedEvent,
497
507
  RunEvent.output_model_response_started.value: OutputModelResponseStartedEvent,
agno/run/base.py CHANGED
@@ -51,6 +51,7 @@ class BaseRunOutputEvent:
51
51
  "session_summary",
52
52
  "metrics",
53
53
  "run_input",
54
+ "requirements",
54
55
  ]
55
56
  }
56
57
 
@@ -138,6 +139,9 @@ class BaseRunOutputEvent:
138
139
  if hasattr(self, "run_input") and self.run_input is not None:
139
140
  _dict["run_input"] = self.run_input.to_dict()
140
141
 
142
+ if hasattr(self, "requirements") and self.requirements is not None:
143
+ _dict["requirements"] = [req.to_dict() if hasattr(req, "to_dict") else req for req in self.requirements]
144
+
141
145
  return _dict
142
146
 
143
147
  def to_json(self, separators=(", ", ": "), indent: Optional[int] = 2) -> str:
@@ -219,6 +223,21 @@ class BaseRunOutputEvent:
219
223
 
220
224
  data["run_input"] = RunInput.from_dict(run_input)
221
225
 
226
+ # Handle requirements
227
+
228
+ # Handle requirements
229
+ requirements_data = data.pop("requirements", None)
230
+ if requirements_data is not None:
231
+ from agno.run.requirement import RunRequirement
232
+
233
+ requirements_list: List[RunRequirement] = []
234
+ for item in requirements_data:
235
+ if isinstance(item, RunRequirement):
236
+ requirements_list.append(item)
237
+ elif isinstance(item, dict):
238
+ requirements_list.append(RunRequirement.from_dict(item))
239
+ data["requirements"] = requirements_list if requirements_list else None
240
+
222
241
  # Filter data to only include fields that are actually defined in the target class
223
242
  from dataclasses import fields
224
243
 
agno/run/team.py CHANGED
@@ -146,6 +146,7 @@ class TeamRunEvent(str, Enum):
146
146
 
147
147
  tool_call_started = "TeamToolCallStarted"
148
148
  tool_call_completed = "TeamToolCallCompleted"
149
+ tool_call_error = "TeamToolCallError"
149
150
 
150
151
  reasoning_started = "TeamReasoningStarted"
151
152
  reasoning_step = "TeamReasoningStep"
@@ -378,6 +379,13 @@ class ToolCallCompletedEvent(BaseTeamRunEvent):
378
379
  audio: Optional[List[Audio]] = None # Audio produced by the tool call
379
380
 
380
381
 
382
+ @dataclass
383
+ class ToolCallErrorEvent(BaseTeamRunEvent):
384
+ event: str = TeamRunEvent.tool_call_error.value
385
+ tool: Optional[ToolExecution] = None
386
+ error: Optional[str] = None
387
+
388
+
381
389
  @dataclass
382
390
  class ParserModelResponseStartedEvent(BaseTeamRunEvent):
383
391
  event: str = TeamRunEvent.parser_model_response_started.value
@@ -428,6 +436,7 @@ TeamRunOutputEvent = Union[
428
436
  SessionSummaryCompletedEvent,
429
437
  ToolCallStartedEvent,
430
438
  ToolCallCompletedEvent,
439
+ ToolCallErrorEvent,
431
440
  ParserModelResponseStartedEvent,
432
441
  ParserModelResponseCompletedEvent,
433
442
  OutputModelResponseStartedEvent,
@@ -458,6 +467,7 @@ TEAM_RUN_EVENT_TYPE_REGISTRY = {
458
467
  TeamRunEvent.session_summary_completed.value: SessionSummaryCompletedEvent,
459
468
  TeamRunEvent.tool_call_started.value: ToolCallStartedEvent,
460
469
  TeamRunEvent.tool_call_completed.value: ToolCallCompletedEvent,
470
+ TeamRunEvent.tool_call_error.value: ToolCallErrorEvent,
461
471
  TeamRunEvent.parser_model_response_started.value: ParserModelResponseStartedEvent,
462
472
  TeamRunEvent.parser_model_response_completed.value: ParserModelResponseCompletedEvent,
463
473
  TeamRunEvent.output_model_response_started.value: OutputModelResponseStartedEvent,
agno/team/team.py CHANGED
@@ -129,6 +129,7 @@ from agno.utils.events import (
129
129
  create_team_session_summary_completed_event,
130
130
  create_team_session_summary_started_event,
131
131
  create_team_tool_call_completed_event,
132
+ create_team_tool_call_error_event,
132
133
  create_team_tool_call_started_event,
133
134
  handle_event,
134
135
  )
@@ -3831,6 +3832,15 @@ class Team:
3831
3832
  events_to_skip=self.events_to_skip,
3832
3833
  store_events=self.store_events,
3833
3834
  )
3835
+ if tool_call.tool_call_error:
3836
+ yield handle_event( # type: ignore
3837
+ create_team_tool_call_error_event(
3838
+ from_run_response=run_response, tool=tool_call, error=str(tool_call.result)
3839
+ ),
3840
+ run_response,
3841
+ events_to_skip=self.events_to_skip,
3842
+ store_events=self.store_events,
3843
+ )
3834
3844
 
3835
3845
  if stream_events:
3836
3846
  if reasoning_step is not None:
agno/tools/toolkit.py CHANGED
@@ -1,5 +1,5 @@
1
1
  from collections import OrderedDict
2
- from typing import Any, Callable, Dict, List, Optional
2
+ from typing import Any, Callable, Dict, List, Optional, Sequence, Union
3
3
 
4
4
  from agno.tools.function import Function
5
5
  from agno.utils.log import log_debug, log_warning, logger
@@ -13,7 +13,7 @@ class Toolkit:
13
13
  def __init__(
14
14
  self,
15
15
  name: str = "toolkit",
16
- tools: List[Callable] = [],
16
+ tools: Sequence[Union[Callable[..., Any], Function]] = [],
17
17
  instructions: Optional[str] = None,
18
18
  add_instructions: bool = False,
19
19
  include_tools: Optional[list[str]] = None,
@@ -31,7 +31,7 @@ class Toolkit:
31
31
 
32
32
  Args:
33
33
  name: A descriptive name for the toolkit
34
- tools: List of tools to include in the toolkit
34
+ tools: List of tools to include in the toolkit (can be callables or Function objects from @tool decorator)
35
35
  instructions: Instructions for the toolkit
36
36
  add_instructions: Whether to add instructions to the toolkit
37
37
  include_tools: List of tool names to include in the toolkit
@@ -46,7 +46,7 @@ class Toolkit:
46
46
  show_result_tools (Optional[List[str]]): List of function names whose results should be shown.
47
47
  """
48
48
  self.name: str = name
49
- self.tools: List[Callable] = tools
49
+ self.tools: Sequence[Union[Callable[..., Any], Function]] = tools
50
50
  self.functions: Dict[str, Function] = OrderedDict()
51
51
  self.instructions: Optional[str] = instructions
52
52
  self.add_instructions: bool = add_instructions
@@ -58,7 +58,9 @@ class Toolkit:
58
58
  self.show_result_tools: list[str] = show_result_tools or []
59
59
 
60
60
  self._check_tools_filters(
61
- available_tools=[tool.__name__ for tool in tools], include_tools=include_tools, exclude_tools=exclude_tools
61
+ available_tools=[self._get_tool_name(tool) for tool in tools],
62
+ include_tools=include_tools,
63
+ exclude_tools=exclude_tools,
62
64
  )
63
65
 
64
66
  self.include_tools = include_tools
@@ -72,6 +74,12 @@ class Toolkit:
72
74
  if auto_register and self.tools:
73
75
  self._register_tools()
74
76
 
77
+ def _get_tool_name(self, tool: Union[Callable[..., Any], Function]) -> str:
78
+ """Get the name of a tool, whether it's a Function or callable."""
79
+ if isinstance(tool, Function):
80
+ return tool.name
81
+ return tool.__name__
82
+
75
83
  def _check_tools_filters(
76
84
  self,
77
85
  available_tools: List[str],
@@ -104,22 +112,45 @@ class Toolkit:
104
112
  f"External execution required tool(s) not present in the toolkit: {', '.join(missing_external_execution_required)}"
105
113
  )
106
114
 
115
+ if self.stop_after_tool_call_tools:
116
+ missing_stop_after_tool_call = set(self.stop_after_tool_call_tools) - set(available_tools)
117
+ if missing_stop_after_tool_call:
118
+ log_warning(
119
+ f"Stop after tool call tool(s) not present in the toolkit: {', '.join(missing_stop_after_tool_call)}"
120
+ )
121
+
122
+ if self.show_result_tools:
123
+ missing_show_result = set(self.show_result_tools) - set(available_tools)
124
+ if missing_show_result:
125
+ log_warning(f"Show result tool(s) not present in the toolkit: {', '.join(missing_show_result)}")
126
+
107
127
  def _register_tools(self) -> None:
108
128
  """Register all tools."""
109
129
  for tool in self.tools:
110
130
  self.register(tool)
111
131
 
112
- def register(self, function: Callable[..., Any], name: Optional[str] = None):
132
+ def register(self, function: Union[Callable[..., Any], Function], name: Optional[str] = None) -> None:
113
133
  """Register a function with the toolkit.
114
134
 
135
+ This method supports both regular callables and Function objects (from @tool decorator).
136
+ When a Function object is passed (e.g., from a @tool decorated method), it will:
137
+ 1. Extract the configuration from the Function object
138
+ 2. Look for a bound method with the same name on `self`
139
+ 3. Create a new Function with the bound method as entrypoint, preserving decorator settings
140
+
115
141
  Args:
116
- function: The callable to register
142
+ function: The callable or Function object to register
117
143
  name: Optional custom name for the function
118
144
 
119
145
  Returns:
120
146
  The registered function
121
147
  """
122
148
  try:
149
+ # Handle Function objects (from @tool decorator)
150
+ if isinstance(function, Function):
151
+ return self._register_decorated_tool(function, name)
152
+
153
+ # Handle regular callables
123
154
  tool_name = name or function.__name__
124
155
  if self.include_tools is not None and tool_name not in self.include_tools:
125
156
  return
@@ -140,9 +171,89 @@ class Toolkit:
140
171
  self.functions[f.name] = f
141
172
  log_debug(f"Function: {f.name} registered with {self.name}")
142
173
  except Exception as e:
143
- logger.warning(f"Failed to create Function for: {function.__name__}")
174
+ func_name = self._get_tool_name(function)
175
+ logger.warning(f"Failed to create Function for: {func_name}")
144
176
  raise e
145
177
 
178
+ def _register_decorated_tool(self, function: Function, name: Optional[str] = None) -> None:
179
+ """Register a Function object from @tool decorator, binding it to self.
180
+
181
+ When @tool decorator is used on a class method, it creates a Function with an unbound
182
+ method as entrypoint. This method creates a bound version of the entrypoint that
183
+ includes `self`, preserving all decorator settings.
184
+
185
+ Args:
186
+ function: The Function object from @tool decorator
187
+ name: Optional custom name override
188
+ """
189
+ import inspect
190
+
191
+ tool_name = name or function.name
192
+ if self.include_tools is not None and len(self.include_tools) > 0 and tool_name not in self.include_tools:
193
+ return
194
+ if self.exclude_tools is not None and len(self.exclude_tools) > 0 and tool_name in self.exclude_tools:
195
+ return
196
+
197
+ # Get the original entrypoint from the Function
198
+ if function.entrypoint is None:
199
+ log_warning(f"Function '{tool_name}' has no entrypoint, skipping registration")
200
+ return
201
+
202
+ original_func = function.entrypoint
203
+
204
+ # Check if the function expects 'self' as first argument (i.e., it's an unbound method)
205
+ sig = inspect.signature(original_func)
206
+ params = list(sig.parameters.keys())
207
+
208
+ if params and params[0] == "self":
209
+ # Create a bound method by wrapping the function to include self
210
+ def make_bound_method(func, instance):
211
+ def bound(*args, **kwargs):
212
+ return func(instance, *args, **kwargs)
213
+
214
+ # Preserve function metadata for debugging
215
+ bound.__name__ = getattr(func, "__name__", tool_name)
216
+ bound.__doc__ = getattr(func, "__doc__", None)
217
+ return bound
218
+
219
+ bound_method = make_bound_method(original_func, self)
220
+ else:
221
+ # Function doesn't expect self (e.g., static method or already bound)
222
+ bound_method = original_func
223
+
224
+ # decorator settings take precedence, then toolkit settings
225
+ stop_after = function.stop_after_tool_call or tool_name in self.stop_after_tool_call_tools
226
+ show_result = function.show_result or tool_name in self.show_result_tools or stop_after
227
+ requires_confirmation = function.requires_confirmation or tool_name in self.requires_confirmation_tools
228
+ external_execution = function.external_execution or tool_name in self.external_execution_required_tools
229
+
230
+ # Create new Function with bound method, preserving decorator settings
231
+ f = Function(
232
+ name=tool_name,
233
+ description=function.description,
234
+ parameters=function.parameters,
235
+ strict=function.strict,
236
+ instructions=function.instructions,
237
+ add_instructions=function.add_instructions,
238
+ entrypoint=bound_method,
239
+ skip_entrypoint_processing=True, # Parameters already processed by decorator
240
+ show_result=show_result,
241
+ stop_after_tool_call=stop_after,
242
+ pre_hook=function.pre_hook,
243
+ post_hook=function.post_hook,
244
+ tool_hooks=function.tool_hooks,
245
+ requires_confirmation=requires_confirmation,
246
+ requires_user_input=function.requires_user_input,
247
+ user_input_fields=function.user_input_fields,
248
+ user_input_schema=function.user_input_schema,
249
+ external_execution=external_execution,
250
+ cache_results=function.cache_results if function.cache_results else self.cache_results,
251
+ cache_dir=function.cache_dir if function.cache_dir else self.cache_dir,
252
+ cache_ttl=function.cache_ttl if function.cache_ttl != 3600 else self.cache_ttl,
253
+ )
254
+ self.functions[f.name] = f
255
+ log_debug(f"Function: {f.name} registered with {self.name} (from @tool decorator)")
256
+
146
257
  @property
147
258
  def requires_connect(self) -> bool:
148
259
  """Whether the toolkit requires connection management."""