@finedesign/cli 0.0.0-stage → 0.1.0-rc.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,17 @@
1
+ export class ToolError extends Error {
2
+ constructor(code, message, exitCode, context = {}) {
3
+ super(message);
4
+ this.code = code;
5
+ this.exitCode = exitCode;
6
+ this.context = context;
7
+ }
8
+ toJSON() {
9
+ return { error: { code: this.code, message: this.message, ...this.context } };
10
+ }
11
+ }
12
+
13
+ export function errorResult(error) {
14
+ return error instanceof ToolError ? error.toJSON() : {
15
+ error: { code: 'INTERNAL_ERROR', message: error.message }
16
+ };
17
+ }
@@ -0,0 +1,23 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { ToolError } from './errors.mjs';
4
+
5
+ export function resolveVersion(explicit, project = process.cwd()) {
6
+ if (explicit !== undefined) return explicit;
7
+ let directory = path.resolve(project);
8
+ if (!fs.existsSync(directory) || !fs.statSync(directory).isDirectory()) {
9
+ throw new ToolError('INVALID_ARGUMENT', `Project directory does not exist: ${directory}`, 2);
10
+ }
11
+ while (true) {
12
+ const filename = path.join(directory, 'node_modules', '@fx-ui', 'fine-design', 'package.json');
13
+ if (fs.existsSync(filename)) {
14
+ const installed = JSON.parse(fs.readFileSync(filename, 'utf8'));
15
+ if (installed.name === '@fx-ui/fine-design' && typeof installed.version === 'string' && /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(installed.version)) return installed.version;
16
+ throw new ToolError('VERSION_NOT_DETECTED', 'Installed Fine Design package has no valid exact version.', 3, { suggestions: ['Specify --fd-version with an exact supported version.'] });
17
+ }
18
+ const parent = path.dirname(directory);
19
+ if (parent === directory) break;
20
+ directory = parent;
21
+ }
22
+ throw new ToolError('VERSION_NOT_DETECTED', 'Cannot determine the installed Fine Design version.', 3, { suggestions: ['Install @fx-ui/fine-design in the project or specify --fd-version.'] });
23
+ }
package/src/mcp.mjs ADDED
@@ -0,0 +1,33 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
3
+ import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
4
+ import fs from 'node:fs';
5
+ import { z } from 'zod';
6
+ import { queryCatalog } from './core/catalog.mjs';
7
+ import { loadCatalog } from './core/data.mjs';
8
+ import { schemas, validateInput } from './core/contracts.mjs';
9
+ import { errorResult } from './core/errors.mjs';
10
+
11
+ export async function startMcp() {
12
+ const packageInfo = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
13
+ const server = new Server({ name: 'fine-design', version: packageInfo.version }, { capabilities: { tools: {} } });
14
+ const descriptions = {
15
+ search_components: 'Search components; use nextOffset to browse subsequent pages.',
16
+ get_component_doc: 'Read component documentation and discover sections and example IDs.',
17
+ get_component_example: 'Read example source files, dependencies and binary asset metadata. Set includeAssets to include base64 asset content.'
18
+ };
19
+ server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: Object.entries(schemas).map(([name, schema]) => ({
20
+ name, description: descriptions[name], inputSchema: z.toJSONSchema(schema, { target: 'draft-07' }),
21
+ annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false }
22
+ })) }));
23
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
24
+ try {
25
+ const { name, arguments: input } = request.params;
26
+ const args = validateInput(name, input);
27
+ return { content: [{ type: 'text', text: JSON.stringify(queryCatalog(loadCatalog(args.version), name, args)) }] };
28
+ } catch (error) {
29
+ return { isError: true, content: [{ type: 'text', text: JSON.stringify(errorResult(error)) }] };
30
+ }
31
+ });
32
+ await server.connect(new StdioServerTransport());
33
+ }