Show live views in Claude Desktop (MCP Apps)
Give an MCP host such as Claude Desktop tools that show live tables, charts, pivots, an SQL editor and agent-drawn surfaces over your data. The model calls a tool; the host shows the Surfaces app page, which fetches only the rows on screen. Queries stay on your machine and run read-only.
How it works:
registerSurfacesApps(server, { dataSource, appHtml })adds the tools and theui://surfaces/app.htmlpage to any MCP server.readAppHtmlfrom@surfaces-ui/mcp/nodereads the page built into the package.- Tools for the model:
show_data_grid,show_chart,show_pivot_table,show_query_result,open_sql_editor,show_schema_explorer,render_surfaceandshow_dashboard, pluslist_tablesandget_component_docs, which show nothing. - A view is shown only after its SQL has run once on the server. A query that fails comes back to the model as a tool error it can correct, and
render_surfacereturns every catalog problem the same way. - The page fetches data through one more tool,
surfaces_rpc, which the model does not see. It is read-only and capped per request.
The server. buildServer is the part the test checks; main runs it over stdio with the sample data:
server.ts:
import { fileURLToPath } from "node:url";
import { McpServer } from "@modelcontextprotocol/server";
import { StdioServerTransport } from "@modelcontextprotocol/server/stdio";
import type { DataSource } from "@surfaces-ui/data";
import { createSurfacesServer, openSources, serverOptions } from "@surfaces-ui/dev-server";
import { registerSurfacesApps } from "@surfaces-ui/mcp";
import { readAppHtml } from "@surfaces-ui/mcp/node";
/**
* An MCP server with the Surfaces tools and app page over any DataSource.
* `appHtml` defaults to the page built into @surfaces-ui/mcp.
*/
export function buildServer(dataSource: DataSource, appHtml: string | (() => string) = readAppHtml): McpServer {
const server = new McpServer({ name: "surfaces", version: "0.0.0" });
registerSurfacesApps(server, { dataSource, appHtml });
return server;
}
/** Runs over stdio with the six sample industry models. stdout carries the protocol, so logs go to stderr. */
export async function main() {
const config = { seeds: true };
const dir = process.cwd();
const backend = await createSurfacesServer(await serverOptions(config, await openSources(config, dir), dir));
await buildServer(backend.dataSource).connect(new StdioServerTransport());
process.stderr.write("Surfaces MCP server ready.\n");
const stop = () => void backend.close().then(() => process.exit(0));
process.on("SIGINT", stop);
process.on("SIGTERM", stop);
}
if (process.argv[1] === fileURLToPath(import.meta.url)) await main();You may not need to write this file. The surfaces-mcp binary in @surfaces-ui/mcp is the same server, reading surfaces.config.ts in the folder it starts in (or serving the sample models when there is none). To add it to a repository's .mcp.json for Claude Code:
npx surfaces-mcp install --dir path/to/your/repoFor Claude Desktop, add an entry to its claude_desktop_config.json (Settings, then Developer, then Edit config) and restart it. This is an example written by hand, not generated or tested; change the paths to yours:
{
"mcpServers": {
"surfaces": {
"command": "npx",
"args": ["-y", "@surfaces-ui/mcp", "--config", "/absolute/path/to/surfaces.config.ts"]
}
}
}Both the install command and this entry run @surfaces-ui/mcp through npx, so the package must be installable from your registry; until it is, point command at node and args at the built cli.js. Claude Desktop does not start the server in your project folder, so give the config file as an absolute path. The server writes nothing a person should read to stdout, which carries the protocol; messages go to stderr, which Claude Desktop keeps in its MCP logs.
The test connects an MCP client to buildServer over an in-memory transport, with memoryDataSource and a stub page in place of the built one. It lists the tools, reads the tables and shows a grid:
mcp.test.ts:
import { Client } from "@modelcontextprotocol/client";
import { InMemoryTransport } from "@modelcontextprotocol/server";
import { memoryDataSource } from "@surfaces-ui/data";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { buildServer } from "./server.js";
const orders = Array.from({ length: 12 }, (_, i) => ({ order_id: i + 1, region: ["EU", "NA", "APAC"][i % 3]!, amount: 10 + i }));
let client: Client;
beforeAll(async () => {
// The same registration server.ts runs, over the in-memory data source and a stub page.
const server = buildServer(memoryDataSource({ sourceId: "shop", tables: { orders } }), "<!doctype html><title>stub</title>");
const [a, b] = InMemoryTransport.createLinkedPair();
client = new Client({ name: "host", version: "0" });
await Promise.all([server.connect(a), client.connect(b)]);
});
afterAll(() => client?.close());
describe("Surfaces as an MCP App", () => {
it("offers the view tools to the model", async () => {
const names = (await client.listTools()).tools.map((t) => t.name);
expect(names).toEqual(expect.arrayContaining(["show_data_grid", "show_chart", "show_pivot_table", "show_query_result", "open_sql_editor", "render_surface", "show_dashboard", "list_tables", "get_component_docs"]));
});
it("describes the tables, and shows a grid only after its query has run", async () => {
const tables = await client.callTool({ name: "list_tables", arguments: {} });
expect((tables.content as { text: string }[])[0]!.text).toContain("orders(order_id");
const grid = await client.callTool({ name: "show_data_grid", arguments: { sql: "SELECT * FROM orders", title: "Orders" } });
expect(grid.structuredContent).toEqual({ view: "grid", sourceId: "shop", sql: "SELECT * FROM orders", title: "Orders" });
expect((grid.content as { text: string }[])[0]!.text).toBe("Showing a table of 12 rows.");
const bad = await client.callTool({ name: "show_data_grid", arguments: { sql: "SELECT * FROM missing" } });
expect(bad.isError).toBe(true);
});
});Not verified inside Claude Desktop or any other desktop host. The tools and the page have run only through the playground's in-page host and a stdio smoke test, so how a given host sizes, sandboxes and refreshes the page is unknown.
Source and test: apps/recipes/src/mcp-claude-desktop