leglas-mcp 0.2.0 → 0.4.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/README.md +125 -24
- package/dist/bin.js +11 -4
- package/dist/channel.d.ts +2 -1
- package/dist/channel.js +10 -1
- package/dist/engagement.d.ts +19 -0
- package/dist/engagement.js +68 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/project.d.ts +78 -0
- package/dist/project.js +102 -0
- package/dist/tools.d.ts +4 -1
- package/dist/tools.js +43 -12
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -14,6 +14,12 @@
|
|
|
14
14
|
<a href="LICENSE"><img src="https://img.shields.io/npm/l/leglas" alt="license"></a>
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
|
+
<p align="center">
|
|
18
|
+
<img src="https://raw.githubusercontent.com/FredAmartey/leglas/pr-assets/rail-single.jpg" width="900" alt="The Leglas interface: a rail of three design directions on the left, and the selected one running as the real app filling the rest of the window" />
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<p align="center"><i>Every direction in the rail, the selected one running as your actual app. Arrow keys flip between them.</i></p>
|
|
22
|
+
|
|
17
23
|
Code is becoming the source of truth. Features go from prompt to
|
|
18
24
|
working code in minutes, and mockups/design files eventually lag behind the product and drift out of sync. The
|
|
19
25
|
fastest teams already design in the medium they ship. Leglas is built
|
|
@@ -39,6 +45,12 @@ and sessions that clean up after themselves.
|
|
|
39
45
|
|
|
40
46
|
## What you can do with Leglas
|
|
41
47
|
|
|
48
|
+
<p align="center">
|
|
49
|
+
<img src="https://raw.githubusercontent.com/FredAmartey/leglas/pr-assets/compare-artboards.jpg" width="900" alt="The Leglas interface: a rail of three design directions on the left, and two of them running side by side as the real app, each labelled with its name." />
|
|
50
|
+
</p>
|
|
51
|
+
|
|
52
|
+
<p align="center"><i>Two directions for the same page, running side by side as the actual app.</i></p>
|
|
53
|
+
|
|
42
54
|
- Pick any two variations for side-by-side comparison when it gets hard to choose.
|
|
43
55
|
- Name each direction, drag to reorder and organise your variants, set aside the ones that
|
|
44
56
|
don't feel right. Your actions on every idea survive a long exploration.
|
|
@@ -51,12 +63,23 @@ and sessions that clean up after themselves.
|
|
|
51
63
|
the spread, your agent supplies the taste.
|
|
52
64
|
- Ask for changes without leaving the comparison: describe what you
|
|
53
65
|
want on the direction you're looking at, and Leglas turns it into a
|
|
54
|
-
precise request for your agent, file path included.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
66
|
+
precise request for your agent, file path included. The composer
|
|
67
|
+
carries its own agent picker, the way every chat you already use
|
|
68
|
+
carries a model picker: the CLIs found on your machine (Claude Code,
|
|
69
|
+
Codex, Cursor) are one click away next to the send button, each asked
|
|
70
|
+
for its login status so a signed-out CLI says so before a run instead
|
|
71
|
+
of failing one. Not on that list? Type the command that runs your
|
|
72
|
+
agent once, in the same menu, and Leglas hands it each request; or
|
|
73
|
+
connect an agent Leglas can't spawn (an IDE panel, an MCP host) with
|
|
74
|
+
the copy-paste MCP wiring behind "Connect another agent". Each run
|
|
75
|
+
reports in a card above the field: who is working, what file they
|
|
76
|
+
are touching, how long it has been, a stop button while it runs and
|
|
77
|
+
retry when it fails. Your agent, your subscription, no keys.
|
|
78
|
+
Prefer a terminal? `npx leglas watch` is the same loop with the
|
|
79
|
+
agent's own output scrolling by. In Claude Code, the Leglas MCP
|
|
80
|
+
server can also push each request straight into your open session as
|
|
81
|
+
a channel event (channels are a research preview: start Claude Code
|
|
82
|
+
with `--dangerously-load-development-channels
|
|
60
83
|
server:<your leglas server name>`).
|
|
61
84
|
- Keep the winner with one command. Leglas moves it into your source
|
|
62
85
|
tree and clears the exploration away.
|
|
@@ -76,66 +99,110 @@ preview of your app root. Add a config file to compare more than one thing.
|
|
|
76
99
|
|
|
77
100
|
It works with whatever you're building in. Leglas never imports or
|
|
78
101
|
executes your framework, so the target can be Next, Vite, Remix,
|
|
79
|
-
SvelteKit, Astro, or a folder of static files.
|
|
80
|
-
|
|
102
|
+
SvelteKit, Astro, or a folder of static files.
|
|
103
|
+
|
|
104
|
+
## Install
|
|
105
|
+
|
|
106
|
+
There is nothing you have to install: `npx leglas` fetches the CLI on
|
|
107
|
+
first use and starts from npm's cache after that, and every instruction
|
|
108
|
+
Leglas writes for agents uses the same form, so a fresh clone works
|
|
109
|
+
with no setup at all. Requires Node 24 or newer.
|
|
110
|
+
|
|
111
|
+
Two optional upgrades:
|
|
112
|
+
|
|
113
|
+
- `npm install -D leglas` pins the version in a project. Teammates and
|
|
114
|
+
CI get the same Leglas from their normal install, and `npx` resolves
|
|
115
|
+
the local copy from then on.
|
|
116
|
+
- `npm install -g leglas` is for typing `leglas` without the prefix.
|
|
81
117
|
|
|
82
118
|
## Working with coding agents
|
|
83
119
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
120
|
+
The fastest way in is the agent skill:
|
|
121
|
+
|
|
122
|
+
```sh
|
|
123
|
+
npx skills add FredAmartey/leglas
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
One install, and your agent recognises "give me a few directions for the
|
|
127
|
+
pricing page" as a Leglas exploration in any project, including ones
|
|
128
|
+
that have never seen Leglas. It sets the project up itself and gets to
|
|
129
|
+
work.
|
|
130
|
+
|
|
131
|
+
In a project, run `npx leglas init` once. It writes a section into your
|
|
132
|
+
project's `AGENTS.md`, creates a starter config, and gitignores Leglas's
|
|
133
|
+
working directory. That section travels with the repo, so Claude Code,
|
|
134
|
+
Cursor, Codex, or whatever you switch to next opens the project already
|
|
135
|
+
knowing how to add design directions to it. Every command accepts
|
|
136
|
+
`--json` and prints a single machine-readable envelope, so agents drive
|
|
137
|
+
the same CLI you do.
|
|
91
138
|
|
|
92
139
|
The instructions center on one rule: add beside what exists, never
|
|
93
140
|
rewrite it. Two directions that rewrite the same file cannot render from
|
|
94
141
|
one server, and asking an agent to "make the hero calmer" tempts it to
|
|
95
142
|
edit the hero. The supporting commands:
|
|
96
143
|
|
|
97
|
-
- `leglas explore hero --count 6` briefs the exploration: what the set
|
|
144
|
+
- `npx leglas explore hero --count 6` briefs the exploration: what the set
|
|
98
145
|
is for, why it only works if the six genuinely disagree, and how each
|
|
99
146
|
direction registers. Unbriefed, six requests come back as six variants
|
|
100
147
|
of one idea. With `--based-on "Aurora"` the goal flips: six deliberate
|
|
101
148
|
variants of a direction you already like, and drifting into a new
|
|
102
149
|
direction is the failure. The designs themselves are the agent's;
|
|
103
150
|
Leglas prescribes none.
|
|
104
|
-
- `leglas new hero --from src/Hero.tsx` scaffolds a switcher under
|
|
151
|
+
- `npx leglas new hero --from src/Hero.tsx` scaffolds a switcher under
|
|
105
152
|
`.leglas/variants/hero/`. With `--from`, the baseline re-exports your
|
|
106
153
|
real component, so you never compare against a stale copy. Leglas
|
|
107
154
|
prints the one line to add in your component and does not edit it,
|
|
108
155
|
because rewriting a file it does not understand is how a tool breaks a
|
|
109
156
|
codebase. Scaffolded branch points return the fallback in production
|
|
110
157
|
builds, so a committed one cannot expose an unreleased direction.
|
|
111
|
-
- `leglas classify --change package.json --rewrite src/theme.css` answers
|
|
158
|
+
- `npx leglas classify --change package.json --rewrite src/theme.css` answers
|
|
112
159
|
where a direction should live before it is written. Changing
|
|
113
160
|
dependencies, build configuration, or an existing file's behaviour
|
|
114
161
|
cannot be additive, so those directions build on their own git branch
|
|
115
162
|
and register with `leglas add --branch`. Everything else stays in-app,
|
|
116
163
|
where switching is instant.
|
|
117
|
-
- `leglas show "Aurora" --json` answers for one direction: its entry, the
|
|
164
|
+
- `npx leglas show "Aurora" --json` answers for one direction: its entry, the
|
|
118
165
|
source file behind it, the variants based on it, what it is being compared
|
|
119
166
|
against, and anything still pending on it. Copying a direction from the
|
|
120
167
|
rail hands over a block that ends in this command, so an agent given the
|
|
121
168
|
block can go and get the rest.
|
|
122
|
-
- `leglas keep "Aurora" --to src/components/hero.tsx` moves the winner
|
|
169
|
+
- `npx leglas keep "Aurora" --to src/components/hero.tsx` moves the winner
|
|
123
170
|
into real source and ends the exploration.
|
|
124
171
|
|
|
125
172
|
Asking for a change works from the interface too. Type what you want
|
|
126
173
|
changed into the field under the rail (or press `R`) and Leglas composes a
|
|
127
174
|
prompt naming the direction and the file behind it, copies it to your
|
|
128
175
|
clipboard, and queues it. The direction it means is the one highlighted
|
|
129
|
-
directly above the field. Your agent drains the queue with `leglas requests --json` and clears
|
|
176
|
+
directly above the field. Your agent drains the queue with `npx leglas requests --json` and clears
|
|
130
177
|
it with `--clear`. Leglas runs no model of its own; your agent already
|
|
131
178
|
knows your conventions and your taste.
|
|
132
179
|
|
|
180
|
+
The line under the field is the whole status: whether anything is
|
|
181
|
+
listening, what you have queued, and when it has been picked up.
|
|
182
|
+
|
|
183
|
+
<p align="center">
|
|
184
|
+
<img src="https://raw.githubusercontent.com/FredAmartey/leglas/pr-assets/field-idle.png" width="290" alt="The change field reading: Enter queues it for npx leglas requests" />
|
|
185
|
+
<img src="https://raw.githubusercontent.com/FredAmartey/leglas/pr-assets/field-queued.png" width="290" alt="The change field reading: one change queued for your agent" />
|
|
186
|
+
<img src="https://raw.githubusercontent.com/FredAmartey/leglas/pr-assets/field-pickedup.png" width="290" alt="The change field reading: Your agent is on it" />
|
|
187
|
+
</p>
|
|
188
|
+
|
|
189
|
+
<p align="center"><i>Nothing waiting, then a request queued, then an agent that has taken it.</i></p>
|
|
190
|
+
|
|
191
|
+
Pick an agent once and the same line shows it working: which file it is
|
|
192
|
+
editing, a cancel if you change your mind, a retry when a run goes
|
|
193
|
+
wrong. `npx leglas watch` in another terminal is the same loop with the
|
|
194
|
+
agent's own output in view, and it needs no flag once an agent has been
|
|
195
|
+
picked in the interface.
|
|
196
|
+
|
|
133
197
|
### MCP server
|
|
134
198
|
|
|
135
199
|
For agent hosts that cannot run shell commands, `leglas-mcp` exposes the
|
|
136
200
|
same operations as MCP tools over stdio: `start`, `add`, `list`, `show`,
|
|
137
201
|
`classify`, `explore`, `scaffold`, `keep`, `requests`, and `init`. Each
|
|
138
202
|
tool calls exactly what the CLI calls and returns the same envelope.
|
|
203
|
+
`watch` is the one command with no tool behind it: it is a loop that
|
|
204
|
+
holds a terminal open, and on a host that speaks channels the server
|
|
205
|
+
already pushes each request into the session as it arrives.
|
|
139
206
|
|
|
140
207
|
```sh
|
|
141
208
|
claude mcp add leglas -- npx -y leglas-mcp
|
|
@@ -147,9 +214,34 @@ Or in `.mcp.json`:
|
|
|
147
214
|
{ "mcpServers": { "leglas": { "command": "npx", "args": ["-y", "leglas-mcp"] } } }
|
|
148
215
|
```
|
|
149
216
|
|
|
150
|
-
The host's working directory names the project
|
|
151
|
-
|
|
152
|
-
|
|
217
|
+
The host's working directory names the project, the same contract as the
|
|
218
|
+
CLI. A host that starts the server somewhere else is asked where the
|
|
219
|
+
project is, over MCP roots. The `start` tool boots the viewer and returns
|
|
220
|
+
its URL, and anything it started stops when the session ends.
|
|
221
|
+
|
|
222
|
+
### As an Agent Plugin
|
|
223
|
+
|
|
224
|
+
The repository is also an [Agent Plugin](https://agent-plugins.org), the
|
|
225
|
+
open standard for shipping Agent Skills and MCP server configuration in
|
|
226
|
+
one format. Clients that implement it install the skill and the server
|
|
227
|
+
together, instead of the two steps above. It is a layout rather than a
|
|
228
|
+
build: `plugin.json` and `mcp.json` at the root, the skill in
|
|
229
|
+
`skills/leglas/`, nothing generated.
|
|
230
|
+
|
|
231
|
+
An Agent Plugins client starts a plugin's server in the plugin's own
|
|
232
|
+
install directory rather than the project, so on that path the working
|
|
233
|
+
directory names a copy of Leglas and nothing else. The server therefore
|
|
234
|
+
takes the project from the workspace the host declares over MCP roots,
|
|
235
|
+
and the working directory only when it sits inside one. If a host offers
|
|
236
|
+
neither, `LEGLAS_PROJECT_DIR` names the project outright; without it the
|
|
237
|
+
tools report that there is no project rather than writing into a plugin
|
|
238
|
+
cache. `mcp.json` passes `${PLUGIN_ROOT}` for exactly that check, and
|
|
239
|
+
nothing else.
|
|
240
|
+
|
|
241
|
+
The plugin's version covers the skill and the configuration, not the
|
|
242
|
+
server it launches: `npx` fetches the current `leglas-mcp` the same way
|
|
243
|
+
every `npx leglas` in these instructions fetches the current CLI, which
|
|
244
|
+
keeps both faces of Leglas on one version in a project they share.
|
|
153
245
|
|
|
154
246
|
## Configuration
|
|
155
247
|
|
|
@@ -203,6 +295,15 @@ what you want for the last two directions in contention: press `C`, or
|
|
|
203
295
|
hover a direction and press its compare button, and it becomes the right
|
|
204
296
|
pane while the active direction holds the left.
|
|
205
297
|
|
|
298
|
+
A split does not hand each side half the room. An app given half the room
|
|
299
|
+
crosses its own breakpoints and draws a different design, so you would be
|
|
300
|
+
choosing between two narrow renderings of directions meant for the wide
|
|
301
|
+
one. Instead each side is drawn at the width it had on its own and scaled
|
|
302
|
+
to fit, keeping the same proportions, so nothing reflows and flipping and
|
|
303
|
+
splitting agree about what the design is. Each pane says the width it is
|
|
304
|
+
drawn at and the scale it is shown at. If you want the narrow rendering,
|
|
305
|
+
that is what the tools popover's "Scale each side to fit" switch is for.
|
|
306
|
+
|
|
206
307
|
Arrows move between directions, `1` to `9` jump straight to one, `R` asks
|
|
207
308
|
for a change to the one you are on, `Cmd K` (`Ctrl K` elsewhere) searches
|
|
208
309
|
and `B` collapses the rail. Press `?` for the whole keymap.
|
package/dist/bin.js
CHANGED
|
@@ -3,10 +3,12 @@ import { createRequire } from "node:module";
|
|
|
3
3
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
4
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
5
|
import { CHANNEL_CAPABILITY, CHANNEL_INSTRUCTIONS, startChannel } from "./channel.js";
|
|
6
|
+
import { hostProject } from "./project.js";
|
|
6
7
|
import { registerLeglasTools } from "./tools.js";
|
|
7
8
|
/**
|
|
8
|
-
* The stdio face.
|
|
9
|
-
*
|
|
9
|
+
* The stdio face. A host that spawns this in the project directory gets the
|
|
10
|
+
* CLI's contract, where the working directory names the project; a host that
|
|
11
|
+
* spawns it somewhere else is asked where the project is. See project.ts.
|
|
10
12
|
*/
|
|
11
13
|
function version() {
|
|
12
14
|
const require = createRequire(import.meta.url);
|
|
@@ -18,7 +20,12 @@ const server = new McpServer({ name: "leglas", version: version() },
|
|
|
18
20
|
// speak channels; on Claude Code they let change requests from the
|
|
19
21
|
// interface arrive in the open session as events.
|
|
20
22
|
{ capabilities: { experimental: CHANNEL_CAPABILITY }, instructions: CHANNEL_INSTRUCTIONS });
|
|
21
|
-
const
|
|
23
|
+
const project = hostProject(server.server, {
|
|
24
|
+
cwd: process.cwd(),
|
|
25
|
+
override: process.env["LEGLAS_PROJECT_DIR"],
|
|
26
|
+
pluginRoot: process.env["LEGLAS_PLUGIN_ROOT"],
|
|
27
|
+
});
|
|
28
|
+
const tools = registerLeglasTools(server, { project });
|
|
22
29
|
let channel = null;
|
|
23
30
|
let stopping = false;
|
|
24
31
|
const shutdown = async () => {
|
|
@@ -39,4 +46,4 @@ transport.onclose = () => {
|
|
|
39
46
|
};
|
|
40
47
|
await server.connect(transport);
|
|
41
48
|
// Only after connect: a notification with no transport throws.
|
|
42
|
-
channel = startChannel(server, {
|
|
49
|
+
channel = startChannel(server, { project });
|
package/dist/channel.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
2
|
import { type PendingRequest } from "leglas";
|
|
3
|
+
import type { Project } from "./project.js";
|
|
3
4
|
/**
|
|
4
5
|
* The push face, for hosts that treat an MCP server as a channel.
|
|
5
6
|
*
|
|
@@ -51,7 +52,7 @@ export type Channel = {
|
|
|
51
52
|
* because the queue file still holds the request.
|
|
52
53
|
*/
|
|
53
54
|
export declare function startChannel(server: McpServer, options: {
|
|
54
|
-
|
|
55
|
+
project: Project;
|
|
55
56
|
pollMs?: number;
|
|
56
57
|
/** Tests gate this to force overlapping polls; production reads the file. */
|
|
57
58
|
read?: (cwd: string) => Promise<PendingRequest[]>;
|
package/dist/channel.js
CHANGED
|
@@ -70,7 +70,16 @@ export function startChannel(server, options) {
|
|
|
70
70
|
}
|
|
71
71
|
};
|
|
72
72
|
const push = async () => {
|
|
73
|
-
|
|
73
|
+
// The first poll waits here for the host to initialize, which is what
|
|
74
|
+
// makes the queue this reads the project's rather than whatever directory
|
|
75
|
+
// the process happened to start in.
|
|
76
|
+
const located = await options.project.locate();
|
|
77
|
+
if (!located.ok) {
|
|
78
|
+
// Settled for the life of the process, so there is no queue coming.
|
|
79
|
+
clearInterval(timer);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const fresh = unpushed(await read(located.directory), pushed);
|
|
74
83
|
for (const request of fresh) {
|
|
75
84
|
pushed.add(request.id);
|
|
76
85
|
try {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type EngagementDeps = {
|
|
2
|
+
post?: (watching: boolean) => Promise<void>;
|
|
3
|
+
setInterval?: (callback: () => void, milliseconds: number) => unknown;
|
|
4
|
+
clearInterval?: (handle: unknown) => void;
|
|
5
|
+
now?: () => number;
|
|
6
|
+
};
|
|
7
|
+
export type Engagement = {
|
|
8
|
+
/**
|
|
9
|
+
* Note queue activity: starts the beat, or extends it. The returned
|
|
10
|
+
* promise settles once the server has had its chance to register the
|
|
11
|
+
* engagement; the caller awaits it before reading the queue, because the
|
|
12
|
+
* embedded runner backs off only after that registration lands. Watch
|
|
13
|
+
* closes the same handoff race the same way with its first heartbeat.
|
|
14
|
+
*/
|
|
15
|
+
touch(): Promise<void>;
|
|
16
|
+
/** End the engagement and say so, as far as one best-effort post goes. */
|
|
17
|
+
stop(): Promise<void>;
|
|
18
|
+
};
|
|
19
|
+
export declare function createEngagement(deps?: EngagementDeps): Engagement;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { DEFAULT_PORT, LEGLAS_PREFIX } from "leglas";
|
|
2
|
+
/**
|
|
3
|
+
* Tell the Leglas server an agent is working the queue over MCP.
|
|
4
|
+
*
|
|
5
|
+
* The embedded runner yields to an attached watcher, and an MCP host driving
|
|
6
|
+
* the requests tool deserves the same right of way: without this, the runner
|
|
7
|
+
* could grab a request that arrives while the host's agent is mid-change in
|
|
8
|
+
* the same tree. Attachment is engagement, not connection: an MCP server sits
|
|
9
|
+
* connected to its host for hours doing nothing, so merely being alive proves
|
|
10
|
+
* nothing. Touch marks real queue activity, the beat keeps the server's
|
|
11
|
+
* window fresh, and a quiet spell lets it lapse.
|
|
12
|
+
*/
|
|
13
|
+
/** How often the server hears from an engaged session; matches watch. */
|
|
14
|
+
const BEAT_MS = 2000;
|
|
15
|
+
/** Queue work older than this no longer counts as engagement. */
|
|
16
|
+
const ENGAGEMENT_MS = 120_000;
|
|
17
|
+
/** A beat is worth a moment, never a stall. */
|
|
18
|
+
const POST_TIMEOUT_MS = 1000;
|
|
19
|
+
function defaultPost(watching) {
|
|
20
|
+
// Best effort at the default port: the queue is a file and every tool works
|
|
21
|
+
// without the server, so a beat that lands nowhere costs nothing. LEGLAS_PORT
|
|
22
|
+
// covers the server that had to bind elsewhere.
|
|
23
|
+
const port = Number(process.env.LEGLAS_PORT ?? "") || DEFAULT_PORT;
|
|
24
|
+
return fetch(`http://localhost:${port}${LEGLAS_PREFIX}/api/watch`, {
|
|
25
|
+
method: "POST",
|
|
26
|
+
headers: { "content-type": "application/json" },
|
|
27
|
+
body: JSON.stringify({ watching }),
|
|
28
|
+
signal: AbortSignal.timeout(POST_TIMEOUT_MS),
|
|
29
|
+
}).then(() => { }, () => { });
|
|
30
|
+
}
|
|
31
|
+
export function createEngagement(deps = {}) {
|
|
32
|
+
const post = deps.post ?? defaultPost;
|
|
33
|
+
const setEvery = deps.setInterval ?? ((callback, milliseconds) => setInterval(callback, milliseconds));
|
|
34
|
+
const clearEvery = deps.clearInterval ?? ((handle) => clearInterval(handle));
|
|
35
|
+
const now = deps.now ?? (() => Date.now());
|
|
36
|
+
let timer = null;
|
|
37
|
+
let lastTouch = 0;
|
|
38
|
+
const quiet = () => {
|
|
39
|
+
if (timer === null)
|
|
40
|
+
return;
|
|
41
|
+
clearEvery(timer);
|
|
42
|
+
timer = null;
|
|
43
|
+
};
|
|
44
|
+
const beat = () => {
|
|
45
|
+
if (now() - lastTouch > ENGAGEMENT_MS) {
|
|
46
|
+
quiet();
|
|
47
|
+
void post(false);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
void post(true);
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
touch() {
|
|
54
|
+
lastTouch = now();
|
|
55
|
+
// Mid-cycle the server already knows: nothing to wait for.
|
|
56
|
+
if (timer !== null)
|
|
57
|
+
return Promise.resolve();
|
|
58
|
+
timer = setEvery(beat, BEAT_MS);
|
|
59
|
+
return post(true).catch(() => { });
|
|
60
|
+
},
|
|
61
|
+
async stop() {
|
|
62
|
+
const wasBeating = timer !== null;
|
|
63
|
+
quiet();
|
|
64
|
+
if (wasBeating)
|
|
65
|
+
await post(false);
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,3 +2,5 @@ export { registerLeglasTools } from "./tools.js";
|
|
|
2
2
|
export type { LeglasTools } from "./tools.js";
|
|
3
3
|
export { CHANNEL_CAPABILITY, CHANNEL_INSTRUCTIONS, CHANNEL_POLL_MS, channelEvent, startChannel, unpushed, } from "./channel.js";
|
|
4
4
|
export type { Channel, ChannelEvent } from "./channel.js";
|
|
5
|
+
export { UNRESOLVED_PROJECT, fixedProject, hostProject } from "./project.js";
|
|
6
|
+
export type { HostProjectOptions, Located, Project, RootsHost } from "./project.js";
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which project the tools act on.
|
|
3
|
+
*
|
|
4
|
+
* Every other face of Leglas learns this the same easy way: the CLI runs in
|
|
5
|
+
* the project, so the working directory is the answer. An MCP server is not
|
|
6
|
+
* always so lucky. An Agent Plugins client starts a plugin's server in the
|
|
7
|
+
* plugin's own install directory (Agent Plugins 1.0.0 §7.2.1), which holds a
|
|
8
|
+
* copy of Leglas and never a project anyone is designing. Taken at face value
|
|
9
|
+
* there, the working directory would send `init` into a plugin cache, leave
|
|
10
|
+
* the rail empty, and report all of it as success.
|
|
11
|
+
*
|
|
12
|
+
* So the directory is asked for rather than assumed:
|
|
13
|
+
*
|
|
14
|
+
* 1. `LEGLAS_PROJECT_DIR`, when someone has said outright where to work.
|
|
15
|
+
* 2. The workspace the host declares over MCP roots, which is the project it
|
|
16
|
+
* has open. The working directory wins when it sits inside one of those
|
|
17
|
+
* roots, because a host started in `packages/app` means that, not the
|
|
18
|
+
* repository above it. Several roots and no match takes the first, which
|
|
19
|
+
* is a guess, and the reason the override above exists.
|
|
20
|
+
* 3. The working directory, which is the right answer for every host that
|
|
21
|
+
* starts the server in the project, and the reason nothing changes for
|
|
22
|
+
* `claude mcp add` or a hand-written `.mcp.json`.
|
|
23
|
+
*
|
|
24
|
+
* When none of those name a project, the tools say so. Refusing is the whole
|
|
25
|
+
* point: a wrong directory is worse than no directory, because it writes.
|
|
26
|
+
*/
|
|
27
|
+
export type Located = {
|
|
28
|
+
ok: true;
|
|
29
|
+
directory: string;
|
|
30
|
+
} | {
|
|
31
|
+
ok: false;
|
|
32
|
+
reason: string;
|
|
33
|
+
};
|
|
34
|
+
export type Project = {
|
|
35
|
+
/**
|
|
36
|
+
* The project directory, resolved once and held for the life of the process.
|
|
37
|
+
* A viewer, a request queue, and a channel all bind to one project, so a
|
|
38
|
+
* session that changed its mind halfway would already have a server running
|
|
39
|
+
* against the old one.
|
|
40
|
+
*/
|
|
41
|
+
locate(): Promise<Located>;
|
|
42
|
+
};
|
|
43
|
+
/** A project fixed to one directory: what an embedder or a test already knows. */
|
|
44
|
+
export declare function fixedProject(directory: string): Project;
|
|
45
|
+
/**
|
|
46
|
+
* The part of an MCP server this needs. The SDK's `Server` satisfies it
|
|
47
|
+
* structurally, which keeps the resolution testable without a transport.
|
|
48
|
+
*/
|
|
49
|
+
export type RootsHost = {
|
|
50
|
+
getClientCapabilities(): {
|
|
51
|
+
roots?: unknown;
|
|
52
|
+
} | undefined;
|
|
53
|
+
listRoots(): Promise<{
|
|
54
|
+
roots: {
|
|
55
|
+
uri: string;
|
|
56
|
+
}[];
|
|
57
|
+
}>;
|
|
58
|
+
oninitialized?: (() => void) | undefined;
|
|
59
|
+
};
|
|
60
|
+
export type HostProjectOptions = {
|
|
61
|
+
/** The process working directory. */
|
|
62
|
+
cwd: string;
|
|
63
|
+
/** `LEGLAS_PROJECT_DIR`: an explicit answer that ends the search. */
|
|
64
|
+
override?: string | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* `LEGLAS_PLUGIN_ROOT`: where an Agent Plugins client installed us, which
|
|
67
|
+
* the plugin's own `mcp.json` passes as `${PLUGIN_ROOT}`. Its only use is
|
|
68
|
+
* recognising a working directory that means nothing.
|
|
69
|
+
*/
|
|
70
|
+
pluginRoot?: string | undefined;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Worded for every way the search comes up empty: a host with no roots, a host
|
|
74
|
+
* with an empty workspace, and a host that failed to answer. Naming only the
|
|
75
|
+
* first would describe the others wrongly.
|
|
76
|
+
*/
|
|
77
|
+
export declare const UNRESOLVED_PROJECT: string;
|
|
78
|
+
export declare function hostProject(host: RootsHost, options: HostProjectOptions): Project;
|
package/dist/project.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { isAbsolute, resolve, sep } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
/** A project fixed to one directory: what an embedder or a test already knows. */
|
|
5
|
+
export function fixedProject(directory) {
|
|
6
|
+
const located = { ok: true, directory };
|
|
7
|
+
return { locate: async () => located };
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Worded for every way the search comes up empty: a host with no roots, a host
|
|
11
|
+
* with an empty workspace, and a host that failed to answer. Naming only the
|
|
12
|
+
* first would describe the others wrongly.
|
|
13
|
+
*/
|
|
14
|
+
export const UNRESOLVED_PROJECT = "Leglas could not tell which project to work in. This agent host started the " +
|
|
15
|
+
"Leglas MCP server in the plugin's own directory and named no workspace to " +
|
|
16
|
+
"work in, so there is no project here to act on. Set LEGLAS_PROJECT_DIR to " +
|
|
17
|
+
"the project directory, or run the leglas CLI in the project instead.";
|
|
18
|
+
export function hostProject(host, options) {
|
|
19
|
+
let pending = null;
|
|
20
|
+
return { locate: () => (pending ??= discover(host, options)) };
|
|
21
|
+
}
|
|
22
|
+
async function discover(host, options) {
|
|
23
|
+
const override = options.override?.trim();
|
|
24
|
+
if (override !== undefined && override !== "") {
|
|
25
|
+
return { ok: true, directory: resolve(override) };
|
|
26
|
+
}
|
|
27
|
+
const cwd = canonical(options.cwd);
|
|
28
|
+
const roots = await declaredRoots(host);
|
|
29
|
+
if (roots.some((root) => contains(canonical(root), cwd))) {
|
|
30
|
+
return { ok: true, directory: options.cwd };
|
|
31
|
+
}
|
|
32
|
+
const first = roots[0];
|
|
33
|
+
if (first !== undefined)
|
|
34
|
+
return { ok: true, directory: first };
|
|
35
|
+
const pluginRoot = options.pluginRoot?.trim();
|
|
36
|
+
// A client that does not expand ${PLUGIN_ROOT} leaves the literal behind; it
|
|
37
|
+
// matches no real directory, so the check simply does not fire.
|
|
38
|
+
if (pluginRoot !== undefined && pluginRoot !== "" && contains(canonical(pluginRoot), cwd)) {
|
|
39
|
+
return { ok: false, reason: UNRESOLVED_PROJECT };
|
|
40
|
+
}
|
|
41
|
+
return { ok: true, directory: options.cwd };
|
|
42
|
+
}
|
|
43
|
+
async function declaredRoots(host) {
|
|
44
|
+
await initialized(host);
|
|
45
|
+
if (host.getClientCapabilities()?.roots === undefined)
|
|
46
|
+
return [];
|
|
47
|
+
try {
|
|
48
|
+
const { roots } = await host.listRoots();
|
|
49
|
+
return roots
|
|
50
|
+
.map((root) => toDirectory(root.uri))
|
|
51
|
+
.filter((directory) => directory !== null);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// A host that advertises roots and then refuses to list them has told us
|
|
55
|
+
// nothing, which leaves the working directory as good a guess as before.
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Roots can only be asked for once the client has initialized; asking earlier
|
|
61
|
+
* is a protocol error. A stdio server is connected well before a host gets
|
|
62
|
+
* around to initializing, and the channel starts polling immediately, so this
|
|
63
|
+
* wait is the ordinary path rather than an edge case.
|
|
64
|
+
*/
|
|
65
|
+
function initialized(host) {
|
|
66
|
+
if (host.getClientCapabilities() !== undefined)
|
|
67
|
+
return Promise.resolve();
|
|
68
|
+
return new Promise((ready) => {
|
|
69
|
+
const previous = host.oninitialized;
|
|
70
|
+
host.oninitialized = () => {
|
|
71
|
+
previous?.();
|
|
72
|
+
ready();
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** Roots are file URIs by the spec; a host that sends a bare path still reads. */
|
|
77
|
+
function toDirectory(uri) {
|
|
78
|
+
if (uri.startsWith("file:")) {
|
|
79
|
+
try {
|
|
80
|
+
return fileURLToPath(uri);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return isAbsolute(uri) ? uri : null;
|
|
87
|
+
}
|
|
88
|
+
function contains(parent, child) {
|
|
89
|
+
if (child === parent)
|
|
90
|
+
return true;
|
|
91
|
+
return child.startsWith(parent.endsWith(sep) ? parent : parent + sep);
|
|
92
|
+
}
|
|
93
|
+
/** Compared paths are real paths: /tmp is a symlink on macOS, among others. */
|
|
94
|
+
function canonical(directory) {
|
|
95
|
+
const absolute = resolve(directory);
|
|
96
|
+
try {
|
|
97
|
+
return realpathSync(absolute);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return absolute;
|
|
101
|
+
}
|
|
102
|
+
}
|
package/dist/tools.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { type Engagement } from "./engagement.js";
|
|
3
|
+
import type { Project } from "./project.js";
|
|
2
4
|
export type LeglasTools = {
|
|
3
5
|
/** Stop anything the tools started. Wired to the transport's close. */
|
|
4
6
|
shutdown(): Promise<void>;
|
|
5
7
|
};
|
|
6
8
|
export declare function registerLeglasTools(server: McpServer, options: {
|
|
7
|
-
|
|
9
|
+
project: Project;
|
|
10
|
+
engagement?: Engagement;
|
|
8
11
|
}): LeglasTools;
|
package/dist/tools.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { run, runAdd, runClassify, runExplore, runInit, runKeep, runList, runNew, runRequests, runShow, } from "leglas";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
import { createEngagement } from "./engagement.js";
|
|
3
4
|
async function capture(invoke) {
|
|
4
5
|
const lines = [];
|
|
5
6
|
const deps = {
|
|
@@ -23,11 +24,30 @@ async function capture(invoke) {
|
|
|
23
24
|
}
|
|
24
25
|
return { content: [{ type: "text", text: lines.join("\n") }], isError: exitCode !== 0 };
|
|
25
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Run a command in the project, or report that there is no project to run it
|
|
29
|
+
* in. Resolution is asked for on the first call and held after that, so the
|
|
30
|
+
* cost lands once and every tool acts on the same directory.
|
|
31
|
+
*/
|
|
32
|
+
async function inProject(project, invoke) {
|
|
33
|
+
const located = await project.locate();
|
|
34
|
+
if (!located.ok) {
|
|
35
|
+
// The CLI's shape for a failure, so a host parses this like any other.
|
|
36
|
+
return {
|
|
37
|
+
content: [{ type: "text", text: JSON.stringify({ ok: false, error: located.reason }) }],
|
|
38
|
+
isError: true,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return capture((deps) => invoke(located.directory, deps));
|
|
42
|
+
}
|
|
26
43
|
export function registerLeglasTools(server, options) {
|
|
27
|
-
const
|
|
44
|
+
const project = options.project;
|
|
28
45
|
// One viewer per MCP process. The handle is held so a host that dies or
|
|
29
46
|
// disconnects never leaves a dev server running on a port nobody remembers.
|
|
30
47
|
let viewer = null;
|
|
48
|
+
// Working the queue over MCP counts as attachment, exactly like watch: the
|
|
49
|
+
// embedded runner must not race a host's agent for the same tree.
|
|
50
|
+
const engagement = options.engagement ?? createEngagement();
|
|
31
51
|
server.registerTool("start", {
|
|
32
52
|
title: "Start the Leglas viewer",
|
|
33
53
|
description: "Boot the Leglas server for this project and return the interface URL. " +
|
|
@@ -46,7 +66,7 @@ export function registerLeglasTools(server, options) {
|
|
|
46
66
|
],
|
|
47
67
|
};
|
|
48
68
|
}
|
|
49
|
-
return
|
|
69
|
+
return inProject(project, async (cwd, deps) => {
|
|
50
70
|
const result = await run({ port, userPort: undefined, configPath: undefined, open: false, json: true, cwd }, { open: async () => { }, log: deps.log });
|
|
51
71
|
viewer = result;
|
|
52
72
|
return result;
|
|
@@ -71,12 +91,12 @@ export function registerLeglasTools(server, options) {
|
|
|
71
91
|
basedOn: z.string().optional()
|
|
72
92
|
.describe("Title of the direction this is a variant of; the rail groups the family."),
|
|
73
93
|
},
|
|
74
|
-
}, async ({ title, url, note, tags, branch, file, basedOn }) =>
|
|
94
|
+
}, async ({ title, url, note, tags, branch, file, basedOn }) => inProject(project, (cwd, deps) => runAdd({ preview: { title, url, note, tags, branch, file, basedOn }, json: true, cwd }, deps)));
|
|
75
95
|
server.registerTool("list", {
|
|
76
96
|
title: "List previews",
|
|
77
97
|
description: "Every preview, shared and local, with its URL and backing branch if any.",
|
|
78
98
|
inputSchema: {},
|
|
79
|
-
}, async () =>
|
|
99
|
+
}, async () => inProject(project, (cwd, deps) => runList({ json: true, cwd }, deps)));
|
|
80
100
|
server.registerTool("show", {
|
|
81
101
|
title: "Inspect one direction",
|
|
82
102
|
description: "Everything Leglas knows about one direction: its full entry, the source file behind " +
|
|
@@ -89,7 +109,7 @@ export function registerLeglasTools(server, options) {
|
|
|
89
109
|
.min(1)
|
|
90
110
|
.describe("The direction's title as the config spells it, not a renamed display name."),
|
|
91
111
|
},
|
|
92
|
-
}, async ({ title }) =>
|
|
112
|
+
}, async ({ title }) => inProject(project, (cwd, deps) => runShow({ title, json: true, cwd }, deps)));
|
|
93
113
|
server.registerTool("classify", {
|
|
94
114
|
title: "Decide where a direction should live",
|
|
95
115
|
description: "Before writing a direction, declare what it will touch and learn whether it can be " +
|
|
@@ -103,7 +123,7 @@ export function registerLeglasTools(server, options) {
|
|
|
103
123
|
}))
|
|
104
124
|
.min(1),
|
|
105
125
|
},
|
|
106
|
-
}, async ({ changes }) =>
|
|
126
|
+
}, async ({ changes }) => inProject(project, (cwd, deps) => runClassify({ changes, json: true, cwd }, deps)));
|
|
107
127
|
server.registerTool("explore", {
|
|
108
128
|
title: "Brief an exploration",
|
|
109
129
|
description: "What a set for a surface needs and how it registers here. Directions must genuinely " +
|
|
@@ -118,7 +138,9 @@ export function registerLeglasTools(server, options) {
|
|
|
118
138
|
.optional()
|
|
119
139
|
.describe("An existing direction's title: ask for variants of it instead of new directions."),
|
|
120
140
|
},
|
|
121
|
-
}, async ({ surface, count, basedOn }) => capture((deps) =>
|
|
141
|
+
}, async ({ surface, count, basedOn }) => capture((deps) =>
|
|
142
|
+
// The brief is the same wherever it is read from; it touches no project.
|
|
143
|
+
runExplore({ surface, count: count ?? 3, basedOn: basedOn ?? null, json: true }, deps)));
|
|
122
144
|
server.registerTool("scaffold", {
|
|
123
145
|
title: "Scaffold a branch point",
|
|
124
146
|
description: "Create a switcher and a first direction for a surface under .leglas/variants/. " +
|
|
@@ -128,7 +150,7 @@ export function registerLeglasTools(server, options) {
|
|
|
128
150
|
from: z.string().optional().describe("Path of the component rendering this surface today."),
|
|
129
151
|
print: z.boolean().optional().describe("Print the scaffold instead of writing it."),
|
|
130
152
|
},
|
|
131
|
-
}, async ({ surface, from, print }) =>
|
|
153
|
+
}, async ({ surface, from, print }) => inProject(project, (cwd, deps) => runNew({ surface, print: print ?? false, json: true, from, cwd }, deps)));
|
|
132
154
|
server.registerTool("keep", {
|
|
133
155
|
title: "Keep a winner",
|
|
134
156
|
description: "Move the winning direction into real source, delete the rest of the exploration, and " +
|
|
@@ -137,28 +159,37 @@ export function registerLeglasTools(server, options) {
|
|
|
137
159
|
title: z.string().min(1).describe("Title of the direction to keep."),
|
|
138
160
|
to: z.string().min(1).describe("Path in real source where the winner should live."),
|
|
139
161
|
},
|
|
140
|
-
}, async ({ title, to }) =>
|
|
162
|
+
}, async ({ title, to }) => inProject(project, (cwd, deps) => runKeep({ title, to, json: true, cwd }, deps)));
|
|
141
163
|
server.registerTool("requests", {
|
|
142
164
|
title: "Collect change requests",
|
|
143
165
|
description: "Pending change requests made from the interface, each naming the direction and the file " +
|
|
144
166
|
"behind it. Call this when starting work in a project that uses Leglas, and again before " +
|
|
145
167
|
"changing any direction: the user may have described the change from the interface while " +
|
|
146
|
-
"you worked, and collecting marks it picked up there. Pass clear once they are done
|
|
168
|
+
"you worked, and collecting marks it picked up there. Pass clear once they are done: it " +
|
|
169
|
+
"drops what you collected and reports anything that arrived since, which is yours to do next.",
|
|
147
170
|
inputSchema: {
|
|
148
171
|
clear: z.boolean().optional(),
|
|
149
172
|
},
|
|
150
|
-
}, async ({ clear }) =>
|
|
173
|
+
}, async ({ clear }) => {
|
|
174
|
+
// Touched on every call, empty queue included: an agent that just asked
|
|
175
|
+
// is an agent about to act, and the beat lapses on its own once the
|
|
176
|
+
// asking stops. Awaited before the queue is read so the runner has
|
|
177
|
+
// backed off by the time this session could collect anything.
|
|
178
|
+
await engagement.touch();
|
|
179
|
+
return inProject(project, (cwd, deps) => runRequests({ json: true, clear: clear ?? false, cwd }, deps));
|
|
180
|
+
});
|
|
151
181
|
server.registerTool("init", {
|
|
152
182
|
title: "Prepare a project",
|
|
153
183
|
description: "Write the AGENTS.md section, a starter config, and the gitignore entry into this project.",
|
|
154
184
|
inputSchema: {
|
|
155
185
|
force: z.boolean().optional().describe("Rewrite the AGENTS.md section if it exists."),
|
|
156
186
|
},
|
|
157
|
-
}, async ({ force }) =>
|
|
187
|
+
}, async ({ force }) => inProject(project, (cwd, deps) => runInit({ cwd, force: force ?? false, json: true }, deps)));
|
|
158
188
|
return {
|
|
159
189
|
shutdown: async () => {
|
|
160
190
|
const running = viewer;
|
|
161
191
|
viewer = null;
|
|
192
|
+
await engagement.stop().catch(() => { });
|
|
162
193
|
await running?.stop().catch(() => { });
|
|
163
194
|
},
|
|
164
195
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "leglas-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Stdio MCP server exposing Leglas to agent hosts that cannot run a shell.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
17
17
|
"zod": "^3.25.76",
|
|
18
|
-
"leglas": "^0.
|
|
18
|
+
"leglas": "^0.4.0"
|
|
19
19
|
},
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"repository": {
|