universal-mcp-agents 0.1.23rc1__py3-none-any.whl → 0.1.23rc2__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.

Potentially problematic release.


This version of universal-mcp-agents might be problematic. Click here for more details.

@@ -9,7 +9,7 @@ from universal_mcp.agents.simple import SimpleAgent
9
9
 
10
10
 
11
11
  def get_agent(
12
- agent_name: Literal["react", "simple", "builder", "bigtool", "codeact-script", "codeact-repl"],
12
+ agent_name: Literal["react", "simple", "builder", "bigtool", "codeact-repl"],
13
13
  ):
14
14
  if agent_name == "react":
15
15
  return ReactAgent
@@ -23,7 +23,7 @@ def get_agent(
23
23
  return CodeActPlaybookAgent
24
24
  else:
25
25
  raise ValueError(
26
- f"Unknown agent: {agent_name}. Possible values: react, simple, builder, bigtool, codeact-script, codeact-repl"
26
+ f"Unknown agent: {agent_name}. Possible values: react, simple, builder, bigtool, codeact-repl"
27
27
  )
28
28
 
29
29
 
@@ -25,7 +25,7 @@ def run(name: str = "react"):
25
25
  client = AgentrClient()
26
26
  params = {
27
27
  "instructions": "You are a helpful assistant",
28
- "model": "anthropic/claude-sonnet-4-20250514",
28
+ "model": "azure/gpt-4.1",
29
29
  "registry": AgentrRegistry(client=client),
30
30
  "memory": MemorySaver(),
31
31
  }
@@ -13,15 +13,12 @@ async def main():
13
13
  agent = CodeActPlaybookAgent(
14
14
  name="CodeAct Agent",
15
15
  instructions="Be very concise in your answers.",
16
- model="anthropic:claude-4-sonnet-20250514",
17
- tools={"google_mail": ["list_messages"]},
16
+ model="azure/gpt-4.1",
18
17
  registry=AgentrRegistry(),
19
18
  memory=memory,
20
19
  )
21
20
  print("Starting agent...")
22
- result = await agent.invoke(
23
- user_input="Fetch unsubscribe links from my Gmail inbox for promo emails I have received in the last 7 days"
24
- )
21
+ result = await agent.invoke(user_input="load all the tools of reddit which can be used to search subreddit")
25
22
  print(messages_to_list(result["messages"]))
26
23
 
27
24
 
@@ -1,35 +1,36 @@
1
+ import copy
1
2
  import json
2
3
  import re
3
- from typing import Literal, cast
4
4
  import uuid
5
+ from typing import Literal, cast
5
6
 
7
+ from langchain_anthropic import ChatAnthropic
6
8
  from langchain_core.messages import AIMessage, ToolMessage
7
9
  from langchain_core.tools import StructuredTool
8
10
  from langgraph.checkpoint.base import BaseCheckpointSaver
9
11
  from langgraph.graph import START, StateGraph
10
12
  from langgraph.types import Command, RetryPolicy, StreamWriter
11
13
  from universal_mcp.tools.registry import ToolRegistry
12
- from universal_mcp.types import ToolConfig, ToolFormat
14
+ from universal_mcp.types import ToolFormat
13
15
 
14
16
  from universal_mcp.agents.base import BaseAgent
15
17
  from universal_mcp.agents.codeact0.llm_tool import smart_print
16
18
  from universal_mcp.agents.codeact0.prompts import (
17
19
  PLAYBOOK_GENERATING_PROMPT,
18
- PLAYBOOK_PLANNING_PROMPT,
19
20
  PLAYBOOK_META_PROMPT,
21
+ PLAYBOOK_PLANNING_PROMPT,
20
22
  create_default_prompt,
21
23
  )
22
24
  from universal_mcp.agents.codeact0.sandbox import eval_unsafe, execute_ipython_cell, handle_execute_ipython_cell
23
- from universal_mcp.agents.codeact0.state import CodeActState, PlaybookCode, PlaybookPlan, PlaybookMeta
25
+ from universal_mcp.agents.codeact0.state import CodeActState, PlaybookCode, PlaybookMeta, PlaybookPlan
24
26
  from universal_mcp.agents.codeact0.tools import (
25
27
  create_meta_tools,
26
28
  enter_playbook_mode,
27
29
  get_valid_tools,
28
30
  )
29
- from universal_mcp.agents.codeact0.utils import add_tools
31
+ from universal_mcp.agents.codeact0.utils import build_anthropic_cache_message, get_connected_apps_string
30
32
  from universal_mcp.agents.llm import load_chat_model
31
33
  from universal_mcp.agents.utils import convert_tool_ids_to_dict, filter_retry_on, get_message_text
32
- from universal_mcp.agents.codeact0.utils import get_connected_apps_string
33
34
 
34
35
 
35
36
  class CodeActPlaybookAgent(BaseAgent):
@@ -59,11 +60,12 @@ class CodeActPlaybookAgent(BaseAgent):
59
60
  self.tools_config = self.playbook.tools if self.playbook else {}
60
61
  self.eval_fn = eval_unsafe
61
62
  self.sandbox_timeout = sandbox_timeout
62
- self.default_tools = {
63
+ self.default_tools_config = {
63
64
  "llm": ["generate_text", "classify_data", "extract_data", "call_llm"],
64
65
  }
65
- add_tools(self.tools_config, self.default_tools)
66
-
66
+ self.final_instructions = ""
67
+ self.tools_context = {}
68
+ self.exported_tools = []
67
69
 
68
70
  async def _build_graph(self):
69
71
  meta_tools = create_meta_tools(self.registry)
@@ -71,28 +73,47 @@ class CodeActPlaybookAgent(BaseAgent):
71
73
  self.additional_tools = [
72
74
  t if isinstance(t, StructuredTool) else StructuredTool.from_function(t) for t in additional_tools
73
75
  ]
76
+
74
77
  if self.tools_config:
75
- # Convert dict format to list format if needed
76
78
  if isinstance(self.tools_config, dict):
77
79
  self.tools_config = [
78
80
  f"{provider}__{tool}" for provider, tools in self.tools_config.items() for tool in tools
79
81
  ]
80
- if not self.registry:
81
- raise ValueError("Tools are configured but no registry is provided")
82
+ if not self.registry:
83
+ raise ValueError("Tools are configured but no registry is provided")
84
+ await self.registry.export_tools(self.tools_config, ToolFormat.LANGCHAIN)
85
+
86
+ await self.registry.export_tools(self.default_tools_config, ToolFormat.LANGCHAIN)
82
87
 
83
88
  async def call_model(state: CodeActState) -> Command[Literal["execute_tools"]]:
84
- messages = [{"role": "system", "content": self.final_instructions}] + state["messages"]
85
-
86
- # Run the model and potentially loop for reflection
87
- model_with_tools = self.model_instance.bind_tools(
88
- tools=[
89
- execute_ipython_cell,
90
- enter_playbook_mode,
91
- meta_tools["search_functions"],
92
- meta_tools["load_functions"],
93
- ],
94
- tool_choice="auto",
95
- )
89
+ """This node now only ever binds the four meta-tools to the LLM."""
90
+ messages = build_anthropic_cache_message(self.final_instructions) + state["messages"]
91
+
92
+ agent_facing_tools = [
93
+ execute_ipython_cell,
94
+ enter_playbook_mode,
95
+ meta_tools["search_functions"],
96
+ meta_tools["load_functions"],
97
+ ]
98
+
99
+ if isinstance(self.model_instance, ChatAnthropic):
100
+ model_with_tools = self.model_instance.bind_tools(
101
+ tools=agent_facing_tools,
102
+ tool_choice="auto",
103
+ cache_control={"type": "ephemeral", "ttl": "1h"},
104
+ )
105
+ if isinstance(messages[-1].content, str):
106
+ pass
107
+ else:
108
+ last = copy.deepcopy(messages[-1])
109
+ last.content[-1]["cache_control"] = {"type": "ephemeral", "ttl": "5m"}
110
+ messages[-1] = last
111
+ else:
112
+ model_with_tools = self.model_instance.bind_tools(
113
+ tools=agent_facing_tools,
114
+ tool_choice="auto",
115
+ )
116
+
96
117
  response = cast(AIMessage, model_with_tools.invoke(messages))
97
118
  if response.tool_calls:
98
119
  return Command(goto="execute_tools", update={"messages": [response]})
@@ -106,15 +127,16 @@ class CodeActPlaybookAgent(BaseAgent):
106
127
 
107
128
  tool_messages = []
108
129
  new_tool_ids = []
109
- ask_user = False
110
- ai_msg = ""
111
130
  tool_result = ""
112
131
  effective_previous_add_context = state.get("add_context", {})
113
132
  effective_existing_context = state.get("context", {})
133
+ # logging.info(f"Initial new_tool_ids_for_context: {new_tool_ids_for_context}")
114
134
 
115
135
  for tool_call in tool_calls:
136
+ tool_name = tool_call["name"]
137
+ tool_args = tool_call["args"]
116
138
  try:
117
- if tool_call["name"] == "enter_playbook_mode":
139
+ if tool_name == "enter_playbook_mode":
118
140
  tool_message = ToolMessage(
119
141
  content=json.dumps("Entered Playbook Mode."),
120
142
  name=tool_call["name"],
@@ -124,11 +146,11 @@ class CodeActPlaybookAgent(BaseAgent):
124
146
  goto="playbook",
125
147
  update={"playbook_mode": "planning", "messages": [tool_message]}, # Entered Playbook mode
126
148
  )
127
- elif tool_call["name"] == "execute_ipython_cell":
149
+ elif tool_name == "execute_ipython_cell":
128
150
  code = tool_call["args"]["snippet"]
129
151
  output, new_context, new_add_context = await handle_execute_ipython_cell(
130
152
  code,
131
- self.tools_context,
153
+ self.tools_context, # Uses the dynamically updated context
132
154
  self.eval_fn,
133
155
  effective_previous_add_context,
134
156
  effective_existing_context,
@@ -136,19 +158,22 @@ class CodeActPlaybookAgent(BaseAgent):
136
158
  effective_existing_context = new_context
137
159
  effective_previous_add_context = new_add_context
138
160
  tool_result = output
139
- elif tool_call["name"] == "load_functions": # Handle load_functions separately
140
- valid_tools, unconnected_links = await get_valid_tools(
141
- tool_ids=tool_call["args"]["tool_ids"], registry=self.registry
142
- )
161
+ elif tool_name == "load_functions":
162
+ # The tool now does all the work of validation and formatting.
163
+ tool_result = await meta_tools["load_functions"].ainvoke(tool_args)
164
+
165
+ # We still need to update the sandbox context for `execute_ipython_cell`
166
+ valid_tools, _ = await get_valid_tools(tool_ids=tool_args["tool_ids"], registry=self.registry)
143
167
  new_tool_ids.extend(valid_tools)
144
- # Create tool message response
145
- tool_result = f"Successfully loaded {len(valid_tools)} tools: {valid_tools}"
146
- links = "\n".join(unconnected_links)
147
- if links:
148
- ask_user = True
149
- ai_msg = f"Please login to the following app(s) using the following links and let me know in order to proceed:\n {links} "
150
- elif tool_call["name"] == "search_functions":
151
- tool_result = await meta_tools["search_functions"].ainvoke(tool_call["args"])
168
+ if new_tool_ids:
169
+ newly_exported = await self.registry.export_tools(new_tool_ids, ToolFormat.LANGCHAIN)
170
+ _, new_context_for_sandbox = create_default_prompt(
171
+ newly_exported, [], "", "", None
172
+ ) # is_initial_prompt is False by default
173
+ self.tools_context.update(new_context_for_sandbox)
174
+
175
+ elif tool_name == "search_functions":
176
+ tool_result = await meta_tools["search_functions"].ainvoke(tool_args)
152
177
  else:
153
178
  raise Exception(
154
179
  f"Unexpected tool call: {tool_call['name']}. "
@@ -164,23 +189,6 @@ class CodeActPlaybookAgent(BaseAgent):
164
189
  )
165
190
  tool_messages.append(tool_message)
166
191
 
167
- if new_tool_ids:
168
- self.tools_config.extend(new_tool_ids)
169
- self.exported_tools = await self.registry.export_tools(new_tool_ids, ToolFormat.LANGCHAIN)
170
- self.final_instructions, self.tools_context = create_default_prompt(
171
- self.exported_tools, self.additional_tools, self.instructions, await get_connected_apps_string(self.registry), self.playbook
172
- )
173
- if ask_user:
174
- tool_messages.append(AIMessage(content=ai_msg))
175
- return Command(
176
- update={
177
- "messages": tool_messages,
178
- "selected_tool_ids": new_tool_ids,
179
- "context": effective_existing_context,
180
- "add_context": effective_previous_add_context,
181
- }
182
- )
183
-
184
192
  return Command(
185
193
  goto="call_model",
186
194
  update={
@@ -195,21 +203,31 @@ class CodeActPlaybookAgent(BaseAgent):
195
203
  playbook_mode = state.get("playbook_mode")
196
204
  if playbook_mode == "planning":
197
205
  plan_id = str(uuid.uuid4())
198
- writer({
199
- "type": "custom",
200
- id: plan_id,
201
- "name": "planning",
202
- "data": {"update": bool(self.playbook)}
203
- })
206
+ writer({"type": "custom", id: plan_id, "name": "planning", "data": {"update": bool(self.playbook)}})
204
207
  planning_instructions = self.instructions + PLAYBOOK_PLANNING_PROMPT
205
208
  messages = [{"role": "system", "content": planning_instructions}] + state["messages"]
206
209
 
207
210
  model_with_structured_output = self.playbook_model_instance.with_structured_output(PlaybookPlan)
208
211
  response = model_with_structured_output.invoke(messages)
209
212
  plan = cast(PlaybookPlan, response)
210
-
213
+
211
214
  writer({"type": "custom", id: plan_id, "name": "planning", "data": {"plan": plan.steps}})
212
- return Command(update={"messages": [AIMessage(content=json.dumps(plan.dict()), additional_kwargs={"type": "planning", "plan": plan.steps, "update": bool(self.playbook)})], "playbook_mode": "confirming", "plan": plan.steps})
215
+ return Command(
216
+ update={
217
+ "messages": [
218
+ AIMessage(
219
+ content=json.dumps(plan.model_dump()),
220
+ additional_kwargs={
221
+ "type": "planning",
222
+ "plan": plan.steps,
223
+ "update": bool(self.playbook),
224
+ },
225
+ )
226
+ ],
227
+ "playbook_mode": "confirming",
228
+ "plan": plan.steps,
229
+ }
230
+ )
213
231
 
214
232
  elif playbook_mode == "confirming":
215
233
  # Deterministic routing based on three exact button inputs from UI
@@ -231,23 +249,20 @@ class CodeActPlaybookAgent(BaseAgent):
231
249
  # Update flow: use existing name/description and do not re-generate
232
250
  name = getattr(self.playbook, "name", None)
233
251
  description = getattr(self.playbook, "description", None)
234
- writer({
235
- "type": "custom",
236
- id: self.meta_id,
237
- "name": "generating",
238
- "data": {
239
- "update": True,
240
- "name": name,
241
- "description": description,
252
+ writer(
253
+ {
254
+ "type": "custom",
255
+ id: self.meta_id,
256
+ "name": "generating",
257
+ "data": {
258
+ "update": True,
259
+ "name": name,
260
+ "description": description,
261
+ },
242
262
  }
243
- })
263
+ )
244
264
  else:
245
- writer({
246
- "type": "custom",
247
- id: self.meta_id,
248
- "name": "generating",
249
- "data": {"update": False}
250
- })
265
+ writer({"type": "custom", id: self.meta_id, "name": "generating", "data": {"update": False}})
251
266
 
252
267
  meta_instructions = self.instructions + PLAYBOOK_META_PROMPT
253
268
  messages = [{"role": "system", "content": meta_instructions}] + state["messages"]
@@ -258,16 +273,28 @@ class CodeActPlaybookAgent(BaseAgent):
258
273
  name, description = meta.name, meta.description
259
274
 
260
275
  # Emit intermediary UI update with created name/description
261
- writer({
262
- "type": "custom",
263
- id: self.meta_id,
264
- "name": "generating",
265
- "data": {"update": False, "name": name, "description": description}
266
- })
267
-
268
- return Command(goto="playbook", update={"playbook_mode": "generating", "playbook_name": name, "playbook_description": description})
276
+ writer(
277
+ {
278
+ "type": "custom",
279
+ id: self.meta_id,
280
+ "name": "generating",
281
+ "data": {"update": False, "name": name, "description": description},
282
+ }
283
+ )
284
+
285
+ return Command(
286
+ goto="playbook",
287
+ update={
288
+ "playbook_mode": "generating",
289
+ "playbook_name": name,
290
+ "playbook_description": description,
291
+ },
292
+ )
269
293
  if t == "i would like to modify the plan":
270
- prompt_ai = AIMessage(content="What would you like to change about the plan? Let me know and I'll update the plan accordingly.", additional_kwargs={"stream": "true"})
294
+ prompt_ai = AIMessage(
295
+ content="What would you like to change about the plan? Let me know and I'll update the plan accordingly.",
296
+ additional_kwargs={"stream": "true"},
297
+ )
271
298
  return Command(update={"playbook_mode": "planning", "messages": [prompt_ai]})
272
299
  if t == "let's do something else":
273
300
  return Command(goto="call_model", update={"playbook_mode": "inactive"})
@@ -278,7 +305,7 @@ class CodeActPlaybookAgent(BaseAgent):
278
305
  elif playbook_mode == "generating":
279
306
  generating_instructions = self.instructions + PLAYBOOK_GENERATING_PROMPT
280
307
  messages = [{"role": "system", "content": generating_instructions}] + state["messages"]
281
-
308
+
282
309
  model_with_structured_output = self.playbook_model_instance.with_structured_output(PlaybookCode)
283
310
  response = model_with_structured_output.invoke(messages)
284
311
  func_code = cast(PlaybookCode, response).code
@@ -291,12 +318,12 @@ class CodeActPlaybookAgent(BaseAgent):
291
318
  function_name = "generated_playbook"
292
319
 
293
320
  # Use generated metadata if available
294
- final_name = state.get("playbook_name") or function_name
321
+ final_name = state.get("playbook_.pyname") or function_name
295
322
  final_description = state.get("playbook_description") or f"Generated playbook: {function_name}"
296
323
 
297
324
  # Save or update an Agent using the helper registry
298
325
  try:
299
- if not self.playbook_registry:
326
+ if not self.playbook_registry:
300
327
  raise ValueError("Playbook registry is not configured")
301
328
 
302
329
  # Build instructions payload embedding the plan and function code
@@ -318,19 +345,21 @@ class CodeActPlaybookAgent(BaseAgent):
318
345
  except Exception as e:
319
346
  raise e
320
347
 
321
- writer({
322
- "type": "custom",
323
- id: self.meta_id,
324
- "name": "generating",
325
- "data": {
326
- "id": str(res.id),
327
- "update": bool(self.playbook),
328
- "name": final_name,
329
- "description": final_description,
348
+ writer(
349
+ {
350
+ "type": "custom",
351
+ id: self.meta_id,
352
+ "name": "generating",
353
+ "data": {
354
+ "id": str(res.id),
355
+ "update": bool(self.playbook),
356
+ "name": final_name,
357
+ "description": final_description,
358
+ },
330
359
  }
331
- })
360
+ )
332
361
  mock_assistant_message = AIMessage(
333
- content=json.dumps(response.dict()),
362
+ content=json.dumps(response.model_dump()),
334
363
  additional_kwargs={
335
364
  "type": "generating",
336
365
  "id": str(res.id),
@@ -340,17 +369,21 @@ class CodeActPlaybookAgent(BaseAgent):
340
369
  },
341
370
  )
342
371
 
343
- return Command(
344
- update={"messages": [mock_assistant_message], "playbook_mode": "normal"}
345
- )
372
+ return Command(update={"messages": [mock_assistant_message], "playbook_mode": "normal"})
346
373
 
347
374
  async def route_entry(state: CodeActState) -> Literal["call_model", "playbook"]:
348
375
  """Route to either normal mode or playbook creation"""
349
- self.exported_tools = []
350
- self.tools_config.extend(state.get("selected_tool_ids", []))
351
- self.exported_tools = await self.registry.export_tools(self.tools_config, ToolFormat.LANGCHAIN)
376
+ all_tools = await self.registry.export_tools(state["selected_tool_ids"], ToolFormat.LANGCHAIN)
377
+ # print(all_tools)
378
+
379
+ # Create the initial system prompt and tools_context in one go
352
380
  self.final_instructions, self.tools_context = create_default_prompt(
353
- self.exported_tools, self.additional_tools, self.instructions, await get_connected_apps_string(self.registry), self.playbook
381
+ all_tools,
382
+ self.additional_tools,
383
+ self.instructions,
384
+ await get_connected_apps_string(self.registry),
385
+ self.playbook,
386
+ is_initial_prompt=True,
354
387
  )
355
388
  if state.get("playbook_mode") in ["planning", "confirming", "generating"]:
356
389
  return "playbook"
@@ -361,5 +394,4 @@ class CodeActPlaybookAgent(BaseAgent):
361
394
  agent.add_node(playbook)
362
395
  agent.add_node(execute_tools)
363
396
  agent.add_conditional_edges(START, route_entry)
364
- # agent.add_edge(START, "call_model")
365
397
  return agent.compile(checkpointer=self.memory)
@@ -3,6 +3,7 @@ import re
3
3
  from collections.abc import Sequence
4
4
 
5
5
  from langchain_core.tools import StructuredTool
6
+
6
7
  from universal_mcp.agents.codeact0.utils import schema_to_signature
7
8
 
8
9
  uneditable_prompt = """
@@ -137,13 +138,20 @@ def create_default_prompt(
137
138
  base_prompt: str | None = None,
138
139
  apps_string: str | None = None,
139
140
  playbook: object | None = None,
141
+ is_initial_prompt: bool = False,
140
142
  ):
141
- system_prompt = uneditable_prompt.strip()
142
- if apps_string:
143
- system_prompt += f"\n\n**Connected external applications (These apps have been logged into by the user):**\n{apps_string}\n\n Use `search_functions` to search for functions you can perform using the above. You can also discover more applications using the `search_functions` tool to find additional tools and integrations, if required.\n"
144
- system_prompt += "\n\nIn addition to the Python Standard Library, you can use the following external functions:\n"
145
-
143
+ if is_initial_prompt:
144
+ system_prompt = uneditable_prompt.strip()
145
+ if apps_string:
146
+ system_prompt += f"\n\n**Connected external applications (These apps have been logged into by the user):**\n{apps_string}\n\n Use `search_functions` to search for functions you can perform using the above. You can also discover more applications using the `search_functions` tool to find additional tools and integrations, if required.\n"
147
+ system_prompt += (
148
+ "\n\nIn addition to the Python Standard Library, you can use the following external functions:\n"
149
+ )
150
+ else:
151
+ system_prompt = ""
152
+
146
153
  tools_context = {}
154
+ tool_definitions = []
147
155
 
148
156
  for tool in tools:
149
157
  if hasattr(tool, "func") and tool.func is not None:
@@ -152,10 +160,11 @@ def create_default_prompt(
152
160
  elif hasattr(tool, "coroutine") and tool.coroutine is not None:
153
161
  tool_callable = tool.coroutine
154
162
  is_async = True
155
- system_prompt += f'''{"async " if is_async else ""}{schema_to_signature(tool.args, tool.name)}:
163
+ tool_definitions.append(
164
+ f'''{"async " if is_async else ""}{schema_to_signature(tool.args, tool.name)}:
156
165
  """{tool.description}"""
157
- ...
158
- '''
166
+ ...'''
167
+ )
159
168
  safe_name = make_safe_function_name(tool.name)
160
169
  tools_context[safe_name] = tool_callable
161
170
 
@@ -166,34 +175,40 @@ def create_default_prompt(
166
175
  elif hasattr(tool, "coroutine") and tool.coroutine is not None:
167
176
  tool_callable = tool.coroutine
168
177
  is_async = True
169
- system_prompt += f'''{"async " if is_async else ""}def {tool.name} {str(inspect.signature(tool_callable))}:
178
+ tool_definitions.append(
179
+ f'''{"async " if is_async else ""}def {tool.name} {str(inspect.signature(tool_callable))}:
170
180
  """{tool.description}"""
171
- ...
172
- '''
181
+ ...'''
182
+ )
173
183
  safe_name = make_safe_function_name(tool.name)
174
184
  tools_context[safe_name] = tool_callable
175
185
 
176
- if base_prompt and base_prompt.strip():
177
- system_prompt += f"Your goal is to perform the following task:\n\n{base_prompt}"
178
-
179
- # Append existing playbook (plan + code) if provided
180
- try:
181
- if playbook and hasattr(playbook, "instructions"):
182
- pb = playbook.instructions or {}
183
- plan = pb.get("playbookPlan")
184
- code = pb.get("playbookScript")
185
- if plan or code:
186
- system_prompt += "\n\nExisting Playbook Provided:\n"
187
- if plan:
188
- if isinstance(plan, list):
189
- plan_block = "\n".join(f"- {str(s)}" for s in plan)
190
- else:
191
- plan_block = str(plan)
192
- system_prompt += f"Plan Steps:\n{plan_block}\n"
193
- if code:
194
- system_prompt += f"\nScript:\n```python\n{str(code)}\n```\n"
195
- except Exception:
196
- # Silently ignore formatting issues
197
- pass
186
+ system_prompt += "\n".join(tool_definitions)
187
+
188
+ if is_initial_prompt:
189
+ if base_prompt and base_prompt.strip():
190
+ system_prompt += (
191
+ f"\n\nUse the following information/instructions while completing your tasks:\n\n{base_prompt}"
192
+ )
193
+
194
+ # Append existing playbook (plan + code) if provided
195
+ try:
196
+ if playbook and hasattr(playbook, "instructions"):
197
+ pb = playbook.instructions or {}
198
+ plan = pb.get("playbookPlan")
199
+ code = pb.get("playbookScript")
200
+ if plan or code:
201
+ system_prompt += "\n\nExisting Playbook Provided:\n"
202
+ if plan:
203
+ if isinstance(plan, list):
204
+ plan_block = "\n".join(f"- {str(s)}" for s in plan)
205
+ else:
206
+ plan_block = str(plan)
207
+ system_prompt += f"Plan Steps:\n{plan_block}\n"
208
+ if code:
209
+ system_prompt += f"\nScript:\n```python\n{str(code)}\n```\n"
210
+ except Exception:
211
+ # Silently ignore formatting issues
212
+ pass
198
213
 
199
214
  return system_prompt, tools_context
@@ -1,11 +1,11 @@
1
- from typing import Annotated, Any, List
1
+ from typing import Annotated, Any
2
2
 
3
3
  from langgraph.prebuilt.chat_agent_executor import AgentState
4
4
  from pydantic import BaseModel, Field
5
5
 
6
6
 
7
7
  class PlaybookPlan(BaseModel):
8
- steps: List[str] = Field(description="The steps of the playbook.")
8
+ steps: list[str] = Field(description="The steps of the playbook.")
9
9
 
10
10
 
11
11
  class PlaybookCode(BaseModel):
@@ -4,16 +4,18 @@ from collections import defaultdict
4
4
  from typing import Annotated, Any
5
5
 
6
6
  from langchain_core.tools import tool
7
- from loguru import logger
8
7
  from pydantic import Field
9
8
  from universal_mcp.agentr.registry import AgentrRegistry
10
9
  from universal_mcp.types import ToolFormat
11
10
 
11
+ from universal_mcp.agents.codeact0.prompts import create_default_prompt
12
+
12
13
 
13
14
  def enter_playbook_mode():
14
15
  """Call this function to enter playbook mode. Playbook mode is when the user wants to store a repeated task as a script with some inputs for the future."""
15
16
  return
16
17
 
18
+
17
19
  def create_meta_tools(tool_registry: AgentrRegistry) -> dict[str, Any]:
18
20
  """Create the meta tools for searching and loading tools"""
19
21
 
@@ -99,7 +101,9 @@ def create_meta_tools(tool_registry: AgentrRegistry) -> dict[str, Any]:
99
101
  prioritized_app_id_list = [canonical_app_id]
100
102
  else:
101
103
  # 1. Perform an initial broad search for tools.
102
- initial_tool_search_tasks = [registry.search_tools(query=q, distance_threshold=THRESHOLD) for q in queries]
104
+ initial_tool_search_tasks = [
105
+ registry.search_tools(query=q, distance_threshold=THRESHOLD) for q in queries
106
+ ]
103
107
  initial_tool_results = await asyncio.gather(*initial_tool_search_tasks)
104
108
 
105
109
  # 2. Search for relevant apps.
@@ -192,15 +196,53 @@ def create_meta_tools(tool_registry: AgentrRegistry) -> dict[str, Any]:
192
196
 
193
197
  @tool
194
198
  async def load_functions(tool_ids: list[str]) -> str:
195
- """Load specific functions by their IDs for use in subsequent steps.
199
+ """
200
+ Loads specified functions and returns their Python signatures and docstrings.
201
+ This makes the functions available for use inside the 'execute_ipython_cell' tool.
202
+ The agent MUST use the returned information to understand how to call the functions correctly.
196
203
 
197
204
  Args:
198
- tool_ids: Function ids in the form 'app__function'. Example: 'google_mail__send_email'
205
+ tool_ids: A list of function IDs in the format 'app__function'. Example: ['google_mail__send_email']
199
206
 
200
207
  Returns:
201
- Confirmation message about loaded functions
208
+ A string containing the signatures and docstrings of the successfully loaded functions,
209
+ ready for the agent to use in its code.
202
210
  """
203
- return f"Successfully loaded {len(tool_ids)} functions: {tool_ids}"
211
+ if not tool_ids:
212
+ return "No tool IDs provided to load."
213
+
214
+ # Step 1: Validate which tools are usable and get login links for others.
215
+ valid_tool_ids, unconnected_links = await get_valid_tools(tool_ids=tool_ids, registry=tool_registry)
216
+
217
+ if not valid_tool_ids:
218
+ return "Error: None of the provided tool IDs could be validated or loaded."
219
+
220
+ # Step 2: Export the schemas of the valid tools.
221
+ try:
222
+ # Create a temporary, clean registry to export only the requested tools
223
+ temp_registry = AgentrRegistry()
224
+ exported_tools = await temp_registry.export_tools(valid_tool_ids, ToolFormat.LANGCHAIN)
225
+ except Exception as e:
226
+ return f"Error exporting tools: {e}"
227
+
228
+ # Step 3: Build the informational string for the agent.
229
+ tool_definitions, _ = create_default_prompt(exported_tools, [], is_initial_prompt=False)
230
+
231
+ result_parts = [
232
+ f"Successfully loaded {len(exported_tools)} functions. They are now available for use inside `execute_ipython_cell`:",
233
+ tool_definitions,
234
+ ]
235
+
236
+ response_string = "\n\n".join(result_parts)
237
+
238
+ # Append login links if any apps were not connected
239
+ if unconnected_links:
240
+ links = "\n".join(unconnected_links)
241
+ response_string += (
242
+ f"\n\nPlease ask the user to log in to the following app(s) to use their full functionality:\n{links}"
243
+ )
244
+
245
+ return response_string
204
246
 
205
247
  @tool
206
248
  async def web_search(query: str) -> dict:
@@ -10,6 +10,26 @@ from universal_mcp.types import ToolConfig
10
10
  MAX_CHARS = 5000
11
11
 
12
12
 
13
+ def build_anthropic_cache_message(text: str, role: str = "system", ttl: str = "1h") -> list[dict[str, Any]]:
14
+ """Build a complete Anthropic cache messages array from text.
15
+
16
+ Returns a list with a single cache message whose content is the
17
+ cached Anthropic content array with ephemeral cache control and TTL.
18
+ """
19
+ return [
20
+ {
21
+ "role": role,
22
+ "content": [
23
+ {
24
+ "type": "text",
25
+ "text": text,
26
+ "cache_control": {"type": "ephemeral", "ttl": ttl},
27
+ }
28
+ ],
29
+ }
30
+ ]
31
+
32
+
13
33
  def add_tools(tool_config: ToolConfig, tools_to_add: ToolConfig):
14
34
  for app_id, new_tools in tools_to_add.items():
15
35
  all_tools = tool_config.get(app_id, []) + new_tools
@@ -375,6 +395,7 @@ def schema_to_signature(schema: dict, func_name: str = "my_function") -> str:
375
395
  param_str = ",\n ".join(params)
376
396
  return f"def {func_name}(\n {param_str},\n):"
377
397
 
398
+
378
399
  def smart_truncate(
379
400
  output: str, max_chars_full: int = 2000, max_lines_headtail: int = 20, summary_threshold: int = 10000
380
401
  ) -> str:
@@ -413,21 +434,21 @@ async def get_connected_apps_string(registry) -> str:
413
434
  """Get a formatted string of connected applications from the registry."""
414
435
  if not registry:
415
436
  return ""
416
-
437
+
417
438
  try:
418
439
  # Get connected apps from registry
419
440
  connections = await registry.list_connected_apps()
420
441
  if not connections:
421
442
  return "No applications are currently connected."
422
-
443
+
423
444
  # Extract app names from connections
424
445
  connected_app_ids = {connection["app_id"] for connection in connections}
425
-
446
+
426
447
  # Format the apps list
427
448
  apps_list = []
428
449
  for app_id in connected_app_ids:
429
450
  apps_list.append(f"- {app_id}")
430
-
451
+
431
452
  return "\n".join(apps_list)
432
453
  except Exception:
433
- return "Unable to retrieve connected applications."
454
+ return "Unable to retrieve connected applications."
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: universal-mcp-agents
3
- Version: 0.1.23rc1
3
+ Version: 0.1.23rc2
4
4
  Summary: Add your description here
5
5
  Project-URL: Homepage, https://github.com/universal-mcp/applications
6
6
  Project-URL: Repository, https://github.com/universal-mcp/applications
@@ -12,8 +12,8 @@ Requires-Dist: langchain-google-genai>=2.1.10
12
12
  Requires-Dist: langchain-openai>=0.3.32
13
13
  Requires-Dist: langgraph>=0.6.6
14
14
  Requires-Dist: typer>=0.17.4
15
- Requires-Dist: universal-mcp-applications>=0.1.24
16
- Requires-Dist: universal-mcp>=0.1.24rc25
15
+ Requires-Dist: universal-mcp-applications>=0.1.25
16
+ Requires-Dist: universal-mcp>=0.1.24rc26
17
17
  Provides-Extra: dev
18
18
  Requires-Dist: pre-commit; extra == 'dev'
19
19
  Requires-Dist: ruff; extra == 'dev'
@@ -1,6 +1,6 @@
1
- universal_mcp/agents/__init__.py,sha256=Rh6vKqpwuZ2joC9nzLFHUI1G7jzbwwC3p0mgpH5qRgo,1112
1
+ universal_mcp/agents/__init__.py,sha256=bW7WJopR6YZSLxghLf8nhohhHPWzm0wdGoZlmKDAcZ4,1078
2
2
  universal_mcp/agents/base.py,sha256=CEnY8y2as_XR311t9v2iqd4DOCSyhpOPOBDcZKNJMpc,7378
3
- universal_mcp/agents/cli.py,sha256=bXdpgxsOMjclm1STHJgx10ocX9EebQ11DrxH0p6KMZk,943
3
+ universal_mcp/agents/cli.py,sha256=9CG7majpWUz7C6t0d8xr-Sg2ZPKBuQdykTbYS6KIZ3A,922
4
4
  universal_mcp/agents/hil.py,sha256=_5PCK6q0goGm8qylJq44aSp2MadP-yCPvhOJYKqWLMo,3808
5
5
  universal_mcp/agents/llm.py,sha256=hVRwjZs3MHl5_3BWedmurs2Jt1oZDfFX0Zj9F8KH7fk,1787
6
6
  universal_mcp/agents/react.py,sha256=8XQvJ0HLVgc-K0qn9Ml48WGcgUGuIKtL67HatlT6Da0,3334
@@ -21,16 +21,16 @@ universal_mcp/agents/builder/helper.py,sha256=8igR1b3Gy_N2u3WxHYKIWzvw7F5BMnfpO2
21
21
  universal_mcp/agents/builder/prompts.py,sha256=8Xs6uzTUHguDRngVMLak3lkXFkk2VV_uQXaDllzP5cI,4670
22
22
  universal_mcp/agents/builder/state.py,sha256=7DeWllxfN-yD6cd9wJ3KIgjO8TctkJvVjAbZT8W_zqk,922
23
23
  universal_mcp/agents/codeact0/__init__.py,sha256=8-fvUo1Sm6dURGI-lW-X3Kd78LqySYbb5NMkNJ4NDwg,76
24
- universal_mcp/agents/codeact0/__main__.py,sha256=_7qSz97YnRgYJTESkALS5_eBIGHiMjA5rhr3IAeBvVo,896
25
- universal_mcp/agents/codeact0/agent.py,sha256=--cAjQ05VntXga2ofpnL9oFXAk6ANZ2SftwtLkQgy00,17844
24
+ universal_mcp/agents/codeact0/__main__.py,sha256=EHW9ePVePEemGI5yMUBc2Mp_JlrP6Apk1liab1y2Rd8,782
25
+ universal_mcp/agents/codeact0/agent.py,sha256=9YwcCeRE_bSl77JG54SOJjQxqZxCd_ZfdBJmgb1N0RA,18818
26
26
  universal_mcp/agents/codeact0/config.py,sha256=H-1woj_nhSDwf15F63WYn723y4qlRefXzGxuH81uYF0,2215
27
27
  universal_mcp/agents/codeact0/langgraph_agent.py,sha256=8nz2wq-LexImx-l1y9_f81fK72IQetnCeljwgnduNGY,420
28
28
  universal_mcp/agents/codeact0/llm_tool.py,sha256=-pAz04OrbZ_dJ2ueysT1qZd02DrbLY4EbU0tiuF_UNU,798
29
- universal_mcp/agents/codeact0/prompts.py,sha256=Gk6WTx2X7IOlbC3wYtGwKWDoeUtgpkMeiyfhj6LEq2I,11221
29
+ universal_mcp/agents/codeact0/prompts.py,sha256=qFcGDsISqW3iUNpW4yNDCH_vDwx75QS0acQsWI0Ul2w,11666
30
30
  universal_mcp/agents/codeact0/sandbox.py,sha256=Xw4tbUV_6haYIZZvteJi6lIYsW6ni_3DCRCOkslTKgM,4459
31
- universal_mcp/agents/codeact0/state.py,sha256=co3BZBuMIt1FP2qzgsbsLkyKbddCG1ieKyAw9TAskSU,1944
32
- universal_mcp/agents/codeact0/tools.py,sha256=1EBStJQQQCuNeWOqt0VP-XFiT0fE4oCR9nvUiwzRhe4,13164
33
- universal_mcp/agents/codeact0/utils.py,sha256=PgisAxmqYIzimc4lFA1nV-R7gxkICIrtO1q0FQZ-UoY,17580
31
+ universal_mcp/agents/codeact0/state.py,sha256=ESlxz68bwudasuL8jCI7GhweTqgLbYQqZM0mPkE06hQ,1938
32
+ universal_mcp/agents/codeact0/tools.py,sha256=bCIJDNkyE_6SULoU8LGvupOrRrvaa9JZVMPMTdsGDEs,15017
33
+ universal_mcp/agents/codeact0/utils.py,sha256=Gvft0W0Sg1qlFWm8ciX14yssCa8y3x037lql92yGsBQ,18164
34
34
  universal_mcp/agents/shared/__main__.py,sha256=XxH5qGDpgFWfq7fwQfgKULXGiUgeTp_YKfcxftuVZq8,1452
35
35
  universal_mcp/agents/shared/prompts.py,sha256=yjP3zbbuKi87qCj21qwTTicz8TqtkKgnyGSeEjMu3ho,3761
36
36
  universal_mcp/agents/shared/tool_node.py,sha256=DC9F-Ri28Pam0u3sXWNODVgmj9PtAEUb5qP1qOoGgfs,9169
@@ -39,6 +39,6 @@ universal_mcp/applications/filesystem/app.py,sha256=0TRjjm8YnslVRSmfkXI7qQOAlqWl
39
39
  universal_mcp/applications/llm/__init__.py,sha256=_XGRxN3O1--ZS5joAsPf8IlI9Qa6negsJrwJ5VJXno0,46
40
40
  universal_mcp/applications/llm/app.py,sha256=g9mK-luOLUshZzBGyQZMOHBeCSXmh2kCKir40YnsGUo,12727
41
41
  universal_mcp/applications/ui/app.py,sha256=c7OkZsO2fRtndgAzAQbKu-1xXRuRp9Kjgml57YD2NR4,9459
42
- universal_mcp_agents-0.1.23rc1.dist-info/METADATA,sha256=Fa-ACWH1oawoVDBAbp2iSSDGSCRovD2rWP9e9lUhcYE,881
43
- universal_mcp_agents-0.1.23rc1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
44
- universal_mcp_agents-0.1.23rc1.dist-info/RECORD,,
42
+ universal_mcp_agents-0.1.23rc2.dist-info/METADATA,sha256=v2vW8cUeI8cn6XY502FcEhBaYVeLpXu1n2D9FpUmJ4Y,881
43
+ universal_mcp_agents-0.1.23rc2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
44
+ universal_mcp_agents-0.1.23rc2.dist-info/RECORD,,