actionmcp 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +74 -2
- package/dist/analyzer/config.d.ts +15 -0
- package/dist/analyzer/config.js +43 -0
- package/dist/analyzer/diagnostics.js +126 -0
- package/dist/analyzer/explicit.js +128 -0
- package/dist/analyzer/index.js +178 -0
- package/dist/analyzer/jsdoc.js +32 -0
- package/dist/analyzer/options.js +65 -0
- package/dist/analyzer/routes.js +81 -0
- package/dist/analyzer/scan.js +280 -0
- package/dist/analyzer/source.js +100 -0
- package/dist/analyzer/types.js +133 -0
- package/dist/cli/bin.d.ts +1 -0
- package/dist/cli/bin.js +5 -0
- package/dist/cli/commands.js +107 -0
- package/dist/cli/environment.js +53 -0
- package/dist/cli/index.js +97 -0
- package/dist/cli/output.js +27 -0
- package/dist/cli/project.js +33 -0
- package/dist/client.d.ts +6 -0
- package/dist/client.js +4 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +84 -0
- package/dist/core/compare.js +6 -0
- package/dist/core/events.js +12 -0
- package/dist/core/execute.js +70 -0
- package/dist/core/formdata.js +15 -0
- package/dist/core/guards.js +8 -0
- package/dist/core/match.js +16 -0
- package/dist/core/names.js +51 -0
- package/dist/core/result.d.ts +18 -0
- package/dist/core/result.js +85 -0
- package/dist/core/schema.d.ts +6 -0
- package/dist/core/schema.js +90 -0
- package/dist/core/types.d.ts +34 -0
- package/dist/core/webmcp.js +62 -0
- package/dist/generator/index.js +36 -0
- package/dist/generator/render.js +91 -0
- package/dist/generator/watch.js +76 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +5 -0
- package/dist/next/NavigateTool.js +40 -0
- package/dist/next/config-store.js +12 -0
- package/dist/next/redirect.js +15 -0
- package/dist/next/routes.d.ts +3 -0
- package/dist/next/routes.js +57 -0
- package/dist/react/AgentTools.d.ts +14 -0
- package/dist/react/AgentTools.js +35 -0
- package/dist/react/Confirm.d.ts +3 -0
- package/dist/react/Confirm.js +99 -0
- package/dist/react/Devtools.d.ts +2 -0
- package/dist/react/Devtools.js +13 -0
- package/dist/react/DevtoolsPanel.js +223 -0
- package/dist/react/Provider.d.ts +6 -0
- package/dist/react/Provider.js +21 -0
- package/dist/react/Registrar.js +12 -0
- package/dist/react/context.d.ts +14 -0
- package/dist/react/context.js +5 -0
- package/dist/react/useAgentTools.js +103 -0
- package/dist/registry.d.ts +4 -0
- package/dist/registry.js +4 -0
- package/dist/server/agent.d.ts +43 -0
- package/dist/server/agent.js +34 -0
- package/dist/server/index.d.ts +6 -0
- package/dist/server/index.js +4 -0
- package/dist/server/registry.d.ts +53 -0
- package/dist/server/registry.js +93 -0
- package/dist/server/resolve.js +68 -0
- package/package.json +96 -4
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import generatedRegistry from "actionmcp/registry";
|
|
2
|
+
import { NAVIGATE_TOOL_NAME, applyPrefix } from "../core/names.js";
|
|
3
|
+
import { NavigateTool } from "../next/NavigateTool.js";
|
|
4
|
+
import { selectRoutes } from "../next/routes.js";
|
|
5
|
+
import { selectTools } from "../server/resolve.js";
|
|
6
|
+
import { Registrar } from "./Registrar.js";
|
|
7
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
// src/react/AgentTools.tsx
|
|
9
|
+
function AgentTools({ registry = generatedRegistry, include, exclude, prefix, navigate = false, exposedTo, polyfill = false, originTrialToken = process.env.NEXT_PUBLIC_WEBMCP_OT_TOKEN }) {
|
|
10
|
+
const tools = selectTools(registry, {
|
|
11
|
+
include,
|
|
12
|
+
exclude,
|
|
13
|
+
prefix
|
|
14
|
+
});
|
|
15
|
+
const routes = navigate ? selectRoutes(registry.routes, navigate) : [];
|
|
16
|
+
if (process.env.NODE_ENV !== "production" && tools.length > 32) console.warn(`actionmcp: AT104 ${tools.length} tools are registered by one <AgentTools />. Agents may truncate lists longer than 32. Use include or exclude to scope them.`);
|
|
17
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
18
|
+
originTrialToken ? /* @__PURE__ */ jsx("meta", {
|
|
19
|
+
httpEquiv: "origin-trial",
|
|
20
|
+
content: originTrialToken
|
|
21
|
+
}) : null,
|
|
22
|
+
/* @__PURE__ */ jsx(Registrar, {
|
|
23
|
+
tools,
|
|
24
|
+
polyfill,
|
|
25
|
+
exposedTo
|
|
26
|
+
}),
|
|
27
|
+
routes.length > 0 ? /* @__PURE__ */ jsx(NavigateTool, {
|
|
28
|
+
routes,
|
|
29
|
+
name: applyPrefix(NAVIGATE_TOOL_NAME, prefix),
|
|
30
|
+
polyfill,
|
|
31
|
+
exposedTo
|
|
32
|
+
}) : null
|
|
33
|
+
] });
|
|
34
|
+
}
|
|
35
|
+
export { AgentTools };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useId, useRef } from "react";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
// src/react/Confirm.tsx
|
|
5
|
+
const dialogStyle = {
|
|
6
|
+
maxWidth: "min(28rem, calc(100vw - 2rem))",
|
|
7
|
+
padding: "1.25rem",
|
|
8
|
+
border: "1px solid GrayText",
|
|
9
|
+
borderRadius: "0.75rem",
|
|
10
|
+
font: "0.875rem/1.5 system-ui, sans-serif",
|
|
11
|
+
colorScheme: "light dark",
|
|
12
|
+
color: "CanvasText",
|
|
13
|
+
background: "Canvas"
|
|
14
|
+
};
|
|
15
|
+
const inputStyle = {
|
|
16
|
+
maxHeight: "12rem",
|
|
17
|
+
margin: "0.75rem 0",
|
|
18
|
+
padding: "0.5rem",
|
|
19
|
+
overflow: "auto",
|
|
20
|
+
border: "1px solid GrayText",
|
|
21
|
+
borderRadius: "0.375rem",
|
|
22
|
+
font: "0.75rem/1.4 ui-monospace, monospace",
|
|
23
|
+
whiteSpace: "pre-wrap",
|
|
24
|
+
wordBreak: "break-word"
|
|
25
|
+
};
|
|
26
|
+
const actionsStyle = {
|
|
27
|
+
display: "flex",
|
|
28
|
+
justifyContent: "flex-end",
|
|
29
|
+
gap: "0.5rem"
|
|
30
|
+
};
|
|
31
|
+
const buttonStyle = {
|
|
32
|
+
padding: "0.375rem 0.875rem",
|
|
33
|
+
font: "inherit",
|
|
34
|
+
cursor: "pointer"
|
|
35
|
+
};
|
|
36
|
+
function ConfirmDialog({ call, title, description, onConfirm, onCancel }) {
|
|
37
|
+
const dialog = useRef(null);
|
|
38
|
+
const cancelButton = useRef(null);
|
|
39
|
+
const id = useId();
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
const element = dialog.current;
|
|
42
|
+
if (element && !element.open) element.showModal();
|
|
43
|
+
cancelButton.current?.focus();
|
|
44
|
+
return () => element?.close();
|
|
45
|
+
}, []);
|
|
46
|
+
return /* @__PURE__ */ jsxs("dialog", {
|
|
47
|
+
ref: dialog,
|
|
48
|
+
"data-actionmcp-confirm": call.name,
|
|
49
|
+
"aria-labelledby": `${id}-title`,
|
|
50
|
+
"aria-describedby": `${id}-description`,
|
|
51
|
+
style: dialogStyle,
|
|
52
|
+
onCancel: (event) => {
|
|
53
|
+
event.preventDefault();
|
|
54
|
+
onCancel();
|
|
55
|
+
},
|
|
56
|
+
children: [
|
|
57
|
+
/* @__PURE__ */ jsxs("h2", {
|
|
58
|
+
id: `${id}-title`,
|
|
59
|
+
style: {
|
|
60
|
+
margin: 0,
|
|
61
|
+
fontSize: "1rem"
|
|
62
|
+
},
|
|
63
|
+
children: [
|
|
64
|
+
"Allow “",
|
|
65
|
+
title ?? call.name,
|
|
66
|
+
"”?"
|
|
67
|
+
]
|
|
68
|
+
}),
|
|
69
|
+
/* @__PURE__ */ jsxs("p", {
|
|
70
|
+
id: `${id}-description`,
|
|
71
|
+
style: { margin: "0.5rem 0 0" },
|
|
72
|
+
children: ["An agent wants to run this action. ", description]
|
|
73
|
+
}),
|
|
74
|
+
/* @__PURE__ */ jsx("pre", {
|
|
75
|
+
tabIndex: 0,
|
|
76
|
+
role: "group",
|
|
77
|
+
"aria-label": "Action input",
|
|
78
|
+
style: inputStyle,
|
|
79
|
+
children: JSON.stringify(call.input, null, 2)
|
|
80
|
+
}),
|
|
81
|
+
/* @__PURE__ */ jsxs("div", {
|
|
82
|
+
style: actionsStyle,
|
|
83
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
84
|
+
ref: cancelButton,
|
|
85
|
+
type: "button",
|
|
86
|
+
style: buttonStyle,
|
|
87
|
+
onClick: onCancel,
|
|
88
|
+
children: "Cancel"
|
|
89
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
90
|
+
type: "button",
|
|
91
|
+
style: buttonStyle,
|
|
92
|
+
onClick: onConfirm,
|
|
93
|
+
children: "Allow"
|
|
94
|
+
})]
|
|
95
|
+
})
|
|
96
|
+
]
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
export { ConfirmDialog };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Suspense, lazy } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
// src/react/Devtools.tsx
|
|
5
|
+
const Panel = process.env.NODE_ENV === "development" ? lazy(() => import("./DevtoolsPanel.js").then((module) => ({ default: module.DevtoolsPanel }))) : void 0;
|
|
6
|
+
function AgentToolsDevtools() {
|
|
7
|
+
if (!Panel) return null;
|
|
8
|
+
return /* @__PURE__ */ jsx(Suspense, {
|
|
9
|
+
fallback: null,
|
|
10
|
+
children: /* @__PURE__ */ jsx(Panel, {})
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
export { AgentToolsDevtools };
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { subscribeToToolCalls } from "../core/events.js";
|
|
4
|
+
import { getWebMCPStatus, whenModelContextAvailable } from "../core/webmcp.js";
|
|
5
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
// src/react/DevtoolsPanel.tsx
|
|
7
|
+
const HISTORY_LIMIT = 50;
|
|
8
|
+
const STATUS_LABELS = {
|
|
9
|
+
native: "WebMCP: native",
|
|
10
|
+
polyfill: "WebMCP: polyfill (native agents do not see these tools)",
|
|
11
|
+
unavailable: "WebMCP unavailable"
|
|
12
|
+
};
|
|
13
|
+
const styles = {
|
|
14
|
+
toggle: {
|
|
15
|
+
position: "fixed",
|
|
16
|
+
right: "1rem",
|
|
17
|
+
bottom: "1rem",
|
|
18
|
+
zIndex: 0x7ffffffe,
|
|
19
|
+
padding: "0.375rem 0.75rem",
|
|
20
|
+
border: "1px solid GrayText",
|
|
21
|
+
borderRadius: "999px",
|
|
22
|
+
font: "0.75rem/1.4 system-ui, sans-serif",
|
|
23
|
+
color: "CanvasText",
|
|
24
|
+
background: "Canvas",
|
|
25
|
+
cursor: "pointer"
|
|
26
|
+
},
|
|
27
|
+
panel: {
|
|
28
|
+
position: "fixed",
|
|
29
|
+
right: "1rem",
|
|
30
|
+
bottom: "3.5rem",
|
|
31
|
+
zIndex: 0x7ffffffe,
|
|
32
|
+
display: "flex",
|
|
33
|
+
flexDirection: "column",
|
|
34
|
+
gap: "0.5rem",
|
|
35
|
+
width: "min(26rem, calc(100vw - 2rem))",
|
|
36
|
+
maxHeight: "min(36rem, calc(100vh - 5rem))",
|
|
37
|
+
padding: "0.75rem",
|
|
38
|
+
overflow: "auto",
|
|
39
|
+
border: "1px solid GrayText",
|
|
40
|
+
borderRadius: "0.75rem",
|
|
41
|
+
font: "0.75rem/1.5 system-ui, sans-serif",
|
|
42
|
+
colorScheme: "light dark",
|
|
43
|
+
color: "CanvasText",
|
|
44
|
+
background: "Canvas"
|
|
45
|
+
},
|
|
46
|
+
heading: {
|
|
47
|
+
margin: 0,
|
|
48
|
+
fontSize: "0.75rem",
|
|
49
|
+
fontWeight: 600
|
|
50
|
+
},
|
|
51
|
+
code: {
|
|
52
|
+
margin: 0,
|
|
53
|
+
padding: "0.375rem",
|
|
54
|
+
maxHeight: "10rem",
|
|
55
|
+
overflow: "auto",
|
|
56
|
+
border: "1px solid GrayText",
|
|
57
|
+
borderRadius: "0.375rem",
|
|
58
|
+
font: "0.6875rem/1.4 ui-monospace, monospace",
|
|
59
|
+
whiteSpace: "pre-wrap",
|
|
60
|
+
wordBreak: "break-word"
|
|
61
|
+
},
|
|
62
|
+
tool: {
|
|
63
|
+
display: "block",
|
|
64
|
+
width: "100%",
|
|
65
|
+
padding: "0.25rem 0.375rem",
|
|
66
|
+
font: "inherit",
|
|
67
|
+
textAlign: "left",
|
|
68
|
+
cursor: "pointer"
|
|
69
|
+
},
|
|
70
|
+
input: {
|
|
71
|
+
width: "100%",
|
|
72
|
+
minHeight: "4rem",
|
|
73
|
+
boxSizing: "border-box",
|
|
74
|
+
font: "0.6875rem/1.4 ui-monospace, monospace"
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
function describeAnnotations(annotations) {
|
|
78
|
+
const labels = [
|
|
79
|
+
annotations?.readOnlyHint ? "read-only" : void 0,
|
|
80
|
+
annotations?.consequentialHint ? "consequential" : void 0,
|
|
81
|
+
annotations?.untrustedContentHint ? "untrusted output" : void 0
|
|
82
|
+
].filter(Boolean);
|
|
83
|
+
return labels.length > 0 ? labels.join(", ") : "no annotations";
|
|
84
|
+
}
|
|
85
|
+
function format(value) {
|
|
86
|
+
return typeof value === "string" ? value : JSON.stringify(value, null, 2);
|
|
87
|
+
}
|
|
88
|
+
function DevtoolsPanel() {
|
|
89
|
+
const [open, setOpen] = useState(false);
|
|
90
|
+
const [context, setContext] = useState();
|
|
91
|
+
const [status, setStatus] = useState("unavailable");
|
|
92
|
+
const [tools, setTools] = useState([]);
|
|
93
|
+
const [selectedName, setSelectedName] = useState();
|
|
94
|
+
const [input, setInput] = useState("{}");
|
|
95
|
+
const [output, setOutput] = useState();
|
|
96
|
+
const [history, setHistory] = useState([]);
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
const controller = new AbortController();
|
|
99
|
+
whenModelContextAvailable(controller.signal).then((available) => {
|
|
100
|
+
if (!available) return;
|
|
101
|
+
const refresh = () => {
|
|
102
|
+
available.getTools().then((registered) => {
|
|
103
|
+
if (!controller.signal.aborted) setTools(registered);
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
available.addEventListener("toolchange", refresh, { signal: controller.signal });
|
|
107
|
+
setContext(available);
|
|
108
|
+
setStatus(getWebMCPStatus());
|
|
109
|
+
refresh();
|
|
110
|
+
});
|
|
111
|
+
const unsubscribe = subscribeToToolCalls((record) => setHistory((records) => [record, ...records].slice(0, HISTORY_LIMIT)));
|
|
112
|
+
return () => {
|
|
113
|
+
controller.abort();
|
|
114
|
+
unsubscribe();
|
|
115
|
+
};
|
|
116
|
+
}, []);
|
|
117
|
+
const selected = tools.find((tool) => tool.name === selectedName);
|
|
118
|
+
const run = async (tool) => {
|
|
119
|
+
if (!context) return;
|
|
120
|
+
try {
|
|
121
|
+
setOutput("Running…");
|
|
122
|
+
setOutput(await context.executeTool(tool, JSON.parse(input)));
|
|
123
|
+
} catch (error) {
|
|
124
|
+
setOutput(error instanceof Error ? `${error.name}: ${error.message}` : String(error));
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("button", {
|
|
128
|
+
type: "button",
|
|
129
|
+
"data-actionmcp-devtools": "toggle",
|
|
130
|
+
"aria-expanded": open,
|
|
131
|
+
style: styles.toggle,
|
|
132
|
+
onClick: () => setOpen((value) => !value),
|
|
133
|
+
children: ["Agent tools · ", tools.length]
|
|
134
|
+
}), open ? /* @__PURE__ */ jsxs("section", {
|
|
135
|
+
"data-actionmcp-devtools": "panel",
|
|
136
|
+
"aria-label": "Agent tools devtools",
|
|
137
|
+
style: styles.panel,
|
|
138
|
+
children: [
|
|
139
|
+
/* @__PURE__ */ jsx("p", {
|
|
140
|
+
"data-actionmcp-devtools": "status",
|
|
141
|
+
style: styles.heading,
|
|
142
|
+
children: STATUS_LABELS[status]
|
|
143
|
+
}),
|
|
144
|
+
/* @__PURE__ */ jsx("h2", {
|
|
145
|
+
style: styles.heading,
|
|
146
|
+
children: "Registered tools"
|
|
147
|
+
}),
|
|
148
|
+
tools.length === 0 ? /* @__PURE__ */ jsx("p", {
|
|
149
|
+
style: { margin: 0 },
|
|
150
|
+
children: "No tools are registered on this page."
|
|
151
|
+
}) : null,
|
|
152
|
+
tools.map((tool) => /* @__PURE__ */ jsxs("button", {
|
|
153
|
+
type: "button",
|
|
154
|
+
"aria-pressed": tool.name === selectedName,
|
|
155
|
+
style: styles.tool,
|
|
156
|
+
onClick: () => {
|
|
157
|
+
setSelectedName(tool.name);
|
|
158
|
+
setOutput(void 0);
|
|
159
|
+
},
|
|
160
|
+
children: [
|
|
161
|
+
/* @__PURE__ */ jsx("strong", { children: tool.name }),
|
|
162
|
+
" · ",
|
|
163
|
+
describeAnnotations(tool.annotations)
|
|
164
|
+
]
|
|
165
|
+
}, `${tool.origin}:${tool.name}`)),
|
|
166
|
+
selected ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
167
|
+
/* @__PURE__ */ jsx("p", {
|
|
168
|
+
style: { margin: 0 },
|
|
169
|
+
children: selected.description
|
|
170
|
+
}),
|
|
171
|
+
/* @__PURE__ */ jsx("pre", {
|
|
172
|
+
style: styles.code,
|
|
173
|
+
children: format(selected.inputSchema ?? "No input schema")
|
|
174
|
+
}),
|
|
175
|
+
/* @__PURE__ */ jsxs("label", { children: [/* @__PURE__ */ jsx("span", {
|
|
176
|
+
style: styles.heading,
|
|
177
|
+
children: "Input (JSON)"
|
|
178
|
+
}), /* @__PURE__ */ jsx("textarea", {
|
|
179
|
+
value: input,
|
|
180
|
+
spellCheck: false,
|
|
181
|
+
style: styles.input,
|
|
182
|
+
onChange: (event) => setInput(event.target.value)
|
|
183
|
+
})] }),
|
|
184
|
+
/* @__PURE__ */ jsxs("button", {
|
|
185
|
+
type: "button",
|
|
186
|
+
"data-actionmcp-devtools": "run",
|
|
187
|
+
onClick: () => void run(selected),
|
|
188
|
+
children: ["Run ", selected.name]
|
|
189
|
+
}),
|
|
190
|
+
output === void 0 ? null : /* @__PURE__ */ jsx("pre", {
|
|
191
|
+
"data-actionmcp-devtools": "output",
|
|
192
|
+
style: styles.code,
|
|
193
|
+
children: output
|
|
194
|
+
})
|
|
195
|
+
] }) : null,
|
|
196
|
+
/* @__PURE__ */ jsx("h2", {
|
|
197
|
+
style: styles.heading,
|
|
198
|
+
children: "Call history"
|
|
199
|
+
}),
|
|
200
|
+
history.length === 0 ? /* @__PURE__ */ jsx("p", {
|
|
201
|
+
style: { margin: 0 },
|
|
202
|
+
children: "No calls yet."
|
|
203
|
+
}) : null,
|
|
204
|
+
history.map((record) => /* @__PURE__ */ jsxs("details", {
|
|
205
|
+
"data-actionmcp-devtools": "call",
|
|
206
|
+
children: [/* @__PURE__ */ jsxs("summary", { children: [
|
|
207
|
+
record.name,
|
|
208
|
+
" · ",
|
|
209
|
+
record.durationMs,
|
|
210
|
+
" ms · confirmation ",
|
|
211
|
+
record.confirmation
|
|
212
|
+
] }), /* @__PURE__ */ jsx("pre", {
|
|
213
|
+
style: styles.code,
|
|
214
|
+
children: format({
|
|
215
|
+
input: record.input,
|
|
216
|
+
result: record.result
|
|
217
|
+
})
|
|
218
|
+
})]
|
|
219
|
+
}, `${record.startedAt}:${record.name}`))
|
|
220
|
+
]
|
|
221
|
+
}) : null] });
|
|
222
|
+
}
|
|
223
|
+
export { DevtoolsPanel };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
import { AgentToolsHooks } from "./context.js";
|
|
3
|
+
export interface AgentToolsProviderProps extends AgentToolsHooks {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
}
|
|
6
|
+
export declare function AgentToolsProvider({ children, onBeforeExecute, onAfterExecute, confirmComponent }: AgentToolsProviderProps): ReactNode;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { AgentToolsContext } from "./context.js";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
// src/react/Provider.tsx
|
|
6
|
+
function AgentToolsProvider({ children, onBeforeExecute, onAfterExecute, confirmComponent }) {
|
|
7
|
+
const hooks = useMemo(() => ({
|
|
8
|
+
onBeforeExecute,
|
|
9
|
+
onAfterExecute,
|
|
10
|
+
confirmComponent
|
|
11
|
+
}), [
|
|
12
|
+
onBeforeExecute,
|
|
13
|
+
onAfterExecute,
|
|
14
|
+
confirmComponent
|
|
15
|
+
]);
|
|
16
|
+
return /* @__PURE__ */ jsx(AgentToolsContext, {
|
|
17
|
+
value: hooks,
|
|
18
|
+
children
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export { AgentToolsProvider };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { recoverRedirect } from "../next/redirect.js";
|
|
3
|
+
import { useAgentTools } from "./useAgentTools.js";
|
|
4
|
+
// src/react/Registrar.tsx
|
|
5
|
+
function Registrar({ tools, polyfill, exposedTo }) {
|
|
6
|
+
return useAgentTools(tools, {
|
|
7
|
+
polyfill,
|
|
8
|
+
exposedTo,
|
|
9
|
+
recoverError: recoverRedirect
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export { Registrar };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ComponentType } from "react";
|
|
2
|
+
import { ToolCall } from "../core/types.js";
|
|
3
|
+
export interface ConfirmProps {
|
|
4
|
+
call: ToolCall;
|
|
5
|
+
title?: string | undefined;
|
|
6
|
+
description: string;
|
|
7
|
+
onConfirm: () => void;
|
|
8
|
+
onCancel: () => void;
|
|
9
|
+
}
|
|
10
|
+
export interface AgentToolsHooks {
|
|
11
|
+
onBeforeExecute?: ((call: ToolCall) => boolean | Promise<boolean>) | undefined;
|
|
12
|
+
onAfterExecute?: ((call: ToolCall, result: unknown) => void) | undefined;
|
|
13
|
+
confirmComponent?: ComponentType<ConfirmProps> | undefined;
|
|
14
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useContext, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { executeTool } from "../core/execute.js";
|
|
4
|
+
import { registerTools, resolveModelContext } from "../core/webmcp.js";
|
|
5
|
+
import { ConfirmDialog } from "./Confirm.js";
|
|
6
|
+
import { AgentToolsContext } from "./context.js";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
// src/react/useAgentTools.tsx
|
|
9
|
+
function useAgentTools(tools, registration) {
|
|
10
|
+
const { polyfill = false, exposedTo, recoverError } = registration;
|
|
11
|
+
const hooks = useContext(AgentToolsContext);
|
|
12
|
+
const [pending, setPending] = useState([]);
|
|
13
|
+
const unsettled = useRef(/* @__PURE__ */ new Set());
|
|
14
|
+
const confirm = useCallback((call, tool, signal) => new Promise((resolve) => {
|
|
15
|
+
const confirmation = {
|
|
16
|
+
call,
|
|
17
|
+
tool,
|
|
18
|
+
settle: (accepted) => {
|
|
19
|
+
unsettled.current.delete(confirmation);
|
|
20
|
+
setPending((queue) => queue.filter((entry) => entry !== confirmation));
|
|
21
|
+
resolve(accepted);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
unsettled.current.add(confirmation);
|
|
25
|
+
signal.addEventListener("abort", () => confirmation.settle(false), { once: true });
|
|
26
|
+
setPending((queue) => [...queue, confirmation]);
|
|
27
|
+
}), []);
|
|
28
|
+
const latest = useRef({
|
|
29
|
+
tools,
|
|
30
|
+
hooks,
|
|
31
|
+
recoverError
|
|
32
|
+
});
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
latest.current = {
|
|
35
|
+
tools,
|
|
36
|
+
hooks,
|
|
37
|
+
recoverError
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
const registrationKey = JSON.stringify([
|
|
41
|
+
tools,
|
|
42
|
+
exposedTo,
|
|
43
|
+
polyfill
|
|
44
|
+
]);
|
|
45
|
+
const [stable, setStable] = useState({
|
|
46
|
+
key: registrationKey,
|
|
47
|
+
tools,
|
|
48
|
+
exposedTo,
|
|
49
|
+
polyfill
|
|
50
|
+
});
|
|
51
|
+
if (stable.key !== registrationKey) setStable({
|
|
52
|
+
key: registrationKey,
|
|
53
|
+
tools,
|
|
54
|
+
exposedTo,
|
|
55
|
+
polyfill
|
|
56
|
+
});
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
const { signal } = controller;
|
|
60
|
+
const definitions = stable.tools.map(({ action: _action, confirm: _confirm, adapter: _adapter, ...definition }, index) => ({
|
|
61
|
+
...definition,
|
|
62
|
+
execute: (input, options) => {
|
|
63
|
+
const current = latest.current;
|
|
64
|
+
const tool = current.tools[index];
|
|
65
|
+
if (!tool) throw new DOMException("The tool is no longer registered.", "InvalidStateError");
|
|
66
|
+
return executeTool(tool, input, options.signal, {
|
|
67
|
+
confirm,
|
|
68
|
+
onBeforeExecute: current.hooks.onBeforeExecute,
|
|
69
|
+
onAfterExecute: current.hooks.onAfterExecute,
|
|
70
|
+
recoverError: current.recoverError
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}));
|
|
74
|
+
resolveModelContext(stable.polyfill, signal).then((context) => {
|
|
75
|
+
if (!context || signal.aborted) return;
|
|
76
|
+
return registerTools(context, definitions, {
|
|
77
|
+
signal,
|
|
78
|
+
exposedTo: stable.exposedTo,
|
|
79
|
+
onCollision: (requested, registered) => {
|
|
80
|
+
if (process.env.NODE_ENV !== "production") console.warn(`actionmcp: the tool name "${requested}" is taken, so the tool is registered as "${registered}".`);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
return () => controller.abort();
|
|
85
|
+
}, [stable, confirm]);
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
const confirmations = unsettled.current;
|
|
88
|
+
return () => {
|
|
89
|
+
for (const confirmation of confirmations) confirmation.settle(false);
|
|
90
|
+
};
|
|
91
|
+
}, []);
|
|
92
|
+
const current = pending[0];
|
|
93
|
+
if (!current) return null;
|
|
94
|
+
const Confirm = hooks.confirmComponent ?? ConfirmDialog;
|
|
95
|
+
return /* @__PURE__ */ jsx(Confirm, {
|
|
96
|
+
call: current.call,
|
|
97
|
+
title: current.tool.title,
|
|
98
|
+
description: current.tool.description,
|
|
99
|
+
onConfirm: () => current.settle(true),
|
|
100
|
+
onCancel: () => current.settle(false)
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
export { useAgentTools };
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import { AgentResult } from "../core/result.js";
|
|
3
|
+
import { ConfirmPolicy } from "../core/types.js";
|
|
4
|
+
export declare const AGENT_META: unique symbol;
|
|
5
|
+
export interface ToolBehaviorOptions {
|
|
6
|
+
name?: string;
|
|
7
|
+
title?: string;
|
|
8
|
+
description: string;
|
|
9
|
+
readOnly?: boolean;
|
|
10
|
+
consequential?: boolean;
|
|
11
|
+
untrustedOutput?: boolean;
|
|
12
|
+
confirm?: ConfirmPolicy;
|
|
13
|
+
}
|
|
14
|
+
export interface AgentToolOptions extends ToolBehaviorOptions {
|
|
15
|
+
input?: StandardSchemaV1 | undefined;
|
|
16
|
+
output?: StandardSchemaV1 | undefined;
|
|
17
|
+
}
|
|
18
|
+
export interface AgentToolMeta {
|
|
19
|
+
readonly options: AgentToolOptions;
|
|
20
|
+
}
|
|
21
|
+
export interface AgentToolMarker {
|
|
22
|
+
readonly [AGENT_META]: AgentToolMeta;
|
|
23
|
+
}
|
|
24
|
+
export type AgentTool<Args extends unknown[] = never[], Data = unknown> = ((...args: Args) => Promise<AgentResult<Data>>) & AgentToolMarker;
|
|
25
|
+
type InputOf<Schema extends StandardSchemaV1> = StandardSchemaV1.InferInput<Schema>;
|
|
26
|
+
type OutputOf<Schema extends StandardSchemaV1> = StandardSchemaV1.InferOutput<Schema>;
|
|
27
|
+
export declare function agent<Input extends StandardSchemaV1, Output extends StandardSchemaV1>(options: ToolBehaviorOptions & {
|
|
28
|
+
input: Input;
|
|
29
|
+
output: Output;
|
|
30
|
+
}, handler: (input: OutputOf<Input>) => Promise<InputOf<Output>>): AgentTool<[input: InputOf<Input>], OutputOf<Output>>;
|
|
31
|
+
export declare function agent<Input extends StandardSchemaV1, Data>(options: ToolBehaviorOptions & {
|
|
32
|
+
input: Input;
|
|
33
|
+
output?: undefined;
|
|
34
|
+
}, handler: (input: OutputOf<Input>) => Promise<Data>): AgentTool<[input: InputOf<Input>], Data>;
|
|
35
|
+
export declare function agent<Output extends StandardSchemaV1>(options: ToolBehaviorOptions & {
|
|
36
|
+
input?: undefined;
|
|
37
|
+
output: Output;
|
|
38
|
+
}, handler: () => Promise<InputOf<Output>>): AgentTool<[], OutputOf<Output>>;
|
|
39
|
+
export declare function agent<Data>(options: ToolBehaviorOptions & {
|
|
40
|
+
input?: undefined;
|
|
41
|
+
output?: undefined;
|
|
42
|
+
}, handler: () => Promise<Data>): AgentTool<[], Data>;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isNonEmptyString } from "../core/guards.js";
|
|
2
|
+
import { AgentToolError, GENERIC_FAILURE_MESSAGE, INVALID_INPUT_MESSAGE, INVALID_OUTPUT_MESSAGE, fail, isFrameworkSignal, succeed } from "../core/result.js";
|
|
3
|
+
import { validateWithSchema } from "../core/schema.js";
|
|
4
|
+
// src/server/agent.ts
|
|
5
|
+
const AGENT_META = Symbol.for("actionmcp.meta");
|
|
6
|
+
function agent(options, handler) {
|
|
7
|
+
if (!isNonEmptyString(options.description)) throw new TypeError("agent() requires a non-empty description.");
|
|
8
|
+
const execute = async (input) => runHandler(options, handler, input);
|
|
9
|
+
return Object.defineProperty(execute, AGENT_META, { value: { options } });
|
|
10
|
+
}
|
|
11
|
+
function readAgentMeta(value) {
|
|
12
|
+
return typeof value === "function" ? value[AGENT_META] : void 0;
|
|
13
|
+
}
|
|
14
|
+
async function runHandler(options, handler, rawInput) {
|
|
15
|
+
let input;
|
|
16
|
+
if (options.input) {
|
|
17
|
+
const validation = await validateWithSchema(options.input, rawInput);
|
|
18
|
+
if (!validation.ok) return fail("invalid_input", INVALID_INPUT_MESSAGE, validation.issues);
|
|
19
|
+
input = validation.value;
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const data = await handler(input);
|
|
23
|
+
if (!options.output) return succeed(data);
|
|
24
|
+
const validation = await validateWithSchema(options.output, data);
|
|
25
|
+
return validation.ok ? succeed(validation.value) : fail("failed", INVALID_OUTPUT_MESSAGE);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (isFrameworkSignal(error)) throw error;
|
|
28
|
+
if (error instanceof AgentToolError) return fail(error.code, error.message);
|
|
29
|
+
console.error(error);
|
|
30
|
+
const exposeMessage = process.env.NODE_ENV !== "production" && error instanceof Error;
|
|
31
|
+
return fail("failed", exposeMessage ? error.message : GENERIC_FAILURE_MESSAGE);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export { AGENT_META, agent, readAgentMeta };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AgentTool, AgentToolMarker, AgentToolOptions, ToolBehaviorOptions, agent } from "./agent.js";
|
|
2
|
+
import { AdaptedAction, AgentToolDefinition, AgentToolRegistry, JsdocToolEntry, RegistrySources, createRegistry, defineAgentTools, fromFormAction, fromFormDataAction, fromSafeAction, tool } from "./registry.js";
|
|
3
|
+
import { AgentError, AgentErrorCode, AgentResult, AgentToolError } from "../core/result.js";
|
|
4
|
+
import { AgentSchema, SchemaIssue } from "../core/schema.js";
|
|
5
|
+
import { ConfirmPolicy, JsonSchema, RouteEntry, ToolAdapter, ToolAnnotations } from "../core/types.js";
|
|
6
|
+
export { type AdaptedAction, type AgentError, type AgentErrorCode, type AgentResult, type AgentSchema, type AgentTool, type AgentToolDefinition, AgentToolError, type AgentToolMarker, type AgentToolOptions, type AgentToolRegistry, type ConfirmPolicy, type JsdocToolEntry, type JsonSchema, type RegistrySources, type RouteEntry, type SchemaIssue, type ToolAdapter, type ToolAnnotations, type ToolBehaviorOptions, agent, createRegistry, defineAgentTools, fromFormAction, fromFormDataAction, fromSafeAction, tool };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { agent } from "./agent.js";
|
|
2
|
+
import { createRegistry, defineAgentTools, fromFormAction, fromFormDataAction, fromSafeAction, tool } from "./registry.js";
|
|
3
|
+
import { AgentToolError } from "../core/result.js";
|
|
4
|
+
export { AgentToolError, agent, createRegistry, defineAgentTools, fromFormAction, fromFormDataAction, fromSafeAction, tool };
|