@teamnetwork-nz/m365-mcp-server 1.2.2 → 1.4.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.
- package/README.md +1 -1
- package/dist/index.js +1 -1
- package/dist/tools/schemas.js +36 -3
- package/dist/tools/sharepoint/downloadFile.js +1 -13
- package/dist/tools/sharepoint/uploadFile.js +43 -6
- package/dist/utils/mimeTypes.js +15 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -225,7 +225,7 @@ Operations always target exactly one mailbox per request, either the default or
|
|
|
225
225
|
| `get_drive_item` | Get metadata for a file or folder — always permitted, no grant required |
|
|
226
226
|
| `check_sharepoint_access` | Check whether an operation is permitted by the grant config (no network call, never errors) — use before content operations |
|
|
227
227
|
| `download_file` | Download a file as base64. Images returned as MCP image content; others as a JSON payload with `fileName`, `mimeType`, `encoding`, and `data`. Requires `read` grant |
|
|
228
|
-
| `upload_file` | Upload a file (
|
|
228
|
+
| `upload_file` | Upload a file (max 4 MB) via `sourcePath` (preferred — reads from disk) or inline `content` (`contentEncoding: "utf8"` or `"base64"`, optionally checked against `expectedBytes`/`sha256`). Creates or overwrites. Requires `create` or `update` grant |
|
|
229
229
|
| `create_folder` | Create a new folder. Requires `create` grant on the parent path |
|
|
230
230
|
| `delete_drive_item` | Permanently delete a file or folder. Requires `delete` grant |
|
|
231
231
|
| `move_drive_item` | Move a file or folder, optionally renaming. Requires `update` at source and `create` at destination |
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ async function main() {
|
|
|
16
16
|
// 3. Create MCP server and register all tools
|
|
17
17
|
const server = new McpServer({
|
|
18
18
|
name: '@teamnetwork/m365-mcp-server',
|
|
19
|
-
version: '1.0
|
|
19
|
+
version: '1.3.0',
|
|
20
20
|
}, {
|
|
21
21
|
instructions: `
|
|
22
22
|
Microsoft 365 MCP server — email, calendar, and To Do via the Microsoft Graph API.
|
package/dist/tools/schemas.js
CHANGED
|
@@ -189,11 +189,44 @@ export const UploadFileSchema = SharepointBase.extend({
|
|
|
189
189
|
.string()
|
|
190
190
|
.describe('Full destination path including filename (e.g. "/Finance/Reports/Q1.pdf"). ' +
|
|
191
191
|
'Requires a grant with "create" OR "update" in allowedOperations.'),
|
|
192
|
-
|
|
192
|
+
sourcePath: z
|
|
193
|
+
.string()
|
|
194
|
+
.optional()
|
|
195
|
+
.describe('Preferred over content: an absolute local file path to upload. The server reads the file ' +
|
|
196
|
+
'directly from disk, so no base64 payload needs to be embedded in the tool call. Overwrites ' +
|
|
197
|
+
'the destination if it already exists; SharePoint manages version history. Exactly one of ' +
|
|
198
|
+
'sourcePath or content must be provided.'),
|
|
199
|
+
content: z
|
|
200
|
+
.string()
|
|
201
|
+
.optional()
|
|
202
|
+
.describe('Inline file content, interpreted per contentEncoding (default base64). Only use this for ' +
|
|
203
|
+
'small files supplied inline — for anything larger, or a file that already exists on disk, ' +
|
|
204
|
+
'use sourcePath instead. For text files, prefer contentEncoding "utf8" so no base64 is ' +
|
|
205
|
+
'needed. Exactly one of sourcePath or content must be provided. Maximum 4 MB.'),
|
|
206
|
+
contentEncoding: z
|
|
207
|
+
.enum(['base64', 'utf8'])
|
|
208
|
+
.default('base64')
|
|
209
|
+
.describe('How to interpret content. "utf8" = plain text sent as-is (recommended for .md, .txt, .csv, ' +
|
|
210
|
+
'.json etc. — avoids error-prone base64). "base64" = binary files. Ignored for sourcePath.'),
|
|
211
|
+
expectedBytes: z
|
|
212
|
+
.number()
|
|
213
|
+
.int()
|
|
214
|
+
.nonnegative()
|
|
215
|
+
.optional()
|
|
216
|
+
.describe('Optional integrity check: the exact size in bytes the file should have after decoding. ' +
|
|
217
|
+
'If the received content decodes to a different size, the upload is rejected before ' +
|
|
218
|
+
'anything is written — this catches truncated or corrupted inline content.'),
|
|
219
|
+
sha256: z
|
|
220
|
+
.string()
|
|
221
|
+
.regex(/^[0-9a-fA-F]{64}$/, 'sha256 must be a 64-character hex string')
|
|
222
|
+
.optional()
|
|
223
|
+
.describe('Optional integrity check: hex SHA-256 of the file bytes. If the received content does not ' +
|
|
224
|
+
'match, the upload is rejected before anything is written.'),
|
|
193
225
|
contentType: z
|
|
194
226
|
.string()
|
|
195
|
-
.
|
|
196
|
-
.describe('MIME type of the file (e.g. "application/pdf", "image/png").'
|
|
227
|
+
.optional()
|
|
228
|
+
.describe('MIME type of the file (e.g. "application/pdf", "image/png"). Inferred from the ' +
|
|
229
|
+
'destination path\'s extension when omitted.'),
|
|
197
230
|
});
|
|
198
231
|
export const CreateFolderSchema = SharepointBase.extend({
|
|
199
232
|
parentPath: z
|
|
@@ -6,22 +6,10 @@ import { errorResponse } from '../../utils/errors.js';
|
|
|
6
6
|
import { logger } from '../../utils/logger.js';
|
|
7
7
|
import { resolveSharepointLibrary } from '../../utils/resolveSharepointLibrary.js';
|
|
8
8
|
import { splitPath } from '../../utils/pathUtils.js';
|
|
9
|
+
import { mimeFromExtension } from '../../utils/mimeTypes.js';
|
|
9
10
|
const IMAGE_MIME_TYPES = new Set([
|
|
10
11
|
'image/png', 'image/jpeg', 'image/gif', 'image/webp', 'image/svg+xml',
|
|
11
12
|
]);
|
|
12
|
-
function mimeFromExtension(filename) {
|
|
13
|
-
const ext = filename.split('.').pop()?.toLowerCase() ?? '';
|
|
14
|
-
const map = {
|
|
15
|
-
png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg',
|
|
16
|
-
gif: 'image/gif', webp: 'image/webp', svg: 'image/svg+xml',
|
|
17
|
-
pdf: 'application/pdf',
|
|
18
|
-
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
19
|
-
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
20
|
-
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
21
|
-
txt: 'text/plain', csv: 'text/csv', json: 'application/json',
|
|
22
|
-
};
|
|
23
|
-
return map[ext] ?? 'application/octet-stream';
|
|
24
|
-
}
|
|
25
13
|
export function registerDownloadFile(server, config, auth) {
|
|
26
14
|
server.tool('download_file', 'Download a file from a SharePoint document library as base64-encoded content. ' +
|
|
27
15
|
'Images are returned as MCP image content; all other files are returned as a JSON ' +
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
1
3
|
import { UploadFileSchema } from '../schemas.js';
|
|
2
4
|
import { createGraphClient } from '../../graph/clientFactory.js';
|
|
3
5
|
import { uploadFile } from '../../graph/sharepoint.js';
|
|
@@ -6,12 +8,19 @@ import { ToolError, errorResponse } from '../../utils/errors.js';
|
|
|
6
8
|
import { logger } from '../../utils/logger.js';
|
|
7
9
|
import { resolveSharepointLibrary } from '../../utils/resolveSharepointLibrary.js';
|
|
8
10
|
import { splitPath } from '../../utils/pathUtils.js';
|
|
11
|
+
import { mimeFromExtension } from '../../utils/mimeTypes.js';
|
|
9
12
|
const MAX_BYTES = 4 * 1024 * 1024; // 4 MB
|
|
10
13
|
export function registerUploadFile(server, config, auth) {
|
|
11
|
-
server.tool('upload_file', 'Upload a file to a SharePoint document library.
|
|
12
|
-
'
|
|
13
|
-
'
|
|
14
|
-
'
|
|
14
|
+
server.tool('upload_file', 'Upload a file to a SharePoint document library. Creates the file if it does not exist; ' +
|
|
15
|
+
'overwrites it if it does (SharePoint manages version history). ' +
|
|
16
|
+
'Provide exactly one of sourcePath or content: sourcePath (preferred) reads an existing ' +
|
|
17
|
+
'local file directly from disk; content is for small inline payloads only — use ' +
|
|
18
|
+
'contentEncoding "utf8" for text files (no base64), "base64" for binary. Long base64 ' +
|
|
19
|
+
'emitted inline can be truncated or corrupted by the caller with no error, so pass ' +
|
|
20
|
+
'expectedBytes and/or sha256 with inline content: a mismatch rejects the upload before ' +
|
|
21
|
+
'anything is written. Prefer sourcePath for anything above a few KB. ' +
|
|
22
|
+
'Maximum 4 MB. Requires a grant with "create" OR "update" in allowedOperations for the path ' +
|
|
23
|
+
'and filename. Call check_sharepoint_access first to verify permissions.', UploadFileSchema.shape, async (params) => {
|
|
15
24
|
try {
|
|
16
25
|
const library = resolveSharepointLibrary(config, params.libraryId);
|
|
17
26
|
const { folderPath, filename } = splitPath(params.path);
|
|
@@ -23,14 +32,42 @@ export function registerUploadFile(server, config, auth) {
|
|
|
23
32
|
// Report whichever gives a more relevant message (create is the more common intent)
|
|
24
33
|
throw new ToolError(createResult.reason, 'ACCESS_DENIED');
|
|
25
34
|
}
|
|
26
|
-
|
|
35
|
+
if (!params.sourcePath === !params.content) {
|
|
36
|
+
throw new ToolError('Provide exactly one of sourcePath or content, not both or neither.', 'INVALID_PARAMS');
|
|
37
|
+
}
|
|
38
|
+
let buf;
|
|
39
|
+
if (params.sourcePath) {
|
|
40
|
+
try {
|
|
41
|
+
buf = await readFile(params.sourcePath);
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
throw new ToolError(`Could not read local file "${params.sourcePath}": ${err.message}`, 'SOURCE_FILE_NOT_FOUND');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
buf = Buffer.from(params.content, params.contentEncoding === 'utf8' ? 'utf8' : 'base64');
|
|
49
|
+
}
|
|
50
|
+
// Integrity checks — reject before any Graph call if the decoded bytes are wrong.
|
|
51
|
+
if (params.expectedBytes !== undefined && buf.byteLength !== params.expectedBytes) {
|
|
52
|
+
throw new ToolError(`Integrity check failed: expected ${params.expectedBytes} bytes but received ` +
|
|
53
|
+
`${buf.byteLength}. The content was likely truncated or corrupted in transit — ` +
|
|
54
|
+
'nothing was uploaded.', 'INTEGRITY_CHECK_FAILED');
|
|
55
|
+
}
|
|
56
|
+
if (params.sha256 !== undefined) {
|
|
57
|
+
const actual = createHash('sha256').update(buf).digest('hex');
|
|
58
|
+
if (actual !== params.sha256.toLowerCase()) {
|
|
59
|
+
throw new ToolError(`Integrity check failed: SHA-256 mismatch (received ${actual}). The content was ` +
|
|
60
|
+
'likely truncated or corrupted in transit — nothing was uploaded.', 'INTEGRITY_CHECK_FAILED');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
27
63
|
if (buf.byteLength > MAX_BYTES) {
|
|
28
64
|
throw new ToolError(`File is ${(buf.byteLength / 1024 / 1024).toFixed(1)} MB which exceeds the 4 MB ` +
|
|
29
65
|
'simple upload limit. For larger files, chunked upload (resumable sessions) is required.', 'PAYLOAD_TOO_LARGE');
|
|
30
66
|
}
|
|
67
|
+
const contentType = params.contentType ?? mimeFromExtension(filename);
|
|
31
68
|
const token = await auth.getAccessToken(config.accounts[0].id);
|
|
32
69
|
const client = createGraphClient(token);
|
|
33
|
-
const result = await uploadFile(client, library.driveId, params.path, buf,
|
|
70
|
+
const result = await uploadFile(client, library.driveId, params.path, buf, contentType);
|
|
34
71
|
return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
|
|
35
72
|
}
|
|
36
73
|
catch (err) {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const EXTENSION_MIME_MAP = {
|
|
2
|
+
png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg',
|
|
3
|
+
gif: 'image/gif', webp: 'image/webp', svg: 'image/svg+xml',
|
|
4
|
+
pdf: 'application/pdf',
|
|
5
|
+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
6
|
+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
7
|
+
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
8
|
+
txt: 'text/plain', csv: 'text/csv', json: 'application/json',
|
|
9
|
+
md: 'text/markdown',
|
|
10
|
+
};
|
|
11
|
+
/** Infer a MIME type from a filename's extension, defaulting to application/octet-stream. */
|
|
12
|
+
export function mimeFromExtension(filename) {
|
|
13
|
+
const ext = filename.split('.').pop()?.toLowerCase() ?? '';
|
|
14
|
+
return EXTENSION_MIME_MAP[ext] ?? 'application/octet-stream';
|
|
15
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teamnetwork-nz/m365-mcp-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "MCP server for Microsoft 365 — Email, Calendar, To Do, SharePoint document libraries, and Teams — using client credentials (app-only auth). Supports multiple accounts, shared mailboxes, per-mailbox read-only access, and grant-based SharePoint access control.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|