universal-mcp-agents 0.1.18__py3-none-any.whl → 0.1.19__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,9 @@ from universal_mcp.agents.react import ReactAgent
9
9
  from universal_mcp.agents.simple import SimpleAgent
10
10
 
11
11
 
12
- def get_agent(agent_name: Literal["react", "simple", "builder", "bigtool", "codeact-script", "codeact-repl"]):
12
+ def get_agent(
13
+ agent_name: Literal["react", "simple", "builder", "bigtool", "codeact-script", "codeact-repl"],
14
+ ):
13
15
  if agent_name == "react":
14
16
  return ReactAgent
15
17
  elif agent_name == "simple":
@@ -49,8 +49,11 @@ class BaseAgent:
49
49
  run_metadata.update(metadata)
50
50
 
51
51
  run_config = {
52
+ "recursion_limit": 25,
52
53
  "configurable": {"thread_id": thread_id},
53
54
  "metadata": run_metadata,
55
+ "run_id": thread_id,
56
+ "run_name": self.name,
54
57
  }
55
58
 
56
59
  async for event, meta in self._graph.astream(
@@ -28,9 +28,6 @@ def run(name: str = "react"):
28
28
  "model": "anthropic/claude-sonnet-4-20250514",
29
29
  "registry": AgentrRegistry(client=client),
30
30
  "memory": MemorySaver(),
31
- "tools": {
32
- "google_mail": ["send_email"],
33
- },
34
31
  }
35
32
  agent_cls = get_agent(name)
36
33
  agent = agent_cls(name=name, **params)
@@ -19,12 +19,6 @@ async def main():
19
19
  memory=memory,
20
20
  )
21
21
  print("Starting agent...")
22
- # await agent.ainit()
23
- # await agent.run_interactive()
24
- # async for event in agent.stream(
25
- # user_input="Fetch unsubscribe links from my Gmail inbox for promo emails I have received in the last 7 days"
26
- # ):
27
- # print(event.content, end="")
28
22
  result = await agent.invoke(
29
23
  user_input="Fetch unsubscribe links from my Gmail inbox for promo emails I have received in the last 7 days"
30
24
  )
@@ -27,7 +27,7 @@ def smart_print(data: Any) -> None:
27
27
  Args:
28
28
  data: Either a dictionary with string keys, or a list of such dictionaries
