@softeria/ms-365-mcp-server 0.186.0 → 0.187.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -12
- package/dist/apps/bridge.js +258 -0
- package/dist/apps/file-browser.html +4 -524
- package/dist/apps/file-browser.js +362 -0
- package/dist/apps/mail-draft.html +109 -0
- package/dist/apps/mail-draft.js +341 -0
- package/dist/cli.js +1 -1
- package/dist/endpoints.json +41 -9
- package/dist/graph-tools.js +27 -3
- package/dist/mcp-apps.js +101 -44
- package/dist/normalize-tool-schema.js +38 -26
- package/eslint.config.js +8 -0
- package/package.json +1 -1
- package/src/apps/bridge.js +258 -0
- package/src/apps/file-browser.html +4 -524
- package/src/apps/file-browser.js +362 -0
- package/src/apps/mail-draft.html +109 -0
- package/src/apps/mail-draft.js +341 -0
- package/src/endpoints.json +41 -9
- package/tsup.config.ts +3 -1
package/dist/mcp-apps.js
CHANGED
|
@@ -5,64 +5,121 @@ import logger from "./logger.js";
|
|
|
5
5
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
6
|
const MCP_APP_MIME_TYPE = "text/html;profile=mcp-app";
|
|
7
7
|
const CONFIG_PLACEHOLDER = "__MCP_APP_CONFIG__";
|
|
8
|
+
const BRIDGE_PLACEHOLDER = "__MCP_APP_BRIDGE__";
|
|
9
|
+
const SCRIPT_PLACEHOLDER = "__MCP_APP_SCRIPT__";
|
|
8
10
|
const RESOURCE_UI_META = { csp: {}, prefersBorder: true };
|
|
9
|
-
function mcpAppUri(
|
|
10
|
-
return `ui://ms365/${
|
|
11
|
+
function mcpAppUri(view, toolName) {
|
|
12
|
+
return `ui://ms365/${view}/${toolName}.html`;
|
|
11
13
|
}
|
|
12
|
-
function
|
|
13
|
-
return path.join(__dirname, "apps",
|
|
14
|
+
function mcpAppPath(file) {
|
|
15
|
+
return path.join(__dirname, "apps", file);
|
|
14
16
|
}
|
|
15
|
-
const
|
|
16
|
-
function
|
|
17
|
-
let
|
|
18
|
-
if (
|
|
17
|
+
const pages = /* @__PURE__ */ new Map();
|
|
18
|
+
function loadPage(view) {
|
|
19
|
+
let page = pages.get(view);
|
|
20
|
+
if (page === void 0) {
|
|
19
21
|
try {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
page = fill(
|
|
23
|
+
readFileSync(mcpAppPath(`${view}.html`), "utf8"),
|
|
24
|
+
BRIDGE_PLACEHOLDER,
|
|
25
|
+
script("bridge.js")
|
|
26
|
+
);
|
|
27
|
+
page = fill(page, SCRIPT_PLACEHOLDER, script(`${view}.js`));
|
|
28
|
+
if (!page.includes(CONFIG_PLACEHOLDER)) {
|
|
29
|
+
throw new Error(`${view}.html has no ${CONFIG_PLACEHOLDER}`);
|
|
23
30
|
}
|
|
24
31
|
} catch (error) {
|
|
25
|
-
|
|
32
|
+
page = error;
|
|
26
33
|
}
|
|
27
|
-
if (
|
|
28
|
-
|
|
34
|
+
if (page instanceof Error) logger.error(`MCP app ${view} left out: ${page.message}`);
|
|
35
|
+
pages.set(view, page);
|
|
29
36
|
}
|
|
30
|
-
return
|
|
37
|
+
return page;
|
|
31
38
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
function script(file) {
|
|
40
|
+
return checkMcpAppScript(file, readFileSync(mcpAppPath(file), "utf8"));
|
|
41
|
+
}
|
|
42
|
+
function checkMcpAppScript(file, source) {
|
|
43
|
+
if (/<\/script/i.test(source)) throw new Error(`${file} contains </script`);
|
|
44
|
+
if (source.includes("<!--")) throw new Error(`${file} contains <!--`);
|
|
45
|
+
if (source.includes("__MCP_APP_")) throw new Error(`${file} contains a placeholder`);
|
|
46
|
+
return source;
|
|
47
|
+
}
|
|
48
|
+
function fill(page, placeholder, content) {
|
|
49
|
+
if (!page.includes(placeholder)) throw new Error(`the page has no ${placeholder}`);
|
|
50
|
+
return page.replace(placeholder, () => content);
|
|
51
|
+
}
|
|
52
|
+
const servers = /* @__PURE__ */ new WeakMap();
|
|
53
|
+
function mcpAppToolMeta(binding, toolName) {
|
|
54
|
+
if (loadPage(binding.view) instanceof Error) return void 0;
|
|
55
|
+
const uri = mcpAppUri(binding.view, toolName);
|
|
56
|
+
return { ui: { resourceUri: uri }, "ui/resourceUri": uri };
|
|
57
|
+
}
|
|
58
|
+
function mcpAppResultWithoutBody(result) {
|
|
59
|
+
if (result.isError) return result;
|
|
60
|
+
const block = result.content.find((item) => item.type === "text");
|
|
61
|
+
let body;
|
|
62
|
+
try {
|
|
63
|
+
body = block?.type === "text" ? JSON.parse(block.text) : void 0;
|
|
64
|
+
} catch {
|
|
65
|
+
body = void 0;
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
...result,
|
|
69
|
+
content: [{ type: "text", text: JSON.stringify({ success: true }) }],
|
|
70
|
+
...body && typeof body === "object" && !Array.isArray(body) ? { structuredContent: body } : {}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function noteMcpAppTool(server, toolName, binding) {
|
|
74
|
+
let apps = servers.get(server);
|
|
36
75
|
if (!apps) {
|
|
37
|
-
apps = /* @__PURE__ */ new Set();
|
|
38
|
-
|
|
76
|
+
apps = { tools: /* @__PURE__ */ new Set(), bindings: [] };
|
|
77
|
+
servers.set(server, apps);
|
|
39
78
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
79
|
+
apps.tools.add(toolName);
|
|
80
|
+
if (binding) apps.bindings.push({ toolName, binding });
|
|
81
|
+
}
|
|
82
|
+
function registerMcpAppViews(server) {
|
|
83
|
+
const apps = servers.get(server);
|
|
84
|
+
if (!apps) return;
|
|
85
|
+
servers.delete(server);
|
|
86
|
+
for (const { toolName, binding } of apps.bindings) {
|
|
87
|
+
const page = loadPage(binding.view);
|
|
88
|
+
if (page instanceof Error) continue;
|
|
89
|
+
const tools = {};
|
|
90
|
+
for (const [role, name] of Object.entries(binding.tools ?? {})) {
|
|
91
|
+
if (apps.tools.has(name)) tools[role] = name;
|
|
92
|
+
}
|
|
93
|
+
const config = JSON.stringify({ toolName, tools }).replace(/</g, "\\u003c");
|
|
94
|
+
const html = page.replace(CONFIG_PLACEHOLDER, () => config);
|
|
95
|
+
const uri = mcpAppUri(binding.view, toolName);
|
|
96
|
+
try {
|
|
97
|
+
server.registerResource(
|
|
98
|
+
`${binding.view}:${toolName}`,
|
|
99
|
+
uri,
|
|
100
|
+
{
|
|
101
|
+
description: `MCP Apps view for ${toolName}`,
|
|
102
|
+
mimeType: MCP_APP_MIME_TYPE,
|
|
103
|
+
_meta: { ui: RESOURCE_UI_META }
|
|
104
|
+
},
|
|
105
|
+
async () => ({
|
|
106
|
+
contents: [
|
|
107
|
+
{ uri, mimeType: MCP_APP_MIME_TYPE, text: html, _meta: { ui: RESOURCE_UI_META } }
|
|
108
|
+
]
|
|
109
|
+
})
|
|
110
|
+
);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
logger.error(`MCP app view for ${toolName} left out: ${error.message}`);
|
|
113
|
+
}
|
|
60
114
|
}
|
|
61
|
-
return { ui: { resourceUri: uri }, "ui/resourceUri": uri };
|
|
62
115
|
}
|
|
63
116
|
export {
|
|
64
117
|
MCP_APP_MIME_TYPE,
|
|
65
|
-
|
|
118
|
+
checkMcpAppScript,
|
|
119
|
+
mcpAppPath,
|
|
120
|
+
mcpAppResultWithoutBody,
|
|
66
121
|
mcpAppToolMeta,
|
|
67
|
-
mcpAppUri
|
|
122
|
+
mcpAppUri,
|
|
123
|
+
noteMcpAppTool,
|
|
124
|
+
registerMcpAppViews
|
|
68
125
|
};
|
|
@@ -243,21 +243,26 @@ function findCyclicDefs(defs) {
|
|
|
243
243
|
}
|
|
244
244
|
return cyclic;
|
|
245
245
|
}
|
|
246
|
-
function
|
|
246
|
+
function isLeafDef(value) {
|
|
247
|
+
if (!isPlainObject(value)) return false;
|
|
248
|
+
const type = value.type;
|
|
249
|
+
return typeof type === "string" && ["boolean", "integer", "number", "string", "null"].includes(type) || Array.isArray(value.enum) || "const" in value;
|
|
250
|
+
}
|
|
251
|
+
function inlineDefRefs(node, defs, keep, expand) {
|
|
247
252
|
if (!node || typeof node !== "object") return node;
|
|
248
|
-
if (Array.isArray(node)) return node.map((item) => inlineDefRefs(item, defs,
|
|
253
|
+
if (Array.isArray(node)) return node.map((item) => inlineDefRefs(item, defs, keep, expand));
|
|
249
254
|
const record = node;
|
|
250
255
|
const name = defRefName(record.$ref);
|
|
251
|
-
if (name !== null && hasDef(defs, name) && !
|
|
256
|
+
if (name !== null && hasDef(defs, name) && !keep.has(name)) {
|
|
252
257
|
const siblings = {};
|
|
253
258
|
for (const [key, value] of Object.entries(record)) {
|
|
254
|
-
if (key !== "$ref") siblings[key] = inlineDefRefs(value, defs,
|
|
259
|
+
if (key !== "$ref") siblings[key] = inlineDefRefs(value, defs, keep, expand);
|
|
255
260
|
}
|
|
256
261
|
return { ...structuredClone(expand(name)), ...siblings };
|
|
257
262
|
}
|
|
258
263
|
const out = {};
|
|
259
264
|
for (const [key, value] of Object.entries(record)) {
|
|
260
|
-
out[key] = inlineDefRefs(value, defs,
|
|
265
|
+
out[key] = inlineDefRefs(value, defs, keep, expand);
|
|
261
266
|
}
|
|
262
267
|
return out;
|
|
263
268
|
}
|
|
@@ -315,29 +320,36 @@ function normalizeToolSchemaRefs(schema) {
|
|
|
315
320
|
for (const name of [...pinned]) {
|
|
316
321
|
if (!hasDef(defs, name)) pinned.delete(name);
|
|
317
322
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
Object.entries(existingDefs).map(([name, value]) => [
|
|
333
|
-
name,
|
|
334
|
-
inlineDefRefs(value, defs, pinned, expand)
|
|
335
|
-
])
|
|
323
|
+
const inlineExcept = (keep) => {
|
|
324
|
+
const memo = /* @__PURE__ */ new Map();
|
|
325
|
+
const expand = (name) => {
|
|
326
|
+
if (memo.has(name)) return memo.get(name);
|
|
327
|
+
const value = inlineDefRefs(defs[name], defs, keep, expand);
|
|
328
|
+
memo.set(name, value);
|
|
329
|
+
return value;
|
|
330
|
+
};
|
|
331
|
+
const inlined2 = inlineDefRefs(clone, defs, keep, expand);
|
|
332
|
+
const keptDefs = Object.fromEntries(
|
|
333
|
+
[
|
|
334
|
+
...Object.entries(existingDefs),
|
|
335
|
+
...[...keep].map((name) => [name, defs[name]])
|
|
336
|
+
].map(([name, value]) => [name, inlineDefRefs(value, defs, keep, expand)])
|
|
336
337
|
);
|
|
338
|
+
if (Object.keys(keptDefs).length > 0) inlined2.$defs = keptDefs;
|
|
339
|
+
return inlined2;
|
|
340
|
+
};
|
|
341
|
+
const hoisted = { ...clone, $defs: { ...existingDefs, ...defs } };
|
|
342
|
+
const limit = JSON.stringify(hoisted).length * MAX_INLINE_GROWTH;
|
|
343
|
+
if (pinned.size === 0) {
|
|
344
|
+
const full = inlineExcept(pinned);
|
|
345
|
+
if (JSON.stringify(full).length <= limit) return full;
|
|
346
|
+
}
|
|
347
|
+
const leavesOnly = new Set(pinned);
|
|
348
|
+
for (const [name, value] of Object.entries(defs)) {
|
|
349
|
+
if (!isLeafDef(value)) leavesOnly.add(name);
|
|
337
350
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
return grewTooMuch ? clone : inlined;
|
|
351
|
+
const inlined = inlineExcept(leavesOnly);
|
|
352
|
+
return JSON.stringify(inlined).length <= limit ? inlined : hoisted;
|
|
341
353
|
}
|
|
342
354
|
function installToolSchemaRefNormalization(server) {
|
|
343
355
|
const lowLevel = server.server;
|
package/eslint.config.js
CHANGED
|
@@ -30,6 +30,14 @@ export default [
|
|
|
30
30
|
'no-console': 'off',
|
|
31
31
|
},
|
|
32
32
|
},
|
|
33
|
+
{
|
|
34
|
+
// MCP Apps views: classic scripts the server inlines into pages in the host's iframe
|
|
35
|
+
files: ['src/apps/**/*.js'],
|
|
36
|
+
languageOptions: {
|
|
37
|
+
parserOptions: { sourceType: 'script' },
|
|
38
|
+
globals: globals.browser,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
33
41
|
{
|
|
34
42
|
ignores: [
|
|
35
43
|
'node_modules/**',
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softeria/ms-365-mcp-server",
|
|
3
3
|
"mcpName": "io.github.Softeria/ms-365-mcp-server",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.187.1",
|
|
5
5
|
"description": " A Model Context Protocol (MCP) server for interacting with Microsoft 365 and Office services through the Graph API",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "dist/index.js",
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/* exported createMcpAppBridge */
|
|
2
|
+
|
|
3
|
+
// MCP Apps (spec 2026-01-26): JSON-RPC 2.0 with the host over postMessage. The server inlines
|
|
4
|
+
// this into every view, before the view's own script, which calls createMcpAppBridge once.
|
|
5
|
+
function createMcpAppBridge(handlers) {
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
const PROTOCOL_VERSION = '2026-01-26';
|
|
9
|
+
// Like ext-apps' App: a request the host never answers fails after this, and a tool call's
|
|
10
|
+
// timer starts over on each progress notification, so a host heartbeat keeps it alive
|
|
11
|
+
const REQUEST_TIMEOUT_MS = 60000;
|
|
12
|
+
|
|
13
|
+
let nextId = 1;
|
|
14
|
+
const pending = new Map();
|
|
15
|
+
let capabilities = {};
|
|
16
|
+
let locale;
|
|
17
|
+
let inputSeen = false;
|
|
18
|
+
let resultSeen = false;
|
|
19
|
+
|
|
20
|
+
function post(message) {
|
|
21
|
+
window.parent.postMessage(Object.assign({ jsonrpc: '2.0' }, message), '*');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// options.timeout: milliseconds, 0 for none. options.progress: ask for progress notifications.
|
|
25
|
+
function request(method, params, options) {
|
|
26
|
+
const id = nextId++;
|
|
27
|
+
const timeout =
|
|
28
|
+
options && typeof options.timeout === 'number' ? options.timeout : REQUEST_TIMEOUT_MS;
|
|
29
|
+
let sent = params;
|
|
30
|
+
if (options && options.progress) {
|
|
31
|
+
const meta = Object.assign({}, params && params._meta, { progressToken: id });
|
|
32
|
+
sent = Object.assign({}, params, { _meta: meta });
|
|
33
|
+
}
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const waiter = { method, resolve, reject, timeout, timer: undefined };
|
|
36
|
+
pending.set(id, waiter);
|
|
37
|
+
arm(id, waiter);
|
|
38
|
+
post({ id, method, params: sent });
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function arm(id, waiter) {
|
|
43
|
+
clearTimeout(waiter.timer);
|
|
44
|
+
if (waiter.timeout > 0) {
|
|
45
|
+
waiter.timer = setTimeout(() => {
|
|
46
|
+
// Like the SDK: tell the host the view gave up, so it can drop the request too
|
|
47
|
+
notify('notifications/cancelled', { requestId: id, reason: 'Timed out' });
|
|
48
|
+
const error = new Error('The host did not answer in time.');
|
|
49
|
+
error.name = 'TimeoutError';
|
|
50
|
+
settle(id, error);
|
|
51
|
+
}, waiter.timeout);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function settle(id, error, result) {
|
|
56
|
+
const waiter = pending.get(id);
|
|
57
|
+
if (!waiter) return;
|
|
58
|
+
pending.delete(id);
|
|
59
|
+
clearTimeout(waiter.timer);
|
|
60
|
+
if (error) waiter.reject(error);
|
|
61
|
+
else waiter.resolve(result);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function notify(method, params) {
|
|
65
|
+
post({ method, params });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
window.addEventListener('message', (event) => {
|
|
69
|
+
if (event.source !== window.parent) return;
|
|
70
|
+
const msg = event.data;
|
|
71
|
+
if (!msg || typeof msg !== 'object' || msg.jsonrpc !== '2.0') return;
|
|
72
|
+
if (typeof msg.method === 'string') {
|
|
73
|
+
if (msg.id !== undefined) answerHost(msg);
|
|
74
|
+
else onNotification(msg.method, isObject(msg.params) ? msg.params : {});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (msg.error) settle(msg.id, new Error(msg.error.message || 'The host refused.'));
|
|
78
|
+
else settle(msg.id, undefined, msg.result);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
function answerHost(msg) {
|
|
82
|
+
if (msg.method === 'ui/resource-teardown' || msg.method === 'ping') {
|
|
83
|
+
post({ id: msg.id, result: {} });
|
|
84
|
+
} else {
|
|
85
|
+
post({ id: msg.id, error: { code: -32601, message: 'Method not found' } });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Only the first tool-input and tool-result count: they are the call that opened the view.
|
|
90
|
+
// Hosts may echo the view's own calls here too (the spec's interactive phase), and those
|
|
91
|
+
// answers already come back to the request that made them.
|
|
92
|
+
function onNotification(method, params) {
|
|
93
|
+
if (method === 'ui/notifications/tool-input') {
|
|
94
|
+
if (!inputSeen && !resultSeen) {
|
|
95
|
+
inputSeen = true;
|
|
96
|
+
handle('onToolInput', isObject(params.arguments) ? params.arguments : {});
|
|
97
|
+
}
|
|
98
|
+
} else if (method === 'ui/notifications/tool-result') {
|
|
99
|
+
if (!resultSeen) {
|
|
100
|
+
resultSeen = true;
|
|
101
|
+
handle('onToolResult', params);
|
|
102
|
+
}
|
|
103
|
+
} else if (method === 'ui/notifications/tool-cancelled') {
|
|
104
|
+
// This is about the call that opened the view, never one the view made itself, so
|
|
105
|
+
// it means nothing once that call's result is in
|
|
106
|
+
if (!resultSeen) handle('onToolCancelled', params);
|
|
107
|
+
} else if (method === 'notifications/progress') {
|
|
108
|
+
const waiter = pending.get(params.progressToken);
|
|
109
|
+
if (waiter) arm(params.progressToken, waiter);
|
|
110
|
+
} else if (method === 'ui/notifications/host-context-changed') {
|
|
111
|
+
applyHostContext(params);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function applyHostContext(context) {
|
|
116
|
+
if (!isObject(context)) return;
|
|
117
|
+
const root = document.documentElement;
|
|
118
|
+
if (context.theme === 'light' || context.theme === 'dark') {
|
|
119
|
+
root.dataset.theme = context.theme;
|
|
120
|
+
root.style.colorScheme = context.theme;
|
|
121
|
+
}
|
|
122
|
+
const variables = context.styles && context.styles.variables;
|
|
123
|
+
if (isObject(variables)) {
|
|
124
|
+
for (const [name, value] of Object.entries(variables)) {
|
|
125
|
+
if (name.startsWith('--') && typeof value === 'string') {
|
|
126
|
+
root.style.setProperty(name, value);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (typeof context.locale === 'string') locale = context.locale;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Tell the host how tall the view is, like the official App's autoResize
|
|
134
|
+
let lastWidth = 0;
|
|
135
|
+
let lastHeight = 0;
|
|
136
|
+
let sizeQueued = false;
|
|
137
|
+
function reportSize() {
|
|
138
|
+
if (sizeQueued) return;
|
|
139
|
+
sizeQueued = true;
|
|
140
|
+
requestAnimationFrame(() => {
|
|
141
|
+
sizeQueued = false;
|
|
142
|
+
const root = document.documentElement;
|
|
143
|
+
const previous = root.style.height;
|
|
144
|
+
root.style.height = 'max-content';
|
|
145
|
+
const height = Math.ceil(root.getBoundingClientRect().height);
|
|
146
|
+
root.style.height = previous;
|
|
147
|
+
const width = Math.ceil(window.innerWidth);
|
|
148
|
+
if (width === lastWidth && height === lastHeight) return;
|
|
149
|
+
lastWidth = width;
|
|
150
|
+
lastHeight = height;
|
|
151
|
+
notify('ui/notifications/size-changed', { width, height });
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// This server's tool results are JSON text; a failure sets isError and has an error field.
|
|
156
|
+
// A body the model asked to leave out comes as structuredContent instead.
|
|
157
|
+
function readToolResult(result, failure) {
|
|
158
|
+
const failed = Boolean(result && result.isError);
|
|
159
|
+
let data =
|
|
160
|
+
!failed && result && isObject(result.structuredContent)
|
|
161
|
+
? result.structuredContent
|
|
162
|
+
: undefined;
|
|
163
|
+
if (data === undefined) {
|
|
164
|
+
const text = resultText(result);
|
|
165
|
+
try {
|
|
166
|
+
data = JSON.parse(text);
|
|
167
|
+
} catch {
|
|
168
|
+
throw new Error(
|
|
169
|
+
failed ? text || failure : 'This result is not JSON, so it is not shown here.'
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (failed || (isObject(data) && data.error)) throw new Error(errorText(data) || failure);
|
|
174
|
+
return data;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// The message of a failed result, undefined when it did not fail. A success need not be
|
|
178
|
+
// JSON at all: Graph answers some actions with an empty body.
|
|
179
|
+
function toolError(result, failure) {
|
|
180
|
+
const text = resultText(result);
|
|
181
|
+
let data;
|
|
182
|
+
try {
|
|
183
|
+
data = JSON.parse(text);
|
|
184
|
+
} catch {
|
|
185
|
+
return result && result.isError ? text || failure : undefined;
|
|
186
|
+
}
|
|
187
|
+
if (!(result && result.isError) && !(isObject(data) && data.error)) return undefined;
|
|
188
|
+
return errorText(data) || text || failure;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function resultText(result) {
|
|
192
|
+
const blocks = result && Array.isArray(result.content) ? result.content : [];
|
|
193
|
+
return blocks
|
|
194
|
+
.filter((block) => block && block.type === 'text' && typeof block.text === 'string')
|
|
195
|
+
.map((block) => block.text)
|
|
196
|
+
.join('');
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// { error: 'text' }, Graph's { error: { message } } or { error: 'code', message }
|
|
200
|
+
function errorText(data) {
|
|
201
|
+
if (!isObject(data)) return undefined;
|
|
202
|
+
if (typeof data.message === 'string') return data.message;
|
|
203
|
+
if (typeof data.error === 'string') return data.error;
|
|
204
|
+
if (isObject(data.error) && typeof data.error.message === 'string') {
|
|
205
|
+
return data.error.message;
|
|
206
|
+
}
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function handle(name, value) {
|
|
211
|
+
if (handlers && typeof handlers[name] === 'function') handlers[name](value);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function isObject(value) {
|
|
215
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
request('ui/initialize', {
|
|
219
|
+
appInfo: { name: (handlers && handlers.name) || 'ms-365-app', version: '1.0.0' },
|
|
220
|
+
appCapabilities: {},
|
|
221
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
222
|
+
}).then(
|
|
223
|
+
(result) => {
|
|
224
|
+
capabilities = (result && isObject(result.hostCapabilities) && result.hostCapabilities) || {};
|
|
225
|
+
applyHostContext(result && result.hostContext);
|
|
226
|
+
notify('ui/notifications/initialized', {});
|
|
227
|
+
const observer = new ResizeObserver(reportSize);
|
|
228
|
+
observer.observe(document.documentElement);
|
|
229
|
+
observer.observe(document.body);
|
|
230
|
+
handle('onReady', capabilities);
|
|
231
|
+
},
|
|
232
|
+
(error) => handle('onInitError', error)
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
return {
|
|
236
|
+
get capabilities() {
|
|
237
|
+
return capabilities;
|
|
238
|
+
},
|
|
239
|
+
get locale() {
|
|
240
|
+
return locale;
|
|
241
|
+
},
|
|
242
|
+
callTool(name, args, options) {
|
|
243
|
+
return request(
|
|
244
|
+
'tools/call',
|
|
245
|
+
{ name, arguments: args },
|
|
246
|
+
Object.assign({ progress: true }, options)
|
|
247
|
+
);
|
|
248
|
+
},
|
|
249
|
+
openLink(url) {
|
|
250
|
+
return request('ui/open-link', { url });
|
|
251
|
+
},
|
|
252
|
+
updateModelContext(text) {
|
|
253
|
+
return request('ui/update-model-context', { content: [{ type: 'text', text }] });
|
|
254
|
+
},
|
|
255
|
+
readToolResult,
|
|
256
|
+
toolError,
|
|
257
|
+
};
|
|
258
|
+
}
|