Sync docs with the defineTool DSL and actual demo code

The architecture cookbook's tool-plugin example now uses defineTool
with typed args (the raw-JSON-Schema + `args: any` example contradicted
the type-safety policy it sits next to); a note explains raw schemas
remain the MCP interop path. The echo-agent README's mock-llm row now
matches the code (registerAdapter(['mock-echo'])) and the echo-tool
row mentions the typed registration.
This commit is contained in:
Tianyi Cui
2026-06-11 13:48:22 +08:00
parent ef45ca823a
commit 2b447625fa
2 changed files with 14 additions and 6 deletions
+12 -4
View File
@@ -261,23 +261,31 @@ implements it **without modifying the loop**:
```ts
import type { Context } from 'cordis'
import { defineTool } from '@deepseek-ai/dsh-tools'
export const name = 'my-tool'
export const inject = ['tools']
export function apply(ctx: Context) {
ctx.tools.register({
ctx.tools.register(defineTool({
name: 'read_file',
description: 'Read a file from disk.',
parameters: { type: 'object', properties: { path: { type: 'string' } }, required: ['path'] },
async execute(args: any) {
parameters: {
path: { type: 'string', required: true, description: 'Absolute file path' },
},
async execute(args) {
// args is typed: { path: string }
const text = await readFile(args.path, 'utf8')
return [{ type: 'text', text }]
},
})
}))
}
```
(Raw JSON-Schema `ToolDefinition`s are still accepted by
`ctx.tools.register()` directly — that's how MCP-sourced tools arrive.
`defineTool` is the typed sugar for first-party tools.)
### A hook plugin (permission gate)
```ts
+2 -2
View File
@@ -18,8 +18,8 @@ Runnable demo: stdin chat with a scripted mock model and an echo tool.
| File | Role | Key patterns demonstrated |
|---|---|---|
| `mock-llm.ts` | `LlmAdapter` registration | `ctx.llm.registerAdapter([])`, streaming chunks with proper `block-start`/`block-end` protocol |
| `echo-tool.ts` | Tool registration | `ctx.tools.register()`, tool execution returning `ContentBlock[]` |
| `mock-llm.ts` | `LlmAdapter` registration | `ctx.llm.registerAdapter(['mock-echo'], …)`, streaming chunks with proper `block-start`/`block-end` protocol |
| `echo-tool.ts` | Tool registration | `ctx.tools.register(defineTool(…))` with typed `execute` args, tool execution returning `ContentBlock[]` |
| `session-jsonl.ts` | Persistence | `session/event` listener + `session/flush` drain, fiber-dispose cleanup |
| `stdio-chat.ts` | UI | `agent/stream-chunk`, `session/event` (tool/*), stdin→send/steer |
| `start.ts` | Bootstrap | `Context` + `Loader` + `plugin-include` wired to `cordis.yml` |