@agntn/tools 0.3.0 → 0.3.2

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/dist/pi.mjs CHANGED
@@ -1,32 +1,102 @@
1
+ import { Check } from "./_chunks/libs/typebox.mjs";
1
2
  import { ToolDefinitionError, ToolInputError, indexTools, invokeTool, resultText, sanitizeLine, sanitizeText, validateInput } from "./_chunks/src.mjs";
3
+ import { throwWithCauses } from "./_chunks/causes.mjs";
2
4
  import { hostContext } from "./_chunks/updates.mjs";
5
+ const EXPOSURES = [
6
+ "direct",
7
+ "model-only",
8
+ "codemode",
9
+ "deferred",
10
+ "hidden"
11
+ ];
12
+ const EXPOSURE_VARIABLE = "AGNTN_PI_EXPOSURE";
3
13
  function registerPiTools(pi, tools, options = {}) {
4
14
  const names = [...indexTools(tools).keys()];
5
15
  const failures = options.failures ?? "throw";
6
16
  refuseStray(names, "confirm", options.confirm);
7
17
  refuseStray(names, "prepareArguments", options.prepareArguments);
18
+ checkOption(names, options.exposure);
19
+ fromEnvironment(EXPOSURE_VARIABLE);
20
+ const exposures = new Map(names.map((name) => [name, exposureOf(name, options.exposure)]));
8
21
  for (const tool of tools) {
9
22
  const renderers = byName(options.renderers, tool.name);
10
23
  const ask = byName(options.confirm, tool.name);
11
- const prepare = byName(options.prepareArguments, tool.name);
24
+ const prepare = parseJsonText(tool, byName(options.prepareArguments, tool.name));
12
25
  pi.registerTool({
13
26
  ...hostRenderers(tool, renderers, options.Text),
14
- ...prepare === void 0 ? {} : { prepareArguments: prepare },
27
+ prepareArguments: prepare,
15
28
  name: tool.name,
16
29
  label: tool.title,
17
30
  description: tool.description,
18
31
  ...tool.snippet === void 0 ? {} : { promptSnippet: tool.snippet },
19
32
  ...tool.guidelines === void 0 ? {} : { promptGuidelines: [...tool.guidelines] },
20
33
  parameters: tool.input,
34
+ ...reach(exposures.get(tool.name), options.namespace),
21
35
  async execute(_toolCallId, params, signal, onUpdate, ctx) {
22
36
  if (ask) await approve(tool, ask, params, signal, ctx);
23
- const result = await invokeTool(tool, params, hostContext(signal, onUpdate, ctx));
37
+ const context = hostContext(signal, onUpdate, ctx);
38
+ const result = await invokeTool(tool, params, context).catch(throwWithCauses);
24
39
  if (result.isError && failures === "throw") throw new Error(resultText(result));
25
40
  return result;
26
41
  }
27
42
  });
28
43
  }
29
44
  }