29
29
  """
30
- print(light_copy(data)) # noqa
30
+ print(light_copy(data)) # noqa: T201
31
31
 
32
32
 
33
33
  def creative_writer(
@@ -275,105 +275,3 @@ def data_extractor(
275
275
  .invoke(prompt)
276
276
  )
277
277
  return cast(dict[str, Any], response)
278
-
279
-
280
- # news_articles_schema = {
281
- # "type": "object",
282
- # "properties": {
283
- # "articles": {
284
- # "type": "array",
285
- # "title": "Articles",
286
- # "description": "List of news articles",
287
- # "items": {
288
- # "type": "object",
289
- # "properties": {
290
- # "headline": {
291
- # "type": "string",
292
- # "title": "Headline",
293
- # "description": "The headline of the news article"
294
- # },
295
- # "url": {
296
- # "type": "string",
297
- # "title": "URL",
298
- # "description": "The URL of the news article"
299
- # }
300
- # },
301
- # "required": ["headline", "url"],
302
- # }
303
- # }
304
- # },
305
- # "required": ["articles"],
306
- # }
307
-
308
-
309
- # news_articles_schema = {
310
- # "title": "NewsArticleList",
311
- # "description": "A list of news articles with headlines and URLs",
312
- # "type": "object",
313
- # "properties": {
314
- # "articles": {
315
- # "type": "array",
316
- # "items": {
317
- # "type": "object",
318
- # "properties": {
319
- # "headline": {
320
- # "type": "string"
321
- # },
322
- # "url": {
323
- # "type": "string"
324
- # }
325
- # },
326
- # "required": ["headline", "url"]
327
- # }
328
- # }
329
- # },
330
- # "required": ["articles"]
331
- # }
332
- # model = init_chat_model(model="claude-4-sonnet-20250514", temperature=0)
333
- # structured_model = model.with_structured_output(news_articles_schema)
334
-
335
-
336
- # class TwitterComment(BaseModel):
337
- # skip: bool
338
- # reason: str
339
- # comment: str
340
-
341
- # twitter_comment_schema = {
342
- # "title": "TwitterComment",
343
- # "description": "A twitter comment to engage with followers",
344
- # "type": "object",
345
- # "properties": {
346
- # "skip": {
347
- # "type": "boolean"
348
- # },
349
- # "reason": {
350
- # "type": "string"
351
- # },
352
- # "comment": {
353
- # "type": "string"
354
- # },
355
- # "tagged_profiles": {
356
- # "type": "array",
357
- # "items": {
358
- # "type": "string"
359
- # }
360
- # }
361
- # },
362
- # "required": ["skip", "reason"]
363
- # }
364
-
365
- # comment = {
366
- # "tweet_id": "08109402",
367
- # "handle": "@iamnishant",
368
- # "text": "Hey really loved this tweet! Well said 💯"
369
- # }
370
-
371
- # comment_instructions = (
372
- # "Goal is to engage with my twitter followers who have commented on my tweets."
373
- # "Please generate a single line, context-aware, conversational reply for the given comment."
374
- # "- Use social media language (can use hinglish)."
375
- # "- Skip the reply, if the comment is too generic."
376
- # "- Also tag relevant people in the reply."
377
- # )
378
-
379
- # my_reply = call_llm(comment_instructions, comment, twitter_comment_schema)
@@ -81,7 +81,7 @@ class CodeActPlaybookAgent(BaseAgent):
81
81
  memory=memory,
82
82
  **kwargs,
83
83
  )
84
- self.model_instance = load_chat_model(model, thinking=True)
84
+ self.model_instance = load_chat_model(model)
85
85
  self.tools_config = tools or []
86
86
  self.registry = registry
87
87
  self.playbook_registry = playbook_registry
@@ -184,8 +184,13 @@ class CodeActPlaybookAgent(BaseAgent):
184
184
  ai_msg = f"Please login to the following app(s) using the following links and let me know in order to proceed:\n {links} "
185
185
  elif tool_call["name"] == "search_functions":
186
186
  tool_result = await meta_tools["search_functions"].ainvoke(tool_call["args"])
187
+ else:
188
+ raise Exception(
189
+ f"Unexpected tool call: {tool_call['name']}. "
190
+ "tool calls must be one of 'enter_playbook_mode', 'execute_ipython_cell', 'load_functions', or 'search_functions'"
191
+ )
187
192
  except Exception as e:
188
- tool_result = f"Error during {tool_call}: {e}"
193
+ tool_result = str(e)
189
194
 
190
195
  tool_message = ToolMessage(
191
196
  content=json.dumps(tool_result),
@@ -13,7 +13,10 @@ Your job is to answer the user's question or perform the task they ask for.
13
13
  - Answer simple questions (which do not require you to write any code or access any external resources) directly. Note that any operation that involves using ONLY print functions should be answered directly.
14
14
  - For task requiring operations or access to external resources, you should achieve the task by executing Python code snippets.
15
15
  - You have access to `execute_ipython_cell` tool that allows you to execute Python code in an IPython notebook cell.
16
- - You also have access to two tools for finding and loading more python functions- `search_functions` and `load_functions`, which you must use for finding functions for using different external applications. Prefer pre-loaded or functions already available when possible, and prioritize connected applications over unconnected ones. When this is not enough to break a tie between similar applications, ask the user.
16
+ - You also have access to two tools for finding and loading more python functions- `search_functions` and `load_functions`, which you must use for finding functions for using different external applications.
17
+ - Prefer pre-loaded or functions already available when possible.
18
+ - Prioritize connected applications over unconnected ones from the output of `search_functions`.
19
+ - When multiple apps are connected, or none of the apps are connected, ask the user to choose the application(s).
17
20
  - In writing or natural language processing tasks DO NOT answer directly. Instead use `execute_ipython_cell` tool with the AI functions provided to you for tasks like summarizing, text generation, classification, data extraction from text or unstructured data, etc. Avoid hardcoded approaches to classification, data extraction.
18
21
  - The code you write will be executed in a sandbox environment, and you can use the output of previous executions in your code. variables, functions, imports are retained.
19
22
  - Read and understand the output of the previous code snippet and use it to answer the user's request. Note that the code output is NOT visible to the user, so after the task is complete, you have to give the output to the user in a markdown format.
@@ -55,6 +55,9 @@ def create_meta_tools(tool_registry: ToolRegistry) -> dict[str, Any]:
55
55
 
56
56
  # Build result string efficiently
57
57
  result_parts = []
58
+ apps_in_results = set(app_tools.keys())
59
+ connected_apps_in_results = apps_in_results.intersection(connected_apps)
60
+
58
61
  for app, tools in app_tools.items():
59
62
  app_status = "connected" if app in connected_apps else "NOT connected"
60
63
  result_parts.append(f"Tools from {app} (status: {app_status} by user):")
@@ -63,6 +66,13 @@ def create_meta_tools(tool_registry: ToolRegistry) -> dict[str, Any]:
63
66
  result_parts.append(f" - {tool}")
64
67
  result_parts.append("") # Empty line between apps
65
68
 
69
+ # Add connection status information
70
+ if len(connected_apps_in_results) == 0 and len(apps_in_results) > 0:
71
+ result_parts.append("Connection Status: None of the apps in the results are connected. You must ask the user to choose the application.")
72
+ elif len(connected_apps_in_results) > 1:
73
+ connected_list = ", ".join(connected_apps_in_results)
74
+ result_parts.append(f"Connection Status: Multiple apps are connected ({connected_list}). You must ask the user to select which application they want to use.")
75
+
66
76
  result_parts.append("Call load_functions to select the required functions only.")
67
77
  return "\n".join(result_parts)
68
78
 
@@ -0,0 +1,90 @@
1
+ import contextlib
2
+ import inspect
3
+ import io
4
+ import queue
5
+ import re
6
+ import socket
7
+ import threading
8
+ import types
9
+ from typing import Any
10
+
11
+ from universal_mcp.agents.codeact0.utils import derive_context
12
+
13
+
14
+ class Sandbox:
15
+ """
16
+ A class to execute code safely in a sandboxed environment with a timeout.
17
+ """
18
+
19
+ def __init__(self, timeout: int = 180):
20
+ """
21
+ Initializes the Sandbox.
22
+ Args:
23
+ timeout: The timeout for code execution in seconds.
24
+ """
25
+ self.timeout = timeout
26
+ self._locals: dict[str, Any] = {}
27
+ self.add_context: dict[str, Any] = {}
28
+
29
+ def run(self, code: str) -> tuple[str, dict[str, Any], dict[str, Any]]:
30
+ """
31
+ Execute code safely with a timeout.
32
+ - Returns (output_str, filtered_locals_dict, new_add_context)
33
+ - Errors or timeout are returned as output_str.
34
+ - Previous variables in _locals persist across calls.
35
+ """
36
+
37
+ EXCLUDE_TYPES = (
38
+ types.ModuleType,
39
+ type(re.match("", "")),
40
+ type(threading.Lock()),
41
+ type(threading.RLock()),
42
+ threading.Event,
43
+ threading.Condition,
44
+ threading.Semaphore,
45
+ queue.Queue,
46
+ socket.socket,
47
+ io.IOBase,
48
+ )
49
+
50
+ result_container = {"output": "<no output>"}
51
+
52
+ def target():
53
+ try:
54
+ with contextlib.redirect_stdout(io.StringIO()) as f:
55
+ exec(code, self._locals, self._locals)
56
+ result_container["output"] = f.getvalue() or "<code ran, no output printed to stdout>"
57
+ except Exception as e:
58
+ result_container["output"] = "Error during execution: " + str(e)
59
+
60
+ thread = threading.Thread(target=target)
61
+ thread.start()
62
+ thread.join(self.timeout)
63
+
64
+ if thread.is_alive():
65
+ result_container["output"] = f"Code timeout: code execution exceeded {self.timeout} seconds."
66
+
67
+ # Filter locals for picklable/storable variables
68
+ all_vars = {}
69
+ for key, value in self._locals.items():
70
+ if key == "__builtins__":
71
+ continue
72
+ if inspect.iscoroutine(value) or inspect.iscoroutinefunction(value):
73
+ continue
74
+ if inspect.isasyncgen(value) or inspect.isasyncgenfunction(value):
75
+ continue
76
+ if isinstance(value, EXCLUDE_TYPES):
77
+ continue
78
+ if not callable(value) or not hasattr(value, "__name__"):
79
+ all_vars[key] = value
80
+
81
+ self._locals = all_vars
82
+
83
+ # Safely derive context
84
+ try:
85
+ self.add_context = derive_context(code, self.add_context)
86
+ except Exception:
87
+ # Keep the old context if derivation fails
88
+ pass
89
+
90
+ return result_container["output"], self._locals, self.add_context
File without changes
@@ -0,0 +1,160 @@
1
+ import fnmatch
2
+ import os
3
+ import pathlib
4
+ import re
5
+ import uuid
6
+
7
+ from loguru import logger
8
+ from universal_mcp.applications.application import BaseApplication
9
+
10
+
11
+ class FileSystemApp(BaseApplication):
12
+ """
13
+ A class to safely interact with the filesystem within a specified working directory.
14
+ """
15
+
16
+ def __init__(self, working_dir: str | None = None, **kwargs):
17
+ """
18
+ Initializes the FileSystemApp with a working directory.
19
+
20
+ Args:
21
+ working_dir: The absolute path to the directory where all operations will be performed.
22
+ """
23
+ super().__init__(name="Filesystem")
24
+
25
+ self.set_working_dir(working_dir or f"/tmp/{uuid.uuid4()}")
26
+
27
+ def set_working_dir(self, working_dir: str):
28
+ self.working_dir = pathlib.Path(working_dir).absolute()
29
+ # Create dir if not exists
30
+ self.working_dir.mkdir(parents=True, exist_ok=True)
31
+
32
+ def _is_safe_path(self, path: str) -> bool:
33
+ """
34
+ Checks if the given path is within the working directory.
35
+
36
+ Args:
37
+ path: The path to check.
38
+
39
+ Returns:
40
+ True if the path is safe, False otherwise.
41
+ """
42
+ common_path = os.path.commonpath([self.working_dir, path])
43
+ return common_path == str(self.working_dir)
44
+
45
+ def create_file(self, path: str, content: str = "") -> None:
46
+ """
47
+ Creates a file with the given content.
48
+
49
+ Args:
50
+ path: The relative path to the file to create.
51
+ content: The content to write to the file.
52
+
53
+ Raises:
54
+ ValueError: If the path is outside the working directory.
55
+ """
56
+ if not self._is_safe_path(path):
57
+ error = f"Path is outside the working directory: {path} vs {self.working_dir}"
58
+ logger.error(error)
59
+ raise ValueError(error)
60
+
61
+ full_path = os.path.join(self.working_dir, path)
62
+ os.makedirs(os.path.dirname(full_path), exist_ok=True)
63
+ with open(full_path, "w") as f:
64
+ f.write(content)
65
+
66
+ def read_file(self, path: str) -> str:
67
+ """
68
+ Reads the content of a file.
69
+
70
+ Args:
71
+ path: The relative path to the file to read.
72
+
73
+ Returns:
74
+ The content of the file.
75
+
76
+ Raises:
77
+ ValueError: If the path is outside the working directory.
78
+ FileNotFoundError: If the file does not exist.
79
+ """
80
+ if not self._is_safe_path(path):
81
+ raise ValueError("Path is outside the working directory.")
82
+
83
+ full_path = os.path.join(self.working_dir, path)
84
+ if not os.path.exists(full_path):
85
+ raise FileNotFoundError(f"File not found: {full_path}")
86
+
87
+ with open(full_path) as f:
88
+ return f.read()
89
+
90
+ def list_files(self, path: str = ".", recursive: bool = False) -> list[str]:
91
+ """
92
+ Lists files in a directory.
93
+
94
+ Args:
95
+ path: The relative path to the directory to list.
96
+ recursive: Whether to list files recursively.
97
+
98
+ Returns:
99
+ A list of file paths.
100
+
101
+ Raises:
102
+ ValueError: If the path is outside the working directory.
103
+ """
104
+ if not self._is_safe_path(path):
105
+ raise ValueError("Path is outside the working directory.")
106
+
107
+ full_path = os.path.join(self.working_dir, path)
108
+ if not os.path.isdir(full_path):
109
+ raise ValueError(f"Path '{path}' is not a directory.")
110
+
111
+ files = []
112
+ if recursive:
113
+ for root, _, filenames in os.walk(full_path):
114
+ for filename in filenames:
115
+ files.append(os.path.relpath(os.path.join(root, filename), self.working_dir))
116
+ else:
117
+ for item in os.listdir(full_path):
118
+ item_path = os.path.join(full_path, item)
119
+ if os.path.isfile(item_path):
120
+ files.append(os.path.relpath(item_path, self.working_dir))
121
+ return files
122
+
123
+ def grep(self, pattern: str, path: str = ".", file_pattern: str = "*") -> list[str]:
124
+ """
125
+ Searches for a pattern in files.
126
+
127
+ Args:
128
+ pattern: The regex pattern to search for.
129
+ path: The relative path to the directory to search in.
130
+ file_pattern: A glob pattern to filter files to search.
131
+
132
+ Returns:
133
+ A list of strings with "file:line_number:line" for each match.
134
+
135
+ Raises:
136
+ ValueError: If the path is outside the working directory.
137
+ """
138
+ if not self._is_safe_path(path):
139
+ raise ValueError("Path is outside the working directory.")
140
+
141
+ full_path = os.path.join(self.working_dir, path)
142
+ if not os.path.isdir(full_path):
143
+ raise ValueError(f"Path '{path}' is not a directory.")
144
+
145
+ matches = []
146
+ for root, _, filenames in os.walk(full_path):
147
+ for filename in fnmatch.filter(filenames, file_pattern):
148
+ file_path = os.path.join(root, filename)
149
+ try:
150
+ with open(file_path, errors="ignore") as f:
151
+ for i, line in enumerate(f, 1):
152
+ if re.search(pattern, line):
153
+ relative_path = os.path.relpath(file_path, self.working_dir)
154
+ matches.append(f"{relative_path}:{i}:{line.strip()}")
155
+ except OSError:
156
+ continue # Skip files that can't be opened
157
+ return matches
158
+
159
+ def list_tools(self):
160
+ return [self.create_file, self.grep, self.list_files, self.read_file]
@@ -1,3 +1,3 @@
1
- from .app import LLMApp
1
+ from .app import LlmApp
2
2
 
3
- __all__ = ["LLMApp"]
3
+ __all__ = ["LlmApp"]
@@ -2,10 +2,11 @@ import json
2
2
  from typing import Any, Literal, cast
3
3
 
4
4
  from langchain.chat_models import init_chat_model
5
- from langchain_openai import AzureChatOpenAI
6
5
  from pydantic import BaseModel, Field
7
6
  from universal_mcp.applications.application import BaseApplication
8
7
 
8
+ from universal_mcp.agents.llm import load_chat_model
9
+
9
10
  MAX_RETRIES = 3
10
11
 
11
12
 
@@ -28,7 +29,7 @@ def _get_context_as_string(source: Any | list[Any] | dict[str, Any]) -> str:
28
29
  return "\n".join(f"<{k}>\n{str(v)}\n</{k}>" for k, v in source.items())
29
30
 
30
31
 
31
- class LLMApp(BaseApplication):
32
+ class LlmApp(BaseApplication):
32
33
  """
