@mastra/mcp-apps-components 0.1.0-alpha.2 → 0.1.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -5
- package/dist/apps/observability-widget/tool.d.ts +25 -1
- package/dist/apps/observability-widget/tool.d.ts.map +1 -1
- package/dist/host.cjs +51 -13
- package/dist/host.cjs.map +1 -1
- package/dist/host.d.ts +72 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +50 -14
- package/dist/host.js.map +1 -1
- package/dist/index.cjs +4 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/{tool-B4yU6CY0.cjs → tool-B9qpVU16.cjs} +9 -1
- package/dist/tool-B9qpVU16.cjs.map +1 -0
- package/dist/{tool-C601G1w9.js → tool-HqUC6iks.js} +4 -2
- package/dist/tool-HqUC6iks.js.map +1 -0
- package/package.json +5 -5
- package/dist/tool-B4yU6CY0.cjs.map +0 -1
- package/dist/tool-C601G1w9.js.map +0 -1
package/README.md
CHANGED
|
@@ -58,7 +58,7 @@ export const greetingServer = new MCPServer({
|
|
|
58
58
|
`observabilityDashboardApp` draws a Studio dashboard (rows of KPIs, time series, tables and scores widgets) with the same components Studio uses. Each widget loads and fails on its own.
|
|
59
59
|
|
|
60
60
|
1. The model calls the tool that opens the app. That tool must declare an `outputSchema` so its result reaches the app as structured content: `{ dashboard, datePreset? }`, where `dashboard` passes `dashboardDefinitionSchema` from `@mastra/core/storage/dashboards` and `datePreset` is one of `24h`, `3d`, `7d`, `14d` or `30d`. Without `datePreset`, the app uses the dashboard's `defaults.datePreset` when it is one of those, else `24h`. The window ends when the app opens. The dashboard's top-level `defaults.filters` chips filter every widget.
|
|
61
|
-
2. The app reads its data through the app-only `queryObservability` tool (exported as `QUERY_OBSERVABILITY_TOOL`). It calls it once per read with `{ action, args }`, where `action` is one of `OBSERVABILITY_READ_ACTIONS`, the `MastraClient` observability reads the app sends (`aggregateTraces`, `getMetricBreakdown`, `getScoreTimeSeries`, `listScores`, …),
|
|
61
|
+
2. The app reads its data through the app-only `queryObservability` tool (exported as `QUERY_OBSERVABILITY_TOOL`). It calls it once per read with `{ action, args, projectId? }`, where `action` is one of `OBSERVABILITY_READ_ACTIONS`, the `MastraClient` observability reads the app sends (`aggregateTraces`, `getMetricBreakdown`, `getScoreTimeSeries`, `listScores`, …), `args` are that method's arguments, with dates as ISO strings, and `projectId` is the project the host passed, if any (see [Drawing over the host page's selection](#drawing-over-the-host-pages-selection)). The tool returns `{ data }`, the method's result; if it throws, that widget shows as failed.
|
|
62
62
|
|
|
63
63
|
Use `observabilityDashboardInputSchema` as the opening tool's input and `toObservabilityDashboard` as its `execute`:
|
|
64
64
|
|
|
@@ -85,6 +85,9 @@ import { z } from 'zod';
|
|
|
85
85
|
|
|
86
86
|
const uri = 'ui://mastra/observability-widget';
|
|
87
87
|
const client = new MastraClient({ baseUrl: process.env.MASTRA_URL! });
|
|
88
|
+
// Yours to write: checks the caller can access `projectId`, throws if not, and returns a client
|
|
89
|
+
// whose reads stay in that project.
|
|
90
|
+
declare function projectClient(projectId: string, context: unknown): Promise<MastraClient>;
|
|
88
91
|
|
|
89
92
|
export const observabilityServer = new MCPServer({
|
|
90
93
|
id: 'observability',
|
|
@@ -105,11 +108,16 @@ export const observabilityServer = new MCPServer({
|
|
|
105
108
|
[QUERY_OBSERVABILITY_TOOL]: createTool({
|
|
106
109
|
id: QUERY_OBSERVABILITY_TOOL,
|
|
107
110
|
description: 'Runs one observability read',
|
|
108
|
-
inputSchema: z.object({
|
|
111
|
+
inputSchema: z.object({
|
|
112
|
+
action: z.enum(OBSERVABILITY_READ_ACTIONS),
|
|
113
|
+
args: z.record(z.string(), z.unknown()),
|
|
114
|
+
projectId: z.string().optional(),
|
|
115
|
+
}),
|
|
109
116
|
outputSchema: z.object({ data: z.unknown() }),
|
|
110
117
|
mcp: { _meta: { ui: { resourceUri: uri, visibility: ['app'] } } },
|
|
111
|
-
execute: async ({ action, args }) => {
|
|
112
|
-
const
|
|
118
|
+
execute: async ({ action, args, projectId }, context) => {
|
|
119
|
+
const reader = projectId ? await projectClient(projectId, context) : client;
|
|
120
|
+
const read = reader[action].bind(reader) as (args: unknown) => Promise<unknown>;
|
|
113
121
|
return { data: await read(args) };
|
|
114
122
|
},
|
|
115
123
|
}),
|
|
@@ -117,6 +125,8 @@ export const observabilityServer = new MCPServer({
|
|
|
117
125
|
});
|
|
118
126
|
```
|
|
119
127
|
|
|
128
|
+
`projectId` comes from the app's iframe, so the server can't trust it: check that the caller can access that project before reading. A read that throws shows its widget as failed.
|
|
129
|
+
|
|
120
130
|
The input names each widget, read and column type by a key instead of a `kind`, `source` or `type` field, so a validation error points at the part that's wrong. Each number holds the read it comes from, so nothing points at a query by id:
|
|
121
131
|
|
|
122
132
|
```ts
|
|
@@ -158,7 +168,34 @@ const host = useMemo(
|
|
|
158
168
|
<McpAppHostProvider value={host}>{chat}</McpAppHostProvider>;
|
|
159
169
|
```
|
|
160
170
|
|
|
161
|
-
A pin carries the widget, the date preset it was drawn over and, when
|
|
171
|
+
A pin carries the widget, the date preset it was drawn over (with its `customRange` when `custom`) and, when it had any, the filters it was drawn under (the host's, else the dashboard's `defaults.filters`). The button only shows when the host sends pinned ids and accepts structured model context. A click shows right away and holds until the host sends new ids.
|
|
172
|
+
|
|
173
|
+
#### Drawing over the host page's selection
|
|
174
|
+
|
|
175
|
+
A host page with its own project, date range and filters, such as Studio's Metrics page, passes them down with `observabilityWidgetHost`. Pinning isn't needed for this:
|
|
176
|
+
|
|
177
|
+
```tsx
|
|
178
|
+
import { McpAppHostProvider } from '@mastra/playground-ui/domains/mcps';
|
|
179
|
+
import { observabilityWidgetHost } from '@mastra/mcp-apps-components/host';
|
|
180
|
+
|
|
181
|
+
const host = useMemo(
|
|
182
|
+
() =>
|
|
183
|
+
observabilityWidgetHost({
|
|
184
|
+
resourceUri: 'ui://mastra/observability-widget',
|
|
185
|
+
projectId,
|
|
186
|
+
datePreset, // '24h' | '3d' | '7d' | '14d' | '30d' | 'custom'
|
|
187
|
+
customRange, // { from?, to? } as Dates or ISO strings, drawn when datePreset is 'custom'
|
|
188
|
+
filters, // FilterNode[], as a dashboard's defaults.filters
|
|
189
|
+
}),
|
|
190
|
+
[projectId, datePreset, customRange, filters],
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
<McpAppHostProvider value={host}>{chat}</McpAppHostProvider>;
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
`projectId` is sent with every `queryObservability` call, so your server can scope the reads to that project. It also sets the widgets' scope, as on the Metrics page: with a `projectId`, they show at project scope; without one, at org scope. Widgets, views and columns tagged for the other scope are hidden. The host's `datePreset` and `filters` replace the tool result's and the dashboard's defaults. The Metrics page's `datePreset`, `customRange` and `filterTokens` can be passed as they are. With `datePreset: 'custom'`, the widgets read over `customRange`, like the Metrics page: without `from`, the window starts 24 hours before now, and without `to`, it ends now. A value the app can't read (filters that fail the dashboard schema, an unknown preset, a range that ends before it starts) shows an error instead of the dashboard, so the widgets never draw data the host page didn't ask for. When any value changes, the app reads again; a preset window then ends at that moment. Each value is optional: leave one out to fall back to the dashboard's.
|
|
197
|
+
|
|
198
|
+
To let the user pin widgets too, add `pins: { pinnedIds, onPin, onUnpin }`. `observabilityWidgetPins` is the same helper with only pins.
|
|
162
199
|
|
|
163
200
|
## Documentation
|
|
164
201
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { FilterNode } from '@mastra/core/storage/dashboards';
|
|
1
2
|
/** The app-only tool the observability widget calls, on the server that opened it, for every read. */
|
|
2
3
|
export declare const QUERY_OBSERVABILITY_TOOL = "queryObservability";
|
|
3
4
|
/**
|
|
@@ -16,6 +17,27 @@ export type ObservabilityDatePreset = (typeof OBSERVABILITY_DATE_PRESETS)[number
|
|
|
16
17
|
export declare const OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY = "mastra.ai/observability-widget-pinned";
|
|
17
18
|
/** `structuredContent` key of the model context update the app sends when the user (un)pins. */
|
|
18
19
|
export declare const OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY = "mastra.ai/observability-widget-pin";
|
|
20
|
+
/** Host context key holding what the host's page draws the widgets over (`ObservabilityWidgetView`). */
|
|
21
|
+
export declare const OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY = "mastra.ai/observability-widget-view";
|
|
22
|
+
/** A custom window, as ISO date strings. A missing `from` is 24h before `to`; a missing `to` is now. */
|
|
23
|
+
export type ObservabilityWidgetDateRange = {
|
|
24
|
+
from?: string;
|
|
25
|
+
to?: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* What the host's page draws the widgets over, e.g. the Metrics page's date range and filter bar.
|
|
29
|
+
* Each field the host sets wins over the opening tool's result and the dashboard's `defaults`.
|
|
30
|
+
*/
|
|
31
|
+
export type ObservabilityWidgetView = {
|
|
32
|
+
/** One of the presets, or `custom` to draw over `customRange`. */
|
|
33
|
+
datePreset?: ObservabilityDatePreset | 'custom';
|
|
34
|
+
/** The window drawn when `datePreset` is `custom`, as the Metrics page's custom range. */
|
|
35
|
+
customRange?: ObservabilityWidgetDateRange;
|
|
36
|
+
/** Replaces the dashboard's `defaults.filters`; only top-level chips filter, as on the Metrics page. */
|
|
37
|
+
filters?: FilterNode[];
|
|
38
|
+
/** Sent with every `queryObservability` call, so the server reads that project's data. */
|
|
39
|
+
projectId?: string;
|
|
40
|
+
};
|
|
19
41
|
/** What the app sends under `OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY`: the state the user asked for. */
|
|
20
42
|
export type ObservabilityWidgetPin = {
|
|
21
43
|
pinned: boolean;
|
|
@@ -25,7 +47,9 @@ export type ObservabilityWidgetPin = {
|
|
|
25
47
|
[key: string]: unknown;
|
|
26
48
|
};
|
|
27
49
|
datePreset: string;
|
|
28
|
-
/** The
|
|
50
|
+
/** The window the widget was drawn over, when `datePreset` is `custom`. */
|
|
51
|
+
customRange?: ObservabilityWidgetDateRange;
|
|
52
|
+
/** The filters the widget was drawn under (the host's, else the dashboard's `defaults.filters`), when any. */
|
|
29
53
|
filters?: unknown[];
|
|
30
54
|
};
|
|
31
55
|
//# sourceMappingURL=tool.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../../src/apps/observability-widget/tool.ts"],"names":[],"mappings":"AAAA,sGAAsG;AACtG,eAAO,MAAM,wBAAwB,uBAAuB,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,0BAA0B,YACrC,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACtB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EACrB,YAAY,CACJ,CAAC;AAEX,MAAM,MAAM,uBAAuB,GAAG,CAAC,OAAO,0BAA0B,CAAC,CAAC,MAAM,CAAC,CAAC;AAElF,mGAAmG;AACnG,eAAO,MAAM,0BAA0B,YAAI,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,CAAU,CAAC;AAErF,MAAM,MAAM,uBAAuB,GAAG,CAAC,OAAO,0BAA0B,CAAC,CAAC,MAAM,CAAC,CAAC;AAElF;;;GAGG;AACH,eAAO,MAAM,uCAAuC,0CAA0C,CAAC;AAE/F,gGAAgG;AAChG,eAAO,MAAM,oCAAoC,uCAAuC,CAAC;AAEzF,qGAAqG;AACrG,MAAM,MAAM,sBAAsB,GAAG;IACnC,MAAM,EAAE,OAAO,CAAC;IAChB,oEAAoE;IACpE,MAAM,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;IAC/C,UAAU,EAAE,MAAM,CAAC;IACnB,
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../../src/apps/observability-widget/tool.ts"],"names":[],"mapp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,GAAG;IACnC,MAAM,EAAE,OAAO,CAAC;IAChB,oEAAoE;IACpE,MAAM,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;IAC/C,UAAU,EAAE,MAAM,CAAC;IACnB,2EAA2E;IAC3E,WAAW,CAAC,EAAE,4BAA4B,CAAC;IAC3C,8GAA8G;IAC9G,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC;CACrB,CAAC"}
|
package/dist/host.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_tool = require("./tool-
|
|
2
|
+
const require_tool = require("./tool-B9qpVU16.cjs");
|
|
3
3
|
// src/host.ts
|
|
4
4
|
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
5
|
/** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */
|
|
@@ -8,6 +8,11 @@ function readPin(structuredContent) {
|
|
|
8
8
|
if (!isRecord(pin) || typeof pin.pinned !== "boolean" || typeof pin.datePreset !== "string") return void 0;
|
|
9
9
|
if (!isRecord(pin.widget) || typeof pin.widget.id !== "string") return void 0;
|
|
10
10
|
if (pin.filters !== void 0 && !Array.isArray(pin.filters)) return void 0;
|
|
11
|
+
const range = isRecord(pin.customRange) ? pin.customRange : void 0;
|
|
12
|
+
const customRange = range && {
|
|
13
|
+
...typeof range.from === "string" ? { from: range.from } : {},
|
|
14
|
+
...typeof range.to === "string" ? { to: range.to } : {}
|
|
15
|
+
};
|
|
11
16
|
return {
|
|
12
17
|
pinned: pin.pinned,
|
|
13
18
|
widget: {
|
|
@@ -15,40 +20,73 @@ function readPin(structuredContent) {
|
|
|
15
20
|
id: pin.widget.id
|
|
16
21
|
},
|
|
17
22
|
datePreset: pin.datePreset,
|
|
23
|
+
...customRange ? { customRange } : {},
|
|
18
24
|
...pin.filters ? { filters: pin.filters } : {}
|
|
19
25
|
};
|
|
20
26
|
}
|
|
27
|
+
const toIso = (date) => date instanceof Date ? date.toISOString() : date;
|
|
21
28
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
29
|
+
* What the host passes down to the observability widget app: the host page's `datePreset` (with its
|
|
30
|
+
* `customRange` when `custom`) and `filters`, drawn instead of the dashboard's own, and the `projectId` sent with every
|
|
31
|
+
* `queryObservability` call. The app redraws when any of them changes.
|
|
24
32
|
*
|
|
25
33
|
* ```tsx
|
|
26
34
|
* const host = useMemo(
|
|
27
|
-
* () =>
|
|
28
|
-
*
|
|
35
|
+
* () =>
|
|
36
|
+
* observabilityWidgetHost({
|
|
37
|
+
* resourceUri: 'ui://mastra/observability-widget',
|
|
38
|
+
* datePreset,
|
|
39
|
+
* customRange,
|
|
40
|
+
* filters,
|
|
41
|
+
* projectId,
|
|
42
|
+
* }),
|
|
43
|
+
* [datePreset, customRange, filters, projectId],
|
|
29
44
|
* );
|
|
30
45
|
* <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>
|
|
31
46
|
* ```
|
|
32
47
|
*
|
|
33
|
-
* A click shows on the button right away and holds
|
|
34
|
-
* that turns a click down leaves it showing until then.
|
|
48
|
+
* Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds
|
|
49
|
+
* until the host's ids next change, so a host that turns a click down leaves it showing until then.
|
|
35
50
|
*/
|
|
36
|
-
function
|
|
51
|
+
function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters, projectId, pins }) {
|
|
52
|
+
const from = toIso(customRange?.from);
|
|
53
|
+
const to = toIso(customRange?.to);
|
|
54
|
+
const view = {
|
|
55
|
+
...datePreset ? { datePreset } : {},
|
|
56
|
+
...datePreset === "custom" && customRange ? { customRange: {
|
|
57
|
+
...from ? { from } : {},
|
|
58
|
+
...to ? { to } : {}
|
|
59
|
+
} } : {},
|
|
60
|
+
...filters ? { filters } : {},
|
|
61
|
+
...projectId ? { projectId } : {}
|
|
62
|
+
};
|
|
37
63
|
return {
|
|
38
|
-
hostContext: (app) => app.resourceUri === resourceUri ? {
|
|
39
|
-
|
|
64
|
+
hostContext: (app) => app.resourceUri === resourceUri ? {
|
|
65
|
+
[require_tool.OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY]: view,
|
|
66
|
+
...pins ? { [require_tool.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {}
|
|
67
|
+
} : void 0,
|
|
68
|
+
onModelContext: pins ? (app, params) => {
|
|
40
69
|
if (app.resourceUri !== resourceUri) return;
|
|
41
70
|
const pin = readPin(params.structuredContent);
|
|
42
71
|
if (!pin) return;
|
|
43
72
|
if (pin.pinned) {
|
|
44
73
|
const { pinned: _pinned, ...pinned } = pin;
|
|
45
|
-
onPin(pinned, app);
|
|
46
|
-
} else onUnpin(pin.widget.id, app);
|
|
47
|
-
}
|
|
74
|
+
pins.onPin(pinned, app);
|
|
75
|
+
} else pins.onUnpin(pin.widget.id, app);
|
|
76
|
+
} : void 0
|
|
48
77
|
};
|
|
49
78
|
}
|
|
79
|
+
/** Lets the user pin observability widgets from the chat: `observabilityWidgetHost` with only `pins`. */
|
|
80
|
+
function observabilityWidgetPins({ resourceUri, ...pins }) {
|
|
81
|
+
return observabilityWidgetHost({
|
|
82
|
+
resourceUri,
|
|
83
|
+
pins
|
|
84
|
+
});
|
|
85
|
+
}
|
|
50
86
|
exports.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY;
|
|
51
87
|
exports.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY;
|
|
88
|
+
exports.OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY;
|
|
89
|
+
exports.observabilityWidgetHost = observabilityWidgetHost;
|
|
52
90
|
exports.observabilityWidgetPins = observabilityWidgetPins;
|
|
53
91
|
|
|
54
92
|
//# sourceMappingURL=host.cjs.map
|
package/dist/host.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host.cjs","names":["OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY","OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY"],"sources":["../src/host.ts"],"sourcesContent":["import {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nimport type {
|
|
1
|
+
{"version":3,"file":"host.cjs","names":["OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY","OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY","OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY"],"sources":["../src/host.ts"],"sourcesContent":["import {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nimport type {\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type {\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\n/** The MCP App instance a host callback is about: one tool call's app. */\nexport type McpAppRef = { toolName: string; toolCallId: string; resourceUri: string };\n\n/**\n * What a chat host passes to `McpAppHostProvider` from `@mastra/playground-ui/domains/mcps`. Typed\n * here by shape, so this package doesn't depend on the host's UI package.\n */\nexport type McpAppHostValue = {\n hostContext?: (app: McpAppRef) => Record<string, unknown> | undefined;\n onModelContext?: (\n app: McpAppRef,\n params: { content?: unknown[]; structuredContent?: Record<string, unknown> },\n ) => void;\n};\n\nexport type ObservabilityWidgetPins = {\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /**\n * The user pinned `pin.widget` over `pin.datePreset` (and `pin.customRange` when `custom`), under\n * `pin.filters` when it has them.\n */\n onPin: (pin: Omit<ObservabilityWidgetPin, 'pinned'>, app: McpAppRef) => void;\n /** The user unpinned the widget with this id. */\n onUnpin: (widgetId: string, app: McpAppRef) => void;\n};\n\nexport type ObservabilityWidgetHostOptions = Omit<ObservabilityWidgetView, 'customRange'> & {\n /** The window drawn when `datePreset` is `custom`: the Metrics page's `customRange` as is, or ISO strings. */\n customRange?: { from?: Date | string; to?: Date | string };\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** Lets the user pin widgets. Left out, the widgets show no pin button. */\n pins?: ObservabilityWidgetPins;\n};\n\nexport type ObservabilityWidgetPinsOptions = ObservabilityWidgetPins & { resourceUri: string };\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */\nfunction readPin(structuredContent: Record<string, unknown> | undefined): ObservabilityWidgetPin | undefined {\n const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];\n if (!isRecord(pin) || typeof pin.pinned !== 'boolean' || typeof pin.datePreset !== 'string') return undefined;\n if (!isRecord(pin.widget) || typeof pin.widget.id !== 'string') return undefined;\n if (pin.filters !== undefined && !Array.isArray(pin.filters)) return undefined;\n const range = isRecord(pin.customRange) ? pin.customRange : undefined;\n const customRange: ObservabilityWidgetDateRange | undefined = range && {\n ...(typeof range.from === 'string' ? { from: range.from } : {}),\n ...(typeof range.to === 'string' ? { to: range.to } : {}),\n };\n return {\n pinned: pin.pinned,\n widget: { ...pin.widget, id: pin.widget.id },\n datePreset: pin.datePreset,\n ...(customRange ? { customRange } : {}),\n ...(pin.filters ? { filters: pin.filters } : {}),\n };\n}\n\nconst toIso = (date: Date | string | undefined) => (date instanceof Date ? date.toISOString() : date);\n\n/**\n * What the host passes down to the observability widget app: the host page's `datePreset` (with its\n * `customRange` when `custom`) and `filters`, drawn instead of the dashboard's own, and the `projectId` sent with every\n * `queryObservability` call. The app redraws when any of them changes.\n *\n * ```tsx\n * const host = useMemo(\n * () =>\n * observabilityWidgetHost({\n * resourceUri: 'ui://mastra/observability-widget',\n * datePreset,\n * customRange,\n * filters,\n * projectId,\n * }),\n * [datePreset, customRange, filters, projectId],\n * );\n * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>\n * ```\n *\n * Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds\n * until the host's ids next change, so a host that turns a click down leaves it showing until then.\n */\nexport function observabilityWidgetHost({\n resourceUri,\n datePreset,\n customRange,\n filters,\n projectId,\n pins,\n}: ObservabilityWidgetHostOptions): McpAppHostValue {\n const from = toIso(customRange?.from);\n const to = toIso(customRange?.to);\n // Always sent, even empty, so a field the host stops setting is cleared in the app too.\n const view: ObservabilityWidgetView = {\n ...(datePreset ? { datePreset } : {}),\n ...(datePreset === 'custom' && customRange\n ? { customRange: { ...(from ? { from } : {}), ...(to ? { to } : {}) } }\n : {}),\n ...(filters ? { filters } : {}),\n ...(projectId ? { projectId } : {}),\n };\n return {\n hostContext: app =>\n app.resourceUri === resourceUri\n ? {\n [OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY]: view,\n ...(pins ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {}),\n }\n : undefined,\n onModelContext: pins\n ? (app, params) => {\n if (app.resourceUri !== resourceUri) return;\n const pin = readPin(params.structuredContent);\n if (!pin) return;\n if (pin.pinned) {\n const { pinned: _pinned, ...pinned } = pin;\n pins.onPin(pinned, app);\n } else pins.onUnpin(pin.widget.id, app);\n }\n : undefined,\n };\n}\n\n/** Lets the user pin observability widgets from the chat: `observabilityWidgetHost` with only `pins`. */\nexport function observabilityWidgetPins({ resourceUri, ...pins }: ObservabilityWidgetPinsOptions): McpAppHostValue {\n return observabilityWidgetHost({ resourceUri, pins });\n}\n"],"mappings":";;;AA4DA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;AAGrE,SAAS,QAAQ,mBAA4F;CAC3G,MAAM,MAAM,oBAAoBA,aAAAA;CAChC,IAAI,CAAC,SAAS,GAAG,KAAK,OAAO,IAAI,WAAW,aAAa,OAAO,IAAI,eAAe,UAAU,OAAO,KAAA;CACpG,IAAI,CAAC,SAAS,IAAI,MAAM,KAAK,OAAO,IAAI,OAAO,OAAO,UAAU,OAAO,KAAA;CACvE,IAAI,IAAI,YAAY,KAAA,KAAa,CAAC,MAAM,QAAQ,IAAI,OAAO,GAAG,OAAO,KAAA;CACrE,MAAM,QAAQ,SAAS,IAAI,WAAW,IAAI,IAAI,cAAc,KAAA;CAC5D,MAAM,cAAwD,SAAS;EACrE,GAAI,OAAO,MAAM,SAAS,WAAW,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;EAC7D,GAAI,OAAO,MAAM,OAAO,WAAW,EAAE,IAAI,MAAM,GAAG,IAAI,CAAC;CACzD;CACA,OAAO;EACL,QAAQ,IAAI;EACZ,QAAQ;GAAE,GAAG,IAAI;GAAQ,IAAI,IAAI,OAAO;EAAG;EAC3C,YAAY,IAAI;EAChB,GAAI,cAAc,EAAE,YAAY,IAAI,CAAC;EACrC,GAAI,IAAI,UAAU,EAAE,SAAS,IAAI,QAAQ,IAAI,CAAC;CAChD;AACF;AAEA,MAAM,SAAS,SAAqC,gBAAgB,OAAO,KAAK,YAAY,IAAI;;;;;;;;;;;;;;;;;;;;;;;;AAyBhG,SAAgB,wBAAwB,EACtC,aACA,YACA,aACA,SACA,WACA,QACkD;CAClD,MAAM,OAAO,MAAM,aAAa,IAAI;CACpC,MAAM,KAAK,MAAM,aAAa,EAAE;CAEhC,MAAM,OAAgC;EACpC,GAAI,aAAa,EAAE,WAAW,IAAI,CAAC;EACnC,GAAI,eAAe,YAAY,cAC3B,EAAE,aAAa;GAAE,GAAI,OAAO,EAAE,KAAK,IAAI,CAAC;GAAI,GAAI,KAAK,EAAE,GAAG,IAAI,CAAC;EAAG,EAAE,IACpE,CAAC;EACL,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;EAC7B,GAAI,YAAY,EAAE,UAAU,IAAI,CAAC;CACnC;CACA,OAAO;EACL,cAAa,QACX,IAAI,gBAAgB,cAChB;IACGC,aAAAA,wCAAwC;GACzC,GAAI,OAAO,GAAGC,aAAAA,0CAA0C,KAAK,UAAU,IAAI,CAAC;EAC9E,IACA,KAAA;EACN,gBAAgB,QACX,KAAK,WAAW;GACf,IAAI,IAAI,gBAAgB,aAAa;GACrC,MAAM,MAAM,QAAQ,OAAO,iBAAiB;GAC5C,IAAI,CAAC,KAAK;GACV,IAAI,IAAI,QAAQ;IACd,MAAM,EAAE,QAAQ,SAAS,GAAG,WAAW;IACvC,KAAK,MAAM,QAAQ,GAAG;GACxB,OAAO,KAAK,QAAQ,IAAI,OAAO,IAAI,GAAG;EACxC,IACA,KAAA;CACN;AACF;;AAGA,SAAgB,wBAAwB,EAAE,aAAa,GAAG,QAAyD;CACjH,OAAO,wBAAwB;EAAE;EAAa;CAAK,CAAC;AACtD"}
|
package/dist/host.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { ObservabilityWidgetPin, ObservabilityWidgetView } from './apps/observability-widget/tool.js';
|
|
2
|
+
export { OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY, OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY, } from './apps/observability-widget/tool.js';
|
|
3
|
+
export type { ObservabilityWidgetDateRange, ObservabilityWidgetPin, ObservabilityWidgetView, } from './apps/observability-widget/tool.js';
|
|
4
|
+
/** The MCP App instance a host callback is about: one tool call's app. */
|
|
5
|
+
export type McpAppRef = {
|
|
6
|
+
toolName: string;
|
|
7
|
+
toolCallId: string;
|
|
8
|
+
resourceUri: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* What a chat host passes to `McpAppHostProvider` from `@mastra/playground-ui/domains/mcps`. Typed
|
|
12
|
+
* here by shape, so this package doesn't depend on the host's UI package.
|
|
13
|
+
*/
|
|
14
|
+
export type McpAppHostValue = {
|
|
15
|
+
hostContext?: (app: McpAppRef) => Record<string, unknown> | undefined;
|
|
16
|
+
onModelContext?: (app: McpAppRef, params: {
|
|
17
|
+
content?: unknown[];
|
|
18
|
+
structuredContent?: Record<string, unknown>;
|
|
19
|
+
}) => void;
|
|
20
|
+
};
|
|
21
|
+
export type ObservabilityWidgetPins = {
|
|
22
|
+
/** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */
|
|
23
|
+
pinnedIds: readonly string[];
|
|
24
|
+
/**
|
|
25
|
+
* The user pinned `pin.widget` over `pin.datePreset` (and `pin.customRange` when `custom`), under
|
|
26
|
+
* `pin.filters` when it has them.
|
|
27
|
+
*/
|
|
28
|
+
onPin: (pin: Omit<ObservabilityWidgetPin, 'pinned'>, app: McpAppRef) => void;
|
|
29
|
+
/** The user unpinned the widget with this id. */
|
|
30
|
+
onUnpin: (widgetId: string, app: McpAppRef) => void;
|
|
31
|
+
};
|
|
32
|
+
export type ObservabilityWidgetHostOptions = Omit<ObservabilityWidgetView, 'customRange'> & {
|
|
33
|
+
/** The window drawn when `datePreset` is `custom`: the Metrics page's `customRange` as is, or ISO strings. */
|
|
34
|
+
customRange?: {
|
|
35
|
+
from?: Date | string;
|
|
36
|
+
to?: Date | string;
|
|
37
|
+
};
|
|
38
|
+
/** The `ui://` URI the observability widget app is registered under; other apps are left alone. */
|
|
39
|
+
resourceUri: string;
|
|
40
|
+
/** Lets the user pin widgets. Left out, the widgets show no pin button. */
|
|
41
|
+
pins?: ObservabilityWidgetPins;
|
|
42
|
+
};
|
|
43
|
+
export type ObservabilityWidgetPinsOptions = ObservabilityWidgetPins & {
|
|
44
|
+
resourceUri: string;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* What the host passes down to the observability widget app: the host page's `datePreset` (with its
|
|
48
|
+
* `customRange` when `custom`) and `filters`, drawn instead of the dashboard's own, and the `projectId` sent with every
|
|
49
|
+
* `queryObservability` call. The app redraws when any of them changes.
|
|
50
|
+
*
|
|
51
|
+
* ```tsx
|
|
52
|
+
* const host = useMemo(
|
|
53
|
+
* () =>
|
|
54
|
+
* observabilityWidgetHost({
|
|
55
|
+
* resourceUri: 'ui://mastra/observability-widget',
|
|
56
|
+
* datePreset,
|
|
57
|
+
* customRange,
|
|
58
|
+
* filters,
|
|
59
|
+
* projectId,
|
|
60
|
+
* }),
|
|
61
|
+
* [datePreset, customRange, filters, projectId],
|
|
62
|
+
* );
|
|
63
|
+
* <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds
|
|
67
|
+
* until the host's ids next change, so a host that turns a click down leaves it showing until then.
|
|
68
|
+
*/
|
|
69
|
+
export declare function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters, projectId, pins, }: ObservabilityWidgetHostOptions): McpAppHostValue;
|
|
70
|
+
/** Lets the user pin observability widgets from the chat: `observabilityWidgetHost` with only `pins`. */
|
|
71
|
+
export declare function observabilityWidgetPins({ resourceUri, ...pins }: ObservabilityWidgetPinsOptions): McpAppHostValue;
|
|
72
|
+
//# sourceMappingURL=host.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAEV,sBAAsB,EACtB,uBAAuB,EACxB,MAAM,kCAAkC,CAAC;AAE1C,O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AC3D,mGAAmG;IACnG,WAAW,EAAE,MAAM,CAAC;IACpB,2EAA2E;IAC3E,IAAI,CAAC,EAAE,uBAAuB,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG,uBAAuB,GAAG;IAAE,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC;AA2B/F;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,uBAAuB,CAAC,EACtC,WAAW,EACX,UAAU,EACV,WAAW,EACX,OAAO,EACP,SAAS,EACT,IAAI,GACL,EAAE,8BAA8B,GAAG,eAAe,CAgClD;AAED,yGAAyG;AACzG,wBAAgB,uBAAuB,CAAC,EAAE,WAAW,EAAE,GAAG,IAAI,EAAE,EAAE,8BAA8B,GAAG,eAAe,CAEjH"}
|
package/dist/host.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, r as OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY } from "./tool-
|
|
1
|
+
import { a as OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY, i as OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, r as OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY } from "./tool-HqUC6iks.js";
|
|
2
2
|
// src/host.ts
|
|
3
3
|
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4
4
|
/** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */
|
|
@@ -7,6 +7,11 @@ function readPin(structuredContent) {
|
|
|
7
7
|
if (!isRecord(pin) || typeof pin.pinned !== "boolean" || typeof pin.datePreset !== "string") return void 0;
|
|
8
8
|
if (!isRecord(pin.widget) || typeof pin.widget.id !== "string") return void 0;
|
|
9
9
|
if (pin.filters !== void 0 && !Array.isArray(pin.filters)) return void 0;
|
|
10
|
+
const range = isRecord(pin.customRange) ? pin.customRange : void 0;
|
|
11
|
+
const customRange = range && {
|
|
12
|
+
...typeof range.from === "string" ? { from: range.from } : {},
|
|
13
|
+
...typeof range.to === "string" ? { to: range.to } : {}
|
|
14
|
+
};
|
|
10
15
|
return {
|
|
11
16
|
pinned: pin.pinned,
|
|
12
17
|
widget: {
|
|
@@ -14,38 +19,69 @@ function readPin(structuredContent) {
|
|
|
14
19
|
id: pin.widget.id
|
|
15
20
|
},
|
|
16
21
|
datePreset: pin.datePreset,
|
|
22
|
+
...customRange ? { customRange } : {},
|
|
17
23
|
...pin.filters ? { filters: pin.filters } : {}
|
|
18
24
|
};
|
|
19
25
|
}
|
|
26
|
+
const toIso = (date) => date instanceof Date ? date.toISOString() : date;
|
|
20
27
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
28
|
+
* What the host passes down to the observability widget app: the host page's `datePreset` (with its
|
|
29
|
+
* `customRange` when `custom`) and `filters`, drawn instead of the dashboard's own, and the `projectId` sent with every
|
|
30
|
+
* `queryObservability` call. The app redraws when any of them changes.
|
|
23
31
|
*
|
|
24
32
|
* ```tsx
|
|
25
33
|
* const host = useMemo(
|
|
26
|
-
* () =>
|
|
27
|
-
*
|
|
34
|
+
* () =>
|
|
35
|
+
* observabilityWidgetHost({
|
|
36
|
+
* resourceUri: 'ui://mastra/observability-widget',
|
|
37
|
+
* datePreset,
|
|
38
|
+
* customRange,
|
|
39
|
+
* filters,
|
|
40
|
+
* projectId,
|
|
41
|
+
* }),
|
|
42
|
+
* [datePreset, customRange, filters, projectId],
|
|
28
43
|
* );
|
|
29
44
|
* <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>
|
|
30
45
|
* ```
|
|
31
46
|
*
|
|
32
|
-
* A click shows on the button right away and holds
|
|
33
|
-
* that turns a click down leaves it showing until then.
|
|
47
|
+
* Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds
|
|
48
|
+
* until the host's ids next change, so a host that turns a click down leaves it showing until then.
|
|
34
49
|
*/
|
|
35
|
-
function
|
|
50
|
+
function observabilityWidgetHost({ resourceUri, datePreset, customRange, filters, projectId, pins }) {
|
|
51
|
+
const from = toIso(customRange?.from);
|
|
52
|
+
const to = toIso(customRange?.to);
|
|
53
|
+
const view = {
|
|
54
|
+
...datePreset ? { datePreset } : {},
|
|
55
|
+
...datePreset === "custom" && customRange ? { customRange: {
|
|
56
|
+
...from ? { from } : {},
|
|
57
|
+
...to ? { to } : {}
|
|
58
|
+
} } : {},
|
|
59
|
+
...filters ? { filters } : {},
|
|
60
|
+
...projectId ? { projectId } : {}
|
|
61
|
+
};
|
|
36
62
|
return {
|
|
37
|
-
hostContext: (app) => app.resourceUri === resourceUri ? {
|
|
38
|
-
|
|
63
|
+
hostContext: (app) => app.resourceUri === resourceUri ? {
|
|
64
|
+
[OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY]: view,
|
|
65
|
+
...pins ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {}
|
|
66
|
+
} : void 0,
|
|
67
|
+
onModelContext: pins ? (app, params) => {
|
|
39
68
|
if (app.resourceUri !== resourceUri) return;
|
|
40
69
|
const pin = readPin(params.structuredContent);
|
|
41
70
|
if (!pin) return;
|
|
42
71
|
if (pin.pinned) {
|
|
43
72
|
const { pinned: _pinned, ...pinned } = pin;
|
|
44
|
-
onPin(pinned, app);
|
|
45
|
-
} else onUnpin(pin.widget.id, app);
|
|
46
|
-
}
|
|
73
|
+
pins.onPin(pinned, app);
|
|
74
|
+
} else pins.onUnpin(pin.widget.id, app);
|
|
75
|
+
} : void 0
|
|
47
76
|
};
|
|
48
77
|
}
|
|
49
|
-
|
|
78
|
+
/** Lets the user pin observability widgets from the chat: `observabilityWidgetHost` with only `pins`. */
|
|
79
|
+
function observabilityWidgetPins({ resourceUri, ...pins }) {
|
|
80
|
+
return observabilityWidgetHost({
|
|
81
|
+
resourceUri,
|
|
82
|
+
pins
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
export { OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY, OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY, observabilityWidgetHost, observabilityWidgetPins };
|
|
50
86
|
|
|
51
87
|
//# sourceMappingURL=host.js.map
|
package/dist/host.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host.js","names":[],"sources":["../src/host.ts"],"sourcesContent":["import {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nimport type {
|
|
1
|
+
{"version":3,"file":"host.js","names":[],"sources":["../src/host.ts"],"sourcesContent":["import {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nimport type {\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type {\n ObservabilityWidgetDateRange,\n ObservabilityWidgetPin,\n ObservabilityWidgetView,\n} from './apps/observability-widget/tool';\n\n/** The MCP App instance a host callback is about: one tool call's app. */\nexport type McpAppRef = { toolName: string; toolCallId: string; resourceUri: string };\n\n/**\n * What a chat host passes to `McpAppHostProvider` from `@mastra/playground-ui/domains/mcps`. Typed\n * here by shape, so this package doesn't depend on the host's UI package.\n */\nexport type McpAppHostValue = {\n hostContext?: (app: McpAppRef) => Record<string, unknown> | undefined;\n onModelContext?: (\n app: McpAppRef,\n params: { content?: unknown[]; structuredContent?: Record<string, unknown> },\n ) => void;\n};\n\nexport type ObservabilityWidgetPins = {\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /**\n * The user pinned `pin.widget` over `pin.datePreset` (and `pin.customRange` when `custom`), under\n * `pin.filters` when it has them.\n */\n onPin: (pin: Omit<ObservabilityWidgetPin, 'pinned'>, app: McpAppRef) => void;\n /** The user unpinned the widget with this id. */\n onUnpin: (widgetId: string, app: McpAppRef) => void;\n};\n\nexport type ObservabilityWidgetHostOptions = Omit<ObservabilityWidgetView, 'customRange'> & {\n /** The window drawn when `datePreset` is `custom`: the Metrics page's `customRange` as is, or ISO strings. */\n customRange?: { from?: Date | string; to?: Date | string };\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** Lets the user pin widgets. Left out, the widgets show no pin button. */\n pins?: ObservabilityWidgetPins;\n};\n\nexport type ObservabilityWidgetPinsOptions = ObservabilityWidgetPins & { resourceUri: string };\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */\nfunction readPin(structuredContent: Record<string, unknown> | undefined): ObservabilityWidgetPin | undefined {\n const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];\n if (!isRecord(pin) || typeof pin.pinned !== 'boolean' || typeof pin.datePreset !== 'string') return undefined;\n if (!isRecord(pin.widget) || typeof pin.widget.id !== 'string') return undefined;\n if (pin.filters !== undefined && !Array.isArray(pin.filters)) return undefined;\n const range = isRecord(pin.customRange) ? pin.customRange : undefined;\n const customRange: ObservabilityWidgetDateRange | undefined = range && {\n ...(typeof range.from === 'string' ? { from: range.from } : {}),\n ...(typeof range.to === 'string' ? { to: range.to } : {}),\n };\n return {\n pinned: pin.pinned,\n widget: { ...pin.widget, id: pin.widget.id },\n datePreset: pin.datePreset,\n ...(customRange ? { customRange } : {}),\n ...(pin.filters ? { filters: pin.filters } : {}),\n };\n}\n\nconst toIso = (date: Date | string | undefined) => (date instanceof Date ? date.toISOString() : date);\n\n/**\n * What the host passes down to the observability widget app: the host page's `datePreset` (with its\n * `customRange` when `custom`) and `filters`, drawn instead of the dashboard's own, and the `projectId` sent with every\n * `queryObservability` call. The app redraws when any of them changes.\n *\n * ```tsx\n * const host = useMemo(\n * () =>\n * observabilityWidgetHost({\n * resourceUri: 'ui://mastra/observability-widget',\n * datePreset,\n * customRange,\n * filters,\n * projectId,\n * }),\n * [datePreset, customRange, filters, projectId],\n * );\n * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>\n * ```\n *\n * Pass `pins` to let the user pin widgets too. A click shows on the button right away and holds\n * until the host's ids next change, so a host that turns a click down leaves it showing until then.\n */\nexport function observabilityWidgetHost({\n resourceUri,\n datePreset,\n customRange,\n filters,\n projectId,\n pins,\n}: ObservabilityWidgetHostOptions): McpAppHostValue {\n const from = toIso(customRange?.from);\n const to = toIso(customRange?.to);\n // Always sent, even empty, so a field the host stops setting is cleared in the app too.\n const view: ObservabilityWidgetView = {\n ...(datePreset ? { datePreset } : {}),\n ...(datePreset === 'custom' && customRange\n ? { customRange: { ...(from ? { from } : {}), ...(to ? { to } : {}) } }\n : {}),\n ...(filters ? { filters } : {}),\n ...(projectId ? { projectId } : {}),\n };\n return {\n hostContext: app =>\n app.resourceUri === resourceUri\n ? {\n [OBSERVABILITY_WIDGET_VIEW_CONTEXT_KEY]: view,\n ...(pins ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pins.pinnedIds } : {}),\n }\n : undefined,\n onModelContext: pins\n ? (app, params) => {\n if (app.resourceUri !== resourceUri) return;\n const pin = readPin(params.structuredContent);\n if (!pin) return;\n if (pin.pinned) {\n const { pinned: _pinned, ...pinned } = pin;\n pins.onPin(pinned, app);\n } else pins.onUnpin(pin.widget.id, app);\n }\n : undefined,\n };\n}\n\n/** Lets the user pin observability widgets from the chat: `observabilityWidgetHost` with only `pins`. */\nexport function observabilityWidgetPins({ resourceUri, ...pins }: ObservabilityWidgetPinsOptions): McpAppHostValue {\n return observabil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