# Working with the model

## Following the model while the app is open

A tool result reaches the app once, when the app opens. To let the model keep writing into an app that is already open,
have it call a normal tool that stores the data on your server, and let the app follow that state:

```ts
// The model calls `write_note`; the app reads `read_note` every second
protected readonly note = this.mcp.pollServerTool('read_note', { intervalMs: 1000, guard: isNote });
```

Mark the reading tool as app-only on the server (`_meta: { ui: { visibility: ['app'] } }`) so the model does not see it.
`pollServerTool` must be called in an injection context (a field initializer or the constructor), or given `{ injector }`.

To talk back to the model:

```ts
await this.mcp.updateModelContext('The user is on exercise 3'); // silent, read on the model's next turn
const sent = await this.mcp.sendMessage('I am done'); // a chat message; false if the host refuses
```

Hosts may ask the user to confirm `sendMessage` or refuse it (common on mobile). Prefer `updateModelContext`
and let the user type in the chat.

## Checking your own data

Protocol messages are checked by the library. For the data your own server sends, pass a guard: any
`(value: unknown) => value is T` works (hand-written, generated by typia, a valibot `is`...). The type is inferred from it.

```ts
interface Note {
  id: number;
  text: string;
}
const isNote = (v: unknown): v is Note =>
  isObject(v) && typeof v['id'] === 'number' && typeof v['text'] === 'string';

note = this.mcp.pollServerTool('read_note', { guard: isNote }); // Signal<Note | undefined>
opened = this.mcp.structuredContentAs(isNote); // Signal<Note | undefined>
```

With `pollServerTool`, an invalid value is dropped and logged, and the signal keeps the last valid one.
`structuredContentAs` is `undefined` while the result of the tool call does not pass the guard.