33
34
  An application for leveraging Large Language Models (LLMs) for advanced text processing tasks.
34
35
  """
@@ -40,22 +41,34 @@ class LLMApp(BaseApplication):
40
41
  def generate_text(
41
42
  self,
42
43
  task: str,
43
- context: Any | list[Any] | dict[str, Any],
44
+ context: str | list[str] | dict[str, str] = "",
44
45
  tone: str = "normal",
45
46
  output_format: Literal["markdown", "html", "plain"] = "markdown",
46
47
  length: Literal["very-short", "concise", "normal", "long"] = "concise",
47
48
  ) -> str:
48
49
  """
49
- Generates well-written text for a high-level task using the provided context.
50
-
51
- Use this function for creative writing, summarization, and other text generation tasks.
50
+ Given a high-level writing task and context, returns a well-written text
51
+ that achieves the task, given the context.
52
+
53
+ Example Call:
54
+ generate_text("Summarize this website with the goal of making it easy to understand.", web_content)
55
+ generate_text("Make a markdown table summarizing the key differences between doc_1 and doc_2.", {"doc_1": str(doc_1), "doc_2": str(doc_2)})
56
+ generate_text("Summarize all the provided documents.", [doc_1, doc_2, doc_3])
57
+
58
+ Important:
59
+ - Include specifics of the goal in the context verbatim.
60
+ - Be precise and direct in the task, and include as much context as possible.
61
+ - Include relevant high-level goals or intent in the task.
62
+ - You can provide multiple documents as input, and reference them in the task.
63
+ - You MUST provide the contents of any source documents to `generate_text`.
64
+ - NEVER use `generate_text` to produce JSON for a Pydantic model.
52
65
 
