You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+37Lines changed: 37 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -184,6 +184,43 @@ let searchTool =
184
184
|> Tool.describe "Searches the knowledge base for relevant documents"
185
185
```
186
186
187
+
### Tools: Dependency Injection (`Tool.inject`)
188
+
189
+
Most real-world tool functions need a dependency — a database, an HTTP client, a domain service. But the agent shouldn't see (or be able to fill in) that dependency: it's a host concern, not a model concern.
190
+
191
+
`Tool.inject` partially applies the **leftmost parameter** of a tool's underlying function with a value you supply, and returns a new `ToolDef` whose metadata and method signature are exactly one parameter shorter. The captured dependency is forwarded to the original function at invoke time.
// The agent now sees a 1-parameter tool: { Name = "lookupUser"; Parameters = [userId: int] }
204
+
// At invoke time, `realDb` is passed automatically; the model only supplies `userId`.
205
+
```
206
+
207
+
**Why `Tool.inject`?**
208
+
-**Hide infrastructure from the model** — the LLM only sees parameters it can meaningfully reason about
209
+
-**Per-request dependencies** — capture a tenant-scoped service, a request-scoped logger, etc. by injecting a fresh value each time you build the agent
210
+
-**No wrapper boilerplate** — you don't need to hand-write a closure-shaped tool function just to thread a dependency through
211
+
-**XML metadata still works** — descriptions on the remaining parameters survive the injection
212
+
213
+
`Tool.inject` is composable in pipelines and works whether the dependency is the only parameter or one of many. You can also inject into functions whose remaining input is `unit`:
214
+
215
+
```fsharp
216
+
let nowFromClock (clock: IClock) () : string = clock.Now()
0 commit comments