@storybook/addon-mcp 0.1.7 → 0.1.8

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.
Files changed (2) hide show
  1. package/dist/preset.js +61 -5
  2. package/package.json +2 -2
package/dist/preset.js CHANGED
@@ -11,7 +11,7 @@ import { buffer } from "node:stream/consumers";
11
11
 
12
12
  //#region package.json
13
13
  var name = "@storybook/addon-mcp";
14
- var version = "0.1.7";
14
+ var version = "0.1.8";
15
15
  var description = "Help agents automatically write and test stories for your UI components";
16
16
 
17
17
  //#endregion
@@ -219,6 +219,60 @@ const getManifestStatus = async (options) => {
219
219
  };
220
220
  };
221
221
 
222
+ //#endregion
223
+ //#region src/utils/estimate-tokens.ts
224
+ /**
225
+ * Checks if a character code is whitespace (space, tab, newline, carriage return)
226
+ *
227
+ * Checking char codes is slightly faster than using regex or string methods.
228
+ */
229
+ function isWhitespace(code) {
230
+ return code === 32 || code === 9 || code === 10 || code === 13;
231
+ }
232
+ /**
233
+ * Checks if a character code is alphanumeric or underscore
234
+ * 0-9 (48-57), A-Z (65-90), a-z (97-122), underscore (95)
235
+ *
236
+ * Checking char codes is slightly faster than using regex or string methods.
237
+ */
238
+ function isAlphanumeric(code) {
239
+ return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122 || code === 95;
240
+ }
241
+ /**
242
+ * Estimates token count from text using a fast approximation.
243
+ * Counts:
244
+ * - Continuous whitespace as a single token
245
+ * - Continuous alphanumeric sequences as single tokens
246
+ * - Each special character as an individual token
247
+ *
248
+ * This is a cheap approximation suitable for telemetry purposes.
249
+ *
250
+ * @param text - The text to estimate token count for
251
+ * @returns Estimated token count
252
+ */
253
+ function estimateTokens(text) {
254
+ if (!text) return 0;
255
+ let tokenCount = 0;
256
+ let i = 0;
257
+ const len = text.length;
258
+ while (i < len) {
259
+ const code = text.charCodeAt(i);
260
+ if (isWhitespace(code)) {
261
+ tokenCount++;
262
+ i++;
263
+ while (i < len && isWhitespace(text.charCodeAt(i))) i++;
264
+ } else if (isAlphanumeric(code)) {
265
+ tokenCount++;
266
+ i++;
267
+ while (i < len && isAlphanumeric(text.charCodeAt(i))) i++;
268
+ } else {
269
+ tokenCount++;
270
+ i++;
271
+ }
272
+ }
273
+ return tokenCount;
274
+ }
275
+
222
276
  //#endregion
223
277
  //#region src/mcp-handler.ts
224
278
  let transport;
@@ -266,22 +320,24 @@ const mcpServerHandler = async ({ req, res, options, addonOptions }) => {
266
320
  disableTelemetry,
267
321
  request: webRequest,
268
322
  ...!disableTelemetry && {
269
- onListAllDocumentation: async ({ manifests }) => {
323
+ onListAllDocumentation: async ({ manifests, resultText }) => {
270
324
  await collectTelemetry({
271
325
  event: "tool:listAllDocumentation",
272
326
  server,
273
327
  toolset: "docs",
274
328
  componentCount: Object.keys(manifests.componentManifest.components).length,
275
- docsCount: Object.keys(manifests.docsManifest?.docs || {}).length
329
+ docsCount: Object.keys(manifests.docsManifest?.docs || {}).length,
330
+ resultTokenCount: estimateTokens(resultText)
276
331
  });
277
332
  },
278
- onGetDocumentation: async ({ input, foundDocumentation }) => {
333
+ onGetDocumentation: async ({ input, foundDocumentation, resultText }) => {
279
334
  await collectTelemetry({
280
335
  event: "tool:getDocumentation",
281
336
  server,
282
337
  toolset: "docs",
283
338
  componentId: input.id,
284
- found: !!foundDocumentation
339
+ found: !!foundDocumentation,
340
+ resultTokenCount: estimateTokens(resultText ?? "")
285
341
  });
286
342
  }
287
343
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/addon-mcp",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Help agents automatically write and test stories for your UI components",
5
5
  "keywords": [
6
6
  "storybook-addon",
@@ -29,7 +29,7 @@
29
29
  "@tmcp/transport-http": "^0.8.0",
30
30
  "tmcp": "^1.16.0",
31
31
  "valibot": "1.2.0",
32
- "@storybook/mcp": "0.2.0"
32
+ "@storybook/mcp": "0.2.1"
33
33
  },
34
34
  "devDependencies": {
35
35
  "storybook": "10.2.0-alpha.14"