mcp-word 1.0.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,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,120 @@
1
+ #!/usr/bin/env node
2
+ // src/index.ts
3
+ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
4
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
5
+ import { CallToolRequestSchema, ListToolsRequestSchema, ErrorCode, McpError, } from "@modelcontextprotocol/sdk/types.js";
6
+ import { z } from "zod";
7
+ import { promises as fs } from "fs";
8
+ import path from "path";
9
+ import { convertMarkdownToDocx } from "@mohtasham/md-to-docx";
10
+ // ============================================
11
+ // Schema 定義(使用 Zod 進行驗證)
12
+ // ============================================
13
+ const MarkdownToDocxSchema = z.object({
14
+ markdown: z.string().describe("要轉換的 Markdown 文字內容"),
15
+ filePath: z.string().describe("要儲存的檔案路徑(包含檔名,例如:/path/to/file.docx)"),
16
+ useGovernmentFormat: z.boolean().optional().default(false).describe("是否套用政府報告書格式(標題字體大小:H1=18pt, H2=14pt, H3=12pt)"),
17
+ });
18
+ // ============================================
19
+ // Server 初始化
20
+ // ============================================
21
+ const server = new Server({
22
+ name: "mcp-word",
23
+ version: "1.0.0",
24
+ }, {
25
+ capabilities: {
26
+ tools: {},
27
+ },
28
+ });
29
+ // ============================================
30
+ // Tools 處理
31
+ // ============================================
32
+ server.setRequestHandler(ListToolsRequestSchema, async () => {
33
+ return {
34
+ tools: [
35
+ {
36
+ name: "markdown_to_docx",
37
+ description: "將 Markdown 文字轉換為 DOCX 檔案並儲存到指定路徑。支援政府報告書格式(3層標題結構:壹、一、(一)),可選擇是否套用政府報告字體大小規範。",
38
+ inputSchema: {
39
+ type: "object",
40
+ properties: {
41
+ markdown: {
42
+ type: "string",
43
+ description: "要轉換的 Markdown 文字內容。支援政府報告書格式:第1層 # 壹、貳、叁...(18pt),第2層 ## 一、二、三...(14pt),第3層 ### (一)、(二)、(三)...(12pt)。僅使用3層標題,避免使用 #### ##### ######。"
44
+ },
45
+ filePath: {
46
+ type: "string",
47
+ description: "要儲存的檔案路徑(包含檔名,例如:/path/to/file.docx)"
48
+ },
49
+ useGovernmentFormat: {
50
+ type: "boolean",
51
+ description: "是否套用政府報告書格式樣式(標題字體大小:H1=18pt, H2=14pt, H3=12pt)。預設為 false。",
52
+ default: false
53
+ },
54
+ },
55
+ required: ["markdown", "filePath"],
56
+ },
57
+ },
58
+ ],
59
+ };
60
+ });
61
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
62
+ const { name, arguments: args } = request.params;
63
+ try {
64
+ switch (name) {
65
+ case "markdown_to_docx": {
66
+ const validated = MarkdownToDocxSchema.parse(args);
67
+ // 確保目錄存在
68
+ const dir = path.dirname(validated.filePath);
69
+ await fs.mkdir(dir, { recursive: true });
70
+ // 準備樣式設定
71
+ const styleOptions = {
72
+ tableLayout: "fixed", // 使用固定寬度模式,表格會填滿頁面寬度,欄位平均分配
73
+ };
74
+ // 如果啟用政府報告格式,套用對應的字體大小
75
+ if (validated.useGovernmentFormat) {
76
+ styleOptions.heading1Size = 18; // 第1層標題:18pt
77
+ styleOptions.heading2Size = 14; // 第2層標題:14pt
78
+ styleOptions.heading3Size = 12; // 第3層標題:12pt
79
+ styleOptions.paragraphSize = 12; // 段落:12pt
80
+ }
81
+ // 轉換 Markdown 為 DOCX
82
+ const docxBlob = await convertMarkdownToDocx(validated.markdown, {
83
+ style: styleOptions,
84
+ });
85
+ // 將 Blob 轉換為 Buffer 並寫入檔案
86
+ const arrayBuffer = await docxBlob.arrayBuffer();
87
+ const buffer = Buffer.from(arrayBuffer);
88
+ await fs.writeFile(validated.filePath, buffer);
89
+ return {
90
+ content: [
91
+ {
92
+ type: "text",
93
+ text: `檔案已成功轉換並儲存至:${validated.filePath}`,
94
+ },
95
+ ],
96
+ };
97
+ }
98
+ default:
99
+ throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`);
100
+ }
101
+ }
102
+ catch (error) {
103
+ if (error instanceof z.ZodError) {
104
+ throw new McpError(ErrorCode.InvalidParams, `Invalid parameters: ${error.issues.map((e) => e.message).join(", ")}`);
105
+ }
106
+ throw error;
107
+ }
108
+ });
109
+ // ============================================
110
+ // Server 啟動
111
+ // ============================================
112
+ async function main() {
113
+ const transport = new StdioServerTransport();
114
+ await server.connect(transport);
115
+ console.error("MCP Word Server started");
116
+ }
117
+ main().catch((error) => {
118
+ console.error("Fatal error:", error);
119
+ process.exit(1);
120
+ });
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "mcp-word",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "bin": {
7
+ "mcp-word": "dist/index.js"
8
+ },
9
+ "scripts": {
10
+ "build": "tsc",
11
+ "start": "node dist/index.js",
12
+ "dev": "tsc --watch",
13
+ "inspect": "npx @modelcontextprotocol/inspector node dist/index.js"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "dependencies": {
19
+ "@modelcontextprotocol/sdk": "^1.25.2",
20
+ "@mohtasham/md-to-docx": "^2.6.0",
21
+ "zod": "^4.3.5"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^25.0.7",
25
+ "typescript": "^5.9.3"
26
+ }
27
+ }