Browse the blog

Add interactive widgets to MCP tool results

  • frontmcp
  • mcp
  • ui
  • widgets

Maintained guide. Example version: FrontMCP 1.10.10. Technical checks recorded: 2026-10-22. How checks work.

A model can summarize a result, but some tasks are easier with controls beside the answer. A support agent might inspect an order and resend its tracking link. FrontMCP lets a tool return structured data for the model and a widget for a compatible host to display.

A successful tool call and a working widget are separate checks. This example verifies the generated HTML and escaping. The site's Widget tab simulates a host; it does not certify a particular hosted client's current behavior.

For the maintained reference, see Tool UI and the lessons in Tools with a UI.

Build a template

Use a typed TemplateContext and ctx.helpers.html. Plain string results containing markup are escaped by default since 1.9.2.

Open
import { Tool, ToolContext, z, type TemplateContext } from "@frontmcp/sdk";

type Input = { city: string };
type Output = { city: string; tempC: number };
@Tool({
  name: "get_weather",
  description: "Show fixture weather for a city",
  inputSchema: { city: z.string() },
  ui: {
    template: (ctx: TemplateContext<Input, Output>) => ctx.helpers.html`
      <article><h2>${ctx.output.city}</h2><p>${ctx.output.tempC}°C</p></article>`,
    csp: { connectDomains: [], resourceDomains: [] },
  },
})
export class GetWeather extends ToolContext {
  async execute({ city }: Input): Promise<Output> {
    return { city, tempC: 21 };
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

The temperature is fixture data. A real API needs network configuration, credentials and failure handling.

Configure origins deliberately

connectDomains permits required network origins; resourceDomains covers required resource origins and also affects requests. They are not raw script-src or img-src directive maps. Inspect generated policy and the UI reference.

Trusted template markup can contain scripts and handlers. CSP does not replace safe template construction or authorization of actions. Keep untrusted values escaped; do not disable escaping to make an old string template display.

Choose a serving mode

ModeBehavior to account for
inline or autoThe call can carry rendered HTML in metadata
staticThe host reads a resource rendered at startup with empty data
hybridCompatible detected clients receive a component reference; component code is not embedded in that reference

See serving modes for exact behavior and caveats. Host detection is not proof that a named client rendered the page.

Connect actions and check a real host

Follow Your First Widget for bridge actions and Hosts and platforms for host-specific behavior. Check tool result, resource read, rendering, button action, authorization and error state separately. Record the client version and date in the compatibility procedure.

Prefer an ordinary result for short answers the model can already explain. Add a widget when a person needs to inspect or act on it.

Report a docs issue. Site usage and privacy.