File size: 2,963 Bytes
854fc2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# coding: utf-8
# Copyright (c) 2025 inclusionAI.

import logging
from aworld.tools import get_function_tools, list_function_tools
from examples.sandbox.inner_tools import aworldsearch_function_tools, another_function_tools

aworldsearch_server = get_function_tools("aworldsearch_server")

print(aworldsearch_server.list_tools())
res = aworldsearch_server.call_tool("search", {"query_list": ["Tencent financial report", "Baidu financial report", "Alibaba financial report"],})
print(res)

another_server = get_function_tools("another-server")
print(another_server.list_tools())


# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

# Step 1: Import different modules, which will automatically register their respective FunctionTools instances
print("=== Step 1: Import modules, automatically register FunctionTools instances ===")
# Import aworldsearch_function_tools module, which registers "aworldsearch-server"
print("Imported aworldsearch_function_tools module")

# Import another_function_tools module, which registers "another-server"

print("Imported another_function_tools module")

# Step 2: Get FunctionTools instances by name
print("\n=== Step 2: Get FunctionTools instances by name ===")
from aworld.tools import get_function_tools, list_function_tools

# List all registered FunctionTools servers
print(f"All registered servers: {list_function_tools()}")

# Get server instance by specific name
aworldsearch_server = get_function_tools("aworldsearch-server")
print(f"Retrieved server: {aworldsearch_server.name}")
print(f"Server description: {aworldsearch_server.description}")

another_server = get_function_tools("another-server")
print(f"Retrieved server: {another_server.name}")
print(f"Server description: {another_server.description}")

# Step 3: Use the retrieved instances to call methods
print("\n=== Step 3: Use the retrieved instances to call methods ===")
# List all tools of aworldsearch server
print("aworldsearch-server tool list:")
for tool in aworldsearch_server.list_tools():
    print(f"  - {tool.name}: {tool.description}")

# List all tools of another server
print("\nanother-server tool list:")
for tool in another_server.list_tools():
    print(f"  - {tool.name}: {tool.description}")

# Step 4: Call tools
print("\n=== Step 4: Call tool examples ===")
# Call aworldsearch server's tool
if "demo_search" in [tool.name for tool in aworldsearch_server.list_tools()]:
    print("Calling demo_search tool:")
    result = aworldsearch_server.call_tool("demo_search", {"query_list": ["Test query"]})
    print(result)

# Call another server's tool
if "get_weather" in [tool.name for tool in another_server.list_tools()]:
    print("\nCalling get_weather tool:")
    result = another_server.call_tool("get_weather", {"city": "Beijing"})
    print(result)

if __name__ == "__main__":
    pass  # Main logic has already been executed at the module level