toolnexus 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/skill.ts CHANGED
@@ -3,6 +3,11 @@
3
3
  * discover **\/SKILL.md, parse frontmatter, and expose ONE `skill` tool that
4
4
  * loads a skill's instructions + sampled resources on demand (progressive
5
5
  * disclosure).
6
+ *
7
+ * Beyond on-disk discovery the source also accepts skills supplied as data
8
+ * (`SkillDef`) — SPEC.md §3. Directory-sourced skills keep the exact `file://`
9
+ * base + on-disk sibling sampling (byte-identical); data-sourced skills use a
10
+ * logical `skill://name/` base + a supplied resource list and never touch disk.
6
11
  */
7
12
  import { readFileSync, readdirSync, realpathSync, statSync } from "node:fs"
8
13
  import path from "node:path"
@@ -27,24 +32,72 @@ export const SKILLS_PROMPT_PREAMBLE =
27
32
  export interface SkillInfo {
28
33
  name: string
29
34
  description?: string
30
- location: string // absolute path to SKILL.md
35
+ location: string // absolute path to SKILL.md (fs) or logical base (data)
31
36
  content: string // body after frontmatter
37
+ /** Internal origin discriminator; "fs" ⇒ on-disk (default), "logical" ⇒ data. */
38
+ origin?: "fs" | "logical"
39
+ /** Logical resource list (data-sourced skills only). */
40
+ resources?: string[]
41
+ /** Logical base, e.g. skill://name/ (data-sourced skills only). */
42
+ base?: string
43
+ }
44
+
45
+ /** A skill supplied directly as data, bypassing the filesystem (SPEC.md §3, S1). */
46
+ export interface SkillDef {
47
+ name: string
48
+ description?: string
49
+ content: string
50
+ /** Optional logical resource names surfaced in the invoke-time <skill_files> block. */
51
+ resources?: string[]
52
+ /** Optional logical base; defaults to `skill://<name>/`. */
53
+ base?: string
54
+ }
55
+
56
+ /** Why a candidate SKILL.md did not become a skill (SPEC.md §3, S3). */
57
+ export type SkillSkipReason = "missing-name" | "malformed-frontmatter" | "duplicate-name" | "unreadable"
58
+
59
+ export interface SkillSkip {
60
+ location: string
61
+ reason: SkillSkipReason
62
+ }
63
+
64
+ /** Result of a list-only validate pass (SPEC.md §3, S3). */
65
+ export interface SkillInventory {
66
+ skills: SkillInfo[]
67
+ skipped: SkillSkip[]
68
+ }
69
+
70
+ /** Options for loadSkills / listSkills (SPEC.md §3, S1/S2/S5). */
71
+ export interface LoadSkillsOptions {
72
+ dirs?: string | string[]
73
+ skills?: SkillDef[]
74
+ /** Per-agent allowlist keyed on skill name; same semantics as the MCP tools filter. */
75
+ filter?: Record<string, boolean>
76
+ /** Sibling-file sample cap: 0 ⇒ default 10, n>0 ⇒ cap, -1 ⇒ omit <skill_files>. */
77
+ sampleLimit?: number
32
78
  }
33
79
 
34
80
  /**
35
81
  * Parse YAML frontmatter (between the leading `---` fences) with a real YAML
36
82
  * parser, so folded (`>`)/literal (`|`) block scalars, quoting, and multi-line
37
83
  * values all resolve correctly. Scalar values are coerced to strings.
84
+ *
85
+ * `malformed` is true only when fences are present but the YAML fails to parse —
86
+ * distinguishing a malformed header from a body with no frontmatter, so the
87
+ * inventory (S3) can report the right skip reason. loadSkills' behavior is
88
+ * unchanged: a malformed header yields no name and the skill is skipped.
38
89
  */
