CSS string containing @font-face rules and/or @import statements
// Apply when host context changes
app.onhostcontextchanged = (ctx) => {
if (ctx.styles?.css?.fonts) {
applyHostFonts(ctx.styles.css.fonts);
}
};
// Apply initial fonts after connecting
app.connect().then(() => {
const ctx = app.getHostContext();
if (ctx?.styles?.css?.fonts) {
applyHostFonts(ctx.styles.css.fonts);
}
});
// Example of what a host might provide:
const fontCss = `
@font-face {
font-family: "Anthropic Sans";
src: url("https://assets.anthropic.com/.../Regular.otf") format("opentype");
font-weight: 400;
}
`;
applyHostFonts(fontCss);
// Example of what a host might provide:
const fontCss = `
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
`;
applyHostFonts(fontCss);
McpUiHostContext for the full host context structure
Apply host font CSS to the document.
This function takes the
css.fontsstring fromMcpUiHostContext.stylesand injects it as a<style>tag. The CSS can contain@font-facerules for self-hosted fonts,@importstatements for Google Fonts or other font services, or a combination of both.The styles are only injected once. Subsequent calls are no-ops and will not create duplicate style tags.