Spaces:
Running
Running
File size: 7,484 Bytes
4cfc55d deb8d3c 48a7d73 ee7487c 48a7d73 ee7487c 4cfc55d deb8d3c d090f04 deb8d3c ee7487c 48a7d73 4cfc55d d090f04 4cfc55d ee7487c deb8d3c ee7487c deb8d3c ee7487c deb8d3c ee7487c deb8d3c ee7487c deb8d3c ee7487c deb8d3c ee7487c deb8d3c ee7487c deb8d3c ee7487c deb8d3c ee7487c deb8d3c ee7487c deb8d3c ee7487c deb8d3c ee7487c 48a7d73 deb8d3c ee7487c deb8d3c 4cfc55d ee7487c 48a7d73 ee7487c deb8d3c 48a7d73 deb8d3c 48a7d73 4cfc55d ee7487c 4cfc55d ee7487c 4cfc55d deb8d3c 48a7d73 deb8d3c 48a7d73 deb8d3c 48a7d73 4cfc55d deb8d3c 48a7d73 deb8d3c 48a7d73 deb8d3c 48a7d73 4cfc55d de2d28e 4cfc55d deb8d3c 48a7d73 deb8d3c 4cfc55d 5e97050 | 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | """render-html MCP Apps server — renders arbitrary HTML inline in AI chat.
Architecture (matching PDF Viewer pattern):
1. Resource at ui://render-html/viewer.html (text/html;profile=mcp-app)
2. Tools have _meta.ui.resourceUri pointing to viewer
3. Tools return structuredContent + outputSchema
4. Viewer implements MCP Apps handshake: ui/initialize -> tool-result
"""
from mcp.server.fastmcp import FastMCP
from mcp.server.transport_security import TransportSecuritySettings
from mcp.types import CallToolResult, TextContent
from pydantic import BaseModel
from typing import Annotated
mcp = FastMCP("render-html", transport_security=TransportSecuritySettings(
enable_dns_rebinding_protection=False,
))
# -- The viewer HTML app ---------------------------------------------------
# Implements the MCP Apps lifecycle:
# 1. View -> Host: ui/initialize (JSON-RPC request)
# 2. Host -> View: response with host context
# 3. View -> Host: ui/notifications/initialized
# 4. Host -> View: ui/notifications/tool-result with structuredContent
VIEWER_HTML = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Render HTML Viewer</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: auto; background: transparent; }
#content { width: 100%; min-height: 100%; }
#loading { display: flex; align-items: center; justify-content: center;
height: 100vh; font-family: system-ui; color: #666; font-size: 14px; }
</style>
</head>
<body>
<div id="loading">Loading…</div>
<div id="content" style="display:none;"></div>
<script>
(function() {
var contentEl = document.getElementById('content');
var loadingEl = document.getElementById('loading');
var msgId = 1;
// -- Render HTML content from structuredContent --
function renderContent(data) {
if (!data) return;
var html = data.html || data.svg || '';
if (!html) return;
loadingEl.style.display = 'none';
contentEl.style.display = 'block';
contentEl.innerHTML = html;
// Re-execute script tags
var scripts = contentEl.querySelectorAll('script');
for (var i = 0; i < scripts.length; i++) {
var old = scripts[i];
var s = document.createElement('script');
if (old.src) s.src = old.src;
else s.textContent = old.textContent;
old.parentNode.replaceChild(s, old);
}
}
// -- Send JSON-RPC message to host --
function sendToHost(msg) {
if (window.parent && window.parent !== window) {
window.parent.postMessage(msg, '*');
}
}
// -- MCP Apps initialization handshake --
// Step 1: Send ui/initialize request to host
var initId = msgId++;
sendToHost({
jsonrpc: '2.0',
id: initId,
method: 'ui/initialize',
params: {
appInfo: { name: 'render-html', version: '1.0.0' },
capabilities: {}
}
});
// -- Listen for messages from host --
window.addEventListener('message', function(event) {
if (event.source !== window.parent) return;
var msg = event.data;
if (!msg || msg.jsonrpc !== '2.0') return;
// Step 2: Host responds to ui/initialize with host context
if (msg.id === initId && msg.result) {
// Step 3: Send ui/notifications/initialized
sendToHost({
jsonrpc: '2.0',
method: 'ui/notifications/initialized',
params: {}
});
}
// Step 4: Receive tool-result notification
if (msg.method === 'ui/notifications/tool-result') {
renderContent(msg.params && msg.params.structuredContent);
}
// Also handle tool-input (streaming partial args)
if (msg.method === 'ui/notifications/tool-input') {
// Could show preview; for render-html we wait for final result
}
}, { passive: true });
})();
</script>
</body>
</html>
"""
VIEWER_URI = "ui://render-html/viewer.html"
# Both forms of the meta key (matching PDF Viewer)
TOOL_META = {
"ui": {"resourceUri": VIEWER_URI},
"ui/resourceUri": VIEWER_URI,
}
# -- Register the viewer as an MCP resource --------------------------------
@mcp.resource(
VIEWER_URI,
name="HTML Viewer",
description="Viewer app that renders arbitrary HTML content",
mime_type="text/html;profile=mcp-app",
)
def viewer_resource() -> str:
return VIEWER_HTML
# -- Output schemas --------------------------------------------------------
class HtmlOutput(BaseModel):
html: str
title: str
class SvgOutput(BaseModel):
svg: str
title: str
# -- Tools -----------------------------------------------------------------
@mcp.tool(
name="render_html",
annotations={
"title": "Render HTML",
"readOnlyHint": True,
"openWorldHint": True,
},
meta=TOOL_META,
)
def render_html(html: str, title: str = "Rendered Content") -> Annotated[CallToolResult, HtmlOutput]:
"""Render arbitrary HTML content inline in the conversation.
Use this to display images, SVG diagrams, charts, styled content,
or any HTML/CSS/JS directly in the chat. The HTML is rendered in
a sandboxed iframe.
Args:
html: Complete HTML content to render. Can include style, script,
inline styles, SVG, images via img tags, etc.
title: Optional title for the rendered content.
"""
return CallToolResult(
content=[TextContent(type="text", text=f"Rendered: {title}")],
structuredContent={"html": html, "title": title},
_meta=TOOL_META,
)
@mcp.tool(
name="render_image",
annotations={
"title": "Render Image",
"readOnlyHint": True,
"openWorldHint": True,
},
meta=TOOL_META,
)
def render_image(url: str, alt: str = "", width: int = 0) -> Annotated[CallToolResult, HtmlOutput]:
"""Display an image from a URL inline in the conversation.
Args:
url: The image URL to display.
alt: Alt text for the image.
width: Optional width in pixels (0 = auto).
"""
style = 'max-width:100%;height:auto;'
if width > 0:
style = f'width:{width}px;max-width:100%;height:auto;'
img_html = (
f'<div style="display:flex;justify-content:center;align-items:center;'
f'min-height:200px;padding:8px;">'
f'<img src="{url}" alt="{alt}" style="{style}" />'
f'</div>'
)
return CallToolResult(
content=[TextContent(type="text", text=f"Image: {alt or url}")],
structuredContent={"html": img_html, "title": alt or "Image"},
_meta=TOOL_META,
)
@mcp.tool(
name="render_svg",
annotations={
"title": "Render SVG",
"readOnlyHint": True,
"openWorldHint": True,
},
meta=TOOL_META,
)
def render_svg(svg: str, title: str = "SVG Diagram") -> Annotated[CallToolResult, SvgOutput]:
"""Render an SVG diagram inline in the conversation.
Args:
svg: SVG markup string.
title: Optional title.
"""
wrapped = (
f'<div style="display:flex;justify-content:center;align-items:center;'
f'min-height:200px;padding:8px;background:#fff;">'
f'{svg}</div>'
)
return CallToolResult(
content=[TextContent(type="text", text=f"SVG: {title}")],
structuredContent={"svg": wrapped, "title": title},
_meta=TOOL_META,
)
if __name__ == "__main__":
import uvicorn
app = mcp.streamable_http_app()
uvicorn.run(app, host="0.0.0.0", port=7860)
|