universal-mcp-agents 0.1.10__py3-none-any.whl → 0.1.11__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.
- universal_mcp/agents/codeact/agent.py +37 -59
- universal_mcp/agents/codeact/prompts.py +25 -34
- universal_mcp/agents/codeact/sandbox.py +28 -40
- universal_mcp/agents/codeact/state.py +3 -4
- {universal_mcp_agents-0.1.10.dist-info → universal_mcp_agents-0.1.11.dist-info}/METADATA +1 -1
- {universal_mcp_agents-0.1.10.dist-info → universal_mcp_agents-0.1.11.dist-info}/RECORD +7 -7
- {universal_mcp_agents-0.1.10.dist-info → universal_mcp_agents-0.1.11.dist-info}/WHEEL +0 -0
|
@@ -11,8 +11,6 @@ from universal_mcp.types import ToolConfig, ToolFormat
|
|
|
11
11
|
|
|
12
12
|
from universal_mcp.agents.base import BaseAgent
|
|
13
13
|
from universal_mcp.agents.codeact.prompts import (
|
|
14
|
-
REFLECTION_PROMPT,
|
|
15
|
-
RETRY_PROMPT,
|
|
16
14
|
create_default_prompt,
|
|
17
15
|
make_safe_function_name,
|
|
18
16
|
)
|
|
@@ -39,11 +37,6 @@ class CodeActAgent(BaseAgent):
|
|
|
39
37
|
self.tools_config = tools or {}
|
|
40
38
|
self.registry = registry
|
|
41
39
|
self.eval_fn = eval_unsafe
|
|
42
|
-
self.reflection_prompt = REFLECTION_PROMPT
|
|
43
|
-
self.reflection_model = self.model_instance
|
|
44
|
-
self.max_reflections = 3
|
|
45
|
-
self.tools_context = {}
|
|
46
|
-
self.context = {}
|
|
47
40
|
self.sandbox_timeout = sandbox_timeout
|
|
48
41
|
self.processed_tools: list[StructuredTool | Callable] = []
|
|
49
42
|
|
|
@@ -57,13 +50,6 @@ class CodeActAgent(BaseAgent):
|
|
|
57
50
|
|
|
58
51
|
self.instructions = create_default_prompt(self.processed_tools, self.instructions)
|
|
59
52
|
|
|
60
|
-
for tool in self.processed_tools:
|
|
61
|
-
safe_name = make_safe_function_name(tool.name)
|
|
62
|
-
tool_callable = tool.coroutine if hasattr(tool, "coroutine") and tool.coroutine is not None else tool.func
|
|
63
|
-
self.tools_context[safe_name] = tool_callable
|
|
64
|
-
|
|
65
|
-
self.context = {**self.context, **self.tools_context}
|
|
66
|
-
|
|
67
53
|
agent = StateGraph(CodeActState)
|
|
68
54
|
agent.add_node("call_model", self.call_model)
|
|
69
55
|
agent.add_node("sandbox", self.sandbox)
|
|
@@ -97,64 +83,56 @@ class CodeActAgent(BaseAgent):
|
|
|
97
83
|
return content
|
|
98
84
|
|
|
99
85
|
async def call_model(self, state: CodeActState) -> dict:
|
|
86
|
+
logger.debug(f"Calling model with state: {state}")
|
|
100
87
|
model = self.model_instance
|
|
101
|
-
reflection_model = self.reflection_model
|
|
102
88
|
|
|
103
|
-
|
|
89
|
+
# Find the last script and its output in the message history
|
|
90
|
+
previous_script = state.get("script", "")
|
|
91
|
+
sandbox_output = state.get("sandbox_output", "")
|
|
92
|
+
|
|
93
|
+
logger.debug(f"Previous script: {previous_script}")
|
|
94
|
+
logger.debug(f"Sandbox output: {sandbox_output}")
|
|
95
|
+
|
|
96
|
+
prompt_messages = [
|
|
97
|
+
{"role": "system", "content": self.instructions},
|
|
98
|
+
*state["messages"],
|
|
99
|
+
]
|
|
100
|
+
if previous_script:
|
|
101
|
+
feedback_message = (
|
|
102
|
+
f"Here is the script you generated in the last turn:\n\n```python\n{previous_script}\n```\n\n"
|
|
103
|
+
)
|
|
104
|
+
if sandbox_output:
|
|
105
|
+
feedback_message += (
|
|
106
|
+
f"When executed, it produced the following output:\n\n```\n{sandbox_output}\n```\n\n"
|
|
107
|
+
)
|
|
108
|
+
feedback_message += "Based on this, please generate a new, improved script to continue the task. Remember to replace the old script entirely."
|
|
109
|
+
prompt_messages.append({"role": "user", "content": feedback_message})
|
|
110
|
+
|
|
111
|
+
logger.debug(f"Prompt messages: {prompt_messages}")
|
|
104
112
|
|
|
105
|
-
response = await model.ainvoke(
|
|
113
|
+
response = await model.ainvoke(prompt_messages)
|
|
114
|
+
logger.debug(f"Model response: {response}")
|
|
106
115
|
|
|
107
116
|
text_content = self._extract_content(response)
|
|
108
117
|
if not isinstance(text_content, str):
|
|
109
118
|
raise ValueError(f"Content is not a string: {text_content}")
|
|
110
119
|
code = extract_and_combine_codeblocks(text_content)
|
|
111
|
-
logger.debug(f"
|
|
112
|
-
|
|
113
|
-
if self.max_reflections > 0 and code:
|
|
114
|
-
reflection_count = 0
|
|
115
|
-
while reflection_count < self.max_reflections:
|
|
116
|
-
conversation_history = "\n".join(
|
|
117
|
-
[
|
|
118
|
-
f'<message role="{("user" if m.type == "human" else "assistant")}">\n{m.content}\n</message>'
|
|
119
|
-
for m in state["messages"]
|
|
120
|
-
]
|
|
121
|
-
)
|
|
122
|
-
conversation_history += f'\n<message role="assistant">\n{response.content}\n</message>'
|
|
123
|
-
|
|
124
|
-
formatted_prompt = REFLECTION_PROMPT.format(conversation_history=conversation_history)
|
|
125
|
-
|
|
126
|
-
reflection_messages = [
|
|
127
|
-
{"role": "system", "content": self.reflection_prompt},
|
|
128
|
-
{"role": "user", "content": formatted_prompt},
|
|
129
|
-
]
|
|
130
|
-
reflection_result = await reflection_model.ainvoke(reflection_messages)
|
|
131
|
-
|
|
132
|
-
if "NONE" in reflection_result.content:
|
|
133
|
-
break
|
|
134
|
-
|
|
135
|
-
retry_prompt = RETRY_PROMPT.format(reflection_result=reflection_result.content)
|
|
136
|
-
|
|
137
|
-
regeneration_messages = [
|
|
138
|
-
{"role": "system", "content": self.instructions},
|
|
139
|
-
*state["messages"],
|
|
140
|
-
{"role": "assistant", "content": response.content},
|
|
141
|
-
{"role": "user", "content": retry_prompt},
|
|
142
|
-
]
|
|
143
|
-
response = await model.ainvoke(regeneration_messages)
|
|
144
|
-
|
|
145
|
-
code = extract_and_combine_codeblocks(response.content)
|
|
146
|
-
|
|
147
|
-
if not code:
|
|
148
|
-
break
|
|
149
|
-
|
|
150
|
-
reflection_count += 1
|
|
120
|
+
logger.debug(f"Extracted code: {code}")
|
|
151
121
|
|
|
152
122
|
return {"messages": [response], "script": code}
|
|
153
123
|
|
|
154
124
|
async def sandbox(self, state: CodeActState) -> dict:
|
|
155
|
-
|
|
156
|
-
|
|
125
|
+
logger.debug(f"Running sandbox with state: {state}")
|
|
126
|
+
tools_context = {}
|
|
127
|
+
for tool in self.processed_tools:
|
|
128
|
+
safe_name = make_safe_function_name(tool.name)
|
|
129
|
+
tool_callable = tool.coroutine if hasattr(tool, "coroutine") and tool.coroutine is not None else tool.func
|
|
130
|
+
tools_context[safe_name] = tool_callable
|
|
131
|
+
|
|
132
|
+
output, _ = await self.eval_fn(state["script"], tools_context, self.sandbox_timeout)
|
|
133
|
+
logger.debug(f"Sandbox output: {output}")
|
|
157
134
|
return {
|
|
158
135
|
"messages": [AIMessageChunk(content=output.strip())],
|
|
159
136
|
"script": None,
|
|
137
|
+
"sandbox_output": output.strip(),
|
|
160
138
|
}
|
|
@@ -24,9 +24,24 @@ def create_default_prompt(
|
|
|
24
24
|
):
|
|
25
25
|
"""Create default prompt for the CodeAct agent."""
|
|
26
26
|
prompt = f"{base_prompt}\n\n" if base_prompt else ""
|
|
27
|
-
prompt += """You will be given a task to perform.
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
prompt += """You are a Python programmer. You will be given a task to perform.
|
|
28
|
+
Your goal is to write a self-contained Python script to accomplish the task.
|
|
29
|
+
|
|
30
|
+
In each turn, you will generate a complete Python script. The script will be executed in a fresh, stateless environment.
|
|
31
|
+
You will be given the previous script you generated and the output it produced.
|
|
32
|
+
Your task is to analyze the output to find errors or opportunities for improvement, and then generate a new, improved script.
|
|
33
|
+
You must take the previous script as a starting point and replace it with a new one that moves closer to the final solution.
|
|
34
|
+
Your final script must be a single, complete piece of code that can be executed independently.
|
|
35
|
+
|
|
36
|
+
The script must follow this structure:
|
|
37
|
+
1. All necessary imports at the top.
|
|
38
|
+
2. An `async def main():` function containing the core logic.
|
|
39
|
+
3. Do NOT include any code outside of the `async def main()` function, and do NOT call it. The execution environment handles this.
|
|
40
|
+
|
|
41
|
+
Any output you want to see from the code should be printed to the console from within the `main` function.
|
|
42
|
+
Code should be output in a fenced code block (e.g. ```python ... ```).
|
|
43
|
+
|
|
44
|
+
If you need to ask for more information or provide the final answer, you can output text to be shown directly to the user.
|
|
30
45
|
|
|
31
46
|
In addition to the Python Standard Library, you can use the following functions:"""
|
|
32
47
|
|
|
@@ -43,17 +58,16 @@ In addition to the Python Standard Library, you can use the following functions:
|
|
|
43
58
|
...
|
|
44
59
|
'''
|
|
45
60
|
|
|
46
|
-
prompt += """
|
|
47
|
-
|
|
48
|
-
Variables defined at the top level of previous code snippets can be referenced in your code.
|
|
49
|
-
|
|
50
|
-
Always use print() statements to explore data structures and function outputs. Simply returning values will not display them back to you for inspection. For example, use print(result) instead of just 'result'.
|
|
61
|
+
prompt += """\n\n\nAlways use print() statements to explore data structures and function outputs. Simply returning values will not display them back to you for inspection. For example, use print(result) instead of just 'result'.
|
|
51
62
|
|
|
52
63
|
As you don't know the output schema of the additional Python functions you have access to, start from exploring their contents before building a final solution.
|
|
53
64
|
|
|
54
65
|
IMPORTANT CODING STRATEGY:
|
|
55
|
-
1.
|
|
56
|
-
2.
|
|
66
|
+
1. All your code must be inside an `async def main()` function.
|
|
67
|
+
2. Do NOT import `asyncio` or call `main()`. The execution environment handles this.
|
|
68
|
+
3. Since many of the provided tools are async, you must use `await` to call them from within `main()`.
|
|
69
|
+
4. Write code up to the point where you make an API call/tool usage with an output.
|
|
70
|
+
5. Print the type/shape and a sample entry of this output, and using that knowledge proceed to write the further code.
|
|
57
71
|
|
|
58
72
|
This means:
|
|
59
73
|
- Write code that makes the API call or tool usage
|
|
@@ -64,28 +78,5 @@ This means:
|
|
|
64
78
|
|
|
65
79
|
Reminder: use Python code snippets to call tools
|
|
66
80
|
|
|
67
|
-
When you have completely finished the task and provided the final
|
|
68
|
-
"""
|
|
81
|
+
When you have completely finished the task, present the final result from your script to the user in a clean and readable Markdown format. Do not just summarize what you did; provide the actual output. For example, if you were asked to find unsubscribe links and your script found them, your final response should be a Markdown-formatted list of those links. After you have provided the final output, you MUST end your response with the exact phrase "TASK_COMPLETE"."""
|
|
69
82
|
return prompt
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
REFLECTION_PROMPT = """
|
|
73
|
-
Review the assistant's latest code for as per the quality rules:
|
|
74
|
-
|
|
75
|
-
<conversation_history>
|
|
76
|
-
{conversation_history}
|
|
77
|
-
</conversation_history>
|
|
78
|
-
|
|
79
|
-
If you find ANY of these issues, describe the problem briefly and clearly.
|
|
80
|
-
If NO issues are found, respond with EXACTLY: "NONE"
|
|
81
|
-
"""
|
|
82
|
-
|
|
83
|
-
RETRY_PROMPT = """
|
|
84
|
-
I need you to completely regenerate your previous response based on this feedback:
|
|
85
|
-
|
|
86
|
-
'''
|
|
87
|
-
{reflection_result}
|
|
88
|
-
'''
|
|
89
|
-
|
|
90
|
-
DO NOT reference the feedback directly. Instead, provide a completely new response that addresses the issues.
|
|
91
|
-
"""
|
|
@@ -4,48 +4,36 @@ import contextlib
|
|
|
4
4
|
import io
|
|
5
5
|
from typing import Any
|
|
6
6
|
|
|
7
|
+
from loguru import logger
|
|
8
|
+
|
|
7
9
|
|
|
8
10
|
async def eval_unsafe(code: str, _locals: dict[str, Any], timeout: int = 10) -> tuple[str, dict[str, Any]]:
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
"""Executes a string of Python code in a sandboxed environment."""
|
|
12
|
+
# Store original keys before execution
|
|
13
|
+
original_keys = set(_locals.keys())
|
|
12
14
|
result = f"Executing code...\n{code}\n\nOutput:\n"
|
|
13
15
|
result += "=" * 50 + "\n"
|
|
14
|
-
|
|
15
|
-
# Create a combined globals/locals environment that includes builtins
|
|
16
|
-
# and the provided context. This allows nested functions to access tools.
|
|
17
|
-
execution_env = {**builtins.__dict__, **_locals}
|
|
18
|
-
|
|
19
|
-
def sync_eval_in_thread():
|
|
20
|
-
"""Synchronously execute code and capture output."""
|
|
21
|
-
try:
|
|
22
|
-
with contextlib.redirect_stdout(io.StringIO()) as f:
|
|
23
|
-
exec(code, execution_env)
|
|
24
|
-
output = f.getvalue()
|
|
25
|
-
if not output:
|
|
26
|
-
output = "<code ran, no output printed to stdout>"
|
|
27
|
-
return output
|
|
28
|
-
except Exception as e:
|
|
29
|
-
return f"Error during execution: {repr(e)}"
|
|
30
|
-
|
|
31
|
-
# Run the synchronous exec in a separate thread to avoid blocking the event loop.
|
|
32
16
|
try:
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
17
|
+
logger.debug(f"Executing code with timeout {timeout}")
|
|
18
|
+
with contextlib.redirect_stdout(io.StringIO()) as f:
|
|
19
|
+
# Execute the code in the provided locals context
|
|
20
|
+
# This should define an async function `main`
|
|
21
|
+
exec(code, builtins.__dict__, _locals)
|
|
22
|
+
|
|
23
|
+
if "main" in _locals and asyncio.iscoroutinefunction(_locals["main"]):
|
|
24
|
+
# Run the main async function
|
|
25
|
+
await asyncio.wait_for(_locals["main"](), timeout=timeout)
|
|
26
|
+
else:
|
|
27
|
+
result += "\nError: No `async def main()` function found in the script."
|
|
28
|
+
|
|
29
|
+
output = f.getvalue()
|
|
30
|
+
result += output
|
|
31
|
+
if not output:
|
|
32
|
+
result += "<code ran, no output printed to stdout>"
|
|
33
|
+
except Exception as e:
|
|
34
|
+
result += f"Error during execution: {repr(e)}"
|
|
35
|
+
|
|
36
|
+
# Determine new variables created during execution
|
|
37
|
+
new_keys = set(_locals.keys()) - original_keys
|
|
38
|
+
new_vars = {key: _locals[key] for key in new_keys}
|
|
39
|
+
return result, new_vars
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
from typing import Any
|
|
2
|
-
|
|
3
1
|
from langgraph.graph import MessagesState
|
|
2
|
+
from pydantic import Field
|
|
4
3
|
|
|
5
4
|
|
|
6
5
|
class CodeActState(MessagesState):
|
|
7
6
|
"""State for CodeAct agent."""
|
|
8
7
|
|
|
9
|
-
script: str | None
|
|
10
|
-
"
|
|
8
|
+
script: str | None = Field(default=None, description="The Python code script to be executed.")
|
|
9
|
+
sandbox_output: str | None = Field(default=None, description="The output of the Python code script execution.")
|
|
@@ -24,10 +24,10 @@ universal_mcp/agents/bigtoolcache/state.py,sha256=TQeGZD99okclkoCh5oz-VYIlEsC9yL
|
|
|
24
24
|
universal_mcp/agents/bigtoolcache/tools.py,sha256=ynyEj9mVwKKDhxm76sjspyH51SFi63g2Vydi39pY0qY,5562
|
|
25
25
|
universal_mcp/agents/codeact/__init__.py,sha256=rLE8gvOo5H4YSr71DRq76b3RV3uuotxuAy_VnBVaVwk,60
|
|
26
26
|
universal_mcp/agents/codeact/__main__.py,sha256=FRfIkgcZfawP-M66v4ePijA6J2fs7nQv92G_8cj5qYA,1142
|
|
27
|
-
universal_mcp/agents/codeact/agent.py,sha256=
|
|
28
|
-
universal_mcp/agents/codeact/prompts.py,sha256=
|
|
29
|
-
universal_mcp/agents/codeact/sandbox.py,sha256=
|
|
30
|
-
universal_mcp/agents/codeact/state.py,sha256=
|
|
27
|
+
universal_mcp/agents/codeact/agent.py,sha256=yB99aTRHWOdl4b67UxRstuV7WplrTjUZuqP1odwSS5o,5586
|
|
28
|
+
universal_mcp/agents/codeact/prompts.py,sha256=6NZkWQCaS7X7CLOTV-hMZgeWa1jf6iKbAT00INuluM0,4668
|
|
29
|
+
universal_mcp/agents/codeact/sandbox.py,sha256=pG6M1elzWw4KS46ewl-2d_U9Ap8HnHJDY9Rna_76sRI,1489
|
|
30
|
+
universal_mcp/agents/codeact/state.py,sha256=K25HcEljNNJDaeUgG5P68Kj752XzWTh8BwILY8w_EtE,357
|
|
31
31
|
universal_mcp/agents/codeact/utils.py,sha256=JUbT_HYGS_D1BzmzoVpORIe7SGur1KgJguTZ_1tZ4JY,1918
|
|
32
32
|
universal_mcp/agents/planner/__init__.py,sha256=9P1UL-ABvrTIWTJ8wcvZmkqT8uyROZxsmUFhpjTK-Q4,1313
|
|
33
33
|
universal_mcp/agents/planner/__main__.py,sha256=OfhTfYDZK_ZUfc8sX-Sa6TWk-dNqD2rl13Ln64mNAtw,771
|
|
@@ -37,6 +37,6 @@ universal_mcp/agents/planner/state.py,sha256=qqyp-jSGsCxe1US-PRLT4-y1sITAcVE6nCM
|
|
|
37
37
|
universal_mcp/agents/shared/prompts.py,sha256=VOsXSUEwBXPaAuxJTUF6bgDGr41u6uctUNQSMRt_OJc,6414
|
|
38
38
|
universal_mcp/agents/shared/tool_node.py,sha256=Ua_wzMt4YgIx4zLp3_ZCow-28qORwrZ2FvKqLPt3RlI,10415
|
|
39
39
|
universal_mcp/applications/ui/app.py,sha256=uaS1KrwrGxw9oexdLj2Jok77DrZQAmby3uVxCONQyV8,11276
|
|
40
|
-
universal_mcp_agents-0.1.
|
|
41
|
-
universal_mcp_agents-0.1.
|
|
42
|
-
universal_mcp_agents-0.1.
|
|
40
|
+
universal_mcp_agents-0.1.11.dist-info/METADATA,sha256=MosDAsvQTSvUr41527xNHF8QpEeatOh7g0bpBDr7lW0,878
|
|
41
|
+
universal_mcp_agents-0.1.11.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
42
|
+
universal_mcp_agents-0.1.11.dist-info/RECORD,,
|
|
File without changes
|