diff --git a/docs/patterns.md b/docs/patterns.md index dc29b8c..ab4a9f1 100644 --- a/docs/patterns.md +++ b/docs/patterns.md @@ -20,12 +20,13 @@ Set {@link types!McpUiToolMeta.visibility Tool.\_meta.ui.visibility} to `["app"] {@includeCode ./patterns.tsx#authenticatedCalls} -## [TODO] Giving errors back to model +## Giving errors back to model -- Before app runs: validate inputs in tool call -- After it runs: use `updateModelContext` +**Server-side**: Tool handler validates inputs and returns `{ isError: true, content: [...] }`. The model receives this error through the normal tool call response. -{@includeCode ./patterns.tsx#errorsToModel} +**Client-side**: If a runtime error occurs (e.g., API failure, permission denied, resource unavailable), use {@link app!App.updateModelContext updateModelContext} to inform the model: + +{@includeCode ../src/app.examples.ts#App_updateModelContext_reportError} ## Matching host styling (CSS variables, theme, and fonts) diff --git a/docs/patterns.tsx b/docs/patterns.tsx index c481ad3..a6fd259 100644 --- a/docs/patterns.tsx +++ b/docs/patterns.tsx @@ -27,17 +27,6 @@ function authenticatedCalls(app: App) { //#endregion authenticatedCalls } -/** - * Example: Giving errors back to model - */ -function errorsToModel(app: App) { - //#region errorsToModel - // Before app runs: validate inputs in tool call - // After it runs: updateModelContext - // TODO: Complete implementation - //#endregion errorsToModel -} - /** * Example: Unified host styling (theme, CSS variables, fonts) */ @@ -132,7 +121,6 @@ function migrateFromOpenai() { // Suppress unused variable warnings void authenticatedCalls; -void errorsToModel; void hostStylingVanillaJs; void hostStylingReact; void persistData; diff --git a/src/app.examples.ts b/src/app.examples.ts index bf6f9dc..c360837 100644 --- a/src/app.examples.ts +++ b/src/app.examples.ts @@ -364,6 +364,28 @@ async function App_updateModelContext_structuredContent(app: App) { //#endregion App_updateModelContext_structuredContent } +/** + * Example: Report runtime error to model. + */ +async function App_updateModelContext_reportError(app: App) { + //#region App_updateModelContext_reportError + try { + const _stream = await navigator.mediaDevices.getUserMedia({ audio: true }); + // ... use _stream for transcription + } catch (err) { + // Inform the model that the app is in a degraded state + await app.updateModelContext({ + content: [ + { + type: "text", + text: "Error: transcription unavailable", + }, + ], + }); + } + //#endregion App_updateModelContext_reportError +} + /** * Example: Open documentation link. */ diff --git a/src/app.ts b/src/app.ts index 0628029..3408526 100644 --- a/src/app.ts +++ b/src/app.ts @@ -674,6 +674,9 @@ export class App extends Protocol { * @example Update with structured content * {@includeCode ./app.examples.ts#App_updateModelContext_structuredContent} * + * @example Report runtime error to model + * {@includeCode ./app.examples.ts#App_updateModelContext_reportError} + * * @returns Promise that resolves when the context update is acknowledged */ updateModelContext(