universal-mcp-agents 0.1.19rc1__py3-none-any.whl → 0.1.24rc3__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.
- universal_mcp/agents/__init__.py +15 -16
- universal_mcp/agents/base.py +46 -35
- universal_mcp/agents/bigtool/state.py +1 -1
- universal_mcp/agents/cli.py +2 -5
- universal_mcp/agents/codeact0/__init__.py +2 -3
- universal_mcp/agents/codeact0/__main__.py +4 -7
- universal_mcp/agents/codeact0/agent.py +444 -96
- universal_mcp/agents/codeact0/langgraph_agent.py +1 -1
- universal_mcp/agents/codeact0/llm_tool.py +2 -254
- universal_mcp/agents/codeact0/prompts.py +247 -137
- universal_mcp/agents/codeact0/sandbox.py +52 -18
- universal_mcp/agents/codeact0/state.py +26 -6
- universal_mcp/agents/codeact0/tools.py +400 -74
- universal_mcp/agents/codeact0/utils.py +175 -11
- universal_mcp/agents/codeact00/__init__.py +3 -0
- universal_mcp/agents/{unified → codeact00}/__main__.py +4 -6
- universal_mcp/agents/codeact00/agent.py +578 -0
- universal_mcp/agents/codeact00/config.py +77 -0
- universal_mcp/agents/{unified → codeact00}/langgraph_agent.py +2 -2
- universal_mcp/agents/{unified → codeact00}/llm_tool.py +1 -1
- universal_mcp/agents/codeact00/prompts.py +364 -0
- universal_mcp/agents/{unified → codeact00}/sandbox.py +52 -18
- universal_mcp/agents/codeact00/state.py +66 -0
- universal_mcp/agents/codeact00/tools.py +525 -0
- universal_mcp/agents/codeact00/utils.py +678 -0
- universal_mcp/agents/codeact01/__init__.py +3 -0
- universal_mcp/agents/{codeact → codeact01}/__main__.py +4 -11
- universal_mcp/agents/codeact01/agent.py +413 -0
- universal_mcp/agents/codeact01/config.py +77 -0
- universal_mcp/agents/codeact01/langgraph_agent.py +14 -0
- universal_mcp/agents/codeact01/llm_tool.py +25 -0
- universal_mcp/agents/codeact01/prompts.py +246 -0
- universal_mcp/agents/codeact01/sandbox.py +162 -0
- universal_mcp/agents/{unified → codeact01}/state.py +26 -10
- universal_mcp/agents/codeact01/tools.py +648 -0
- universal_mcp/agents/{unified → codeact01}/utils.py +175 -11
- universal_mcp/agents/llm.py +14 -4
- universal_mcp/agents/react.py +3 -3
- universal_mcp/agents/sandbox.py +124 -69
- universal_mcp/applications/llm/app.py +76 -24
- {universal_mcp_agents-0.1.19rc1.dist-info → universal_mcp_agents-0.1.24rc3.dist-info}/METADATA +6 -5
- universal_mcp_agents-0.1.24rc3.dist-info/RECORD +66 -0
- universal_mcp/agents/codeact/__init__.py +0 -3
- universal_mcp/agents/codeact/agent.py +0 -240
- universal_mcp/agents/codeact/models.py +0 -11
- universal_mcp/agents/codeact/prompts.py +0 -82
- universal_mcp/agents/codeact/sandbox.py +0 -85
- universal_mcp/agents/codeact/state.py +0 -11
- universal_mcp/agents/codeact/utils.py +0 -68
- universal_mcp/agents/codeact0/playbook_agent.py +0 -355
- universal_mcp/agents/unified/README.md +0 -45
- universal_mcp/agents/unified/__init__.py +0 -3
- universal_mcp/agents/unified/agent.py +0 -289
- universal_mcp/agents/unified/prompts.py +0 -192
- universal_mcp/agents/unified/tools.py +0 -188
- universal_mcp_agents-0.1.19rc1.dist-info/RECORD +0 -64
- {universal_mcp_agents-0.1.19rc1.dist-info → universal_mcp_agents-0.1.24rc3.dist-info}/WHEEL +0 -0
|
@@ -1,112 +1,350 @@
|
|
|
1
1
|
import asyncio
|
|
2
|
+
import base64
|
|
2
3
|
from collections import defaultdict
|
|
3
|
-
from
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Annotated, Any
|
|
4
6
|
|
|
5
7
|
from langchain_core.tools import tool
|
|
6
|
-
from
|
|
8
|
+
from pydantic import Field
|
|
9
|
+
from universal_mcp.agentr.client import AgentrClient
|
|
10
|
+
from universal_mcp.agentr.registry import AgentrRegistry
|
|
11
|
+
from universal_mcp.applications.markitdown.app import MarkitdownApp
|
|
7
12
|
from universal_mcp.types import ToolFormat
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
from universal_mcp.agents.codeact0.prompts import build_tool_definitions
|
|
10
15
|
|
|
11
16
|
|
|
12
|
-
def
|
|
13
|
-
"""Call this function to enter
|
|
17
|
+
def enter_agent_builder_mode():
|
|
18
|
+
"""Call this function to enter agent builder mode. Agent builder mode is when the user wants to store a repeated task as a script with some inputs for the future."""
|
|
14
19
|
return
|
|
15
20
|
|
|
16
21
|
|
|
17
|
-
def
|
|
18
|
-
"""Call this function to exit playbook mode. Playbook mode is when the user wants to store a repeated task as a script with some inputs for the future."""
|
|
19
|
-
return
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def create_meta_tools(tool_registry: ToolRegistry) -> dict[str, Any]:
|
|
22
|
+
def create_meta_tools(tool_registry: AgentrRegistry) -> dict[str, Any]:
|
|
23
23
|
"""Create the meta tools for searching and loading tools"""
|
|
24
24
|
|
|
25
25
|
@tool
|
|
26
|
-
async def search_functions(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
async def search_functions(
|
|
27
|
+
queries: Annotated[
|
|
28
|
+
list[list[str]] | None,
|
|
29
|
+
Field(
|
|
30
|
+
description="A list of query lists. Each inner list contains one or more search terms that will be used together to find relevant tools."
|
|
31
|
+
),
|
|
32
|
+
] = None,
|
|
33
|
+
app_ids: Annotated[
|
|
34
|
+
list[str] | None,
|
|
35
|
+
Field(description="The ID or list of IDs (common names) of specific applications to search within."),
|
|
36
|
+
] = None,
|
|
37
|
+
) -> str:
|
|
38
|
+
"""
|
|
39
|
+
Searches for relevant functions based on queries and/or applications. This function
|
|
40
|
+
operates in three powerful modes with support for multi-query searches:
|
|
41
|
+
|
|
42
|
+
1. **Global Search** (`queries` only as List[List[str]]):
|
|
43
|
+
- Searches all functions across all applications.
|
|
44
|
+
- Supports multiple independent searches in parallel.
|
|
45
|
+
- Each inner list represents a separate search query.
|
|
46
|
+
|
|
47
|
+
Examples:
|
|
48
|
+
- Single global search:
|
|
49
|
+
`search_functions(queries=[["create presentation"]])`
|
|
50
|
+
|
|
51
|
+
- Multiple independent global searches:
|
|
52
|
+
`search_functions(queries=[["send email"], ["schedule meeting"]])`
|
|
53
|
+
|
|
54
|
+
- Multi-term search for comprehensive results:
|
|
55
|
+
`search_functions(queries=[["send email", "draft email", "compose email"]])`
|
|
56
|
+
|
|
57
|
+
2. **App Discovery** (`app_ids` only as List[str]):
|
|
58
|
+
- Returns ALL available functions for one or more specific applications.
|
|
59
|
+
- Use this to explore the complete capability set of an application.
|
|
60
|
+
|
|
61
|
+
Examples:
|
|
62
|
+
- Single app discovery:
|
|
63
|
+
`search_functions(app_ids=["Gmail"])`
|
|
64
|
+
|
|
65
|
+
- Multiple app discovery:
|
|
66
|
+
`search_functions(app_ids=["Gmail", "Google Calendar", "Slack"])`
|
|
67
|
+
|
|
68
|
+
3. **Scoped Search** (`queries` as List[List[str]] and `app_ids` as List[str]):
|
|
69
|
+
- Performs targeted searches within specific applications in parallel.
|
|
70
|
+
- The number of app_ids must match the number of inner query lists.
|
|
71
|
+
- Each query list is searched within its corresponding app_id.
|
|
72
|
+
- Supports multiple search terms per app for comprehensive discovery.
|
|
73
|
+
|
|
74
|
+
Examples:
|
|
75
|
+
- Basic scoped search (one query per app):
|
|
76
|
+
`search_functions(queries=[["find email"], ["share file"]], app_ids=["Gmail", "Google_Drive"])`
|
|
77
|
+
|
|
78
|
+
- Multi-term scoped search (multiple queries per app):
|
|
79
|
+
`search_functions(
|
|
80
|
+
queries=[
|
|
81
|
+
["send email", "draft email", "compose email", "reply to email"],
|
|
82
|
+
["create event", "schedule meeting", "find free time"],
|
|
83
|
+
["upload file", "share file", "create folder", "search files"]
|
|
84
|
+
],
|
|
85
|
+
app_ids=["Gmail", "Google Calendar", "Google_Drive"]
|
|
86
|
+
)`
|
|
87
|
+
|
|
88
|
+
- Mixed complexity (some apps with single query, others with multiple):
|
|
89
|
+
`search_functions(
|
|
90
|
+
queries=[
|
|
91
|
+
["list messages"],
|
|
92
|
+
["create event", "delete event", "update event"]
|
|
93
|
+
],
|
|
94
|
+
app_ids=["Gmail", "Google Calendar"]
|
|
95
|
+
)`
|
|
96
|
+
|
|
97
|
+
**Pro Tips:**
|
|
98
|
+
- Use multiple search terms in a single query list to cast a wider net and discover related functionality
|
|
99
|
+
- Multi-term searches are more efficient than separate calls
|
|
100
|
+
- Scoped searches return more focused results than global searches
|
|
101
|
+
- The function returns connection status for each app (connected vs NOT connected)
|
|
102
|
+
- All searches within a single call execute in parallel for maximum efficiency
|
|
103
|
+
|
|
104
|
+
**Parameters:**
|
|
105
|
+
- `queries` (List[List[str]], optional): A list of query lists. Each inner list contains one or more
|
|
106
|
+
search terms that will be used together to find relevant tools.
|
|
107
|
+
- `app_ids` (List[str], optional): A list of application IDs to search within or discover.
|
|
108
|
+
|
|
109
|
+
**Returns:**
|
|
110
|
+
- A structured response containing:
|
|
111
|
+
- Matched tools with their descriptions
|
|
112
|
+
- Connection status for each app
|
|
113
|
+
- Recommendations for which tools to load next
|
|
114
|
+
"""
|
|
115
|
+
registry = tool_registry
|
|
34
116
|
|
|
35
|
-
|
|
36
|
-
|
|
117
|
+
TOOL_THRESHOLD = 0.75
|
|
118
|
+
APP_THRESHOLD = 0.7
|
|
37
119
|
|
|
38
|
-
|
|
39
|
-
search_tasks = []
|
|
40
|
-
for query in queries:
|
|
41
|
-
search_tasks.append(_search_query_tools(query))
|
|
120
|
+
# --- Helper Functions for Different Search Modes ---
|
|
42
121
|
|
|
43
|
-
|
|
122
|
+
async def _handle_global_search(queries: list[str]) -> list[list[dict[str, Any]]]:
|
|
123
|
+
"""Performs a broad search across all apps to find relevant tools and apps."""
|
|
124
|
+
# 1. Perform initial broad searches for tools and apps concurrently.
|
|
125
|
+
initial_tool_tasks = [registry.search_tools(query=q, distance_threshold=TOOL_THRESHOLD) for q in queries]
|
|
126
|
+
app_search_tasks = [registry.search_apps(query=q, distance_threshold=APP_THRESHOLD) for q in queries]
|
|
44
127
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
app = tool["id"].split("__")[0]
|
|
49
|
-
tool_id = tool["id"]
|
|
128
|
+
initial_tool_results, app_search_results = await asyncio.gather(
|
|
129
|
+
asyncio.gather(*initial_tool_tasks), asyncio.gather(*app_search_tasks)
|
|
130
|
+
)
|
|
50
131
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
app_tools[app].add(f"{tool_id}: {cleaned_desc}")
|
|
132
|
+
# 2. Create a prioritized list of app IDs for the final search.
|
|
133
|
+
app_ids_from_apps = {app["id"] for result_list in app_search_results for app in result_list}
|
|
134
|
+
prioritized_app_id_list = list(app_ids_from_apps)
|
|
55
135
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
result_parts.append(f"Tools from {app} (status: {app_status} by user):")
|
|
61
|
-
# Convert set to sorted list for consistent output
|
|
62
|
-
for tool in sorted(tools):
|
|
63
|
-
result_parts.append(f" - {tool}")
|
|
64
|
-
result_parts.append("") # Empty line between apps
|
|
136
|
+
app_ids_from_tools = {tool["app_id"] for result_list in initial_tool_results for tool in result_list}
|
|
137
|
+
for tool_app_id in app_ids_from_tools:
|
|
138
|
+
if tool_app_id not in app_ids_from_apps:
|
|
139
|
+
prioritized_app_id_list.append(tool_app_id)
|
|
65
140
|
|
|
66
|
-
|
|
67
|
-
|
|
141
|
+
if not prioritized_app_id_list:
|
|
142
|
+
return []
|
|
68
143
|
|
|
69
|
-
|
|
70
|
-
|
|
144
|
+
# 3. Perform the final, comprehensive tool search across the prioritized apps.
|
|
145
|
+
final_tool_search_tasks = [
|
|
146
|
+
registry.search_tools(query=query, app_id=app_id_to_search, distance_threshold=TOOL_THRESHOLD)
|
|
147
|
+
for app_id_to_search in prioritized_app_id_list
|
|
148
|
+
for query in queries
|
|
149
|
+
]
|
|
150
|
+
return await asyncio.gather(*final_tool_search_tasks)
|
|
71
151
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
apps_search_task = tool_registry.search_apps(query, limit=4)
|
|
152
|
+
async def _handle_scoped_search(app_ids: list[str], queries: list[list[str]]) -> list[list[dict[str, Any]]]:
|
|
153
|
+
"""Performs targeted searches for specific queries within specific applications."""
|
|
154
|
+
if len(app_ids) != len(queries):
|
|
155
|
+
raise ValueError("The number of app_ids must match the number of query lists.")
|
|
77
156
|
|
|
78
|
-
|
|
79
|
-
|
|
157
|
+
tasks = []
|
|
158
|
+
for app_id, query_list in zip(app_ids, queries):
|
|
159
|
+
for query in query_list:
|
|
160
|
+
# Create a search task for each query in the list for the corresponding app
|
|
161
|
+
tasks.append(registry.search_tools(query=query, app_id=app_id, distance_threshold=TOOL_THRESHOLD))
|
|
80
162
|
|
|
81
|
-
|
|
82
|
-
app_tool_tasks = [tool_registry.search_tools(query, limit=5, app_id=app["id"]) for app in apps_list]
|
|
163
|
+
return await asyncio.gather(*tasks)
|
|
83
164
|
|
|
84
|
-
|
|
85
|
-
|
|
165
|
+
async def _handle_app_discovery(app_ids: list[str]) -> list[list[dict[str, Any]]]:
|
|
166
|
+
"""Fetches all tools for a list of applications."""
|
|
167
|
+
tasks = [registry.search_tools(query="", app_id=app_id, limit=20) for app_id in app_ids]
|
|
168
|
+
return await asyncio.gather(*tasks)
|
|
86
169
|
|
|
87
|
-
#
|
|
88
|
-
tools_list = list(tools_from_general_search)
|
|
89
|
-
for app_tools in app_tools_results:
|
|
90
|
-
tools_list.extend(app_tools)
|
|
170
|
+
# --- Helper Functions for Structuring and Formatting Results ---
|
|
91
171
|
|
|
92
|
-
|
|
172
|
+
def _format_response(structured_results: list[dict[str, Any]]) -> str:
|
|
173
|
+
"""Builds the final, user-facing formatted string response from structured data."""
|
|
174
|
+
if not structured_results:
|
|
175
|
+
return "No relevant functions were found."
|
|
176
|
+
|
|
177
|
+
result_parts = []
|
|
178
|
+
apps_in_results = {app["app_id"] for app in structured_results}
|
|
179
|
+
connected_apps_in_results = {
|
|
180
|
+
app["app_id"] for app in structured_results if app["connection_status"] == "connected"
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
for app in structured_results:
|
|
184
|
+
app_id = app["app_id"]
|
|
185
|
+
app_status = "connected" if app["connection_status"] == "connected" else "NOT connected"
|
|
186
|
+
result_parts.append(f"Tools from {app_id} (status: {app_status} by user):")
|
|
187
|
+
|
|
188
|
+
for tool in app["tools"]:
|
|
189
|
+
result_parts.append(f" - {tool['id']}: {tool['description']}")
|
|
190
|
+
result_parts.append("") # Empty line for readability
|
|
191
|
+
|
|
192
|
+
# Add summary connection status messages
|
|
193
|
+
if not connected_apps_in_results and len(apps_in_results) > 1:
|
|
194
|
+
result_parts.append(
|
|
195
|
+
"Connection Status: None of the apps in the results are connected. "
|
|
196
|
+
"You must ask the user to choose the application."
|
|
197
|
+
)
|
|
198
|
+
elif len(connected_apps_in_results) > 1:
|
|
199
|
+
connected_list = ", ".join(sorted(list(connected_apps_in_results)))
|
|
200
|
+
result_parts.append(
|
|
201
|
+
f"Connection Status: Multiple apps are connected ({connected_list}). "
|
|
202
|
+
"You must ask the user to select which application they want to use."
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
result_parts.append("Call load_functions to select the required functions only.")
|
|
206
|
+
if 0 <= len(connected_apps_in_results) < len(apps_in_results):
|
|
207
|
+
result_parts.append(
|
|
208
|
+
"Unconnected app functions can also be loaded if asked for by the user, they will generate a connection link"
|
|
209
|
+
"but prefer connected ones. Ask the user to choose the app if none of the "
|
|
210
|
+
"relevant apps are connected."
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
return "\n".join(result_parts)
|
|
214
|
+
|
|
215
|
+
def _structure_tool_results(
|
|
216
|
+
raw_tool_lists: list[list[dict[str, Any]]], connected_app_ids: set[str]
|
|
217
|
+
) -> list[dict[str, Any]]:
|
|
218
|
+
"""
|
|
219
|
+
Converts raw search results into a structured format, handling duplicates,
|
|
220
|
+
cleaning descriptions, and adding connection status.
|
|
221
|
+
"""
|
|
222
|
+
aggregated_tools = defaultdict(dict)
|
|
223
|
+
# Use a list to maintain the order of apps as they are found.
|
|
224
|
+
ordered_app_ids = []
|
|
225
|
+
|
|
226
|
+
for tool_list in raw_tool_lists:
|
|
227
|
+
for tool in tool_list:
|
|
228
|
+
app_id = tool.get("app_id", "unknown")
|
|
229
|
+
tool_id = tool.get("id")
|
|
230
|
+
|
|
231
|
+
if not tool_id:
|
|
232
|
+
continue
|
|
233
|
+
|
|
234
|
+
if app_id not in aggregated_tools:
|
|
235
|
+
ordered_app_ids.append(app_id)
|
|
236
|
+
|
|
237
|
+
if tool_id not in aggregated_tools[app_id]:
|
|
238
|
+
aggregated_tools[app_id][tool_id] = {
|
|
239
|
+
"id": tool_id,
|
|
240
|
+
"description": _clean_tool_description(tool.get("description", "")),
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
# Build the final results list respecting the discovery order.
|
|
244
|
+
found_tools_result = []
|
|
245
|
+
for app_id in ordered_app_ids:
|
|
246
|
+
if app_id in aggregated_tools and aggregated_tools[app_id]:
|
|
247
|
+
found_tools_result.append(
|
|
248
|
+
{
|
|
249
|
+
"app_id": app_id,
|
|
250
|
+
"connection_status": "connected" if app_id in connected_app_ids else "not_connected",
|
|
251
|
+
"tools": list(aggregated_tools[app_id].values()),
|
|
252
|
+
}
|
|
253
|
+
)
|
|
254
|
+
return found_tools_result
|
|
255
|
+
|
|
256
|
+
def _clean_tool_description(description: str) -> str:
|
|
257
|
+
"""Consistently formats tool descriptions by removing implementation details."""
|
|
258
|
+
return description.split("Context:")[0].strip()
|
|
259
|
+
|
|
260
|
+
# Main Function Logic
|
|
261
|
+
|
|
262
|
+
if not queries and not app_ids:
|
|
263
|
+
raise ValueError("You must provide 'queries', 'app_ids', or both.")
|
|
264
|
+
|
|
265
|
+
# --- Initialization and Input Normalization ---
|
|
266
|
+
connections = await registry.list_connected_apps()
|
|
267
|
+
connected_app_ids = {connection["app_id"] for connection in connections}
|
|
268
|
+
|
|
269
|
+
canonical_app_ids = []
|
|
270
|
+
if app_ids:
|
|
271
|
+
# Concurrently search for all provided app names
|
|
272
|
+
app_search_tasks = [
|
|
273
|
+
registry.search_apps(query=app_name, distance_threshold=APP_THRESHOLD) for app_name in app_ids
|
|
274
|
+
]
|
|
275
|
+
app_search_results = await asyncio.gather(*app_search_tasks)
|
|
276
|
+
|
|
277
|
+
# Process results and build the list of canonical IDs, handling not found errors
|
|
278
|
+
for app_name, result_list in zip(app_ids, app_search_results):
|
|
279
|
+
if not result_list:
|
|
280
|
+
raise ValueError(f"Application '{app_name}' could not be found.")
|
|
281
|
+
# Assume the first result is the correct one
|
|
282
|
+
canonical_app_ids.append(result_list[0]["id"])
|
|
283
|
+
|
|
284
|
+
# --- Mode Dispatching ---
|
|
285
|
+
raw_results = []
|
|
286
|
+
|
|
287
|
+
if canonical_app_ids and queries:
|
|
288
|
+
raw_results = await _handle_scoped_search(canonical_app_ids, queries)
|
|
289
|
+
elif canonical_app_ids:
|
|
290
|
+
raw_results = await _handle_app_discovery(canonical_app_ids)
|
|
291
|
+
elif queries:
|
|
292
|
+
# Flatten list of lists to list of strings for global search
|
|
293
|
+
flat_queries = (
|
|
294
|
+
[q for sublist in queries for q in sublist] if queries and not isinstance(queries[0], str) else queries
|
|
295
|
+
)
|
|
296
|
+
raw_results = await _handle_global_search(flat_queries)
|
|
297
|
+
|
|
298
|
+
# --- Structuring and Formatting ---
|
|
299
|
+
structured_data = _structure_tool_results(raw_results, connected_app_ids)
|
|
300
|
+
return _format_response(structured_data)
|
|
93
301
|
|
|
94
302
|
@tool
|
|
95
303
|
async def load_functions(tool_ids: list[str]) -> str:
|
|
96
|
-
"""
|
|
304
|
+
"""
|
|
305
|
+
Loads specified functions and returns their Python signatures and docstrings.
|
|
306
|
+
This makes the functions available for use inside the 'execute_ipython_cell' tool.
|
|
307
|
+
The agent MUST use the returned information to understand how to call the functions correctly.
|
|
97
308
|
|
|
98
309
|
Args:
|
|
99
|
-
tool_ids:
|
|
310
|
+
tool_ids: A list of function IDs in the format 'app__function'. Example: ['google_mail__send_email']
|
|
100
311
|
|
|
101
312
|
Returns:
|
|
102
|
-
|
|
313
|
+
A string containing the signatures and docstrings of the successfully loaded functions,
|
|
314
|
+
ready for the agent to use in its code.
|
|
103
315
|
"""
|
|
104
|
-
|
|
316
|
+
if not tool_ids:
|
|
317
|
+
return "No tool IDs provided to load."
|
|
318
|
+
|
|
319
|
+
# Step 1: Validate which tools are usable and get login links for others.
|
|
320
|
+
valid_tools, unconnected_links = await get_valid_tools(tool_ids=tool_ids, registry=tool_registry)
|
|
321
|
+
|
|
322
|
+
if not valid_tools:
|
|
323
|
+
response_string = "Error: None of the provided tool IDs could be validated or loaded."
|
|
324
|
+
return response_string, {}, [], ""
|
|
325
|
+
|
|
326
|
+
# Step 2: Export the schemas of the valid tools.
|
|
327
|
+
await tool_registry.load_tools(valid_tools)
|
|
328
|
+
exported_tools = await tool_registry.export_tools(
|
|
329
|
+
valid_tools, ToolFormat.NATIVE
|
|
330
|
+
) # Get definition for only the new tools
|
|
331
|
+
|
|
332
|
+
# Step 3: Build the informational string for the agent.
|
|
333
|
+
tool_definitions, new_tools_context = build_tool_definitions(exported_tools)
|
|
334
|
+
|
|
335
|
+
result_parts = [
|
|
336
|
+
f"Successfully loaded {len(exported_tools)} functions. They are now available for use inside `execute_ipython_cell`:",
|
|
337
|
+
"\n".join(tool_definitions),
|
|
338
|
+
]
|
|
339
|
+
|
|
340
|
+
response_string = "\n\n".join(result_parts)
|
|
341
|
+
unconnected_links = "\n".join(unconnected_links)
|
|
342
|
+
|
|
343
|
+
return response_string, new_tools_context, valid_tools, unconnected_links
|
|
105
344
|
|
|
106
|
-
@tool
|
|
107
345
|
async def web_search(query: str) -> dict:
|
|
108
346
|
"""
|
|
109
|
-
Get an LLM answer to a question informed by Exa search results.
|
|
347
|
+
Get an LLM answer to a question informed by Exa search results. Useful when you need information from a wide range of real-time sources on the web. Do not use this when you need to access contents of a specific webpage.
|
|
110
348
|
|
|
111
349
|
This tool performs an Exa `/answer` request, which:
|
|
112
350
|
1. Provides a **direct answer** for factual queries (e.g., "What is the capital of France?" → "Paris")
|
|
@@ -129,10 +367,98 @@ def create_meta_tools(tool_registry: ToolRegistry) -> dict[str, Any]:
|
|
|
129
367
|
"citations": response.get("citations", []),
|
|
130
368
|
}
|
|
131
369
|
|
|
132
|
-
|
|
370
|
+
async def read_file(uri: str) -> str:
|
|
371
|
+
"""
|
|
372
|
+
Asynchronously reads a local file or uri and returns the content as a markdown string.
|
|
373
|
+
|
|
374
|
+
This tool aims to extract the main text content from various sources.
|
|
375
|
+
It automatically prepends 'file://' to the input string if it appears
|
|
376
|
+
to be a local path without a specified scheme (like http, https, data, file).
|
|
377
|
+
|
|
378
|
+
Args:
|
|
379
|
+
uri (str): The URI pointing to the resource or a local file path.
|
|
380
|
+
Supported schemes:
|
|
381
|
+
- http:// or https:// (Web pages, feeds, APIs)
|
|
382
|
+
- file:// (Local or accessible network files)
|
|
383
|
+
- data: (Embedded data)
|
|
384
|
+
|
|
385
|
+
Returns:
|
|
386
|
+
A string containing the markdown representation of the content at the specified URI
|
|
387
|
+
|
|
388
|
+
Raises:
|
|
389
|
+
ValueError: If the URI is invalid, empty, or uses an unsupported scheme
|
|
390
|
+
after automatic prefixing.
|
|
391
|
+
|
|
392
|
+
Tags:
|
|
393
|
+
convert, markdown, async, uri, transform, document, important
|
|
394
|
+
"""
|
|
395
|
+
markitdown = MarkitdownApp()
|
|
396
|
+
response = await markitdown.convert_to_markdown(uri)
|
|
397
|
+
return response
|
|
398
|
+
|
|
399
|
+
def save_file(file_name: str, content: str) -> dict:
|
|
400
|
+
"""
|
|
401
|
+
Saves a file to the local filesystem.
|
|
402
|
+
|
|
403
|
+
Args:
|
|
404
|
+
file_name (str): The name of the file to save.
|
|
405
|
+
content (str): The content to save to the file.
|
|
406
|
+
|
|
407
|
+
Returns:
|
|
408
|
+
dict: A dictionary containing the result of the save operation with the following fields:
|
|
409
|
+
- status (str): "success" if the save succeeded, "error" otherwise.
|
|
410
|
+
- message (str): A message returned by the server, typically indicating success or providing error details.
|
|
411
|
+
"""
|
|
412
|
+
with Path(file_name).open("w") as f:
|
|
413
|
+
f.write(content)
|
|
414
|
+
|
|
415
|
+
return {
|
|
416
|
+
"status": "success",
|
|
417
|
+
"message": f"File {file_name} saved successfully",
|
|
418
|
+
"file_path": Path(file_name).absolute(),
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
def upload_file(file_name: str, mime_type: str, base64_data: str) -> dict:
|
|
422
|
+
"""
|
|
423
|
+
Uploads a file to the server.
|
|
424
|
+
|
|
425
|
+
Args:
|
|
426
|
+
file_name (str): The name of the file to upload.
|
|
427
|
+
mime_type (str): The MIME type of the file.
|
|
428
|
+
base64_data (str): The file content encoded as a base64 string.
|
|
429
|
+
|
|
430
|
+
Returns:
|
|
431
|
+
dict: A dictionary containing the result of the upload operation with the following fields:
|
|
432
|
+
- status (str): "success" if the upload succeeded, "error" otherwise.
|
|
433
|
+
- message (str): A message returned by the server, typically indicating success or providing error details.
|
|
434
|
+
- signed_url (str or None): The signed URL to access the uploaded file if successful, None otherwise.
|
|
435
|
+
"""
|
|
436
|
+
client: AgentrClient = tool_registry.client
|
|
437
|
+
bytes_data = base64.b64decode(base64_data)
|
|
438
|
+
response = client._upload_file(file_name, mime_type, bytes_data)
|
|
439
|
+
if response.get("status") != "success":
|
|
440
|
+
return {
|
|
441
|
+
"status": "error",
|
|
442
|
+
"message": response.get("message"),
|
|
443
|
+
"signed_url": None,
|
|
444
|
+
}
|
|
445
|
+
return {
|
|
446
|
+
"status": "success",
|
|
447
|
+
"message": response.get("message"),
|
|
448
|
+
"signed_url": response.get("signed_url"),
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return {
|
|
452
|
+
"search_functions": search_functions,
|
|
453
|
+
"load_functions": load_functions,
|
|
454
|
+
"web_search": web_search,
|
|
455
|
+
"read_file": read_file,
|
|
456
|
+
"upload_file": upload_file,
|
|
457
|
+
"save_file": save_file,
|
|
458
|
+
}
|
|
133
459
|
|
|
134
460
|
|
|
135
|
-
async def get_valid_tools(tool_ids: list[str], registry:
|
|
461
|
+
async def get_valid_tools(tool_ids: list[str], registry: AgentrRegistry) -> tuple[list[str], list[str]]:
|
|
136
462
|
"""For a given list of tool_ids, validates the tools and returns a list of links for the apps that have not been logged in"""
|
|
137
463
|
correct, incorrect = [], []
|
|
138
464
|
connections = await registry.list_connected_apps()
|
|
@@ -173,11 +499,11 @@ async def get_valid_tools(tool_ids: list[str], registry: ToolRegistry) -> tuple[
|
|
|
173
499
|
continue
|
|
174
500
|
if app not in connected_apps and app not in unconnected:
|
|
175
501
|
unconnected.add(app)
|
|
176
|
-
text = registry.
|
|
502
|
+
text = await registry.authorise_app(app_id=app)
|
|
177
503
|
start = text.find(":") + 1
|
|
178
504
|
end = text.find(". R", start)
|
|
179
505
|
url = text[start:end].strip()
|
|
180
|
-
markdown_link = f"[{app}]({url})"
|
|
506
|
+
markdown_link = f"[Connect to {app.capitalize()}]({url})"
|
|
181
507
|
unconnected_links.append(markdown_link)
|
|
182
508
|
for tool_id, tool_name in tool_entries:
|
|
183
509
|
if tool_name in available:
|