Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/acidcat-0-26.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@hebilicious/cssforge": minor
---

Update `@acid-cat/core-ts` to 0.26.0. The catalog `cssforge.acidcat.json` now names the
surfaces of each target as `surfaces` (AcidCat 0.18). `cssforge mcp` is now the capability
`mcp.serve` of the command line, with the alias `mcp`: it appears in `cssforge list` and has
its own `--help`. The MCP server still serves `tokens.build`, `tokens.generate`,
`config.check` and the event `tokens.built`.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2030,8 +2030,8 @@ cssforge list
cssforge build --agent
```

`build`, `generate`, `check` and `--watch` are short names for the commands `tokens build`,
`tokens generate`, `config check` and `watch tokens.built`. `--watch` comes before the other
`build`, `generate`, `check`, `--watch` and `mcp` are short names for the commands `tokens build`,
`tokens generate`, `config check`, `watch tokens.built` and `mcp serve`. `--watch` comes before the other
flags: `cssforge --watch --mode css`.

| Command | JSON result on stdout |
Expand All @@ -2040,6 +2040,7 @@ flags: `cssforge --watch --mode css`.
| `generate` | `{ format, content, diagnostics, dependencies }` |
| `check` | `{ diagnostics, dependencies }` |
| `--watch` | One line per build: `{ data: { ok: true, files, … } }`, or `{ data: { ok: false, failure } }` |
| `mcp` | MCP messages only, until standard input ends. See [Agentic usage](#agentic-usage) |

Build warnings, such as a fluid type step below the legibility floor, are the `diagnostics` of
the result. They do not fail the build: the outputs are still written and the exit code stays
Expand Down
5 changes: 3 additions & 2 deletions packages/cssforge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2030,8 +2030,8 @@ cssforge list
cssforge build --agent
```

`build`, `generate`, `check` and `--watch` are short names for the commands `tokens build`,
`tokens generate`, `config check` and `watch tokens.built`. `--watch` comes before the other
`build`, `generate`, `check`, `--watch` and `mcp` are short names for the commands `tokens build`,
`tokens generate`, `config check`, `watch tokens.built` and `mcp serve`. `--watch` comes before the other
flags: `cssforge --watch --mode css`.

