|
| 1 | +# Example: ShaderToy Server |
| 2 | + |
| 3 | +A demo MCP App that renders [ShaderToy](https://www.shadertoy.com/)-compatible GLSL fragment shaders in real-time using WebGL 2.0 and [ShaderToyLite.js](https://github.com/nickoala/ShaderToyLite). |
| 4 | + |
| 5 | +<table> |
| 6 | + <tr> |
| 7 | + <td><a href="https://modelcontextprotocol.github.io/ext-apps/screenshots/shadertoy-server/01-gradient.png"><img src="https://modelcontextprotocol.github.io/ext-apps/screenshots/shadertoy-server/01-gradient.png" alt="Gradient" width="100%"></a></td> |
| 8 | + <td><a href="https://modelcontextprotocol.github.io/ext-apps/screenshots/shadertoy-server/02-kaleidoscope.png"><img src="https://modelcontextprotocol.github.io/ext-apps/screenshots/shadertoy-server/02-kaleidoscope.png" alt="Kaleidoscope" width="100%"></a></td> |
| 9 | + <td><a href="https://modelcontextprotocol.github.io/ext-apps/screenshots/shadertoy-server/03-fractal.png"><img src="https://modelcontextprotocol.github.io/ext-apps/screenshots/shadertoy-server/03-fractal.png" alt="Kaleidoscope" width="100%"></a></td> |
| 10 | + </tr> |
| 11 | +</table> |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- **Real-time Rendering**: Renders GLSL shaders using WebGL 2.0 |
| 16 | +- **ShaderToy Compatibility**: Uses the standard `mainImage(out vec4 fragColor, in vec2 fragCoord)` entry point |
| 17 | +- **Multi-pass Rendering**: Supports buffers A-D for feedback effects, blur chains, and simulations |
| 18 | +- **Standard Uniforms**: iResolution, iTime, iTimeDelta, iFrame, iMouse, iDate, iChannel0-3 |
| 19 | + |
| 20 | +## Running |
| 21 | + |
| 22 | +1. Install dependencies: |
| 23 | + |
| 24 | + ```bash |
| 25 | + npm install |
| 26 | + ``` |
| 27 | + |
| 28 | +2. Build and start the server: |
| 29 | + |
| 30 | + ```bash |
| 31 | + npm run start:http # for Streamable HTTP transport |
| 32 | + # OR |
| 33 | + npm run start:stdio # for stdio transport |
| 34 | + ``` |
| 35 | + |
| 36 | +3. View using the [`basic-host`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-host) example or another MCP Apps-compatible host. |
| 37 | + |
| 38 | +### Tool Input Examples |
| 39 | + |
| 40 | +**Gradient with Time:** |
| 41 | + |
| 42 | +```glsl |
| 43 | +void mainImage(out vec4 fragColor, in vec2 fragCoord) { |
| 44 | + vec2 uv = fragCoord / iResolution.xy; |
| 45 | + fragColor = vec4(uv, 0.5 + 0.5*sin(iTime), 1.0); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +_Tool input:_ |
| 50 | + |
| 51 | +```json |
| 52 | +{ |
| 53 | + "fragmentShader": "void mainImage(out vec4 fragColor, in vec2 fragCoord) {\n vec2 uv = fragCoord / iResolution.xy;\n fragColor = vec4(uv, 0.5 + 0.5*sin(iTime), 1.0);\n}" |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +**Kaleidoscope**: |
| 58 | + |
| 59 | +```glsl |
| 60 | +void mainImage(out vec4 fragColor, in vec2 fragCoord) { |
| 61 | + vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y; |
| 62 | + float segments = 6.0; |
| 63 | + float zoom = 1.0 + 0.3 * sin(iTime * 0.2); |
| 64 | + float angle = atan(uv.y, uv.x) + iTime * 0.3; |
| 65 | + float r = length(uv) * zoom; |
| 66 | + angle = mod(angle, 6.28 / segments); |
| 67 | + angle = abs(angle - 3.14 / segments); |
| 68 | + vec2 p = vec2(cos(angle), sin(angle)) * r; |
| 69 | + p += iTime * 0.1; |
| 70 | + float v = sin(p.x * 10.0) * sin(p.y * 10.0); |
| 71 | + v += sin(length(p) * 15.0 - iTime * 2.0); |
| 72 | + v += sin(p.x * 5.0 + p.y * 7.0 + iTime); |
| 73 | + vec3 col = 0.5 + 0.5 * cos(v * 2.0 + vec3(0.0, 2.0, 4.0) + iTime); |
| 74 | + fragColor = vec4(col, 1.0); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +_Tool input:_ |
| 79 | + |
| 80 | +```json |
| 81 | +{ |
| 82 | + "fragmentShader": "void mainImage(out vec4 fragColor, in vec2 fragCoord) {\n vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n float segments = 6.0;\n float zoom = 1.0 + 0.3 * sin(iTime * 0.2);\n float angle = atan(uv.y, uv.x) + iTime * 0.3;\n float r = length(uv) * zoom;\n angle = mod(angle, 6.28 / segments);\n angle = abs(angle - 3.14 / segments);\n vec2 p = vec2(cos(angle), sin(angle)) * r;\n p += iTime * 0.1;\n float v = sin(p.x * 10.0) * sin(p.y * 10.0);\n v += sin(length(p) * 15.0 - iTime * 2.0);\n v += sin(p.x * 5.0 + p.y * 7.0 + iTime);\n vec3 col = 0.5 + 0.5 * cos(v * 2.0 + vec3(0.0, 2.0, 4.0) + iTime);\n fragColor = vec4(col, 1.0);\n}" |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +**Interactive Julia Set** (mouse controls the fractal's c parameter): |
| 87 | + |
| 88 | +```glsl |
| 89 | +void mainImage(out vec4 fragColor, in vec2 fragCoord) { |
| 90 | + vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y * 2.5; |
| 91 | + vec2 mouse = (iMouse.xy / iResolution.xy - 0.5) * 2.0; |
| 92 | + vec2 c = mouse; |
| 93 | + vec2 z = uv; |
| 94 | + float iter = 0.0; |
| 95 | + for (int i = 0; i < 100; i++) { |
| 96 | + z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c; |
| 97 | + if (dot(z, z) > 4.0) break; |
| 98 | + iter++; |
| 99 | + } |
| 100 | + float t = iter / 100.0; |
| 101 | + vec3 col = 0.5 + 0.5 * cos(3.0 + t * 6.28 * 2.0 + vec3(0.0, 0.6, 1.0)); |
| 102 | + if (iter == 100.0) col = vec3(0.0); |
| 103 | + fragColor = vec4(col, 1.0); |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +_Tool input:_ |
| 108 | + |
| 109 | +```json |
| 110 | +{ |
| 111 | + "fragmentShader": "void mainImage(out vec4 fragColor, in vec2 fragCoord) {\n vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y * 2.5;\n vec2 mouse = (iMouse.xy / iResolution.xy - 0.5) * 2.0;\n vec2 c = mouse;\n vec2 z = uv;\n float iter = 0.0;\n for (int i = 0; i < 100; i++) {\n z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c;\n if (dot(z, z) > 4.0) break;\n iter++;\n }\n float t = iter / 100.0;\n vec3 col = 0.5 + 0.5 * cos(3.0 + t * 6.28 * 2.0 + vec3(0.0, 0.6, 1.0));\n if (iter == 100.0) col = vec3(0.0);\n fragColor = vec4(col, 1.0);\n}" |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Architecture |
| 116 | + |
| 117 | +### Server (`server.ts`) |
| 118 | + |
| 119 | +Exposes a single `render-shadertoy` tool that accepts: |
| 120 | + |
| 121 | +- `fragmentShader`: Main Image shader code (required) |
| 122 | +- `common`: Shared code across all shaders (optional) |
| 123 | +- `bufferA`: Buffer A shader, accessible as iChannel0 (optional) |
| 124 | +- `bufferB`: Buffer B shader, accessible as iChannel1 (optional) |
| 125 | +- `bufferC`: Buffer C shader, accessible as iChannel2 (optional) |
| 126 | +- `bufferD`: Buffer D shader, accessible as iChannel3 (optional) |
| 127 | + |
| 128 | +### App (`src/mcp-app.ts`) |
| 129 | + |
| 130 | +- Receives shader code via `ontoolinput` handler |
| 131 | +- Uses ShaderToyLite.js for WebGL rendering |
| 132 | +- Displays compilation errors in an overlay |
0 commit comments