-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathmcp-app.ts
More file actions
207 lines (177 loc) · 5.45 KB
/
mcp-app.ts
File metadata and controls
207 lines (177 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* ShaderToy renderer MCP App using ShaderToyLite.js
*/
import {
App,
type McpUiHostContext,
applyHostStyleVariables,
applyDocumentTheme,
} from "@modelcontextprotocol/ext-apps";
import "./global.css";
import "./mcp-app.css";
import ShaderToyLite, {
type ShaderToyLiteInstance,
} from "./vendor/ShaderToyLite.js";
interface ShaderInput {
fragmentShader: string;
common?: string;
bufferA?: string;
bufferB?: string;
bufferC?: string;
bufferD?: string;
}
function isShaderInput(value: unknown): value is ShaderInput {
return (
typeof value === "object" &&
value !== null &&
typeof (value as Record<string, unknown>).fragmentShader === "string"
);
}
const log = {
info: console.log.bind(console, "[APP]"),
warn: console.warn.bind(console, "[APP]"),
error: console.error.bind(console, "[APP]"),
};
// Get element references
const mainEl = document.querySelector(".main") as HTMLElement;
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
const codePreview = document.getElementById("code-preview") as HTMLPreElement;
const fullscreenBtn = document.getElementById(
"fullscreen-btn",
) as HTMLButtonElement;
// Display mode state
let currentDisplayMode: "inline" | "fullscreen" = "inline";
// Resize canvas to fill viewport
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas();
window.addEventListener("resize", resizeCanvas);
// Handle host context changes (display mode, styling)
function handleHostContextChanged(ctx: McpUiHostContext) {
// Apply host styling
if (ctx.theme) applyDocumentTheme(ctx.theme);
if (ctx.styles?.variables) applyHostStyleVariables(ctx.styles.variables);
// Note: We ignore safeAreaInsets to maximize shader display area
// Show fullscreen button if available (only update if field is present)
if (ctx.availableDisplayModes !== undefined) {
if (ctx.availableDisplayModes.includes("fullscreen")) {
fullscreenBtn.classList.add("available");
} else {
fullscreenBtn.classList.remove("available");
}
}
// Update display mode state and UI
if (ctx.displayMode) {
currentDisplayMode = ctx.displayMode as "inline" | "fullscreen";
if (currentDisplayMode === "fullscreen") {
mainEl.classList.add("fullscreen");
} else {
mainEl.classList.remove("fullscreen");
}
}
}
// Handle Escape key to exit fullscreen
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && currentDisplayMode === "fullscreen") {
toggleFullscreen();
}
});
// Toggle fullscreen mode
async function toggleFullscreen() {
const newMode = currentDisplayMode === "fullscreen" ? "inline" : "fullscreen";
try {
const result = await app.requestDisplayMode({ mode: newMode });
currentDisplayMode = result.mode as "inline" | "fullscreen";
if (currentDisplayMode === "fullscreen") {
mainEl.classList.add("fullscreen");
} else {
mainEl.classList.remove("fullscreen");
}
} catch (err) {
log.error("Failed to change display mode:", err);
}
}
fullscreenBtn.addEventListener("click", toggleFullscreen);
// ShaderToyLite instance
let shaderToy: ShaderToyLiteInstance | null = null;
// Create app instance
const app = new App({ name: "ShaderToy Renderer", version: "1.0.0" });
app.onteardown = async () => {
log.info("App is being torn down");
if (shaderToy) {
shaderToy.pause();
}
return {};
};
app.ontoolinputpartial = (params) => {
// Show code preview, hide canvas
codePreview.classList.add("visible");
canvas.classList.add("hidden");
const code = params.arguments?.fragmentShader;
codePreview.textContent = typeof code === "string" ? code : "";
codePreview.scrollTop = codePreview.scrollHeight;
};
app.ontoolinput = (params) => {
log.info("Received shader input");
// Hide code preview, show canvas
codePreview.classList.remove("visible");
canvas.classList.remove("hidden");
if (!isShaderInput(params.arguments)) {
log.error("Invalid tool input");
return;
}
const { fragmentShader, common, bufferA, bufferB, bufferC, bufferD } =
params.arguments;
// Initialize ShaderToyLite if needed
if (!shaderToy) {
shaderToy = new ShaderToyLite("canvas");
}
// Set common code (shared across all shaders)
shaderToy.setCommon(common || "");
// Set buffer shaders with self-feedback
if (bufferA) {
shaderToy.setBufferA({ source: bufferA, iChannel0: "A" });
}
if (bufferB) {
shaderToy.setBufferB({ source: bufferB, iChannel1: "B" });
}
if (bufferC) {
shaderToy.setBufferC({ source: bufferC, iChannel2: "C" });
}
if (bufferD) {
shaderToy.setBufferD({ source: bufferD, iChannel3: "D" });
}
// Set main Image shader with buffer inputs
shaderToy.setImage({
source: fragmentShader,
iChannel0: bufferA ? "A" : undefined,
iChannel1: bufferB ? "B" : undefined,
iChannel2: bufferC ? "C" : undefined,
iChannel3: bufferD ? "D" : undefined,
});
shaderToy.play();
log.info("Setup complete");
};
app.onerror = log.error;
app.onhostcontextchanged = handleHostContextChanged;
// Pause/resume shader based on visibility
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
shaderToy?.play();
} else {
shaderToy?.pause();
}
});
});
observer.observe(mainEl);
// Connect to host
app.connect().then(() => {
log.info("Connected to host");
const ctx = app.getHostContext();
if (ctx) {
handleHostContextChanged(ctx);
}
});