universal-mcp 0.1.24rc14__py3-none-any.whl → 0.1.24rc17__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.
Files changed (60) hide show
  1. universal_mcp/agentr/registry.py +4 -4
  2. universal_mcp/applications/application.py +0 -2
  3. universal_mcp/applications/utils.py +52 -0
  4. universal_mcp/servers/server.py +4 -3
  5. universal_mcp/tools/manager.py +0 -3
  6. universal_mcp/types.py +1 -21
  7. universal_mcp/utils/prompts.py +0 -2
  8. universal_mcp/utils/testing.py +1 -1
  9. {universal_mcp-0.1.24rc14.dist-info → universal_mcp-0.1.24rc17.dist-info}/METADATA +2 -1
  10. universal_mcp-0.1.24rc17.dist-info/RECORD +54 -0
  11. universal_mcp/__init__.py +0 -0
  12. universal_mcp/agents/__init__.py +0 -10
  13. universal_mcp/agents/autoagent/__init__.py +0 -30
  14. universal_mcp/agents/autoagent/__main__.py +0 -25
  15. universal_mcp/agents/autoagent/context.py +0 -26
  16. universal_mcp/agents/autoagent/graph.py +0 -151
  17. universal_mcp/agents/autoagent/prompts.py +0 -9
  18. universal_mcp/agents/autoagent/state.py +0 -27
  19. universal_mcp/agents/autoagent/studio.py +0 -25
  20. universal_mcp/agents/autoagent/utils.py +0 -13
  21. universal_mcp/agents/base.py +0 -129
  22. universal_mcp/agents/bigtool/__init__.py +0 -54
  23. universal_mcp/agents/bigtool/__main__.py +0 -24
  24. universal_mcp/agents/bigtool/context.py +0 -24
  25. universal_mcp/agents/bigtool/graph.py +0 -166
  26. universal_mcp/agents/bigtool/prompts.py +0 -31
  27. universal_mcp/agents/bigtool/state.py +0 -27
  28. universal_mcp/agents/bigtool2/__init__.py +0 -53
  29. universal_mcp/agents/bigtool2/__main__.py +0 -24
  30. universal_mcp/agents/bigtool2/agent.py +0 -11
  31. universal_mcp/agents/bigtool2/context.py +0 -33
  32. universal_mcp/agents/bigtool2/graph.py +0 -169
  33. universal_mcp/agents/bigtool2/prompts.py +0 -12
  34. universal_mcp/agents/bigtool2/state.py +0 -27
  35. universal_mcp/agents/builder.py +0 -80
  36. universal_mcp/agents/cli.py +0 -27
  37. universal_mcp/agents/codeact/__init__.py +0 -243
  38. universal_mcp/agents/codeact/sandbox.py +0 -27
  39. universal_mcp/agents/codeact/test.py +0 -15
  40. universal_mcp/agents/codeact/utils.py +0 -61
  41. universal_mcp/agents/hil.py +0 -104
  42. universal_mcp/agents/llm.py +0 -45
  43. universal_mcp/agents/planner/__init__.py +0 -37
  44. universal_mcp/agents/planner/__main__.py +0 -24
  45. universal_mcp/agents/planner/graph.py +0 -82
  46. universal_mcp/agents/planner/prompts.py +0 -1
  47. universal_mcp/agents/planner/state.py +0 -12
  48. universal_mcp/agents/react.py +0 -84
  49. universal_mcp/agents/shared/agent_node.py +0 -34
  50. universal_mcp/agents/shared/tool_node.py +0 -235
  51. universal_mcp/agents/simple.py +0 -40
  52. universal_mcp/agents/tools.py +0 -35
  53. universal_mcp/agents/utils.py +0 -111
  54. universal_mcp/analytics.py +0 -111
  55. universal_mcp/applications/__init__.py +0 -70
  56. universal_mcp/utils/common.py +0 -278
  57. universal_mcp-0.1.24rc14.dist-info/RECORD +0 -99
  58. {universal_mcp-0.1.24rc14.dist-info → universal_mcp-0.1.24rc17.dist-info}/WHEEL +0 -0
  59. {universal_mcp-0.1.24rc14.dist-info → universal_mcp-0.1.24rc17.dist-info}/entry_points.txt +0 -0
  60. {universal_mcp-0.1.24rc14.dist-info → universal_mcp-0.1.24rc17.dist-info}/licenses/LICENSE +0 -0
