create-ailk 0.44.0 → 0.45.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.
@@ -0,0 +1,189 @@
1
+ /**
2
+ * The tools the site's own MCP server serves (#11951, spec
3
+ * docs/specs/11951-the-mcp-discovery-file-lists-spec.md D1–D3).
4
+ *
5
+ * `/.well-known/mcp.json` lists exactly what the MCP server's `tools/list`
6
+ * answers at `MCP_PUBLIC_HTTP_URL`, because only the MCP deployment knows
7
+ * which module packages it installed — a second copy of its registration rule
8
+ * here drifted by construction. `readServedMcpTools(url)` asks it over plain
9
+ * `fetch`, no MCP SDK: `initialize`, `notifications/initialized`, `tools/list`
10
+ * (following `nextCursor`, at most `MAX_PAGES` pages), then a `DELETE` when
11
+ * the server opened a session. One exchange serves both transport modes: a
12
+ * stateless server answers JSON with no session, a stateful one
13
+ * `text/event-stream` with an `Mcp-Session-Id` every later request carries.
14
+ *
15
+ * Every request carries `MCP_DISCOVERY_USER_AGENT`, because the server's abuse
16
+ * guard refuses a missing UA and the built-in client ones (`curl/`,
17
+ * `node-fetch`, …) with 403 — and one deadline, `MCP_TOOLS_TIMEOUT_MS`, bounds
18
+ * the whole exchange. One answer per URL per server instance for
19
+ * `MCP_TOOLS_TTL_MS` — `siteDiscoverable()`'s shape
20
+ * (`packages/api-client/src/discovery.ts`): concurrent and later requests
21
+ * inside the window share it, a failure included, so an outage never adds the
22
+ * deadline to every manifest request.
23
+ *
24
+ * A failed read is `null` — a throw, the deadline, a non-2xx `initialize` or
25
+ * `tools/list`, no JSON-RPC answer with the request's `id`, an `error`
26
+ * answer, a `result.tools` that is not an array, an entry with no string
27
+ * `name` or no object `inputSchema`, or a tenth page still carrying
28
+ * `nextCursor`. The closing `DELETE` is courtesy, sent whenever `initialize`
29
+ * opened a session: its answer is not read, and its failure fails nothing.
30
+ * It never rejects.
31
+ */
32
+
33
+ /** Names this caller and its purpose to the server's user-agent guard. */
34
+ export const MCP_DISCOVERY_USER_AGENT = "ailk-web/mcp-discovery";
35
+ /** The MCP protocol version this site speaks and its manifest advertises. */
36
+ export const MCP_PROTOCOL_VERSION = "2025-03-26";
37
+ /** Shared per URL per instance for this long. */
38
+ export const MCP_TOOLS_TTL_MS = 60_000;
39
+ /** The whole exchange, every request together, is bounded by this. */
40
+ export const MCP_TOOLS_TIMEOUT_MS = 3_000;
41
+
42
+ const MAX_PAGES = 10;
43
+
44
+ /** One `tools/list` entry, as the manifest needs it. */
45
+ export interface ServedMcpTool {
46
+ name: string;
47
+ description?: string;
48
+ inputSchema: Record<string, unknown>;
49
+ }
50
+
51
+ type JsonRpcMessage = { id?: unknown; result?: unknown; error?: unknown };
52
+
53
+ function isRecord(value: unknown): value is Record<string, unknown> {
54
+ return typeof value === "object" && value !== null && !Array.isArray(value);
55
+ }
56
+
57
+ function parseJson(text: string): unknown {
58
+ try {
59
+ return JSON.parse(text) as unknown;
60
+ } catch {
61
+ return undefined;
62
+ }
63
+ }
64
+
65
+ /** The JSON-RPC answer to request `id` in a reply body, read by its content type. */
66
+ async function answerTo(res: Response, id: number): Promise<JsonRpcMessage | null> {
67
+ const type = res.headers.get("content-type") ?? "";
68
+ const text = await res.text();
69
+ const messages: unknown[] = [];
70
+ if (type.includes("text/event-stream")) {
71
+ for (const event of text.split(/\r?\n\r?\n/)) {
72
+ const data = event
73
+ .split(/\r?\n/)
74
+ .filter((line) => line.startsWith("data:"))
75
+ .map((line) => line.slice(5).replace(/^ /, ""))
76
+ .join("\n");
77
+ if (data) messages.push(parseJson(data));
78
+ }
79
+ } else if (type.includes("application/json")) {
80
+ messages.push(parseJson(text));
81
+ }
82
+ const answer = messages.find((m) => isRecord(m) && m["id"] === id);
83
+ return isRecord(answer) ? answer : null;
84
+ }
85
+
86
+ function toServedTool(entry: unknown): ServedMcpTool | null {
87
+ if (!isRecord(entry)) return null;
88
+ const { name, description, inputSchema } = entry;
89
+ if (typeof name !== "string" || !isRecord(inputSchema)) return null;
90
+ return {
91
+ name,
92
+ ...(typeof description === "string" ? { description } : {}),
93
+ inputSchema,
94
+ };
95
+ }
96
+
97
+ async function exchange(url: string): Promise<ServedMcpTool[] | null> {
98
+ const signal = AbortSignal.timeout(MCP_TOOLS_TIMEOUT_MS);
99
+ const headers: Record<string, string> = {
100
+ "user-agent": MCP_DISCOVERY_USER_AGENT,
101
+ accept: "application/json, text/event-stream",
102
+ };
103
+ let nextId = 1;
104
+
105
+ const post = (body: Record<string, unknown>): Promise<Response> =>
106
+ fetch(url, {
107
+ method: "POST",
108
+ // The transport answers a POST that is not application/json with 415.
109
+ headers: { ...headers, "content-type": "application/json" },
110
+ body: JSON.stringify({ jsonrpc: "2.0", ...body }),
111
+ cache: "no-store",
112
+ signal,
113
+ });
114
+
115
+ /** One request: its answer's `result` and the reply, or `null` on any failure shape. */
116
+ const call = async (
117
+ method: string,
118
+ params: Record<string, unknown>,
119
+ ): Promise<{ result: Record<string, unknown>; res: Response } | null> => {
120
+ const id = nextId++;
121
+ const res = await post({ id, method, params });
122
+ if (!res.ok) {
123
+ await res.body?.cancel();
124
+ return null;
125
+ }
126
+ const answer = await answerTo(res, id);
127
+ if (answer === null || answer.error !== undefined || !isRecord(answer.result)) return null;
128
+ return { result: answer.result, res };
129
+ };
130
+
131
+ const init = await call("initialize", {
132
+ protocolVersion: MCP_PROTOCOL_VERSION,
133
+ capabilities: {},
134
+ clientInfo: { name: "ailk-web", version: "1" },
135
+ });
136
+ if (init === null) return null;
137
+ const sessionId = init.res.headers.get("mcp-session-id");
138
+ const version = init.result["protocolVersion"];
139
+ headers["mcp-protocol-version"] =
140
+ typeof version === "string" ? version : MCP_PROTOCOL_VERSION;
141
+ if (sessionId) headers["mcp-session-id"] = sessionId;
142
+
143
+ try {
144
+ // The lifecycle's required notice; its answer is not read.
145
+ await (await post({ method: "notifications/initialized" })).body?.cancel();
146
+
147
+ const tools: ServedMcpTool[] = [];
148
+ let cursor: string | undefined;
149
+ for (let page = 0; page < MAX_PAGES; page++) {
150
+ const listed = await call("tools/list", cursor === undefined ? {} : { cursor });
151
+ if (listed === null || !Array.isArray(listed.result["tools"])) return null;
152
+ for (const entry of listed.result["tools"] as unknown[]) {
153
+ const tool = toServedTool(entry);
154
+ if (tool === null) return null;
155
+ tools.push(tool);
156
+ }
157
+ const next = listed.result["nextCursor"];
158
+ cursor = typeof next === "string" ? next : undefined;
159
+ if (cursor === undefined) break;
160
+ }
161
+ return cursor === undefined ? tools : null;
162
+ } finally {
163
+ if (sessionId) {
164
+ // End the stateful session, whatever the list's outcome. No
165
+ // content-type: the server refuses an empty JSON body. Courtesy only —
166
+ // its answer is not read, and its failure fails nothing.
167
+ await fetch(url, { method: "DELETE", headers, cache: "no-store", signal })
168
+ .then((res) => res.body?.cancel())
169
+ .catch(() => undefined);
170
+ }
171
+ }
172
+ }
173
+
174
+ const held = new Map<string, { at: number; answer: Promise<ServedMcpTool[] | null> }>();
175
+
176
+ /** #11951 D2 — the tools the MCP server at `url` serves, or `null`. Never rejects. */
177
+ export function readServedMcpTools(url: string): Promise<ServedMcpTool[] | null> {
178
+ const now = Date.now();
179
+ const hit = held.get(url);
180
+ if (hit !== undefined && now - hit.at < MCP_TOOLS_TTL_MS) return hit.answer;
181
+ const answer = exchange(url).catch(() => null);
182
+ held.set(url, { at: now, answer });
183
+ return answer;
184
+ }
185
+
186
+ /** Test seam: forget every held answer. */
187
+ export function __resetServedMcpToolsForTests(): void {
188
+ held.clear();
189
+ }
package/versions.json CHANGED
@@ -1,69 +1,69 @@
1
1
  {
2
- "@working-theory/account": "0.72.0",
3
- "@working-theory/admin-ui": "0.72.0",
4
- "@working-theory/aeo": "0.72.0",
5
- "@working-theory/agent-context": "0.72.0",
2
+ "@working-theory/account": "0.73.0",
3
+ "@working-theory/admin-ui": "0.73.0",
4
+ "@working-theory/aeo": "0.73.0",
5
+ "@working-theory/agent-context": "0.73.0",
6
6
  "@working-theory/agent-contract": "0.1.0",
7
7
  "@working-theory/agent-memory": "0.0.0",
8
8
  "@working-theory/ai": "0.1.0",
9
- "@working-theory/analytics": "0.72.0",
10
- "@working-theory/api": "0.72.0",
11
- "@working-theory/api-client": "0.72.0",
12
- "@working-theory/auth": "0.72.0",
9
+ "@working-theory/analytics": "0.73.0",
10
+ "@working-theory/api": "0.73.0",
11
+ "@working-theory/api-client": "0.73.0",
12
+ "@working-theory/auth": "0.73.0",
13
13
  "@working-theory/cda-filesystem": "0.0.0",
14
- "@working-theory/cli": "0.72.0",
15
- "@working-theory/common": "0.72.0",
14
+ "@working-theory/cli": "0.73.0",
15
+ "@working-theory/common": "0.73.0",
16
16
  "@working-theory/consent": "0.1.0",
17
- "@working-theory/content-adapters": "0.72.0",
18
- "@working-theory/content-projection": "0.72.0",
19
- "@working-theory/email": "0.72.0",
17
+ "@working-theory/content-adapters": "0.73.0",
18
+ "@working-theory/content-projection": "0.73.0",
19
+ "@working-theory/email": "0.73.0",
20
20
  "@working-theory/env": "0.0.0",
21
- "@working-theory/eslint-config": "0.72.0",
22
- "@working-theory/fumadocs-schema": "0.72.0",
21
+ "@working-theory/eslint-config": "0.73.0",
22
+ "@working-theory/fumadocs-schema": "0.73.0",
23
23
  "@working-theory/ids": "0.22.0",
24
- "@working-theory/jsonld": "0.72.0",
25
- "@working-theory/license": "0.72.0",
26
- "@working-theory/mcp-server": "0.72.0",
27
- "@working-theory/metadata": "0.72.0",
28
- "@working-theory/observability": "0.72.0",
29
- "@working-theory/page-renderer": "0.72.0",
24
+ "@working-theory/jsonld": "0.73.0",
25
+ "@working-theory/license": "0.73.0",
26
+ "@working-theory/mcp-server": "0.73.0",
27
+ "@working-theory/metadata": "0.73.0",
28
+ "@working-theory/observability": "0.73.0",
29
+ "@working-theory/page-renderer": "0.73.0",
30
30
  "@working-theory/plugin-mcp": "0.1.3",
31
- "@working-theory/schema": "0.72.0",
32
- "@working-theory/schema-pack-registry": "0.72.0",
33
- "@working-theory/schema-validator": "0.72.0",
34
- "@working-theory/sections-registry": "0.72.0",
35
- "@working-theory/site-config": "0.72.0",
36
- "@working-theory/site-pages": "0.72.0",
37
- "@working-theory/spoke-checkout": "0.7.28",
38
- "@working-theory/stripe-client": "0.72.0",
39
- "@working-theory/templates": "0.72.0",
40
- "@working-theory/theme": "0.72.0",
41
- "@working-theory/ui": "0.72.0",
42
- "@working-theory/validation": "0.72.0",
43
- "@working-theory/web-i18n": "0.72.0",
44
- "@working-theory/admin": "0.23.0",
45
- "@working-theory/api-commerce": "0.2.6",
46
- "@working-theory/api-content-write": "0.15.0",
47
- "@working-theory/api-hub": "0.7.6",
48
- "@working-theory/api-multi-tenant": "0.4.6",
49
- "@working-theory/api-scheduling": "0.6.11",
50
- "@working-theory/api-signal-collectors": "0.7.1",
51
- "@working-theory/api-workspace": "0.16.0",
31
+ "@working-theory/schema": "0.73.0",
32
+ "@working-theory/schema-pack-registry": "0.73.0",
33
+ "@working-theory/schema-validator": "0.73.0",
34
+ "@working-theory/sections-registry": "0.73.0",
35
+ "@working-theory/site-config": "0.73.0",
36
+ "@working-theory/site-pages": "0.73.0",
37
+ "@working-theory/spoke-checkout": "0.7.29",
38
+ "@working-theory/stripe-client": "0.73.0",
39
+ "@working-theory/templates": "0.73.0",
40
+ "@working-theory/theme": "0.73.0",
41
+ "@working-theory/ui": "0.73.0",
42
+ "@working-theory/validation": "0.73.0",
43
+ "@working-theory/web-i18n": "0.73.0",
44
+ "@working-theory/admin": "0.24.0",
45
+ "@working-theory/api-commerce": "0.2.7",
46
+ "@working-theory/api-content-write": "0.15.1",
47
+ "@working-theory/api-hub": "0.7.7",
48
+ "@working-theory/api-multi-tenant": "0.4.7",
49
+ "@working-theory/api-scheduling": "0.6.12",
50
+ "@working-theory/api-signal-collectors": "0.7.2",
51
+ "@working-theory/api-workspace": "0.17.0",
52
52
  "@working-theory/billing-client": "0.0.0",
53
53
  "@working-theory/branding": "0.0.0",
54
54
  "@working-theory/license-client": "0.0.0",
55
55
  "@working-theory/license-manifest": "0.0.0",
56
- "@working-theory/mcp-scheduling": "0.4.13",
57
- "@working-theory/mcp-signal-collectors": "0.2.26",
58
- "@working-theory/mcp-workspace": "0.15.0",
56
+ "@working-theory/mcp-scheduling": "0.4.14",
57
+ "@working-theory/mcp-signal-collectors": "0.2.27",
58
+ "@working-theory/mcp-workspace": "0.15.1",
59
59
  "@working-theory/partner-session": "0.0.0",
60
60
  "@working-theory/preview-protocol": "0.0.0",
61
- "@working-theory/ui-commerce": "0.72.0",
62
- "@working-theory/ui-docs-blog": "0.72.0",
63
- "@working-theory/ui-scheduling": "0.72.0",
64
- "@working-theory/ui-signal-collectors": "0.72.0",
61
+ "@working-theory/ui-commerce": "0.73.0",
62
+ "@working-theory/ui-docs-blog": "0.73.0",
63
+ "@working-theory/ui-scheduling": "0.73.0",
64
+ "@working-theory/ui-signal-collectors": "0.73.0",
65
65
  "@working-theory/web": "0.0.0",
66
66
  "@working-theory/app-api": "1.0.0",
67
67
  "@working-theory/mcp": "0.0.0",
68
- "@working-theory/database": "0.72.0"
68
+ "@working-theory/database": "0.73.0"
69
69
  }
