@nera-static/mcp 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tools/cli.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import fs from 'node:fs/promises'
2
2
  import path from 'node:path'
3
- import { hasErrors, validateSite } from '@nera-static/validate'
3
+ import { hasErrors, validate } from '@nera-static/nera'
4
4
  import { z } from 'zod'
5
5
  import { PathError, resolveInRoot } from '../paths.js'
6
6
  import { ok, tool, ToolError } from '../results.js'
7
+ import { childEnv } from '../runner.js'
7
8
  import { relativeTo, resolveSite, siteFolders, siteInput } from './sites.js'
8
9
 
9
10
  const PLUGIN_SCOPE = '@nera-static/'
@@ -41,9 +42,9 @@ function countBySeverity(results) {
41
42
  }
42
43
 
43
44
  /**
44
- * The `--starter` value for `nera new`: a package name as given, or a local
45
- * starter folder checked against the root and passed relative to it (the
46
- * command runs in the root).
45
+ * The `starter` for `createSite`: a package name as given, or a local starter
46
+ * folder checked against the root and passed relative to it (`createSite`
47
+ * runs with the root as its `cwd`).
47
48
  */
48
49
  async function starterArg(ctx, starter) {
49
50
  if (PACKAGE_SPEC.test(starter) || GITHUB_STARTER.test(starter)) {
@@ -117,12 +118,12 @@ async function publishTemplateScript(dir, packageDir) {
117
118
  * Registers `create_site`, `build`, `validate` and `add_plugin`.
118
119
  *
119
120
  * @param {import('@modelcontextprotocol/sdk/server/mcp.js').McpServer} server
120
- * @param {{ root: string, runner: ReturnType<typeof import('../runner.js').createRunner> }} ctx
121
+ * @param {{ root: string, runner: ReturnType<typeof import('../runner.js').createRunner>, createSite: typeof import('@nera-static/nera').createSite }} ctx `create_site` installs through `createSite`, the other tools through `runner`
121
122
  */
122
123
  export function registerCliTools(server, ctx) {
123
124
  server.registerTool('create_site', {
124
125
  title: 'Create a Nera site',
125
- description: 'Creates a new Nera static site in a new folder of the root folder (`nera new`), installs its packages and returns the folder to pass as `site` to every other tool. Pick a starter from list_starters (`blank`, `blog`, `business`, `portfolio`) to begin with sample pages and a theme, or a theme from list_themes; with neither you get a minimal site. This can take a minute (npm install). Afterwards call get_site, then edit pages with write_page.',
126
+ description: 'Creates a new Nera static site in a new folder of the root folder (like `nera new`), installs its packages and returns the folder to pass as `site` to every other tool. Pick a starter from list_starters (`blank`, `blog`, `business`, `portfolio`) to begin with sample pages and a theme, or a theme from list_themes; with neither you get a minimal site. This can take a minute (npm install). A starter\'s suggested first steps come back as `firstSteps`. Afterwards call get_site, then edit pages with write_page.',
126
127
  inputSchema: {
127
128
  name: z.string().describe('The new folder, created directly in the root: letters, digits, `.`, `-`, `_` (e.g. `my-bakery`). `.` creates the site in the root itself, which must then be empty.'),
128
129
  starter: z.string().optional().describe('A starter kit from list_starters, e.g. `blog` or `@nera-static/starter-business`. Leave out for a minimal site.'),
@@ -143,20 +144,36 @@ export function registerCliTools(server, ctx) {
143
144
  throw new ToolError(`Invalid theme "${theme}". Use a theme name from list_themes (e.g. \`blog\`) or a package name (\`@nera-static/theme-blog\`).`)
144
145
  }
145
146
 
146
- const args = ['new', inRoot ? '.' : name, '--yes']
147
- if (starter !== undefined) args.push(`--starter=${await starterArg(ctx, starter)}`)
148
- if (theme !== undefined) args.push(`--theme=${theme}`)
149
- if (title !== undefined) args.push(`--name=${title}`)
150
- if (lang !== undefined) args.push(`--lang=${lang}`)
147
+ const spec = starter === undefined ? undefined : await starterArg(ctx, starter)
151
148
 
152
- const run = await ctx.runner('nera', args, { cwd: ctx.root, signal })
153
- if (run.code !== 0 || !await exists(path.join(target, 'package.json'))) {
154
- throw new ToolError(`Creating the site failed: ${failureText(run)}`)
155
- }
149
+ // Every progress line is collected for the result and npm install's
150
+ // output goes to stderr (createSite's default): stdout belongs to the
151
+ // protocol. `env` is a child's (the shim first under the bundled
152
+ // runtime) and reaches createSite's own children; the call's signal
153
+ // stops them.
156
154
  const site = relativeTo(ctx.root, target)
155
+ const lines = []
156
+ let created
157
+ try {
158
+ created = await ctx.createSite(inRoot ? '.' : name, {
159
+ cwd: ctx.root, starter: spec, theme, name: title, lang,
160
+ install: true, signal, env: childEnv(), log: (line) => lines.push(line),
161
+ })
162
+ } catch (error) {
163
+ // A failed install keeps the written site (npm install can be
164
+ // rerun there); after anything else, an abort included,
165
+ // createSite has removed a new folder again.
166
+ if (!signal.aborted && typeof error.code === 'number' && await exists(path.join(target, 'package.json'))) {
167
+ throw new ToolError(`The files of the site "${site}" are written, but installing its packages failed: ${error.message}\nnpm's own output is in the server's log. Fix what npm reports (often the network connection), then run \`npm install\` in that folder, or call create_site again with another name.`)
168
+ }
169
+ throw new ToolError(`Creating the site failed: ${error.message}`)
170
+ }
171
+ const firstSteps = created.firstSteps ?? null
157
172
  return ok(
158
- { site, log: tail(run.stdout) },
159
- `Created the Nera site "${site}". Call get_site with site "${site}" to see its pages, then edit them with write_page.`,
173
+ { site, log: tail(lines.join('\n')), firstSteps },
174
+ firstSteps
175
+ ? `Created the Nera site "${site}". Its starter suggests first steps (in firstSteps); follow them, starting with get_site with site "${site}".`
176
+ : `Created the Nera site "${site}". Call get_site with site "${site}" to see its pages, then edit them with write_page.`,
160
177
  )
161
178
  }))
162
179
 
@@ -195,7 +212,7 @@ export function registerCliTools(server, ctx) {
195
212
  annotations: { readOnlyHint: true },
196
213
  }, tool(async ({ site }) => {
197
214
  const resolved = await resolveSite(ctx, site)
198
- const results = validateSite({ cwd: resolved.dir })
215
+ const results = validate({ cwd: resolved.dir })
199
216
  const { errors, warnings } = countBySeverity(results)
200
217
  const text = results.length
201
218
  ? `"${resolved.site}" has ${errors} error(s) and ${warnings} warning(s); each message says how to fix it.`