Skip to content

Commit 1f431d0

Browse files
committed
Merge remote-tracking branch 'origin/main' into ochafik/openai-compatibility
2 parents 33b58e0 + f3b3c48 commit 1f431d0

14 files changed

Lines changed: 330 additions & 116 deletions

File tree

examples/basic-host/src/implementation.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RESOURCE_MIME_TYPE, getToolUiResourceUri, type McpUiSandboxProxyReadyNotification, AppBridge, PostMessageTransport, type McpUiResourceCsp, type McpUiResourcePermissions, buildAllowAttribute, type McpUiUpdateModelContextRequest, type McpUiMessageRequest } from "@modelcontextprotocol/ext-apps/app-bridge";
22
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
3+
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
34
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
45
import type { CallToolResult, Tool } from "@modelcontextprotocol/sdk/types.js";
56

@@ -24,11 +25,8 @@ export interface ServerInfo {
2425

2526

2627
export async function connectToServer(serverUrl: URL): Promise<ServerInfo> {
27-
const client = new Client(IMPLEMENTATION);
28-
2928
log.info("Connecting to server:", serverUrl.href);
30-
await client.connect(new StreamableHTTPClientTransport(serverUrl));
31-
log.info("Connection successful");
29+
const client = await connectWithFallback(serverUrl);
3230

3331
const name = client.getServerVersion()?.name ?? serverUrl.href;
3432

@@ -39,6 +37,28 @@ export async function connectToServer(serverUrl: URL): Promise<ServerInfo> {
3937
return { name, client, tools, appHtmlCache: new Map() };
4038
}
4139

40+
async function connectWithFallback(serverUrl: URL): Promise<Client> {
41+
// Try Streamable HTTP first (modern transport)
42+
try {
43+
const client = new Client(IMPLEMENTATION);
44+
await client.connect(new StreamableHTTPClientTransport(serverUrl));
45+
log.info("Connected via Streamable HTTP transport");
46+
return client;
47+
} catch (streamableError) {
48+
log.info("Streamable HTTP failed:", streamableError);
49+
}
50+
51+
// Fall back to SSE (deprecated but needed for older servers)
52+
try {
53+
const client = new Client(IMPLEMENTATION);
54+
await client.connect(new SSEClientTransport(serverUrl));
55+
log.info("Connected via SSE transport");
56+
return client;
57+
} catch (sseError) {
58+
throw new Error(`Could not connect with any transport. SSE error: ${sseError}`);
59+
}
60+
}
61+
4262

4363
interface UiResourceData {
4464
html: string;

examples/qr-server/grid-cell.png

-12.1 KB
Loading

examples/qr-server/server.py

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# /// script
33
# requires-python = ">=3.10"
44
# dependencies = [
5-
# "mcp>=1.9.0",
5+
# "mcp @ git+https://github.com/modelcontextprotocol/python-sdk@main",
66
# "qrcode[pil]>=8.0",
77
# "uvicorn>=0.34.0",
88
# "starlette>=0.46.0",
@@ -30,7 +30,10 @@
3030
mcp = FastMCP("QR Code Server", port=PORT, stateless_http=True)
3131

3232

33-
@mcp.tool(meta={"ui/resourceUri": WIDGET_URI})
33+
@mcp.tool(meta={
34+
"ui":{"resourceUri": WIDGET_URI},
35+
"ui/resourceUri": WIDGET_URI, # legacy support
36+
})
3437
def generate_qr(
3538
text: str = "https://modelcontextprotocol.io",
3639
box_size: int = 10,
@@ -72,53 +75,17 @@ def generate_qr(
7275
return [types.ImageContent(type="image", data=b64, mimeType="image/png")]
7376

7477

75-
# Register widget resource using FastMCP decorator (returns HTML string)
76-
@mcp.resource(WIDGET_URI, mime_type="text/html;profile=mcp-app")
78+
# IMPORTANT: all the external domains used by app must be listed
79+
# in the meta.ui.csp.resourceDomains - otherwise they will be blocked by CSP policy
80+
@mcp.resource(
81+
WIDGET_URI,
82+
mime_type="text/html;profile=mcp-app",
83+
meta={"ui": {"csp": {"resourceDomains": ["https://unpkg.com"]}}},
84+
)
7785
def widget() -> str:
86+
"""Widget HTML resource with CSP metadata for external dependencies."""
7887
return Path(__file__).parent.joinpath("widget.html").read_text()
7988

80-
81-
# Override the read_resource handler to inject _meta into the response
82-
# This is needed because FastMCP doesn't support custom _meta on resources
83-
_low_level_server = mcp._mcp_server
84-
85-
86-
async def _read_resource_with_meta(req: types.ReadResourceRequest):
87-
"""Custom handler that injects CSP metadata for the widget resource."""
88-
uri = str(req.params.uri)
89-
html = Path(__file__).parent.joinpath("widget.html").read_text()
90-
91-
if uri == WIDGET_URI:
92-
# NOTE: Must use model_validate with '_meta' key (not 'meta') due to Pydantic alias behavior
93-
content = types.TextResourceContents.model_validate({
94-
"uri": WIDGET_URI,
95-
"mimeType": "text/html;profile=mcp-app",
96-
"text": html,
97-
# IMPORTANT: all the external domains used by app must be listed
98-
# in the _meta.ui.csp.resourceDomains - otherwise they will be blocked by CSP policy
99-
"_meta": {"ui": {"csp": {"resourceDomains": ["https://unpkg.com"]}}}
100-
})
101-
return types.ServerResult(
102-
types.ReadResourceResult(contents=[content])
103-
)
104-
105-
# Fallback for other resources (shouldn't happen for this server)
106-
return types.ServerResult(
107-
types.ReadResourceResult(
108-
contents=[
109-
types.TextResourceContents(
110-
uri=uri,
111-
mimeType="text/plain",
112-
text="Resource not found"
113-
)
114-
]
115-
)
116-
)
117-
118-
119-
# Replace the handler after FastMCP has registered its own
120-
_low_level_server.request_handlers[types.ReadResourceRequest] = _read_resource_with_meta
121-
12289
if __name__ == "__main__":
12390
if "--stdio" in sys.argv:
12491
# Claude Desktop mode

examples/run-all.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* bun examples/run-all.ts start - Build and start all examples
77
* bun examples/run-all.ts dev - Run all examples in dev/watch mode
88
* bun examples/run-all.ts build - Build all examples
9+
*
10+
* Environment:
11+
* EXAMPLE=<folder> - Run only a single example (e.g., EXAMPLE=say-server)
912
*/
1013

1114
import { readdirSync, statSync, existsSync } from "fs";
@@ -14,21 +17,36 @@ import concurrently from "concurrently";
1417
const BASE_PORT = 3101;
1518
const BASIC_HOST = "basic-host";
1619

20+
// Optional: filter to a single example via EXAMPLE env var (folder name)
21+
const EXAMPLE_FILTER = process.env.EXAMPLE;
22+
1723
// Find all example directories except basic-host that have a package.json,
1824
// assign ports, and build URL list
19-
const servers = readdirSync("examples")
25+
const allServers = readdirSync("examples")
2026
.filter(
2127
(d) =>
2228
d !== BASIC_HOST &&
2329
statSync(`examples/${d}`).isDirectory() &&
2430
existsSync(`examples/${d}/package.json`),
2531
)
26-
.sort() // Sort for consistent port assignment
27-
.map((dir, i) => ({
28-
dir,
29-
port: BASE_PORT + i,
30-
url: `http://localhost:${BASE_PORT + i}/mcp`,
31-
}));
32+
.sort(); // Sort for consistent port assignment
33+
34+
// Filter servers if EXAMPLE is specified
35+
const filteredDirs = EXAMPLE_FILTER
36+
? allServers.filter((d) => d === EXAMPLE_FILTER)
37+
: allServers;
38+
39+
if (EXAMPLE_FILTER && filteredDirs.length === 0) {
40+
console.error(`Error: No example found matching EXAMPLE=${EXAMPLE_FILTER}`);
41+
console.error(`Available examples: ${allServers.join(", ")}`);
42+
process.exit(1);
43+
}
44+
45+
const servers = filteredDirs.map((dir, i) => ({
46+
dir,
47+
port: BASE_PORT + i,
48+
url: `http://localhost:${BASE_PORT + i}/mcp`,
49+
}));
3250

3351
const COMMANDS = ["start", "dev", "build"];
3452

@@ -43,6 +61,9 @@ if (!command || !COMMANDS.includes(command)) {
4361
const serversEnv = JSON.stringify(servers.map((s) => s.url));
4462

4563
console.log(`Running command: ${command}`);
64+
if (EXAMPLE_FILTER) {
65+
console.log(`Filtering to single example: ${EXAMPLE_FILTER}`);
66+
}
4667
console.log(
4768
`Server examples: ${servers.map((s) => `${s.dir}:${s.port}`).join(", ")}`,
4869
);

examples/shadertoy-server/README.md

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ A demo MCP App that renders [ShaderToy](https://www.shadertoy.com/)-compatible G
1515
- **Real-time Rendering**: Renders GLSL shaders using WebGL 2.0
1616
- **ShaderToy Compatibility**: Uses the standard `mainImage(out vec4 fragColor, in vec2 fragCoord)` entry point
1717
- **Multi-pass Rendering**: Supports buffers A-D for feedback effects, blur chains, and simulations
18+
- **Mouse & Touch Interaction**: Full iMouse support with click detection (works on mobile)
1819
- **Standard Uniforms**: iResolution, iTime, iTimeDelta, iFrame, iMouse, iDate, iChannel0-3
1920

2021
## Running
@@ -101,13 +102,20 @@ _Tool input:_
101102
}
102103
```
103104

104-
**Interactive Julia Set** (mouse controls the fractal's c parameter):
105+
**Interactive Julia Set** (click and drag to control the fractal's c parameter):
105106

106107
```glsl
107108
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
108109
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y * 2.5;
109-
vec2 mouse = (iMouse.xy / iResolution.xy - 0.5) * 2.0;
110-
vec2 c = mouse;
110+
// Use mouse position if clicked, otherwise use animated default
111+
vec2 c;
112+
if (iMouse.z > 0.0) {
113+
// Mouse is pressed - use mouse position
114+
c = (iMouse.xy / iResolution.xy - 0.5) * 2.0;
115+
} else {
116+
// Not pressed - animate around an interesting region
117+
c = vec2(-0.8 + 0.2 * sin(iTime * 0.5), 0.156 + 0.1 * cos(iTime * 0.7));
118+
}
111119
vec2 z = uv;
112120
float iter = 0.0;
113121
for (int i = 0; i < 100; i++) {
@@ -126,25 +134,33 @@ _Tool input:_
126134

127135
```json
128136
{
129-
"fragmentShader": "void mainImage(out vec4 fragColor, in vec2 fragCoord) {
130-
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y * 2.5;
131-
vec2 mouse = (iMouse.xy / iResolution.xy - 0.5) * 2.0;
132-
vec2 c = mouse;
133-
vec2 z = uv;
134-
float iter = 0.0;
135-
for (int i = 0; i < 100; i++) {
136-
z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c;
137-
if (dot(z, z) > 4.0) break;
138-
iter++;
139-
}
140-
float t = iter / 100.0;
141-
vec3 col = 0.5 + 0.5 * cos(3.0 + t * 6.28 * 2.0 + vec3(0.0, 0.6, 1.0));
142-
if (iter == 100.0) col = vec3(0.0);
143-
fragColor = vec4(col, 1.0);
144-
}"
137+
"fragmentShader": "void mainImage(out vec4 fragColor, in vec2 fragCoord) {\n vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y * 2.5;\n vec2 c;\n if (iMouse.z > 0.0) {\n c = (iMouse.xy / iResolution.xy - 0.5) * 2.0;\n } else {\n c = vec2(-0.8 + 0.2 * sin(iTime * 0.5), 0.156 + 0.1 * cos(iTime * 0.7));\n }\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}"
138+
}
139+
```
140+
141+
## Mouse & Touch Interaction
142+
143+
The `iMouse` uniform provides interactive input, compatible with the official Shadertoy specification:
144+
145+
| Component | When Button Down | After Release | Never Clicked |
146+
| ----------- | ---------------------- | ---------------- | ------------- |
147+
| `iMouse.xy` | Current position | Last position | `(0, 0)` |
148+
| `iMouse.zw` | Click start (positive) | Negated (-x, -y) | `(0, 0)` |
149+
150+
**Detecting button state:**
151+
152+
```glsl
153+
if (iMouse.z > 0.0) {
154+
// Button/touch is currently held down
155+
} else if (iMouse.z < 0.0) {
156+
// Button was released (can use abs(iMouse.zw) for last click position)
157+
} else {
158+
// Never clicked - show default state or animate
145159
}
146160
```
147161

162+
Touch events are automatically supported for mobile devices.
163+
148164
## Architecture
149165

150166
### Server (`server.ts`)

examples/shadertoy-server/server.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@ AVAILABLE UNIFORMS:
3333
- iTime (float): elapsed time in seconds
3434
- iTimeDelta (float): time since last frame
3535
- iFrame (int): frame counter
36-
- iMouse (vec4): mouse position (xy=current, zw=click)
36+
- iMouse (vec4): mouse/touch position in pixels (see MOUSE INTERACTION below)
3737
- iDate (vec4): year, month, day, seconds
3838
- iChannel0-3 (sampler2D): buffer inputs for multi-pass shaders
3939
40+
MOUSE INTERACTION:
41+
The iMouse uniform provides interactive mouse/touch input (works on mobile):
42+
- iMouse.xy: Current position while button/touch is held down (frozen on release)
43+
- iMouse.zw: Click start position (positive when down, negative when released)
44+
- iMouse.z > 0: Button is currently pressed
45+
- iMouse.z < 0: Button was released
46+
- iMouse.z == 0: Never clicked
47+
48+
Example - camera control:
49+
vec2 uv = iMouse.xy / iResolution.xy; // normalized 0-1
50+
51+
Example - detect click:
52+
if (iMouse.z > 0.0) { /* button is down */ }
53+
4054
MULTI-PASS RENDERING:
4155
- Use bufferA-D parameters for feedback effects, blur chains, simulations
4256
- BufferA output -> iChannel0, BufferB -> iChannel1, etc.

examples/shadertoy-server/src/vendor/ShaderToyLite.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ function ShaderToyLite(canvasId) {
9090

9191
// uniforms
9292
var iFrame = 0;
93+
// iMouse state: xy = current position (only updates when button down)
94+
// zw = click start position (positive when down, negative when released)
9395
var iMouse = {x: 0, y: 0, clickX: 0, clickY: 0};
96+
var isMouseDown = false;
9497

9598
// shader common source
9699
var common = "";
@@ -145,19 +148,75 @@ function ShaderToyLite(canvasId) {
145148
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
146149
});
147150

151+
// Mouse event handlers - match official Shadertoy iMouse behavior:
152+
// iMouse.xy: current position (only updates when button is down)
153+
// iMouse.zw: click position (positive when down, negated when released)
148154
canvas.addEventListener("mousemove", (event) => {
149-
iMouse.x = event.offsetX;
150-
iMouse.y = canvas.height - event.offsetY;
155+
if (isMouseDown) {
156+
iMouse.x = event.offsetX;
157+
iMouse.y = canvas.height - event.offsetY;
158+
}
151159
});
152160

153161
canvas.addEventListener("mousedown", (event) => {
154-
iMouse.clickX = event.offsetX;
155-
iMouse.clickY = canvas.height - event.offsetY;
162+
isMouseDown = true;
163+
var x = event.offsetX;
164+
var y = canvas.height - event.offsetY;
165+
iMouse.x = x;
166+
iMouse.y = y;
167+
iMouse.clickX = x;
168+
iMouse.clickY = y;
156169
});
157170

158171
canvas.addEventListener("mouseup", () => {
159-
iMouse.clickX = 0;
160-
iMouse.clickY = 0;
172+
isMouseDown = false;
173+
// Negate click position to indicate button released (Shadertoy convention)
174+
iMouse.clickX = -Math.abs(iMouse.clickX);
175+
iMouse.clickY = -Math.abs(iMouse.clickY);
176+
});
177+
178+
// Prevent context menu on right-click to avoid interrupting interaction
179+
canvas.addEventListener("contextmenu", (event) => {
180+
event.preventDefault();
181+
});
182+
183+
// Touch support for mobile devices
184+
canvas.addEventListener("touchstart", (event) => {
185+
event.preventDefault();
186+
if (event.touches.length > 0) {
187+
var touch = event.touches[0];
188+
var rect = canvas.getBoundingClientRect();
189+
var x = touch.clientX - rect.left;
190+
var y = canvas.height - (touch.clientY - rect.top);
191+
isMouseDown = true;
192+
iMouse.x = x;
193+
iMouse.y = y;
194+
iMouse.clickX = x;
195+
iMouse.clickY = y;
196+
}
197+
}, { passive: false });
198+
199+
canvas.addEventListener("touchmove", (event) => {
200+
event.preventDefault();
201+
if (isMouseDown && event.touches.length > 0) {
202+
var touch = event.touches[0];
203+
var rect = canvas.getBoundingClientRect();
204+
iMouse.x = touch.clientX - rect.left;
205+
iMouse.y = canvas.height - (touch.clientY - rect.top);
206+
}
207+
}, { passive: false });
208+
209+
canvas.addEventListener("touchend", (event) => {
210+
event.preventDefault();
211+
isMouseDown = false;
212+
iMouse.clickX = -Math.abs(iMouse.clickX);
213+
iMouse.clickY = -Math.abs(iMouse.clickY);
214+
}, { passive: false });
215+
216+
canvas.addEventListener("touchcancel", () => {
217+
isMouseDown = false;
218+
iMouse.clickX = -Math.abs(iMouse.clickX);
219+
iMouse.clickY = -Math.abs(iMouse.clickY);
161220
});
162221
}
163222

0 commit comments

Comments
 (0)