-
Notifications
You must be signed in to change notification settings - Fork 13
MCP Apps: interactive Vega-Lite chart widget #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nicosuave
wants to merge
12
commits into
main
Choose a base branch
from
mcp-apps-vega
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dc3ba0e
MCP Apps interactive Vega-Lite charts with CSP-safe rendering
nicosuave 520c751
Chart widget: render inline with expand bar for fullscreen
nicosuave 606d450
Polish chart widget: expand icon, fullscreen sizing, padding
nicosuave f894c5e
Dispose old chart observer and view before re-rendering
nicosuave 9d4786a
Guard against stale renders and clean up chart on non-chart content
nicosuave 4d8fe76
Invalidate in-flight embeds on tool input, clear stale spec on error
nicosuave aa70f56
Clear cached spec on new tool input to prevent stale chart resurrection
nicosuave 8e2ce34
Add interactive metrics explorer as MCP App
nicosuave 6e28033
Add start_date/end_date params to explore_metrics for time filtering
nicosuave 85b7278
Explorer: lazy loading, per-item queries, and metric series fixes
nicosuave cb5075c
Merge remote-tracking branch 'origin/main' into bs-133
nicosuave d554c77
Resolve explorer dimension leaderboards to configured metric ref
nicosuave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| node_modules/ | ||
| bun.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { App, applyDocumentTheme, type McpUiHostContext } from "@modelcontextprotocol/ext-apps"; | ||
| import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; | ||
| import embed from "vega-embed"; | ||
| import { expressionInterpreter } from "vega-interpreter"; | ||
|
|
||
| const container = document.getElementById("chart")!; | ||
| let currentDisplayMode: "inline" | "fullscreen" = "inline"; | ||
| let lastSpec: Record<string, unknown> | null = null; | ||
| let activeObserver: ResizeObserver | null = null; | ||
| let activeView: { finalize: () => void } | null = null; | ||
| let renderGeneration = 0; | ||
|
|
||
| function cleanupChart() { | ||
| if (activeObserver) { activeObserver.disconnect(); activeObserver = null; } | ||
| if (activeView) { activeView.finalize(); activeView = null; } | ||
| } | ||
|
|
||
| function renderChart(vegaSpec: Record<string, unknown>) { | ||
| cleanupChart(); | ||
| const generation = ++renderGeneration; | ||
|
|
||
| container.innerHTML = ""; | ||
| const isFullscreen = currentDisplayMode === "fullscreen"; | ||
| document.documentElement.classList.toggle("fullscreen", isFullscreen); | ||
|
|
||
| const spec = { ...vegaSpec }; | ||
| spec.width = "container"; | ||
| spec.height = isFullscreen ? "container" : 500; | ||
| spec.background = "transparent"; | ||
|
|
||
| const prefersDark = window.matchMedia?.("(prefers-color-scheme: dark)").matches; | ||
|
|
||
| embed(container, spec as any, { | ||
| actions: false, | ||
| theme: prefersDark ? "dark" : undefined, | ||
| ast: true, | ||
| expr: expressionInterpreter, | ||
| }) | ||
| .then((result) => { | ||
| if (generation !== renderGeneration) { result.finalize(); return; } | ||
|
|
||
| activeView = result; | ||
| const ro = new ResizeObserver(() => result.view.resize().run()); | ||
| ro.observe(container); | ||
| activeObserver = ro; | ||
|
nicosuave marked this conversation as resolved.
|
||
|
|
||
| if (!isFullscreen) { | ||
| addExpandButton(); | ||
| } | ||
|
|
||
| requestAnimationFrame(() => { | ||
| if (generation !== renderGeneration) return; | ||
| if (isFullscreen) { | ||
| app.sendSizeChanged({ height: window.innerHeight - 150 }); | ||
| } else { | ||
| const h = Math.max(505, document.documentElement.scrollHeight + 5); | ||
| app.sendSizeChanged({ height: h }); | ||
| } | ||
| }); | ||
| }) | ||
| .catch((err) => { | ||
| if (generation !== renderGeneration) return; | ||
| container.innerHTML = `<div class="error">Chart render error: ${err.message}</div>`; | ||
| }); | ||
| } | ||
|
|
||
| function addExpandButton() { | ||
| const btn = document.createElement("div"); | ||
| btn.className = "expand-btn"; | ||
| btn.title = "Expand to fullscreen"; | ||
| btn.textContent = "Expand ↗"; | ||
| btn.addEventListener("click", goFullscreen); | ||
| container.appendChild(btn); | ||
| } | ||
|
|
||
| async function goFullscreen() { | ||
| try { | ||
| const result = await app.requestDisplayMode({ mode: "fullscreen" }); | ||
| currentDisplayMode = result.mode as "inline" | "fullscreen"; | ||
| if (lastSpec) renderChart(lastSpec); | ||
| } catch { | ||
| // host doesn't support fullscreen | ||
| } | ||
| } | ||
|
|
||
| function extractVegaSpec(result: CallToolResult): Record<string, unknown> | null { | ||
| const sc = result.structuredContent as Record<string, unknown> | undefined; | ||
| if (sc?.vega_spec) return sc.vega_spec as Record<string, unknown>; | ||
| if (result.content) { | ||
| for (const item of result.content) { | ||
| if (item.type === "text") { | ||
| try { | ||
| const data = JSON.parse((item as { text: string }).text); | ||
| if (data.vega_spec) return data.vega_spec; | ||
| } catch {} | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| const app = new App( | ||
| { name: "sidemantic-chart", version: "1.0.0" }, | ||
| {}, | ||
| { autoResize: false }, | ||
| ); | ||
|
|
||
| app.ontoolresult = (result: CallToolResult) => { | ||
| const spec = extractVegaSpec(result); | ||
| if (spec) { | ||
| lastSpec = spec; | ||
| renderChart(spec); | ||
| } else { | ||
| cleanupChart(); | ||
| lastSpec = null; | ||
| container.innerHTML = '<div class="error">No chart data in tool result</div>'; | ||
| } | ||
|
nicosuave marked this conversation as resolved.
nicosuave marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| app.ontoolinput = () => { | ||
| cleanupChart(); | ||
| lastSpec = null; | ||
| ++renderGeneration; | ||
| container.innerHTML = '<div class="loading">Running query...</div>'; | ||
|
nicosuave marked this conversation as resolved.
nicosuave marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| app.onhostcontextchanged = (ctx: McpUiHostContext) => { | ||
| if (ctx.theme) applyDocumentTheme(ctx.theme); | ||
| if (ctx.displayMode === "inline" || ctx.displayMode === "fullscreen") { | ||
| currentDisplayMode = ctx.displayMode; | ||
| if (lastSpec) renderChart(lastSpec); | ||
| } | ||
| }; | ||
|
|
||
| app.connect().then(() => { | ||
| const ctx = app.getHostContext(); | ||
| if (ctx?.theme) applyDocumentTheme(ctx.theme); | ||
| const loading = container.querySelector(".loading"); | ||
| if (loading) loading.textContent = "Waiting for chart data..."; | ||
| app.sendSizeChanged({ height: 500 }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="color-scheme" content="light dark"> | ||
| <style> | ||
| html, body { margin: 0; padding: 0; background: transparent; | ||
| font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; } | ||
| #chart { width: 100%; min-height: 500px; position: relative; } | ||
| html.fullscreen, html.fullscreen body { height: 100%; } | ||
| html.fullscreen { padding: 16px 24px 0; box-sizing: border-box; } | ||
| html.fullscreen #chart { height: calc(100vh - 150px - 16px); min-height: auto; } | ||
| .vega-embed { background: transparent !important; } | ||
| #chart .vega-embed, #chart .vega-embed > div, | ||
| #chart .vega-embed canvas, #chart .vega-embed svg { overflow: hidden !important; } | ||
| .error { padding: 2rem; text-align: center; color: #dc2626; } | ||
| .loading { padding: 2rem; text-align: center; color: #999; } | ||
| .expand-btn { | ||
| position: absolute; top: 6px; right: 8px; z-index: 10; | ||
| cursor: pointer; color: #666; font-size: 13px; | ||
| line-height: 1; padding: 4px 8px; border-radius: 4px; | ||
| transition: color 0.2s, background 0.2s; | ||
| } | ||
| .expand-btn:hover { color: #333; background: rgba(0,0,0,0.06); } | ||
| @media (prefers-color-scheme: dark) { | ||
| .expand-btn { color: #999; } | ||
| .expand-btn:hover { color: #ddd; background: rgba(255,255,255,0.1); } | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div id="chart"> | ||
| <div class="loading">Loading...</div> | ||
| </div> | ||
| <script type="module" src="./chart-app.ts"></script> | ||
| </body> | ||
| </html> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.