Skip to content

Commit d5995c3

Browse files
Implement automatic tool registration on import (#862)
Co-authored-by: openhands <[email protected]>
1 parent 55cf62e commit d5995c3

File tree

68 files changed

+597
-358
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+597
-358
lines changed

examples/01_standalone_sdk/02_custom_tools.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
logger = get_logger(__name__)
3636

37-
3837
# --- Action / Observation ---
3938

4039

@@ -142,7 +141,6 @@ def create(
142141

143142
return [
144143
cls(
145-
name="grep",
146144
description=_GREP_DESCRIPTION,
147145
action_type=GrepAction,
148146
observation_type=GrepObservation,
@@ -180,11 +178,10 @@ def _make_bash_and_grep_tools(conv_state) -> list[ToolDefinition]:
180178
return [bash_tool, grep_tool]
181179

182180

183-
register_tool("FileEditorTool", FileEditorTool)
184181
register_tool("BashAndGrepToolSet", _make_bash_and_grep_tools)
185182

186183
tools = [
187-
Tool(name="FileEditorTool"),
184+
Tool(name=FileEditorTool.name),
188185
Tool(name="BashAndGrepToolSet"),
189186
]
190187

examples/01_standalone_sdk/03_activate_skill.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
KeywordTrigger,
1616
Skill,
1717
)
18-
from openhands.sdk.tool import Tool, register_tool
18+
from openhands.sdk.tool import Tool
1919
from openhands.tools.execute_bash import BashTool
2020
from openhands.tools.file_editor import FileEditorTool
2121

@@ -36,13 +36,11 @@
3636

3737
# Tools
3838
cwd = os.getcwd()
39-
register_tool("BashTool", BashTool)
40-
register_tool("FileEditorTool", FileEditorTool)
4139
tools = [
4240
Tool(
43-
name="BashTool",
41+
name=BashTool.name,
4442
),
45-
Tool(name="FileEditorTool"),
43+
Tool(name=FileEditorTool.name),
4644
]
4745

4846
agent_context = AgentContext(
@@ -73,11 +71,9 @@
7371
user_message_suffix="The first character of your response should be 'I'",
7472
)
7573

76-
7774
# Agent
7875
agent = Agent(llm=llm, tools=tools, agent_context=agent_context)
7976

80-
8177
llm_messages = [] # collect raw LLM messages
8278

8379

examples/01_standalone_sdk/05_use_llm_registry.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
TextContent,
1414
get_logger,
1515
)
16-
from openhands.sdk.tool import Tool, register_tool
16+
from openhands.sdk.tool import Tool
1717
from openhands.tools.execute_bash import BashTool
1818

1919

@@ -42,8 +42,7 @@
4242

4343
# Tools
4444
cwd = os.getcwd()
45-
register_tool("BashTool", BashTool)
46-
tools = [Tool(name="BashTool")]
45+
tools = [Tool(name=BashTool.name)]
4746

4847
# Agent
4948
agent = Agent(llm=llm, tools=tools)

examples/01_standalone_sdk/06_interactive_terminal_w_reasoning.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
LLMConvertibleEvent,
1111
get_logger,
1212
)
13-
from openhands.sdk.tool import Tool, register_tool
13+
from openhands.sdk.tool import Tool
1414
from openhands.tools.execute_bash import BashTool
1515

1616

@@ -30,10 +30,9 @@
3030

3131
# Tools
3232
cwd = os.getcwd()
33-
register_tool("BashTool", BashTool)
3433
tools = [
3534
Tool(
36-
name="BashTool",
35+
name=BashTool.name,
3736
params={"no_change_timeout_seconds": 3},
3837
)
3938
]

examples/01_standalone_sdk/07_mcp_integration.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
get_logger,
1212
)
1313
from openhands.sdk.security.llm_analyzer import LLMSecurityAnalyzer
14-
from openhands.sdk.tool import Tool, register_tool
14+
from openhands.sdk.tool import Tool
1515
from openhands.tools.execute_bash import BashTool
1616
from openhands.tools.file_editor import FileEditorTool
1717

@@ -31,11 +31,9 @@
3131
)
3232

3333
cwd = os.getcwd()
34-
register_tool("BashTool", BashTool)
35-
register_tool("FileEditorTool", FileEditorTool)
3634
tools = [
37-
Tool(name="BashTool"),
38-
Tool(name="FileEditorTool"),
35+
Tool(name=BashTool.name),
36+
Tool(name=FileEditorTool.name),
3937
]
4038

4139
# Add MCP Tools

examples/01_standalone_sdk/08_mcp_with_oauth.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
LLMConvertibleEvent,
1111
get_logger,
1212
)
13-
from openhands.sdk.tool import Tool, register_tool
13+
from openhands.sdk.tool import Tool
1414
from openhands.tools.execute_bash import BashTool
1515
from openhands.tools.file_editor import FileEditorTool
1616

