-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathagent_tools.py
More file actions
53 lines (42 loc) · 1.67 KB
/
Copy pathagent_tools.py
File metadata and controls
53 lines (42 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# SPDX-License-Identifier: Apache-2.0
"""Tool implementations for the mcp_agent example.
Plain stdlib-only functions in their own importable module. McpSandbox
imports this module by name inside each per-call jail, so it must not pull
in sandlock, openai, or any other heavy dependency. Module-level imports
are fine (and shown here): the worker imports the module normally.
"""
import contextlib
import io
import os
from urllib.request import urlopen
def read_file(path: str, *, workspace: str) -> str:
"""Read a file from the workspace."""
with open(os.path.join(workspace, path)) as f:
return f.read()
def write_file(path: str, content: str, *, workspace: str) -> str:
"""Write content to a file in the workspace."""
full = os.path.join(workspace, path)
parent = os.path.dirname(full)
if parent:
os.makedirs(parent, exist_ok=True)
with open(full, "w") as f:
f.write(content)
return f"Wrote {len(content)} bytes to {path}"
def run_python(code: str) -> str:
"""Execute Python code and return stdout."""
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
exec(code)
return buf.getvalue()
def list_files(*, workspace: str) -> str:
"""List files in the workspace."""
entries = sorted(os.listdir(workspace))
lines = []
for e in entries:
kind = "dir" if os.path.isdir(os.path.join(workspace, e)) else "file"
lines.append(f"{kind} {e}")
return "\n".join(lines) if lines else "(empty)"
def web_fetch(url: str) -> str:
"""Fetch a URL and return the response body (first 4KB)."""
resp = urlopen(url, timeout=10)
return resp.read(4096).decode("utf-8", errors="replace")