@softeria/ms-365-mcp-server 0.186.0 → 0.187.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,362 @@
1
+ /* global createMcpAppBridge */
2
+
3
+ (() => {
4
+ 'use strict';
5
+
6
+ // What the view needs from each item when it lists a folder itself
7
+ const SELECT =
8
+ 'id,name,size,lastModifiedDateTime,webUrl,folder,file,package,parentReference,remoteItem';
9
+ // Without these the view can't open folders or files (id always comes back)
10
+ const NEEDED = ['name', 'folder', 'file', 'webUrl'];
11
+ const PAGE_SIZE = 100;
12
+ const FOLDER_ICON =
13
+ 'M2 5.5A1.5 1.5 0 0 1 3.5 4h4.4l1.6 2h7A1.5 1.5 0 0 1 18 7.5v7a1.5 1.5 0 0 1-1.5 1.5h-13A1.5 1.5 0 0 1 2 14.5z';
14
+ const FILE_ICON = 'M5 2.5h6.5L15 6v11.5H5zM11.5 2.5V6H15';
15
+
16
+ let config = {};
17
+ try {
18
+ config = JSON.parse(document.getElementById('mcp-app-config').textContent) || {};
19
+ } catch {
20
+ // Unfilled placeholder: the view can still show the result, just not navigate
21
+ }
22
+
23
+ const els = {
24
+ crumbs: document.getElementById('crumbs'),
25
+ refresh: document.getElementById('refresh'),
26
+ items: document.getElementById('items'),
27
+ more: document.getElementById('more'),
28
+ status: document.getElementById('status'),
29
+ };
30
+
31
+ const state = {
32
+ // Arguments of the tool call the host rendered this view for
33
+ input: undefined,
34
+ // { driveId, id, name } of the folders opened here, outermost first
35
+ crumbs: [],
36
+ items: [],
37
+ nextLink: undefined,
38
+ // The arguments behind the current page; the next page repeats them with skiptoken
39
+ lastArgs: undefined,
40
+ busy: false,
41
+ };
42
+
43
+ const bridge = createMcpAppBridge({
44
+ name: 'ms-365-file-browser',
45
+ onToolInput(args) {
46
+ state.input = args;
47
+ },
48
+ onToolResult: showToolResult,
49
+ onToolCancelled() {
50
+ setStatus('The listing was cancelled.', true);
51
+ },
52
+ onReady() {
53
+ if (state.items.length || state.crumbs.length) render();
54
+ },
55
+ onInitError(error) {
56
+ setStatus('Could not connect to the host: ' + error.message, true);
57
+ },
58
+ });
59
+
60
+ els.refresh.addEventListener('click', refresh);
61
+ els.more.addEventListener('click', loadMore);
62
+
63
+ // A list-folder-style result: { value: [driveItem], @odata.nextLink? }
64
+ function readListing(result) {
65
+ const data = bridge.readToolResult(result, 'The listing failed.');
66
+ if (!isObject(data) || !Array.isArray(data.value)) {
67
+ throw new Error('This result is not a folder listing.');
68
+ }
69
+ return {
70
+ items: data.value.filter(isObject),
71
+ nextLink: str(data['@odata.nextLink']),
72
+ };
73
+ }
74
+
75
+ function showToolResult(result) {
76
+ let listing;
77
+ try {
78
+ listing = readListing(result);
79
+ } catch (error) {
80
+ setStatus(error.message, true);
81
+ updateControls();
82
+ return;
83
+ }
84
+ const input = state.input || {};
85
+ const parent = (listing.items[0] && listing.items[0].parentReference) || {};
86
+ const driveId = str(input.driveId) || str(parent.driveId);
87
+ const id = str(input.driveItemId) || str(parent.id);
88
+ state.crumbs = driveId && id ? [{ driveId, id, name: folderName(parent.path, id) }] : [];
89
+ state.lastArgs = state.input ? pageArgs(state.input) : undefined;
90
+ state.items = listing.items;
91
+ state.nextLink = listing.nextLink;
92
+ render();
93
+ }
94
+
95
+ // parentReference.path is like /drive/root:/Documents/Reports
96
+ function folderName(path, id) {
97
+ if (typeof path === 'string' && path.includes('root:')) {
98
+ const parts = path
99
+ .slice(path.indexOf('root:') + 5)
100
+ .split('/')
101
+ .filter(Boolean);
102
+ return parts.length ? decode(parts[parts.length - 1]) : 'Root';
103
+ }
104
+ return id === 'root' ? 'Root' : 'Folder';
105
+ }
106
+
107
+ function listArgs(driveId, id) {
108
+ const args = { driveId, driveItemId: id, select: SELECT, top: PAGE_SIZE };
109
+ const account = state.input && str(state.input.account);
110
+ if (account) args.account = account;
111
+ return args;
112
+ }
113
+
114
+ async function load(args, crumbs, append) {
115
+ if (state.busy || !canCallTools()) return;
116
+ state.busy = true;
117
+ setStatus('Loading...', false);
118
+ updateControls();
119
+ try {
120
+ const result = await bridge.callTool(config.toolName, args);
121
+ const listing = readListing(result);
122
+ state.crumbs = crumbs;
123
+ state.lastArgs = pageArgs(args);
124
+ state.items = append ? state.items.concat(listing.items) : listing.items;
125
+ state.nextLink = listing.nextLink;
126
+ state.busy = false;
127
+ render();
128
+ } catch (error) {
129
+ state.busy = false;
130
+ setStatus(error.message || String(error), true);
131
+ updateControls();
132
+ }
133
+ }
134
+
135
+ function openFolder(folder) {
136
+ load(listArgs(folder.driveId, folder.id), state.crumbs.concat([folder]), false);
137
+ }
138
+
139
+ function openCrumb(index) {
140
+ const crumb = state.crumbs[index];
141
+ load(listArgs(crumb.driveId, crumb.id), state.crumbs.slice(0, index + 1), false);
142
+ }
143
+
144
+ // Lists the folder again with the fields the view needs, keeping the listing's own
145
+ // filter or order
146
+ function refresh() {
147
+ const crumb = current();
148
+ if (!crumb) return;
149
+ const args = pageArgs(state.lastArgs || {});
150
+ delete args.excludeResponse;
151
+ load(Object.assign(args, listArgs(crumb.driveId, crumb.id)), state.crumbs, false);
152
+ }
153
+
154
+ function loadMore() {
155
+ const crumb = current();
156
+ const base = state.lastArgs || (crumb && listArgs(crumb.driveId, crumb.id));
157
+ if (!state.nextLink || !base) return;
158
+ load(Object.assign(pageArgs(base), { skiptoken: state.nextLink }), state.crumbs, true);
159
+ }
160
+
161
+ async function openLink(url) {
162
+ try {
163
+ const result = await bridge.openLink(url);
164
+ if (result && result.isError) setStatus('The host did not open the link.', true);
165
+ } catch (error) {
166
+ setStatus(error.message || 'The host did not open the link.', true);
167
+ }
168
+ }
169
+
170
+ function render() {
171
+ renderCrumbs();
172
+ const folders = state.items.filter(isFolder);
173
+ const files = state.items.filter((item) => !isFolder(item));
174
+ const rows = folders.concat(files).map(renderRow);
175
+ els.items.replaceChildren(...rows);
176
+ els.items.hidden = rows.length === 0;
177
+ setStatus(rows.length === 0 ? 'This folder is empty.' : selectHint(), false);
178
+ updateControls();
179
+ }
180
+
181
+ // Models often pick their own select and leave out fields the view needs
182
+ function selectHint() {
183
+ const select = state.lastArgs && state.lastArgs.select;
184
+ if (select === undefined || !canCallTools() || !current()) return '';
185
+ const fields = (Array.isArray(select) ? select : String(select).split(',')).map((field) =>
186
+ String(field).trim().toLowerCase()
187
+ );
188
+ if (NEEDED.every((field) => fields.includes(field.toLowerCase()))) return '';
189
+ return 'This listing leaves out details the browser needs. Refresh to load them.';
190
+ }
191
+
192
+ function renderCrumbs() {
193
+ const parts = [];
194
+ state.crumbs.forEach((crumb, index) => {
195
+ if (index > 0) parts.push(span('sep', '/'));
196
+ const button = document.createElement('button');
197
+ button.type = 'button';
198
+ button.className = 'crumb';
199
+ button.textContent = crumb.name;
200
+ if (index === state.crumbs.length - 1) {
201
+ button.disabled = true;
202
+ button.setAttribute('aria-current', 'location');
203
+ } else if (canCallTools()) {
204
+ button.addEventListener('click', () => openCrumb(index));
205
+ } else {
206
+ button.disabled = true;
207
+ }
208
+ parts.push(button);
209
+ });
210
+ els.crumbs.replaceChildren(...parts);
211
+ }
212
+
213
+ function renderRow(item) {
214
+ // A shortcut to a folder shared from another drive carries the real item here
215
+ const target = isObject(item.remoteItem) ? item.remoteItem : item;
216
+ const folder = isFolder(item);
217
+ const name = str(item.name) || str(target.name) || '(no name)';
218
+ const button = document.createElement('button');
219
+ button.type = 'button';
220
+ button.className = 'row';
221
+ button.title = name;
222
+ button.append(icon(folder), span('name', name), span('meta', describe(item, target, folder)));
223
+ const action = rowAction(item, target, folder, name);
224
+ if (action) button.addEventListener('click', action);
225
+ else button.disabled = true;
226
+ const li = document.createElement('li');
227
+ li.append(button);
228
+ return li;
229
+ }
230
+
231
+ function rowAction(item, target, folder, name) {
232
+ if (folder && canCallTools()) {
233
+ const parent = isObject(target.parentReference) ? target.parentReference : {};
234
+ // A shortcut's folder is on another drive, so never borrow this one's id for it
235
+ const here = target === item ? current() && current().driveId : undefined;
236
+ const driveId = str(parent.driveId) || here;
237
+ const id = str(target.id);
238
+ if (driveId && id) return () => openFolder({ driveId, id, name });
239
+ }
240
+ const url = httpsUrl(target.webUrl) || httpsUrl(item.webUrl);
241
+ if (url && canOpenLinks()) return () => openLink(url);
242
+ return undefined;
243
+ }
244
+
245
+ function describe(item, target, folder) {
246
+ const parts = [];
247
+ if (folder) {
248
+ const count = target.folder.childCount;
249
+ if (typeof count === 'number') parts.push(count === 1 ? '1 item' : count + ' items');
250
+ } else if (typeof target.size === 'number') {
251
+ parts.push(formatSize(target.size));
252
+ }
253
+ const date = formatDate(target.lastModifiedDateTime || item.lastModifiedDateTime);
254
+ if (date) parts.push(date);
255
+ return parts.join(' · ');
256
+ }
257
+
258
+ function formatSize(bytes) {
259
+ const units = ['B', 'KB', 'MB', 'GB', 'TB'];
260
+ let value = bytes;
261
+ let unit = 0;
262
+ while (value >= 1024 && unit < units.length - 1) {
263
+ value /= 1024;
264
+ unit++;
265
+ }
266
+ return (unit === 0 ? String(value) : value.toFixed(value < 10 ? 1 : 0)) + ' ' + units[unit];
267
+ }
268
+
269
+ function formatDate(value) {
270
+ if (typeof value !== 'string') return '';
271
+ const date = new Date(value);
272
+ if (Number.isNaN(date.getTime())) return '';
273
+ try {
274
+ return new Intl.DateTimeFormat(bridge.locale, { dateStyle: 'medium' }).format(date);
275
+ } catch {
276
+ return date.toISOString().slice(0, 10);
277
+ }
278
+ }
279
+
280
+ function icon(folder) {
281
+ const ns = 'http://www.w3.org/2000/svg';
282
+ const svg = document.createElementNS(ns, 'svg');
283
+ svg.setAttribute('viewBox', '0 0 20 20');
284
+ svg.setAttribute('class', folder ? 'icon folder' : 'icon file');
285
+ svg.setAttribute('aria-hidden', 'true');
286
+ const path = document.createElementNS(ns, 'path');
287
+ path.setAttribute('d', folder ? FOLDER_ICON : FILE_ICON);
288
+ svg.append(path);
289
+ return svg;
290
+ }
291
+
292
+ function span(className, text) {
293
+ const el = document.createElement('span');
294
+ el.className = className;
295
+ el.textContent = text;
296
+ return el;
297
+ }
298
+
299
+ function setStatus(text, isError) {
300
+ els.status.textContent = text;
301
+ els.status.hidden = !text;
302
+ els.status.classList.toggle('error', Boolean(isError));
303
+ }
304
+
305
+ function updateControls() {
306
+ els.refresh.disabled = state.busy || !current() || !canCallTools();
307
+ els.more.hidden = !state.nextLink || !canCallTools();
308
+ els.more.disabled = state.busy;
309
+ }
310
+
311
+ function current() {
312
+ return state.crumbs[state.crumbs.length - 1];
313
+ }
314
+
315
+ function canCallTools() {
316
+ return Boolean(bridge.capabilities.serverTools && config.toolName);
317
+ }
318
+
319
+ function canOpenLinks() {
320
+ return Boolean(bridge.capabilities.openLinks);
321
+ }
322
+
323
+ function isFolder(item) {
324
+ const target = isObject(item.remoteItem) ? item.remoteItem : item;
325
+ return isObject(target.folder);
326
+ }
327
+
328
+ function isObject(value) {
329
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
330
+ }
331
+
332
+ function str(value) {
333
+ return typeof value === 'string' && value ? value : undefined;
334
+ }
335
+
336
+ function decode(value) {
337
+ try {
338
+ return decodeURIComponent(value);
339
+ } catch {
340
+ return value;
341
+ }
342
+ }
343
+
344
+ // The arguments a listing repeats for its next page: no cursor, and never every
345
+ // page at once
346
+ function pageArgs(args) {
347
+ const copy = Object.assign({}, args);
348
+ delete copy.skiptoken;
349
+ delete copy.fetchAllPages;
350
+ return copy;
351
+ }
352
+
353
+ function httpsUrl(value) {
354
+ if (typeof value !== 'string') return undefined;
355
+ try {
356
+ const url = new URL(value);
357
+ return url.protocol === 'https:' ? url.href : undefined;
358
+ } catch {
359
+ return undefined;
360
+ }
361
+ }
362
+ })();
@@ -0,0 +1,109 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <title>Mail draft</title>
7
+ <style>
8
+ :root {
9
+ color-scheme: light dark;
10
+ font-family: var(--font-sans, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif);
11
+ font-size: var(--font-text-sm-size, 14px);
12
+ line-height: var(--font-text-sm-line-height, 1.4);
13
+ color: var(--color-text-primary, CanvasText);
14
+ background: var(--color-background-primary, transparent);
15
+ }
16
+ * {
17
+ box-sizing: border-box;
18
+ }
19
+ [hidden] {
20
+ display: none !important;
21
+ }
22
+ body {
23
+ margin: 0;
24
+ padding: 12px;
25
+ }
26
+ button {
27
+ font: inherit;
28
+ color: inherit;
29
+ border-radius: var(--border-radius-sm, 4px);
30
+ cursor: pointer;
31
+ }
32
+ button:disabled {
33
+ opacity: 0.5;
34
+ cursor: default;
35
+ }
36
+ h1 {
37
+ margin: 0 0 8px;
38
+ font-size: var(--font-heading-sm-size, 16px);
39
+ font-weight: var(--font-weight-semibold, 600);
40
+ overflow-wrap: anywhere;
41
+ }
42
+ .fields {
43
+ display: grid;
44
+ grid-template-columns: max-content 1fr;
45
+ gap: 2px 12px;
46
+ margin-bottom: 8px;
47
+ }
48
+ .label {
49
+ color: var(--color-text-secondary, GrayText);
50
+ }
51
+ .value {
52
+ min-width: 0;
53
+ overflow-wrap: anywhere;
54
+ }
55
+ #body {
56
+ max-height: 320px;
57
+ overflow-y: auto;
58
+ padding: 8px;
59
+ white-space: pre-wrap;
60
+ overflow-wrap: anywhere;
61
+ border: 1px solid var(--color-border-tertiary, rgba(127, 127, 127, 0.25));
62
+ border-radius: var(--border-radius-sm, 4px);
63
+ }
64
+ .actions {
65
+ display: flex;
66
+ gap: 8px;
67
+ margin-top: 8px;
68
+ }
69
+ .primary {
70
+ border: 1px solid transparent;
71
+ background: var(--color-text-primary, CanvasText);
72
+ color: var(--color-background-primary, Canvas);
73
+ padding: 4px 16px;
74
+ font-weight: var(--font-weight-semibold, 600);
75
+ }
76
+ .tool {
77
+ border: 1px solid var(--color-border-secondary, rgba(127, 127, 127, 0.4));
78
+ background: none;
79
+ padding: 4px 12px;
80
+ }
81
+ #status {
82
+ margin: 8px 0 0;
83
+ color: var(--color-text-secondary, GrayText);
84
+ }
85
+ #status.error {
86
+ color: var(--color-text-danger, #c62828);
87
+ }
88
+ </style>
89
+ </head>
90
+ <body>
91
+ <h1 id="subject">Mail draft</h1>
92
+ <div id="fields" class="fields"></div>
93
+ <div id="body" hidden></div>
94
+ <div class="actions">
95
+ <button id="send" class="primary" type="button" hidden>Send</button>
96
+ <button id="open" class="tool" type="button" hidden>Open in Outlook</button>
97
+ </div>
98
+ <p id="status" role="status">Loading...</p>
99
+ <script type="application/json" id="mcp-app-config">
100
+ __MCP_APP_CONFIG__
101
+ </script>
102
+ <script>
103
+ __MCP_APP_BRIDGE__
104
+ </script>
105
+ <script>
106
+ __MCP_APP_SCRIPT__
107
+ </script>
108
+ </body>
109
+ </html>