53
66
  Args:
54
67
  task: The main writing task or directive.
55
- context: A single string, list of strings, or dictionary mapping labels to content.
56
- tone: The desired tone of the output (e.g., "formal", "casual", "technical").
68
+ context: A single string, list of strings, or dict mapping labels to content.
69
+ tone: The desired tone of the output (e.g., "normal", "flirty", "formal", "casual", "crisp", "poetic", "technical", "internet-chat", "smartass", etc.).
57
70
  output_format: The desired output format ('markdown', 'html', 'plain').
58
- length: The desired length of the output ('very-short', 'concise', 'normal', 'long').
71
+ length: Desired length of the output ('very-short', 'concise', 'normal', 'long').
59
72
 
60
73
  Returns:
61
74
  The generated text as a string.
@@ -78,31 +91,63 @@ class LLMApp(BaseApplication):
78
91
 
79
92
  full_prompt = f"{prompt}\n\nContext:\n{context_str}\n\n"
80
93
 
81
- model = AzureChatOpenAI(model="gpt-4o", temperature=0.7)
94
+ model = load_chat_model("azure/gpt-5-mini")
82
95
  response = model.with_retry(stop_after_attempt=MAX_RETRIES).invoke(full_prompt)
83
96
  return str(response.content)
84
97
 