45
+ function parseJsonText(tool, own) {
46
+ if (own === void 0) return (args) => repairJsonText(tool, args);
47
+ return (args) => own(repairJsonText(tool, args));
48
+ }
49
+ function repairJsonText(tool, args) {
50
+ if (typeof args !== "object" || args === null || Array.isArray(args)) return args;
51
+ const raw = args;
52
+ let repaired;
53
+ const properties = Object.entries(tool.input.properties);
54
+ for (const [key, schema] of properties) {
55
+ const parsed = Object.hasOwn(raw, key) ? jsonValue(raw[key], schema) : void 0;
56
+ if (parsed === void 0) continue;
57
+ repaired ??= { ...raw };
58
+ repaired[key] = parsed;
59
+ }
60
+ return repaired ?? args;
61
+ }
62
+ function jsonValue(value, schema) {
63
+ if (typeof value !== "string" || !/^\s*[[{]/.test(value)) return void 0;
64
+ try {
65
+ const parsed = JSON.parse(value);
66
+ return typeof parsed === "object" && parsed !== null && Check(schema, parsed) ? parsed : void 0;
67
+ } catch {
68
+ return;
69
+ }
70
+ }
71
+ function reach(exposure, namespace) {
72
+ return {
73
+ ...exposure === void 0 ? {} : { exposure },
74
+ ...namespace === void 0 ? {} : { namespace: { ...namespace } }
75
+ };
76
+ }
77
+ function exposureOf(name, option) {
78
+ const own = `${EXPOSURE_VARIABLE}_${name.split("_", 1)[0]?.toUpperCase() ?? ""}`;
79
+ const global = fromEnvironment(EXPOSURE_VARIABLE);
80
+ return fromEnvironment(own) ?? global ?? (typeof option === "object" ? byName(option.tools, name) ?? option.default : option);
81
+ }
82
+ function checkOption(names, option) {
83
+ if (typeof option !== "object") {
84
+ if (option !== void 0) checkExposure(option, "exposure");
85
+ return;
86
+ }
87
+ refuseStray(names, "exposure.tools", option.tools);
88
+ if (option.default !== void 0) checkExposure(option.default, "exposure.default");
89
+ for (const [name, value] of Object.entries(option.tools ?? {})) checkExposure(value, `exposure of ${name}`);
90
+ }
91
+ function fromEnvironment(variable) {
92
+ const value = typeof process === "undefined" ? void 0 : process.env[variable];
93
+ return value ? checkExposure(value, variable) : void 0;
94
+ }
95
+ function checkExposure(value, where) {
96
+ if (typeof value === "string" && EXPOSURES.includes(value)) return value;
97
+ const shown = typeof value === "string" ? `is ${JSON.stringify(value)}` : "isn't a string";
98
+ throw new ToolDefinitionError(`${where} ${shown}; takes ${EXPOSURES.join(", ")}`);
99
+ }
30
100
  function refuseStray(names, option, entries) {
31
101
  const stray = Object.keys(entries ?? {}).find((name) => !names.includes(name));
32
102
  if (stray === void 0) return;
@@ -1,8 +1,19 @@
1
1
  import { ToolDefinition } from "./_chunks/index.mjs";
2
+ import { McpHost } from "./_chunks/mcp-answers.mjs";
3
+ /** What SDK v1 keeps of the HTTP request. */
4
+ export interface ToolkitRequestInfo {
5
+ readonly headers: Readonly<Record<string, string | readonly string[] | undefined>>;
6
+ readonly url?: {
7
+ readonly href: string;
8
+ };
9
+ }
2
10
  /** The part of the SDK v1 request `extra` a handler reads: abort signal, progress and questions. */
3
11
  export interface ToolkitExtra {
4
12
  readonly signal?: AbortSignal;
13
+ /** Present on an HTTP transport. */
14
+ readonly requestInfo?: ToolkitRequestInfo;
5
15
  readonly _meta?: {
16
+ readonly [key: string]: unknown;
6
17
  readonly progressToken?: string | number;
7
18
  };
8
19
  readonly sendNotification?: (notification: {
@@ -64,6 +75,9 @@ export interface ToolkitTool {
64
75
  /**
65
76
  * Turns tools into toolkit entries that list what `listTools` lists and answer via `callTool`.
66
77
  *
78
+ * `host` is an {@link McpHost} with the call's `_meta` and a `Request` rebuilt from SDK v1's
79
+ * `requestInfo`: the caller's URL and headers, but no body and always `GET`.
80
+ *
67
81
  * Progress and questions need the toolkit's sessions: its stateless default answers in plain JSON.
68
82
  * `useMcpElicitation` also needs Nitro's `asyncContext`, since it finds the server in the request.
69
83
  *
@@ -75,4 +89,5 @@ export interface ToolkitTool {
75
89
  */
76
90
  export declare function toToolkitTools(info: {
77
91
  readonly name: string;
78
- }, tools: readonly ToolDefinition[], options?: ToolkitOptions): ToolkitTool[];
92
+ }, tools: readonly ToolDefinition[], options?: ToolkitOptions): ToolkitTool[];
93
+ export type { McpHost };
package/dist/toolkit.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import "./_chunks/src.mjs";
2
2
  import { hostAsk } from "./_chunks/questions.mjs";
3
- import { callTool, listTools, progressSteps } from "./_chunks/mcp-answers.mjs";
3
+ import { callTool, listTools, mcpHost, progressSteps } from "./_chunks/mcp-answers.mjs";
4
4
  import { z } from "zod";
5
5
  function toToolkitTools(info, tools, options = {}) {
6
6
  return listTools(tools).map((entry) => ({
@@ -35,6 +35,10 @@ function callContext(extra) {
35
35
  const send = extra?.sendNotification;
36
36
  return {
37
37
  signal: extra?.signal,
38
+ ...mcpHost({
39
+ request: rebuiltRequest(extra?.requestInfo),
40
+ meta: extra?._meta
41
+ }),
38
42
  ...progressToken === void 0 || send === void 0 ? {} : { progress: progressSteps((params) => {
39
43
  send({
40
44
  method: "notifications/progress",
@@ -46,6 +50,23 @@ function callContext(extra) {
46
50
  }) }
47
51
  };
48
52
  }
53
+ function rebuiltRequest(info) {
54
+ if (info?.url === void 0) return void 0;
55
+ const headers = new Headers();
56
+ for (const [name, value] of Object.entries(info.headers)) for (const item of [value ?? []].flat()) if (isHeader(name, item)) headers.append(name, item);
57
+ try {
58
+ return new Request(info.url.href, { headers });
59
+ } catch {
60
+ return;
61
+ }
62
+ }
63
+ function isHeader(name, value) {
64
+ try {
65
+ return new Headers([[name, value]]).has(name);
66
+ } catch {
67
+ return false;
68
+ }
69
+ }
49
70
  function formAsk(extra, elicitation) {
50
71
  const send = extra?.sendRequest;
51
72
  if (send === void 0 || elicitation?.().supports("form") !== true) return {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agntn/tools",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "One tool definition for MCP, Pi, OMP and the AI SDK",
5
5
  "homepage": "https://github.com/agntn/tools#readme",
6
6
  "bugs": "https://github.com/agntn/tools/issues",
@@ -55,19 +55,19 @@
55
55
  "devDependencies": {
56
56
  "@agntn/ox": "0.1.0",
57
57
  "@earendil-works/pi-coding-agent": "1.0.4",
58
- "@modelcontextprotocol/client": "2.2.0",
58
+ "@modelcontextprotocol/client": "2.4.0",
59
59
  "@modelcontextprotocol/sdk": "1.32.1",
60
- "@modelcontextprotocol/server": "2.2.0",
60
+ "@modelcontextprotocol/server": "2.4.0",
61
61
  "@oh-my-pi/pi-coding-agent": "18.0.11",
62
- "@types/node": "26.2.0",
62
+ "@types/node": "26.6.6",
63
63
  "ai": "7.0.127",
64
64
  "h3": "2.0.1",
65
65
  "h3-mcp": "0.2.0",
66
66
  "obuild": "0.4.43",
67
- "typebox": "1.3.34",
67
+ "typebox": "1.4.3",
68
68
  "typescript": "7.0.2",
69
- "vite": "npm:@voidzero-dev/vite-plus-core@1.0.0",
70
- "vite-plus": "1.0.0",
69
+ "vite": "npm:@voidzero-dev/vite-plus-core@1.4.0",
70
+ "vite-plus": "1.4.0",
71
71
  "zod": "4.6.5"
72
72
  },
73
73
  "peerDependencies": {