Spaces:
Paused
Paused
BetterTools
Browse files- agent/__init__.py +3 -1
- agent/agent.py +8 -84
- agent/mcp.py +4 -0
- agent/tools.py +65 -0
- app.py +3 -5
agent/__init__.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from .agent import Agent
|
| 2 |
-
from .tools import Tool, tool, fetch_webpage, FETCH_WEBPAGE_TOOL, SHELL_TOOL
|
| 3 |
from .mcp import MCPClient, mcp_tool, load_mcp_tools, load_all_mcp_tools, MCP_SERVERS
|
| 4 |
from .shell import ShellManager, get_shell_manager
|
| 5 |
|
|
@@ -10,6 +10,8 @@ __all__ = [
|
|
| 10 |
"fetch_webpage",
|
| 11 |
"FETCH_WEBPAGE_TOOL",
|
| 12 |
"SHELL_TOOL",
|
|
|
|
|
|
|
| 13 |
"MCPClient",
|
| 14 |
"mcp_tool",
|
| 15 |
"load_mcp_tools",
|
|
|
|
| 1 |
from .agent import Agent
|
| 2 |
+
from .tools import Tool, tool, fetch_webpage, FETCH_WEBPAGE_TOOL, SHELL_TOOL, READ_TOOL, FINAL_MESSAGE_TOOL
|
| 3 |
from .mcp import MCPClient, mcp_tool, load_mcp_tools, load_all_mcp_tools, MCP_SERVERS
|
| 4 |
from .shell import ShellManager, get_shell_manager
|
| 5 |
|
|
|
|
| 10 |
"fetch_webpage",
|
| 11 |
"FETCH_WEBPAGE_TOOL",
|
| 12 |
"SHELL_TOOL",
|
| 13 |
+
"READ_TOOL",
|
| 14 |
+
"FINAL_MESSAGE_TOOL",
|
| 15 |
"MCPClient",
|
| 16 |
"mcp_tool",
|
| 17 |
"load_mcp_tools",
|
agent/agent.py
CHANGED
|
@@ -3,7 +3,7 @@ from typing import Any, Generator
|
|
| 3 |
|
| 4 |
from openai import OpenAI
|
| 5 |
|
| 6 |
-
from .tools import Tool
|
| 7 |
from .mcp import MCPClient, load_mcp_tools, load_all_mcp_tools
|
| 8 |
|
| 9 |
# ---------------------------------------------------------------------------
|
|
@@ -42,66 +42,13 @@ class Agent:
|
|
| 42 |
self._final_tool_name: str | None = None
|
| 43 |
self._max_iterations = max_iterations
|
| 44 |
self.system_prompt = system_prompt
|
| 45 |
-
self._tool_results: dict[str, str] = {} # tool_call_id → full result
|
| 46 |
|
| 47 |
# ------------------------------------------------------------------
|
| 48 |
# Tool registration
|
| 49 |
# ------------------------------------------------------------------
|
| 50 |
|
| 51 |
-
def register_tool(self,
|
| 52 |
-
self._tools.
|
| 53 |
-
|
| 54 |
-
def register_read_tool(self) -> None:
|
| 55 |
-
"""Register a tool to read truncated tool responses by line range."""
|
| 56 |
-
results_cache = self._tool_results
|
| 57 |
-
|
| 58 |
-
def _read(tool_call_id: str, start_line: int, num_lines: int = 50) -> str:
|
| 59 |
-
"""Read more lines from a truncated tool response.
|
| 60 |
-
|
| 61 |
-
tool_call_id (required): The tool_call_id from the truncated response
|
| 62 |
-
start_line (required): Line number to start reading from
|
| 63 |
-
num_lines: Number of lines to read (default 50)
|
| 64 |
-
"""
|
| 65 |
-
full = results_cache.get(tool_call_id)
|
| 66 |
-
if full is None:
|
| 67 |
-
return f"Error: No result found for tool_call_id '{tool_call_id}'"
|
| 68 |
-
lines = full.split("\n")
|
| 69 |
-
total = len(lines)
|
| 70 |
-
if start_line >= total:
|
| 71 |
-
return f"Error: start_line {start_line} >= total lines {total}"
|
| 72 |
-
end = min(start_line + num_lines, total)
|
| 73 |
-
chunk = "\n".join(lines[start_line:end])
|
| 74 |
-
remaining = total - end
|
| 75 |
-
header = f"Lines {start_line}-{end} of {total}"
|
| 76 |
-
if remaining > 0:
|
| 77 |
-
header += f" ({remaining} lines remaining)"
|
| 78 |
-
return f"{header}\n\n{chunk}"
|
| 79 |
-
|
| 80 |
-
self._tools.append(
|
| 81 |
-
Tool(
|
| 82 |
-
name="read_tool_response",
|
| 83 |
-
description="Read more lines from a truncated tool response. Use when a previous tool output was truncated.",
|
| 84 |
-
parameters={
|
| 85 |
-
"type": "object",
|
| 86 |
-
"properties": {
|
| 87 |
-
"tool_call_id": {
|
| 88 |
-
"type": "string",
|
| 89 |
-
"description": "The tool_call_id from the truncated response",
|
| 90 |
-
},
|
| 91 |
-
"start_line": {
|
| 92 |
-
"type": "integer",
|
| 93 |
-
"description": "Line number to start reading from (0-indexed)",
|
| 94 |
-
},
|
| 95 |
-
"num_lines": {
|
| 96 |
-
"type": "integer",
|
| 97 |
-
"description": "Number of lines to read (default 50)",
|
| 98 |
-
},
|
| 99 |
-
},
|
| 100 |
-
"required": ["tool_call_id", "start_line"],
|
| 101 |
-
},
|
| 102 |
-
handler=_read,
|
| 103 |
-
)
|
| 104 |
-
)
|
| 105 |
|
| 106 |
def register_mcp(self, url: str, headers: dict[str, str] | None = None) -> list[Tool]:
|
| 107 |
"""Connect to an MCP server and register all its tools.
|
|
@@ -122,32 +69,9 @@ class Agent:
|
|
| 122 |
self._tools.extend(server_tools)
|
| 123 |
return all_tools
|
| 124 |
|
| 125 |
-
def
|
| 126 |
-
"""
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
Until the model calls this tool the agent keeps looping — plain
|
| 130 |
-
text responses or other tool calls will not end the conversation.
|
| 131 |
-
|
| 132 |
-
The tool call is handled internally: no ``tool_call`` /
|
| 133 |
-
``tool_output`` events are yielded and the caller only sees a
|
| 134 |
-
``done`` event.
|
| 135 |
-
"""
|
| 136 |
-
self._final_tool_name = "final_message"
|
| 137 |
-
self._tools.append(
|
| 138 |
-
Tool(
|
| 139 |
-
name="final_message",
|
| 140 |
-
description=(
|
| 141 |
-
"Signal that you have completed your response and want "
|
| 142 |
-
"to end the conversation. Call this ONLY when you are "
|
| 143 |
-
"truly done. Until you call this tool, the conversation "
|
| 144 |
-
"will continue. Means you will multiple times answer the"
|
| 145 |
-
"same question or can get stuck in loops if you never call it."
|
| 146 |
-
),
|
| 147 |
-
parameters={"type": "object", "properties": {}, "required": []},
|
| 148 |
-
handler=lambda: "",
|
| 149 |
-
)
|
| 150 |
-
)
|
| 151 |
|
| 152 |
# ------------------------------------------------------------------
|
| 153 |
# Streaming loop
|
|
@@ -295,7 +219,7 @@ class Agent:
|
|
| 295 |
|
| 296 |
if tool_obj is None:
|
| 297 |
result_str = f"Error: Tool '{tname}' not found"
|
| 298 |
-
|
| 299 |
yield {
|
| 300 |
"type": "tool_output",
|
| 301 |
"name": tname,
|
|
@@ -366,7 +290,7 @@ class Agent:
|
|
| 366 |
result_str = f"Error executing {tname}: {e}"
|
| 367 |
|
| 368 |
# Store full result and truncate for message history
|
| 369 |
-
|
| 370 |
lines = result_str.split("\n")
|
| 371 |
if len(result_str) > 5_000:
|
| 372 |
char_count = 0
|
|
|
|
| 3 |
|
| 4 |
from openai import OpenAI
|
| 5 |
|
| 6 |
+
from .tools import Tool, _TOOL_RESULTS_CACHE
|
| 7 |
from .mcp import MCPClient, load_mcp_tools, load_all_mcp_tools
|
| 8 |
|
| 9 |
# ---------------------------------------------------------------------------
|
|
|
|
| 42 |
self._final_tool_name: str | None = None
|
| 43 |
self._max_iterations = max_iterations
|
| 44 |
self.system_prompt = system_prompt
|
|
|
|
| 45 |
|
| 46 |
# ------------------------------------------------------------------
|
| 47 |
# Tool registration
|
| 48 |
# ------------------------------------------------------------------
|
| 49 |
|
| 50 |
+
def register_tool(self, *tools: Tool) -> None:
|
| 51 |
+
self._tools.extend(tools)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def register_mcp(self, url: str, headers: dict[str, str] | None = None) -> list[Tool]:
|
| 54 |
"""Connect to an MCP server and register all its tools.
|
|
|
|
| 69 |
self._tools.extend(server_tools)
|
| 70 |
return all_tools
|
| 71 |
|
| 72 |
+
def set_final_message_tool(self, tool_name: str = "final_message") -> None:
|
| 73 |
+
"""Set the name of the final message tool for internal handling."""
|
| 74 |
+
self._final_tool_name = tool_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# ------------------------------------------------------------------
|
| 77 |
# Streaming loop
|
|
|
|
| 219 |
|
| 220 |
if tool_obj is None:
|
| 221 |
result_str = f"Error: Tool '{tname}' not found"
|
| 222 |
+
_TOOL_RESULTS_CACHE[tc_spec["id"]] = result_str
|
| 223 |
yield {
|
| 224 |
"type": "tool_output",
|
| 225 |
"name": tname,
|
|
|
|
| 290 |
result_str = f"Error executing {tname}: {e}"
|
| 291 |
|
| 292 |
# Store full result and truncate for message history
|
| 293 |
+
_TOOL_RESULTS_CACHE[tc_spec["id"]] = result_str
|
| 294 |
lines = result_str.split("\n")
|
| 295 |
if len(result_str) > 5_000:
|
| 296 |
char_count = 0
|
agent/mcp.py
CHANGED
|
@@ -234,6 +234,10 @@ MCP_SERVERS: dict[str, dict[str, Any]] = {
|
|
| 234 |
"url": "https://mcp.grep.app",
|
| 235 |
"description": "GitHub code search via grep.app",
|
| 236 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
}
|
| 238 |
|
| 239 |
|
|
|
|
| 234 |
"url": "https://mcp.grep.app",
|
| 235 |
"description": "GitHub code search via grep.app",
|
| 236 |
},
|
| 237 |
+
"github": {
|
| 238 |
+
"url": "https://gitmcp.io/docs",
|
| 239 |
+
"description": "GitHub code search and repository info",
|
| 240 |
+
},
|
| 241 |
}
|
| 242 |
|
| 243 |
|
agent/tools.py
CHANGED
|
@@ -278,3 +278,68 @@ SHELL_TOOL = Tool(
|
|
| 278 |
handler=_shell_handler,
|
| 279 |
streamable=False,
|
| 280 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
handler=_shell_handler,
|
| 279 |
streamable=False,
|
| 280 |
)
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
# Shared cache for read_tool_response - agent writes, tool reads
|
| 284 |
+
_TOOL_RESULTS_CACHE: dict[str, str] = {}
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def _read_tool_handler(tool_call_id: str, start_line: int, num_lines: int = 50) -> str:
|
| 288 |
+
"""Read more lines from a truncated tool response.
|
| 289 |
+
|
| 290 |
+
tool_call_id (required): The tool_call_id from the truncated response
|
| 291 |
+
start_line (required): Line number to start reading from
|
| 292 |
+
num_lines: Number of lines to read (default 50)
|
| 293 |
+
"""
|
| 294 |
+
full = _TOOL_RESULTS_CACHE.get(tool_call_id)
|
| 295 |
+
if full is None:
|
| 296 |
+
return f"Error: No result found for tool_call_id '{tool_call_id}'"
|
| 297 |
+
lines = full.split("\n")
|
| 298 |
+
total = len(lines)
|
| 299 |
+
if start_line >= total:
|
| 300 |
+
return f"Error: start_line {start_line} >= total lines {total}"
|
| 301 |
+
end = min(start_line + num_lines, total)
|
| 302 |
+
chunk = "\n".join(lines[start_line:end])
|
| 303 |
+
remaining = total - end
|
| 304 |
+
header = f"Lines {start_line}-{end} of {total}"
|
| 305 |
+
if remaining > 0:
|
| 306 |
+
header += f" ({remaining} lines remaining)"
|
| 307 |
+
return f"{header}\n\n{chunk}"
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
READ_TOOL = Tool(
|
| 311 |
+
name="read_tool_response",
|
| 312 |
+
description="Read more lines from a truncated tool response. Use when a previous tool output was truncated.",
|
| 313 |
+
parameters={
|
| 314 |
+
"type": "object",
|
| 315 |
+
"properties": {
|
| 316 |
+
"tool_call_id": {
|
| 317 |
+
"type": "string",
|
| 318 |
+
"description": "The tool_call_id from the truncated response",
|
| 319 |
+
},
|
| 320 |
+
"start_line": {
|
| 321 |
+
"type": "integer",
|
| 322 |
+
"description": "Line number to start reading from (0-indexed)",
|
| 323 |
+
},
|
| 324 |
+
"num_lines": {
|
| 325 |
+
"type": "integer",
|
| 326 |
+
"description": "Number of lines to read (default 50)",
|
| 327 |
+
},
|
| 328 |
+
},
|
| 329 |
+
"required": ["tool_call_id", "start_line"],
|
| 330 |
+
},
|
| 331 |
+
handler=_read_tool_handler,
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
FINAL_MESSAGE_TOOL = Tool(
|
| 335 |
+
name="final_message",
|
| 336 |
+
description=(
|
| 337 |
+
"Signal that you have completed your response and want "
|
| 338 |
+
"to end the conversation. Call this ONLY when you are "
|
| 339 |
+
"truly done. Until you call this tool, the conversation "
|
| 340 |
+
"will continue. Means you will multiple times answer the"
|
| 341 |
+
"same question or can get stuck in loops if you never call it."
|
| 342 |
+
),
|
| 343 |
+
parameters={"type": "object", "properties": {}, "required": []},
|
| 344 |
+
handler=lambda: "",
|
| 345 |
+
)
|
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import time
|
|
| 2 |
import uuid
|
| 3 |
import gradio as gr
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
-
from agent import Agent, FETCH_WEBPAGE_TOOL, SHELL_TOOL
|
| 6 |
import os
|
| 7 |
from pathlib import Path
|
| 8 |
|
|
@@ -30,11 +30,9 @@ agent = Agent(
|
|
| 30 |
model=os.getenv("OPENAI_MODEL"),
|
| 31 |
system_prompt=_SYSTEM_PROMPT,
|
| 32 |
)
|
| 33 |
-
agent.register_tool(FETCH_WEBPAGE_TOOL)
|
| 34 |
-
agent.register_tool(SHELL_TOOL)
|
| 35 |
agent.register_all_mcp()
|
| 36 |
-
agent.
|
| 37 |
-
agent.register_final_message_tool()
|
| 38 |
|
| 39 |
# Load JS from external files
|
| 40 |
_js_dir = Path(__file__).parent / "static" / "js"
|
|
|
|
| 2 |
import uuid
|
| 3 |
import gradio as gr
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
from agent import Agent, FETCH_WEBPAGE_TOOL, SHELL_TOOL, READ_TOOL, FINAL_MESSAGE_TOOL
|
| 6 |
import os
|
| 7 |
from pathlib import Path
|
| 8 |
|
|
|
|
| 30 |
model=os.getenv("OPENAI_MODEL"),
|
| 31 |
system_prompt=_SYSTEM_PROMPT,
|
| 32 |
)
|
| 33 |
+
agent.register_tool(FETCH_WEBPAGE_TOOL, SHELL_TOOL, READ_TOOL, FINAL_MESSAGE_TOOL)
|
|
|
|
| 34 |
agent.register_all_mcp()
|
| 35 |
+
agent.set_final_message_tool()
|
|
|
|
| 36 |
|
| 37 |
# Load JS from external files
|
| 38 |
_js_dir = Path(__file__).parent / "static" / "js"
|