administrate-mcp 0.5.0 → 0.6.0

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.
@@ -0,0 +1,135 @@
1
+ # Tool views
2
+
3
+ A tool view is an interactive page an MCP client renders in place of a tool's raw result. The
4
+ engine implements the server side of the [MCP Apps extension](https://github.com/modelcontextprotocol/ext-apps)
5
+ (SEP-1865): a view is a `ui://` HTML resource, a tool names the view that renders it in its `_meta`,
6
+ and a client that supports the extension fetches the view with `resources/read` and shows it next
7
+ to the conversation. A client without the extension ignores all of it and reads the tool's text
8
+ result as before.
9
+
10
+ Views are off until the host turns them on:
11
+
12
+ ```ruby
13
+ Administrate::MCP.configure do |config|
14
+ config.tool_views = true
15
+ end
16
+ ```
17
+
18
+ Nothing a tool returns changes for the model: the text content stays the same. A view receives the
19
+ full result, including a `_meta` entry the host keeps out of the model's context.
20
+
21
+ ## Built-in views
22
+
23
+ | View | URI | Tools | What it does |
24
+ | --- | --- | --- | --- |
25
+ | Resource | `ui://<server-name>/resource` | `admin_resource_list`, `admin_resource_show`, every `mcp_action` | A table of records with paging, sorting and search, a record page, and links that follow `belongs_to` and `has_many` associations to other registered resources. For an action, a confirmation, the data the action returned, and the record it saved |
26
+ | Sidekiq | `ui://<server-name>/sidekiq` | `sidekiq_stats`, `sidekiq_retries` | Counts per job class and per queue, the first page of retries, and a refresh button |
27
+
28
+ `<server-name>` is `config.server_name` with underscores turned into dashes.
29
+
30
+ The resource view needs nothing from a dashboard. `admin_resource_list` and `admin_resource_show`
31
+ add, when views are on, a `_meta["administrate-mcp/view"]` entry to their result: the resource name
32
+ and, per returned field, its kind (`belongs_to`, `has_many`, `datetime`, `enum`, `attachment`, `url`,
33
+ `scalar`, ...) and, for an association to a registered resource, that resource's name and the
34
+ foreign key a filtered list of it takes. Paging, sorting, search and following an association are
35
+ ordinary `tools/call` requests the client makes on the view's behalf, so they go through the same
36
+ authentication, authorization and `on_tool_call` audit as a call the model makes. A client that does
37
+ not proxy tool calls gets a read-only table whose rows open the record's admin page.
38
+
39
+ An `mcp_action` renders in the resource view unless it declares `view:`. The view shows the
40
+ action's outcome and the data it returned, then loads the record whose `id` the answer carries with
41
+ `admin_resource_show`, so an uploaded picture or a computed field is on screen right after the write.
42
+ A failed action shows its error message.
43
+
44
+ When the person moves to another page of the view, the view tells the client which records are on
45
+ screen (`ui/update-model-context`), so the next answer is about what the person is looking at. It
46
+ names them by resource and id only. Record text such as a name, and the person's search terms, stay
47
+ out of it: a model reads that context as coming from the client, and record text is written by
48
+ whoever owns the record.
49
+
50
+ The field hints name an associated resource only when the caller can list it, as
51
+ `admin_resource_list_resources` does.
52
+
53
+ Only `Field::Url` fields, attachments and the record's own admin link become links. A text field
54
+ that happens to hold a URL stays text, as on the admin show page, since end users write many of
55
+ those fields.
56
+
57
+ ## Images and other external content
58
+
59
+ A client renders a view under a Content Security Policy that, by default, loads nothing from another
60
+ origin. An attachment field whose URL points at your storage shows as a link until the origin is
61
+ declared:
62
+
63
+ ```ruby
64
+ config.tool_view_csp = { resource_domains: ['https://uploads.example.com'] }
65
+ ```
66
+
67
+ The keys are `resource_domains` (images, media, fonts, stylesheets, scripts), `connect_domains`
68
+ (`fetch` and WebSocket), `frame_domains` and `base_uri_domains`. Any other key raises when a view is
69
+ read. The client may still refuse an origin.
70
+
71
+ ## A view of your own
72
+
73
+ A view is a subclass of `Administrate::MCP::ToolView` that names a script and, optionally, a
74
+ stylesheet:
75
+
76
+ ```ruby
77
+ # app/mcp_views/review_queue_view.rb
78
+ class ReviewQueueView < Administrate::MCP::ToolView
79
+ title 'Review queue'
80
+ description 'The open reviews, with approve and reject buttons.'
81
+ script Rails.root.join('app/mcp_views/review_queue.js')
82
+ stylesheet Rails.root.join('app/mcp_views/review_queue.css')
83
+ end
84
+ ```
85
+
86
+ The view's name, used in its URI, is the class name without `View`, underscored (`review_queue`);
87
+ `view_name 'queue'` overrides it.
88
+
89
+ Link a tool to it with `tool_view`, or an action with `view:`:
90
+
91
+ ```ruby
92
+ class ReviewQueueTool < Administrate::MCP::BaseTool
93
+ tool_name 'review_queue'
94
+ tool_view ReviewQueueView
95
+ end
96
+
97
+ mcp_action :approve, description: 'Approve a review.', view: 'ReviewQueueView' do |record:, **|
98
+ record.approve!
99
+ end
100
+ ```
101
+
102
+ `tool_view` takes `visibility: %i[app]` for a tool only the view calls, which a client that supports
103
+ the extension keeps out of the model's tool list. It is not access control: a client without the
104
+ extension lists and calls the tool like any other, so the tool must run every check a model call
105
+ would need, and it does, since it is an ordinary tool on the server.
106
+
107
+ The engine wraps the script in an HTML document with a shared runtime, `window.AdministrateMCP`:
108
+
109
+ | Member | Does |
110
+ | --- | --- |
111
+ | `connect()` | Runs the `ui/initialize` handshake. Call it once, after registering handlers |
112
+ | `on('input' \| 'result' \| 'cancelled' \| 'context' \| 'teardown', callback)` | Registers a handler; `input` and `result` replay the last value to a late handler |
113
+ | `resultData(result)` | The result's `structuredContent`, or its JSON text content parsed |
114
+ | `resultError(result)` | The error message of a failed result, or `null` |
115
+ | `resultMeta(result, key)` | One entry of the result's `_meta` |
116
+ | `callTool(name, args)` | Calls a tool on this server through the client |
117
+ | `canCallTools()` | Whether the client proxies tool calls |
118
+ | `toolName()` | The name of the tool whose result the view is showing, when the client says |
119
+ | `openLink(url)`, `sendMessage(text)`, `updateModelContext(text)`, `requestDisplayMode(mode)` | The extension's `ui/open-link`, `ui/message`, `ui/update-model-context` and `ui/request-display-mode` |
120
+ | `h(tag, attributes, ...children)`, `mount(node)`, `link(url, label)`, `message(text, className)`, `formatDate(value)` | DOM helpers that set text, never markup |
121
+
122
+ The runtime reports the view's size to the client as it changes, and applies the client's theme
123
+ and CSS variables. The stylesheet it ships defines `--am-*` custom properties from those variables,
124
+ with light and dark fallbacks, for a view's own stylesheet to use.
125
+
126
+ A view renders admin data. Build its DOM with `h` or `textContent`, never `innerHTML`, so a record
127
+ cannot inject markup into the page. `h` only attaches functions as event handlers, and drops an
128
+ `href`, `src` or other URL attribute that is not `http` or `https`; `openLink` refuses the same
129
+ URLs. Keep record text out of `updateModelContext` and `sendMessage`: refer to records by id.
130
+
131
+ ## Asking for a view
132
+
133
+ `report_mcp_improvement` has a `missing_view` category for a result the person needed to browse,
134
+ compare or act on, and that read badly as text. Reports in it are the list of views worth building
135
+ next.
@@ -60,6 +60,8 @@ module Administrate
60
60
  :feedback_tool,
61
61
  :sidekiq_stats_provider,
62
62
  :action_data_max_bytes,
63
+ :tool_views,
64
+ :tool_view_csp,
63
65
  :admin_route_namespace,
64
66
  :admin_url_options,
65
67
  :skipped_field_classes,
@@ -105,6 +107,8 @@ module Administrate
105
107
  @sidekiq_stats_provider = nil
106
108
  @action_data_max_bytes = 32_768
107
109
  @uploads = UploadsConfiguration.new
110
+ @tool_views = false
111
+ @tool_view_csp = {}
108
112
  end
109
113
 
110
114
  def assign_fields
@@ -0,0 +1,25 @@
1
+ dl.am-fields {
2
+ display: grid;
3
+ grid-template-columns: minmax(110px, max-content) 1fr;
4
+ gap: 6px 16px;
5
+ margin: 0;
6
+ }
7
+ dl.am-fields dt { color: var(--am-text-muted); }
8
+ dl.am-fields dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
9
+ dl.am-fields dd .am-table-wrap { margin-top: 6px; }
10
+
11
+ img.am-image {
12
+ display: block;
13
+ max-width: min(100%, 320px);
14
+ max-height: 240px;
15
+ border-radius: var(--am-radius);
16
+ border: 1px solid var(--am-border);
17
+ }
18
+ img.am-thumb { display: block; max-width: 48px; max-height: 48px; border-radius: 4px; }
19
+
20
+ .am-pager { justify-content: flex-end; margin: 10px 0 0; }
21
+
22
+ @media (max-width: 480px) {
23
+ dl.am-fields { grid-template-columns: 1fr; gap: 2px; }
24
+ dl.am-fields dd { margin-bottom: 8px; }
25
+ }
@@ -0,0 +1,384 @@
1
+ // The resource view: renders admin_resource_list and admin_resource_show results, and, when the
2
+ // host proxies tool calls, pages, sorts, searches and follows associations in place.
3
+ (function () {
4
+ 'use strict';
5
+
6
+ var A = window.AdministrateMCP;
7
+ var h = A.h;
8
+ var HINTS_KEY = 'administrate-mcp/view';
9
+ var LIST_TOOL = 'admin_resource_list';
10
+ var SHOW_TOOL = 'admin_resource_show';
11
+ var IMAGE_PATH = /\.(png|jpe?g|gif|webp|avif|svg)(\?|#|$)/i;
12
+ var ISO_DATE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/;
13
+
14
+ var stack = [];
15
+ var input = {};
16
+
17
+ function current() {
18
+ return stack[stack.length - 1];
19
+ }
20
+
21
+ function humanize(name) {
22
+ var text = String(name).replace(/_id$/, '').replace(/[_/]+/g, ' ').trim();
23
+ return text.charAt(0).toUpperCase() + text.slice(1);
24
+ }
25
+
26
+ function resourceForType(type) {
27
+ return String(type)
28
+ .replace(/::/g, '/')
29
+ .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
30
+ .replace(/([a-z\d])([A-Z])/g, '$1_$2')
31
+ .toLowerCase();
32
+ }
33
+
34
+ function screenFrom(result, args) {
35
+ var hints = A.resultMeta(result, HINTS_KEY) || {};
36
+ var screen = {
37
+ args: Object.assign({}, args),
38
+ resource: hints.resource || args.resource,
39
+ fields: hints.fields || {},
40
+ error: A.resultError(result)
41
+ };
42
+ if (screen.error) return Object.assign(screen, { kind: 'error' });
43
+
44
+ var data = A.resultData(result);
45
+ screen.data = data;
46
+ if (data && data.status === 'ok' && data.action && data.model) {
47
+ screen.kind = 'action';
48
+ screen.resource = data.model;
49
+ } else if (data && Array.isArray(data.columns) && Array.isArray(data.rows)) {
50
+ screen.kind = data.meta && data.meta.page ? 'list' : 'batch';
51
+ } else if (data && typeof data === 'object') {
52
+ screen.kind = 'show';
53
+ } else {
54
+ screen.kind = 'error';
55
+ screen.error = typeof data === 'string' ? data : 'Nothing to show.';
56
+ }
57
+ return screen;
58
+ }
59
+
60
+ function describe(screen) {
61
+ if (screen.kind === 'action') return 'the result of ' + screen.data.action + ' on ' + screen.resource;
62
+ if (screen.kind === 'show') return 'the ' + screen.resource + ' record with id ' + JSON.stringify(String(screen.data.id));
63
+ if (screen.kind === 'list') {
64
+ var meta = screen.data.meta;
65
+ return 'page ' + meta.page + ' of ' + meta.total_pages + ' of the ' + screen.resource + ' list (' +
66
+ meta.total_count + ' records' + (screen.args.query ? ', filtered by a search' : '') + ')';
67
+ }
68
+ return 'the ' + screen.resource + ' records';
69
+ }
70
+
71
+ function load(tool, args, mode) {
72
+ var previous = current();
73
+ if (previous) previous.loading = true;
74
+ render();
75
+ return A.callTool(tool, args).then(function (result) {
76
+ var screen = screenFrom(result, args);
77
+ if (mode === 'replace') stack[stack.length - 1] = screen;
78
+ else stack.push(screen);
79
+ render();
80
+ A.updateModelContext('The user is viewing ' + describe(screen) + ' in the admin view.');
81
+ }).catch(function (error) {
82
+ if (previous) {
83
+ previous.loading = false;
84
+ previous.notice = error.message;
85
+ }
86
+ render();
87
+ });
88
+ }
89
+
90
+ function openShow(resource, id) {
91
+ load(SHOW_TOOL, { resource: resource, id: String(id) }, 'push');
92
+ }
93
+
94
+ function openList(resource, filters) {
95
+ load(LIST_TOOL, { resource: resource, filters: filters }, 'push');
96
+ }
97
+
98
+ function relist(changes) {
99
+ var args = Object.assign({}, current().args, changes);
100
+ load(LIST_TOOL, args, 'replace');
101
+ }
102
+
103
+ function recordLabel(screen) {
104
+ var data = screen.data || {};
105
+ return data.name || data.title || data.display_name || data.label || data.email || data.slug ||
106
+ (data.id !== undefined ? '#' + data.id : '');
107
+ }
108
+
109
+ function empty() {
110
+ return h('span', { className: 'am-muted' }, '—');
111
+ }
112
+
113
+ function json(value, compact) {
114
+ var size = Array.isArray(value) ? value.length + ' items' : Object.keys(value).length + ' keys';
115
+ if (compact) return h('span', { className: 'am-muted' }, size);
116
+ return h('details', { className: 'am-json' }, h('summary', null, size), h('pre', null, JSON.stringify(value, null, 2)));
117
+ }
118
+
119
+ function attachment(url, compact) {
120
+ if (!A.isSafeUrl(url)) return document.createTextNode(String(url));
121
+ if (!IMAGE_PATH.test(url)) return A.link(url, compact ? 'File' : url);
122
+ var image = h('img', { src: url, alt: '', className: compact ? 'am-thumb' : 'am-image', loading: 'lazy' });
123
+ image.addEventListener('error', function () {
124
+ image.replaceWith(A.link(url, compact ? 'Image' : url));
125
+ });
126
+ return h('a', {
127
+ href: url,
128
+ onClick: function (event) {
129
+ event.preventDefault();
130
+ A.openLink(url);
131
+ }
132
+ }, image);
133
+ }
134
+
135
+ function related(value, hint) {
136
+ var label = value.display || value.slug || '#' + value.id;
137
+ var resource = (hint && hint.resource) || (value.type && resourceForType(value.type));
138
+ if (!resource || !A.canCallTools()) return document.createTextNode(label);
139
+ return h('button', {
140
+ className: 'am-link',
141
+ title: 'Open ' + resource + ' ' + value.id,
142
+ onClick: function (event) {
143
+ event.stopPropagation();
144
+ openShow(resource, value.id);
145
+ }
146
+ }, label);
147
+ }
148
+
149
+ function hasMany(value, hint, name, owner, compact) {
150
+ var count = value.count;
151
+ var label = count + ' ' + humanize(name).toLowerCase();
152
+ var canFollow = hint && hint.resource && hint.foreign_key && owner && owner.id !== undefined && A.canCallTools();
153
+ var summary = canFollow && count > 0
154
+ ? h('button', {
155
+ className: 'am-link',
156
+ onClick: function (event) {
157
+ event.stopPropagation();
158
+ var filters = {};
159
+ filters[hint.foreign_key] = owner.id;
160
+ openList(hint.resource, filters);
161
+ }
162
+ }, label)
163
+ : h('span', null, label);
164
+ if (compact || !Array.isArray(value.items) || value.items.length === 0) return summary;
165
+
166
+ var columns = Object.keys(value.items[0]).filter(function (key) { return key !== 'url'; });
167
+ var rows = value.items.map(function (item) { return columns.map(function (key) { return item[key]; }); });
168
+ var shown = value.items.length < count ? h('p', { className: 'am-muted' }, 'Showing ' + value.items.length + ' of ' + count + '.') : null;
169
+ return h('div', null, summary, table(columns, rows, {}, {
170
+ resource: hint && hint.resource,
171
+ urls: value.items.map(function (item) { return item.url; })
172
+ }), shown);
173
+ }
174
+
175
+ function isRelated(value) {
176
+ return Object.keys(value).every(function (key) { return ['id', 'display', 'slug', 'type'].indexOf(key) >= 0; });
177
+ }
178
+
179
+ function renderValue(value, hint, name, owner, compact) {
180
+ var kind = hint && hint.kind;
181
+ if (value === null || value === undefined || value === '') return empty();
182
+ if (typeof value === 'boolean') return h('span', { className: 'am-badge am-' + value }, value ? 'Yes' : 'No');
183
+ if (typeof value === 'number') return document.createTextNode(value.toLocaleString());
184
+ if (typeof value === 'object' && !Array.isArray(value)) {
185
+ if (kind === 'has_many' || (value.count !== undefined && Object.keys(value).every(function (key) { return key === 'count' || key === 'items'; }))) {
186
+ return hasMany(value, hint, name, owner, compact);
187
+ }
188
+ if (value.id !== undefined && (kind === 'belongs_to' || kind === 'polymorphic' || isRelated(value))) {
189
+ return related(value, hint);
190
+ }
191
+ return json(value, compact);
192
+ }
193
+ if (Array.isArray(value)) return json(value, compact);
194
+ if (typeof value === 'string' && kind === 'attachment') return attachment(value, compact);
195
+ if (typeof value === 'string' && (kind === 'datetime' || ISO_DATE.test(value))) {
196
+ return h('time', { datetime: value, title: value }, A.formatDate(value));
197
+ }
198
+ if (kind === 'enum') return h('span', { className: 'am-badge' }, String(value));
199
+ if (typeof value === 'string' && kind === 'url' && A.isSafeUrl(value)) return A.link(value, value);
200
+ return document.createTextNode(String(value));
201
+ }
202
+
203
+ function table(columns, rows, fields, options) {
204
+ var idIndex = columns.indexOf('id');
205
+ var sort = options.sort;
206
+ var head = h('tr', null, columns.map(function (column) {
207
+ var label = humanize(column);
208
+ if (!options.onSort) return h('th', { scope: 'col' }, label);
209
+ var arrow = sort && sort.field === column ? (sort.direction === 'asc' ? ' ▲' : ' ▼') : '';
210
+ return h('th', { scope: 'col' }, h('button', {
211
+ className: 'am-link',
212
+ title: 'Sort by ' + label,
213
+ onClick: function () { options.onSort(column); }
214
+ }, label + arrow));
215
+ }));
216
+
217
+ var body = rows.map(function (row, index) {
218
+ if (!Array.isArray(row)) {
219
+ return h('tr', null, h('td', { colspan: columns.length, className: 'am-error' }, (row && row.error) || 'Not found'));
220
+ }
221
+ var owner = idIndex >= 0 ? { id: row[idIndex] } : null;
222
+ var url = options.urls && options.urls[index];
223
+ var target = options.resource && owner && A.canCallTools()
224
+ ? function () { openShow(options.resource, owner.id); }
225
+ : (A.isSafeUrl(url) ? function () { A.openLink(url); } : null);
226
+ return h('tr', { className: target ? 'am-clickable' : null, onClick: target },
227
+ row.map(function (value, column) {
228
+ return h('td', null, renderValue(value, fields[columns[column]], columns[column], owner, true));
229
+ }));
230
+ });
231
+
232
+ return h('div', { className: 'am-table-wrap' },
233
+ h('table', { className: 'am-table' }, h('thead', null, head), h('tbody', null, body)));
234
+ }
235
+
236
+ function backButton() {
237
+ if (stack.length < 2) return null;
238
+ return h('button', {
239
+ title: 'Back',
240
+ onClick: function () {
241
+ stack.pop();
242
+ render();
243
+ A.updateModelContext('The user is viewing ' + describe(current()) + ' in the admin view.');
244
+ }
245
+ }, '←');
246
+ }
247
+
248
+ function toolbar(screen, title, extra, kicker) {
249
+ return h('div', { className: 'am-toolbar' },
250
+ backButton(),
251
+ kicker ? h('span', { className: 'am-badge' }, kicker) : null,
252
+ h('h1', null, title),
253
+ extra,
254
+ screen.loading ? h('span', { className: 'am-muted' }, 'Loading…') : null);
255
+ }
256
+
257
+ function splitRows(data) {
258
+ var urlIndex = data.columns.indexOf('url');
259
+ var columns = data.columns.filter(function (_, index) { return index !== urlIndex; });
260
+ var urls = [];
261
+ var rows = data.rows.map(function (row) {
262
+ if (!Array.isArray(row)) return row;
263
+ urls.push(urlIndex >= 0 ? row[urlIndex] : null);
264
+ return row.filter(function (_, index) { return index !== urlIndex; });
265
+ });
266
+ return { columns: columns, rows: rows, urls: urls };
267
+ }
268
+
269
+ function renderList(screen) {
270
+ var data = screen.data;
271
+ var meta = data.meta || {};
272
+ var split = splitRows(data);
273
+ var interactive = screen.kind === 'list' && A.canCallTools();
274
+ var sort = screen.args.sort ? { field: screen.args.sort, direction: screen.args.sort_direction || 'desc' } : null;
275
+
276
+ var search = interactive ? h('form', {
277
+ onSubmit: function (event) {
278
+ event.preventDefault();
279
+ relist({ query: event.target.elements.query.value || undefined, page: 1 });
280
+ }
281
+ }, h('input', { type: 'search', name: 'query', placeholder: 'Search', value: screen.args.query || '' })) : null;
282
+
283
+ var count = screen.kind === 'list'
284
+ ? h('span', { className: 'am-muted' }, meta.total_count + ' record' + (meta.total_count === 1 ? '' : 's'))
285
+ : null;
286
+
287
+ var pager = screen.kind === 'list' && meta.total_pages > 1 ? h('div', { className: 'am-toolbar am-pager' },
288
+ h('button', { disabled: !interactive || meta.page <= 1, onClick: function () { relist({ page: meta.page - 1 }); } }, 'Previous'),
289
+ h('span', { className: 'am-muted' }, 'Page ' + meta.page + ' of ' + meta.total_pages),
290
+ h('button', { disabled: !interactive || meta.page >= meta.total_pages, onClick: function () { relist({ page: meta.page + 1 }); } }, 'Next')
291
+ ) : null;
292
+
293
+ var body = split.rows.length === 0
294
+ ? A.message('No records match.')
295
+ : table(split.columns, split.rows, screen.fields, {
296
+ resource: screen.resource,
297
+ urls: split.urls,
298
+ sort: sort,
299
+ onSort: interactive ? function (field) {
300
+ var direction = sort && sort.field === field && sort.direction === 'desc' ? 'asc' : 'desc';
301
+ relist({ sort: field, sort_direction: direction, page: 1 });
302
+ } : null
303
+ });
304
+
305
+ return h('div', null, toolbar(screen, humanize(screen.resource || 'Records'), [count, h('span', { className: 'am-spacer' }), search]), body, pager);
306
+ }
307
+
308
+ function renderShow(screen) {
309
+ var data = screen.data;
310
+ var open = A.isSafeUrl(data.url) ? h('button', { onClick: function () { A.openLink(data.url); } }, 'Open in admin') : null;
311
+ var keys = Object.keys(data).filter(function (key) { return key !== 'url'; });
312
+ var fields = h('dl', { className: 'am-fields' }, keys.map(function (key) {
313
+ return [
314
+ h('dt', null, humanize(key)),
315
+ h('dd', null, renderValue(data[key], screen.fields[key], key, data, false))
316
+ ];
317
+ }));
318
+ var resource = humanize(screen.resource || 'Record');
319
+ return h('div', null, toolbar(screen, recordLabel(screen) || resource, [h('span', { className: 'am-spacer' }), open], resource), fields);
320
+ }
321
+
322
+ function loadActionRecord(screen) {
323
+ screen.recordRequested = true;
324
+ A.callTool(SHOW_TOOL, { resource: screen.resource, id: String(screen.data.id) }).then(function (result) {
325
+ screen.record = screenFrom(result, { resource: screen.resource, id: String(screen.data.id) });
326
+ render();
327
+ }).catch(function () {
328
+ screen.record = null;
329
+ });
330
+ }
331
+
332
+ function renderAction(screen) {
333
+ var data = screen.data;
334
+ var extra = data.data && typeof data.data === 'object' && Object.keys(data.data).length
335
+ ? h('div', { className: 'am-action-data' }, h('h2', null, 'Returned data'), json(data.data, false))
336
+ : null;
337
+ var title = humanize(data.action) + ' — done';
338
+ var view = h('div', null, toolbar(screen, title, [h('span', { className: 'am-spacer' }), h('span', { className: 'am-badge am-true' }, 'OK')], humanize(screen.resource)), extra);
339
+ if (data.id !== undefined && A.canCallTools() && !screen.recordRequested) loadActionRecord(screen);
340
+ if (screen.record && screen.record.kind === 'show') {
341
+ var record = renderShow(screen.record);
342
+ record.classList.add('am-action-record');
343
+ view.appendChild(record);
344
+ } else if (data.id !== undefined && !A.canCallTools()) {
345
+ view.appendChild(A.message(humanize(screen.resource) + ' ' + data.id));
346
+ }
347
+ return view;
348
+ }
349
+
350
+ function render() {
351
+ var screen = current();
352
+ if (!screen) return;
353
+ var view;
354
+ if (screen.kind === 'action') {
355
+ view = renderAction(screen);
356
+ } else if (screen.kind === 'error') {
357
+ view = h('div', null, toolbar(screen, humanize(screen.resource || A.toolName() || 'Admin'), null), A.message(screen.error, 'am-error'));
358
+ } else if (screen.kind === 'show') {
359
+ view = renderShow(screen);
360
+ } else {
361
+ view = renderList(screen);
362
+ }
363
+ if (screen.notice) view.appendChild(A.message(screen.notice, 'am-error'));
364
+ screen.notice = null;
365
+ A.mount(view);
366
+ }
367
+
368
+ A.on('input', function (args) {
369
+ input = args || {};
370
+ });
371
+
372
+ A.on('result', function (result) {
373
+ stack = [screenFrom(result, input)];
374
+ render();
375
+ });
376
+
377
+ A.on('cancelled', function () {
378
+ if (!stack.length) A.mount(A.message('The tool call was cancelled.'));
379
+ });
380
+
381
+ A.connect().catch(function (error) {
382
+ A.mount(A.message('Could not connect to the host: ' + error.message, 'am-error'));
383
+ });
384
+ })();
@@ -0,0 +1,112 @@
1
+ :root {
2
+ color-scheme: light dark;
3
+ --am-bg: var(--color-background-primary, light-dark(#ffffff, #1b1b1b));
4
+ --am-bg-alt: var(--color-background-secondary, light-dark(#f6f7f9, #242424));
5
+ --am-bg-hover: var(--color-background-tertiary, light-dark(#eef0f3, #2e2e2e));
6
+ --am-text: var(--color-text-primary, light-dark(#1d2025, #ececec));
7
+ --am-text-muted: var(--color-text-secondary, light-dark(#5d6470, #a3a3a3));
8
+ --am-border: var(--color-border-primary, light-dark(#dde1e6, #3a3a3a));
9
+ --am-accent: var(--color-text-info, light-dark(#1f5fbf, #7aaaf0));
10
+ --am-danger: var(--color-text-danger, light-dark(#b42318, #f08a80));
11
+ --am-success: var(--color-text-success, light-dark(#1a7f37, #6fcf8a));
12
+ --am-warning: var(--color-text-warning, light-dark(#9a6700, #e3b341));
13
+ --am-radius: var(--border-radius-md, 8px);
14
+ --am-font: var(--font-sans, system-ui, -apple-system, 'Segoe UI', sans-serif);
15
+ --am-mono: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
16
+ }
17
+
18
+ :root[data-theme='light'] { color-scheme: light; }
19
+ :root[data-theme='dark'] { color-scheme: dark; }
20
+
21
+ * { box-sizing: border-box; }
22
+
23
+ html, body {
24
+ margin: 0;
25
+ background: var(--am-bg);
26
+ color: var(--am-text);
27
+ font: 14px/1.45 var(--am-font);
28
+ }
29
+
30
+ main { padding: 12px 14px; overflow: auto; }
31
+
32
+ a { color: var(--am-accent); text-decoration: none; cursor: pointer; }
33
+ a:hover { text-decoration: underline; }
34
+
35
+ button {
36
+ font: inherit;
37
+ color: inherit;
38
+ background: var(--am-bg-alt);
39
+ border: 1px solid var(--am-border);
40
+ border-radius: var(--am-radius);
41
+ padding: 4px 10px;
42
+ cursor: pointer;
43
+ }
44
+ button:hover:not(:disabled) { background: var(--am-bg-hover); }
45
+ button:disabled { opacity: 0.5; cursor: default; }
46
+ button.am-link { background: none; border: 0; padding: 0; color: var(--am-accent); text-align: left; }
47
+ button.am-link:hover { text-decoration: underline; background: none; }
48
+
49
+ input[type='search'] {
50
+ font: inherit;
51
+ color: inherit;
52
+ background: var(--am-bg);
53
+ border: 1px solid var(--am-border);
54
+ border-radius: var(--am-radius);
55
+ padding: 4px 8px;
56
+ min-width: 0;
57
+ }
58
+
59
+ .am-muted { color: var(--am-text-muted); }
60
+ .am-error { color: var(--am-danger); white-space: pre-wrap; }
61
+ .am-mono { font-family: var(--am-mono); font-size: 12.5px; }
62
+
63
+ .am-toolbar {
64
+ display: flex;
65
+ flex-wrap: wrap;
66
+ align-items: center;
67
+ gap: 8px;
68
+ margin-bottom: 10px;
69
+ }
70
+ .am-toolbar h1 { font-size: 15px; font-weight: 600; margin: 0; }
71
+ h2 { font-size: 13px; font-weight: 600; margin: 14px 0 6px; }
72
+ .am-spacer { flex: 1; }
73
+
74
+ .am-badge {
75
+ display: inline-block;
76
+ padding: 0 7px;
77
+ border-radius: 999px;
78
+ font-size: 12px;
79
+ border: 1px solid var(--am-border);
80
+ background: var(--am-bg-alt);
81
+ white-space: nowrap;
82
+ }
83
+ .am-badge.am-true { color: var(--am-success); }
84
+ .am-badge.am-false { color: var(--am-text-muted); }
85
+
86
+ .am-table-wrap { overflow-x: auto; border: 1px solid var(--am-border); border-radius: var(--am-radius); }
87
+ table.am-table { border-collapse: collapse; width: 100%; }
88
+ .am-table th, .am-table td {
89
+ text-align: left;
90
+ vertical-align: top;
91
+ padding: 6px 10px;
92
+ border-bottom: 1px solid var(--am-border);
93
+ max-width: 320px;
94
+ overflow: hidden;
95
+ text-overflow: ellipsis;
96
+ white-space: nowrap;
97
+ }
98
+ .am-table th { background: var(--am-bg-alt); font-weight: 600; white-space: nowrap; position: sticky; top: 0; }
99
+ .am-table tr:last-child td { border-bottom: 0; }
100
+ .am-table tbody tr.am-clickable { cursor: pointer; }
101
+ .am-table tbody tr.am-clickable:hover { background: var(--am-bg-hover); }
102
+
103
+ details.am-json summary { cursor: pointer; color: var(--am-text-muted); }
104
+ details.am-json pre {
105
+ margin: 4px 0 0;
106
+ padding: 8px;
107
+ background: var(--am-bg-alt);
108
+ border-radius: var(--am-radius);
109
+ overflow: auto;
110
+ max-height: 280px;
111
+ font: 12px/1.4 var(--am-mono);
112
+ }