Skip to content

Plugin surfaces

A Surface is metadata that tells a consumer which plugin-owned page to mount. Its event and sync snapshot include the plugin owner, Surface ID, generation, revision, and a source path. HTML, signals, and patches are served separately over the plugin’s HTTP routes; they are not copied into the Surface event or conversation history.

V1 supports plugin-local HTTP sources only. Absolute URLs and external HTTPS pages are not accepted.

The HTML Surface example is a normal production .pt-plugin package. Build the development CLI and plugin with the upstream recipe:

Terminal window
direnv exec . just build-surface-dev

This produces the development CLI and html_surface.pt-plugin. For manual installation, use the package workflow described in Plugins, then approve the package’s requested emit_message and serve_http capabilities. emit_message permits Surface publication; serve_http permits the plugin’s registered routes. Approval is tied to the package digest, so a changed package needs a new approval decision. serve_http is distinct from outbound network access.

The TypeScript package @polytoken/plugin-framework provides publishSurface:

import { publishSurface } from "@polytoken/plugin-framework";
const result = publishSurface({
surface_id: "results",
source: { type: "plugin_http", path: "/results" },
});
if (!result.ok) throw new Error(result.error);

After publication, the host emits a metadata event with its plugin name, generation, and revision:

{
"version": "v1",
"type": "surface_event",
"plugin_name": "surface-demo",
"surface_id": "results",
"generation": "0190abcd-1234-7abc-8def-0123456789ab",
"revision": "1",
"source": { "type": "plugin_http", "path": "/results" }
}

The path is local to the publishing plugin’s HTTP namespace. The host supplies the owner, generation, and revision. Schemes, authorities, query strings, fragments, traversal, encoded separators, and paths into another plugin’s namespace are rejected.

The HTML example registers its routes during plugin initialization before the host publishes that plugin generation. Its show({ html }) tool stores agent-authored HTML in the current plugin instance and then publishes /surface; a later GET returns that exact HTML. The tool requires a non-empty string no larger than 128 KiB when UTF-8 encoded. A later valid show replaces the page. Invalid or oversized input leaves the current page unchanged, and a failed metadata publication restores the previously served page. Before the first successful show, GET returns 404 with an unavailable message rather than a demo page. The HTML is transient plugin-instance state: this package declares persist_across_clear: false, so clear and reload start without authored HTML. The host closes route registration after publication. Package replacement or reload publishes a new generation.

Plugin routes are under /plugins/{plugin_name}/http/{path} in a session daemon. Through an authenticated Outpost session, the corresponding URL is /api/v1/sessions/{session_id}/proxy/plugins/{plugin_name}/http/{path}. The Outpost forwards the request to that session’s daemon; clients should use the active session and plugin source they were given.

Each running plugin generation has a document at /plugins/{plugin_name}/openapi.json. It describes that plugin’s paths, operations, input schemas, and responses. Through an Outpost it is available at /api/v1/sessions/{session_id}/proxy/plugins/{plugin_name}/openapi.json; the Outpost document sets its server URL to the same-session proxy HTTP base. The regular Outpost management document remains separate.

A route registration declares its method, plugin-local path, callback, operation metadata, request schemas, and response status/media schemas. The host checks registered request data and finite response bodies against those declarations. Unknown paths or methods and invalid inputs or outputs are rejected. Request data does not include daemon credentials or Outpost internals.

HTML, Datastar actions, and streamed updates

Section titled “HTML, Datastar actions, and streamed updates”

The shipped html-surface package provides:

  • GET /surface, which returns the page as text/html;
  • POST /action, which returns JSON, HTML, or a bodyless 204 depending on the submitted message;
  • POST /action/stream, which emits ordered Datastar patch-elements and patch-signals events;
  • GET /updates, which opens a persistent event stream; and
  • POST /updates/publish, which writes later Datastar events to an open stream.

The HTML uses relative Datastar URLs such as @post('/backend/action') and @get('/backend/updates'). In a Surface consumer these actions go through the consumer’s bound HTTP path, not an arbitrary destination selected by the plugin page.

Plugins use the framework helpers openHttpStream, commitHttpStream, patchElements, patchSignals, and closeHttpStream. The host owns SSE framing and checks each event against the variants declared for that route. The stream handle is opaque; a later callback from the same plugin generation can write to the open stream after the opening callback has returned. Events are delivered in write order. Closing the client connection or replacing the plugin generation ends the old response; a handle from a closed stream cannot be used to resume it.

The OpenAPI response lists text/event-stream without a schema that could imply validation of the stream frames. The plugin’s x-polytoken-sse-events extension is an array of decoded event variants, each with its event name and JSON schema. Ordinary OpenAPI tooling does not validate each SSE frame.

Surface events and sync snapshots are metadata, not durable history. Consumers should replace older metadata when a newer generation is published and stop using a source after its plugin generation is gone.

V1 accepts plugin-local HTTP source paths only. It does not support external pages, redirects to external destinations, or plugin-selected Outpost/session identities. MCP Apps are a separate ui:// resource and JSON-RPC protocol; they are not plugin Surfaces.