85
98
  def classify_data(
86
99
  self,
87
- task: str,
100
+ classification_task_and_requirements: str,
88
101
  context: Any | list[Any] | dict[str, Any],
89
102
  class_descriptions: dict[str, str],
90
103
  ) -> dict[str, Any]:
91
104
  """
92
- Classifies data into one of several categories based on a given task and context.
105
+ Classifies and compares data based on given requirements.
106
+
107
+ Use `classify_data` for tasks which need to classify data into one of many categories.
108
+ If making multiple binary classifications, call `classify_data` for each.
109
+
110
+ Guidance:
111
+ - Prefer to use classify_data operations to compare strings, rather than string ops.
112
+ - Prefer to include an "Unsure" category for classification tasks.
113
+ - The `class_descriptions` dict argument MUST be a map from possible class names to a precise description.
114
+ - Use precise and specific class names and concise descriptions.
115
+ - Pass ALL relevant context, preferably as a dict mapping labels to content.
116
+ - Returned dict maps each possible class name to a probability.
117
+
118
+ Example Usage:
119
+ classification_task_and_requirements = "Does the document contain an address?"
120
+ class_descriptions = {
121
+ "Is_Address": "Valid addresses usually have street names, city, and zip codes.",
122
+ "Not_Address": "Not valid addresses."
123
+ }
124
+ classification = classify_data(
125
+ classification_task_and_requirements,
126
+ {"address": extracted_address},
127
+ class_descriptions
128
+ )
129
+ if classification["probabilities"]["Is_Address"] > 0.5:
130
+ ...
93
131
 
94
132
  Args:
95
- task: The classification question and any specific rules or requirements.
96
- context: The data to be classified, provided as a string, list, or dictionary.
97
- class_descriptions: A dictionary mapping class names to their descriptions.
133
+ classification_task_and_requirements: The classification question and rules.
134
+ context: The data to classify (string, list, or dict).
135
+ class_descriptions: Mapping from class names to descriptions.
136
+
137
+ Tags:
138
+ important
98
139
 
99
140
  Returns:
100
- A dictionary containing the classification probabilities, the reasoning, and the top class.
141
+ dict: {
142
+ "probabilities": dict[str, float],
143
+ "reason": str,
144
+ "top_class": str,
145
+ }
101
146
  """
