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,296 @@
1
+ // The runtime every administrate-mcp tool view shares: the MCP Apps (SEP-1865) postMessage
2
+ // bridge to the host, theming from the host context, automatic resizing, and DOM helpers that
3
+ // only ever set text, never markup, because what they render is admin data.
4
+ (function () {
5
+ 'use strict';
6
+
7
+ var PROTOCOL_VERSION = '2026-01-26';
8
+ var URL_ATTRIBUTES = ['href', 'src', 'srcset', 'action', 'formaction', 'xlink:href', 'poster'];
9
+ var nextId = 1;
10
+ var pending = {};
11
+ var listeners = {};
12
+ var state = { hostContext: {}, hostCapabilities: {}, toolInput: null, toolResult: null };
13
+
14
+ function post(message) {
15
+ window.parent.postMessage(Object.assign({ jsonrpc: '2.0' }, message), '*');
16
+ }
17
+
18
+ function request(method, params) {
19
+ var id = nextId++;
20
+ return new Promise(function (resolve, reject) {
21
+ pending[id] = { resolve: resolve, reject: reject };
22
+ post({ id: id, method: method, params: params || {} });
23
+ });
24
+ }
25
+
26
+ function notify(method, params) {
27
+ post({ method: method, params: params || {} });
28
+ }
29
+
30
+ function emit(event, payload) {
31
+ (listeners[event] || []).forEach(function (callback) {
32
+ try {
33
+ callback(payload);
34
+ } catch (error) {
35
+ console.error(error);
36
+ }
37
+ });
38
+ }
39
+
40
+ function on(event, callback) {
41
+ (listeners[event] = listeners[event] || []).push(callback);
42
+ if (event === 'input' && state.toolInput) callback(state.toolInput);
43
+ if (event === 'result' && state.toolResult) callback(state.toolResult);
44
+ }
45
+
46
+ function respond(id, result) {
47
+ post({ id: id, result: result || {} });
48
+ }
49
+
50
+ function handleRequest(message) {
51
+ if (message.method === 'ui/resource-teardown') {
52
+ emit('teardown', message.params);
53
+ respond(message.id);
54
+ } else if (message.method === 'ping') {
55
+ respond(message.id);
56
+ } else {
57
+ post({ id: message.id, error: { code: -32601, message: 'Method not found' } });
58
+ }
59
+ }
60
+
61
+ function handleNotification(message) {
62
+ var params = message.params || {};
63
+ switch (message.method) {
64
+ case 'ui/notifications/tool-input':
65
+ state.toolInput = params.arguments || {};
66
+ emit('input', state.toolInput);
67
+ break;
68
+ case 'ui/notifications/tool-result':
69
+ state.toolResult = params;
70
+ emit('result', params);
71
+ break;
72
+ case 'ui/notifications/tool-cancelled':
73
+ emit('cancelled', params);
74
+ break;
75
+ case 'ui/notifications/host-context-changed':
76
+ applyHostContext(Object.assign(state.hostContext, params));
77
+ break;
78
+ default:
79
+ break;
80
+ }
81
+ }
82
+
83
+ window.addEventListener('message', function (event) {
84
+ if (event.source !== window.parent) return;
85
+ var message = event.data;
86
+ if (!message || message.jsonrpc !== '2.0') return;
87
+
88
+ if (message.id !== undefined && !message.method) {
89
+ var waiter = pending[message.id];
90
+ if (!waiter) return;
91
+ delete pending[message.id];
92
+ if (message.error) waiter.reject(new Error(message.error.message || 'Request failed'));
93
+ else waiter.resolve(message.result);
94
+ } else if (message.id !== undefined) {
95
+ handleRequest(message);
96
+ } else if (message.method) {
97
+ handleNotification(message);
98
+ }
99
+ });
100
+
101
+ function applyHostContext(context) {
102
+ var root = document.documentElement;
103
+ if (context.theme) root.dataset.theme = context.theme;
104
+ var variables = context.styles && context.styles.variables;
105
+ if (variables) {
106
+ Object.keys(variables).forEach(function (name) {
107
+ if (variables[name]) root.style.setProperty(name, variables[name]);
108
+ });
109
+ }
110
+ var dimensions = context.containerDimensions;
111
+ if (dimensions && dimensions.maxHeight) root.style.maxHeight = dimensions.maxHeight + 'px';
112
+ emit('context', context);
113
+ }
114
+
115
+ function watchSize() {
116
+ var last = '';
117
+ var report = function () {
118
+ var box = document.body.getBoundingClientRect();
119
+ var width = Math.ceil(Math.max(box.width, document.body.scrollWidth));
120
+ var height = Math.ceil(box.height);
121
+ var key = width + 'x' + height;
122
+ if (key === last) return;
123
+ last = key;
124
+ notify('ui/notifications/size-changed', { width: width, height: height });
125
+ };
126
+ if (window.ResizeObserver) new ResizeObserver(report).observe(document.body);
127
+ report();
128
+ }
129
+
130
+ function connect() {
131
+ var app = { name: 'administrate-mcp', version: '1' };
132
+ return request('ui/initialize', {
133
+ protocolVersion: PROTOCOL_VERSION,
134
+ appInfo: app,
135
+ clientInfo: app,
136
+ capabilities: {},
137
+ appCapabilities: { availableDisplayModes: ['inline', 'fullscreen'] }
138
+ }).then(function (result) {
139
+ state.hostCapabilities = (result && result.hostCapabilities) || {};
140
+ state.hostContext = (result && result.hostContext) || {};
141
+ applyHostContext(state.hostContext);
142
+ notify('ui/notifications/initialized');
143
+ watchSize();
144
+ return result;
145
+ });
146
+ }
147
+
148
+ function resultData(result) {
149
+ if (!result) return null;
150
+ if (result.structuredContent) return result.structuredContent;
151
+ var texts = (result.content || []).filter(function (block) { return block.type === 'text'; });
152
+ for (var i = texts.length - 1; i >= 0; i--) {
153
+ try {
154
+ return JSON.parse(texts[i].text);
155
+ } catch (_error) {
156
+ continue;
157
+ }
158
+ }
159
+ return texts.length ? texts[0].text : null;
160
+ }
161
+
162
+ function resultError(result) {
163
+ if (!result || !result.isError) return null;
164
+ var text = (result.content || []).find(function (block) { return block.type === 'text'; });
165
+ return text ? text.text : 'The tool call failed.';
166
+ }
167
+
168
+ function resultMeta(result, key) {
169
+ return (result && result._meta && result._meta[key]) || null;
170
+ }
171
+
172
+ function toolName() {
173
+ var info = state.hostContext.toolInfo;
174
+ return (info && info.tool && info.tool.name) || null;
175
+ }
176
+
177
+ function canCallTools() {
178
+ return Boolean(state.hostCapabilities.serverTools);
179
+ }
180
+
181
+ function callTool(name, args) {
182
+ return request('tools/call', { name: name, arguments: args || {} });
183
+ }
184
+
185
+ function openLink(url) {
186
+ if (!isSafeUrl(url)) return Promise.reject(new Error('Only http and https links can be opened.'));
187
+ return request('ui/open-link', { url: url }).catch(function () {
188
+ window.open(url, '_blank', 'noopener');
189
+ });
190
+ }
191
+
192
+ function sendMessage(text) {
193
+ return request('ui/message', { role: 'user', content: { type: 'text', text: text } });
194
+ }
195
+
196
+ function updateModelContext(text) {
197
+ return request('ui/update-model-context', { content: [{ type: 'text', text: text }] }).catch(function () {});
198
+ }
199
+
200
+ function requestDisplayMode(mode) {
201
+ return request('ui/request-display-mode', { mode: mode });
202
+ }
203
+
204
+ function h(tag, attributes) {
205
+ var element = document.createElement(tag);
206
+ var attrs = attributes || {};
207
+ Object.keys(attrs).forEach(function (key) {
208
+ var value = attrs[key];
209
+ if (value === null || value === undefined || value === false) return;
210
+ if (key.indexOf('on') === 0) {
211
+ if (typeof value === 'function') element.addEventListener(key.slice(2).toLowerCase(), value);
212
+ } else if (URL_ATTRIBUTES.indexOf(key.toLowerCase()) >= 0 && !isSafeUrl(value)) {
213
+ return;
214
+ } else if (key === 'className') {
215
+ element.className = value;
216
+ } else if (key === 'dataset') {
217
+ Object.assign(element.dataset, value);
218
+ } else {
219
+ element.setAttribute(key, value === true ? '' : String(value));
220
+ }
221
+ });
222
+ for (var i = 2; i < arguments.length; i++) append(element, arguments[i]);
223
+ return element;
224
+ }
225
+
226
+ function append(parent, child) {
227
+ if (child === null || child === undefined || child === false) return;
228
+ if (Array.isArray(child)) {
229
+ child.forEach(function (item) { append(parent, item); });
230
+ } else if (child instanceof Node) {
231
+ parent.appendChild(child);
232
+ } else {
233
+ parent.appendChild(document.createTextNode(String(child)));
234
+ }
235
+ }
236
+
237
+ function mount(node) {
238
+ var root = document.getElementById('app');
239
+ root.replaceChildren(node);
240
+ }
241
+
242
+ function isSafeUrl(value) {
243
+ return typeof value === 'string' && /^https?:\/\//i.test(value);
244
+ }
245
+
246
+ function link(url, label) {
247
+ if (!isSafeUrl(url)) return document.createTextNode(label || '');
248
+ return h('a', {
249
+ href: url,
250
+ onClick: function (event) {
251
+ event.preventDefault();
252
+ openLink(url);
253
+ }
254
+ }, label || url);
255
+ }
256
+
257
+ function formatDate(value) {
258
+ var date = new Date(value);
259
+ if (isNaN(date.getTime())) return String(value);
260
+ try {
261
+ return date.toLocaleString(state.hostContext.locale || undefined, {
262
+ dateStyle: 'medium',
263
+ timeStyle: 'short',
264
+ timeZone: state.hostContext.timeZone || undefined
265
+ });
266
+ } catch (_error) {
267
+ return date.toLocaleString();
268
+ }
269
+ }
270
+
271
+ function message(text, className) {
272
+ return h('p', { className: className || 'am-muted' }, text);
273
+ }
274
+
275
+ window.AdministrateMCP = {
276
+ state: state,
277
+ connect: connect,
278
+ on: on,
279
+ callTool: callTool,
280
+ canCallTools: canCallTools,
281
+ toolName: toolName,
282
+ openLink: openLink,
283
+ sendMessage: sendMessage,
284
+ updateModelContext: updateModelContext,
285
+ requestDisplayMode: requestDisplayMode,
286
+ resultData: resultData,
287
+ resultError: resultError,
288
+ resultMeta: resultMeta,
289
+ h: h,
290
+ mount: mount,
291
+ link: link,
292
+ isSafeUrl: isSafeUrl,
293
+ formatDate: formatDate,
294
+ message: message
295
+ };
296
+ })();
@@ -0,0 +1,6 @@
1
+ .am-bars { display: grid; gap: 4px; }
2
+ .am-bar-row { display: grid; grid-template-columns: minmax(120px, 40%) 1fr auto; align-items: center; gap: 8px; }
3
+ .am-bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
4
+ .am-bar-track { background: var(--am-bg-alt); border-radius: 999px; height: 8px; overflow: hidden; }
5
+ .am-bar { display: block; height: 100%; background: var(--am-accent); border-radius: 999px; }
6
+ .am-bar-value { font-variant-numeric: tabular-nums; }
@@ -0,0 +1,108 @@
1
+ // The Sidekiq view: renders sidekiq_stats as counts per job class and sidekiq_retries as the
2
+ // retry queue, and re-runs the call in place when the host proxies tool calls.
3
+ (function () {
4
+ 'use strict';
5
+
6
+ var A = window.AdministrateMCP;
7
+ var h = A.h;
8
+ var input = {};
9
+ var screen = null;
10
+
11
+ function toolFor(data) {
12
+ return A.toolName() || (data && data.total_size !== undefined ? 'sidekiq_retries' : 'sidekiq_stats');
13
+ }
14
+
15
+ function rerun(changes) {
16
+ var args = Object.assign({}, screen.args, changes);
17
+ screen.loading = true;
18
+ render();
19
+ A.callTool(screen.tool, args).then(function (result) {
20
+ show(result, args);
21
+ }).catch(function (error) {
22
+ screen.loading = false;
23
+ screen.notice = error.message;
24
+ render();
25
+ });
26
+ }
27
+
28
+ function show(result, args) {
29
+ var data = A.resultData(result);
30
+ screen = { args: args || {}, data: data, error: A.resultError(result), tool: toolFor(data) };
31
+ render();
32
+ }
33
+
34
+ function bars(counts) {
35
+ var entries = Object.keys(counts || {}).map(function (name) { return [name, counts[name]]; });
36
+ if (entries.length === 0) return A.message('Nothing queued.');
37
+ var max = Math.max.apply(null, entries.map(function (entry) { return entry[1]; })) || 1;
38
+ return h('div', { className: 'am-bars' }, entries.map(function (entry) {
39
+ return h('div', { className: 'am-bar-row' },
40
+ h('span', { className: 'am-bar-label am-mono', title: entry[0] }, entry[0]),
41
+ h('span', { className: 'am-bar-track' }, h('span', { className: 'am-bar', style: 'width:' + Math.max(2, (entry[1] / max) * 100) + '%' })),
42
+ h('span', { className: 'am-bar-value' }, entry[1].toLocaleString()));
43
+ }));
44
+ }
45
+
46
+ function retriesTable(entries) {
47
+ if (!entries.length) return A.message('The retry queue is empty.');
48
+ var head = ['Job', 'Error', 'Retries', 'Failed', 'Next retry'];
49
+ return h('div', { className: 'am-table-wrap' }, h('table', { className: 'am-table' },
50
+ h('thead', null, h('tr', null, head.map(function (label) { return h('th', { scope: 'col' }, label); }))),
51
+ h('tbody', null, entries.map(function (entry) {
52
+ return h('tr', null,
53
+ h('td', null, h('div', { className: 'am-mono' }, entry.class), h('div', { className: 'am-muted' }, entry.queue)),
54
+ h('td', null, h('div', null, entry.error_class), h('div', { className: 'am-muted' }, entry.error_message)),
55
+ h('td', null, String(entry.retry_count === undefined ? '' : entry.retry_count)),
56
+ h('td', null, entry.failed_at ? A.formatDate(entry.failed_at) : ''),
57
+ h('td', null, entry.next_retry_at ? A.formatDate(entry.next_retry_at) : ''));
58
+ }))));
59
+ }
60
+
61
+ function statsView(data) {
62
+ var parts = [
63
+ h('h2', null, 'Retries by job class ', h('span', { className: 'am-muted' }, '(' + (data.total_retries || 0).toLocaleString() + ')')),
64
+ bars(data.retries)
65
+ ];
66
+ if (data.queues) {
67
+ Object.keys(data.queues).forEach(function (queue) {
68
+ var entry = data.queues[queue];
69
+ parts.push(h('h2', null, queue + ' ', h('span', { className: 'am-muted' }, '(' + (entry.total || 0).toLocaleString() + ')')));
70
+ parts.push(bars(entry.counts));
71
+ });
72
+ } else if (A.canCallTools()) {
73
+ parts.push(h('button', { onClick: function () { rerun({ include_queues: true }); } }, 'Show queues'));
74
+ }
75
+ if (data.stats_cleared_at) parts.push(A.message('Counting since ' + A.formatDate(data.stats_cleared_at) + '.'));
76
+ return parts;
77
+ }
78
+
79
+ function retriesView(data) {
80
+ var parts = [h('p', null, h('strong', null, (data.total_size || 0).toLocaleString()), ' jobs waiting to retry.')];
81
+ if (data.entries) parts.push(retriesTable(data.entries));
82
+ else if (A.canCallTools()) parts.push(h('button', { onClick: function () { rerun({ include_entries: true }); } }, 'Show the first jobs'));
83
+ return parts;
84
+ }
85
+
86
+ function render() {
87
+ if (!screen) return;
88
+ var data = screen.data || {};
89
+ var refresh = A.canCallTools() ? h('button', { onClick: function () { rerun({}); }, disabled: screen.loading }, 'Refresh') : null;
90
+ var title = screen.tool === 'sidekiq_retries' ? 'Sidekiq retries' : 'Sidekiq stats';
91
+ var body = screen.error
92
+ ? A.message(screen.error, 'am-error')
93
+ : (screen.tool === 'sidekiq_retries' ? retriesView(data) : statsView(data));
94
+ var view = h('div', null,
95
+ h('div', { className: 'am-toolbar' }, h('h1', null, title), h('span', { className: 'am-spacer' }),
96
+ screen.loading ? h('span', { className: 'am-muted' }, 'Loading…') : null, refresh),
97
+ body,
98
+ screen.notice ? A.message(screen.notice, 'am-error') : null);
99
+ screen.notice = null;
100
+ A.mount(view);
101
+ }
102
+
103
+ A.on('input', function (args) { input = args || {}; });
104
+ A.on('result', function (result) { show(result, input); });
105
+ A.connect().catch(function (error) {
106
+ A.mount(A.message('Could not connect to the host: ' + error.message, 'am-error'));
107
+ });
108
+ })();
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Administrate
4
4
  module MCP