39
- function parseFrontmatter(text: string): { data: Record<string, string>; content: string } {
90
+ function parseFrontmatter(text: string): { data: Record<string, string>; content: string; malformed: boolean } {
40
91
  const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/.exec(text)
41
- if (!match) return { data: {}, content: text }
92
+ if (!match) return { data: {}, content: text, malformed: false }
42
93
  const data: Record<string, string> = {}
43
94
  let parsed: unknown
95
+ let malformed = false
44
96
  try {
45
97
  parsed = parseYaml(match[1])
46
98
  } catch {
47
99
  parsed = null
100
+ malformed = true
48
101
  }
49
102
  if (parsed && typeof parsed === "object") {
50
103
  for (const [key, value] of Object.entries(parsed as Record<string, unknown>)) {
@@ -55,7 +108,7 @@ function parseFrontmatter(text: string): { data: Record<string, string>; content
55
108
  }
56
109
  }
57
110
  }
58
- return { data, content: match[2] }
111
+ return { data, content: match[2], malformed }
59
112
  }
60
113
 
61
114
  function walkSkillFiles(root: string): string[] {
@@ -150,50 +203,145 @@ function sampleSiblingFiles(dir: string, limit = 10): string[] {
150
203
  return out
151
204
  }
152
205
 
153
- export interface SkillSource {
154
- skills: Record<string, SkillInfo>
155
- tool: Tool
156
- /** Markdown catalog for the system prompt (mirrors opencode Skill.fmt). */
157
- prompt(): string
206
+ /** One raw candidate before cross-source dedupe: a parsed skill OR a typed skip. */
207
+ interface RawCandidate {
208
+ info?: SkillInfo
209
+ skip?: SkillSkip
158
210
  }
159
211
 
160
- /** Discover skills under one or more roots and build the `skill` loader tool. */
161
- export function loadSkills(dirs: string | string[]): SkillSource {
162
- const roots = Array.isArray(dirs) ? dirs : [dirs]
163
- const skills: Record<string, SkillInfo> = {}
164
-
165
- for (const root of roots) {
166
- let isDir = false
212
+ /** Discover candidates under one on-disk root (order = discovery order). */
213
+ function candidatesFromDir(root: string): RawCandidate[] {
214
+ const out: RawCandidate[] = []
215
+ let isDir = false
216
+ try {
217
+ isDir = statSync(root).isDirectory()
218
+ } catch {
219
+ isDir = false
220
+ }
221
+ if (!isDir) {
222
+ console.warn(`[toolnexus] skills dir not found: ${root}`)
223
+ return out
224
+ }
225
+ for (const file of walkSkillFiles(root)) {
226
+ let text: string
167
227
  try {
168
- isDir = statSync(root).isDirectory()
228
+ text = readFileSync(file, "utf8")
169
229
  } catch {
170
- isDir = false
230
+ out.push({ skip: { location: file, reason: "unreadable" } })
231
+ continue
171
232
  }
172
- if (!isDir) {
173
- console.warn(`[toolnexus] skills dir not found: ${root}`)
233
+ const { data, content, malformed } = parseFrontmatter(text)
234
+ if (malformed) {
235
+ out.push({ skip: { location: file, reason: "malformed-frontmatter" } })
174
236
  continue
175
237
  }
176
- for (const file of walkSkillFiles(root)) {
177
- let text: string
178
- try {
179
- text = readFileSync(file, "utf8")
180
- } catch {
181
- continue
182
- }
183
- const { data, content } = parseFrontmatter(text)
184
- if (!data.name) continue
185
- if (skills[data.name]) {
186
- console.warn(`[toolnexus] duplicate skill name "${data.name}" (${file}) — keeping first`)
187
- continue
188
- }
189
- skills[data.name] = {
190
- name: data.name,
191
- description: data.description,
192
- location: file,
193
- content,
194
- }
238
+ if (!data.name) {
239
+ out.push({ skip: { location: file, reason: "missing-name" } })
240
+ continue
195
241
  }
242
+ out.push({ info: { name: data.name, description: data.description, location: file, content, origin: "fs" } })
196
243
  }
244
+ return out
245
+ }
246
+
247
+ /** Turn data-supplied skill defs into candidates (logical origin). */
248
+ function candidatesFromDefs(defs: SkillDef[]): RawCandidate[] {
249
+ return defs.map((d): RawCandidate => {
250
+ if (!d.name) return { skip: { location: d.base ?? "skill://", reason: "missing-name" } }
251
+ const base = d.base && d.base.length > 0 ? d.base : `skill://${d.name}/`
252
+ return {
253
+ info: {
254
+ name: d.name,
255
+ description: d.description,
256
+ location: base,
257
+ content: d.content ?? "",
258
+ origin: "logical",
259
+ resources: d.resources ?? [],
260
+ base,
261
+ },
262
+ }
263
+ })
264
+ }
265
+
266
+ /** Collect ordered candidates from dirs then data defs. */
267
+ function collectCandidates(opts: LoadSkillsOptions): RawCandidate[] {
268
+ const roots = opts.dirs === undefined ? [] : Array.isArray(opts.dirs) ? opts.dirs : [opts.dirs]
269
+ const cands: RawCandidate[] = []
270
+ for (const root of roots) cands.push(...candidatesFromDir(root))
271
+ if (opts.skills) cands.push(...candidatesFromDefs(opts.skills))
272
+ return cands
273
+ }
274
+
275
+ /** Dedupe candidates by name (first-wins); later duplicates become skips. */
276
+ function mergeCandidates(cands: RawCandidate[]): { skills: Record<string, SkillInfo>; skipped: SkillSkip[] } {
277
+ const skills: Record<string, SkillInfo> = {}
278
+ const skipped: SkillSkip[] = []
279
+ for (const c of cands) {
280
+ if (c.skip) {
281
+ skipped.push(c.skip)
282
+ continue
283
+ }
284
+ const info = c.info!
285
+ if (skills[info.name]) {
286
+ console.warn(`[toolnexus] duplicate skill name "${info.name}" (${info.location}) — keeping first`)
287
+ skipped.push({ location: info.location, reason: "duplicate-name" })
288
+ continue
289
+ }
290
+ skills[info.name] = info
291
+ }
292
+ return { skills, skipped }
293
+ }
294
+
295
+ /**
296
+ * Apply the per-agent skill allowlist (S2), semantics identical to the MCP
297
+ * per-server tools filter and builtins: nil/empty ⇒ all; ≥1 true ⇒ allowlist
298
+ * (only true-mapped names); only-false ⇒ drop-list over all-on; unknown names
299
+ * are ignored and warned once.
300
+ */
301
+ function applySkillsFilter(
302
+ skills: Record<string, SkillInfo>,
303
+ filter: Record<string, boolean> | undefined,
304
+ ): Record<string, SkillInfo> {
305
+ if (!filter) return skills
306
+ const keys = Object.keys(filter)
307
+ if (keys.length === 0) return skills
308
+ const hasTrue = keys.some((k) => filter[k] === true)
309
+ for (const k of keys) {
310
+ if (!(k in skills)) console.warn(`[toolnexus] skill filter name "${k}" matched no skill`)
311
+ }
312
+ const out: Record<string, SkillInfo> = {}
313
+ for (const [name, info] of Object.entries(skills)) {
314
+ if (hasTrue ? filter[name] === true : filter[name] !== false) out[name] = info
315
+ }
316
+ return out
317
+ }
318
+
319
+ export interface SkillSource {
320
+ skills: Record<string, SkillInfo>
321
+ tool: Tool
322
+ /** Markdown catalog for the system prompt (mirrors opencode Skill.fmt). */
323
+ prompt(): string
324
+ }
325
+
326
+ /**
327
+ * Discover + validate skills from the same sources loadSkills accepts, returning
328
+ * parsed skills plus typed skip reasons — no toolkit wired (SPEC.md §3, S3). The
329
+ * inventory is UNFILTERED (it exists to author/validate the allowlist).
330
+ */
331
+ export function listSkills(input: string | string[] | LoadSkillsOptions): SkillInventory {
332
+ const opts: LoadSkillsOptions =
333
+ typeof input === "string" || Array.isArray(input) ? { dirs: input } : input
334
+ const merged = mergeCandidates(collectCandidates(opts))
335
+ return { skills: Object.values(merged.skills), skipped: merged.skipped }
336
+ }
337
+
338
+ /** Discover skills (dirs and/or data) and build the `skill` loader tool. */
339
+ export function loadSkills(input: string | string[] | LoadSkillsOptions): SkillSource {
340
+ const opts: LoadSkillsOptions =
341
+ typeof input === "string" || Array.isArray(input) ? { dirs: input } : input
342
+ const sampleLimit = opts.sampleLimit ?? 0
343
+ const merged = mergeCandidates(collectCandidates(opts))
344
+ const skills = applySkillsFilter(merged.skills, opts.filter)
197
345
 
198
346
  const tool: Tool = {
199
347
  name: "skill",
@@ -214,10 +362,28 @@ export function loadSkills(dirs: string | string[]): SkillSource {
214
362
  isError: true,
215
363
  }
216
364
  }
217
- const dir = path.dirname(info.location)
218
- const base = pathToFileURL(dir).href
219
- const files = sampleSiblingFiles(dir)
220
- const output = [
365
+ // effLimit: 0 ⇒ default 10 (byte-identical to today), n>0 ⇒ cap, -1 ⇒ omit.
366
+ const effLimit = sampleLimit === 0 ? 10 : sampleLimit
367
+ let base: string
368
+ let files: string[]
369
+ let metaDir: string
370
+ // Emit the <skill_files> block: always for fs skills (byte-identical, even
371
+ // when empty), only when a data skill actually carries resources (an
372
+ // instruction-only data skill omits it). Never when sampling is disabled.
373
+ let emitFiles = effLimit !== -1
374
+ if (info.origin === "logical") {
375
+ base = info.base ?? `skill://${info.name}/`
376
+ const res = info.resources ?? []
377
+ if (res.length === 0) emitFiles = false
378
+ files = effLimit > 0 ? res.slice(0, effLimit) : res
379
+ metaDir = base
380
+ } else {
381
+ const dir = path.dirname(info.location)
382
+ base = pathToFileURL(dir).href
383
+ files = effLimit === -1 ? [] : sampleSiblingFiles(dir, effLimit)
384
+ metaDir = dir
385
+ }
386
+ const lines = [
221
387
  `<skill_content name="${info.name}">`,
222
388
  `# Skill: ${info.name}`,
223
389
  "",
@@ -225,14 +391,18 @@ export function loadSkills(dirs: string | string[]): SkillSource {
225
391
  "",
226
392
  `Base directory for this skill: ${base}`,
227
393
  "Relative paths in this skill (e.g., scripts/, reference/) are relative to this base directory.",
228
- "Note: file list is sampled.",
229
- "",
230
- "<skill_files>",
231
- files.map((f) => `<file>${f}</file>`).join("\n"),
232
- "</skill_files>",
233
- "</skill_content>",
234
- ].join("\n")
235
- return { output, isError: false, metadata: { name: info.name, dir } }
394
+ ]
395
+ if (emitFiles) {
396
+ lines.push(
397
+ "Note: file list is sampled.",
398
+ "",
399
+ "<skill_files>",
400
+ files.map((f) => `<file>${f}</file>`).join("\n"),
401
+ "</skill_files>",
402
+ )
403
+ }
404
+ lines.push("</skill_content>")
405
+ return { output: lines.join("\n"), isError: false, metadata: { name: info.name, dir: metaDir } }
236
406
  },
237
407
  }
238
408
 
package/src/toolkit.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  */
5
5
  import { type Tool, type ToolContext, type ToolResult, type McpStatus } from "./types.js"
6
6
  import { loadMcp, type McpSource } from "./mcp.js"
7
- import { loadSkills, type SkillSource } from "./skill.js"
7
+ import { loadSkills, type SkillDef, type SkillSource } from "./skill.js"
8
8
  import { selectBuiltins, type BuiltinsConfig } from "./builtin.js"
9
9
  import { agentTools, parseAgentsConfig, type Agent } from "./a2a.js"
10
10
  import { startA2AServer, type A2AConfig, type OnTask, type ServeHandle } from "./serve.js"
@@ -15,11 +15,26 @@ import type { Client } from "./client.js"
15
15
  export interface ToolkitOptions {
16
16
  mcpConfig?: string | object
17
17
  skillsDir?: string | string[]
18
+ /** Skills supplied as data, bypassing the filesystem (§3, S1). */
19
+ skills?: SkillDef[]
20
+ /** Lazy provider of data-supplied skills, resolved once at toolkit build (§3, S1). */
21
+ skillProvider?: () => SkillDef[] | Promise<SkillDef[]>
22
+ /** Per-agent skill allowlist keyed on name; same semantics as the MCP tools filter (§3, S2). */
23
+ skillsFilter?: Record<string, boolean>
24
+ /** Sibling-file sample cap: 0 ⇒ default 10, n>0 ⇒ cap, -1 ⇒ omit <skill_files> (§3, S5). */
25
+ skillSampleLimit?: number
18
26
  extraTools?: Tool[]
19
27
  /** Built-in tools (§4A). On by default. false | { disabled:true } | { enabled:false } ⇒ off. */
20
28
  builtins?: BuiltinsConfig
21
29
  /** Remote A2A agents — each advertised skill becomes a tool (source:"a2a"). */
22
30
  agents?: Agent[]
31
+ /**
32
+ * Host resolver for out-of-band input (§10). When set, connected MCP servers may elicit input
33
+ * from the human mid-`tools/call` and it is bridged onto this `waitFor` (form→kind:"input",
34
+ * URL→kind:"authorization"). Typically the same function passed to the client. Omit ⇒ MCP
35
+ * elicitation is not advertised.
36
+ */
37
+ waitFor?: (request: import("./types.js").Request) => Promise<import("./types.js").Answer>
23
38
  }
24
39
 
25
40
  export class Toolkit {
@@ -51,8 +66,29 @@ export class Toolkit {
51
66
  }
52
67
 
53
68
  static async create(opts: ToolkitOptions): Promise<Toolkit> {
54
- const mcp = opts.mcpConfig !== undefined ? await loadMcp(opts.mcpConfig) : undefined
55
- const skill = opts.skillsDir !== undefined ? loadSkills(opts.skillsDir) : undefined
69
+ const mcp = opts.mcpConfig !== undefined ? await loadMcp(opts.mcpConfig, { waitFor: opts.waitFor }) : undefined
70
+ // Skills come from directories, in-memory data, and/or a lazy provider (§3,
71
+ // S1). The provider is resolved here (create() is async) and merged with the
72
+ // data list; a provider failure is isolated so other sources still load.
73
+ let skill: SkillSource | undefined
74
+ if (opts.skillsDir !== undefined || opts.skills !== undefined || opts.skillProvider !== undefined) {
75
+ let providerDefs: SkillDef[] = []
76
+ if (opts.skillProvider) {
77
+ try {
78
+ providerDefs = await opts.skillProvider()
79
+ } catch (e) {
80
+ console.warn(`[toolnexus] skill provider failed: ${e instanceof Error ? e.message : String(e)}`)
81
+ providerDefs = []
82
+ }
83
+ }
84
+ const dataDefs = [...(opts.skills ?? []), ...providerDefs]
85
+ skill = loadSkills({
86
+ dirs: opts.skillsDir,
87
+ skills: dataDefs.length > 0 ? dataDefs : undefined,
88
+ filter: opts.skillsFilter,
89
+ sampleLimit: opts.skillSampleLimit,
90
+ })
91
+ }
56
92
  // The toggle comes from the `builtins` option, or a top-level `builtins`
57
93
  // key on a parsed config object — same precedence as MCP's isEnabled.
58
94
  let builtinsCfg = opts.builtins
package/src/types.ts CHANGED
@@ -43,6 +43,12 @@ export interface Answer {
43
43
  id: string
44
44
  ok: boolean
45
45
  data?: Record<string, unknown>
46
+ /**
47
+ * When `ok` is false, why (advisory — the loop rule branches only on `ok`). Distinguishes an
48
+ * explicit refusal from a dismissal/timeout; the MCP elicitation bridge maps `declined`→`decline`
49
+ * and everything else→`cancel`. (R1)
50
+ */
51
+ reason?: "declined" | "cancelled" | "expired"
46
52
  }
47
53
 
48
54
  let _pendingSeq = 0