102
147
  context_str = _get_context_as_string(context)
103
148
 
104
149
  prompt = (
105
- f"{task}\n\n"
150
+ f"{classification_task_and_requirements}\n\n"
106
151
  f"This is a classification task.\nPossible classes and descriptions:\n"
107
152
  f"{json.dumps(class_descriptions, indent=2)}\n\n"
108
153
  f"Context:\n{context_str}\n\n"
@@ -125,25 +170,61 @@ class LLMApp(BaseApplication):
125
170
 
126
171
  def extract_data(
127
172
  self,
128
- task: str,
173
+ extraction_task: str,
129
174
  source: Any | list[Any] | dict[str, Any],
130
175
  output_schema: dict[str, Any],
131
176
  ) -> dict[str, Any]:
132
177
  """
133
- Extracts structured data from unstructured text based on a provided JSON schema.
178
+ Extracts structured data from unstructured data (documents, webpages, images, large bodies of text),
179
+ returning a dictionary matching the given output_schema.
180
+
181
+ You MUST anticipate Exception raised for unextractable data; skip this item if applicable.
182
+
183
+ Strongly prefer to:
184
+ - Be comprehensive, specific, and precise on the data you want to extract.
185
+ - Use optional fields everywhere.
186
+ - Extract multiple items from each source unless otherwise specified.
187
+ - The more specific your extraction task and output_schema are, the better the results.
134
188
 
135
189
  Args:
136
- task: A description of the data to be extracted.
137
- source: The unstructured data to extract from (e.g., document, webpage content).
138
- output_schema: A valid JSON schema with a 'title' and 'description'.
190
+ extraction_task: The directive describing what to extract.
191
+ source: The unstructured data to extract from.
192
+ output_schema: must be a valid JSON schema with top-level 'title' and 'description' keys.
139
193
 
140
194
  Returns:
141
195
  A dictionary containing the extracted data, matching the provided schema.
196
+
197
+ Example:
198
+ news_articles_schema = {
199
+ "title": "NewsArticleList",
200
+ "description": "A list of news articles with headlines and URLs",
201
+ "type": "object",
202
+ "properties": {
203
+ "articles": {
204
+ "type": "array",
205
+ "items": {
206
+ "type": "object",
207
+ "properties": {
208
+ "headline": {
209
+ "type": "string"
210
+ },
211
+ "url": {
212
+ "type": "string"
213
+ }
214
+ },
215
+ "required": ["headline", "url"]
216
+ }
217
+ }
218
+ },
219
+ "required": ["articles"]
220
+ }
221
+
222
+ news_articles = extract_data("Extract headlines and their corresponding URLs.", content, news_articles_schema)
142
223
  """
143
224
  context_str = _get_context_as_string(source)
144
225
 
145
226
  prompt = (
146
- f"{task}\n\n"
227
+ f"{extraction_task}\n\n"
147
228
  f"Context:\n{context_str}\n\n"
148
229
  "Return ONLY a valid JSON object that conforms to the provided schema, with no extra text."
149
230
  )
@@ -156,3 +237,64 @@ class LLMApp(BaseApplication):
156
237
  .invoke(prompt)
157
238
  )
158
239
  return cast(dict[str, Any], response)
