11#!/usr/bin/env python
22"""
3- Example demonstrating utility tools for dynamic tool discovery and execution .
3+ Example demonstrating dynamic tool discovery using search_tool .
44
5- Utility tools allow AI agents to search for relevant tools based on natural language queries
6- and execute them dynamically without hardcoding tool names.
5+ The search tool allows AI agents to discover relevant tools based on natural language
6+ queries without hardcoding tool names.
77
88Prerequisites:
99- STACKONE_API_KEY environment variable set
1212
1313This example is runnable with the following command:
1414```bash
15- uv run examples/utility_tools_example .py
15+ uv run examples/search_tool_example .py
1616```
1717"""
1818
3131_account_ids = [aid .strip () for aid in os .getenv ("STACKONE_ACCOUNT_ID" , "" ).split ("," ) if aid .strip ()]
3232
3333
34- def example_utility_tools_basic ():
35- """Basic example of using utility tools for tool discovery"""
34+ def example_search_tool_basic ():
35+ """Basic example of using the search tool for tool discovery"""
3636 print ("Example 1: Dynamic tool discovery\n " )
3737
3838 # Initialize StackOne toolset
@@ -46,20 +46,20 @@ def example_utility_tools_basic():
4646 print ("No tools found. Check your linked accounts." )
4747 return
4848
49- # Get utility tools for dynamic discovery
50- utility_tools = all_tools . utility_tools ()
49+ # Get a search tool for dynamic discovery
50+ search_tool = toolset . get_search_tool ()
5151
52- # Search for employee management tools
53- result = utility_tools . get_search_tool ()( query = "manage employees create update list" , top_k = 5 )
52+ # Search for employee management tools — returns a Tools collection
53+ tools = search_tool ( "manage employees create update list" , top_k = 5 , account_ids = _account_ids )
5454
55- print ("Found relevant tools:" )
56- for tool in result . get ( " tools" , []) :
57- print (f" - { tool [ ' name' ] } (score : { tool [ 'score' ]:.2f } ): { tool [ ' description' ] } " )
55+ print (f "Found { len ( tools ) } relevant tools:" )
56+ for tool in tools :
57+ print (f" - { tool . name } : { tool . description } " )
5858
5959 print ()
6060
6161
62- def example_utility_tools_with_execution ():
62+ def example_search_tool_with_execution ():
6363 """Example of discovering and executing tools dynamically"""
6464 print ("Example 2: Dynamic tool execution\n " )
6565
@@ -73,22 +73,20 @@ def example_utility_tools_with_execution():
7373 print ("No tools found. Check your linked accounts." )
7474 return
7575
76- utility_tools = all_tools . utility_tools ()
76+ search_tool = toolset . get_search_tool ()
7777
7878 # Step 1: Search for relevant tools
79- search_result = utility_tools . get_search_tool ()( query = "list all employees" , top_k = 1 )
79+ tools = search_tool ( "list all employees" , top_k = 1 , account_ids = _account_ids )
8080
81- tools_found = search_result .get ("tools" , [])
82- if tools_found :
83- best_tool = tools_found [0 ]
84- print (f"Best matching tool: { best_tool ['name' ]} " )
85- print (f"Description: { best_tool ['description' ]} " )
86- print (f"Relevance score: { best_tool ['score' ]:.2f} " )
81+ if tools :
82+ best_tool = tools [0 ]
83+ print (f"Best matching tool: { best_tool .name } " )
84+ print (f"Description: { best_tool .description } " )
8785
88- # Step 2: Execute the found tool
86+ # Step 2: Execute the found tool directly
8987 try :
90- print (f"\n Executing { best_tool [ ' name' ] } ..." )
91- result = utility_tools . get_execute_tool ()( toolName = best_tool [ "name" ], params = { " limit" : 5 } )
88+ print (f"\n Executing { best_tool . name } ..." )
89+ result = best_tool ( limit = 5 )
9290 print (f"Execution result: { result } " )
9391 except Exception as e :
9492 print (f"Execution failed (expected in example): { e } " )
@@ -97,8 +95,8 @@ def example_utility_tools_with_execution():
9795
9896
9997def example_with_openai ():
100- """Example of using utility tools with OpenAI"""
101- print ("Example 3: Using utility tools with OpenAI\n " )
98+ """Example of using search tool with OpenAI"""
99+ print ("Example 3: Using search tool with OpenAI\n " )
102100
103101 try :
104102 from openai import OpenAI
@@ -109,20 +107,19 @@ def example_with_openai():
109107 # Initialize StackOne toolset
110108 toolset = StackOneToolSet ()
111109
112- # Get BambooHR tools and their utility tools using MCP-backed fetch_tools()
113- bamboohr_tools = toolset .fetch_tools (account_ids = _account_ids , actions = ["bamboohr_*" ])
114- utility_tools = bamboohr_tools .utility_tools ()
110+ # Search for BambooHR employee tools
111+ tools = toolset .search_tools ("manage employees" , account_ids = _account_ids , top_k = 5 )
115112
116113 # Convert to OpenAI format
117- openai_tools = utility_tools .to_openai ()
114+ openai_tools = tools .to_openai ()
118115
119- # Create a chat completion with utility tools
116+ # Create a chat completion with discovered tools
120117 response = client .chat .completions .create (
121118 model = "gpt-4" ,
122119 messages = [
123120 {
124121 "role" : "system" ,
125- "content" : "You are an HR assistant. Use tool_search to find appropriate tools, then tool_execute to execute them ." ,
122+ "content" : "You are an HR assistant with access to employee management tools." ,
126123 },
127124 {"role" : "user" , "content" : "Can you help me find tools for managing employee records?" },
128125 ],
@@ -158,18 +155,11 @@ def example_with_langchain():
158155 toolset = StackOneToolSet ()
159156
160157 # Get tools and convert to LangChain format using MCP-backed fetch_tools()
161- tools = toolset .fetch_tools ( account_ids = _account_ids , actions = [ "bamboohr_list_*" ] )
162- langchain_tools = tools .to_langchain ()
158+ tools = toolset .search_tools ( "list employees" , account_ids = _account_ids , top_k = 5 )
159+ langchain_tools = list ( tools .to_langchain () )
163160
164- # Get utility tools as well
165- utility_tools = tools .utility_tools ()
166- langchain_utility_tools = utility_tools .to_langchain ()
167-
168- # Combine all tools
169- all_langchain_tools = list (langchain_tools ) + list (langchain_utility_tools )
170-
171- print (f"Available tools for LangChain: { len (all_langchain_tools )} " )
172- for tool in all_langchain_tools :
161+ print (f"Available tools for LangChain: { len (langchain_tools )} " )
162+ for tool in langchain_tools :
173163 print (f" - { tool .name } : { tool .description } " )
174164
175165 # Create LangChain agent
@@ -179,15 +169,15 @@ def example_with_langchain():
179169 [
180170 (
181171 "system" ,
182- "You are an HR assistant. Use the utility tools to discover and execute relevant tools ." ,
172+ "You are an HR assistant. Use the available tools to help the user ." ,
183173 ),
184174 ("human" , "{input}" ),
185175 ("placeholder" , "{agent_scratchpad}" ),
186176 ]
187177 )
188178
189- agent = create_tool_calling_agent (llm , all_langchain_tools , prompt )
190- agent_executor = AgentExecutor (agent = agent , tools = all_langchain_tools , verbose = True )
179+ agent = create_tool_calling_agent (llm , langchain_tools , prompt )
180+ agent_executor = AgentExecutor (agent = agent , tools = langchain_tools , verbose = True )
191181
192182 # Run the agent
193183 result = agent_executor .invoke ({"input" : "Find tools that can list employee data" })
@@ -206,7 +196,7 @@ def example_with_langchain():
206196def main ():
207197 """Run all examples"""
208198 print ("=" * 60 )
209- print ("StackOne AI SDK - Utility Tools Examples" )
199+ print ("StackOne AI SDK - Search Tool Examples" )
210200 print ("=" * 60 )
211201 print ()
212202
@@ -220,8 +210,8 @@ def main():
220210 return
221211
222212 # Basic examples that work without external APIs
223- example_utility_tools_basic ()
224- example_utility_tools_with_execution ()
213+ example_search_tool_basic ()
214+ example_search_tool_with_execution ()
225215
226216 # Examples that require OpenAI API
227217 if os .getenv ("OPENAI_API_KEY" ):
0 commit comments