| Command | JSON result on stdout |
Expand All @@ -2040,6 +2040,7 @@ flags: `cssforge --watch --mode css`.
| `generate` | `{ format, content, diagnostics, dependencies }` |
| `check` | `{ diagnostics, dependencies }` |
| `--watch` | One line per build: `{ data: { ok: true, files, … } }`, or `{ data: { ok: false, failure } }` |
| `mcp` | MCP messages only, until standard input ends. See [Agentic usage](#agentic-usage) |

Build warnings, such as a fluid type step below the legibility floor, are the `diagnostics` of
the result. They do not fail the build: the outputs are still written and the exit code stays
Expand Down
15 changes: 12 additions & 3 deletions packages/cssforge/cssforge.acidcat.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Generate CSS custom properties, JSON, TypeScript and Style Dictionary tokens from a CSS Forge configuration.",
"targets": {
"cli": {
"bindings": [
"surfaces": [
"cli"
],
"description": "The cssforge command line.",
Expand All @@ -22,20 +22,23 @@
},
"generate": {
"capability": "tokens.generate"
},
"mcp": {
"capability": "mcp.serve"
}
},
"default": "build"
}
}
},
"library": {
"bindings": [
"surfaces": [
"typescript"
],
"description": "Typed calls from code, through @hebilicious/cssforge/capabilities."
},
"mcp": {
"bindings": [
"surfaces": [
"mcp"
],
"description": "The MCP server of `cssforge mcp`, for agents."
Expand Down Expand Up @@ -181,6 +184,12 @@
},
"readOnly": true
},
"mcp.serve": {
"description": "Serve the CSS Forge capabilities to an agent over MCP, through standard input and output, until the input ends.",
"audience": [
"cli"
]
},
"tokens.build": {
"description": "Generate the outputs of a CSS Forge configuration and write them to files: CSS custom properties, JSON, TypeScript and Style Dictionary tokens. Every output is generated before the first file is written, so an invalid configuration leaves the previous files in place. The result lists the files written and the warnings of the configuration.",
"audience": [
Expand Down
2 changes: 1 addition & 1 deletion packages/cssforge/jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"./capabilities": "./src/capabilities.ts"
},
"imports": {
"@acid-cat/core-ts": "npm:@acid-cat/core-ts@0.13.0",
"@acid-cat/core-ts": "npm:@acid-cat/core-ts@0.26.0",
"chokidar": "npm:chokidar@4.0.3",
"colorjs.io": "npm:colorjs.io@0.5.2",
"utopia-core": "npm:utopia-core@1.6.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/cssforge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@
"cssforge.acidcat.json"
],
"dependencies": {
"@acid-cat/core-ts": "0.13.0",
"@acid-cat/core-ts": "0.26.0",
"chokidar": "4.0.3",
"colorjs.io": "0.5.2",
"utopia-core": "1.6.0",
"zod": "4.6.7"
},
"devDependencies": {
"@acid-cat/cli": "0.13.0",
"@acid-cat/cli": "0.26.0",
"@biomejs/biome": "2.4.2",
"@tanstack/eslint-plugin-oracles": "0.0.1",
"@tanstack/oracles": "0.0.1",
Expand Down
49 changes: 43 additions & 6 deletions packages/cssforge/src/capabilities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* The capabilities of the CSS Forge tools. The command line (`cssforge`), the
* MCP server (`cssforge mcp`) and code (the `library` target) all use them, and
* MCP server (`cssforge mcp`, the capability `mcp.serve`) and code (the `library` target) all use them, and
* the catalog `cssforge.acidcat.json` describes them.
*
* @example
Expand All @@ -25,10 +25,12 @@
*/

import { resolve } from "node:path";
import process from "node:process";
import type {
App,
Capability,
Emitted,
EmptyInput,
EventDefinition,
Result,
ToJsonSchema,
Expand All @@ -41,6 +43,7 @@ import {
eventsFor,
fail,
ok,
runMcpStdio,
} from "@acid-cat/core-ts";
import { z } from "zod";
import {
Expand All @@ -65,6 +68,7 @@ import {
} from "./forge.ts";
import { type ColorFormat, diagnosticCodes } from "./lib.ts";
import { supportedColorFormats } from "./modules/colors.ts";
import { version } from "./version.ts";

/**
* `createSession` opens a session on {@link cssforge}, for typed calls from
Expand All @@ -74,7 +78,7 @@ import { supportedColorFormats } from "./modules/colors.ts";
*/
export { CallFailureError, createSession } from "@acid-cat/core-ts";

/** The JSON Schema of a schema of {@link cssforge}, for the catalog and the bindings. */
/** The JSON Schema of a schema of {@link cssforge}, for the catalog and the surfaces. */
export const toJsonSchema: ToJsonSchema = (schema) => {
if (!(schema instanceof z.ZodType))
throw new Error("Each CSS Forge schema is a Zod schema.");
Expand Down Expand Up @@ -418,6 +422,38 @@ export const configCheck: Capability<
),
});

/**
* `cssforge mcp`: serve the capabilities of the target `mcp` to one agent, through standard
* input and output. Only the command line has it, so an agent cannot start a second server.
*/
export const mcpServe: Capability<
ForgeContext,
EmptyInput,
null,
Record<never, never>,
never,
"mcp.serve",
never
> = capability({
id: "mcp.serve",
description:
"Serve the CSS Forge capabilities to an agent over MCP, through standard input and output, until the input ends.",
audience: ["cli"],
run: async (_input, { application, signal }) => {
// An interrupt cancels the call. Closing the input then ends the server at once,
// instead of when the command line runs out of time.
signal.addEventListener("abort", () => process.stdin.destroy(), { once: true });
try {
// Standard output carries only MCP messages from here on.
await runMcpStdio(application, { name: "cssforge", version, toJsonSchema });
} catch (error) {
// A destroyed input reads as a premature close.
if (!signal.aborted) throw error;
}
return ok(null);
},
});

// --- Events --------------------------------------------------------------------------------

/** `cssforge --watch`: build now and after each change of the configuration. */
Expand Down Expand Up @@ -453,7 +489,7 @@ export const cssforge: App<ForgeContext> = defineApp<ForgeContext>({
"Generate CSS custom properties, JSON, TypeScript and Style Dictionary tokens from a CSS Forge configuration.",
targets: {
cli: {
bindings: ["cli"],
surfaces: ["cli"],
description: "The cssforge command line.",
cli: {
aliases: {
Expand All @@ -462,18 +498,19 @@ export const cssforge: App<ForgeContext> = defineApp<ForgeContext>({
generate: cliAlias(tokensGenerate),
check: cliAlias(configCheck),
"--watch": cliAlias(tokensBuilt),
mcp: cliAlias(mcpServe),
},
},
},
mcp: {
bindings: ["mcp"],
surfaces: ["mcp"],
description: "The MCP server of `cssforge mcp`, for agents.",
},
library: {
bindings: ["typescript"],
surfaces: ["typescript"],
description: "Typed calls from code, through @hebilicious/cssforge/capabilities.",
},
},
capabilities: [tokensBuild, tokensGenerate, configCheck],
capabilities: [tokensBuild, tokensGenerate, configCheck, mcpServe],
events: [tokensBuilt],
});
42 changes: 4 additions & 38 deletions packages/cssforge/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node
/**
* The `cssforge` program: run it, do not import it. Its commands are the capabilities of
* `capabilities.ts`, and `cssforge mcp` serves the same capabilities to an
* agent as an MCP server through standard input and output.
* `capabilities.ts`. `cssforge mcp` (the capability `mcp.serve`) serves the same capabilities
* to an agent as an MCP server through standard input and output.
*
* On a terminal, the output is for a person. For a script, in CI or for a
* coding agent, it is JSON. `--human` and `--agent`, or `--output human|agent`, choose.
Expand All @@ -12,8 +12,8 @@

import { relative } from "node:path";
import process from "node:process";
import type { CliCommand, CliUi } from "@acid-cat/core-ts";
import { cliFormat, cliMain, runMcpStdio } from "@acid-cat/core-ts";
import type { CliUi } from "@acid-cat/core-ts";
import { cliFormat, cliMain } from "@acid-cat/core-ts";
import {
type BuiltPayload,
configCheck,
Expand All @@ -30,39 +30,6 @@ import { version } from "./version.ts";

const context: ForgeContext = { cwd: process.cwd() };

/** `cssforge mcp`: the MCP server, through standard input and output. */
const mcpCommand: CliCommand<ForgeContext> = {
name: "mcp",
description:
"Serve the cssforge commands to an agent over MCP, through standard input and output.",
run: async (args, { stdout, stderr, signal }) => {
const [first] = args;
if (first === "--help" || first === "-h") {
stdout(`Usage: cssforge mcp\n\n${mcpCommand.description}`);
return 0;
}
if (first !== undefined) {
stderr(`Unexpected argument ${first}.`);
return 3;
}

// An interrupt closes the input, which ends the session and the server.
signal?.addEventListener("abort", () => process.stdin.destroy(), { once: true });
try {
// Standard output carries only MCP messages from here on.
await runMcpStdio(
cssforge,
{ name: "cssforge", version, context, toJsonSchema },
{ input: process.stdin, write: (text) => process.stdout.write(text) },
);
} catch (error) {
// A closed input reads as a premature close.
if (signal?.aborted !== true) throw error;
}
return signal?.aborted === true ? 130 : 0;
},
};

// --- Human output ----------------------------------------------------------------------------

/** A path as a person types it: relative to the working directory. */
Expand Down Expand Up @@ -128,7 +95,6 @@ cliMain(cssforge, {
// The version is in the help so that a published build shows which one it is. The bundler
// plugin is a separate package, so the help names it for a reader who only has this one.
description: `cssforge ${version}\n\n${cssforge.description}\n\nTo generate the tokens inside a build instead (Vite, webpack, Rollup and the other bundlers\nunplugin supports), use the bundler plugin: @hebilicious/cssforge-unplugin.`,
commands: [mcpCommand],
output: {
default: "auto",
flags: [
Expand Down
28 changes: 28 additions & 0 deletions packages/cssforge/tests/acidcat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,31 @@ test("mcp - cssforge mcp serves the capabilities as tools", async () => {
child.kill();
}
}, 30_000);

test("mcp - an interrupt stops cssforge mcp at once", async () => {
const { child, nextLine, exited } = startCli(["mcp"]);

try {
child.stdin.write(
`${JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
protocolVersion: "2025-06-18",
capabilities: {},
clientInfo: { name: "cssforge-test", version: "1.0.0" },
},
})}\n`,
);
expect((await nextLine()).id).toEqual(1);

const interrupted = performance.now();
child.kill("SIGINT");
expect(await exited).toEqual(130);
// The command line forces an exit one second after an interrupt; the server ends before.
expect(performance.now() - interrupted).toBeLessThan(900);
} finally {
child.kill();
}
}, 30_000);
Loading
Loading