240
+
241
+ def call_llm(
242
+ self,
243
+ task_instructions: str,
244
+ context: Any | list[Any] | dict[str, Any],
245
+ output_schema: dict[str, Any],
246
+ ) -> dict[str, Any]:
247
+ """
248
+ Call a Large Language Model (LLM) with an instruction and contextual information,
249
+ returning a dictionary matching the given output_schema.
250
+ Can be used for tasks like creative writing, llm reasoning based content generation, etc.
251
+
252
+ You MUST anticipate Exceptions in reasoning based tasks which will lead to some empty fields
253
+ in the returned output; skip this item if applicable.
254
+
255
+ General Guidelines:
256
+ - Be comprehensive, specific, and precise on the task instructions.
257
+ - Include as much context as possible.
258
+ - You can provide multiple items in context, and reference them in the task.
259
+ - Include relevant high-level goals or intent in the task.
260
+ - In the output_schema, use required field wherever necessary.
261
+ - The more specific your task instructions and output_schema are, the better the results.
262
+
263
+ Guidelines for content generation tasks:
264
+ - Feel free to add instructions for tone, length, and format (markdown, html, plain-text, xml)
265
+ - Some examples of tone are: "normal", "flirty", "formal", "casual", "crisp", "poetic", "technical", "internet-chat", "smartass", etc.
266
+ - Prefer length to be concise by default. Other examples are: "very-short", "concise", "normal", "long", "2-3 lines", etc.
267
+ - In format prefer plain-text but you can also use markdown and html wherever useful.
268
+
269
+ Args:
270
+ task_instructions: The main directive for the LLM (e.g., "Summarize the article" or "Extract key entities").
271
+ context:
272
+ A dictionary containing named text elements that provide additional
273
+ information for the LLM. Keys are labels (e.g., 'article', 'transcript'),
274
+ values are strings of content.
275
+ output_schema: must be a valid JSON schema with top-level 'title' and 'description' keys.
276
+
277
+ Returns:
278
+ dict: Parsed JSON object matching the desired output_schema.
279
+
280
+ """
281
+ context_str = _get_context_as_string(context)
282
+
283
+ prompt = f"{task_instructions}\n\nContext:\n{context_str}\n\nReturn ONLY a valid JSON object, no extra text."
284
+
285
+ model = init_chat_model(model="claude-4-sonnet-20250514", temperature=0)
286
+
287
+ response = (
288
+ model.with_structured_output(schema=output_schema, method="json_mode")
289
+ .with_retry(stop_after_attempt=MAX_RETRIES)
290
+ .invoke(prompt)
291
+ )
292
+ return cast(dict[str, Any], response)
293
+
294
+ def list_tools(self):
295
+ return [
296
+ self.generate_text,
297
+ self.classify_data,
298
+ self.extract_data,
299
+ self.call_llm,
300
+ ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: universal-mcp-agents
3
- Version: 0.1.18
3
+ Version: 0.1.19
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
@@ -1,9 +1,10 @@
1
- universal_mcp/agents/__init__.py,sha256=5aE4zVgem6ehzfRrt5QqE6gLi7949vySZAn_uFuaU7Q,1252
2
- universal_mcp/agents/base.py,sha256=GmagwWFJdPpp_yLeAfsrr4fu-zzxh7CgIlBJbTgFgWM,7072
3
- universal_mcp/agents/cli.py,sha256=AG9e4iSX3GazT537573YrYT1wSaZYOr42rrYQ7xP3YA,1016
1
+ universal_mcp/agents/__init__.py,sha256=NuS_JOKYeHfAkNA4xRyvxdGZP_tXKfvsmoc2fhkMrj8,1259
2
+ universal_mcp/agents/base.py,sha256=hrlvSv0TLS-ShaKofpHD3kishciql7SSbTKCUHKGObc,7175
3
+ universal_mcp/agents/cli.py,sha256=bXdpgxsOMjclm1STHJgx10ocX9EebQ11DrxH0p6KMZk,943
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
7
+ universal_mcp/agents/sandbox.py,sha256=Int2O8JNFPlB8c7gb86KRxlNbuV0zdz5_NCo_GMcCds,2876
7
8
  universal_mcp/agents/simple.py,sha256=NSATg5TWzsRNS7V3LFiDG28WSOCIwCdcC1g7NRwg2nM,2095
8
9
  universal_mcp/agents/utils.py,sha256=P6W9k6XAOBp6tdjC2VTP4tE0B2M4-b1EDmr-ylJ47Pw,7765
9
10
  universal_mcp/agents/bigtool/__init__.py,sha256=mZG8dsaCVyKlm82otxtiTA225GIFLUCUUYPEIPF24uw,2299
@@ -28,23 +29,25 @@ universal_mcp/agents/codeact/sandbox.py,sha256=NjN6ISj8psFtHf8V0w24ChJdUMUWkq7Or
28
29
  universal_mcp/agents/codeact/state.py,sha256=WTPfpxDlGRnlr5tZuXMg_KU7GS7TZbnrIKslOvZLbQI,565
29
30
  universal_mcp/agents/codeact/utils.py,sha256=JUbT_HYGS_D1BzmzoVpORIe7SGur1KgJguTZ_1tZ4JY,1918
30
31
  universal_mcp/agents/codeact0/__init__.py,sha256=ebKkpgg-0UnsvDtagEJ2tMer1VsfhmEE5KJcFzUk9fU,133
31
- universal_mcp/agents/codeact0/__main__.py,sha256=V2wLWW9ym3rtiSvPEs-N0Mki7G5dYHzV5dAsAoF-ygQ,1148
32
+ universal_mcp/agents/codeact0/__main__.py,sha256=xeqNuawP9M8JVAnkhLesalnpI_TakC49ATJaSCzCsYs,880
32
33
  universal_mcp/agents/codeact0/agent.py,sha256=9BInAQr3csES-XHSscmeJlYJ3-wQUHPvLOf-6wFILUU,6695
33
34
  universal_mcp/agents/codeact0/config.py,sha256=H-1woj_nhSDwf15F63WYn723y4qlRefXzGxuH81uYF0,2215
34
35
  universal_mcp/agents/codeact0/langgraph_agent.py,sha256=ehjMV_Z1118pCFWB_Sa5H7XnUp0udsbUHjfjXjhIQM8,435
35
- universal_mcp/agents/codeact0/llm_tool.py,sha256=fydA4BbSnhG3OLMR_VEtJxTPTmRK_mMvXGsUCO-5Mig,13829
36
- universal_mcp/agents/codeact0/playbook_agent.py,sha256=JIyn4RZxFDJnXW7-jM16RLM1ylbVU02M8RBJuj1oBUs,17811
37
- universal_mcp/agents/codeact0/prompts.py,sha256=CF2X6zSK1lYQ9ef78cn0iN0oQN_tDe3T02Ecfn1o45U,8627
36
+ universal_mcp/agents/codeact0/llm_tool.py,sha256=q-hiqkKtjVmpyNceFoRgo7hvKh4HtQf_I1VudRUEPR0,11075
37
+ universal_mcp/agents/codeact0/playbook_agent.py,sha256=6ePcpEOrHxoNoaAdKdg9i7Yi6hcJOxBAo1MavCn8J6A,18081
38
+ universal_mcp/agents/codeact0/prompts.py,sha256=2MF0J371Ib7cDXqpW6Ei_CwBpRFmK95neLh9QM4emIY,8708
38
39
  universal_mcp/agents/codeact0/sandbox.py,sha256=zMgHrWnQYkSkJb2MzfXvT3euCc4hvqzBE_EbX2_iLxA,3142
39
40
  universal_mcp/agents/codeact0/state.py,sha256=Y-Rzn_S7--aXH18KPvyhqDqOOB-miu1lsAmLgmMlaAg,1259
40
- universal_mcp/agents/codeact0/tools.py,sha256=7hcFJxR26w_VCOWL8Oec8Ezfn3Auyv3YgeRv9f8j9xo,7642
41
+ universal_mcp/agents/codeact0/tools.py,sha256=qVZLq1YlVKABZdpEqFAzLo04DTFd1ZJi18atfzmxEb8,8374
41
42
  universal_mcp/agents/codeact0/utils.py,sha256=jAZItSd3KGDkY9PquSWRIFCj9N26K9Kt0HKQ_jwvvSQ,15944
42
43
  universal_mcp/agents/shared/__main__.py,sha256=XxH5qGDpgFWfq7fwQfgKULXGiUgeTp_YKfcxftuVZq8,1452
43
44
  universal_mcp/agents/shared/prompts.py,sha256=yjP3zbbuKi87qCj21qwTTicz8TqtkKgnyGSeEjMu3ho,3761
44
45
  universal_mcp/agents/shared/tool_node.py,sha256=DC9F-Ri28Pam0u3sXWNODVgmj9PtAEUb5qP1qOoGgfs,9169
45
- universal_mcp/applications/llm/__init__.py,sha256=xnpxq4Wl_pevvwtSUtEwcty8_d61ywO1V2YnEXyCREY,46
46
- universal_mcp/applications/llm/app.py,sha256=iNLU6z2LRZc01GfSKvV0vNzT1LhKAjq_UrSJEmjthjw,6032
46
+ universal_mcp/applications/filesystem/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
47
+ universal_mcp/applications/filesystem/app.py,sha256=0TRjjm8YnslVRSmfkXI7qQOAlqWlD1eEn8Jm0xBeigs,5561
48
+ universal_mcp/applications/llm/__init__.py,sha256=_XGRxN3O1--ZS5joAsPf8IlI9Qa6negsJrwJ5VJXno0,46
49
+ universal_mcp/applications/llm/app.py,sha256=oqX3byvlFRmeRo4jJJxUBGy-iTDGm2fplMEKA2pcMtw,12743
47
50
  universal_mcp/applications/ui/app.py,sha256=c7OkZsO2fRtndgAzAQbKu-1xXRuRp9Kjgml57YD2NR4,9459
48
- universal_mcp_agents-0.1.18.dist-info/METADATA,sha256=_OEOGCno6HWgMENbxmi7zUeeMdyMcZyLVUELcBS72Rs,878
49
- universal_mcp_agents-0.1.18.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
50
- universal_mcp_agents-0.1.18.dist-info/RECORD,,
51
+ universal_mcp_agents-0.1.19.dist-info/METADATA,sha256=imopF36ZWDrHP3tweT0GtvIlKdNg_CLa9yZrXvv3s9s,878
52
+ universal_mcp_agents-0.1.19.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
53
+ universal_mcp_agents-0.1.19.dist-info/RECORD,,