Widget utilities for NitroStack - Build interactive UI widgets for MCP tools
npm install @nitrostack/widgetsLightweight widget utilities for building interactive UI components in NitroStack MCP servers.
``bash`
npm install @nitrostack/widgets
- OpenAI ChatGPT Compatible - Works with OpenAI's widget format
- React Hooks - Easy-to-use hooks for widget state management
- Theme Support - Automatic dark/light mode detection
- Type Safe - Full TypeScript support
`tsx
import { useWidgetSDK, useTheme, WidgetLayout } from '@nitrostack/widgets';
export default function MyWidget() {
const sdk = useWidgetSDK();
const { isDark } = useTheme();
// Access tool output data
const data = sdk.getOutput();
return (
Hello from Widget!
{JSON.stringify(data, null, 2)}
);
}
`
Main SDK hook providing access to all widget functionality:
`tsx
const sdk = useWidgetSDK();
// Get tool output data
const data = sdk.getOutput();
// Call another tool
const result = await sdk.callTool('tool_name', { param: 'value' });
// Update widget state
sdk.setState({ count: 1 });
const state = sdk.getState();
// Theme
const isDark = sdk.isDarkMode();
// Display mode
const mode = sdk.getDisplayMode(); // 'split' | 'fullscreen'
`
Access current theme:
`tsx`
const { isDark, theme } = useTheme();
Persistent widget state:
`tsx`
const [state, setState] = useWidgetState({ count: 0 });
Get maximum available height for the widget:
`tsx`
const maxHeight = useMaxHeight(); // e.g., '600px'
Get current display mode:
`tsx`
const displayMode = useDisplayMode(); // 'split' | 'fullscreen'
Wrapper component that handles theme and polyfills:
`tsx
import { WidgetLayout } from '@nitrostack/widgets';
export default function Page() {
return (
);
}
`
`tsx
import {
prefersReducedMotion,
isPrimarilyTouchDevice,
isHoverAvailable,
prefersDarkColorScheme,
} from '@nitrostack/widgets';
// Check user preferences
if (prefersReducedMotion()) {
// Disable animations
}
``
Apache-2.0