@@ -30,13 +30,11 @@
3030
)
3131

3232
cwd = os.getcwd()
33-
register_tool("BashTool", BashTool)
34-
register_tool("FileEditorTool", FileEditorTool)
3533
tools = [
3634
Tool(
37-
name="BashTool",
35+
name=BashTool.name,
3836
),
39-
Tool(name="FileEditorTool"),
37+
Tool(name=FileEditorTool.name),
4038
]
4139

4240
mcp_config = {

examples/01_standalone_sdk/09_pause_example.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Agent,
1010
Conversation,
1111
)
12-
from openhands.sdk.tool import Tool, register_tool
12+
from openhands.sdk.tool import Tool
1313
from openhands.tools.execute_bash import BashTool
1414
from openhands.tools.file_editor import FileEditorTool
1515

@@ -27,20 +27,17 @@
2727
)
2828

2929
# Tools
30-
register_tool("BashTool", BashTool)
31-
register_tool("FileEditorTool", FileEditorTool)
3230
tools = [
3331
Tool(
34-
name="BashTool",
32+
name=BashTool.name,
3533
),
36-
Tool(name="FileEditorTool"),
34+
Tool(name=FileEditorTool.name),
3735
]
3836

3937
# Agent
4038
agent = Agent(llm=llm, tools=tools)
4139
conversation = Conversation(agent, workspace=os.getcwd())
4240

43-
4441
print("=" * 60)
4542
print("Pause and Continue Example")
4643
print("=" * 60)
@@ -75,7 +72,6 @@
7572
print(f"Agent status after pause: {conversation.state.execution_status}")
7673
print()
7774

78-
7975
# Phase 3: Send a new message while paused
8076
print("Phase 3: Sending a new message while agent is paused...")
8177
conversation.send_message(

examples/01_standalone_sdk/10_persistence.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
LLMConvertibleEvent,
1212
get_logger,
1313
)
14-
from openhands.sdk.tool import Tool, register_tool
14+
from openhands.sdk.tool import Tool
1515
from openhands.tools.execute_bash import BashTool
1616
from openhands.tools.file_editor import FileEditorTool
1717

@@ -32,11 +32,9 @@
3232

3333
# Tools
3434
cwd = os.getcwd()
35-
register_tool("BashTool", BashTool)
36-
register_tool("FileEditorTool", FileEditorTool)
3735
tools = [
38-
Tool(name="BashTool"),
39-
Tool(name="FileEditorTool"),
36+
Tool(name=BashTool.name),
37+
Tool(name=FileEditorTool.name),
4038
]
4139

4240
# Add MCP Tools

examples/01_standalone_sdk/11_async.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
get_logger,
1919
)
2020
from openhands.sdk.conversation.types import ConversationCallbackType
21-
from openhands.sdk.tool import Tool, register_tool
21+
from openhands.sdk.tool import Tool
2222
from openhands.sdk.utils.async_utils import AsyncCallbackWrapper
2323
from openhands.tools.execute_bash import BashTool
2424
from openhands.tools.file_editor import FileEditorTool
@@ -41,15 +41,12 @@
4141

4242
# Tools
4343
cwd = os.getcwd()
44-
register_tool("BashTool", BashTool)
45-
register_tool("FileEditorTool", FileEditorTool)
46-
register_tool("TaskTrackerTool", TaskTrackerTool)
4744
tools = [
4845
Tool(
49-
name="BashTool",
46+
name=BashTool.name,
5047
),
51-
Tool(name="FileEditorTool"),
52-
Tool(name="TaskTrackerTool"),
48+
Tool(name=FileEditorTool.name),
49+
Tool(name=TaskTrackerTool.name),
5350
]
5451

5552
# Agent

examples/01_standalone_sdk/12_custom_secrets.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Conversation,
99
)
1010
from openhands.sdk.conversation.secret_source import SecretSource
11-
from openhands.sdk.tool import Tool, register_tool
11+
from openhands.sdk.tool import Tool
1212
from openhands.tools.execute_bash import BashTool
1313
from openhands.tools.file_editor import FileEditorTool
1414

@@ -26,11 +26,9 @@
2626
)
2727

2828
# Tools
29-
register_tool("BashTool", BashTool)
30-
register_tool("FileEditorTool", FileEditorTool)
3129
tools = [
32-
Tool(name="BashTool"),
33-
Tool(name="FileEditorTool"),
30+
Tool(name=BashTool.name),
31+
Tool(name=FileEditorTool.name),
3432
]
3533

3634
# Agent

0 commit comments

Comments
 (0)