@storybook/addon-mcp 0.6.0 → 10.6.0-alpha.4
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/CHANGELOG.md +423 -0
- package/README.md +20 -0
- package/dist/preset.js +3194 -1273
- package/dist/preview.js +25 -44
- package/dist/tools/preview-stories/preview-stories-app-script.js +176 -0
- package/package.json +33 -31
- package/LICENSE +0 -21
- package/dist/preview-stories-app-script.js +0 -142
package/dist/preview.js
CHANGED
|
@@ -1,46 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const MCP_APP_SIZE_CHANGED_EVENT = "storybook-mcp:size-changed";
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var MCP_APP_PARAM = "mcp-app", MCP_APP_SIZE_CHANGED_EVENT = "storybook-mcp:size-changed";
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
height
|
|
28
|
-
}, "*");
|
|
29
|
-
}
|
|
30
|
-
function debouncedSendSize() {
|
|
31
|
-
if (debounceTimer) clearTimeout(debounceTimer);
|
|
32
|
-
debounceTimer = setTimeout(sendSizeToParent, DEBOUNCE_MS);
|
|
33
|
-
}
|
|
34
|
-
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", sendSizeToParent);
|
|
35
|
-
else sendSizeToParent();
|
|
36
|
-
window.addEventListener("load", sendSizeToParent);
|
|
37
|
-
new ResizeObserver(debouncedSendSize).observe(document.body);
|
|
38
|
-
new MutationObserver(debouncedSendSize).observe(document.body, {
|
|
39
|
-
childList: true,
|
|
40
|
-
subtree: true,
|
|
41
|
-
attributes: true
|
|
42
|
-
});
|
|
4
|
+
// src/preview.ts
|
|
5
|
+
var isMcpApp = new URLSearchParams(window.location.search).has(MCP_APP_PARAM);
|
|
6
|
+
if (isMcpApp) {
|
|
7
|
+
let sendSizeToParent2 = function() {
|
|
8
|
+
let height = document.body.scrollHeight;
|
|
9
|
+
Math.abs(height - lastSentHeight) <= 2 || (lastSentHeight = height, window.parent.postMessage(
|
|
10
|
+
{
|
|
11
|
+
type: MCP_APP_SIZE_CHANGED_EVENT,
|
|
12
|
+
height
|
|
13
|
+
},
|
|
14
|
+
"*"
|
|
15
|
+
));
|
|
16
|
+
}, debouncedSendSize2 = function() {
|
|
17
|
+
debounceTimer && clearTimeout(debounceTimer), debounceTimer = setTimeout(sendSizeToParent2, DEBOUNCE_MS);
|
|
18
|
+
};
|
|
19
|
+
sendSizeToParent = sendSizeToParent2, debouncedSendSize = debouncedSendSize2;
|
|
20
|
+
let SIZE_CHANGE_THRESHOLD = 2, debounceTimer = null, lastSentHeight = 0, DEBOUNCE_MS = 100;
|
|
21
|
+
document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", sendSizeToParent2) : sendSizeToParent2(), window.addEventListener("load", sendSizeToParent2), new ResizeObserver(debouncedSendSize2).observe(document.body), new MutationObserver(debouncedSendSize2).observe(document.body, {
|
|
22
|
+
childList: !0,
|
|
23
|
+
subtree: !0,
|
|
24
|
+
attributes: !0
|
|
25
|
+
});
|
|
43
26
|
}
|
|
44
|
-
|
|
45
|
-
//#endregion
|
|
46
|
-
export { };
|
|
27
|
+
var sendSizeToParent, debouncedSendSize;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var MCP_APP_PARAM = "mcp-app", MCP_APP_SIZE_CHANGED_EVENT = "storybook-mcp:size-changed";
|
|
3
|
+
|
|
4
|
+
// package.json
|
|
5
|
+
var package_default = {
|
|
6
|
+
name: "@storybook/addon-mcp",
|
|
7
|
+
version: "10.6.0-alpha.4",
|
|
8
|
+
description: "Help agents automatically write and test stories for your UI components",
|
|
9
|
+
keywords: [
|
|
10
|
+
"ai",
|
|
11
|
+
"mcp",
|
|
12
|
+
"storybook-addon"
|
|
13
|
+
],
|
|
14
|
+
homepage: "https://github.com/storybookjs/storybook/tree/next/code/addons/mcp",
|
|
15
|
+
bugs: {
|
|
16
|
+
url: "https://github.com/storybookjs/storybook/issues"
|
|
17
|
+
},
|
|
18
|
+
repository: {
|
|
19
|
+
type: "git",
|
|
20
|
+
url: "https://github.com/storybookjs/storybook.git",
|
|
21
|
+
directory: "code/addons/mcp"
|
|
22
|
+
},
|
|
23
|
+
funding: {
|
|
24
|
+
type: "opencollective",
|
|
25
|
+
url: "https://opencollective.com/storybook"
|
|
26
|
+
},
|
|
27
|
+
license: "MIT",
|
|
28
|
+
author: "Storybook Core Team",
|
|
29
|
+
type: "module",
|
|
30
|
+
exports: {
|
|
31
|
+
"./internal/preview-stories-app-script": "./dist/tools/preview-stories/preview-stories-app-script.js",
|
|
32
|
+
"./package.json": "./package.json",
|
|
33
|
+
"./preset": "./dist/preset.js"
|
|
34
|
+
},
|
|
35
|
+
files: [
|
|
36
|
+
"dist/**/*",
|
|
37
|
+
"README.md",
|
|
38
|
+
"*.js",
|
|
39
|
+
"*.d.ts",
|
|
40
|
+
"!src/**/*"
|
|
41
|
+
],
|
|
42
|
+
dependencies: {
|
|
43
|
+
"@storybook/mcp": "workspace:*",
|
|
44
|
+
"@tmcp/adapter-valibot": "^0.1.5",
|
|
45
|
+
"@tmcp/transport-http": "^0.8.5",
|
|
46
|
+
picoquery: "^2.5.0",
|
|
47
|
+
tmcp: "^1.19.4",
|
|
48
|
+
valibot: "^1.4.0"
|
|
49
|
+
},
|
|
50
|
+
devDependencies: {
|
|
51
|
+
"@storybook/addon-a11y": "workspace:*",
|
|
52
|
+
"@storybook/addon-vitest": "workspace:*",
|
|
53
|
+
storybook: "workspace:*",
|
|
54
|
+
typescript: "^6.0.3"
|
|
55
|
+
},
|
|
56
|
+
peerDependencies: {
|
|
57
|
+
"@storybook/addon-vitest": "workspace:^",
|
|
58
|
+
storybook: "workspace:^"
|
|
59
|
+
},
|
|
60
|
+
peerDependenciesMeta: {
|
|
61
|
+
"@storybook/addon-vitest": {
|
|
62
|
+
optional: !0
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
publishConfig: {
|
|
66
|
+
access: "public"
|
|
67
|
+
},
|
|
68
|
+
mcpName: "io.github.storybookjs/addon-mcp",
|
|
69
|
+
storybook: {
|
|
70
|
+
displayName: "Addon MCP",
|
|
71
|
+
supportedFrameworks: [
|
|
72
|
+
"react",
|
|
73
|
+
"vue3",
|
|
74
|
+
"angular",
|
|
75
|
+
"web-components",
|
|
76
|
+
"html",
|
|
77
|
+
"svelte",
|
|
78
|
+
"preact",
|
|
79
|
+
"react-native"
|
|
80
|
+
],
|
|
81
|
+
icon: "https://user-images.githubusercontent.com/321738/63501763-88dbf600-c4cc-11e9-96cd-94adadc2fd72.png"
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// src/tools/preview-stories/preview-stories-app-script.ts
|
|
86
|
+
var LATEST_PROTOCOL_VERSION = "2025-11-21", METHODS = {
|
|
87
|
+
// Lifecycle
|
|
88
|
+
INITIALIZE: "ui/initialize",
|
|
89
|
+
INITIALIZED: "ui/notifications/initialized",
|
|
90
|
+
// Tool data (Host -> Guest)
|
|
91
|
+
TOOL_INPUT: "ui/notifications/tool-input",
|
|
92
|
+
TOOL_INPUT_PARTIAL: "ui/notifications/tool-input-partial",
|
|
93
|
+
TOOL_RESULT: "ui/notifications/tool-result",
|
|
94
|
+
TOOL_CANCELLED: "ui/notifications/tool-cancelled",
|
|
95
|
+
// Context & UI
|
|
96
|
+
HOST_CONTEXT_CHANGED: "ui/notifications/host-context-changed",
|
|
97
|
+
SIZE_CHANGED: "ui/notifications/size-changed",
|
|
98
|
+
RESOURCE_TEARDOWN: "ui/resource-teardown",
|
|
99
|
+
// Standard MCP methods
|
|
100
|
+
TOOLS_CALL: "tools/call",
|
|
101
|
+
NOTIFICATIONS_MESSAGE: "notifications/message",
|
|
102
|
+
OPEN_LINK: "ui/open-link",
|
|
103
|
+
MESSAGE: "ui/message"
|
|
104
|
+
}, nextId = 1;
|
|
105
|
+
function sendHostRequest(method, params) {
|
|
106
|
+
let id = nextId++, { promise, resolve, reject } = Promise.withResolvers();
|
|
107
|
+
return window.parent.postMessage({ jsonrpc: "2.0", id, method, params }, "*"), window.addEventListener("message", function listener(event) {
|
|
108
|
+
event.data?.id === id && (window.removeEventListener("message", listener), event.data?.result ? resolve(event.data.result) : event.data?.error && reject(new Error(String(event.data.error))));
|
|
109
|
+
}), promise;
|
|
110
|
+
}
|
|
111
|
+
function sendHostNotification(method, params) {
|
|
112
|
+
window.parent.postMessage({ jsonrpc: "2.0", method, params }, "*");
|
|
113
|
+
}
|
|
114
|
+
function onHostNotification(method, handler) {
|
|
115
|
+
window.addEventListener("message", function(event) {
|
|
116
|
+
event.data?.method === method && handler(event.data.params);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
var initializeResult = await sendHostRequest(METHODS.INITIALIZE, {
|
|
120
|
+
appInfo: {
|
|
121
|
+
name: "storybook-story-preview",
|
|
122
|
+
version: package_default.version
|
|
123
|
+
},
|
|
124
|
+
appCapabilities: {},
|
|
125
|
+
protocolVersion: LATEST_PROTOCOL_VERSION
|
|
126
|
+
});
|
|
127
|
+
applyHostStyles(initializeResult?.hostContext);
|
|
128
|
+
onHostNotification(METHODS.TOOL_RESULT, loadStoryIframes);
|
|
129
|
+
onHostNotification(METHODS.HOST_CONTEXT_CHANGED, applyHostStyles);
|
|
130
|
+
sendHostNotification(METHODS.INITIALIZED, {});
|
|
131
|
+
window.addEventListener("message", function(event) {
|
|
132
|
+
if (event.data?.type !== MCP_APP_SIZE_CHANGED_EVENT)
|
|
133
|
+
return;
|
|
134
|
+
let iframes = document.querySelectorAll(".story-iframe"), hasResizedIframes = !1;
|
|
135
|
+
for (let iframe of iframes)
|
|
136
|
+
if (iframe.contentWindow === event.source) {
|
|
137
|
+
iframe.style.height = (event.data.height ?? 0) + "px", hasResizedIframes = !0;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
hasResizedIframes && resizeApp();
|
|
141
|
+
});
|
|
142
|
+
function applyHostStyles(hostContext) {
|
|
143
|
+
if (hostContext?.theme && document.documentElement.setAttribute("data-theme", hostContext.theme), !!hostContext?.styles?.variables) {
|
|
144
|
+
for (let [key, value] of Object.entries(hostContext.styles.variables))
|
|
145
|
+
value && document.documentElement.style.setProperty(key, value);
|
|
146
|
+
resizeApp();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function resizeApp() {
|
|
150
|
+
console.log("Resizing app to fit content", {
|
|
151
|
+
width: document.body.scrollWidth,
|
|
152
|
+
height: document.body.scrollHeight
|
|
153
|
+
}), sendHostNotification(METHODS.SIZE_CHANGED, {
|
|
154
|
+
width: document.body.scrollWidth,
|
|
155
|
+
height: document.body.scrollHeight
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function loadStoryIframes(params) {
|
|
159
|
+
let stories = params.structuredContent?.stories;
|
|
160
|
+
if (!stories || stories.length === 0) {
|
|
161
|
+
console.warn("No preview URLs found in tool result.");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
let template = document.getElementById("preview-template");
|
|
165
|
+
for (let storyResult of stories) {
|
|
166
|
+
if ("error" in storyResult) {
|
|
167
|
+
console.warn("Skipping story with error:", storyResult.error);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
let clone = template.content.cloneNode(!0), article = clone.querySelector("article"), heading = clone.querySelector("h1"), iframe = clone.querySelector("iframe");
|
|
171
|
+
heading.textContent = `${storyResult.title} - ${storyResult.name}`, iframe.style.width = "100%", iframe.style.height = "0";
|
|
172
|
+
let iframeSrc = storyResult.previewUrl.replace("/?path=/story/", "/iframe.html?id="), url = new URL(iframeSrc);
|
|
173
|
+
url.searchParams.set(MCP_APP_PARAM, "true"), iframe.src = url.toString(), document.body.appendChild(article);
|
|
174
|
+
}
|
|
175
|
+
resizeApp();
|
|
176
|
+
}
|
package/package.json
CHANGED
|
@@ -1,56 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storybook/addon-mcp",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.6.0-alpha.4",
|
|
4
4
|
"description": "Help agents automatically write and test stories for your UI components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
7
7
|
"mcp",
|
|
8
8
|
"storybook-addon"
|
|
9
9
|
],
|
|
10
|
-
"
|
|
11
|
-
"
|
|
10
|
+
"homepage": "https://github.com/storybookjs/storybook/tree/next/code/addons/mcp",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/storybookjs/storybook/issues"
|
|
13
|
+
},
|
|
12
14
|
"repository": {
|
|
13
15
|
"type": "git",
|
|
14
|
-
"url": "
|
|
15
|
-
"directory": "
|
|
16
|
+
"url": "https://github.com/storybookjs/storybook.git",
|
|
17
|
+
"directory": "code/addons/mcp"
|
|
18
|
+
},
|
|
19
|
+
"funding": {
|
|
20
|
+
"type": "opencollective",
|
|
21
|
+
"url": "https://opencollective.com/storybook"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "Storybook Core Team",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
"./internal/preview-stories-app-script": "./dist/tools/preview-stories/preview-stories-app-script.js",
|
|
28
|
+
"./package.json": "./package.json",
|
|
29
|
+
"./preset": "./dist/preset.js"
|
|
16
30
|
},
|
|
17
31
|
"files": [
|
|
18
32
|
"dist/**/*",
|
|
19
33
|
"README.md",
|
|
20
34
|
"*.js",
|
|
21
|
-
"*.d.ts"
|
|
35
|
+
"*.d.ts",
|
|
36
|
+
"!src/**/*"
|
|
22
37
|
],
|
|
23
|
-
"type": "module",
|
|
24
|
-
"exports": {
|
|
25
|
-
"./preset": "./dist/preset.js",
|
|
26
|
-
"./internal/preview-stories-app-script": "./dist/preview-stories-app-script.js",
|
|
27
|
-
"./package.json": "./package.json"
|
|
28
|
-
},
|
|
29
|
-
"publishConfig": {
|
|
30
|
-
"access": "public"
|
|
31
|
-
},
|
|
32
38
|
"dependencies": {
|
|
39
|
+
"@storybook/mcp": "10.6.0-alpha.4",
|
|
33
40
|
"@tmcp/adapter-valibot": "^0.1.5",
|
|
34
41
|
"@tmcp/transport-http": "^0.8.5",
|
|
35
42
|
"picoquery": "^2.5.0",
|
|
36
|
-
"tmcp": "^1.19.
|
|
37
|
-
"valibot": "1.
|
|
38
|
-
"@storybook/mcp": "0.7.0"
|
|
43
|
+
"tmcp": "^1.19.4",
|
|
44
|
+
"valibot": "^1.4.0"
|
|
39
45
|
},
|
|
40
46
|
"devDependencies": {
|
|
41
|
-
"@storybook/addon-a11y": "10.
|
|
42
|
-
"@storybook/addon-vitest": "10.
|
|
43
|
-
"storybook": "10.
|
|
47
|
+
"@storybook/addon-a11y": "10.6.0-alpha.4",
|
|
48
|
+
"@storybook/addon-vitest": "10.6.0-alpha.4",
|
|
49
|
+
"storybook": "10.6.0-alpha.4",
|
|
50
|
+
"typescript": "^6.0.3"
|
|
44
51
|
},
|
|
45
52
|
"peerDependencies": {
|
|
46
|
-
"@storybook/addon-vitest": "^
|
|
47
|
-
"storybook": "^
|
|
53
|
+
"@storybook/addon-vitest": "^10.6.0-alpha.4",
|
|
54
|
+
"storybook": "^10.6.0-alpha.4"
|
|
48
55
|
},
|
|
49
56
|
"peerDependenciesMeta": {
|
|
50
57
|
"@storybook/addon-vitest": {
|
|
51
58
|
"optional": true
|
|
52
59
|
}
|
|
53
60
|
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
},
|
|
54
64
|
"mcpName": "io.github.storybookjs/addon-mcp",
|
|
55
65
|
"storybook": {
|
|
56
66
|
"displayName": "Addon MCP",
|
|
@@ -65,13 +75,5 @@
|
|
|
65
75
|
"react-native"
|
|
66
76
|
],
|
|
67
77
|
"icon": "https://user-images.githubusercontent.com/321738/63501763-88dbf600-c4cc-11e9-96cd-94adadc2fd72.png"
|
|
68
|
-
},
|
|
69
|
-
"scripts": {
|
|
70
|
-
"prebuild": "node -e \"fs.rmSync('./dist', { recursive: true, force: true })\"",
|
|
71
|
-
"build": "tsdown",
|
|
72
|
-
"inspect": "mcp-inspector --config ../../.mcp.inspect.json --server storybook-addon-mcp",
|
|
73
|
-
"publint": "publint",
|
|
74
|
-
"test": "cd ../.. && pnpm vitest --project=@storybook/addon-mcp",
|
|
75
|
-
"typecheck": "tsc"
|
|
76
78
|
}
|
|
77
79
|
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023 Storybook contributors
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
//#region src/constants.ts
|
|
2
|
-
const MCP_APP_PARAM = "mcp-app";
|
|
3
|
-
const MCP_APP_SIZE_CHANGED_EVENT = "storybook-mcp:size-changed";
|
|
4
|
-
|
|
5
|
-
//#endregion
|
|
6
|
-
//#region package.json
|
|
7
|
-
var version = "0.6.0";
|
|
8
|
-
|
|
9
|
-
//#endregion
|
|
10
|
-
//#region src/tools/preview-stories/preview-stories-app-script.ts
|
|
11
|
-
/**
|
|
12
|
-
* Current protocol version - must match LATEST_PROTOCOL_VERSION from ext-apps
|
|
13
|
-
* @see https://github.com/modelcontextprotocol/ext-apps
|
|
14
|
-
*/
|
|
15
|
-
const LATEST_PROTOCOL_VERSION = "2025-11-21";
|
|
16
|
-
/**
|
|
17
|
-
* MCP Apps SEP protocol method constants
|
|
18
|
-
* These match the `method` field values from @modelcontextprotocol/ext-apps type definitions:
|
|
19
|
-
* - McpUiInitializeRequest: "ui/initialize"
|
|
20
|
-
* - McpUiInitializedNotification: "ui/notifications/initialized"
|
|
21
|
-
* - McpUiToolInputNotification: "ui/notifications/tool-input"
|
|
22
|
-
* - McpUiToolInputPartialNotification: "ui/notifications/tool-input-partial"
|
|
23
|
-
* - McpUiToolResultNotification: "ui/notifications/tool-result"
|
|
24
|
-
* - McpUiHostContextChangedNotification: "ui/notifications/host-context-changed"
|
|
25
|
-
* - McpUiSizeChangedNotification: "ui/notifications/size-changed"
|
|
26
|
-
* - McpUiResourceTeardownRequest: "ui/resource-teardown"
|
|
27
|
-
*
|
|
28
|
-
* @see https://github.com/modelcontextprotocol/ext-apps/blob/main/src/spec.types.ts
|
|
29
|
-
*/
|
|
30
|
-
const METHODS = {
|
|
31
|
-
INITIALIZE: "ui/initialize",
|
|
32
|
-
INITIALIZED: "ui/notifications/initialized",
|
|
33
|
-
TOOL_INPUT: "ui/notifications/tool-input",
|
|
34
|
-
TOOL_INPUT_PARTIAL: "ui/notifications/tool-input-partial",
|
|
35
|
-
TOOL_RESULT: "ui/notifications/tool-result",
|
|
36
|
-
TOOL_CANCELLED: "ui/notifications/tool-cancelled",
|
|
37
|
-
HOST_CONTEXT_CHANGED: "ui/notifications/host-context-changed",
|
|
38
|
-
SIZE_CHANGED: "ui/notifications/size-changed",
|
|
39
|
-
RESOURCE_TEARDOWN: "ui/resource-teardown",
|
|
40
|
-
TOOLS_CALL: "tools/call",
|
|
41
|
-
NOTIFICATIONS_MESSAGE: "notifications/message",
|
|
42
|
-
OPEN_LINK: "ui/open-link",
|
|
43
|
-
MESSAGE: "ui/message"
|
|
44
|
-
};
|
|
45
|
-
let nextId = 1;
|
|
46
|
-
function sendHostRequest(method, params) {
|
|
47
|
-
const id = nextId++;
|
|
48
|
-
const { promise, resolve, reject } = Promise.withResolvers();
|
|
49
|
-
window.parent.postMessage({
|
|
50
|
-
jsonrpc: "2.0",
|
|
51
|
-
id,
|
|
52
|
-
method,
|
|
53
|
-
params
|
|
54
|
-
}, "*");
|
|
55
|
-
window.addEventListener("message", function listener(event) {
|
|
56
|
-
if (event.data?.id !== id) return;
|
|
57
|
-
window.removeEventListener("message", listener);
|
|
58
|
-
if (event.data?.result) resolve(event.data.result);
|
|
59
|
-
else if (event.data?.error) reject(new Error(String(event.data.error)));
|
|
60
|
-
});
|
|
61
|
-
return promise;
|
|
62
|
-
}
|
|
63
|
-
function sendHostNotification(method, params) {
|
|
64
|
-
window.parent.postMessage({
|
|
65
|
-
jsonrpc: "2.0",
|
|
66
|
-
method,
|
|
67
|
-
params
|
|
68
|
-
}, "*");
|
|
69
|
-
}
|
|
70
|
-
function onHostNotification(method, handler) {
|
|
71
|
-
window.addEventListener("message", function listener(event) {
|
|
72
|
-
if (event.data?.method === method) handler(event.data.params);
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
applyHostStyles((await sendHostRequest(METHODS.INITIALIZE, {
|
|
76
|
-
appInfo: {
|
|
77
|
-
name: "storybook-story-preview",
|
|
78
|
-
version
|
|
79
|
-
},
|
|
80
|
-
appCapabilities: {},
|
|
81
|
-
protocolVersion: LATEST_PROTOCOL_VERSION
|
|
82
|
-
}))?.hostContext);
|
|
83
|
-
onHostNotification(METHODS.TOOL_RESULT, loadStoryIframes);
|
|
84
|
-
onHostNotification(METHODS.HOST_CONTEXT_CHANGED, applyHostStyles);
|
|
85
|
-
sendHostNotification(METHODS.INITIALIZED, {});
|
|
86
|
-
window.addEventListener("message", function(event) {
|
|
87
|
-
if (event.data?.type !== MCP_APP_SIZE_CHANGED_EVENT) return;
|
|
88
|
-
const iframes = document.querySelectorAll(".story-iframe");
|
|
89
|
-
let hasResizedIframes = false;
|
|
90
|
-
for (const iframe of iframes) if (iframe.contentWindow === event.source) {
|
|
91
|
-
iframe.style.height = (event.data.height ?? 0) + "px";
|
|
92
|
-
hasResizedIframes = true;
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
if (hasResizedIframes) resizeApp();
|
|
96
|
-
});
|
|
97
|
-
function applyHostStyles(hostContext) {
|
|
98
|
-
if (hostContext?.theme) document.documentElement.setAttribute("data-theme", hostContext.theme);
|
|
99
|
-
if (!hostContext?.styles?.variables) return;
|
|
100
|
-
for (const [key, value] of Object.entries(hostContext.styles.variables)) if (value) document.documentElement.style.setProperty(key, value);
|
|
101
|
-
resizeApp();
|
|
102
|
-
}
|
|
103
|
-
function resizeApp() {
|
|
104
|
-
console.log("Resizing app to fit content", {
|
|
105
|
-
width: document.body.scrollWidth,
|
|
106
|
-
height: document.body.scrollHeight
|
|
107
|
-
});
|
|
108
|
-
sendHostNotification(METHODS.SIZE_CHANGED, {
|
|
109
|
-
width: document.body.scrollWidth,
|
|
110
|
-
height: document.body.scrollHeight
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
function loadStoryIframes(params) {
|
|
114
|
-
const stories = params.structuredContent?.stories;
|
|
115
|
-
if (!stories || stories.length === 0) {
|
|
116
|
-
console.warn("No preview URLs found in tool result.");
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
const template = document.getElementById("preview-template");
|
|
120
|
-
for (const storyResult of stories) {
|
|
121
|
-
if ("error" in storyResult) {
|
|
122
|
-
console.warn("Skipping story with error:", storyResult.error);
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
const clone = template.content.cloneNode(true);
|
|
126
|
-
const article = clone.querySelector("article");
|
|
127
|
-
const heading = clone.querySelector("h1");
|
|
128
|
-
const iframe = clone.querySelector("iframe");
|
|
129
|
-
heading.textContent = `${storyResult.title} - ${storyResult.name}`;
|
|
130
|
-
iframe.style.width = "100%";
|
|
131
|
-
iframe.style.height = "0";
|
|
132
|
-
const iframeSrc = storyResult.previewUrl.replace("/?path=/story/", "/iframe.html?id=");
|
|
133
|
-
const url = new URL(iframeSrc);
|
|
134
|
-
url.searchParams.set(MCP_APP_PARAM, "true");
|
|
135
|
-
iframe.src = url.toString();
|
|
136
|
-
document.body.appendChild(article);
|
|
137
|
-
}
|
|
138
|
-
resizeApp();
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
//#endregion
|
|
142
|
-
export { };
|