Skip to content

Commit 6441224

Browse files
committed
fix(mcp): inline JSON data via static imports for self-contained bundle
Switch tool handlers from readFileSync to static JSON imports so tsup inlines the data into dist/index.js. Also read version from package.json to prevent drift.
1 parent 38c74f7 commit 6441224

4 files changed

Lines changed: 10 additions & 40 deletions

File tree

packages/mcp/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { z } from 'zod';
55
import { listComponents, getComponentProps, getComponentExample } from './tools/components.js';
66
import { getDesignTokens } from './tools/tokens.js';
77
import { listIcons, getIcon } from './tools/icons.js';
8+
import pkg from '../package.json';
89

910
const server = new McpServer({
1011
name: '@tiny-design/mcp',
11-
version: '1.6.0',
12+
version: pkg.version,
1213
});
1314

1415
// --- Component tools ---

packages/mcp/src/tools/components.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
import { readFileSync } from 'fs';
2-
import { resolve, dirname } from 'path';
3-
import { fileURLToPath } from 'url';
41
import type { ComponentData } from '../types.js';
2+
import componentsData from '../data/components.json';
53

6-
const __filename = fileURLToPath(import.meta.url);
7-
const __dirname = dirname(__filename);
8-
9-
function loadComponents(): ComponentData[] {
10-
const dataPath = resolve(__dirname, '../data/components.json');
11-
const raw = readFileSync(dataPath, 'utf-8');
12-
return JSON.parse(raw) as ComponentData[];
13-
}
4+
const components = componentsData as ComponentData[];
145

156
export function listComponents(category?: string) {
16-
const components = loadComponents();
177
const filtered = category
188
? components.filter((c) => c.category === category)
199
: components;
@@ -26,7 +16,6 @@ export function listComponents(category?: string) {
2616
}
2717

2818
export function getComponentProps(name: string) {
29-
const components = loadComponents();
3019
const component = components.find(
3120
(c) => c.name.toLowerCase() === name.toLowerCase()
3221
);
@@ -41,7 +30,6 @@ export function getComponentProps(name: string) {
4130
}
4231

4332
export function getComponentExample(name: string, demo?: string) {
44-
const components = loadComponents();
4533
const component = components.find(
4634
(c) => c.name.toLowerCase() === name.toLowerCase()
4735
);

packages/mcp/src/tools/icons.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import * as fs from 'node:fs';
2-
import * as path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
4-
import type { IconData } from '../types';
1+
import type { IconData } from '../types.js';
2+
import iconsData from '../data/icons.json';
53

6-
const __filename = fileURLToPath(import.meta.url);
7-
const __dirname = path.dirname(__filename);
8-
const DATA_PATH = path.resolve(__dirname, '../data/icons.json');
9-
10-
function loadIcons(): IconData {
11-
return JSON.parse(fs.readFileSync(DATA_PATH, 'utf-8'));
12-
}
4+
const data = iconsData as IconData;
135

146
export function listIcons(search?: string): string[] {
15-
const data = loadIcons();
167
if (search) {
178
const term = search.toLowerCase();
189
return data.icons.filter((name) => name.toLowerCase().includes(term));
@@ -21,7 +12,6 @@ export function listIcons(search?: string): string[] {
2112
}
2213

2314
export function getIcon(name: string) {
24-
const data = loadIcons();
2515
const icon = data.icons.find(
2616
(i) => i.toLowerCase() === name.toLowerCase()
2717
);

packages/mcp/src/tools/tokens.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import * as fs from 'node:fs';
2-
import * as path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
4-
import type { TokenData } from '../types';
1+
import type { TokenData } from '../types.js';
2+
import tokensData from '../data/tokens.json';
53

6-
const __filename = fileURLToPath(import.meta.url);
7-
const __dirname = path.dirname(__filename);
8-
const DATA_PATH = path.resolve(__dirname, '../data/tokens.json');
9-
10-
function loadTokens(): TokenData {
11-
return JSON.parse(fs.readFileSync(DATA_PATH, 'utf-8'));
12-
}
4+
const tokens = tokensData as TokenData;
135

146
export function getDesignTokens(category?: string): TokenData {
15-
const tokens = loadTokens();
167
if (category) {
178
if (!(category in tokens)) return {} as TokenData;
189
return { [category]: tokens[category] } as TokenData;

0 commit comments

Comments
 (0)