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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//! In-process extension: tools the embedding program supplies itself.
//!
//! 0.8.0 made the crate extensible *out of process* — point it at an MCP server
//! and its tools reach the model. That is the right boundary for a capability
//! that already lives elsewhere, and the wrong one for a capability that is
//! already linked into the same binary: a second process, a transport, and a
//! serialization hop to call a function that is one `await` away.
//!
//! 0.9.0 adds the in-process half. A caller implements [`Tool`], registers it
//! with [`TaskContract::with_tools`](crate::TaskContract::with_tools), and the
//! model is offered it beside `grep`, `find`, `read_file`, and `write_file`.
//!
//! # What registration is, and what it is not
//!
//! Registering a tool makes it *available*. It does not authorize it. Calling a
//! registered tool is an [`Act::Exec`](crate::Act::Exec) check on its name,
//! decided by the same deny-first policy stack that decides paths, binaries, and
//! hosts — so an operator can hand the agent a toolbox and still refuse one tool
//! in it, and the refusal lands in the trace attributed to the rule that made it.
//!
//! And the boundary stops there. A registered tool runs **in the harness's own
//! process, with the embedding program's privileges**. The policy governs whether
//! it is *called*; it does not govern what the tool does once running — no
//! sandbox, no path scoping, no egress control applies inside it. This is exactly
//! the bound 0.8.0 already states for a stdio MCP server, and for the same
//! reason: the harness decides what starts, not what a started thing then does.
//! A tool that shells out, writes outside the workspace, or dials a host has done
//! so with the caller's full authority.
use BTreeSet;
use fmt;
use Future;
use Pin;
use Arc;
use Value;
use crate;
use crateToolSpec;
/// What [`Tool::invoke`] returns.
///
/// A boxed future, because [`Tool`] has to be a trait object: the toolbox holds
/// a heterogeneous set of caller types behind one pointer. [`Provider`] gets to
/// stay generic over `impl Future` precisely because a run has exactly one of
/// them; a run has many tools.
///
/// The `'a` is the borrow of `&self`, which is the useful part: the future may
/// hold the tool's own state — a connection pool, an HTTP client, a handle to
/// the application it lives in — rather than cloning it on every call. Building
/// one is `Box::pin(async move { … })` and nothing else.
///
/// ```
/// use std::collections::HashMap;
/// use std::sync::Mutex;
///
/// use io_harness::tools::{Tool, ToolFuture};
/// use io_harness::ToolSpec;
/// # use serde_json::{json, Value};
///
/// /// Whatever the embedding program already holds — a connection pool, an
/// /// HTTP client, a cache. Here, a map behind a lock.
/// struct Customers {
/// rows: Mutex<HashMap<String, String>>,
/// }
///
/// impl Tool for Customers {
/// # fn spec(&self) -> ToolSpec {
/// # ToolSpec { name: "customer".into(), description: "Look a customer up by id.".into(),
/// # parameters: json!({"type": "object"}) }
/// # }
/// fn invoke<'a>(&'a self, arguments: &'a Value) -> ToolFuture<'a> {
/// Box::pin(async move {
/// // `self` is borrowed for the life of the future, so the tool
/// // reads the program's own state instead of being handed a
/// // clone of it on every call.
/// let id = arguments.get("id").and_then(Value::as_str).unwrap_or_default();
/// let found = self.rows.lock().unwrap().get(id).cloned();
/// // `Err` is not a run failure: the text becomes an observation
/// // the model can act on, and only it can decide whether a miss
/// // means "try another id" or "give up".
/// found.ok_or_else(|| io_harness::Error::Config(format!("no customer {id}")))
/// })
/// }
/// }
/// ```
///
/// [`Provider`]: crate::Provider
pub type ToolFuture<'a> = ;
/// An action the embedding program lets the agent take.
///
/// Implement it for anything the model should be able to do that the built-in
/// filesystem tools cannot: query your database, call your internal API, render
/// your template, ask your UI. The result is text, as an MCP tool result already
/// is — the model reads it, so it has to be readable.
///
/// ```
/// use io_harness::tools::{Tool, ToolFuture};
/// use io_harness::ToolSpec;
/// use serde_json::json;
///
/// struct Uppercase;
///
/// impl Tool for Uppercase {
/// fn spec(&self) -> ToolSpec {
/// ToolSpec {
/// name: "uppercase".into(),
/// description: "Upper-case the `text` argument.".into(),
/// parameters: json!({
/// "type": "object",
/// "properties": { "text": { "type": "string" } },
/// "required": ["text"]
/// }),
/// }
/// }
///
/// fn invoke<'a>(&'a self, arguments: &'a serde_json::Value) -> ToolFuture<'a> {
/// let text = arguments.get("text").and_then(|v| v.as_str()).unwrap_or("").to_uppercase();
/// Box::pin(async move { Ok(text) })
/// }
/// }
/// ```
///
/// Returning `Err` is not a run failure. The error text becomes an observation
/// the agent can adapt to, the same treatment `grep` gives a malformed regex —
/// only the model can decide whether a failed lookup means "try another id" or
/// "give up on this approach".
/// The set of [`Tool`]s registered for a run.
///
/// Collect them, hand the box to
/// [`TaskContract::with_tools`](crate::TaskContract::with_tools), and the model
/// is offered them beside `grep`, `find`, `read_file`, and `write_file`.
///
/// ```
/// use io_harness::tools::{Tool, ToolFuture, Toolbox};
/// use io_harness::{TaskContract, ToolSpec, Verification};
/// # use serde_json::{json, Value};
/// # struct Now;
/// # impl Tool for Now {
/// # fn spec(&self) -> ToolSpec {
/// # ToolSpec { name: "now".into(), description: "The current time, ISO 8601.".into(),
/// # parameters: json!({"type": "object"}) }
/// # }
/// # fn invoke<'a>(&'a self, _a: &'a Value) -> ToolFuture<'a> {
/// # Box::pin(async { Ok("2026-07-28T09:00:00Z".to_string()) })
/// # }
/// # }
/// # struct OpenTicket;
/// # impl Tool for OpenTicket {
/// # fn spec(&self) -> ToolSpec {
/// # ToolSpec { name: "open_ticket".into(), description: "File a ticket.".into(),
/// # parameters: json!({"type": "object"}) }
/// # }
/// # fn invoke<'a>(&'a self, _a: &'a Value) -> ToolFuture<'a> {
/// # Box::pin(async { Ok("PROJ-1".to_string()) })
/// # }
/// # }
/// let tools = Toolbox::new().with(Now).with(OpenTicket);
/// assert_eq!(tools.names(), vec!["now", "open_ticket"]);
///
/// let contract = TaskContract::workspace(
/// "triage the failing build",
/// "/path/to/repo",
/// Verification::WorkspaceFileContains { file: "TRIAGE.md".into(), needle: "#".into() },
/// )
/// .with_tools(tools);
/// # let _ = contract;
/// ```
///
/// Registration makes a tool *available*; it does not authorize it. Each call
/// is an [`Act::Exec`](crate::Act::Exec) check on the tool's name, so an
/// operator can be handed this box and still refuse one tool in it:
/// `deny_exec("open_ticket")` leaves `now` working.
///
/// [`Toolbox::validate`] runs before the first completion, which is what turns
/// a naming mistake into "your config is wrong" rather than an agent that has
/// silently stopped being able to write files:
///
/// ```
/// use io_harness::tools::{Tool, ToolFuture, Toolbox};
/// use io_harness::ToolSpec;
/// # use serde_json::{json, Value};
///
/// struct Impostor;
///
/// impl Tool for Impostor {
/// fn spec(&self) -> ToolSpec {
/// // The name of a built-in. Dispatch matches the built-in first, so
/// // this tool would be registered, offered, and never reached.
/// ToolSpec { name: "write_file".into(), description: "…".into(),
/// parameters: json!({"type": "object"}) }
/// }
/// # fn invoke<'a>(&'a self, _a: &'a Value) -> ToolFuture<'a> {
/// # Box::pin(async { Ok(String::new()) })
/// # }
/// }
///
/// let err = Toolbox::new().with(Impostor).validate().unwrap_err();
/// assert!(err.to_string().contains("write_file"));
/// ```
///
/// The same rejection covers an empty name, a name using the `mcp__` prefix
/// reserved for server tools, and two tools sharing one name.
///
/// Cheap to clone (each tool is behind an `Arc`), so a whole 0.5.0 tree shares
/// one toolbox and every child is offered what its parent was — inheritance
/// grants the tool, and the child's own narrowed policy still decides each call.
/// Names the harness owns. A registered tool taking one of these would shadow a
/// built-in the agent and the verification layer depend on.
pub const RESERVED_TOOL_NAMES: & = &;