@mastra/mcp-apps-components 0.1.0-alpha.1 → 0.1.0-alpha.2
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 +50 -20
- package/dist/apps/observability-widget/tool.d.ts +11 -0
- package/dist/apps/observability-widget/tool.d.ts.map +1 -1
- package/dist/host.cjs +8 -7
- package/dist/host.cjs.map +1 -1
- package/dist/host.js +8 -7
- package/dist/host.js.map +1 -1
- package/dist/index.cjs +1072 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +10 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1065 -10
- package/dist/index.js.map +1 -1
- package/dist/observability-dashboard.d.ts +183 -0
- package/dist/observability-dashboard.d.ts.map +1 -0
- package/dist/{tool-C6_-nIOM.cjs → tool-B4yU6CY0.cjs} +38 -1
- package/dist/tool-B4yU6CY0.cjs.map +1 -0
- package/dist/tool-C601G1w9.js +38 -0
- package/dist/tool-C601G1w9.js.map +1 -0
- package/package.json +7 -2
- package/dist/tool-BD-7EJf2.js +0 -13
- package/dist/tool-BD-7EJf2.js.map +0 -1
- package/dist/tool-C6_-nIOM.cjs.map +0 -1
package/README.md
CHANGED
|
@@ -53,46 +53,59 @@ export const greetingServer = new MCPServer({
|
|
|
53
53
|
});
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
### Observability
|
|
56
|
+
### Observability dashboard
|
|
57
57
|
|
|
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
|
-
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: `{
|
|
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
|
|
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`, …), and `args` are that method's arguments, with dates as ISO strings. 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
|
+
|
|
65
|
+
- The schema advertises every widget kind's options to the model, so the model can write its own widgets, and a wrong one fails input validation with errors the model can act on.
|
|
66
|
+
- The schema names the metrics Mastra records, but leaves metric names free: a name that doesn't exist reads as zero rather than failing. To make the model pick only metrics that have data, build the schema with `createObservabilityDashboardInputSchema({ metricNames })`, passing the names from `MastraClient#getMetricNames`. Unknown names are then rejected with the list of valid ones. Past `OBSERVABILITY_CATALOG_MAX_METRIC_NAMES` (200) names, or with none, they stay free. The schema is a snapshot, so rebuild the tool (`server.toolActions.add`) when the names change; `examples/agent` does this before each agent run.
|
|
67
|
+
- Label keys and values aren't listed. Give the model a tool that looks them up (for example with `MastraClient#getMetricLabelKeys` and `getMetricLabelValues`).
|
|
68
|
+
- Widgets without an `id` get a fresh UUID on every open, so the host can pin each one by an id that stays stable when the result is shown again.
|
|
69
|
+
|
|
70
|
+
The tools run on your server, so you decide which Mastra server the dashboard reads from and how it authenticates. Allow `OBSERVABILITY_READ_ACTIONS` and nothing else: a widget whose read is missing from the list fails.
|
|
64
71
|
|
|
65
72
|
```typescript
|
|
66
73
|
import { MastraClient } from '@mastra/client-js';
|
|
67
74
|
import { createTool } from '@mastra/core/tools';
|
|
68
75
|
import { MCPServer } from '@mastra/mcp';
|
|
69
|
-
import {
|
|
76
|
+
import {
|
|
77
|
+
OBSERVABILITY_READ_ACTIONS,
|
|
78
|
+
QUERY_OBSERVABILITY_TOOL,
|
|
79
|
+
observabilityDashboardApp,
|
|
80
|
+
observabilityDashboardInputSchema,
|
|
81
|
+
observabilityDashboardOutputSchema,
|
|
82
|
+
toObservabilityDashboard,
|
|
83
|
+
} from '@mastra/mcp-apps-components';
|
|
70
84
|
import { z } from 'zod';
|
|
71
85
|
|
|
72
86
|
const uri = 'ui://mastra/observability-widget';
|
|
73
87
|
const client = new MastraClient({ baseUrl: process.env.MASTRA_URL! });
|
|
74
|
-
const ACTIONS = ['getMetricAggregate', 'getMetricTimeSeries'] as const;
|
|
75
88
|
|
|
76
89
|
export const observabilityServer = new MCPServer({
|
|
77
90
|
id: 'observability',
|
|
78
91
|
name: 'Observability',
|
|
79
92
|
version: '1.0.0',
|
|
80
|
-
appResources: { [uri]:
|
|
93
|
+
appResources: { [uri]: observabilityDashboardApp },
|
|
81
94
|
tools: {
|
|
82
|
-
// Visible to the model: opens the app with a
|
|
83
|
-
|
|
84
|
-
id: '
|
|
85
|
-
description: 'Shows
|
|
86
|
-
inputSchema:
|
|
87
|
-
outputSchema:
|
|
95
|
+
// Visible to the model: opens the app with a dashboard.
|
|
96
|
+
showObservabilityDashboard: createTool({
|
|
97
|
+
id: 'showObservabilityDashboard',
|
|
98
|
+
description: 'Shows an observability dashboard of kpis, timeseries, table and scores widgets.',
|
|
99
|
+
inputSchema: observabilityDashboardInputSchema,
|
|
100
|
+
outputSchema: observabilityDashboardOutputSchema,
|
|
88
101
|
mcp: { _meta: { ui: { resourceUri: uri } } },
|
|
89
|
-
execute: async
|
|
102
|
+
execute: async input => toObservabilityDashboard(input),
|
|
90
103
|
}),
|
|
91
104
|
// Visible to the app only: runs one read on the server's client.
|
|
92
105
|
[QUERY_OBSERVABILITY_TOOL]: createTool({
|
|
93
106
|
id: QUERY_OBSERVABILITY_TOOL,
|
|
94
107
|
description: 'Runs one observability read',
|
|
95
|
-
inputSchema: z.object({ action: z.enum(
|
|
108
|
+
inputSchema: z.object({ action: z.enum(OBSERVABILITY_READ_ACTIONS), args: z.record(z.string(), z.unknown()) }),
|
|
96
109
|
outputSchema: z.object({ data: z.unknown() }),
|
|
97
110
|
mcp: { _meta: { ui: { resourceUri: uri, visibility: ['app'] } } },
|
|
98
111
|
execute: async ({ action, args }) => {
|
|
@@ -104,11 +117,28 @@ export const observabilityServer = new MCPServer({
|
|
|
104
117
|
});
|
|
105
118
|
```
|
|
106
119
|
|
|
107
|
-
`
|
|
120
|
+
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
|
+
|
|
122
|
+
```ts
|
|
123
|
+
{
|
|
124
|
+
span: 6,
|
|
125
|
+
timeseries: {
|
|
126
|
+
chart: 'stackedBar',
|
|
127
|
+
format: 'count',
|
|
128
|
+
series: [{ label: 'Runs', color: 'green', value: { metricTimeSeries: { name: ['mastra_agent_duration_ms'], aggregation: 'count' } } }],
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
`toObservabilityDashboard` gathers the reads into each view's queries, one per distinct read, and fills in what the stored definition points at: series keys, a chart's whole-range totals, a table's default sort.
|
|
134
|
+
|
|
135
|
+
A host can also build a dashboard itself: `toObservabilityDashboard({ dashboard: { rows: [{ widgets: [runsWidget] }] }, datePreset: '7d' })` returns `{ dashboard, datePreset? }` as a regular `dashboardDefinitionSchema` definition and throws when the input is invalid.
|
|
136
|
+
|
|
137
|
+
`examples/agent` has a complete version with every read action and an agent that opens dashboards.
|
|
108
138
|
|
|
109
139
|
#### Pinning widgets
|
|
110
140
|
|
|
111
|
-
When the chat host supports it,
|
|
141
|
+
When the chat host supports it, each widget shows a pin button. The host owns the pinned widgets: it sends their ids to the app, and the app reports each pin and unpin through `ui/update-model-context`. In a host built on `@mastra/playground-ui`, pass `observabilityWidgetPins` from `@mastra/mcp-apps-components/host` to `McpAppHostProvider`:
|
|
112
142
|
|
|
113
143
|
```tsx
|
|
114
144
|
import { McpAppHostProvider } from '@mastra/playground-ui/domains/mcps';
|
|
@@ -119,7 +149,7 @@ const host = useMemo(
|
|
|
119
149
|
observabilityWidgetPins({
|
|
120
150
|
resourceUri: 'ui://mastra/observability-widget',
|
|
121
151
|
pinnedIds,
|
|
122
|
-
onPin: ({ widget, datePreset }) => savePin(widget, datePreset),
|
|
152
|
+
onPin: ({ widget, datePreset, filters }) => savePin(widget, datePreset, filters),
|
|
123
153
|
onUnpin: widgetId => removePin(widgetId),
|
|
124
154
|
}),
|
|
125
155
|
[pinnedIds],
|
|
@@ -128,7 +158,7 @@ const host = useMemo(
|
|
|
128
158
|
<McpAppHostProvider value={host}>{chat}</McpAppHostProvider>;
|
|
129
159
|
```
|
|
130
160
|
|
|
131
|
-
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.
|
|
161
|
+
A pin carries the widget, the date preset it was drawn over and, when the dashboard had any, its `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.
|
|
132
162
|
|
|
133
163
|
## Documentation
|
|
134
164
|
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
/** The app-only tool the observability widget calls, on the server that opened it, for every read. */
|
|
2
2
|
export declare const QUERY_OBSERVABILITY_TOOL = "queryObservability";
|
|
3
|
+
/**
|
|
4
|
+
* Every `MastraClient` method the app sends as a `queryObservability` action. All are reads; allow
|
|
5
|
+
* these and nothing else (`z.enum(OBSERVABILITY_READ_ACTIONS)`).
|
|
6
|
+
*/
|
|
7
|
+
export declare const OBSERVABILITY_READ_ACTIONS: readonly ['aggregateTraces', 'aggregateSpans', 'getMetricAggregate', 'getMetricBreakdown', 'getMetricTimeSeries', 'getMetricPercentiles', 'getScoreAggregate', 'getScoreBreakdown', 'getScoreTimeSeries', 'getScorePercentiles', 'listScores'];
|
|
8
|
+
export type ObservabilityReadAction = (typeof OBSERVABILITY_READ_ACTIONS)[number];
|
|
9
|
+
/** The windows the widget can show, ending when it opens; the same presets as Studio's metrics. */
|
|
10
|
+
export declare const OBSERVABILITY_DATE_PRESETS: readonly ['24h', '3d', '7d', '14d', '30d'];
|
|
11
|
+
export type ObservabilityDatePreset = (typeof OBSERVABILITY_DATE_PRESETS)[number];
|
|
3
12
|
/**
|
|
4
13
|
* Host context key holding the ids of the widgets the host has pinned (`string[]`). The pin button
|
|
5
14
|
* only shows when the host sends it and supports `updateModelContext.structuredContent`.
|
|
@@ -16,5 +25,7 @@ export type ObservabilityWidgetPin = {
|
|
|
16
25
|
[key: string]: unknown;
|
|
17
26
|
};
|
|
18
27
|
datePreset: string;
|
|
28
|
+
/** The dashboard's `defaults.filters` the widget was drawn under, when it has any. */
|
|
29
|
+
filters?: unknown[];
|
|
19
30
|
};
|
|
20
31
|
//# 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,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;
|
|
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,sFAAsF;IACtF,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-B4yU6CY0.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. */
|
|
@@ -7,13 +7,15 @@ function readPin(structuredContent) {
|
|
|
7
7
|
const pin = structuredContent?.[require_tool.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];
|
|
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
|
+
if (pin.filters !== void 0 && !Array.isArray(pin.filters)) return void 0;
|
|
10
11
|
return {
|
|
11
12
|
pinned: pin.pinned,
|
|
12
13
|
widget: {
|
|
13
14
|
...pin.widget,
|
|
14
15
|
id: pin.widget.id
|
|
15
16
|
},
|
|
16
|
-
datePreset: pin.datePreset
|
|
17
|
+
datePreset: pin.datePreset,
|
|
18
|
+
...pin.filters ? { filters: pin.filters } : {}
|
|
17
19
|
};
|
|
18
20
|
}
|
|
19
21
|
/**
|
|
@@ -38,11 +40,10 @@ function observabilityWidgetPins({ resourceUri, pinnedIds, onPin, onUnpin }) {
|
|
|
38
40
|
if (app.resourceUri !== resourceUri) return;
|
|
39
41
|
const pin = readPin(params.structuredContent);
|
|
40
42
|
if (!pin) return;
|
|
41
|
-
if (pin.pinned)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}, app);
|
|
45
|
-
else onUnpin(pin.widget.id, app);
|
|
43
|
+
if (pin.pinned) {
|
|
44
|
+
const { pinned: _pinned, ...pinned } = pin;
|
|
45
|
+
onPin(pinned, app);
|
|
46
|
+
} else onUnpin(pin.widget.id, app);
|
|
46
47
|
}
|
|
47
48
|
};
|
|
48
49
|
}
|
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 { ObservabilityWidgetPin } from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type { ObservabilityWidgetPin } 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 ObservabilityWidgetPinsOptions = {\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /** The user pinned `pin.widget` over `pin.datePreset
|
|
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 { ObservabilityWidgetPin } from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type { ObservabilityWidgetPin } 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 ObservabilityWidgetPinsOptions = {\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /** The user pinned `pin.widget` over `pin.datePreset`, under `pin.filters` when it has them. */\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\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 return {\n pinned: pin.pinned,\n widget: { ...pin.widget, id: pin.widget.id },\n datePreset: pin.datePreset,\n ...(pin.filters ? { filters: pin.filters } : {}),\n };\n}\n\n/**\n * Lets the user pin observability widgets from the chat. The host owns the pinned widgets: it\n * sends their ids to each widget app, and hears each pin and unpin.\n *\n * ```tsx\n * const host = useMemo(\n * () => observabilityWidgetPins({ resourceUri: 'ui://mastra/observability-widget', pinnedIds, onPin, onUnpin }),\n * [pinnedIds, onPin, onUnpin],\n * );\n * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>\n * ```\n *\n * A click shows on the button right away and holds until the host's ids next change, so a host\n * that turns a click down leaves it showing until then.\n */\nexport function observabilityWidgetPins({\n resourceUri,\n pinnedIds,\n onPin,\n onUnpin,\n}: ObservabilityWidgetPinsOptions): McpAppHostValue {\n return {\n hostContext: app =>\n app.resourceUri === resourceUri ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pinnedIds } : undefined,\n onModelContext: (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 onPin(pinned, app);\n } else onUnpin(pin.widget.id, app);\n },\n };\n}\n"],"mappings":";;;AAsCA,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,OAAO;EACL,QAAQ,IAAI;EACZ,QAAQ;GAAE,GAAG,IAAI;GAAQ,IAAI,IAAI,OAAO;EAAG;EAC3C,YAAY,IAAI;EAChB,GAAI,IAAI,UAAU,EAAE,SAAS,IAAI,QAAQ,IAAI,CAAC;CAChD;AACF;;;;;;;;;;;;;;;;AAiBA,SAAgB,wBAAwB,EACtC,aACA,WACA,OACA,WACkD;CAClD,OAAO;EACL,cAAa,QACX,IAAI,gBAAgB,cAAc,GAAGC,aAAAA,0CAA0C,UAAU,IAAI,KAAA;EAC/F,iBAAiB,KAAK,WAAW;GAC/B,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,MAAM,QAAQ,GAAG;GACnB,OAAO,QAAQ,IAAI,OAAO,IAAI,GAAG;EACnC;CACF;AACF"}
|
package/dist/host.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, r as OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY } from "./tool-C601G1w9.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. */
|
|
@@ -6,13 +6,15 @@ function readPin(structuredContent) {
|
|
|
6
6
|
const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];
|
|
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
|
+
if (pin.filters !== void 0 && !Array.isArray(pin.filters)) return void 0;
|
|
9
10
|
return {
|
|
10
11
|
pinned: pin.pinned,
|
|
11
12
|
widget: {
|
|
12
13
|
...pin.widget,
|
|
13
14
|
id: pin.widget.id
|
|
14
15
|
},
|
|
15
|
-
datePreset: pin.datePreset
|
|
16
|
+
datePreset: pin.datePreset,
|
|
17
|
+
...pin.filters ? { filters: pin.filters } : {}
|
|
16
18
|
};
|
|
17
19
|
}
|
|
18
20
|
/**
|
|
@@ -37,11 +39,10 @@ function observabilityWidgetPins({ resourceUri, pinnedIds, onPin, onUnpin }) {
|
|
|
37
39
|
if (app.resourceUri !== resourceUri) return;
|
|
38
40
|
const pin = readPin(params.structuredContent);
|
|
39
41
|
if (!pin) return;
|
|
40
|
-
if (pin.pinned)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}, app);
|
|
44
|
-
else onUnpin(pin.widget.id, app);
|
|
42
|
+
if (pin.pinned) {
|
|
43
|
+
const { pinned: _pinned, ...pinned } = pin;
|
|
44
|
+
onPin(pinned, app);
|
|
45
|
+
} else onUnpin(pin.widget.id, app);
|
|
45
46
|
}
|
|
46
47
|
};
|
|
47
48
|
}
|
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 { ObservabilityWidgetPin } from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type { ObservabilityWidgetPin } 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 ObservabilityWidgetPinsOptions = {\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /** The user pinned `pin.widget` over `pin.datePreset
|
|
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 { ObservabilityWidgetPin } from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type { ObservabilityWidgetPin } 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 ObservabilityWidgetPinsOptions = {\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /** The user pinned `pin.widget` over `pin.datePreset`, under `pin.filters` when it has them. */\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\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 return {\n pinned: pin.pinned,\n widget: { ...pin.widget, id: pin.widget.id },\n datePreset: pin.datePreset,\n ...(pin.filters ? { filters: pin.filters } : {}),\n };\n}\n\n/**\n * Lets the user pin observability widgets from the chat. The host owns the pinned widgets: it\n * sends their ids to each widget app, and hears each pin and unpin.\n *\n * ```tsx\n * const host = useMemo(\n * () => observabilityWidgetPins({ resourceUri: 'ui://mastra/observability-widget', pinnedIds, onPin, onUnpin }),\n * [pinnedIds, onPin, onUnpin],\n * );\n * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>\n * ```\n *\n * A click shows on the button right away and holds until the host's ids next change, so a host\n * that turns a click down leaves it showing until then.\n */\nexport function observabilityWidgetPins({\n resourceUri,\n pinnedIds,\n onPin,\n onUnpin,\n}: ObservabilityWidgetPinsOptions): McpAppHostValue {\n return {\n hostContext: app =>\n app.resourceUri === resourceUri ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pinnedIds } : undefined,\n onModelContext: (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 onPin(pinned, app);\n } else onUnpin(pin.widget.id, app);\n },\n };\n}\n"],"mappings":";;AAsCA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;AAGrE,SAAS,QAAQ,mBAA4F;CAC3G,MAAM,MAAM,oBAAoB;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,OAAO;EACL,QAAQ,IAAI;EACZ,QAAQ;GAAE,GAAG,IAAI;GAAQ,IAAI,IAAI,OAAO;EAAG;EAC3C,YAAY,IAAI;EAChB,GAAI,IAAI,UAAU,EAAE,SAAS,IAAI,QAAQ,IAAI,CAAC;CAChD;AACF;;;;;;;;;;;;;;;;AAiBA,SAAgB,wBAAwB,EACtC,aACA,WACA,OACA,WACkD;CAClD,OAAO;EACL,cAAa,QACX,IAAI,gBAAgB,cAAc,GAAG,0CAA0C,UAAU,IAAI,KAAA;EAC/F,iBAAiB,KAAK,WAAW;GAC/B,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,MAAM,QAAQ,GAAG;GACnB,OAAO,QAAQ,IAAI,OAAO,IAAI,GAAG;EACnC;CACF;AACF"}
|