@mastra/mcp-apps-components 0.0.0-beta-gen-ui-20261110035130
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/LICENSE.md +32 -0
- package/README.md +211 -0
- package/dist/apps/observability-widget/tool.d.ts +55 -0
- package/dist/apps/observability-widget/tool.d.ts.map +1 -0
- package/dist/host.cjs +92 -0
- package/dist/host.cjs.map +1 -0
- package/dist/host.d.ts +72 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +87 -0
- package/dist/host.js.map +1 -0
- package/dist/index.cjs +1096 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1086 -0
- package/dist/index.js.map +1 -0
- package/dist/observability-dashboard.d.ts +183 -0
- package/dist/observability-dashboard.d.ts.map +1 -0
- package/dist/tool-B9qpVU16.cjs +75 -0
- package/dist/tool-B9qpVU16.cjs.map +1 -0
- package/dist/tool-HqUC6iks.js +40 -0
- package/dist/tool-HqUC6iks.js.map +1 -0
- package/package.json +89 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Portions of this software are licensed as follows:
|
|
2
|
+
|
|
3
|
+
- All content that resides under any directory named `ee/` within this
|
|
4
|
+
repository, including but not limited to:
|
|
5
|
+
- `@mastra/core/auth/ee`
|
|
6
|
+
- `@mastra/core/agent-builder/ee`
|
|
7
|
+
- `@mastra/editor/ee`
|
|
8
|
+
|
|
9
|
+
is licensed under the license defined in [`ee/LICENSE`](https://github.com/mastra-ai/mastra/blob/main/ee/LICENSE).
|
|
10
|
+
|
|
11
|
+
- All third-party components incorporated into the Mastra Software are
|
|
12
|
+
licensed under the original license provided by the owner of the
|
|
13
|
+
applicable component.
|
|
14
|
+
|
|
15
|
+
- Content outside of the above-mentioned directories or restrictions is
|
|
16
|
+
available under the "Apache License 2.0" as defined below.
|
|
17
|
+
|
|
18
|
+
# Apache License 2.0
|
|
19
|
+
|
|
20
|
+
Copyright (c) 2025 Kepler Software, Inc.
|
|
21
|
+
|
|
22
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
23
|
+
you may not use this file except in compliance with the License.
|
|
24
|
+
You may obtain a copy of the License at
|
|
25
|
+
|
|
26
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
27
|
+
|
|
28
|
+
Unless required by applicable law or agreed to in writing, software
|
|
29
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
30
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
31
|
+
See the License for the specific language governing permissions and
|
|
32
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# @mastra/mcp-apps-components
|
|
2
|
+
|
|
3
|
+
Prebuilt [MCP Apps](https://mastra.ai/docs/connections/mcp) that you register as app resources on an `MCPServer`. Each app is a single self-contained HTML page that follows the [MCP Apps spec](https://github.com/modelcontextprotocol/ext-apps), so it runs in any MCP Apps host, such as Mastra Studio.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mastra/mcp-apps-components
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
`greetingApp` greets someone by name. It talks to its host with standard MCP Apps messages only:
|
|
14
|
+
|
|
15
|
+
1. The model calls the tool that opens the app. The host sends the app that tool's arguments (`ui/notifications/tool-input`); the app reads `name`.
|
|
16
|
+
2. The app calls the app-only `getGreeting` tool on the same server (`tools/call`) and shows the `greeting` string from its structured content, or the tool's error.
|
|
17
|
+
3. A **Say hi back** button posts a user message to the conversation (`ui/message`).
|
|
18
|
+
|
|
19
|
+
You write both tools. `getGreeting` runs on your server, so it can call any API with your server's credentials; the app's iframe has no network access.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { createTool } from '@mastra/core/tools';
|
|
23
|
+
import { MCPServer } from '@mastra/mcp';
|
|
24
|
+
import { greetingApp } from '@mastra/mcp-apps-components';
|
|
25
|
+
import { z } from 'zod';
|
|
26
|
+
|
|
27
|
+
const uri = 'ui://mastra/greeting';
|
|
28
|
+
|
|
29
|
+
export const greetingServer = new MCPServer({
|
|
30
|
+
id: 'greeting',
|
|
31
|
+
name: 'Greeting',
|
|
32
|
+
version: '1.0.0',
|
|
33
|
+
appResources: { [uri]: greetingApp },
|
|
34
|
+
tools: {
|
|
35
|
+
// Visible to the model: opens the app.
|
|
36
|
+
showGreeting: createTool({
|
|
37
|
+
id: 'showGreeting',
|
|
38
|
+
description: 'Shows a greeting card for someone',
|
|
39
|
+
inputSchema: z.object({ name: z.string() }),
|
|
40
|
+
mcp: { _meta: { ui: { resourceUri: uri } } },
|
|
41
|
+
execute: async ({ name }) => ({ name }),
|
|
42
|
+
}),
|
|
43
|
+
// Visible to the app only: returns its data.
|
|
44
|
+
getGreeting: createTool({
|
|
45
|
+
id: 'getGreeting',
|
|
46
|
+
description: 'Returns the greeting for a name',
|
|
47
|
+
inputSchema: z.object({ name: z.string() }),
|
|
48
|
+
outputSchema: z.object({ greeting: z.string() }),
|
|
49
|
+
mcp: { _meta: { ui: { resourceUri: uri, visibility: ['app'] } } },
|
|
50
|
+
execute: async ({ name }) => ({ greeting: `Hello, ${name}!` }),
|
|
51
|
+
}),
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Observability dashboard
|
|
57
|
+
|
|
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
|
+
|
|
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, 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
|
+
|
|
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.
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { MastraClient } from '@mastra/client-js';
|
|
74
|
+
import { createTool } from '@mastra/core/tools';
|
|
75
|
+
import { MCPServer } from '@mastra/mcp';
|
|
76
|
+
import {
|
|
77
|
+
OBSERVABILITY_READ_ACTIONS,
|
|
78
|
+
QUERY_OBSERVABILITY_TOOL,
|
|
79
|
+
observabilityDashboardApp,
|
|
80
|
+
observabilityDashboardInputSchema,
|
|
81
|
+
observabilityDashboardOutputSchema,
|
|
82
|
+
toObservabilityDashboard,
|
|
83
|
+
} from '@mastra/mcp-apps-components';
|
|
84
|
+
import { z } from 'zod';
|
|
85
|
+
|
|
86
|
+
const uri = 'ui://mastra/observability-widget';
|
|
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>;
|
|
91
|
+
|
|
92
|
+
export const observabilityServer = new MCPServer({
|
|
93
|
+
id: 'observability',
|
|
94
|
+
name: 'Observability',
|
|
95
|
+
version: '1.0.0',
|
|
96
|
+
appResources: { [uri]: observabilityDashboardApp },
|
|
97
|
+
tools: {
|
|
98
|
+
// Visible to the model: opens the app with a dashboard.
|
|
99
|
+
showObservabilityDashboard: createTool({
|
|
100
|
+
id: 'showObservabilityDashboard',
|
|
101
|
+
description: 'Shows an observability dashboard of kpis, timeseries, table and scores widgets.',
|
|
102
|
+
inputSchema: observabilityDashboardInputSchema,
|
|
103
|
+
outputSchema: observabilityDashboardOutputSchema,
|
|
104
|
+
mcp: { _meta: { ui: { resourceUri: uri } } },
|
|
105
|
+
execute: async input => toObservabilityDashboard(input),
|
|
106
|
+
}),
|
|
107
|
+
// Visible to the app only: runs one read on the server's client.
|
|
108
|
+
[QUERY_OBSERVABILITY_TOOL]: createTool({
|
|
109
|
+
id: QUERY_OBSERVABILITY_TOOL,
|
|
110
|
+
description: 'Runs one observability read',
|
|
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
|
+
}),
|
|
116
|
+
outputSchema: z.object({ data: z.unknown() }),
|
|
117
|
+
mcp: { _meta: { ui: { resourceUri: uri, visibility: ['app'] } } },
|
|
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>;
|
|
121
|
+
return { data: await read(args) };
|
|
122
|
+
},
|
|
123
|
+
}),
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
```
|
|
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
|
+
|
|
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:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
{
|
|
134
|
+
span: 6,
|
|
135
|
+
timeseries: {
|
|
136
|
+
chart: 'stackedBar',
|
|
137
|
+
format: 'count',
|
|
138
|
+
series: [{ label: 'Runs', color: 'green', value: { metricTimeSeries: { name: ['mastra_agent_duration_ms'], aggregation: 'count' } } }],
|
|
139
|
+
},
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
`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.
|
|
144
|
+
|
|
145
|
+
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.
|
|
146
|
+
|
|
147
|
+
`examples/agent` has a complete version with every read action and an agent that opens dashboards.
|
|
148
|
+
|
|
149
|
+
#### Pinning widgets
|
|
150
|
+
|
|
151
|
+
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`:
|
|
152
|
+
|
|
153
|
+
```tsx
|
|
154
|
+
import { McpAppHostProvider } from '@mastra/playground-ui/domains/mcps';
|
|
155
|
+
import { observabilityWidgetPins } from '@mastra/mcp-apps-components/host';
|
|
156
|
+
|
|
157
|
+
const host = useMemo(
|
|
158
|
+
() =>
|
|
159
|
+
observabilityWidgetPins({
|
|
160
|
+
resourceUri: 'ui://mastra/observability-widget',
|
|
161
|
+
pinnedIds,
|
|
162
|
+
onPin: ({ widget, datePreset, filters }) => savePin(widget, datePreset, filters),
|
|
163
|
+
onUnpin: widgetId => removePin(widgetId),
|
|
164
|
+
}),
|
|
165
|
+
[pinnedIds],
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
<McpAppHostProvider value={host}>{chat}</McpAppHostProvider>;
|
|
169
|
+
```
|
|
170
|
+
|
|
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.
|
|
199
|
+
|
|
200
|
+
## Documentation
|
|
201
|
+
|
|
202
|
+
- [MCP Apps in Mastra](https://mastra.ai/docs/connections/mcp)
|
|
203
|
+
- [MCP Apps spec](https://github.com/modelcontextprotocol/ext-apps)
|
|
204
|
+
|
|
205
|
+
## Changelog
|
|
206
|
+
|
|
207
|
+
See the [package changelog](https://github.com/mastra-ai/mastra/blob/main/packages/mcp-apps-components/CHANGELOG.md) for version history and release notes.
|
|
208
|
+
|
|
209
|
+
## Support
|
|
210
|
+
|
|
211
|
+
We have an [open community Discord](https://discord.gg/mastra-ai). Come and say hello and let us know if you have any questions or need any help getting things running.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { FilterNode } from '@mastra/core/storage/dashboards';
|
|
2
|
+
/** The app-only tool the observability widget calls, on the server that opened it, for every read. */
|
|
3
|
+
export declare const QUERY_OBSERVABILITY_TOOL = "queryObservability";
|
|
4
|
+
/**
|
|
5
|
+
* Every `MastraClient` method the app sends as a `queryObservability` action. All are reads; allow
|
|
6
|
+
* these and nothing else (`z.enum(OBSERVABILITY_READ_ACTIONS)`).
|
|
7
|
+
*/
|
|
8
|
+
export declare const OBSERVABILITY_READ_ACTIONS: readonly ['aggregateTraces', 'aggregateSpans', 'getMetricAggregate', 'getMetricBreakdown', 'getMetricTimeSeries', 'getMetricPercentiles', 'getScoreAggregate', 'getScoreBreakdown', 'getScoreTimeSeries', 'getScorePercentiles', 'listScores'];
|
|
9
|
+
export type ObservabilityReadAction = (typeof OBSERVABILITY_READ_ACTIONS)[number];
|
|
10
|
+
/** The windows the widget can show, ending when it opens; the same presets as Studio's metrics. */
|
|
11
|
+
export declare const OBSERVABILITY_DATE_PRESETS: readonly ['24h', '3d', '7d', '14d', '30d'];
|
|
12
|
+
export type ObservabilityDatePreset = (typeof OBSERVABILITY_DATE_PRESETS)[number];
|
|
13
|
+
/**
|
|
14
|
+
* Host context key holding the ids of the widgets the host has pinned (`string[]`). The pin button
|
|
15
|
+
* only shows when the host sends it and supports `updateModelContext.structuredContent`.
|
|
16
|
+
*/
|
|
17
|
+
export declare const OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY = "mastra.ai/observability-widget-pinned";
|
|
18
|
+
/** `structuredContent` key of the model context update the app sends when the user (un)pins. */
|
|
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
|
+
};
|
|
41
|
+
/** What the app sends under `OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY`: the state the user asked for. */
|
|
42
|
+
export type ObservabilityWidgetPin = {
|
|
43
|
+
pinned: boolean;
|
|
44
|
+
/** The stored dashboard widget, as the opening tool returned it. */
|
|
45
|
+
widget: {
|
|
46
|
+
id: string;
|
|
47
|
+
[key: string]: unknown;
|
|
48
|
+
};
|
|
49
|
+
datePreset: string;
|
|
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. */
|
|
53
|
+
filters?: unknown[];
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../../src/apps/observability-widget/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAA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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
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_tool = require("./tool-B9qpVU16.cjs");
|
|
3
|
+
// src/host.ts
|
|
4
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
+
/** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */
|
|
6
|
+
function readPin(structuredContent) {
|
|
7
|
+
const pin = structuredContent?.[require_tool.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];
|
|
8
|
+
if (!isRecord(pin) || typeof pin.pinned !== "boolean" || typeof pin.datePreset !== "string") return void 0;
|
|
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;
|
|
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
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
pinned: pin.pinned,
|
|
18
|
+
widget: {
|
|
19
|
+
...pin.widget,
|
|
20
|
+
id: pin.widget.id
|
|
21
|
+
},
|
|
22
|
+
datePreset: pin.datePreset,
|
|
23
|
+
...customRange ? { customRange } : {},
|
|
24
|
+
...pin.filters ? { filters: pin.filters } : {}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
const toIso = (date) => date instanceof Date ? date.toISOString() : date;
|
|
28
|
+
/**
|
|
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.
|
|
32
|
+
*
|
|
33
|
+
* ```tsx
|
|
34
|
+
* const host = useMemo(
|
|
35
|
+
* () =>
|
|
36
|
+
* observabilityWidgetHost({
|
|
37
|
+
* resourceUri: 'ui://mastra/observability-widget',
|
|
38
|
+
* datePreset,
|
|
39
|
+
* customRange,
|
|
40
|
+
* filters,
|
|
41
|
+
* projectId,
|
|
42
|
+
* }),
|
|
43
|
+
* [datePreset, customRange, filters, projectId],
|
|
44
|
+
* );
|
|
45
|
+
* <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
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.
|
|
50
|
+
*/
|
|
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
|
+
};
|
|
63
|
+
return {
|
|
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) => {
|
|
69
|
+
if (app.resourceUri !== resourceUri) return;
|
|
70
|
+
const pin = readPin(params.structuredContent);
|
|
71
|
+
if (!pin) return;
|
|
72
|
+
if (pin.pinned) {
|
|
73
|
+
const { pinned: _pinned, ...pinned } = pin;
|
|
74
|
+
pins.onPin(pinned, app);
|
|
75
|
+
} else pins.onUnpin(pin.widget.id, app);
|
|
76
|
+
} : void 0
|
|
77
|
+
};
|
|
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
|
+
}
|
|
86
|
+
exports.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY;
|
|
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;
|
|
90
|
+
exports.observabilityWidgetPins = observabilityWidgetPins;
|
|
91
|
+
|
|
92
|
+
//# sourceMappingURL=host.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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,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AAM,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,OAAO,EACL,oCAAoC,EACpC,uCAAuC,EACvC,qCAAqC,GACtC,MAAM,kCAAkC,CAAC;AAC1C,YAAY,EACV,4BAA4B,EAC5B,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,kCAAkC,CAAC;AAE1C,0EAA0E;AAC1E,MAAM,MAAM,SAAS,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtF;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,SAAS,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IACtE,cAAc,CAAC,EAAE,CACf,GAAG,EAAE,SAAS,EACd,MAAM,EAAE;QAAE,OAAO,CAAC,EAAE,OA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ERVABILITY_WIDGET_VIEW_CONTEXT_KEY, i as OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, r as OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY } from "./tool-HqUC6iks.js";
|
|
2
|
+
// src/host.ts
|
|
3
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4
|
+
/** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */
|
|
5
|
+
function readPin(structuredContent) {
|
|
6
|
+
const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];
|
|
7
|
+
if (!isRecord(pin) || typeof pin.pinned !== "boolean" || typeof pin.datePreset !== "string") return void 0;
|
|
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;
|
|
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
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
pinned: pin.pinned,
|
|
17
|
+
widget: {
|
|
18
|
+
...pin.widget,
|
|
19
|
+
id: pin.widget.id
|
|
20
|
+
},
|
|
21
|
+
datePreset: pin.datePreset,
|
|
22
|
+
...customRange ? { customRange } : {},
|
|
23
|
+
...pin.filters ? { filters: pin.filters } : {}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const toIso = (date) => date instanceof Date ? date.toISOString() : date;
|
|
27
|
+
/**
|
|
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.
|
|
31
|
+
*
|
|
32
|
+
* ```tsx
|
|
33
|
+
* const host = useMemo(
|
|
34
|
+
* () =>
|
|
35
|
+
* observabilityWidgetHost({
|
|
36
|
+
* resourceUri: 'ui://mastra/observability-widget',
|
|
37
|
+
* datePreset,
|
|
38
|
+
* customRange,
|
|
39
|
+
* filters,
|
|
40
|
+
* projectId,
|
|
41
|
+
* }),
|
|
42
|
+
* [datePreset, customRange, filters, projectId],
|
|
43
|
+
* );
|
|
44
|
+
* <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
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.
|
|
49
|
+
*/
|
|
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
|
+
};
|
|
62
|
+
return {
|
|
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) => {
|
|
68
|
+
if (app.resourceUri !== resourceUri) return;
|
|
69
|
+
const pin = readPin(params.structuredContent);
|
|
70
|
+
if (!pin) return;
|
|
71
|
+
if (pin.pinned) {
|
|
72
|
+
const { pinned: _pinned, ...pinned } = pin;
|
|
73
|
+
pins.onPin(pinned, app);
|
|
74
|
+
} else pins.onUnpin(pin.widget.id, app);
|
|
75
|
+
} : void 0
|
|
76
|
+
};
|
|
77
|
+
}
|
|
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 };
|
|
86
|
+
|
|
87
|
+
//# sourceMappingURL=host.js.map
|
package/dist/host.js.map
ADDED
|
@@ -0,0 +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 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,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,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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