Skip to content

Commit 938ea3f

Browse files
committed
feat(mcp): auto-create blank documents when file doesn't exist
superdoc_open now creates a new blank document from the built-in OOXML template when the target path doesn't exist, instead of failing.
1 parent 4474539 commit 938ea3f

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

apps/mcp/src/session-manager.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { readFile, writeFile } from 'node:fs/promises';
1+
import { readFile, writeFile, access } from 'node:fs/promises';
22
import { randomBytes } from 'node:crypto';
33
import { resolve, basename } from 'node:path';
44
import { Editor } from 'superdoc/super-editor';
55
import { getDocumentApiAdapters } from '@superdoc/super-editor/document-api-adapters';
66
import { createDocumentApi, type DocumentApi } from '@superdoc/document-api';
7+
import { BLANK_DOCX_BASE64 } from '@superdoc/super-editor/blank-docx';
78

89
export interface Session {
910
id: string;
@@ -19,9 +20,17 @@ export class SessionManager {
1920
async open(filePath: string): Promise<Session> {
2021
const absolutePath = resolve(filePath);
2122

22-
const bytes = await readFile(absolutePath);
23+
let bytes: Buffer;
2324

24-
const editor = await Editor.open(Buffer.from(bytes), {
25+
try {
26+
await access(absolutePath);
27+
bytes = await readFile(absolutePath);
28+
} catch {
29+
// File doesn't exist — create a blank document from the built-in template
30+
bytes = Buffer.from(BLANK_DOCX_BASE64, 'base64');
31+
}
32+
33+
const editor = await Editor.open(bytes, {
2534
documentId: absolutePath,
2635
user: { id: 'mcp', name: 'MCP Server' },
2736
});

apps/mcp/src/tools/lifecycle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function registerLifecycleTools(server: McpServer, sessions: SessionManag
88
{
99
title: 'Open Document',
1010
description:
11-
'Open a Word document (.docx) for reading and editing. Must be called before any other operation. Returns a session_id to use in subsequent calls.',
11+
'Open a Word document (.docx) for reading and editing. If the file does not exist, a new blank document is created at that path. Must be called before any other operation. Returns a session_id to use in subsequent calls.',
1212
inputSchema: {
1313
path: z.string().describe('Absolute path to the .docx file.'),
1414
},

apps/mcp/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"skipLibCheck": true,
88
"types": ["bun"],
99
"paths": {
10-
"@superdoc/super-editor/document-api-adapters": ["../../packages/super-editor/src/document-api-adapters/index.ts"]
10+
"@superdoc/super-editor/document-api-adapters": [
11+
"../../packages/super-editor/src/document-api-adapters/index.ts"
12+
],
13+
"@superdoc/super-editor/blank-docx": ["../../packages/super-editor/src/core/blank-docx.ts"]
1114
}
1215
},
1316
"include": ["src"]

0 commit comments

Comments
 (0)