@openfairygui/mcp 0.6.3 → 0.7.1
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/README.md +12 -4
- package/bin/ofgui-mcp.cjs +3 -3
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +18 -9
- package/dist/index.d.mts +19 -10
- package/dist/index.mjs +1 -1
- package/dist/{stdio-BWd0nXcw.mjs → stdio-CGEOPi_6.mjs} +389 -239
- package/dist/{stdio-D0L4ie5Z.cjs → stdio-uhD6y1BG.cjs} +389 -239
- package/dist/stdio.cjs +1 -1
- package/dist/stdio.mjs +1 -1
- package/package.json +7 -7
- package/src/contract-schema.ts +0 -39
- package/src/index.ts +0 -35
- package/src/prompt-definitions.ts +0 -122
- package/src/resource-definitions.ts +0 -141
- package/src/server.ts +0 -94
- package/src/stdio.ts +0 -20
- package/src/tool-definitions.ts +0 -64
- package/src/tool-handler.ts +0 -107
- package/src/tool-metadata.ts +0 -160
|
@@ -2,105 +2,11 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
3
|
import { createNodeBackendRuntime } from "@openfairygui/backend/node";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
+
import { OPENFAIRYGUI_DOCS_INDEX_URI, OPENFAIRYGUI_OPERATION_CATALOG_URI, OPENFAIRYGUI_OPERATION_CATALOG_URI as OPENFAIRYGUI_OPERATION_CATALOG_URI$1, OPENFAIRYGUI_OPERATION_SCHEMA_TEMPLATE, OPENFAIRYGUI_OPERATION_SCHEMA_TEMPLATE as OPENFAIRYGUI_OPERATION_SCHEMA_TEMPLATE$1, decodeContractBytes as decodeToolBytes, getInstalledContractSnapshot, getInstalledDocumentationIndex, getOpenFairyGuiOperationCatalog, getOpenFairyGuiOperationSchema, readInstalledDocumentation } from "@openfairygui/backend/docs";
|
|
5
6
|
import { BACKEND_CAPABILITY_SCHEMA_VERSION, BACKEND_CONTRACT_VERSION, BACKEND_DIAGNOSTICS_URI, BACKEND_DIAGNOSTIC_TEMPLATE, getBackendDiagnosticCatalog, getBackendDiagnosticGuide } from "@openfairygui/backend";
|
|
6
|
-
import { OPENFAIRYGUI_DOCS_INDEX_URI, OPENFAIRYGUI_OPERATION_CATALOG_URI, OPENFAIRYGUI_OPERATION_SCHEMA_TEMPLATE, getInstalledContractSnapshot, getInstalledDocumentationIndex, getOpenFairyGuiOperationCatalog, getOpenFairyGuiOperationSchema, readInstalledDocumentation } from "@openfairygui/backend/docs";
|
|
7
7
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
8
8
|
import path from "node:path";
|
|
9
9
|
import { pathToFileURL } from "node:url";
|
|
10
|
-
//#region src/prompt-definitions.ts
|
|
11
|
-
const OPENFAIRYGUI_BACKEND_PROMPT_NAMES = [
|
|
12
|
-
"openfairygui_inspect_capabilities",
|
|
13
|
-
"openfairygui_open_and_inspect_session",
|
|
14
|
-
"openfairygui_inspect_project_outline",
|
|
15
|
-
"openfairygui_plan_revision_checked_transaction",
|
|
16
|
-
"openfairygui_save_session",
|
|
17
|
-
"openfairygui_poll_runtime_state"
|
|
18
|
-
];
|
|
19
|
-
const OPENFAIRYGUI_BACKEND_PROMPT_DEFINITIONS = [
|
|
20
|
-
{
|
|
21
|
-
name: "openfairygui_inspect_capabilities",
|
|
22
|
-
title: "Inspect OpenFairyGUI Backend Capabilities",
|
|
23
|
-
description: "Guide a client through the capability and version discovery tool.",
|
|
24
|
-
text: [
|
|
25
|
-
"Use openfairygui_backend_get_capabilities first.",
|
|
26
|
-
"Read contractVersion, capabilitySchemaVersion, capability planes, methods, and runtime non-goals from the backend envelope.",
|
|
27
|
-
"Do not infer artifact publish/restore, subscriptions, or cache source-of-truth support when the backend marks them unsupported."
|
|
28
|
-
].join("\n")
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
name: "openfairygui_open_and_inspect_session",
|
|
32
|
-
title: "Open and Inspect an OpenFairyGUI Session",
|
|
33
|
-
description: "Guide a client through opening and reading a backend session.",
|
|
34
|
-
text: [
|
|
35
|
-
"Use openfairygui_backend_open_session with a projectPath, then use openfairygui_backend_get_session with the returned sessionId.",
|
|
36
|
-
"Backend path policy remains authoritative for project paths and save targets; MCP roots are only client context in this package.",
|
|
37
|
-
"Close the session with openfairygui_backend_close_session when finished."
|
|
38
|
-
].join("\n")
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
name: "openfairygui_inspect_project_outline",
|
|
42
|
-
title: "Inspect an OpenFairyGUI Project Outline",
|
|
43
|
-
description: "Guide a client through selector discovery before a revision-checked transaction.",
|
|
44
|
-
text: [
|
|
45
|
-
"Use openfairygui_backend_get_project_outline with the active sessionId before planning mutations.",
|
|
46
|
-
"Use package, resource, folder, display node, controller page, and transition identities from the returned outline.",
|
|
47
|
-
"Pass the returned revision as expectedRevision when calling openfairygui_backend_apply_transaction.",
|
|
48
|
-
"Read a new outline if the backend reports a stale revision; the outline intentionally excludes source bytes and full property payloads."
|
|
49
|
-
].join("\n")
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
name: "openfairygui_plan_revision_checked_transaction",
|
|
53
|
-
title: "Plan a Revision-Checked OpenFairyGUI Transaction",
|
|
54
|
-
description: "Guide a client through backend-owned revision checks without inventing operation grammar.",
|
|
55
|
-
text: [
|
|
56
|
-
"Use openfairygui_backend_get_session to read the current revision before mutation.",
|
|
57
|
-
"Use openfairygui_backend_get_project_outline for identities, then openfairygui_backend_query_entity for current properties at the returned revision.",
|
|
58
|
-
"For settings edits, query target {kind:\"project\"} or {kind:\"package\",selector:{packageId}}; copy entity.properties.settings, change the requested fields, and submit the complete settings to updateProjectSettings or updatePackageSettings.",
|
|
59
|
-
"Read openfairygui://contracts/operations and openfairygui://contracts/operations/{kind} for the current operation names and exact JSON parameters.",
|
|
60
|
-
"Call openfairygui_backend_preflight_transaction with the queried revision and planned operations to execute and discard an isolated preview; inspect the backend diagnostics.",
|
|
61
|
-
"A successful preview does not reserve a revision or guarantee save. Apply the same batch with expectedRevision set to the preview baseRevision; refresh properties and re-plan on stale revision.",
|
|
62
|
-
"Call openfairygui_backend_apply_transaction with sessionId, expectedRevision, and backend/UAM-owned operations.",
|
|
63
|
-
"If the backend returns a stale revision error, refresh the session snapshot and re-plan against the new revision.",
|
|
64
|
-
"Do not invent selector grammar, transaction grammar, or operation payload semantics at the MCP layer."
|
|
65
|
-
].join("\n")
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
name: "openfairygui_save_session",
|
|
69
|
-
title: "Save an OpenFairyGUI Backend Session",
|
|
70
|
-
description: "Guide a client through coordinated backend save semantics.",
|
|
71
|
-
text: [
|
|
72
|
-
"Use openfairygui_backend_save_session with sessionId and expectedRevision when available.",
|
|
73
|
-
"Backend path policy remains authoritative for targetPath; MCP does not canonicalize or authorize paths.",
|
|
74
|
-
"Handle stale revision and partial save failure envelopes from the backend without rewriting their error semantics."
|
|
75
|
-
].join("\n")
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
name: "openfairygui_poll_runtime_state",
|
|
79
|
-
title: "Poll OpenFairyGUI Runtime State",
|
|
80
|
-
description: "Guide a client through event polling and synchronous cache snapshots.",
|
|
81
|
-
text: [
|
|
82
|
-
"Use openfairygui_backend_get_events for polling events with the backend cursor contract.",
|
|
83
|
-
"Use openfairygui_backend_get_cache_snapshot for derived read-only cache state; openfairygui_backend_refresh_cache returns the refreshed snapshot synchronously.",
|
|
84
|
-
"Subscriptions and cache-as-source-of-truth behavior are not supported by the backend."
|
|
85
|
-
].join("\n")
|
|
86
|
-
}
|
|
87
|
-
];
|
|
88
|
-
function promptResult(text) {
|
|
89
|
-
return { messages: [{
|
|
90
|
-
role: "user",
|
|
91
|
-
content: {
|
|
92
|
-
type: "text",
|
|
93
|
-
text
|
|
94
|
-
}
|
|
95
|
-
}] };
|
|
96
|
-
}
|
|
97
|
-
function registerOpenFairyGuiBackendPrompts(server) {
|
|
98
|
-
for (const definition of OPENFAIRYGUI_BACKEND_PROMPT_DEFINITIONS) server.registerPrompt(definition.name, {
|
|
99
|
-
title: definition.title,
|
|
100
|
-
description: definition.description
|
|
101
|
-
}, () => promptResult(definition.text));
|
|
102
|
-
}
|
|
103
|
-
//#endregion
|
|
104
10
|
//#region src/contract-schema.ts
|
|
105
11
|
const CONTRACT_SNAPSHOT = getInstalledContractSnapshot();
|
|
106
12
|
function contractObjectSchema(schema) {
|
|
@@ -111,129 +17,67 @@ function contractObjectSchema(schema) {
|
|
|
111
17
|
if (!(result instanceof z.ZodObject)) throw new TypeError("Tool contract must be an object");
|
|
112
18
|
return result;
|
|
113
19
|
}
|
|
20
|
+
function valueAt(value, path) {
|
|
21
|
+
return path.reduce((node, key) => node !== null && typeof node === "object" ? node[key] : void 0, value);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A union of `kind`-tagged objects fails with one nested issue list per branch.
|
|
25
|
+
* Report only the branch selected by the input's kind, or the valid kinds when none matches;
|
|
26
|
+
* other unions keep Zod's issue. Paths stay relative to `input`.
|
|
27
|
+
*/
|
|
28
|
+
function focusContractIssues(issues, input) {
|
|
29
|
+
return issues.flatMap((issue) => {
|
|
30
|
+
if (issue.code !== "invalid_union" || issue.errors.length < 2) return [issue];
|
|
31
|
+
const tags = issue.errors.map((branch) => branch.find((entry) => entry.code === "invalid_value" && entry.path.length === 1 && entry.path[0] === "kind"));
|
|
32
|
+
const untagged = issue.errors.filter((_branch, index) => !tags[index]);
|
|
33
|
+
if (untagged.length > 1 || untagged.some((branch) => branch.some((entry) => entry.code === "invalid_type" && entry.path.length === 0))) return [issue];
|
|
34
|
+
const target = valueAt(input, issue.path);
|
|
35
|
+
const kind = valueAt(target, ["kind"]);
|
|
36
|
+
const kinds = tags.flatMap((tag) => tag?.code === "invalid_value" ? tag.values : []);
|
|
37
|
+
const operation = issue.errors.length === Object.keys(CONTRACT_SNAPSHOT.operations).length && kinds.every((value) => typeof value === "string" && Object.hasOwn(CONTRACT_SNAPSHOT.operations, value));
|
|
38
|
+
if (untagged.length === 1) {
|
|
39
|
+
const note = operation && typeof kind === "string" ? ` (schema: ${OPENFAIRYGUI_OPERATION_CATALOG_URI}/${kind})` : "";
|
|
40
|
+
return focusContractIssues(untagged[0], target).map((entry) => ({
|
|
41
|
+
...entry,
|
|
42
|
+
path: [...issue.path, ...entry.path],
|
|
43
|
+
message: entry.message + note
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
const subject = operation ? "operation kind" : "kind";
|
|
47
|
+
const found = kind === void 0 ? `Missing ${subject}` : typeof kind === "string" ? `Unknown ${subject} ${JSON.stringify(kind)}` : `Invalid ${subject} of type ${typeof kind}`;
|
|
48
|
+
const help = operation ? `. Read ${OPENFAIRYGUI_OPERATION_SCHEMA_TEMPLATE} for one operation's fields` : "";
|
|
49
|
+
return [{
|
|
50
|
+
code: "invalid_value",
|
|
51
|
+
values: kinds,
|
|
52
|
+
path: [...issue.path, "kind"],
|
|
53
|
+
message: `${found}; expected one of: ${kinds.join(", ")}${help}`
|
|
54
|
+
}];
|
|
55
|
+
});
|
|
56
|
+
}
|
|
114
57
|
/** Keep SDK discovery dynamic without expanding shared contract definitions. */
|
|
115
|
-
function compactToolSchema(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (!parsed.success) for (const issue of parsed.error.issues) context.addIssue({ ...issue });
|
|
119
|
-
}).meta(z.toJSONSchema(schema, {
|
|
58
|
+
function compactToolSchema(getSchema, io, withinBudget) {
|
|
59
|
+
let metadata;
|
|
60
|
+
const getMetadata = () => metadata ??= { ...z.toJSONSchema(getSchema(), {
|
|
120
61
|
target: "draft-07",
|
|
121
62
|
io,
|
|
122
63
|
reused: "ref"
|
|
123
|
-
})
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
if (
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
//#endregion
|
|
141
|
-
//#region src/resource-definitions.ts
|
|
142
|
-
const JSON_MIME_TYPE = "application/json";
|
|
143
|
-
function firstVariable(value) {
|
|
144
|
-
return Array.isArray(value) ? value[0] ?? "" : value ?? "";
|
|
145
|
-
}
|
|
146
|
-
function jsonResource(uri, backendResult) {
|
|
147
|
-
return { contents: [{
|
|
148
|
-
uri: uri.toString(),
|
|
149
|
-
mimeType: JSON_MIME_TYPE,
|
|
150
|
-
text: JSON.stringify(backendResult, null, 2)
|
|
151
|
-
}] };
|
|
152
|
-
}
|
|
153
|
-
const OPENFAIRYGUI_BACKEND_CAPABILITIES_RESOURCE_URI = "openfairygui://backend/capabilities";
|
|
154
|
-
const OPENFAIRYGUI_BACKEND_RESOURCE_TEMPLATES = [
|
|
155
|
-
"openfairygui://docs/methods/{method}",
|
|
156
|
-
"openfairygui://docs/cli/{command}",
|
|
157
|
-
BACKEND_DIAGNOSTIC_TEMPLATE,
|
|
158
|
-
OPENFAIRYGUI_OPERATION_SCHEMA_TEMPLATE,
|
|
159
|
-
"openfairygui://backend/session/{sessionId}",
|
|
160
|
-
"openfairygui://backend/session/{sessionId}/outline",
|
|
161
|
-
"openfairygui://backend/cache/{sessionId}"
|
|
162
|
-
];
|
|
163
|
-
function registerOpenFairyGuiBackendResources(server, runtime) {
|
|
164
|
-
server.registerResource("openfairygui_docs_index", OPENFAIRYGUI_DOCS_INDEX_URI, {
|
|
165
|
-
title: "Installed Documentation",
|
|
166
|
-
description: "Offline documentation IDs, URIs, installed package version and contract digest shared with the CLI.",
|
|
167
|
-
mimeType: JSON_MIME_TYPE
|
|
168
|
-
}, (uri) => jsonResource(uri, getInstalledDocumentationIndex()));
|
|
169
|
-
function installedDocument(uri, id) {
|
|
170
|
-
const document = readInstalledDocumentation(id);
|
|
171
|
-
return { contents: [{
|
|
172
|
-
uri: uri.toString(),
|
|
173
|
-
mimeType: document.mimeType,
|
|
174
|
-
text: document.text
|
|
175
|
-
}] };
|
|
176
|
-
}
|
|
177
|
-
for (const id of [
|
|
178
|
-
"workflow",
|
|
179
|
-
"restore-limits",
|
|
180
|
-
"skill",
|
|
181
|
-
"contracts"
|
|
182
|
-
]) server.registerResource(`openfairygui_docs_${id}`, `openfairygui://docs/${id}`, {
|
|
183
|
-
title: `Installed ${id}`,
|
|
184
|
-
description: "Read the installed-version corpus without repository or network access.",
|
|
185
|
-
mimeType: id === "contracts" ? JSON_MIME_TYPE : "text/markdown"
|
|
186
|
-
}, (uri) => installedDocument(uri, id));
|
|
187
|
-
server.registerResource("openfairygui_docs_method", new ResourceTemplate("openfairygui://docs/methods/{method}", { list: void 0 }), {
|
|
188
|
-
title: "Installed Method Contract",
|
|
189
|
-
description: "Read self-contained Backend/MCP wire input/output schemas and metadata.",
|
|
190
|
-
mimeType: JSON_MIME_TYPE
|
|
191
|
-
}, (uri, variables) => installedDocument(uri, `methods/${firstVariable(variables.method)}`));
|
|
192
|
-
server.registerResource("openfairygui_diagnostic_catalog", BACKEND_DIAGNOSTICS_URI, {
|
|
193
|
-
title: "Diagnostic Recovery Catalog",
|
|
194
|
-
description: "Complete formal diagnostic ownership and recovery guidance; never automatic repair.",
|
|
195
|
-
mimeType: JSON_MIME_TYPE
|
|
196
|
-
}, (uri) => jsonResource(uri, getBackendDiagnosticCatalog()));
|
|
197
|
-
server.registerResource("openfairygui_docs_cli", new ResourceTemplate("openfairygui://docs/cli/{command}", { list: void 0 }), {
|
|
198
|
-
title: "Installed CLI Output Contract",
|
|
199
|
-
description: "Read a generated, self-contained CLI JSON envelope schema.",
|
|
200
|
-
mimeType: JSON_MIME_TYPE
|
|
201
|
-
}, (uri, variables) => installedDocument(uri, `cli/${decodeURIComponent(firstVariable(variables.command))}`));
|
|
202
|
-
server.registerResource("openfairygui_diagnostic_guide", new ResourceTemplate(BACKEND_DIAGNOSTIC_TEMPLATE, { list: void 0 }), {
|
|
203
|
-
title: "Diagnostic Recovery Guide",
|
|
204
|
-
description: "Read the recovery boundary for one stable diagnostic code.",
|
|
205
|
-
mimeType: JSON_MIME_TYPE
|
|
206
|
-
}, (uri, variables) => jsonResource(uri, getBackendDiagnosticGuide(firstVariable(variables.code))));
|
|
207
|
-
server.registerResource("openfairygui_operation_catalog", OPENFAIRYGUI_OPERATION_CATALOG_URI, {
|
|
208
|
-
title: "UAM Operation Catalog",
|
|
209
|
-
description: "Discover current operations and their generated JSON schemas.",
|
|
210
|
-
mimeType: JSON_MIME_TYPE
|
|
211
|
-
}, (uri) => jsonResource(uri, getOpenFairyGuiOperationCatalog()));
|
|
212
|
-
server.registerResource("openfairygui_operation_schema", new ResourceTemplate(OPENFAIRYGUI_OPERATION_SCHEMA_TEMPLATE, { list: void 0 }), {
|
|
213
|
-
title: "UAM Operation Schema",
|
|
214
|
-
description: "Read the precise Core-derived JSON wire schema for one operation. Structure is not semantic preflight.",
|
|
215
|
-
mimeType: JSON_MIME_TYPE
|
|
216
|
-
}, (uri, variables) => jsonResource(uri, getOpenFairyGuiOperationSchema(firstVariable(variables.kind))));
|
|
217
|
-
server.registerResource("openfairygui_backend_capabilities", OPENFAIRYGUI_BACKEND_CAPABILITIES_RESOURCE_URI, {
|
|
218
|
-
title: "OpenFairyGUI Backend Capabilities",
|
|
219
|
-
description: "Read the backend capability and version envelope as JSON.",
|
|
220
|
-
mimeType: JSON_MIME_TYPE
|
|
221
|
-
}, (uri) => jsonResource(uri, runtime.getCapabilities()));
|
|
222
|
-
server.registerResource("openfairygui_backend_session", new ResourceTemplate("openfairygui://backend/session/{sessionId}", { list: void 0 }), {
|
|
223
|
-
title: "OpenFairyGUI Backend Session Snapshot",
|
|
224
|
-
description: "Read a backend session envelope by backend-local session id.",
|
|
225
|
-
mimeType: JSON_MIME_TYPE
|
|
226
|
-
}, (uri, variables) => jsonResource(uri, runtime.getSession({ sessionId: firstVariable(variables.sessionId) })));
|
|
227
|
-
server.registerResource("openfairygui_backend_project_outline", new ResourceTemplate("openfairygui://backend/session/{sessionId}/outline", { list: void 0 }), {
|
|
228
|
-
title: "OpenFairyGUI Project Outline",
|
|
229
|
-
description: "Read a revision-bound project identity outline without source bytes or full property payloads.",
|
|
230
|
-
mimeType: JSON_MIME_TYPE
|
|
231
|
-
}, (uri, variables) => jsonResource(uri, runtime.getProjectOutline({ sessionId: firstVariable(variables.sessionId) })));
|
|
232
|
-
server.registerResource("openfairygui_backend_cache", new ResourceTemplate("openfairygui://backend/cache/{sessionId}", { list: void 0 }), {
|
|
233
|
-
title: "OpenFairyGUI Backend Cache Snapshot",
|
|
234
|
-
description: "Read a derived backend cache envelope by backend-local session id.",
|
|
235
|
-
mimeType: JSON_MIME_TYPE
|
|
236
|
-
}, (uri, variables) => jsonResource(uri, runtime.getCacheSnapshot({ sessionId: firstVariable(variables.sessionId) })));
|
|
64
|
+
}) };
|
|
65
|
+
const lazyMetadata = new Proxy({}, {
|
|
66
|
+
get: (_target, key) => Reflect.get(getMetadata(), key),
|
|
67
|
+
ownKeys: () => Reflect.ownKeys(getMetadata()),
|
|
68
|
+
getOwnPropertyDescriptor: (_target, key) => Object.getOwnPropertyDescriptor(getMetadata(), key)
|
|
69
|
+
});
|
|
70
|
+
return z.looseObject({}).superRefine((value, context) => {
|
|
71
|
+
if (withinBudget && !withinBudget(value)) {
|
|
72
|
+
context.addIssue({
|
|
73
|
+
code: "custom",
|
|
74
|
+
message: "MCP input exceeds the payload budget."
|
|
75
|
+
});
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const parsed = getSchema().safeParse(value);
|
|
79
|
+
if (!parsed.success) for (const issue of focusContractIssues(parsed.error.issues, value)) context.addIssue({ ...issue });
|
|
80
|
+
}).meta(lazyMetadata);
|
|
237
81
|
}
|
|
238
82
|
//#endregion
|
|
239
83
|
//#region src/tool-metadata.ts
|
|
@@ -253,7 +97,7 @@ const OPENFAIRYGUI_BACKEND_TOOL_METADATA = [
|
|
|
253
97
|
name: "openfairygui_backend_open_session",
|
|
254
98
|
backendMethod: "openSession",
|
|
255
99
|
title: "Open Backend Session",
|
|
256
|
-
description: "Open a
|
|
100
|
+
description: "Open a project directory or .fairy file within allowed roots (stdio: OPENFAIRYGUI_ALLOWED_PROJECT_ROOTS). Acquire a session lock; close the session when finished.",
|
|
257
101
|
annotations: {
|
|
258
102
|
readOnlyHint: false,
|
|
259
103
|
idempotentHint: false,
|
|
@@ -286,7 +130,8 @@ const OPENFAIRYGUI_BACKEND_TOOL_METADATA = [
|
|
|
286
130
|
name: "openfairygui_backend_get_project_outline",
|
|
287
131
|
backendMethod: "getProjectOutline",
|
|
288
132
|
title: "Get Project Outline",
|
|
289
|
-
description: "Return a revision-bound project/package/resource/component identity outline without source bytes or full property payloads.",
|
|
133
|
+
description: "Return a revision-bound project/package/resource/component identity outline without source bytes or full property payloads. Complete tool response is limited to 16 MiB.",
|
|
134
|
+
maxResponseBytes: 16777216,
|
|
290
135
|
annotations: {
|
|
291
136
|
readOnlyHint: true,
|
|
292
137
|
idempotentHint: true,
|
|
@@ -332,7 +177,8 @@ const OPENFAIRYGUI_BACKEND_TOOL_METADATA = [
|
|
|
332
177
|
name: "openfairygui_backend_validate_session",
|
|
333
178
|
backendMethod: "validateSession",
|
|
334
179
|
title: "Validate Project Session",
|
|
335
|
-
description: "Validate the current session project structure, references, paths, and available source bytes without writing files.",
|
|
180
|
+
description: "Validate the current session project structure, references, paths, and available source bytes without writing files. Complete tool response is limited to 16 MiB.",
|
|
181
|
+
maxResponseBytes: 16777216,
|
|
336
182
|
annotations: {
|
|
337
183
|
readOnlyHint: true,
|
|
338
184
|
idempotentHint: true,
|
|
@@ -343,7 +189,7 @@ const OPENFAIRYGUI_BACKEND_TOOL_METADATA = [
|
|
|
343
189
|
name: "openfairygui_backend_preflight_transaction",
|
|
344
190
|
backendMethod: "preflightTransaction",
|
|
345
191
|
title: "Preview UAM Transaction",
|
|
346
|
-
description: "Execute a revision-checked operation batch on an isolated project snapshot and discard the result. Returns the base revision and Core diagnostics; does not write, reserve a revision, or guarantee a later apply/save.",
|
|
192
|
+
description: "Execute a revision-checked operation batch on an isolated project snapshot and discard the result. Each operations[] item is one object whose \"kind\" field selects its fields, e.g. {\"kind\":\"renameResource\",\"selector\":{\"packageId\":\"<packageId>\",\"resourceId\":\"<resourceId>\"},\"newName\":\"Panel\"} with IDs from openfairygui_backend_get_project_outline. Read openfairygui://contracts/operations for every kind and openfairygui://contracts/operations/{kind} for the exact fields of one kind before composing operations. Returns the base revision and Core diagnostics; does not write, reserve a revision, or guarantee a later apply/save.",
|
|
347
193
|
annotations: {
|
|
348
194
|
readOnlyHint: true,
|
|
349
195
|
idempotentHint: true,
|
|
@@ -354,7 +200,7 @@ const OPENFAIRYGUI_BACKEND_TOOL_METADATA = [
|
|
|
354
200
|
name: "openfairygui_backend_apply_transaction",
|
|
355
201
|
backendMethod: "applyTransaction",
|
|
356
202
|
title: "Apply UAM Transaction",
|
|
357
|
-
description: "Apply a bounded, revision-checked UAM operation batch
|
|
203
|
+
description: "Apply a bounded, revision-checked UAM operation batch. Each operations[] item is one object whose \"kind\" field selects its fields, e.g. {\"kind\":\"renameResource\",\"selector\":{\"packageId\":\"<packageId>\",\"resourceId\":\"<resourceId>\"},\"newName\":\"Panel\"} with IDs from openfairygui_backend_get_project_outline. Read openfairygui://contracts/operations for every kind and openfairygui://contracts/operations/{kind} for the exact fields of one kind before composing operations. Check a batch first with openfairygui_backend_preflight_transaction. On stale_write, re-query and reconsider the batch; do not retry blindly. Validate and save afterward.",
|
|
358
204
|
annotations: {
|
|
359
205
|
readOnlyHint: false,
|
|
360
206
|
destructiveHint: true,
|
|
@@ -366,7 +212,7 @@ const OPENFAIRYGUI_BACKEND_TOOL_METADATA = [
|
|
|
366
212
|
name: "openfairygui_backend_save_session",
|
|
367
213
|
backendMethod: "saveSession",
|
|
368
214
|
title: "Save Backend Session",
|
|
369
|
-
description: "Write the current backend session through its coordinated save path; Node
|
|
215
|
+
description: "Write the current backend session through its coordinated save path; Node stages project-owned files and reports rollback or retained-backup diagnostics.",
|
|
370
216
|
annotations: {
|
|
371
217
|
readOnlyHint: false,
|
|
372
218
|
destructiveHint: true,
|
|
@@ -401,7 +247,8 @@ const OPENFAIRYGUI_BACKEND_TOOL_METADATA = [
|
|
|
401
247
|
name: "openfairygui_backend_get_events",
|
|
402
248
|
backendMethod: "getEvents",
|
|
403
249
|
title: "Get Runtime Events",
|
|
404
|
-
description: "Poll backend runtime events for a session using the backend
|
|
250
|
+
description: "Poll backend runtime events for a session using the backend event cursor contract. Complete tool response is limited to 16 MiB.",
|
|
251
|
+
maxResponseBytes: 16777216,
|
|
405
252
|
annotations: {
|
|
406
253
|
readOnlyHint: true,
|
|
407
254
|
idempotentHint: true,
|
|
@@ -484,21 +331,265 @@ function isOpenFairyGuiMcpPayloadWithinBudget(root, bytePaths = []) {
|
|
|
484
331
|
}
|
|
485
332
|
const OPENFAIRYGUI_BACKEND_TOOL_DEFINITIONS = OPENFAIRYGUI_BACKEND_TOOL_METADATA.map((metadata) => {
|
|
486
333
|
const contract = CONTRACT_SNAPSHOT.tools[metadata.backendMethod];
|
|
334
|
+
let inputSchema;
|
|
335
|
+
let outputSchema;
|
|
487
336
|
return {
|
|
488
337
|
...metadata,
|
|
489
|
-
inputSchema
|
|
490
|
-
|
|
338
|
+
get inputSchema() {
|
|
339
|
+
return inputSchema ??= contractObjectSchema(contract.input);
|
|
340
|
+
},
|
|
341
|
+
get outputSchema() {
|
|
342
|
+
return outputSchema ??= contractObjectSchema(contract.output);
|
|
343
|
+
}
|
|
491
344
|
};
|
|
492
345
|
});
|
|
493
346
|
//#endregion
|
|
347
|
+
//#region src/prompt-definitions.ts
|
|
348
|
+
const OPENFAIRYGUI_BACKEND_PROMPT_NAMES = [
|
|
349
|
+
"openfairygui_inspect_capabilities",
|
|
350
|
+
"openfairygui_open_and_inspect_session",
|
|
351
|
+
"openfairygui_inspect_project_outline",
|
|
352
|
+
"openfairygui_plan_revision_checked_transaction",
|
|
353
|
+
"openfairygui_save_session",
|
|
354
|
+
"openfairygui_poll_runtime_state"
|
|
355
|
+
];
|
|
356
|
+
const OPENFAIRYGUI_BACKEND_PROMPT_DEFINITIONS = [
|
|
357
|
+
{
|
|
358
|
+
name: "openfairygui_inspect_capabilities",
|
|
359
|
+
title: "Inspect OpenFairyGUI Backend Capabilities",
|
|
360
|
+
description: "Guide a client through the capability and version discovery tool.",
|
|
361
|
+
text: [
|
|
362
|
+
"Use openfairygui_backend_get_capabilities first.",
|
|
363
|
+
"Read contractVersion, capabilitySchemaVersion, capability planes, methods, and runtime non-goals from the backend envelope.",
|
|
364
|
+
"Do not infer artifact publish/restore, subscriptions, or cache source-of-truth support when the backend marks them unsupported."
|
|
365
|
+
].join("\n")
|
|
366
|
+
},
|
|
367
|
+
{
|
|
368
|
+
name: "openfairygui_open_and_inspect_session",
|
|
369
|
+
title: "Open and Inspect an OpenFairyGUI Session",
|
|
370
|
+
description: "Guide a client through opening and reading a backend session.",
|
|
371
|
+
text: [
|
|
372
|
+
"Use openfairygui_backend_open_session with a projectPath, then use openfairygui_backend_get_session with the returned sessionId.",
|
|
373
|
+
"Backend path policy remains authoritative for project paths and save targets; MCP roots are only client context in this package.",
|
|
374
|
+
"Close the session with openfairygui_backend_close_session when finished."
|
|
375
|
+
].join("\n")
|
|
376
|
+
},
|
|
377
|
+
{
|
|
378
|
+
name: "openfairygui_inspect_project_outline",
|
|
379
|
+
title: "Inspect an OpenFairyGUI Project Outline",
|
|
380
|
+
description: "Guide a client through selector discovery before a revision-checked transaction.",
|
|
381
|
+
text: [
|
|
382
|
+
"Use openfairygui_backend_get_project_outline with the active sessionId before planning mutations.",
|
|
383
|
+
"Use package, resource, folder, display node, controller page, and transition identities from the returned outline.",
|
|
384
|
+
"Pass the returned revision as expectedRevision when calling openfairygui_backend_apply_transaction.",
|
|
385
|
+
"Read a new outline if the backend reports a stale revision; the outline intentionally excludes source bytes and full property payloads."
|
|
386
|
+
].join("\n")
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
name: "openfairygui_plan_revision_checked_transaction",
|
|
390
|
+
title: "Plan a Revision-Checked OpenFairyGUI Transaction",
|
|
391
|
+
description: "Guide a client through backend-owned revision checks without inventing operation grammar.",
|
|
392
|
+
text: [
|
|
393
|
+
"Use openfairygui_backend_get_session to read the current revision before mutation.",
|
|
394
|
+
"Use openfairygui_backend_get_project_outline for identities, then openfairygui_backend_query_entity for current properties at the returned revision.",
|
|
395
|
+
"For settings edits, query target {kind:\"project\"} or {kind:\"package\",selector:{packageId}}; copy entity.properties.settings, change the requested fields, and submit the complete settings to updateProjectSettings or updatePackageSettings.",
|
|
396
|
+
"Read openfairygui://contracts/operations and openfairygui://contracts/operations/{kind} for the current operation names and exact JSON parameters.",
|
|
397
|
+
"Call openfairygui_backend_preflight_transaction with the queried revision and planned operations to execute and discard an isolated preview; inspect the backend diagnostics.",
|
|
398
|
+
"A successful preview does not reserve a revision or guarantee save. Apply the same batch with expectedRevision set to the preview baseRevision; refresh properties and re-plan on stale revision.",
|
|
399
|
+
"Call openfairygui_backend_apply_transaction with sessionId, expectedRevision, and backend/UAM-owned operations.",
|
|
400
|
+
"If the backend returns a stale revision error, refresh the session snapshot and re-plan against the new revision.",
|
|
401
|
+
"Do not invent selector grammar, transaction grammar, or operation payload semantics at the MCP layer."
|
|
402
|
+
].join("\n")
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
name: "openfairygui_save_session",
|
|
406
|
+
title: "Save an OpenFairyGUI Backend Session",
|
|
407
|
+
description: "Guide a client through coordinated backend save semantics.",
|
|
408
|
+
text: [
|
|
409
|
+
"Use openfairygui_backend_save_session with sessionId and expectedRevision when available.",
|
|
410
|
+
"Backend path policy remains authoritative for targetPath; MCP does not canonicalize or authorize paths.",
|
|
411
|
+
"Handle stale revision and partial save failure envelopes from the backend without rewriting their error semantics."
|
|
412
|
+
].join("\n")
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
name: "openfairygui_poll_runtime_state",
|
|
416
|
+
title: "Poll OpenFairyGUI Runtime State",
|
|
417
|
+
description: "Guide a client through event polling and synchronous cache snapshots.",
|
|
418
|
+
text: [
|
|
419
|
+
"Use openfairygui_backend_get_events for polling events with the backend cursor contract.",
|
|
420
|
+
"Use openfairygui_backend_get_cache_snapshot for derived read-only cache state; openfairygui_backend_refresh_cache returns the refreshed snapshot synchronously.",
|
|
421
|
+
"Subscriptions and cache-as-source-of-truth behavior are not supported by the backend."
|
|
422
|
+
].join("\n")
|
|
423
|
+
}
|
|
424
|
+
];
|
|
425
|
+
function promptResult(text) {
|
|
426
|
+
return { messages: [{
|
|
427
|
+
role: "user",
|
|
428
|
+
content: {
|
|
429
|
+
type: "text",
|
|
430
|
+
text
|
|
431
|
+
}
|
|
432
|
+
}] };
|
|
433
|
+
}
|
|
434
|
+
function registerOpenFairyGuiBackendPrompts(server) {
|
|
435
|
+
for (const definition of OPENFAIRYGUI_BACKEND_PROMPT_DEFINITIONS) server.registerPrompt(definition.name, {
|
|
436
|
+
title: definition.title,
|
|
437
|
+
description: definition.description
|
|
438
|
+
}, () => promptResult(definition.text));
|
|
439
|
+
}
|
|
440
|
+
//#endregion
|
|
441
|
+
//#region src/resource-definitions.ts
|
|
442
|
+
const JSON_MIME_TYPE = "application/json";
|
|
443
|
+
function firstVariable(value) {
|
|
444
|
+
return Array.isArray(value) ? value[0] ?? "" : value ?? "";
|
|
445
|
+
}
|
|
446
|
+
function jsonResource(uri, backendResult) {
|
|
447
|
+
return { contents: [{
|
|
448
|
+
uri: uri.toString(),
|
|
449
|
+
mimeType: JSON_MIME_TYPE,
|
|
450
|
+
text: JSON.stringify(backendResult, null, 2)
|
|
451
|
+
}] };
|
|
452
|
+
}
|
|
453
|
+
const OPENFAIRYGUI_BACKEND_CAPABILITIES_RESOURCE_URI = "openfairygui://backend/capabilities";
|
|
454
|
+
const OPENFAIRYGUI_BACKEND_RESOURCE_TEMPLATES = [
|
|
455
|
+
"openfairygui://docs/methods/{method}",
|
|
456
|
+
"openfairygui://docs/cli/{command}",
|
|
457
|
+
BACKEND_DIAGNOSTIC_TEMPLATE,
|
|
458
|
+
OPENFAIRYGUI_OPERATION_SCHEMA_TEMPLATE$1,
|
|
459
|
+
"openfairygui://backend/session/{sessionId}",
|
|
460
|
+
"openfairygui://backend/session/{sessionId}/outline",
|
|
461
|
+
"openfairygui://backend/cache/{sessionId}"
|
|
462
|
+
];
|
|
463
|
+
function registerOpenFairyGuiBackendResources(server, runtime) {
|
|
464
|
+
server.registerResource("openfairygui_docs_index", OPENFAIRYGUI_DOCS_INDEX_URI, {
|
|
465
|
+
title: "Installed Documentation",
|
|
466
|
+
description: "Offline documentation IDs, URIs, installed package version and contract digest shared with the CLI.",
|
|
467
|
+
mimeType: JSON_MIME_TYPE
|
|
468
|
+
}, (uri) => jsonResource(uri, getInstalledDocumentationIndex()));
|
|
469
|
+
function installedDocument(uri, id) {
|
|
470
|
+
const document = readInstalledDocumentation(id);
|
|
471
|
+
return { contents: [{
|
|
472
|
+
uri: uri.toString(),
|
|
473
|
+
mimeType: document.mimeType,
|
|
474
|
+
text: document.text
|
|
475
|
+
}] };
|
|
476
|
+
}
|
|
477
|
+
for (const id of [
|
|
478
|
+
"workflow",
|
|
479
|
+
"restore-limits",
|
|
480
|
+
"skill",
|
|
481
|
+
"contracts"
|
|
482
|
+
]) server.registerResource(`openfairygui_docs_${id}`, `openfairygui://docs/${id}`, {
|
|
483
|
+
title: `Installed ${id}`,
|
|
484
|
+
description: "Read the installed-version corpus without repository or network access.",
|
|
485
|
+
mimeType: id === "contracts" ? JSON_MIME_TYPE : "text/markdown"
|
|
486
|
+
}, (uri) => installedDocument(uri, id));
|
|
487
|
+
server.registerResource("openfairygui_docs_method", new ResourceTemplate("openfairygui://docs/methods/{method}", { list: void 0 }), {
|
|
488
|
+
title: "Installed Method Contract",
|
|
489
|
+
description: "Read self-contained Backend/MCP wire input/output schemas and metadata.",
|
|
490
|
+
mimeType: JSON_MIME_TYPE
|
|
491
|
+
}, (uri, variables) => installedDocument(uri, `methods/${firstVariable(variables.method)}`));
|
|
492
|
+
server.registerResource("openfairygui_diagnostic_catalog", BACKEND_DIAGNOSTICS_URI, {
|
|
493
|
+
title: "Diagnostic Recovery Catalog",
|
|
494
|
+
description: "Complete formal diagnostic ownership and recovery guidance; never automatic repair.",
|
|
495
|
+
mimeType: JSON_MIME_TYPE
|
|
496
|
+
}, (uri) => jsonResource(uri, getBackendDiagnosticCatalog()));
|
|
497
|
+
server.registerResource("openfairygui_docs_cli", new ResourceTemplate("openfairygui://docs/cli/{command}", { list: void 0 }), {
|
|
498
|
+
title: "Installed CLI Output Contract",
|
|
499
|
+
description: "Read a generated, self-contained CLI JSON envelope schema.",
|
|
500
|
+
mimeType: JSON_MIME_TYPE
|
|
501
|
+
}, (uri, variables) => installedDocument(uri, `cli/${decodeURIComponent(firstVariable(variables.command))}`));
|
|
502
|
+
server.registerResource("openfairygui_diagnostic_guide", new ResourceTemplate(BACKEND_DIAGNOSTIC_TEMPLATE, { list: void 0 }), {
|
|
503
|
+
title: "Diagnostic Recovery Guide",
|
|
504
|
+
description: "Read the recovery boundary for one stable diagnostic code.",
|
|
505
|
+
mimeType: JSON_MIME_TYPE
|
|
506
|
+
}, (uri, variables) => jsonResource(uri, getBackendDiagnosticGuide(firstVariable(variables.code))));
|
|
507
|
+
server.registerResource("openfairygui_operation_catalog", OPENFAIRYGUI_OPERATION_CATALOG_URI$1, {
|
|
508
|
+
title: "UAM Operation Catalog",
|
|
509
|
+
description: "Discover current operations and their generated JSON schemas.",
|
|
510
|
+
mimeType: JSON_MIME_TYPE
|
|
511
|
+
}, (uri) => jsonResource(uri, getOpenFairyGuiOperationCatalog()));
|
|
512
|
+
server.registerResource("openfairygui_operation_schema", new ResourceTemplate(OPENFAIRYGUI_OPERATION_SCHEMA_TEMPLATE$1, { list: void 0 }), {
|
|
513
|
+
title: "UAM Operation Schema",
|
|
514
|
+
description: "Read the precise Core-derived JSON wire schema for one operation. Structure is not semantic preflight.",
|
|
515
|
+
mimeType: JSON_MIME_TYPE
|
|
516
|
+
}, (uri, variables) => jsonResource(uri, getOpenFairyGuiOperationSchema(firstVariable(variables.kind))));
|
|
517
|
+
server.registerResource("openfairygui_backend_capabilities", OPENFAIRYGUI_BACKEND_CAPABILITIES_RESOURCE_URI, {
|
|
518
|
+
title: "OpenFairyGUI Backend Capabilities",
|
|
519
|
+
description: "Read the backend capability and version envelope as JSON.",
|
|
520
|
+
mimeType: JSON_MIME_TYPE
|
|
521
|
+
}, (uri) => jsonResource(uri, runtime.getCapabilities()));
|
|
522
|
+
server.registerResource("openfairygui_backend_session", new ResourceTemplate("openfairygui://backend/session/{sessionId}", { list: void 0 }), {
|
|
523
|
+
title: "OpenFairyGUI Backend Session Snapshot",
|
|
524
|
+
description: "Read a backend session envelope by backend-local session id.",
|
|
525
|
+
mimeType: JSON_MIME_TYPE
|
|
526
|
+
}, (uri, variables) => jsonResource(uri, runtime.getSession({ sessionId: firstVariable(variables.sessionId) })));
|
|
527
|
+
server.registerResource("openfairygui_backend_project_outline", new ResourceTemplate("openfairygui://backend/session/{sessionId}/outline", { list: void 0 }), {
|
|
528
|
+
title: "OpenFairyGUI Project Outline",
|
|
529
|
+
description: "Read a revision-bound project identity outline without source bytes or full property payloads.",
|
|
530
|
+
mimeType: JSON_MIME_TYPE
|
|
531
|
+
}, (uri, variables) => jsonResource(uri, runtime.getProjectOutline({ sessionId: firstVariable(variables.sessionId) })));
|
|
532
|
+
server.registerResource("openfairygui_backend_cache", new ResourceTemplate("openfairygui://backend/cache/{sessionId}", { list: void 0 }), {
|
|
533
|
+
title: "OpenFairyGUI Backend Cache Snapshot",
|
|
534
|
+
description: "Read a derived backend cache envelope by backend-local session id.",
|
|
535
|
+
mimeType: JSON_MIME_TYPE
|
|
536
|
+
}, (uri, variables) => jsonResource(uri, runtime.getCacheSnapshot({ sessionId: firstVariable(variables.sessionId) })));
|
|
537
|
+
}
|
|
538
|
+
//#endregion
|
|
494
539
|
//#region src/tool-handler.ts
|
|
495
|
-
function
|
|
496
|
-
const
|
|
540
|
+
function failureText(payload) {
|
|
541
|
+
const record = (value) => value !== null && typeof value === "object" ? value : {};
|
|
542
|
+
const root = record(payload), error = record(root.error), meta = record(root.meta);
|
|
543
|
+
const bounded = (value, limit) => typeof value === "string" ? value.slice(0, limit) : void 0;
|
|
544
|
+
const codes = /* @__PURE__ */ new Set();
|
|
545
|
+
let truncated = false;
|
|
546
|
+
for (const entries of [
|
|
547
|
+
error.issues,
|
|
548
|
+
error.diagnostics,
|
|
549
|
+
meta.diagnostics
|
|
550
|
+
]) {
|
|
551
|
+
if (!Array.isArray(entries)) continue;
|
|
552
|
+
for (const entry of entries) {
|
|
553
|
+
const code = bounded(record(entry).code, 128);
|
|
554
|
+
if (!code || codes.has(code)) continue;
|
|
555
|
+
if (codes.size === 32) {
|
|
556
|
+
truncated = true;
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
codes.add(code);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return JSON.stringify({
|
|
563
|
+
ok: false,
|
|
564
|
+
error: {
|
|
565
|
+
code: bounded(error.code, 128),
|
|
566
|
+
message: bounded(error.message, 1024)
|
|
567
|
+
},
|
|
568
|
+
diagnosticCodes: [...codes],
|
|
569
|
+
...truncated ? { diagnosticCodesTruncated: true } : {},
|
|
570
|
+
requestId: bounded(meta.requestId, 128),
|
|
571
|
+
fullResult: "structuredContent.backendResult"
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
function jsonResult(payload, isError = false, paths = []) {
|
|
575
|
+
const envelope = { backendResult: structuredClone(payload) };
|
|
576
|
+
function encode(value, parts) {
|
|
577
|
+
if (!value || typeof value !== "object") return;
|
|
578
|
+
const [key, ...rest] = parts;
|
|
579
|
+
const record = value;
|
|
580
|
+
for (const name of key === "*" ? Object.keys(record) : [key]) {
|
|
581
|
+
if (!Object.hasOwn(record, name)) continue;
|
|
582
|
+
if (rest.length) encode(record[name], rest);
|
|
583
|
+
else if (record[name] instanceof Uint8Array) record[name] = Buffer.from(record[name]).toString("base64");
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
for (const path of paths) encode(envelope, path);
|
|
587
|
+
const text = JSON.stringify(envelope.backendResult);
|
|
497
588
|
const wirePayload = JSON.parse(text);
|
|
498
589
|
return {
|
|
499
590
|
content: [{
|
|
500
591
|
type: "text",
|
|
501
|
-
text
|
|
592
|
+
text: isError ? failureText(wirePayload) : "Result available in structuredContent.backendResult."
|
|
502
593
|
}],
|
|
503
594
|
structuredContent: { backendResult: wirePayload },
|
|
504
595
|
isError
|
|
@@ -530,7 +621,9 @@ async function callOpenFairyGuiBackendTool(runtime, name, input, policy) {
|
|
|
530
621
|
if (!definition) throw new RangeError(`Unknown OpenFairyGUI backend MCP tool: ${name}`);
|
|
531
622
|
const bytePaths = CONTRACT_SNAPSHOT.tools[definition.backendMethod].bytePaths;
|
|
532
623
|
if (!isOpenFairyGuiMcpPayloadWithinBudget(input, bytePaths)) throw new RangeError("MCP input exceeds the depth, node, key, string, or byte budget.");
|
|
533
|
-
const
|
|
624
|
+
const checked = definition.inputSchema.safeParse(input);
|
|
625
|
+
if (!checked.success) throw new z.ZodRealError(focusContractIssues(checked.error.issues, input));
|
|
626
|
+
const parsed = checked.data;
|
|
534
627
|
const decoded = decodeToolBytes(parsed, bytePaths);
|
|
535
628
|
const startedAt = Date.now();
|
|
536
629
|
try {
|
|
@@ -540,7 +633,7 @@ async function callOpenFairyGuiBackendTool(runtime, name, input, policy) {
|
|
|
540
633
|
if (!isBackendFailure(hostFailure)) throw new TypeError("Host policy must return a failure or undefined.");
|
|
541
634
|
}
|
|
542
635
|
const result = hostFailure ?? await Reflect.apply(runtime[definition.backendMethod], runtime, definition.backendMethod === "getCapabilities" ? [] : [decoded]);
|
|
543
|
-
let response = jsonResult(result, isBackendFailure(result), definition.
|
|
636
|
+
let response = jsonResult(result, isBackendFailure(result), CONTRACT_SNAPSHOT.tools[definition.backendMethod].outputBytePaths);
|
|
544
637
|
if (definition.maxResponseBytes !== void 0 && new TextEncoder().encode(JSON.stringify(response)).byteLength > definition.maxResponseBytes) {
|
|
545
638
|
response = jsonResult({
|
|
546
639
|
...unhandledBackendFailure(startedAt),
|
|
@@ -555,15 +648,17 @@ async function callOpenFairyGuiBackendTool(runtime, name, input, policy) {
|
|
|
555
648
|
if (hostFailure === void 0) definition.outputSchema.parse(response.structuredContent);
|
|
556
649
|
else policy.failureSchema.parse(response.structuredContent?.backendResult);
|
|
557
650
|
return response;
|
|
558
|
-
} catch {
|
|
559
|
-
|
|
651
|
+
} catch (error) {
|
|
652
|
+
const failure = unhandledBackendFailure(startedAt);
|
|
653
|
+
console.error(`[openfairygui-mcp] ${name} failed (requestId ${failure.meta.requestId}):`, error);
|
|
654
|
+
return jsonResult(failure, true);
|
|
560
655
|
}
|
|
561
656
|
}
|
|
562
657
|
//#endregion
|
|
563
658
|
//#region src/server.ts
|
|
564
659
|
const require = createRequire(import.meta.url);
|
|
565
660
|
function getInjectedPackageVersion() {
|
|
566
|
-
const version = "0.
|
|
661
|
+
const version = "0.7.1";
|
|
567
662
|
return typeof version === "string" && true ? version : null;
|
|
568
663
|
}
|
|
569
664
|
function readPackageVersion() {
|
|
@@ -576,16 +671,25 @@ function readPackageVersion() {
|
|
|
576
671
|
return "0.0.0-dev";
|
|
577
672
|
}
|
|
578
673
|
const PACKAGE_VERSION = readPackageVersion();
|
|
674
|
+
const DEFAULT_INSTRUCTIONS = [
|
|
675
|
+
"Read openfairygui_docs_read with id index, then workflow and the needed operation/method documents.",
|
|
676
|
+
"Open a project directory or .fairy file within the configured allowed roots.",
|
|
677
|
+
"Read the project outline, then query exact entity IDs and their current revision.",
|
|
678
|
+
"Preflight the operation batch with expectedRevision before applying it.",
|
|
679
|
+
"Apply at that same revision; on stale_write re-query and reconsider, never retry blindly.",
|
|
680
|
+
"Validate the edited session, save with its current revision, and close the session when finished."
|
|
681
|
+
].join("\n");
|
|
579
682
|
function createOpenFairyGuiMcpServer(options = {}) {
|
|
580
683
|
for (const name of Object.keys(options.toolPolicies ?? {})) if (!OPENFAIRYGUI_BACKEND_TOOL_DEFINITIONS.some((definition) => definition.name === name)) throw new RangeError(`Unknown OpenFairyGUI backend MCP tool policy: ${name}`);
|
|
581
684
|
const runtime = options.runtime ?? createNodeBackendRuntime({ allowedProjectRoots: options.allowedProjectRoots ?? [process.cwd()] });
|
|
582
685
|
const server = new McpServer({
|
|
583
686
|
name: options.name ?? "openfairygui-mcp",
|
|
584
687
|
version: options.version ?? PACKAGE_VERSION
|
|
585
|
-
}, { instructions: options.instructions });
|
|
688
|
+
}, { instructions: options.instructions ?? DEFAULT_INSTRUCTIONS });
|
|
586
689
|
for (const definition of OPENFAIRYGUI_BACKEND_TOOL_DEFINITIONS) {
|
|
587
690
|
const policy = options.toolPolicies?.[definition.name];
|
|
588
|
-
|
|
691
|
+
let outputSchema;
|
|
692
|
+
const getOutputSchema = () => outputSchema ??= policy ? definition.outputSchema.extend({ backendResult: z.union([definition.outputSchema.shape.backendResult, policy.failureSchema]) }) : definition.outputSchema;
|
|
589
693
|
const metadata = {
|
|
590
694
|
name: definition.name,
|
|
591
695
|
title: definition.title,
|
|
@@ -593,30 +697,76 @@ function createOpenFairyGuiMcpServer(options = {}) {
|
|
|
593
697
|
annotations: definition.annotations,
|
|
594
698
|
_meta: {
|
|
595
699
|
"openfairygui/backendMethod": definition.backendMethod,
|
|
596
|
-
"openfairygui/adapter": "
|
|
700
|
+
"openfairygui/adapter": "backend",
|
|
597
701
|
"openfairygui/contractDigest": CONTRACT_SNAPSHOT.digest,
|
|
598
702
|
...policy ? { "openfairygui/hostPolicy": true } : {}
|
|
599
703
|
}
|
|
600
704
|
};
|
|
601
705
|
server.registerTool(definition.name, {
|
|
602
706
|
...metadata,
|
|
603
|
-
inputSchema: compactToolSchema(definition.inputSchema, "input"),
|
|
604
|
-
outputSchema: compactToolSchema(
|
|
707
|
+
inputSchema: compactToolSchema(() => definition.inputSchema, "input", (value) => isOpenFairyGuiMcpPayloadWithinBudget(value, CONTRACT_SNAPSHOT.tools[definition.backendMethod].bytePaths)),
|
|
708
|
+
outputSchema: compactToolSchema(getOutputSchema, "output")
|
|
605
709
|
}, async (args) => callOpenFairyGuiBackendTool(runtime, definition.name, args, policy));
|
|
606
710
|
}
|
|
711
|
+
server.registerTool("openfairygui_docs_read", {
|
|
712
|
+
title: "Read installed documentation",
|
|
713
|
+
description: "Read index first, then an exact document ID from that index. Offline, read-only and bound to the installed version.",
|
|
714
|
+
inputSchema: z.object({ id: z.string().min(1).max(256).default("index") }),
|
|
715
|
+
_meta: { "openfairygui/contractDigest": CONTRACT_SNAPSHOT.digest },
|
|
716
|
+
annotations: {
|
|
717
|
+
readOnlyHint: true,
|
|
718
|
+
idempotentHint: true,
|
|
719
|
+
openWorldHint: false
|
|
720
|
+
}
|
|
721
|
+
}, async ({ id }) => {
|
|
722
|
+
try {
|
|
723
|
+
return { content: [{
|
|
724
|
+
type: "text",
|
|
725
|
+
text: id === "index" ? JSON.stringify(getInstalledDocumentationIndex()) : readInstalledDocumentation(id).text
|
|
726
|
+
}] };
|
|
727
|
+
} catch {
|
|
728
|
+
return {
|
|
729
|
+
isError: true,
|
|
730
|
+
content: [{
|
|
731
|
+
type: "text",
|
|
732
|
+
text: "Unknown document ID; read index for available IDs."
|
|
733
|
+
}]
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
});
|
|
607
737
|
registerOpenFairyGuiBackendResources(server, runtime);
|
|
738
|
+
if (options.publish) server.registerTool("openfairygui_host_publish", {
|
|
739
|
+
title: "Publish project through host",
|
|
740
|
+
description: "Publish saved project files through an explicitly enabled host. Host owns path authorization, output policy and plugin execution; unsaved session edits are not included.",
|
|
741
|
+
inputSchema: z.object({
|
|
742
|
+
projectPath: z.string().min(1).max(4096),
|
|
743
|
+
outputDirectory: z.string().min(1).max(4096),
|
|
744
|
+
packageNames: z.array(z.string().min(1).max(256)).max(1e3).optional()
|
|
745
|
+
}),
|
|
746
|
+
annotations: {
|
|
747
|
+
readOnlyHint: false,
|
|
748
|
+
destructiveHint: true,
|
|
749
|
+
idempotentHint: false,
|
|
750
|
+
openWorldHint: false
|
|
751
|
+
}
|
|
752
|
+
}, options.publish);
|
|
608
753
|
registerOpenFairyGuiBackendPrompts(server);
|
|
609
754
|
return server;
|
|
610
755
|
}
|
|
611
756
|
//#endregion
|
|
612
757
|
//#region src/stdio.ts
|
|
758
|
+
function parseAllowedProjectRoots(value) {
|
|
759
|
+
if (value === void 0) return void 0;
|
|
760
|
+
const roots = value.split(path.delimiter).map((entry) => entry.trim()).filter(Boolean);
|
|
761
|
+
if (roots.length === 0) throw new Error("OPENFAIRYGUI_ALLOWED_PROJECT_ROOTS is set but lists no project roots. Set absolute roots separated by the platform path delimiter, or unset it to allow only the working directory.");
|
|
762
|
+
return roots;
|
|
763
|
+
}
|
|
613
764
|
async function connectOpenFairyGuiMcpStdio() {
|
|
614
|
-
|
|
615
|
-
await createOpenFairyGuiMcpServer({ allowedProjectRoots: configuredRoots }).connect(new StdioServerTransport());
|
|
765
|
+
await createOpenFairyGuiMcpServer({ allowedProjectRoots: parseAllowedProjectRoots(process.env.OPENFAIRYGUI_ALLOWED_PROJECT_ROOTS) }).connect(new StdioServerTransport());
|
|
616
766
|
}
|
|
617
767
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) connectOpenFairyGuiMcpStdio().catch((error) => {
|
|
618
768
|
console.error(error instanceof Error ? error.stack ?? error.message : String(error));
|
|
619
769
|
process.exitCode = 1;
|
|
620
770
|
});
|
|
621
771
|
//#endregion
|
|
622
|
-
export {
|
|
772
|
+
export { OPENFAIRYGUI_BACKEND_RESOURCE_TEMPLATES as a, OPENFAIRYGUI_BACKEND_TOOL_DEFINITIONS as c, OPENFAIRYGUI_OPERATION_CATALOG_URI$1 as d, OPENFAIRYGUI_OPERATION_SCHEMA_TEMPLATE$1 as f, OPENFAIRYGUI_BACKEND_CAPABILITIES_RESOURCE_URI as i, OPENFAIRYGUI_BACKEND_TOOL_NAMES as l, getOpenFairyGuiOperationSchema as m, createOpenFairyGuiMcpServer as n, OPENFAIRYGUI_BACKEND_PROMPT_DEFINITIONS as o, getOpenFairyGuiOperationCatalog as p, callOpenFairyGuiBackendTool as r, OPENFAIRYGUI_BACKEND_PROMPT_NAMES as s, connectOpenFairyGuiMcpStdio as t, OPENFAIRYGUI_BACKEND_TOOL_PREFIX as u };
|