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,141 @@
|
|
|
1
|
+
import { compareText } from "../core/compare.js";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { isRecord } from "../core/guards.js";
|
|
6
|
+
import { sanitizeJsonSchema } from "../core/schema.js";
|
|
7
|
+
import { SCHEMA_GENERATOR_PACKAGE, loadTypeToolchain, toolchainOrigins } from "./toolchain.js";
|
|
8
|
+
// src/analyzer/types.ts
|
|
9
|
+
const CACHE_VERSION = 1;
|
|
10
|
+
const UNREADABLE_TYPE = {
|
|
11
|
+
ok: false,
|
|
12
|
+
reason: "The type could not be read."
|
|
13
|
+
};
|
|
14
|
+
const MISSING_TOOLCHAIN = {
|
|
15
|
+
ok: false,
|
|
16
|
+
reason: `${SCHEMA_GENERATOR_PACKAGE} is not installed`,
|
|
17
|
+
missingToolchain: true
|
|
18
|
+
};
|
|
19
|
+
function inferParameterSchemas(targets, options) {
|
|
20
|
+
if (targets.length === 0) return [];
|
|
21
|
+
const cache = readCache(options.cacheFile);
|
|
22
|
+
const tsconfig = findTsconfig(options.cwd);
|
|
23
|
+
const tsconfigText = tsconfig ? readFileSync(tsconfig, "utf8") : "";
|
|
24
|
+
const hashes = targets.map((target) => hashTarget(target, tsconfigText));
|
|
25
|
+
const isCached = (target, index) => cache.get(cacheKey(target))?.hash === hashes[index];
|
|
26
|
+
const missing = targets.filter((target, index) => !isCached(target, index));
|
|
27
|
+
if (missing.length === 0) return targets.map((target) => cache.get(cacheKey(target))?.inference ?? UNREADABLE_TYPE);
|
|
28
|
+
const toolchain = loadTypeToolchain(toolchainOrigins(options.cwd));
|
|
29
|
+
if (!toolchain) return targets.map((target, index) => (isCached(target, index) ? cache.get(cacheKey(target))?.inference : void 0) ?? MISSING_TOOLCHAIN);
|
|
30
|
+
const inferred = inferWithTypeScript(toolchain, missing, options.cwd, tsconfig);
|
|
31
|
+
missing.forEach((target, index) => {
|
|
32
|
+
const inference = inferred[index];
|
|
33
|
+
const hash = hashes[targets.indexOf(target)];
|
|
34
|
+
if (inference && hash !== void 0) cache.set(cacheKey(target), {
|
|
35
|
+
hash,
|
|
36
|
+
inference
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
writeCache(options.cacheFile, cache, targets);
|
|
40
|
+
return targets.map((target) => cache.get(cacheKey(target))?.inference ?? UNREADABLE_TYPE);
|
|
41
|
+
}
|
|
42
|
+
function normalizeTypeSchema(schema) {
|
|
43
|
+
const { $schema: _dialect, definitions, ...rest } = schema;
|
|
44
|
+
const renamed = renameDefinitionReferences(isRecord(definitions) && Object.keys(definitions).length > 0 ? {
|
|
45
|
+
...rest,
|
|
46
|
+
$defs: definitions
|
|
47
|
+
} : rest);
|
|
48
|
+
const sanitized = sanitizeJsonSchema(renamed);
|
|
49
|
+
if (sanitized.type !== "object") return {
|
|
50
|
+
ok: false,
|
|
51
|
+
reason: "the first parameter is not an object type"
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
ok: true,
|
|
55
|
+
schema: sanitized
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function renameDefinitionReferences(node) {
|
|
59
|
+
if (Array.isArray(node)) return node.map(renameDefinitionReferences);
|
|
60
|
+
if (!isRecord(node)) return node;
|
|
61
|
+
return Object.fromEntries(Object.entries(node).map(([key, value]) => [key, key === "$ref" && typeof value === "string" ? value.replace(/^#\/definitions\//, "#/$defs/") : renameDefinitionReferences(value)]));
|
|
62
|
+
}
|
|
63
|
+
function inferWithTypeScript({ typescript, generator: generatorModule }, targets, cwd, tsconfig) {
|
|
64
|
+
const { SchemaGenerator, createFormatter, createParser, DEFAULT_CONFIG } = generatorModule;
|
|
65
|
+
const compilerOptions = {
|
|
66
|
+
...readCompilerOptions(typescript, cwd, tsconfig),
|
|
67
|
+
noEmit: true,
|
|
68
|
+
skipLibCheck: true
|
|
69
|
+
};
|
|
70
|
+
const program = typescript.createProgram([...new Set(targets.map((target) => target.file))], compilerOptions);
|
|
71
|
+
const config = {
|
|
72
|
+
...DEFAULT_CONFIG,
|
|
73
|
+
expose: "none",
|
|
74
|
+
topRef: false,
|
|
75
|
+
jsDoc: "extended",
|
|
76
|
+
additionalProperties: true,
|
|
77
|
+
skipTypeCheck: true
|
|
78
|
+
};
|
|
79
|
+
const generator = new SchemaGenerator(program, createParser(program, config), createFormatter(config), config);
|
|
80
|
+
return targets.map((target) => {
|
|
81
|
+
const sourceFile = program.getSourceFile(target.file);
|
|
82
|
+
const parameter = sourceFile && findParameter(typescript, sourceFile, target.parameterOffset);
|
|
83
|
+
if (!parameter?.type) return {
|
|
84
|
+
ok: false,
|
|
85
|
+
reason: "the first parameter has no type annotation"
|
|
86
|
+
};
|
|
87
|
+
try {
|
|
88
|
+
return normalizeTypeSchema(generator.createSchemaFromNodes([parameter.type]));
|
|
89
|
+
} catch (error) {
|
|
90
|
+
return {
|
|
91
|
+
ok: false,
|
|
92
|
+
reason: error instanceof Error ? error.message.split("\n")[0] ?? error.name : String(error)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function findParameter(typescript, sourceFile, offset) {
|
|
98
|
+
const search = (node) => {
|
|
99
|
+
if (offset < node.getFullStart() || offset >= node.getEnd()) return void 0;
|
|
100
|
+
if (typescript.isParameter(node) && node.getStart(sourceFile) === offset) return node;
|
|
101
|
+
return typescript.forEachChild(node, search);
|
|
102
|
+
};
|
|
103
|
+
return search(sourceFile);
|
|
104
|
+
}
|
|
105
|
+
function readCompilerOptions(typescript, cwd, tsconfig) {
|
|
106
|
+
if (!tsconfig) return {
|
|
107
|
+
strict: true,
|
|
108
|
+
target: typescript.ScriptTarget.ES2022,
|
|
109
|
+
module: typescript.ModuleKind.ESNext,
|
|
110
|
+
moduleResolution: typescript.ModuleResolutionKind.Bundler,
|
|
111
|
+
jsx: typescript.JsxEmit.Preserve
|
|
112
|
+
};
|
|
113
|
+
const { config } = typescript.readConfigFile(tsconfig, typescript.sys.readFile);
|
|
114
|
+
return typescript.parseJsonConfigFileContent(config ?? {}, typescript.sys, cwd).options;
|
|
115
|
+
}
|
|
116
|
+
function findTsconfig(cwd) {
|
|
117
|
+
const file = join(cwd, "tsconfig.json");
|
|
118
|
+
return existsSync(file) ? file : void 0;
|
|
119
|
+
}
|
|
120
|
+
function cacheKey(target) {
|
|
121
|
+
return `${target.file}#${target.parameterOffset}`;
|
|
122
|
+
}
|
|
123
|
+
function hashTarget(target, tsconfigText) {
|
|
124
|
+
return createHash("sha256").update(String(CACHE_VERSION)).update(readFileSync(target.file)).update(tsconfigText).update(String(target.parameterOffset)).digest("hex");
|
|
125
|
+
}
|
|
126
|
+
function readCache(cacheFile) {
|
|
127
|
+
if (!cacheFile || !existsSync(cacheFile)) return /* @__PURE__ */ new Map();
|
|
128
|
+
try {
|
|
129
|
+
return new Map(Object.entries(JSON.parse(readFileSync(cacheFile, "utf8"))));
|
|
130
|
+
} catch {
|
|
131
|
+
return /* @__PURE__ */ new Map();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function writeCache(cacheFile, cache, targets) {
|
|
135
|
+
if (!cacheFile) return;
|
|
136
|
+
const live = new Set(targets.map(cacheKey));
|
|
137
|
+
const entries = [...cache].filter(([key]) => live.has(key)).toSorted(([left], [right]) => compareText(left, right));
|
|
138
|
+
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
139
|
+
writeFileSync(cacheFile, `${JSON.stringify(Object.fromEntries(entries), null, 2)}\n`);
|
|
140
|
+
}
|
|
141
|
+
export { inferParameterSchemas, normalizeTypeSchema };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/dist/cli/bin.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
import { analyze } from "../analyzer/index.js";
|
|
3
|
+
import { hasErrors, sortDiagnostics } from "../analyzer/diagnostics.js";
|
|
4
|
+
import { generate } from "../generator/index.js";
|
|
5
|
+
import { watchProject } from "../generator/watch.js";
|
|
6
|
+
import { checkEnvironment } from "./environment.js";
|
|
7
|
+
import { plural, printDiagnostics, summarize, table } from "./output.js";
|
|
8
|
+
// src/cli/commands.ts
|
|
9
|
+
async function runGenerate({ config, output }, watch) {
|
|
10
|
+
const print = (result) => {
|
|
11
|
+
const { analysis, written, durationMs } = result;
|
|
12
|
+
printDiagnostics(output, analysis.diagnostics);
|
|
13
|
+
const target = relative(config.cwd, config.outDir) || ".";
|
|
14
|
+
const changes = written.length > 0 ? `wrote ${written.join(", ")}` : "no changes";
|
|
15
|
+
output.write(`actionmcp: ${plural(analysis.tools.length, "tool")}, ${plural(analysis.routes.length, "route")} in ${target} (${changes}, ${Math.round(durationMs)} ms)`);
|
|
16
|
+
};
|
|
17
|
+
const result = await generate(config);
|
|
18
|
+
print(result);
|
|
19
|
+
if (!watch) return hasErrors(result.analysis.diagnostics) ? 1 : 0;
|
|
20
|
+
watchProject(config, {
|
|
21
|
+
onResult: print,
|
|
22
|
+
onError: (error) => output.error(`actionmcp: ${String(error)}`)
|
|
23
|
+
});
|
|
24
|
+
output.write("actionmcp: watching for changes");
|
|
25
|
+
return new Promise(() => {});
|
|
26
|
+
}
|
|
27
|
+
async function runInspect({ config, output }, toolName, json) {
|
|
28
|
+
const analysis = await analyze(config);
|
|
29
|
+
const tools = toolName === void 0 ? analysis.tools : analysis.tools.filter((tool) => tool.name === toolName);
|
|
30
|
+
if (toolName !== void 0 && tools.length === 0) {
|
|
31
|
+
output.error(`actionmcp: no tool named "${toolName}". Run \`actionmcp inspect\` to list the tools.`);
|
|
32
|
+
return 1;
|
|
33
|
+
}
|
|
34
|
+
if (json) {
|
|
35
|
+
const diagnostics = toolName === void 0 ? analysis.diagnostics : analysis.diagnostics.filter((diagnostic) => diagnostic.tool === toolName);
|
|
36
|
+
output.write(JSON.stringify({
|
|
37
|
+
tools,
|
|
38
|
+
routes: analysis.routes,
|
|
39
|
+
diagnostics
|
|
40
|
+
}, null, 2));
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
if (toolName !== void 0) {
|
|
44
|
+
for (const tool of tools) describeTool(output, tool);
|
|
45
|
+
printDiagnostics(output, analysis.diagnostics.filter((diagnostic) => diagnostic.tool === toolName));
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
if (tools.length === 0) {
|
|
49
|
+
output.write("actionmcp: no tools found. Wrap a Server Function in agent(), or list it in agent-tools.ts.");
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
const header = [
|
|
53
|
+
"NAME",
|
|
54
|
+
"KIND",
|
|
55
|
+
"ANNOTATIONS",
|
|
56
|
+
"CONFIRM",
|
|
57
|
+
"SCHEMA",
|
|
58
|
+
"SOURCE",
|
|
59
|
+
"DIAGNOSTICS"
|
|
60
|
+
];
|
|
61
|
+
const rows = tools.map((tool) => [
|
|
62
|
+
tool.name,
|
|
63
|
+
tool.kind,
|
|
64
|
+
describeAnnotations(tool),
|
|
65
|
+
tool.confirm,
|
|
66
|
+
tool.schemaSource,
|
|
67
|
+
`${tool.source.file}:${tool.source.line}`,
|
|
68
|
+
tool.diagnostics.toSorted().join(",") || "-"
|
|
69
|
+
]);
|
|
70
|
+
for (const line of table([header, ...rows])) output.write(line);
|
|
71
|
+
output.write(`${plural(tools.length, "tool")}, ${plural(analysis.routes.length, "route")}, ${summarize(analysis.diagnostics)}`);
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
async function runDoctor({ config, output }, json) {
|
|
75
|
+
const analysis = await analyze(config);
|
|
76
|
+
const diagnostics = [...checkEnvironment(config.cwd), ...sortDiagnostics(analysis.diagnostics)];
|
|
77
|
+
if (json) output.write(JSON.stringify({ diagnostics }, null, 2));
|
|
78
|
+
else {
|
|
79
|
+
printDiagnostics(output, diagnostics);
|
|
80
|
+
output.write(`actionmcp doctor: ${plural(analysis.tools.length, "tool")} checked, ${summarize(diagnostics)}`);
|
|
81
|
+
}
|
|
82
|
+
return hasErrors(diagnostics) ? 1 : 0;
|
|
83
|
+
}
|
|
84
|
+
function describeAnnotations(tool) {
|
|
85
|
+
return [
|
|
86
|
+
tool.annotations.readOnlyHint ? "read-only" : void 0,
|
|
87
|
+
tool.annotations.consequentialHint ? "consequential" : void 0,
|
|
88
|
+
tool.annotations.untrustedContentHint ? "untrusted-output" : void 0
|
|
89
|
+
].filter((label) => label !== void 0).join(",") || "-";
|
|
90
|
+
}
|
|
91
|
+
function describeTool(output, tool) {
|
|
92
|
+
const rows = [
|
|
93
|
+
["name", tool.name],
|
|
94
|
+
["kind", tool.kind],
|
|
95
|
+
["description", tool.description || "(resolved at runtime)"],
|
|
96
|
+
["source", `${tool.source.file}:${tool.source.line} (${tool.source.export})`],
|
|
97
|
+
["annotations", describeAnnotations(tool)],
|
|
98
|
+
["confirm", tool.confirm],
|
|
99
|
+
["adapter", tool.adapter ?? "-"],
|
|
100
|
+
["validated", tool.validated ? "yes, before the handler runs" : "no, the action validates its own input"],
|
|
101
|
+
["schema", tool.schemaSource]
|
|
102
|
+
];
|
|
103
|
+
for (const line of table(rows)) output.write(line);
|
|
104
|
+
if (tool.inputSchema) output.write(JSON.stringify(tool.inputSchema, null, 2));
|
|
105
|
+
else if (tool.inputSource) output.write(`input: ${tool.inputSource}`);
|
|
106
|
+
}
|
|
107
|
+
export { runDoctor, runGenerate, runInspect };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { createDiagnostic } from "../analyzer/diagnostics.js";
|
|
5
|
+
// src/cli/environment.ts
|
|
6
|
+
const LOCATION = {
|
|
7
|
+
file: "package.json",
|
|
8
|
+
line: 1,
|
|
9
|
+
column: 1
|
|
10
|
+
};
|
|
11
|
+
const TOKEN_VARIABLE = "NEXT_PUBLIC_WEBMCP_OT_TOKEN";
|
|
12
|
+
function readPackageVersion(cwd, name) {
|
|
13
|
+
try {
|
|
14
|
+
const manifestPath = createRequire(join(cwd, "package.json")).resolve(`${name}/package.json`);
|
|
15
|
+
return JSON.parse(readFileSync(manifestPath, "utf8")).version;
|
|
16
|
+
} catch {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function isAtLeast(version, minimum) {
|
|
21
|
+
const parse = (value) => value.split("-")[0]?.split(".").map(Number) ?? [];
|
|
22
|
+
const [current, required] = [parse(version), parse(minimum)];
|
|
23
|
+
for (let index = 0; index < required.length; index += 1) {
|
|
24
|
+
const difference = (current[index] ?? 0) - (required[index] ?? 0);
|
|
25
|
+
if (difference !== 0) return difference > 0;
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
function checkEnvironment(cwd, env = process.env) {
|
|
30
|
+
const version = (name) => readPackageVersion(cwd, name);
|
|
31
|
+
const diagnostics = [];
|
|
32
|
+
const next = version("next");
|
|
33
|
+
if (!next) diagnostics.push(createDiagnostic("AT201", LOCATION, "Next.js is not installed"));
|
|
34
|
+
else if (!next.startsWith("16.")) diagnostics.push(createDiagnostic("AT201", LOCATION, `found next@${next}`));
|
|
35
|
+
const react = version("react");
|
|
36
|
+
if (!react) diagnostics.push(createDiagnostic("AT202", LOCATION, "React is not installed"));
|
|
37
|
+
else if (!isAtLeast(react, "19.2.0")) diagnostics.push(createDiagnostic("AT202", LOCATION, `found react@${react}`));
|
|
38
|
+
const zod = version("zod");
|
|
39
|
+
if (zod && !isAtLeast(zod, "4.2.0")) diagnostics.push(createDiagnostic("AT203", LOCATION, `found zod@${zod}`));
|
|
40
|
+
const arktype = version("arktype");
|
|
41
|
+
if (arktype && !isAtLeast(arktype, "2.1.28")) diagnostics.push(createDiagnostic("AT203", LOCATION, `found arktype@${arktype}`));
|
|
42
|
+
const valibot = version("valibot");
|
|
43
|
+
if (valibot && !isAtLeast(valibot, "1.2.0")) diagnostics.push(createDiagnostic("AT203", LOCATION, `found valibot@${valibot}`));
|
|
44
|
+
else if (valibot && !version("@valibot/to-json-schema")) diagnostics.push(createDiagnostic("AT203", LOCATION, "valibot is installed without @valibot/to-json-schema"));
|
|
45
|
+
if (!hasOriginTrialToken(cwd, env)) diagnostics.push(createDiagnostic("AT204", LOCATION));
|
|
46
|
+
return diagnostics;
|
|
47
|
+
}
|
|
48
|
+
function hasOriginTrialToken(cwd, env) {
|
|
49
|
+
if (env[TOKEN_VARIABLE]) return true;
|
|
50
|
+
if (!existsSync(cwd)) return false;
|
|
51
|
+
return readdirSync(cwd).filter((name) => name === ".env" || name.startsWith(".env.")).some((name) => new RegExp(`^${TOKEN_VARIABLE}=.+`, "m").test(readFileSync(join(cwd, name), "utf8")));
|
|
52
|
+
}
|
|
53
|
+
export { checkEnvironment, isAtLeast, readPackageVersion };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { runDoctor, runGenerate, runInspect } from "./commands.js";
|
|
5
|
+
import { processOutput } from "./output.js";
|
|
6
|
+
import { loadProjectConfig } from "./project.js";
|
|
7
|
+
// src/cli/index.ts
|
|
8
|
+
const USAGE = `Usage: actionmcp <command> [options]
|
|
9
|
+
|
|
10
|
+
Commands:
|
|
11
|
+
generate [--watch] Scan the project and write the registry and the manifest
|
|
12
|
+
inspect [--json] [tool] List the tools, or show one tool
|
|
13
|
+
doctor [--json] Check versions, names, validation, confirmation, and the origin trial token
|
|
14
|
+
|
|
15
|
+
Options:
|
|
16
|
+
--cwd <dir> Project directory (default: current directory)
|
|
17
|
+
--registry <file> Explicit registry file (default: ./agent-tools.ts if it exists)
|
|
18
|
+
--scan <glob> Glob to scan for agent() and @agent; repeatable
|
|
19
|
+
--out-dir <dir> Output folder (default: .agent-tools)
|
|
20
|
+
--jsdoc Enable the experimental @agent JSDoc path
|
|
21
|
+
-h, --help Show this help
|
|
22
|
+
-v, --version Show the version
|
|
23
|
+
|
|
24
|
+
Options default to the withAgentTools() options in next.config.`;
|
|
25
|
+
function readVersion() {
|
|
26
|
+
return JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
|
|
27
|
+
}
|
|
28
|
+
async function run(argv, output = processOutput) {
|
|
29
|
+
let parsed;
|
|
30
|
+
try {
|
|
31
|
+
parsed = parseArgs({
|
|
32
|
+
args: [...argv],
|
|
33
|
+
allowPositionals: true,
|
|
34
|
+
options: {
|
|
35
|
+
cwd: { type: "string" },
|
|
36
|
+
registry: { type: "string" },
|
|
37
|
+
scan: {
|
|
38
|
+
type: "string",
|
|
39
|
+
multiple: true
|
|
40
|
+
},
|
|
41
|
+
"out-dir": { type: "string" },
|
|
42
|
+
jsdoc: { type: "boolean" },
|
|
43
|
+
watch: {
|
|
44
|
+
type: "boolean",
|
|
45
|
+
default: false
|
|
46
|
+
},
|
|
47
|
+
json: {
|
|
48
|
+
type: "boolean",
|
|
49
|
+
default: false
|
|
50
|
+
},
|
|
51
|
+
help: {
|
|
52
|
+
type: "boolean",
|
|
53
|
+
short: "h",
|
|
54
|
+
default: false
|
|
55
|
+
},
|
|
56
|
+
version: {
|
|
57
|
+
type: "boolean",
|
|
58
|
+
short: "v",
|
|
59
|
+
default: false
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
} catch (error) {
|
|
64
|
+
output.error(`actionmcp: ${error instanceof Error ? error.message : String(error)}`);
|
|
65
|
+
output.error(USAGE);
|
|
66
|
+
return 1;
|
|
67
|
+
}
|
|
68
|
+
const { values, positionals } = parsed;
|
|
69
|
+
const [command, argument] = positionals;
|
|
70
|
+
if (values.version) {
|
|
71
|
+
output.write(readVersion());
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
if (values.help || command === void 0) {
|
|
75
|
+
output.write(USAGE);
|
|
76
|
+
return values.help ? 0 : 1;
|
|
77
|
+
}
|
|
78
|
+
if (command !== "generate" && command !== "inspect" && command !== "doctor") {
|
|
79
|
+
output.error(`actionmcp: unknown command "${command}".`);
|
|
80
|
+
output.error(USAGE);
|
|
81
|
+
return 1;
|
|
82
|
+
}
|
|
83
|
+
const cwd = resolve(values.cwd ?? process.cwd());
|
|
84
|
+
const context = {
|
|
85
|
+
config: await loadProjectConfig(cwd, {
|
|
86
|
+
registry: values.registry,
|
|
87
|
+
scan: values.scan,
|
|
88
|
+
outDir: values["out-dir"],
|
|
89
|
+
jsdoc: values.jsdoc
|
|
90
|
+
}, output),
|
|
91
|
+
output
|
|
92
|
+
};
|
|
93
|
+
if (command === "generate") return runGenerate(context, values.watch);
|
|
94
|
+
if (command === "inspect") return runInspect(context, argument, values.json);
|
|
95
|
+
return runDoctor(context, values.json);
|
|
96
|
+
}
|
|
97
|
+
export { readVersion, run };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { styleText } from "node:util";
|
|
2
|
+
// src/cli/output.ts
|
|
3
|
+
const processOutput = {
|
|
4
|
+
write: (text) => process.stdout.write(`${text}\n`),
|
|
5
|
+
error: (text) => process.stderr.write(`${text}\n`)
|
|
6
|
+
};
|
|
7
|
+
function printDiagnostics(output, diagnostics) {
|
|
8
|
+
for (const diagnostic of diagnostics) {
|
|
9
|
+
const isError = diagnostic.level === "error";
|
|
10
|
+
const label = styleText(isError ? "red" : "yellow", `${isError ? "error" : "warn "} ${diagnostic.code}`);
|
|
11
|
+
const tool = diagnostic.tool ? ` [${diagnostic.tool}]` : "";
|
|
12
|
+
output.write(`${label} ${diagnostic.file}:${diagnostic.line}:${diagnostic.column}${tool} ${diagnostic.message}`);
|
|
13
|
+
output.write(` fix: ${diagnostic.fix}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function summarize(diagnostics) {
|
|
17
|
+
const errors = diagnostics.filter((diagnostic) => diagnostic.level === "error").length;
|
|
18
|
+
return `${plural(errors, "error")}, ${plural(diagnostics.length - errors, "warning")}`;
|
|
19
|
+
}
|
|
20
|
+
function plural(count, noun) {
|
|
21
|
+
return `${count} ${noun}${count === 1 ? "" : "s"}`;
|
|
22
|
+
}
|
|
23
|
+
function table(rows) {
|
|
24
|
+
const widths = rows.reduce((result, row) => row.map((cell, index) => Math.max(result[index] ?? 0, cell.length)), []);
|
|
25
|
+
return rows.map((row) => row.map((cell, index) => cell.padEnd(widths[index] ?? 0)).join(" ").trimEnd());
|
|
26
|
+
}
|
|
27
|
+
export { plural, printDiagnostics, processOutput, summarize, table };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { resolveConfig } from "../analyzer/config.js";
|
|
5
|
+
import { clearRecordedConfig, readRecordedConfig } from "../next/config-store.js";
|
|
6
|
+
// src/cli/project.ts
|
|
7
|
+
const NEXT_CONFIG_FILES = [
|
|
8
|
+
"next.config.ts",
|
|
9
|
+
"next.config.mts",
|
|
10
|
+
"next.config.mjs",
|
|
11
|
+
"next.config.js"
|
|
12
|
+
];
|
|
13
|
+
async function loadProjectConfig(cwd, flags, output) {
|
|
14
|
+
const merged = { ...await readNextConfig(cwd, output) };
|
|
15
|
+
if (flags.registry !== void 0) merged.registry = flags.registry;
|
|
16
|
+
if (flags.scan !== void 0 && flags.scan.length > 0) merged.scan = flags.scan;
|
|
17
|
+
if (flags.outDir !== void 0) merged.outDir = flags.outDir;
|
|
18
|
+
if (flags.jsdoc !== void 0) merged.jsdoc = flags.jsdoc;
|
|
19
|
+
return resolveConfig(merged, cwd);
|
|
20
|
+
}
|
|
21
|
+
async function readNextConfig(cwd, output) {
|
|
22
|
+
const file = NEXT_CONFIG_FILES.map((name) => join(cwd, name)).find((candidate) => existsSync(candidate));
|
|
23
|
+
if (!file) return void 0;
|
|
24
|
+
clearRecordedConfig();
|
|
25
|
+
try {
|
|
26
|
+
await import(pathToFileURL(file).href);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
const reason = error instanceof Error ? error.message.split("\n")[0] : String(error);
|
|
29
|
+
output.error(`actionmcp: could not read withAgentTools() options from ${file}. Using defaults and flags. ${reason}`);
|
|
30
|
+
}
|
|
31
|
+
return readRecordedConfig();
|
|
32
|
+
}
|
|
33
|
+
export { loadProjectConfig };
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AgentToolsProvider, AgentToolsProviderProps } from "./react/Provider.js";
|
|
2
|
+
import { AgentToolsDevtools } from "./react/Devtools.js";
|
|
3
|
+
import { ConfirmDialog } from "./react/Confirm.js";
|
|
4
|
+
import { AgentToolsHooks, ConfirmLabels, ConfirmProps } from "./react/context.js";
|
|
5
|
+
import { ToolAnnotations, ToolCall } from "./core/types.js";
|
|
6
|
+
export { AgentToolsDevtools, type AgentToolsHooks, AgentToolsProvider, type AgentToolsProviderProps, ConfirmDialog, type ConfirmLabels, type ConfirmProps, type ToolAnnotations, type ToolCall };
|
package/dist/client.js
ADDED
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AgentToolsConfig, ResolvedConfig } from "./analyzer/config.js";
|
|
2
|
+
export interface ConfigContext {
|
|
3
|
+
defaultConfig: object;
|
|
4
|
+
}
|
|
5
|
+
export type ConfigFunction<Config extends object> = (phase: string, context: ConfigContext) => Config | Promise<Config>;
|
|
6
|
+
export declare const REGISTRY_ALIAS = "actionmcp/registry";
|
|
7
|
+
export declare function withAgentTools(options?: AgentToolsConfig): <Config extends object = object>(nextConfig?: Config | ConfigFunction<Config>) => ConfigFunction<Config>;
|
|
8
|
+
export declare function applyRegistryAlias<Config extends object>(nextConfig: Config, config: ResolvedConfig): Config;
|
|
9
|
+
export type { AgentToolsConfig };
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { join, relative, sep } from "node:path";
|
|
2
|
+
import { resolveConfig } from "./analyzer/config.js";
|
|
3
|
+
import { formatDiagnostic, hasErrors } from "./analyzer/diagnostics.js";
|
|
4
|
+
import { REGISTRY_FILE, generate } from "./generator/index.js";
|
|
5
|
+
import { watchProject } from "./generator/watch.js";
|
|
6
|
+
import { recordConfig } from "./next/config-store.js";
|
|
7
|
+
// src/config.ts
|
|
8
|
+
const REGISTRY_ALIAS = "actionmcp/registry";
|
|
9
|
+
const BUILD_PHASE = "phase-production-build";
|
|
10
|
+
const DEVELOPMENT_PHASE = "phase-development-server";
|
|
11
|
+
const PREPARED = Symbol.for("actionmcp.prepared");
|
|
12
|
+
function withAgentTools(options = {}) {
|
|
13
|
+
recordConfig(options);
|
|
14
|
+
return (nextConfig) => async (phase, context) => {
|
|
15
|
+
const base = typeof nextConfig === "function" ? await nextConfig(phase, context) : nextConfig ?? {};
|
|
16
|
+
const config = resolveConfig(options, process.cwd());
|
|
17
|
+
await prepareOnce(phase, config);
|
|
18
|
+
return applyRegistryAlias(base, config);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function applyRegistryAlias(nextConfig, config) {
|
|
22
|
+
const registry = join(config.outDir, REGISTRY_FILE);
|
|
23
|
+
const relativeRegistry = `./${relative(config.cwd, registry).split(sep).join("/")}`;
|
|
24
|
+
const { turbopack, webpack: userWebpack } = nextConfig;
|
|
25
|
+
const aliased = {
|
|
26
|
+
turbopack: {
|
|
27
|
+
...turbopack,
|
|
28
|
+
resolveAlias: {
|
|
29
|
+
...turbopack?.resolveAlias,
|
|
30
|
+
[REGISTRY_ALIAS]: relativeRegistry
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
webpack(configuration, context) {
|
|
34
|
+
configuration.resolve ??= {};
|
|
35
|
+
configuration.resolve.alias = {
|
|
36
|
+
...configuration.resolve.alias,
|
|
37
|
+
[REGISTRY_ALIAS]: registry
|
|
38
|
+
};
|
|
39
|
+
return userWebpack ? userWebpack(configuration, context) : configuration;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
...nextConfig,
|
|
44
|
+
...aliased
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function prepareOnce(phase, config) {
|
|
48
|
+
if (phase !== BUILD_PHASE && phase !== DEVELOPMENT_PHASE) return Promise.resolve();
|
|
49
|
+
const store = globalThis;
|
|
50
|
+
const prepared = store[PREPARED] ??= /* @__PURE__ */ new Map();
|
|
51
|
+
const key = `${phase}:${config.cwd}`;
|
|
52
|
+
let pending = prepared.get(key);
|
|
53
|
+
if (!pending) {
|
|
54
|
+
pending = phase === BUILD_PHASE ? prepareBuild(config) : prepareDevelopment(config);
|
|
55
|
+
prepared.set(key, pending);
|
|
56
|
+
}
|
|
57
|
+
return pending;
|
|
58
|
+
}
|
|
59
|
+
async function prepareBuild(config) {
|
|
60
|
+
const { analysis } = await generate(config);
|
|
61
|
+
report(analysis.diagnostics);
|
|
62
|
+
if (hasErrors(analysis.diagnostics)) {
|
|
63
|
+
const errors = analysis.diagnostics.filter((diagnostic) => diagnostic.level === "error");
|
|
64
|
+
throw new Error(`actionmcp found ${errors.length} error(s) in the agent tools:\n${errors.map(formatDiagnostic).join("\n")}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function prepareDevelopment(config) {
|
|
68
|
+
const { analysis } = await generate(config);
|
|
69
|
+
report(analysis.diagnostics);
|
|
70
|
+
let previous = JSON.stringify(analysis.diagnostics);
|
|
71
|
+
watchProject(config, {
|
|
72
|
+
persistent: false,
|
|
73
|
+
onResult: (result) => {
|
|
74
|
+
const current = JSON.stringify(result.analysis.diagnostics);
|
|
75
|
+
if (current !== previous) report(result.analysis.diagnostics);
|
|
76
|
+
previous = current;
|
|
77
|
+
},
|
|
78
|
+
onError: (error) => console.error("actionmcp: could not regenerate the tool registry.", error)
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function report(diagnostics) {
|
|
82
|
+
for (const diagnostic of diagnostics) (diagnostic.level === "error" ? console.error : console.warn)(`actionmcp ${formatDiagnostic(diagnostic)}`);
|
|
83
|
+
}
|
|
84
|
+
export { REGISTRY_ALIAS, applyRegistryAlias, withAgentTools };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/core/events.ts
|
|
2
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
3
|
+
function subscribeToToolCalls(listener) {
|
|
4
|
+
listeners.add(listener);
|
|
5
|
+
return () => {
|
|
6
|
+
listeners.delete(listener);
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function publishToolCall(record) {
|
|
10
|
+
for (const listener of listeners) listener(record);
|
|
11
|
+
}
|
|
12
|
+
export { publishToolCall, subscribeToToolCalls };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { publishToolCall } from "./events.js";
|
|
2
|
+
import { toFormData } from "./formdata.js";
|
|
3
|
+
import { BLOCKED_MESSAGE, DECLINED_MESSAGE, GENERIC_FAILURE_MESSAGE, fail, normalizeActionResult } from "./result.js";
|
|
4
|
+
// src/core/execute.ts
|
|
5
|
+
function requiresConfirmation(tool) {
|
|
6
|
+
return tool.confirm === "always" || tool.confirm === "consequential" && tool.annotations.consequentialHint;
|
|
7
|
+
}
|
|
8
|
+
async function executeTool(tool, input, signal, runtime) {
|
|
9
|
+
const call = {
|
|
10
|
+
name: tool.name,
|
|
11
|
+
input,
|
|
12
|
+
annotations: tool.annotations,
|
|
13
|
+
source: "agent"
|
|
14
|
+
};
|
|
15
|
+
const startedAt = Date.now();
|
|
16
|
+
let confirmation = "none";
|
|
17
|
+
const publish = (result) => publishToolCall({
|
|
18
|
+
name: tool.name,
|
|
19
|
+
input,
|
|
20
|
+
startedAt,
|
|
21
|
+
durationMs: Date.now() - startedAt,
|
|
22
|
+
confirmation,
|
|
23
|
+
result
|
|
24
|
+
});
|
|
25
|
+
const run = async () => {
|
|
26
|
+
if (requiresConfirmation(tool)) {
|
|
27
|
+
const accepted = await runtime.confirm(call, tool, signal);
|
|
28
|
+
confirmation = accepted ? "accepted" : "declined";
|
|
29
|
+
if (!accepted) return fail("declined", DECLINED_MESSAGE);
|
|
30
|
+
}
|
|
31
|
+
if (runtime.onBeforeExecute && await runtime.onBeforeExecute(call) === false) return fail("blocked", BLOCKED_MESSAGE);
|
|
32
|
+
if (signal.aborted) return fail("failed", GENERIC_FAILURE_MESSAGE);
|
|
33
|
+
const result = await invoke(tool, input, runtime);
|
|
34
|
+
runtime.onAfterExecute?.(call, result);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
try {
|
|
38
|
+
const output = await untilAborted(run, signal);
|
|
39
|
+
publish(output);
|
|
40
|
+
return output;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
publish({ aborted: true });
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function callAction(tool, input) {
|
|
47
|
+
const action = tool.action;
|
|
48
|
+
if (tool.adapter === "formdata") return action(void 0, toFormData(input));
|
|
49
|
+
if (tool.adapter === "form") return action(toFormData(input));
|
|
50
|
+
return action(input);
|
|
51
|
+
}
|
|
52
|
+
async function invoke(tool, input, runtime) {
|
|
53
|
+
try {
|
|
54
|
+
return normalizeActionResult(await callAction(tool, input), tool.adapter);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
return runtime.recoverError?.(error) ?? fail("failed", error instanceof Error && error.message ? error.message : "The tool failed.");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function untilAborted(start, signal) {
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
const onAbort = () => reject(new DOMException("The tool call was cancelled.", "AbortError"));
|
|
62
|
+
if (signal.aborted) {
|
|
63
|
+
onAbort();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
67
|
+
start().then(resolve, reject).finally(() => signal.removeEventListener("abort", onAbort));
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
export { executeTool, requiresConfirmation };
|