@UIApp Decorator
@UIApp vs @GPTApp: Use
@UIApp for general MCP apps. For ChatGPT Apps with OpenAI-specific metadata (widget controls, CSP, invocation messages), use @GPTApp.@UIApp decorator links an MCP tool to a React UI component. When applied to a tool method, it automatically:
- Adds
_meta.ui.resourceUrito the tool definition - Registers a resource that renders the component to HTML
- Enables hosts to display your UI when the tool is called
Installation
Usage
API Reference
@UIApp(options)
Decorator function that links a tool to a UI component.Configuration options
UIAppOptions
React component or path to component file (relative to service file)
- Use component reference for direct SSR rendering
- Use path string (e.g.,
'./WeatherCard') for CLI build - avoids importing browser code in server
Custom resource URI. Auto-generated if not provided.Default format:
ui://{className}/{methodName}Example: ui://weather/getWeatherHTML document title for the rendered UI
Additional CSS styles to inject into the HTML document
Examples
Basic Usage
Component File (TaskList.tsx)
Custom URI
Path-based Component (for CLI builds)
Custom Styling
Helper Functions
getUIAppMetadata()
Get UIApp metadata from a method.Returns
UIApp options if decorator was applied, undefined otherwise
getUIAppUri()
Get the resource URI from a method.Returns
Resource URI if decorator was applied, undefined otherwise
How It Works
1. Metadata Storage
The decorator stores metadata usingreflect-metadata:
2. Tool Metadata Enhancement
The decorator adds UI metadata to the tool definition:3. Resource Registration
A resource is automatically registered that:- Renders the React component to HTML
- Injects the tool result as context
- Serves the HTML to the host
4. Host Integration
When the tool is called, the host:- Sees the
ui.resourceUriin tool metadata - Fetches the resource
- Displays the rendered HTML to the user
URI Format
By default, URIs follow the pattern:WeatherService.getWeather→ui://weather/getWeatherTaskService.listTasks→ui://task/listTasksUserService.getProfile→ui://user/getProfile
- Is converted to lowercase
- Has the
Servicesuffix removed (if present)
Integration with @leanmcp/core
The@UIApp decorator is designed to work seamlessly with @Tool from @leanmcp/core:
@Tool decorator will automatically detect the UI metadata added by @UIApp and include it in the tool definition.
Best Practices
1. Component Separation
Keep UI components in separate files:2. Type Safety
Use TypeScript generics for type-safe tool results:3. Error Handling
Handle errors gracefully in your UI components:4. Path-based Components for CLI
When using the LeanMCP CLI, use path strings to avoid importing React in Node.js:Troubleshooting
UI not displaying
- Check that
reflect-metadatais imported at app entry point - Verify the resource URI is correct in tool metadata
- Ensure the component is exported properly
TypeScript errors
Make sureexperimentalDecorators and emitDecoratorMetadata are enabled:
Component not receiving data
UseuseToolResult() hook in your component to access the tool result:
See Also
- React Components - Available UI components
- Hooks - React hooks for MCP
- @Tool Decorator - Core tool decorator