Skip to content

Commit 55db6f4

Browse files
authored
Merge pull request #63 from Accenture/feat/agent-tools
feat(agent-tools): the built-in agent tools - files, the web and a clock (RFC-0013 step 1)
2 parents e294d86 + a4cade8 commit 55db6f4

17 files changed

Lines changed: 3375 additions & 7 deletions

‎CHANGELOG.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,33 @@
3333
lockstep. **Upgrade note:** none without `llm.transcript.audit`. A record saved by the previous helper (version 1)
3434
continues under the new one; its turns before the upgrade carry no facts, and the check logs them as unaudited.
3535

36+
2. **The built-in agent tools: files inside one workspace, the web inside an allowlist, and a clock (RFC-0013 in
37+
mercury-composable, build plan step 1).** `extensions/agent-tools/agent_tools.py` serves seven composable functions
38+
an agent loop names in its tool catalog: `file.read`, `file.write` (the one writer, `write: true`, `create` the
39+
default and refusing an existing file), `file.list` and `file.search` inside `tools.file.root` - every path
40+
relative to the root, resolved with symbolic links followed and refused outside it, hidden entries invisible unless
41+
`tools.file.hidden=true`; `web.fetch` and `web.crawl` inside `tools.web.allow` - https unless a host is listed with
42+
`http://`, the host resolved and checked against the loopback, private, link-local and multicast ranges before the
43+
request is made to the checked address, every redirect checked again, the body cut at `tools.web.max.bytes`, HTML
44+
reduced to readable text or Markdown by a small in-house parser, a crawl same-host, breadth-first, `robots.txt`
45+
honoured, bounded by pages, depth, an include prefix and a delay; and `time.now`. A tool whose sandbox is not
46+
configured does not register, so an unconfigured tool is a 404 to the loop's pre-flight check; every refusal is a
47+
400 naming the rule and the key, which the loop returns to the model as the call's error. Each tool ships its
48+
catalog row - the name the model sees, the description, the closed input schema, the route and the write mark - as a
49+
constant, as a file under `extensions/agent-tools/catalog/` (shared byte for byte with the Node.js twin) and through
50+
`python agent_tools.py describe`, which also says what registers under the current configuration. `tools.health`
51+
reports the registered routes, the workspace and the allowed sites. No new dependency: aiohttp fetches, the standard
52+
library parses. Pinned by `tests/test_agent_tools.py` (97 tests: a workspace with an escaping link, a fixture site
53+
served in-process with redirects to private and foreign hosts, the allowlist and address rules without a network).
54+
The knowledge-graph tools and `file.extract` follow as step 2, the Node.js twin as step 3.
55+
56+
### Fixed
57+
58+
1. **A direct `python extensions/mcp-binding/mcp_binding.py` now connects its servers.** The binding attached its boot
59+
and shutdown hooks to the platform only when loaded by `mercury-serve`; run directly, the Event API started with no
60+
server connected and every bound route answered 503. Both entry points attach the hooks now, so a bad server file
61+
stops the direct run by name as it stops the served one.
62+
3663
### Changed
3764

3865
1. **A `-D` override value is resolved with the same `${ENV_VAR:default}` substitution as a file value - the engines'

‎README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ allowlisted remote tool, over stdio or streamable HTTP, on the official MCP SDK.
227227

228228
The LLM helper in `extensions/llm-helper/` is an application on this package, not part of it.
229229
Its `llm.chat` is configurable: a stateless chat, or one turn of a multi-turn tool-use loop whose
230-
transcript lives in the store the helper is configured with.
230+
transcript lives in the store the helper is configured with. The built-in agent tools in
231+
`extensions/agent-tools/` are the essential tools such a loop names in its catalog - files inside one
232+
workspace, the web inside an allowlist of sites, a clock - each a composable function whose sandbox is
233+
deployment configuration, never an argument of a call.
231234

232235
## Development
233236

‎extensions/agent-tools/README.md‎

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
# The built-in agent tools
2+
3+
The essential tools every second agent use case needs - **files** inside one workspace, **the web** inside an allowlist
4+
of sites, and **a clock** - as composable functions of a pack extension (RFC-0013 in the mercury-composable repository,
5+
`draft-design-specs/agent-essential-tools.md`; the knowledge-graph tools and `file.extract` follow in the next step). A
6+
graph's agent loop (`llm.run` in the engines) names a route of this application in its tool catalog, the engine reaches
7+
the route through declarative Event-over-HTTP like any other function, and the model's arguments arrive as the event
8+
body. The loop, the catalog and the graph see nothing special about a built-in tool: **a route names the capability**
9+
(`file.read`, `web.fetch`), never the implementation, so a built-in may later be replaced by an MCP tool behind the
10+
same route, or the other way round.
11+
12+
Three principles shape the extension:
13+
14+
- **The sandbox is deployment configuration, never an argument of a call.** Which directory tree the file tools may
15+
touch and which sites the web tools may reach are keys of this application's configuration, certified with the
16+
deployment like the MCP binding's server file. **A tool whose sandbox is not configured does not register**, so an
17+
unconfigured tool is a 404 to the loop's pre-flight check, not an open door.
18+
- **Every refusal is a 400 that names the rule.** The loop returns it to the model as that call's error, the model
19+
reads it and chooses another step, and the node never fails because of a tool.
20+
- **Results are text, capped, and untrusted.** A tool returns text or a small map, never a binary; it caps its own
21+
result; and what it returns is data a model reads, never an instruction the loop obeys. A page that says "ignore
22+
your instructions" is a page.
23+
24+
The Node.js twin (mercury-nodejs `extensions/agent-tools`) serves the same routes with the same catalog rows.
25+
26+
## Run
27+
28+
```bash
29+
pip install mercury-composable # no extra: files, web and clock add no dependency
30+
mercury-serve extensions/agent-tools/agent_tools.py -Dtools.file.root=/srv/workspace -Dtools.web.allow=docs.example.com
31+
python extensions/agent-tools/agent_tools.py # the same without mercury-serve
32+
```
33+
34+
The extension reads `resources/application.yml` next to `agent_tools.py`, whichever directory the command runs from;
35+
`-Dkey=value` overrides any key, and a value may carry `${ENV_VAR:default}` references. It listens on port 8089 by
36+
default (`rest.server.port`). Its boot log says what registered:
37+
38+
```text
39+
File tools registered - workspace /srv/workspace
40+
Web tools registered - 1 allowed site pattern(s)
41+
Built-in tools ready - file.read, file.write, file.list, file.search, web.fetch, web.crawl, time.now
42+
```
43+
44+
Without `tools.file.root` the file tools stay out (`File tools not registered - tools.file.root is not set`); without
45+
`tools.web.allow` the web tools stay out; `time.now` registers unless `tools.time=false`. A root that is not a
46+
directory, an allowlist entry that is not a host pattern or a limit that is not a number stops the application by name.
47+
48+
## The tools
49+
50+
### Files, inside one workspace
51+
52+
The workspace is `tools.file.root`, one directory tree. Every `path` is relative to it, resolved with symbolic links
53+
followed, and refused when the result is outside the root (`..`, an absolute path, a link that escapes). Hidden entries
54+
(a name starting with `.`) are invisible to every file tool unless `tools.file.hidden=true`: a `.env` or a `.git`
55+
folder inside the workspace stays out of a model's reach by default. Run the process as a user with no rights beyond
56+
the tree.
57+
58+
| Route | Row | Arguments | Result |
59+
| --- | --- | --- | --- |
60+
| `file.read` | `file_read` | `path` (required); `max_bytes` (default and cap `tools.file.max.bytes`, 256 KiB) | `{path, bytes, text, truncated}` - `bytes` is the file's size, `truncated` says the text was cut at `max_bytes`; UTF-8 text only, a binary file is a 400 naming the detected type (a PDF, a PNG image, a ZIP archive or an Office document, …) |
61+
| `file.write` | `file_write`, **`write: true`** | `path`, `content` (required); `mode`: `create` (default), `overwrite`, `append` | `{path, bytes, mode}`; `create` refuses an existing file with a 409; parent folders are created inside the root; the content is bounded by `tools.file.max.bytes`; `tools.file.write.extensions` may restrict the suffixes |
62+
| `file.list` | `file_list` | `path` (a folder, default `.`); `glob` (optional, `*.md` or `**/*.md`); `max_entries` (default 200, cap 2000) | `{entries: [{path, type, bytes, modified}], truncated}`, sorted by path; `type` is `file`, `directory` or `other`; `modified` in UTC |
63+
| `file.search` | `file_search` | `pattern` (required; a literal, or a regular expression with `regex: true`); `path` (default `.`); `glob` (default `**/*`); `case_sensitive` (default false); `context` (default 0, cap 3); `max_results` (default 100, cap 1000) | `{matches: [{path, line, text, before, after}], files_searched, truncated}`; text files only, binaries skipped, each line cut to `tools.file.line.max.chars` (500) |
64+
65+
`file.write` carries the write mark, so under the node's default mode a person approves it; the other three are reads
66+
and run unasked.
67+
68+
### The web, inside an allowlist
69+
70+
The web tools register only when `tools.web.allow` names at least one host pattern. A URL is fetched only when its host
71+
matches, over `https` (an `http` host must be listed with its scheme), and **every redirect is checked against the same
72+
list**. The host is resolved before the request, every address it resolves to is checked, and the request is made to
73+
the address that was checked, so a second resolution cannot point elsewhere. Addresses in loopback, private,
74+
link-local, multicast, reserved and unspecified ranges are refused unless `tools.web.allow.private=true`, which closes
75+
the request-forgery route to a cloud metadata service or an internal admin port. The download stops at
76+
`tools.web.max.bytes` (1 MiB), the request at `tools.web.timeout.ms` (10 s), cookies are never kept, and the tool
77+
identifies itself as `Mercury-Composable-AgentTools/<version>`.
78+
79+
| Route | Row | Arguments | Result |
80+
| --- | --- | --- | --- |
81+
| `web.fetch` | `web_fetch` | `url` (required); `as`: `text` (default), `markdown`, `raw` | `{url, status, content_type, title, text, bytes, truncated}`; HTML is reduced to its readable text (scripts, styles, `nav`, `header` and `footer` dropped; headings, lists, code and links kept as text or as Markdown); a text resource (`text/*`, JSON, XML) is returned as is; `raw` returns the body as received; a binary response is a 400; the page's own status (a 404, a 500) is a result, not an error |
82+
| `web.crawl` | `web_crawl` | `url` (the start page); `max_pages` (default 10, cap `tools.web.crawl.max.pages`, 50); `max_depth` (default 2, cap 5); `include` (a path prefix the followed links must start with) | `{pages: [{url, title, text, bytes}], visited, truncated}`; same host only, links followed breadth-first, `robots.txt` honoured, `tools.web.crawl.delay.ms` (500) between requests, each page's text cut to `tools.web.page.max.chars` (20 000), the whole result to 60 000 characters |
83+
84+
The crawl is a bounded reader of a documentation section or a small site, not a spider: the page cap, the depth, the
85+
include prefix and the delay are all limits, and a model that needs more asks again with a narrower start. HTML is read
86+
by a small in-house parser (no dependency), enough for documentation and articles; an installation whose pages need
87+
main-content extraction may put a library behind the same route.
88+
89+
### The clock
90+
91+
`time.now` (`time_now`) takes an optional `timezone` (an IANA name, default `UTC`) and returns
92+
`{utc, local, timezone, offset, weekday, epoch_ms}`. It reads nothing and has no sandbox to miss. A system without a
93+
time zone database (some containers) needs the `tzdata` package for any zone but UTC.
94+
95+
## Configuration
96+
97+
| Key | Default | Meaning |
98+
| --- | --- | --- |
99+
| `tools.file.root` | unset | the workspace; unset, the file tools do not register |
100+
| `tools.file.max.bytes` | `262144` | the most bytes `file.read` returns and `file.write` accepts |
101+
| `tools.file.hidden` | `false` | expose hidden entries (a name starting with `.`) to the file tools |
102+
| `tools.file.write.extensions` | any | the suffixes `file.write` accepts, comma-separated (`.md, .txt`) |
103+
| `tools.file.line.max.chars` | `500` | the longest line `file.search` returns |
104+
| `tools.web.allow` | empty | the host patterns the web tools may reach, comma-separated: `docs.example.com`, `*.example.org`, `http://intranet.example`, `host:8443`; empty, the web tools do not register |
105+
| `tools.web.allow.private` | `false` | allow addresses in loopback, private, link-local and multicast ranges |
106+
| `tools.web.max.bytes` | `1048576` | the most bytes read from one response |
107+
| `tools.web.timeout.ms` | `10000` | how long one request may take |
108+
| `tools.web.crawl.max.pages` | `50` | the most pages one crawl may ask for; the tool's cap, a deployment may lower it |
109+
| `tools.web.crawl.delay.ms` | `500` | the pause between two requests of a crawl |
110+
| `tools.web.page.max.chars` | `20000` | the characters kept of a page's text |
111+
| `tools.time` | `true` | register `time.now` |
112+
113+
Limits are defaults a deployment lowers, never raises past the tool's caps (a `tools.web.crawl.max.pages` above 50 is
114+
logged and capped). The shipped `application.yml` takes the root and the allowlist from `${TOOLS_FILE_ROOT:}` and
115+
`${TOOLS_WEB_ALLOW:}`, so a deployment sets two environment variables or two `-D` flags.
116+
117+
## The function contract
118+
119+
| | |
120+
| --- | --- |
121+
| Request | The event body is the tool's arguments, a map of the row's properties. Anything else, an unknown argument or a wrong type is a 400 naming it. |
122+
| Reply | The result map of the table above. |
123+
| 400 | A refusal by rule: a path outside the workspace, a hidden entry, a binary file, a site that is not allowed, a private address, a bad argument, too many redirects, a response that is not text. The message names the rule and the key that governs it. |
124+
| 404 | The file or folder does not exist. |
125+
| 408 | The site did not answer within `tools.web.timeout.ms`. |
126+
| 409 | `file.write` in `create` mode found the file. |
127+
| 502 | The host cannot be resolved or the connection failed. |
128+
129+
A Java or Rust engine maps the routes in its `yaml.event.over.http` file to this application's `/api/event`, and a
130+
graph's catalog row names the route:
131+
132+
```yaml
133+
event:
134+
http:
135+
- route: 'file.read, file.write, file.list, file.search, web.fetch, web.crawl, time.now'
136+
target: 'http://127.0.0.1:8089/api/event'
137+
```
138+
139+
**Logs.** A tool logs its route, the status, the byte, entry or page count and the host of a URL - never a path's
140+
content, a page's text or a full URL (a query string may carry a token). The engines' trace names the route like any
141+
function's.
142+
143+
## The catalog rows
144+
145+
Each tool ships the row an author copies onto the graph: the name the model sees, the description it reads, the input
146+
schema the loop validates against (a closed object - an unknown argument is refused), the route and the write mark. No
147+
row sets `strict`; the graph's author adds it when the model in use constrains optional properties by grammar.
148+
149+
```bash
150+
python extensions/agent-tools/agent_tools.py describe # every row
151+
python extensions/agent-tools/agent_tools.py describe file_write time.now
152+
```
153+
154+
```text
155+
# 7 built-in tool(s) - the catalog rows a graph copies, and what registers here
156+
157+
## file.read (file_read) - registers (workspace /srv/workspace)
158+
{
159+
"name": "file_read",
160+
"description": "Read a UTF-8 text file from the agent's workspace. ...",
161+
"input_schema": { "type": "object", "properties": { ... }, "required": ["path"], "additionalProperties": false },
162+
"route": "file.read"
163+
}
164+
```
165+
166+
The same rows are kept as files under [`catalog/`](./catalog) (`file_read.json`, …) for a copy without running
167+
anything; a test pins each file to the row the module publishes, byte for byte, and the Node.js twin carries the same
168+
files. A catalog node on a graph takes them as its `tools` list:
169+
170+
```json
171+
[ { "name": "file_read", "route": "file.read", "description": "...", "input_schema": { ... } },
172+
{ "name": "file_write", "route": "file.write", "write": true, "description": "...", "input_schema": { ... } } ]
173+
```
174+
175+
## Health
176+
177+
`tools.health` (the `/health` actuator, `mandatory.health.dependencies`) reports the registered routes, the workspace
178+
and the allowed site patterns - the answer to "what can this agent reach?" without reading code.
179+
180+
## Security
181+
182+
- **The catalog is still the boundary.** A graph names the tools its agent reaches; a built-in the graph does not name
183+
is as unreachable as any route, and a deployment whose sandbox is not configured fails the graph's pre-flight check
184+
before a token is spent.
185+
- **The root, the allowlist and the private-address gate are certification artifacts**, reviewed with the deployment.
186+
- **No shell, no arbitrary request.** A URL the model chooses is checked by host, scheme, port, resolved address and
187+
on every redirect; a path the model chooses is resolved and checked against the root. There is no generic
188+
`http.request` and no command execution, by design.
189+
- **Untrusted content.** A page or a file may carry instructions; the loop never obeys a result, and a graph's system
190+
prompt should say that fetched and stored content is data.
191+
192+
## Tests
193+
194+
```bash
195+
pip install -e '.[dev]'
196+
pytest tests/test_agent_tools.py
197+
```
198+
199+
The file tools run against a workspace built in a temporary directory (a link that escapes it, hidden entries, a
200+
binary, a file that is not UTF-8, a large file); the HTML reader against a fixture page; the allowlist and the
201+
address rules without a network; the web tools against a small site served in-process by aiohttp (pages that link each
202+
other, `robots.txt`, redirects to allowed, private and foreign hosts, a slow page, a large body, charsets); the boot,
203+
the health route, the `describe` output and the catalog files complete the suite.

0 commit comments

Comments
 (0)