-
-
Notifications
You must be signed in to change notification settings - Fork 984
Expand file tree
/
Copy path03_execute_llm_tool_calls.py
More file actions
66 lines (47 loc) · 1.72 KB
/
Copy path03_execute_llm_tool_calls.py
File metadata and controls
66 lines (47 loc) · 1.72 KB
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
"""
Example 3 — Execute the tool calls an LLM asked for.
When a model replies with tool calls, hand that response straight to
``execute_tool_calls()``. The manager parses the calls, routes each one to the
server that advertised the tool, and returns the results in order. This is the
step an ``Agent`` performs for you between LLM turns.
Run a server first:
python examples/mcp/servers/crypto_price_server.py
Then:
python examples/mcp/client/03_execute_llm_tool_calls.py
"""
import asyncio
from swarms.tools.mcp_manager import MCPManager
SERVER_URL = "http://localhost:8000/mcp"
# The shape an LLM produces when it decides to call a tool.
LLM_RESPONSE = {
"function": {
"name": "get_crypto_price",
"arguments": {"coin_id": "bitcoin"},
}
}
def main() -> None:
manager = MCPManager(mcp_url=SERVER_URL)
# dict (default) — a list of result dicts.
results = manager.execute_tool_calls(LLM_RESPONSE)
print(f"dict:\n{results}\n")
# json — one JSON string, handy for logging or feeding back to the model.
as_json = manager.execute_tool_calls(
LLM_RESPONSE, output_type="json"
)
print(f"json:\n{as_json}\n")
# str — plain text, for dropping into a prompt.
as_text = manager.execute_tool_calls(
LLM_RESPONSE, output_type="str"
)
print(f"str:\n{as_text}\n")
# Already have results and just want them rendered differently?
print(
f"re-formatted: {MCPManager.format_results(results, 'str')}"
)
async def main_async() -> None:
manager = MCPManager(mcp_url=SERVER_URL)
results = await manager.aexecute_tool_calls(LLM_RESPONSE)
print(f"async: {results}")
if __name__ == "__main__":
main()
asyncio.run(main_async())