mcpapps

package
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package mcpapps provides MCP Apps support for interactive UI components.

MCP Apps allow tool responses to include interactive UI elements that hosts can render for enhanced user experiences. This package implements the infrastructure for registering apps, injecting metadata into tool responses, and serving filesystem-based HTML/JS/CSS assets.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingName is returned when AppDefinition.Name is empty.
	ErrMissingName = errors.New("app name is required")

	// ErrMissingResourceURI is returned when AppDefinition.ResourceURI is empty.
	ErrMissingResourceURI = errors.New("resource URI is required")

	// ErrMissingToolNames is returned when AppDefinition.ToolNames is empty.
	ErrMissingToolNames = errors.New("at least one tool name is required")

	// ErrMissingEntryPoint is returned when AppDefinition.EntryPoint is empty.
	ErrMissingEntryPoint = errors.New("entry point is required")

	// ErrMissingAssetsPath is returned when AppDefinition.AssetsPath is empty.
	ErrMissingAssetsPath = errors.New("assets_path is required")

	// ErrAssetsPathNotAbsolute is returned when AssetsPath is not an absolute path.
	ErrAssetsPathNotAbsolute = errors.New("assets_path must be absolute")

	// ErrEntryPointNotFound is returned when the entry point file doesn't exist.
	ErrEntryPointNotFound = errors.New("entry point not found")

	// ErrPathTraversal is returned when a path traversal attack is detected.
	ErrPathTraversal = errors.New("path traversal detected")

	// ErrAppAlreadyRegistered is returned when trying to register an app
	// with a name that's already registered.
	ErrAppAlreadyRegistered = errors.New("app already registered")

	// ErrAppNotFound is returned when looking up an app that doesn't exist.
	ErrAppNotFound = errors.New("app not found")

	// ErrAssetNotFound is returned when a requested asset doesn't exist.
	ErrAssetNotFound = errors.New("asset not found")
)

Validation errors for AppDefinition.

Functions

func MIMEType

func MIMEType(filename string) string

MIMEType returns the MIME type for a file based on its extension.

func ToolMetadataMiddleware

func ToolMetadataMiddleware(reg *Registry) mcp.Middleware

ToolMetadataMiddleware creates MCP protocol-level middleware that injects _meta.ui metadata into tools/list responses for tools that have registered apps.

When a host requests tools/list, this middleware intercepts the response and adds UI metadata to tools that have associated MCP Apps. This allows MCP Apps-compatible hosts to render interactive UIs for tool results.

Types

type AppDefinition

type AppDefinition struct {
	// Name is the unique identifier for this app (e.g., "query-results").
	Name string

	// ResourceURI is the MCP resource URI that serves this app's UI
	// (e.g., "ui://query-results").
	ResourceURI string

	// ToolNames is the list of tool names this app enhances.
	// When any of these tools are called, the response will include
	// _meta.ui metadata pointing to this app.
	ToolNames []string

	// AssetsPath is the absolute filesystem path to the directory
	// containing the app's HTML/JS/CSS files.
	AssetsPath string

	// EntryPoint is the main HTML file within AssetsPath (e.g., "index.html").
	EntryPoint string

	// Config holds app-specific configuration that will be injected
	// into the HTML as JSON. This can be used to configure CDN URLs,
	// feature flags, or other runtime parameters.
	Config any

	// CSP defines Content Security Policy requirements for the app.
	// The host uses this to enforce appropriate CSP headers.
	CSP *CSPConfig
}

AppDefinition defines an MCP App that provides interactive UI for tool results.

func (*AppDefinition) Validate

func (a *AppDefinition) Validate() error

Validate checks that the AppDefinition has all required fields.

func (*AppDefinition) ValidateAssets added in v0.11.0

func (a *AppDefinition) ValidateAssets() error

ValidateAssets verifies that the assets path exists and contains the entry point. This should be called after Validate to ensure the filesystem is ready.

type CSPConfig

type CSPConfig struct {
	// ResourceDomains lists origins for static resources (scripts, images, styles, fonts).
	// Maps to CSP script-src, img-src, style-src, font-src, media-src directives.
	ResourceDomains []string `json:"resourceDomains,omitempty"`

	// ConnectDomains lists origins for network requests (fetch/XHR/WebSocket).
	// Maps to CSP connect-src directive.
	ConnectDomains []string `json:"connectDomains,omitempty"`

	// FrameDomains lists origins for nested iframes.
	// Maps to CSP frame-src directive.
	FrameDomains []string `json:"frameDomains,omitempty"`

	// Permissions lists browser capabilities the app needs.
	// Hosts MAY honor these by setting appropriate iframe allow attributes.
	Permissions *PermissionsConfig `json:"permissions,omitempty"`
}

CSPConfig defines Content Security Policy requirements for an MCP App.

type PermissionsConfig

type PermissionsConfig struct {
	// ClipboardWrite requests write access to the clipboard.
	ClipboardWrite *struct{} `json:"clipboardWrite,omitempty"`
}

PermissionsConfig defines browser permissions requested by an MCP App.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages registered MCP Apps.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new app registry.

func (*Registry) Apps

func (r *Registry) Apps() []*AppDefinition

Apps returns all registered apps.

func (*Registry) Get

func (r *Registry) Get(name string) *AppDefinition

Get retrieves an app by name. Returns nil if not found.

func (*Registry) GetForTool

func (r *Registry) GetForTool(toolName string) *AppDefinition

GetForTool retrieves the app associated with a tool name. Returns nil if no app is registered for the tool.

func (*Registry) HasApps

func (r *Registry) HasApps() bool

HasApps returns true if any apps are registered.

func (*Registry) Register

func (r *Registry) Register(app *AppDefinition) error

Register adds an app to the registry. Returns an error if validation fails or if an app with the same name exists.

func (*Registry) RegisterResources

func (r *Registry) RegisterResources(server *mcp.Server)

RegisterResources registers all app resources with the MCP server. Each app's UI is served as an MCP resource at its configured ResourceURI.

func (*Registry) ToolNames

func (r *Registry) ToolNames() []string

ToolNames returns all tool names that have associated apps.

type ToolMeta

type ToolMeta struct {
	// UI contains MCP Apps UI metadata.
	UI *UIMetadata `json:"ui,omitempty"`
}

ToolMeta represents the _meta field for a tool definition.

type UIMetadata

type UIMetadata struct {
	// ResourceURI points to the MCP resource that serves the app UI.
	ResourceURI string `json:"resourceUri"`
}

UIMetadata represents the _meta.ui field injected into tool definitions. MCP Apps-compatible hosts use this to associate interactive UIs with tools.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL