actionmcp 0.0.0-stage → 0.2.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 +136 -0
- package/dist/analyzer/explicit.js +128 -0
- package/dist/analyzer/index.js +191 -0
- package/dist/analyzer/jsdoc.js +32 -0
- package/dist/analyzer/options.js +65 -0
- package/dist/analyzer/routes.js +105 -0
- package/dist/analyzer/scan.js +292 -0
- package/dist/analyzer/source.js +100 -0
- package/dist/analyzer/toolchain.js +27 -0
- package/dist/analyzer/types.js +141 -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 +95 -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 +62 -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 +149 -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 +23 -0
- package/dist/react/Registrar.js +12 -0
- package/dist/react/context.d.ts +24 -0
- package/dist/react/context.js +5 -0
- package/dist/react/useAgentTools.js +105 -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 +102 -4
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useRouter } from "next/navigation";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { NAVIGATE_TOOL_NAME } from "../core/names.js";
|
|
5
|
+
import { fail, succeed } from "../core/result.js";
|
|
6
|
+
import { useAgentTools } from "../react/useAgentTools.js";
|
|
7
|
+
import { NAVIGATE_DESCRIPTION, buildHref, navigateInputSchema } from "./routes.js";
|
|
8
|
+
// src/next/NavigateTool.tsx
|
|
9
|
+
function NavigateTool({ routes, name = NAVIGATE_TOOL_NAME, polyfill, exposedTo }) {
|
|
10
|
+
const router = useRouter();
|
|
11
|
+
const tools = useMemo(() => [{
|
|
12
|
+
name,
|
|
13
|
+
description: NAVIGATE_DESCRIPTION,
|
|
14
|
+
inputSchema: navigateInputSchema(routes),
|
|
15
|
+
annotations: {
|
|
16
|
+
readOnlyHint: false,
|
|
17
|
+
consequentialHint: false,
|
|
18
|
+
untrustedContentHint: false
|
|
19
|
+
},
|
|
20
|
+
confirm: "never",
|
|
21
|
+
action: async (input) => {
|
|
22
|
+
try {
|
|
23
|
+
const url = buildHref(routes, input);
|
|
24
|
+
router.push(url);
|
|
25
|
+
return succeed({ url });
|
|
26
|
+
} catch (error) {
|
|
27
|
+
return fail("invalid_input", error instanceof Error ? error.message : String(error));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}], [
|
|
31
|
+
routes,
|
|
32
|
+
name,
|
|
33
|
+
router
|
|
34
|
+
]);
|
|
35
|
+
return useAgentTools(tools, {
|
|
36
|
+
polyfill,
|
|
37
|
+
exposedTo
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
export { NavigateTool };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/next/config-store.ts
|
|
2
|
+
const RECORDED_CONFIG = Symbol.for("actionmcp.config");
|
|
3
|
+
function recordConfig(config) {
|
|
4
|
+
globalThis[RECORDED_CONFIG] = config;
|
|
5
|
+
}
|
|
6
|
+
function readRecordedConfig() {
|
|
7
|
+
return globalThis[RECORDED_CONFIG];
|
|
8
|
+
}
|
|
9
|
+
function clearRecordedConfig() {
|
|
10
|
+
delete globalThis[RECORDED_CONFIG];
|
|
11
|
+
}
|
|
12
|
+
export { clearRecordedConfig, readRecordedConfig, recordConfig };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/next/redirect.ts
|
|
2
|
+
const REDIRECT_DIGEST = /^NEXT_REDIRECT;[^;]*;(.*);\d+;$/;
|
|
3
|
+
function readRedirect(error) {
|
|
4
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
5
|
+
const { digest } = error;
|
|
6
|
+
return typeof digest === "string" ? REDIRECT_DIGEST.exec(digest)?.[1] : void 0;
|
|
7
|
+
}
|
|
8
|
+
function recoverRedirect(error) {
|
|
9
|
+
const navigatedTo = readRedirect(error);
|
|
10
|
+
return navigatedTo === void 0 ? void 0 : {
|
|
11
|
+
ok: true,
|
|
12
|
+
data: { navigatedTo }
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export { readRedirect, recoverRedirect };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createMatcher } from "../core/match.js";
|
|
2
|
+
// src/next/routes.ts
|
|
3
|
+
const DOT_SEGMENTS = /* @__PURE__ */ new Set([".", ".."]);
|
|
4
|
+
const NAVIGATE_DESCRIPTION = "Navigate this app to one of its pages. Available tools may change after navigating.";
|
|
5
|
+
function selectRoutes(routes, options) {
|
|
6
|
+
if (options === true || !options.routes) return routes.filter((route) => route.params.length === 0 || route.staticParams === true);
|
|
7
|
+
const isAllowed = createMatcher(options.routes);
|
|
8
|
+
return routes.filter((route) => isAllowed(route.pattern));
|
|
9
|
+
}
|
|
10
|
+
function navigateInputSchema(routes) {
|
|
11
|
+
return {
|
|
12
|
+
type: "object",
|
|
13
|
+
properties: {
|
|
14
|
+
route: {
|
|
15
|
+
type: "string",
|
|
16
|
+
enum: routes.map((route) => route.pattern)
|
|
17
|
+
},
|
|
18
|
+
params: {
|
|
19
|
+
type: "object",
|
|
20
|
+
additionalProperties: { type: "string" }
|
|
21
|
+
},
|
|
22
|
+
query: {
|
|
23
|
+
type: "object",
|
|
24
|
+
additionalProperties: { type: "string" }
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
required: ["route"]
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function buildHref(routes, input) {
|
|
31
|
+
const route = routes.find((candidate) => candidate.pattern === input.route);
|
|
32
|
+
if (!route) throw new RangeError(`Unknown route "${String(input.route)}".`);
|
|
33
|
+
const params = input.params ?? {};
|
|
34
|
+
const path = route.pattern.split("/").flatMap((segment) => resolveSegment(segment, params)).join("/") || "/";
|
|
35
|
+
const query = new URLSearchParams(input.query ?? {}).toString();
|
|
36
|
+
return query ? `${path}?${query}` : path;
|
|
37
|
+
}
|
|
38
|
+
function resolveSegment(segment, params) {
|
|
39
|
+
const optionalCatchAll = /^\[\[\.\.\.(.+)\]\]$/.exec(segment)?.[1];
|
|
40
|
+
if (optionalCatchAll !== void 0) return splitPath(optionalCatchAll, params[optionalCatchAll] ?? "");
|
|
41
|
+
const catchAll = /^\[\.\.\.(.+)\]$/.exec(segment)?.[1];
|
|
42
|
+
if (catchAll !== void 0) {
|
|
43
|
+
const parts = splitPath(catchAll, requireParam(params, catchAll));
|
|
44
|
+
if (parts.length === 0) throw new RangeError(`Missing route parameter "${catchAll}".`);
|
|
45
|
+
return parts;
|
|
46
|
+
}
|
|
47
|
+
const dynamic = /^\[(.+)\]$/.exec(segment)?.[1];
|
|
48
|
+
return [dynamic === void 0 ? segment : encodeSegment(dynamic, requireParam(params, dynamic))];
|
|
49
|
+
}
|
|
50
|
+
function requireParam(params, name) {
|
|
51
|
+
const value = params[name];
|
|
52
|
+
if (typeof value !== "string" || value.length === 0) throw new RangeError(`Missing route parameter "${name}".`);
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
function splitPath(name, value) {
|
|
56
|
+
return value.split("/").filter(Boolean).map((part) => encodeSegment(name, part));
|
|
57
|
+
}
|
|
58
|
+
function encodeSegment(name, value) {
|
|
59
|
+
if (DOT_SEGMENTS.has(value)) throw new RangeError(`The route parameter "${name}" cannot contain the path segment "${value}".`);
|
|
60
|
+
return encodeURIComponent(value);
|
|
61
|
+
}
|
|
62
|
+
export { NAVIGATE_DESCRIPTION, buildHref, navigateInputSchema, selectRoutes };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
import { NavigateOptions } from "../next/routes.js";
|
|
3
|
+
import { AgentToolRegistry } from "../server/registry.js";
|
|
4
|
+
export interface AgentToolsProps {
|
|
5
|
+
registry?: AgentToolRegistry;
|
|
6
|
+
include?: string[];
|
|
7
|
+
exclude?: string[];
|
|
8
|
+
prefix?: string;
|
|
9
|
+
navigate?: boolean | NavigateOptions;
|
|
10
|
+
exposedTo?: string[];
|
|
11
|
+
polyfill?: boolean;
|
|
12
|
+
originTrialToken?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function AgentTools({ registry, include, exclude, prefix, navigate, exposedTo, polyfill, originTrialToken }: AgentToolsProps): ReactNode;
|
|
@@ -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,149 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment, useEffect, useId, useRef } from "react";
|
|
3
|
+
import { isRecord } from "../core/guards.js";
|
|
4
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
// src/react/Confirm.tsx
|
|
6
|
+
const TOOL_PLACEHOLDER = "{tool}";
|
|
7
|
+
const DEFAULT_CONFIRM_LABELS = {
|
|
8
|
+
title: `Allow “${TOOL_PLACEHOLDER}”?`,
|
|
9
|
+
intro: "An agent wants to run this action.",
|
|
10
|
+
input: "Action input",
|
|
11
|
+
allow: "Allow",
|
|
12
|
+
cancel: "Cancel"
|
|
13
|
+
};
|
|
14
|
+
const dialogStyle = {
|
|
15
|
+
maxWidth: "min(28rem, calc(100vw - 2rem))",
|
|
16
|
+
padding: "1.25rem",
|
|
17
|
+
border: "1px solid GrayText",
|
|
18
|
+
borderRadius: "0.75rem",
|
|
19
|
+
font: "0.875rem/1.5 system-ui, sans-serif",
|
|
20
|
+
colorScheme: "light dark",
|
|
21
|
+
color: "CanvasText",
|
|
22
|
+
background: "Canvas"
|
|
23
|
+
};
|
|
24
|
+
const inputStyle = {
|
|
25
|
+
maxHeight: "12rem",
|
|
26
|
+
margin: "0.75rem 0 0",
|
|
27
|
+
padding: "0.5rem",
|
|
28
|
+
overflow: "auto",
|
|
29
|
+
border: "1px solid GrayText",
|
|
30
|
+
borderRadius: "0.375rem",
|
|
31
|
+
font: "0.75rem/1.4 ui-monospace, monospace",
|
|
32
|
+
whiteSpace: "pre-wrap",
|
|
33
|
+
wordBreak: "break-word"
|
|
34
|
+
};
|
|
35
|
+
const fieldsStyle = {
|
|
36
|
+
display: "grid",
|
|
37
|
+
gridTemplateColumns: "auto 1fr",
|
|
38
|
+
gap: "0.25rem 0.75rem",
|
|
39
|
+
margin: 0
|
|
40
|
+
};
|
|
41
|
+
const fieldNameStyle = { fontWeight: 600 };
|
|
42
|
+
const fieldValueStyle = { margin: 0 };
|
|
43
|
+
const actionsStyle = {
|
|
44
|
+
display: "flex",
|
|
45
|
+
justifyContent: "flex-end",
|
|
46
|
+
gap: "0.5rem",
|
|
47
|
+
marginTop: "0.75rem"
|
|
48
|
+
};
|
|
49
|
+
const buttonStyle = {
|
|
50
|
+
padding: "0.375rem 0.875rem",
|
|
51
|
+
font: "inherit",
|
|
52
|
+
cursor: "pointer"
|
|
53
|
+
};
|
|
54
|
+
function formatValue(value) {
|
|
55
|
+
return typeof value === "string" ? value : JSON.stringify(value, null, 2);
|
|
56
|
+
}
|
|
57
|
+
function fieldLabel(name, inputSchema) {
|
|
58
|
+
const properties = inputSchema?.properties;
|
|
59
|
+
const property = isRecord(properties) ? properties[name] : void 0;
|
|
60
|
+
return isRecord(property) && typeof property.title === "string" && property.title.trim() ? property.title : name;
|
|
61
|
+
}
|
|
62
|
+
function InputSummary({ input, inputSchema }) {
|
|
63
|
+
if (!isRecord(input)) return formatValue(input);
|
|
64
|
+
return /* @__PURE__ */ jsx("dl", {
|
|
65
|
+
style: fieldsStyle,
|
|
66
|
+
children: Object.entries(input).map(([name, value]) => /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("dt", {
|
|
67
|
+
style: fieldNameStyle,
|
|
68
|
+
children: fieldLabel(name, inputSchema)
|
|
69
|
+
}), /* @__PURE__ */ jsx("dd", {
|
|
70
|
+
style: fieldValueStyle,
|
|
71
|
+
children: formatValue(value)
|
|
72
|
+
})] }, name))
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function hasInput(input) {
|
|
76
|
+
return isRecord(input) ? Object.keys(input).length > 0 : input !== void 0 && input !== null;
|
|
77
|
+
}
|
|
78
|
+
function ConfirmDialog({ call, title, description, inputSchema, labels, onConfirm, onCancel }) {
|
|
79
|
+
const dialog = useRef(null);
|
|
80
|
+
const cancelButton = useRef(null);
|
|
81
|
+
const id = useId();
|
|
82
|
+
const text = {
|
|
83
|
+
...DEFAULT_CONFIRM_LABELS,
|
|
84
|
+
...labels
|
|
85
|
+
};
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
const element = dialog.current;
|
|
88
|
+
if (element && !element.open) element.showModal();
|
|
89
|
+
cancelButton.current?.focus();
|
|
90
|
+
return () => element?.close();
|
|
91
|
+
}, []);
|
|
92
|
+
return /* @__PURE__ */ jsxs("dialog", {
|
|
93
|
+
ref: dialog,
|
|
94
|
+
"data-actionmcp-confirm": call.name,
|
|
95
|
+
"aria-labelledby": `${id}-title`,
|
|
96
|
+
"aria-describedby": `${id}-description`,
|
|
97
|
+
style: dialogStyle,
|
|
98
|
+
onCancel: (event) => {
|
|
99
|
+
event.preventDefault();
|
|
100
|
+
onCancel();
|
|
101
|
+
},
|
|
102
|
+
children: [
|
|
103
|
+
/* @__PURE__ */ jsx("h2", {
|
|
104
|
+
id: `${id}-title`,
|
|
105
|
+
style: {
|
|
106
|
+
margin: 0,
|
|
107
|
+
fontSize: "1rem"
|
|
108
|
+
},
|
|
109
|
+
children: text.title.replaceAll(TOOL_PLACEHOLDER, title ?? call.name)
|
|
110
|
+
}),
|
|
111
|
+
/* @__PURE__ */ jsxs("p", {
|
|
112
|
+
id: `${id}-description`,
|
|
113
|
+
style: { margin: "0.5rem 0 0" },
|
|
114
|
+
children: [
|
|
115
|
+
text.intro,
|
|
116
|
+
" ",
|
|
117
|
+
description
|
|
118
|
+
]
|
|
119
|
+
}),
|
|
120
|
+
hasInput(call.input) ? /* @__PURE__ */ jsx("div", {
|
|
121
|
+
tabIndex: 0,
|
|
122
|
+
role: "group",
|
|
123
|
+
"aria-label": text.input,
|
|
124
|
+
"data-actionmcp-confirm-input": "",
|
|
125
|
+
style: inputStyle,
|
|
126
|
+
children: /* @__PURE__ */ jsx(InputSummary, {
|
|
127
|
+
input: call.input,
|
|
128
|
+
inputSchema
|
|
129
|
+
})
|
|
130
|
+
}) : null,
|
|
131
|
+
/* @__PURE__ */ jsxs("div", {
|
|
132
|
+
style: actionsStyle,
|
|
133
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
134
|
+
ref: cancelButton,
|
|
135
|
+
type: "button",
|
|
136
|
+
style: buttonStyle,
|
|
137
|
+
onClick: onCancel,
|
|
138
|
+
children: text.cancel
|
|
139
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
140
|
+
type: "button",
|
|
141
|
+
style: buttonStyle,
|
|
142
|
+
onClick: onConfirm,
|
|
143
|
+
children: text.allow
|
|
144
|
+
})]
|
|
145
|
+
})
|
|
146
|
+
]
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
export { ConfirmDialog, DEFAULT_CONFIRM_LABELS, TOOL_PLACEHOLDER };
|
|
@@ -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 as Fragment$1, 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$1, { 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$1, { 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, confirmLabels }: AgentToolsProviderProps): ReactNode;
|
|
@@ -0,0 +1,23 @@
|
|
|
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, confirmLabels }) {
|
|
7
|
+
const hooks = useMemo(() => ({
|
|
8
|
+
onBeforeExecute,
|
|
9
|
+
onAfterExecute,
|
|
10
|
+
confirmComponent,
|
|
11
|
+
confirmLabels
|
|
12
|
+
}), [
|
|
13
|
+
onBeforeExecute,
|
|
14
|
+
onAfterExecute,
|
|
15
|
+
confirmComponent,
|
|
16
|
+
confirmLabels
|
|
17
|
+
]);
|
|
18
|
+
return /* @__PURE__ */ jsx(AgentToolsContext, {
|
|
19
|
+
value: hooks,
|
|
20
|
+
children
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
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,24 @@
|
|
|
1
|
+
import { ComponentType } from "react";
|
|
2
|
+
import { JsonSchema, ToolCall } from "../core/types.js";
|
|
3
|
+
export interface ConfirmLabels {
|
|
4
|
+
title: string;
|
|
5
|
+
intro: string;
|
|
6
|
+
input: string;
|
|
7
|
+
allow: string;
|
|
8
|
+
cancel: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ConfirmProps {
|
|
11
|
+
call: ToolCall;
|
|
12
|
+
title?: string | undefined;
|
|
13
|
+
description: string;
|
|
14
|
+
inputSchema?: JsonSchema | undefined;
|
|
15
|
+
labels?: Partial<ConfirmLabels> | undefined;
|
|
16
|
+
onConfirm: () => void;
|
|
17
|
+
onCancel: () => void;
|
|
18
|
+
}
|
|
19
|
+
export interface AgentToolsHooks {
|
|
20
|
+
onBeforeExecute?: ((call: ToolCall) => boolean | Promise<boolean>) | undefined;
|
|
21
|
+
onAfterExecute?: ((call: ToolCall, result: unknown) => void) | undefined;
|
|
22
|
+
confirmComponent?: ComponentType<ConfirmProps> | undefined;
|
|
23
|
+
confirmLabels?: Partial<ConfirmLabels> | undefined;
|
|
24
|
+
}
|