@storybook/mcp 0.0.2 → 0.0.3
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/dist/index.d.ts +19 -11
- package/dist/index.js +36 -35
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,27 +1,35 @@
|
|
|
1
|
+
import { McpServer } from "tmcp";
|
|
1
2
|
import "valibot";
|
|
2
3
|
|
|
3
4
|
//#region src/types.d.ts
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Custom context passed to MCP server and tools.
|
|
7
|
-
* Contains the source URL for
|
|
8
|
+
* Contains the source URL for getting component manifests.
|
|
8
9
|
*/
|
|
9
10
|
interface StorybookContext extends Record<string, unknown> {
|
|
10
11
|
/**
|
|
11
|
-
* The URL of the remote manifest to
|
|
12
|
+
* The URL of the remote manifest to get component data from.
|
|
12
13
|
*/
|
|
13
14
|
source?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Optional function to provide custom manifest retrieval logic.
|
|
17
|
+
* If provided, this function will be called instead of using fetch.
|
|
18
|
+
* The function receives the source URL and should return the manifest as a string.
|
|
19
|
+
*/
|
|
20
|
+
manifestProvider?: (source: string) => Promise<string>;
|
|
14
21
|
}
|
|
15
22
|
//#endregion
|
|
23
|
+
//#region src/tools/list-all-components.d.ts
|
|
24
|
+
declare const LIST_TOOL_NAME = "list-all-components";
|
|
25
|
+
declare function addListAllComponentsTool(server: McpServer<any, StorybookContext>): Promise<void>;
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/tools/get-component-documentation.d.ts
|
|
28
|
+
declare const GET_TOOL_NAME = "get-component-documentation";
|
|
29
|
+
declare function addGetComponentDocumentationTool(server: McpServer<any, StorybookContext>): Promise<void>;
|
|
30
|
+
//#endregion
|
|
16
31
|
//#region src/index.d.ts
|
|
17
32
|
type Handler = (req: Request, context?: StorybookContext) => Promise<Response>;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* The default source URL for fetching component manifests.
|
|
21
|
-
* Can be overridden per-request via context parameter.
|
|
22
|
-
*/
|
|
23
|
-
source?: string;
|
|
24
|
-
};
|
|
25
|
-
declare const createStorybookMcpHandler: (options?: StorybookMcpHandlerOptions) => Promise<Handler>;
|
|
33
|
+
declare const createStorybookMcpHandler: (options?: StorybookContext) => Promise<Handler>;
|
|
26
34
|
//#endregion
|
|
27
|
-
export { createStorybookMcpHandler };
|
|
35
|
+
export { GET_TOOL_NAME, LIST_TOOL_NAME, type StorybookContext, addGetComponentDocumentationTool, addListAllComponentsTool, createStorybookMcpHandler };
|
package/dist/index.js
CHANGED
|
@@ -5,25 +5,17 @@ import * as v from "valibot";
|
|
|
5
5
|
|
|
6
6
|
//#region package.json
|
|
7
7
|
var name = "@storybook/mcp";
|
|
8
|
-
var version = "0.0.
|
|
8
|
+
var version = "0.0.3";
|
|
9
9
|
var description = "MCP server that serves knowledge about your components based on your Storybook stories and documentation";
|
|
10
10
|
|
|
11
11
|
//#endregion
|
|
12
12
|
//#region src/types.ts
|
|
13
|
-
const JSDocTag = v.
|
|
14
|
-
key: v.string(),
|
|
15
|
-
value: v.union([
|
|
16
|
-
v.string(),
|
|
17
|
-
v.number(),
|
|
18
|
-
v.boolean(),
|
|
19
|
-
v.null()
|
|
20
|
-
])
|
|
21
|
-
});
|
|
13
|
+
const JSDocTag = v.record(v.string(), v.array(v.string()));
|
|
22
14
|
const BaseManifest = v.object({
|
|
23
15
|
name: v.string(),
|
|
24
16
|
description: v.exactOptional(v.string()),
|
|
25
17
|
import: v.exactOptional(v.string()),
|
|
26
|
-
jsDocTags: v.exactOptional(
|
|
18
|
+
jsDocTags: v.exactOptional(JSDocTag)
|
|
27
19
|
});
|
|
28
20
|
const Example = v.object({
|
|
29
21
|
...BaseManifest.entries,
|
|
@@ -42,16 +34,16 @@ const ComponentManifestMap = v.object({
|
|
|
42
34
|
});
|
|
43
35
|
|
|
44
36
|
//#endregion
|
|
45
|
-
//#region src/utils/
|
|
37
|
+
//#region src/utils/get-manifest.ts
|
|
46
38
|
/**
|
|
47
|
-
* Error thrown when
|
|
39
|
+
* Error thrown when getting or parsing a manifest fails
|
|
48
40
|
*/
|
|
49
|
-
var
|
|
41
|
+
var ManifestGetError = class extends Error {
|
|
50
42
|
url;
|
|
51
43
|
cause;
|
|
52
44
|
constructor(message, url, cause) {
|
|
53
45
|
super(message);
|
|
54
|
-
this.name = "
|
|
46
|
+
this.name = "ManifestGetError";
|
|
55
47
|
this.url = url ?? "No source URL provided";
|
|
56
48
|
this.cause = cause;
|
|
57
49
|
}
|
|
@@ -63,8 +55,8 @@ var ManifestFetchError = class extends Error {
|
|
|
63
55
|
* @returns A tool result with error content and isError flag
|
|
64
56
|
*/
|
|
65
57
|
const errorToMCPContent = (error) => {
|
|
66
|
-
let fullMessage = `${error instanceof
|
|
67
|
-
if (error instanceof
|
|
58
|
+
let fullMessage = `${error instanceof ManifestGetError ? "Error getting manifest" : "Unexpected error"}: ${error instanceof Error ? error.message : String(error)}`;
|
|
59
|
+
if (error instanceof ManifestGetError && error.cause) {
|
|
68
60
|
const causeMessage = error.cause instanceof Error ? error.cause.message : String(error.cause);
|
|
69
61
|
fullMessage += `\nCaused by: ${causeMessage}`;
|
|
70
62
|
}
|
|
@@ -77,28 +69,33 @@ const errorToMCPContent = (error) => {
|
|
|
77
69
|
};
|
|
78
70
|
};
|
|
79
71
|
/**
|
|
80
|
-
*
|
|
72
|
+
* Gets a component manifest from a remote URL or using a custom provider
|
|
81
73
|
*
|
|
82
|
-
* @param url - The URL to
|
|
74
|
+
* @param url - The URL to get the manifest from
|
|
75
|
+
* @param manifestProvider - Optional custom function to get the manifest
|
|
83
76
|
* @returns A promise that resolves to the parsed ComponentManifestMap
|
|
84
|
-
* @throws {
|
|
77
|
+
* @throws {ManifestGetError} If getting the manifest fails or the response is invalid
|
|
85
78
|
*/
|
|
86
|
-
async function
|
|
79
|
+
async function getManifest(url, manifestProvider) {
|
|
87
80
|
try {
|
|
88
|
-
if (!url) throw new
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
if (
|
|
93
|
-
const data = await response.json();
|
|
94
|
-
const manifest = v.parse(ComponentManifestMap, data);
|
|
95
|
-
if (Object.keys(manifest.components).length === 0) throw new ManifestFetchError(`No components found in the manifest`, url);
|
|
81
|
+
if (!url) throw new ManifestGetError("The source URL is required, but was not part of the request context nor was a default source for the server set");
|
|
82
|
+
const manifestString = await (manifestProvider ?? defaultFetchManifestProvider)(url);
|
|
83
|
+
const manifestData = JSON.parse(manifestString);
|
|
84
|
+
const manifest = v.parse(ComponentManifestMap, manifestData);
|
|
85
|
+
if (Object.keys(manifest.components).length === 0) throw new ManifestGetError(`No components found in the manifest`, url);
|
|
96
86
|
return manifest;
|
|
97
87
|
} catch (error) {
|
|
98
|
-
if (error instanceof
|
|
99
|
-
throw new
|
|
88
|
+
if (error instanceof ManifestGetError) throw error;
|
|
89
|
+
throw new ManifestGetError(`Failed to get manifest: ${error instanceof Error ? error.message : String(error)}`, url, error instanceof Error ? error : void 0);
|
|
100
90
|
}
|
|
101
91
|
}
|
|
92
|
+
async function defaultFetchManifestProvider(source) {
|
|
93
|
+
const response = await fetch(source);
|
|
94
|
+
if (!response.ok) throw new ManifestGetError(`Failed to fetch manifest: ${response.status} ${response.statusText}`, source);
|
|
95
|
+
const contentType = response.headers.get("content-type");
|
|
96
|
+
if (!contentType?.includes("application/json")) throw new ManifestGetError(`Invalid content type: expected application/json, got ${contentType}`, source);
|
|
97
|
+
return response.text();
|
|
98
|
+
}
|
|
102
99
|
|
|
103
100
|
//#endregion
|
|
104
101
|
//#region src/utils/dedent.ts
|
|
@@ -186,7 +183,7 @@ async function addListAllComponentsTool(server) {
|
|
|
186
183
|
try {
|
|
187
184
|
return { content: [{
|
|
188
185
|
type: "text",
|
|
189
|
-
text: formatComponentManifestMapToList(await
|
|
186
|
+
text: formatComponentManifestMapToList(await getManifest(server.ctx.custom?.source, server.ctx.custom?.manifestProvider))
|
|
190
187
|
}] };
|
|
191
188
|
} catch (error) {
|
|
192
189
|
return errorToMCPContent(error);
|
|
@@ -206,7 +203,7 @@ async function addGetComponentDocumentationTool(server) {
|
|
|
206
203
|
schema: GetComponentDocumentationInput
|
|
207
204
|
}, async (input) => {
|
|
208
205
|
try {
|
|
209
|
-
const manifest = await
|
|
206
|
+
const manifest = await getManifest(server.ctx.custom?.source, server.ctx.custom?.manifestProvider);
|
|
210
207
|
const content = [];
|
|
211
208
|
const notFoundIds = [];
|
|
212
209
|
for (const componentId of input.componentIds) {
|
|
@@ -256,9 +253,13 @@ const createStorybookMcpHandler = async (options = {}) => {
|
|
|
256
253
|
const transport = new HttpTransport(server, { path: null });
|
|
257
254
|
return (async (req, context) => {
|
|
258
255
|
const source = context?.source ?? options.source;
|
|
259
|
-
|
|
256
|
+
const manifestProvider = context?.manifestProvider ?? options.manifestProvider;
|
|
257
|
+
return await transport.respond(req, {
|
|
258
|
+
source,
|
|
259
|
+
manifestProvider
|
|
260
|
+
});
|
|
260
261
|
});
|
|
261
262
|
};
|
|
262
263
|
|
|
263
264
|
//#endregion
|
|
264
|
-
export { createStorybookMcpHandler };
|
|
265
|
+
export { GET_TOOL_NAME, LIST_TOOL_NAME, addGetComponentDocumentationTool, addListAllComponentsTool, createStorybookMcpHandler };
|