5
- VERSION = '0.5.0'
5
+ VERSION = '0.6.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: administrate-mcp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sorare
@@ -106,6 +106,10 @@ files:
106
106
  - app/lib/administrate/mcp/report_improvement.rb
107
107
  - app/lib/administrate/mcp/resource_resolver.rb
108
108
  - app/lib/administrate/mcp/server_builder.rb
109
+ - app/lib/administrate/mcp/tool_view.rb
110
+ - app/lib/administrate/mcp/tool_views/field_hints.rb
111
+ - app/lib/administrate/mcp/tool_views/resource_view.rb
112
+ - app/lib/administrate/mcp/tool_views/sidekiq_view.rb
109
113
  - app/lib/administrate/mcp/tools/admin_resource_list.rb
110
114
  - app/lib/administrate/mcp/tools/admin_resource_list_resources.rb
111
115
  - app/lib/administrate/mcp/tools/admin_resource_show.rb
@@ -136,6 +140,7 @@ files:
136
140
  - docs/development.md
137
141
  - docs/oauth.md
138
142
  - docs/routes.md
143
+ - docs/tool-views.md
139
144
  - docs/uploads.md
140
145
  - lib/administrate-mcp.rb
141
146
  - lib/administrate/mcp.rb
@@ -150,6 +155,12 @@ files:
150
155
  - lib/administrate/mcp/loopback_uri.rb
151
156
  - lib/administrate/mcp/rack_attack.rb
152
157
  - lib/administrate/mcp/routes.rb
158
+ - lib/administrate/mcp/tool_views/resource.css
159
+ - lib/administrate/mcp/tool_views/resource.js
160
+ - lib/administrate/mcp/tool_views/runtime.css
161
+ - lib/administrate/mcp/tool_views/runtime.js
162
+ - lib/administrate/mcp/tool_views/sidekiq.css
163
+ - lib/administrate/mcp/tool_views/sidekiq.js
153
164
  - lib/administrate/mcp/uploads_configuration.rb
154
165
  - lib/administrate/mcp/version.rb
155
166
  homepage: https://github.com/sorare/administrate-mcp