@@ -1,169 +0,0 @@
1
- import json
2
- from datetime import UTC, datetime
3
- from typing import Literal, TypedDict, cast
4
-
5
- from langchain_anthropic import ChatAnthropic
6
- from langchain_core.language_models import BaseChatModel
7
- from langchain_core.messages import AIMessage, ToolMessage
8
- from langchain_core.tools import tool
9
- from langgraph.graph import StateGraph
10
- from langgraph.runtime import Runtime
11
- from langgraph.types import Command
12
-
13
- from universal_mcp.agents.bigtool2.context import Context
14
- from universal_mcp.agents.bigtool2.state import State
15
- from universal_mcp.logger import logger
16
- from universal_mcp.tools.registry import ToolRegistry
17
- from universal_mcp.types import ToolFormat
18
-
19
-
20
-
21
- def build_graph(
22
- tool_registry: ToolRegistry,
23
- llm: BaseChatModel
24
- ):
25
- @tool
26
- async def search_tools(queries: list[str]) -> str:
27
- """Search tools for a given list of queries
28
- Each single query should be atomic (doable with a single tool).
29
- For tasks requiring multiple tools, add separate queries for each subtask"""
30
- logger.info(f"Searching tools for queries: '{queries}'")
31
- try:
32
- all_tool_candidates = ""
33
- app_ids = await tool_registry.list_all_apps()
34
- connections = await tool_registry.list_connected_apps()
35
- connection_ids = set([connection["app_id"] for connection in connections])
36
- connected_apps = [app["id"] for app in app_ids if app["id"] in connection_ids]
37
- unconnected_apps = [app["id"] for app in app_ids if app["id"] not in connection_ids]
38
- app_tools = {}
39
- for task_query in queries:
40
- tools_list = await tool_registry.search_tools(task_query, limit=40)
41
- tool_candidates = [f"{tool['id']}: {tool['description']}" for tool in tools_list]
42
- for tool in tool_candidates:
43
- app = tool.split("__")[0]
44
- if app not in app_tools:
45
- if len(app_tools.keys()) >= 10:
46
- break
47
- app_tools[app] = []
48
- if len(app_tools[app]) < 3:
49
- app_tools[app].append(tool)
50
- for app in app_tools:
51
- app_status = "connected" if app in connected_apps else "NOT connected"
52
- all_tool_candidates += f"Tools from {app} (status: {app_status} by user):\n"
53
- for tool in app_tools[app]:
54
- all_tool_candidates += f" - {tool}\n"
55
- all_tool_candidates += "\n"
56
-
57
-
58
- return all_tool_candidates
59
- except Exception as e:
60
- logger.error(f"Error retrieving tools: {e}")
61
- return "Error: " + str(e)
62
-
63
- @tool
64
- async def load_tools(tool_ids: list[str]) -> list[str]:
65
- """Load the tools for the given tool ids. Returns the tool ids."""
66
- return tool_ids
67
-
68
-
69
- async def call_model(state: State, runtime: Runtime[Context]) -> Command[Literal["select_tools", "call_tools"]]:
70
- logger.info("Calling model...")
71
- try:
72
- system_message = runtime.context.system_prompt.format(system_time=datetime.now(tz=UTC).isoformat())
73
- messages = [{"role": "system", "content": system_message}, *state["messages"]]
74
-
75
- logger.info(f"Selected tool IDs: {state['selected_tool_ids']}")
76
- if len(state["selected_tool_ids"]) > 0:
77
- selected_tools = await tool_registry.export_tools(tools=state["selected_tool_ids"], format=ToolFormat.LANGCHAIN)
78
- logger.info(f"Exported {len(selected_tools)} tools for model.")
79
- else:
80
- selected_tools = []
81
-
82
- model = llm
83
-
84
- model_with_tools = model.bind_tools([search_tools, load_tools, *selected_tools], tool_choice="auto")
85
- response = cast(AIMessage, await model_with_tools.ainvoke(messages))
86
-
87
- if response.tool_calls:
88
- logger.info(f"Model responded with {len(response.tool_calls)} tool calls.")
89
- if len(response.tool_calls) > 1:
90
- raise Exception("Not possible in Claude with llm.bind_tools(tools=tools, tool_choice='auto')")
91
- tool_call = response.tool_calls[0]
92
- if tool_call["name"] == search_tools.name:
93
- logger.info("Model requested to select tools.")
94
- return Command(goto="select_tools", update={"messages": [response]})
95
- elif tool_call["name"] == load_tools.name:
96
- logger.info("Model requested to load tools.")
97
- tool_msg = ToolMessage(f"Loaded tools.", tool_call_id=tool_call["id"])
98
- selected_tool_ids = tool_call["args"]["tool_ids"]
99
- logger.info(f"Loaded tools: {selected_tool_ids}")
100
- return Command(goto="call_model", update={ "messages": [response, tool_msg], "selected_tool_ids": selected_tool_ids})
101
-
102
- elif tool_call["name"] not in state["selected_tool_ids"]:
103
- try:
104
- await tool_registry.export_tools([tool_call["name"]], ToolFormat.LANGCHAIN)
105
- logger.info(
106
- f"Tool '{tool_call['name']}' not in selected tools, but available. Proceeding to call."
107
- )
108
- return Command(goto="call_tools", update={"messages": [response]})
109
- except Exception as e:
110
- logger.error(f"Unexpected tool call: {tool_call['name']}. Error: {e}")
111
- raise Exception(
112
- f"Unexpected tool call: {tool_call['name']}. Available tools: {state['selected_tool_ids']}"
113
- ) from e
114
- logger.info(f"Proceeding to call tool: {tool_call['name']}")
115
- return Command(goto="call_tools", update={"messages": [response]})
116
- else:
117
- logger.info("Model responded with a message, ending execution.")
118
- return Command(update={"messages": [response]})
119
- except Exception as e:
120
- logger.error(f"Error in call_model: {e}")
121
- raise
122
-
123
- async def select_tools(state: State, runtime: Runtime[Context]) -> Command[Literal["call_model"]]:
124
- logger.info("Selecting tools...")
125
- try:
126
- tool_call = state["messages"][-1].tool_calls[0]
127
- searched_tools= await search_tools.ainvoke(input=tool_call["args"])
128
- tool_msg = ToolMessage(f"Available tools: {searched_tools}", tool_call_id=tool_call["id"])
129
- return Command(goto="call_model", update={"messages": [tool_msg]})
130
- except Exception as e:
131
- logger.error(f"Error in select_tools: {e}")
132
- raise
133
-
134
- async def call_tools(state: State) -> Command[Literal["call_model"]]:
135
- logger.info("Calling tools...")
136
- outputs = []
137
- recent_tool_ids = []
138
- for tool_call in state["messages"][-1].tool_calls:
139
- logger.info(f"Executing tool: {tool_call['name']} with args: {tool_call['args']}")
140
- try:
141
- await tool_registry.export_tools([tool_call["name"]], ToolFormat.LANGCHAIN)
142
- tool_result = await tool_registry.call_tool(tool_call["name"], tool_call["args"])
143
- logger.info(f"Tool '{tool_call['name']}' executed successfully.")
144
- outputs.append(
145
- ToolMessage(
146
- content=json.dumps(tool_result),
147
- name=tool_call["name"],
148
- tool_call_id=tool_call["id"],
149
- )
150
- )
151
- recent_tool_ids.append(tool_call["name"])
152
- except Exception as e:
153
- logger.error(f"Error executing tool '{tool_call['name']}': {e}")
154
- outputs.append(
155
- ToolMessage(
156
- content=json.dumps("Error: " + str(e)),
157
- name=tool_call["name"],
158
- tool_call_id=tool_call["id"],
159
- )
160
- )
161
- return Command(goto="call_model", update={"messages": outputs, "selected_tool_ids": recent_tool_ids})
162
-
163
- builder = StateGraph(State, context_schema=Context)
164
-
165
- builder.add_node(call_model)
166
- builder.add_node(select_tools)
167
- builder.add_node(call_tools)
168
- builder.set_entry_point("call_model")
169
- return builder
@@ -1,12 +0,0 @@
1
- """Default prompts used by the agent."""
2
-
3
- SYSTEM_PROMPT = """You are a helpful AI assistant.
4
-
5
- **Core Directives:**
6
- 1. **Always Use Tools for Tasks:** For any user request that requires an action (e.g., sending an email, searching for information, creating an event), you MUST use a tool. Do not answer from your own knowledge or refuse a task if a tool might exist for it.
7
- 2. **First Step is ALWAYS `search_tools`:** Before you can use any other tool, you MUST first call the `search_tools` function to find the right tools for the user's request. This is your mandatory first action. You must not use the same/similar query multiple times in the list. The list should have multiple queries only if the task has clearly different sub-tasks.
8
- 3. **Load Tools:** After looking at the output of `search_tools`, you MUST call the `load_tools` function to load only the tools you want to use. Use your judgement to eliminate irrelevant apps that came up just because of semantic similarity. However, sometimes, multiple apps might be relevant for the same task. Prefer connected apps over unconnected apps while breaking a tie. If more than one relevant app (or none of the relevant apps) are connected, you must ask the user to choose the app. In case the user asks you to use an app that is not connected, call the apps tools normally. The tool will return a link for connecting that you should pass on to the user.
9
- 3. **Strictly Follow the Process:** Your only job in your first turn is to analyze the user's request and call `search_tools` with a concise query describing the core task. Do not engage in conversation.
10
-
11
- System time: {system_time}
12
- """
@@ -1,27 +0,0 @@
1
- from typing import Annotated
2
-
3
- from langgraph.prebuilt.chat_agent_executor import AgentState
4
-
5
-
6
- def _enqueue(left: list, right: list) -> list:
7
- """Treat left as a FIFO queue, append new items from right (preserve order),
8
- keep items unique, and cap total size to 20 (drop oldest items)."""
9
- max_size = 30
10
- preferred_size = 20
11
- if len(right) > preferred_size:
12
- preferred_size = min(max_size, len(right))
13
- queue = list(left or [])
14
-
15
- for item in right[:preferred_size] or []:
16
- if item in queue:
17
- queue.remove(item)
18
- queue.append(item)
19
-
20
- if len(queue) > preferred_size:
21
- queue = queue[-preferred_size:]
22
-
23
- return queue
24
-
25
-
26
- class State(AgentState):
27
- selected_tool_ids: Annotated[list[str], _enqueue]
@@ -1,80 +0,0 @@
1
- import asyncio
2
- from collections.abc import Sequence
3
- from typing import Annotated, TypedDict
4
-
5
- from langchain_core.language_models import BaseChatModel
6
- from langchain_core.messages import BaseMessage
7
- from langgraph.checkpoint.base import BaseCheckpointSaver
8
- from langgraph.graph import END, START, StateGraph
9
- from langgraph.graph.message import add_messages
10
-
11
- from universal_mcp.agents.base import BaseAgent
12
- from universal_mcp.agents.llm import load_chat_model
13
- from universal_mcp.agents.shared.agent_node import Agent, generate_agent
14
- from universal_mcp.agents.shared.tool_node import build_tool_node_graph
15
- from universal_mcp.tools.registry import ToolRegistry
16
- from universal_mcp.types import ToolConfig
17
-
18
-
19
- class BuilderState(TypedDict):
20
- messages: Annotated[Sequence[BaseMessage], add_messages]
21
- generated_agent: Agent | None
22
- tool_config: ToolConfig | None
23
-
24
-
25
- class BuilderAgent(BaseAgent):
26
- def __init__(
27
- self,
28
- name: str,
29
- instructions: str,
30
- model: str,
31
- registry: ToolRegistry,
32
- memory: BaseCheckpointSaver | None = None,
33
- **kwargs,
34
- ):
35
- super().__init__(name, instructions, model, memory, **kwargs)
36
- self.registry = registry
37
- self.llm: BaseChatModel = load_chat_model(model)
38
-
39
- async def _create_agent(self, state: BuilderState):
40
- last_message = state["messages"][-1]
41
- generated_agent = await generate_agent(self.llm, last_message.content)
42
- return {"generated_agent": generated_agent}
43
-
44
- async def _create_tool_config(self, state: BuilderState):
45
- last_message = state["messages"][-1]
46
- tool_finder_graph = build_tool_node_graph(self.llm, self.registry)
47
- tool_config = await tool_finder_graph.ainvoke({"task": last_message.content, "messages": [last_message]})
48
- tool_config = tool_config.get("apps_with_tools", {})
49
- return {"tool_config": tool_config}
50
-
51
- async def _build_graph(self):
52
- builder = StateGraph(BuilderState)
53
- builder.add_node("create_agent", self._create_agent)
54
- builder.add_node("create_tool_config", self._create_tool_config)
55
- builder.add_edge(START, "create_agent")
56
- builder.add_edge("create_agent", "create_tool_config")
57
- builder.add_edge("create_tool_config", END)
58
- return builder.compile()
59
-
60
-
61
- async def main():
62
- from universal_mcp.agentr.registry import AgentrRegistry
63
-
64
- registry = AgentrRegistry()
65
- agent = BuilderAgent(
66
- name="Builder Agent",
67
- instructions="You are a builder agent that creates other agents.",
68
- model="gemini/gemini-1.5-pro",
69
- registry=registry,
70
- )
71
- result = await agent.invoke(
72
- "Send a daily email to manoj@agentr.dev with daily agenda of the day",
73
- )
74
- print(result.model_dump_json(indent=2))
75
- # print(f"Agent: {result['generated_agent'].model_dump_json(indent=2)}")
76
- # print(f"Tool Config: {result['tool_config'].model_dump_json(indent=2)}")
77
-
78
-
79
- if __name__ == "__main__":
80
- asyncio.run(main())
@@ -1,27 +0,0 @@
1
- from typer import Typer
2
-
3
- from universal_mcp.agents import ReactAgent
4
- from universal_mcp.logger import setup_logger
5
-
6
- app = Typer()
7
-
8
-
9
- @app.command(
10
- help="Run the agent CLI",
11
- epilog="""
12
- Example:
13
- mcp client run --config client_config.json
14
- """,
15
- )
16
- def run():
17
- """Run the agent CLI"""
18
- import asyncio
19
-
20
- setup_logger(log_file=None, level="WARNING")
21
-
22
- agent = ReactAgent("React Agent", "You are a helpful assistant", "openrouter/auto")
23
- asyncio.run(agent.run_interactive())
24
-
25
-
26
- if __name__ == "__main__":
27
- app()
@@ -1,243 +0,0 @@
1
- import inspect
2
- import re
3
- from collections.abc import Awaitable, Callable, Sequence
4
- from typing import Any, TypeVar
5
-
6
- from langchain_core.language_models import BaseChatModel
7
- from langchain_core.tools import StructuredTool
8
- from langchain_core.tools import tool as create_tool
9
- from langgraph.graph import END, START, MessagesState, StateGraph
10
- from langgraph.types import Command
11
-
12
- from .utils import extract_and_combine_codeblocks
13
-
14
- EvalFunction = Callable[[str, dict[str, Any]], tuple[str, dict[str, Any]]]
15
- EvalCoroutine = Callable[[str, dict[str, Any]], Awaitable[tuple[str, dict[str, Any]]]]
16
-
17
-
18
- class CodeActState(MessagesState):
19
- """State for CodeAct agent."""
20
-
21
- script: str | None
22
- """The Python code script to be executed."""
23
- context: dict[str, Any]
24
- """Dictionary containing the execution context with available tools and variables."""
25
-
26
-
27
- StateSchema = TypeVar("StateSchema", bound=CodeActState)
28
- StateSchemaType = type[StateSchema]
29
-
30
-
31
- def make_safe_function_name(name: str) -> str:
32
- """Convert a tool name to a valid Python function name."""
33
- # Replace non-alphanumeric characters with underscores
34
- safe_name = re.sub(r"[^a-zA-Z0-9_]", "_", name)
35
- # Ensure the name doesn't start with a digit
36
- if safe_name and safe_name[0].isdigit():
37
- safe_name = f"tool_{safe_name}"
38
- # Handle empty name edge case
39
- if not safe_name:
40
- safe_name = "unnamed_tool"
41
- return safe_name
42
-
43
-
44
- def create_default_prompt(tools: list[StructuredTool], base_prompt: str | None = None):
45
- """Create default prompt for the CodeAct agent."""
46
- tools = [t if isinstance(t, StructuredTool) else create_tool(t) for t in tools]
47
- prompt = f"{base_prompt}\n\n" if base_prompt else ""
48
- prompt += """You will be given a task to perform. You should output either
49
- - a Python code snippet that provides the solution to the task, or a step towards the solution. Any output you want to extract from the code should be printed to the console. Code should be output in a fenced code block.
50
- - text to be shown directly to the user, if you want to ask for more information or provide the final answer.
51
-
52
- In addition to the Python Standard Library, you can use the following functions:
53
- """
54
-
55
- for tool in tools:
56
- # Use coroutine if it exists, otherwise use func
57
- tool_callable = tool.coroutine if hasattr(tool, "coroutine") and tool.coroutine is not None else tool.func
58
- # Create a safe function name
59
- safe_name = make_safe_function_name(tool.name)
60
- # Determine if it's an async function
61
- is_async = inspect.iscoroutinefunction(tool_callable)
62
- # Add appropriate function definition
63
- prompt += f'''
64
- {"async " if is_async else ""}def {safe_name}{str(inspect.signature(tool_callable))}:
65
- """{tool.description}"""
66
- ...
67
- '''
68
-
69
- prompt += """
70
-
71
- Variables defined at the top level of previous code snippets can be referenced in your code.
72
-
73
- Reminder: use Python code snippets to call tools"""
74
- return prompt
75
-
76
-
77
- def create_codeact(
78
- model: BaseChatModel,
79
- tools: Sequence[StructuredTool | Callable],
80
- eval_fn: EvalFunction | EvalCoroutine,
81
- *,
82
- prompt: str | None = None,
83
- reflection_prompt: str | None = None,
84
- reflection_model: BaseChatModel | None = None,
85
- max_reflections: int = 3,
86
- state_schema: StateSchemaType = CodeActState,
87
- ) -> StateGraph:
88
- """Create a CodeAct agent.
89
-
90
- Args:
91
- model: The language model to use for generating code
92
- tools: List of tools available to the agent. Can be passed as python functions or StructuredTool instances.
93
- eval_fn: Function or coroutine that executes code in a sandbox. Takes code string and locals dict,
94
- returns a tuple of (stdout output, new variables dict)
95
- prompt: Optional custom system prompt. If None, uses default prompt.
96
- To customize default prompt you can use `create_default_prompt` helper:
97
- `create_default_prompt(tools, "You are a helpful assistant.")`
98
- reflection_prompt: Optional prompt for reflection. If provided, will be used to evaluate responses.
99
- If the reflection output contains "NONE", the response is considered valid, otherwise the
100
- reflection output is passed back to the model for regeneration.
101
- reflection_model: Optional model to use for reflection. If None, uses the same model as for generation.
102
- max_reflections: Maximum number of reflection iterations (default: 3).
103
- state_schema: The state schema to use for the agent.
104
-
105
- Returns:
106
- A StateGraph implementing the CodeAct architecture
107
- """
108
- tools = [t if isinstance(t, StructuredTool) else create_tool(t) for t in tools]
109
-
110
- if prompt is None:
111
- prompt = create_default_prompt(tools)
112
-
113
- # If no reflection model is provided, use the main model
114
- if reflection_model is None:
115
- reflection_model = model
116
-
117
- # Make tools available to the code sandbox - use safe names for keys
118
- tools_context = {}
119
- for tool in tools:
120
- safe_name = make_safe_function_name(tool.name)
121
- # Use coroutine if it exists, otherwise use func (same as in create_default_prompt)
122
- tool_callable = tool.coroutine if hasattr(tool, "coroutine") and tool.coroutine is not None else tool.func
123
- # Only use the safe name for consistency with the prompt
124
- tools_context[safe_name] = tool_callable
125
-
126
- def call_model(state: StateSchema) -> Command:
127
- messages = [{"role": "system", "content": prompt}] + state["messages"]
128
-
129
- # Run the model and potentially loop for reflection
130
- response = model.invoke(messages)
131
-
132
- # Extract and combine all code blocks
133
- code = extract_and_combine_codeblocks(response.content)
134
-
135
- # Loop for reflection if needed and if code is present
136
- if reflection_prompt and code:
137
- reflection_count = 0
138
- while reflection_count < max_reflections:
139
- # Format conversation history with XML-style tags
140
- conversation_history = "\n".join(
141
- [
142
- f'<message role="{("user" if m.type == "human" else "assistant")}">\n{m.content}\n</message>'
143
- for m in state["messages"]
144
- ]
145
- )
146
-
147
- # Add the current response
148
- conversation_history += f'\n<message role="assistant">\n{response.content}\n</message>'
149
-
150
- # Create the reflection prompt with the tagged conversation history
151
- formatted_prompt = f"""
152
- Review the assistant's latest code for as per the quality rules:
153
-
154
- <conversation_history>
155
- {conversation_history}
156
- </conversation_history>
157
-
158
- If you find ANY of these issues, describe the problem briefly and clearly.
159
- If NO issues are found, respond with EXACTLY: "NONE"
160
- """
161
-
162
- # Create messages for reflection with correct ordering
163
- reflection_messages = [
164
- {"role": "system", "content": reflection_prompt},
165
- # Include the formatted reflection prompt as the final user message
166
- {"role": "user", "content": formatted_prompt},
167
- ]
168
- reflection_result = reflection_model.invoke(reflection_messages)
169
-
170
- # Check if reflection passed
171
- if "NONE" in reflection_result.content:
172
- # Reflection passed, exit loop
173
- break
174
-
175
- # Reflection didn't pass, regenerate response
176
- reflection_messages = [
177
- {"role": "system", "content": prompt},
178
- *state["messages"],
179
- {"role": "assistant", "content": response.content},
180
- {
181
- "role": "user",
182
- "content": f"""
183
- I need you to completely regenerate your previous response based on this feedback:
184
-
185
- '''
186
- {reflection_result.content}
187
- '''
188
-
189
- DO NOT reference the feedback directly. Instead, provide a completely new response that addresses the issues.
190
- """,
191
- },
192
- ]
193
- response = model.invoke(reflection_messages)
194
-
195
- # Extract code from the new response
196
- code = extract_and_combine_codeblocks(response.content)
197
-
198
- # If no code in the new response, exit the reflection loop
199
- if not code:
200
- break
201
-
202
- # Increment reflection count
203
- reflection_count += 1
204
-
205
- # Return appropriate command with only the latest response
206
- if code:
207
- return Command(goto="sandbox", update={"messages": [response], "script": code})
208
- else:
209
- # no code block, end the loop and respond to the user
210
- return Command(update={"messages": [response], "script": None})
211
-
212
- # If eval_fn is a async, we define async node function.
213
- if inspect.iscoroutinefunction(eval_fn):
214
-
215
- async def sandbox(state: StateSchema):
216
- existing_context = state.get("context", {})
217
- context = {**existing_context, **tools_context}
218
- # Execute the script in the sandbox
219
- output, new_vars = await eval_fn(state["script"], context)
220
- new_context = {**existing_context, **new_vars}
221
- return {
222
- "messages": [{"role": "user", "content": output}],
223
- "context": new_context,
224
- }
225
- else:
226
-
227
- def sandbox(state: StateSchema):
228
- existing_context = state.get("context", {})
229
- context = {**existing_context, **tools_context}
230
- # Execute the script in the sandbox
231
- output, new_vars = eval_fn(state["script"], context)
232
- new_context = {**existing_context, **new_vars}
233
- return {
234
- "messages": [{"role": "user", "content": output}],
235
- "context": new_context,
236
- }
237
-
238
- agent = StateGraph(state_schema)
239
- agent.add_node(call_model, destinations=(END, "sandbox"))
240
- agent.add_node(sandbox)
241
- agent.add_edge(START, "call_model")
242
- agent.add_edge("sandbox", "call_model")
243
- return agent
@@ -1,27 +0,0 @@
1
- import builtins
2
- import contextlib
3
- import io
4
- from typing import Any
5
-
6
-
7
- def eval_unsafe(code: str, _locals: dict[str, Any]) -> tuple[str, dict[str, Any]]:
8
- # Store original keys before execution
9
- original_keys = set(_locals.keys())
10
- result = f"Executing code...\n{code}\n\nOutput:\n"
11
- result += "=" * 50 + "\n"
12
- try:
13
- with contextlib.redirect_stdout(io.StringIO()) as f:
14
- # Execute the code in the provided locals context
15
- # Using exec to allow dynamic code execution
16
- # This is a simplified version; in production, consider security implications
17
- exec(code, builtins.__dict__, _locals)
18
- result += f.getvalue()
19
- if not result:
20
- result = "<code ran, no output printed to stdout>"
21
- except Exception as e:
22
- result += f"Error during execution: {repr(e)}"
23
-
24
- # Determine new variables created during execution
25
- new_keys = set(_locals.keys()) - original_keys
26
- new_vars = {key: _locals[key] for key in new_keys}
27
- return result, new_vars
@@ -1,15 +0,0 @@
1
- from universal_mcp.agentr import Agentr
2
- from universal_mcp.agents.codeact import create_codeact
3
- from universal_mcp.agents.codeact.sandbox import eval_unsafe
4
- from universal_mcp.agents.llm import load_chat_model
5
- from universal_mcp.tools.adapters import ToolFormat
6
-
7
- model = load_chat_model("gpt-4.1")
8
-
9
- agentr = Agentr()
10
- agentr.load_tools(["google-mail_send_email"])
11
-
12
- tools = agentr.list_tools(format=ToolFormat.NATIVE)
13
-
14
- code_act = create_codeact(model, tools, eval_unsafe)
15
- agent = code_act.compile()
@@ -1,61 +0,0 @@
1
- import re
2
-
3
- BACKTICK_PATTERN = r"(?:^|\n)```(.*?)(?:```(?:\n|$))"
4
-
5
-
6
- def extract_and_combine_codeblocks(text: str) -> str:
7
- """
8
- Extracts all codeblocks from a text string and combines them into a single code string.
9
-
10
- Args:
11
- text: A string containing zero or more codeblocks, where each codeblock is
12
- surrounded by triple backticks (```).
13
-
14
- Returns:
15
- A string containing the combined code from all codeblocks, with each codeblock
16
- separated by a newline.
17
-
18
- Example:
19
- text = '''Here's some code:
20
-
21
- ```python
22
- print('hello')
23
- ```
24
- And more:
25
-
26
- ```
27
- print('world')
28
- ```'''
29
-
30
- result = extract_and_combine_codeblocks(text)
31
-
32
- Result:
33
-
34
- print('hello')
35
-
36
- print('world')
37
- """
38
- # Find all code blocks in the text using regex
39
- # Pattern matches anything between triple backticks, with or without a language identifier
40
- code_blocks = re.findall(BACKTICK_PATTERN, text, re.DOTALL)
41
-
42
- if not code_blocks:
43
- return ""
44
-
45
- # Process each codeblock
46
- processed_blocks = []
47
- for block in code_blocks:
48
- # Strip leading and trailing whitespace
49
- block = block.strip()
50
-
51
- # If the first line looks like a language identifier, remove it
52
- lines = block.split("\n")
53
- if lines and (not lines[0].strip() or " " not in lines[0].strip()):
54
- # First line is empty or likely a language identifier (no spaces)
55
- block = "\n".join(lines[1:])
56
-
57
- processed_blocks.append(block)
58
-
59
- # Combine all codeblocks with newlines between them
60
- combined_code = "\n\n".join(processed_blocks)
61
- return combined_code