@@ -1,47 +0,0 @@
1
- /**
2
- * Whether create-ailk's release scope marks the Free level released (#9534).
3
- *
4
- * `/.well-known/mcp.json` leaves `create_project` out while Free is held, in
5
- * step with `apps/mcp`, whose `isFreeLevelReleased()` (`src/tools/
6
- * create_project.ts`) registers the tool on the same read. apps/web cannot
7
- * import apps/mcp, so this is the same predicate over the same file:
8
- * `<cwd>/packages/create-ailk/release-scope.json` — present only in a
9
- * monorepo checkout started from the repo root, which is also the only place
10
- * the MCP server can spawn the CLI. The scope FILE alone decides (the CLI's
11
- * `AILK_RELEASE_SCOPE=ignore` bypass is not honoured); a missing or
12
- * unreadable file fails closed, with one line on stderr.
13
- */
14
-
15
- import { readFileSync } from "node:fs";
16
- import { join } from "node:path";
17
-
18
- let releaseScopeFileOverride: string | undefined;
19
-
20
- export function isFreeLevelReleased(): boolean {
21
- const file =
22
- releaseScopeFileOverride ??
23
- join(process.cwd(), "packages/create-ailk/release-scope.json");
24
- try {
25
- const scope = JSON.parse(readFileSync(file, "utf8")) as {
26
- levels?: Record<string, unknown>;
27
- };
28
- return scope.levels?.["free"] === "released";
29
- } catch (err) {
30
- console.warn(
31
- `[web] create_project is left out of /.well-known/mcp.json: release scope ${file} could not be read (${
32
- err instanceof Error ? err.message : String(err)
33
- })`,
34
- );
35
- return false;
36
- }
37
- }
38
-
39
- /** Test-only: point {@link isFreeLevelReleased} at a scope copy. */
40
- export function __setReleaseScopeFileForTest(file: string | undefined): void {
41
- if (process.env["NODE_ENV"] !== "test") {
42
- throw new Error(
43
- "__setReleaseScopeFileForTest is test-only and cannot run outside a test",
44
- );
45
- }
46
- releaseScopeFileOverride = file;
47
- }