feishu-mcp 0.0.6 → 0.0.7
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 -6
- package/dist/config.js +22 -100
- package/dist/index.js +14 -13
- package/dist/server.js +363 -449
- package/dist/services/baseService.js +204 -0
- package/dist/services/blockFactory.js +184 -0
- package/dist/services/feishuApiService.js +488 -0
- package/dist/services/feishuBlockService.js +179 -0
- package/dist/services/feishuService.js +475 -0
- package/dist/types/feishuSchema.js +97 -0
- package/dist/utils/cache.js +221 -0
- package/dist/utils/config.js +363 -0
- package/dist/utils/document.js +74 -0
- package/dist/utils/error.js +154 -0
- package/dist/utils/logger.js +257 -0
- package/dist/utils/paramUtils.js +193 -0
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import { FeishuService } from './services/feishu.js';
|
|
4
3
|
import express from 'express';
|
|
5
4
|
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
error: (...args) => {
|
|
11
|
-
console.error(...args);
|
|
12
|
-
},
|
|
13
|
-
};
|
|
5
|
+
import { formatErrorMessage } from './utils/error.js';
|
|
6
|
+
import { FeishuApiService } from './services/feishuApiService.js';
|
|
7
|
+
import { Logger } from './utils/logger.js';
|
|
8
|
+
import { DocumentIdSchema, ParentBlockIdSchema, BlockIdSchema, IndexSchema, StartIndexSchema, AlignSchema, AlignSchemaWithValidation, TextElementsArraySchema, CodeLanguageSchema, CodeWrapSchema, BlockConfigSchema } from './types/feishuSchema.js';
|
|
14
9
|
export class FeishuMcpServer {
|
|
15
|
-
constructor(
|
|
10
|
+
constructor() {
|
|
16
11
|
Object.defineProperty(this, "server", {
|
|
17
12
|
enumerable: true,
|
|
18
13
|
configurable: true,
|
|
@@ -31,11 +26,10 @@ export class FeishuMcpServer {
|
|
|
31
26
|
writable: true,
|
|
32
27
|
value: null
|
|
33
28
|
});
|
|
34
|
-
// 详细记录飞书配置状态
|
|
35
|
-
Logger.log(`飞书配置已提供 - AppID: ${feishuConfig.appId.substring(0, 4)}...${feishuConfig.appId.substring(feishuConfig.appId.length - 4)}, AppSecret: ${feishuConfig.appSecret.substring(0, 4)}...${feishuConfig.appSecret.substring(feishuConfig.appSecret.length - 4)}`);
|
|
36
29
|
try {
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
// 使用单例模式获取飞书服务实例
|
|
31
|
+
this.feishuService = FeishuApiService.getInstance();
|
|
32
|
+
Logger.info('飞书服务初始化成功');
|
|
39
33
|
}
|
|
40
34
|
catch (error) {
|
|
41
35
|
Logger.error('飞书服务初始化失败:', error);
|
|
@@ -54,399 +48,165 @@ export class FeishuMcpServer {
|
|
|
54
48
|
}
|
|
55
49
|
registerTools() {
|
|
56
50
|
// 添加创建飞书文档工具
|
|
57
|
-
this.server.tool('
|
|
58
|
-
title: z.string().describe('Document title'),
|
|
59
|
-
folderToken: z
|
|
60
|
-
.string()
|
|
61
|
-
.optional()
|
|
62
|
-
.describe('Folder token where the document will be created. If not provided, the document will be created in the root directory'),
|
|
51
|
+
this.server.tool('create_feishu_document', 'Creates a new Feishu document and returns its information. Use this tool when you need to create a document from scratch with a specific title and folder location.', {
|
|
52
|
+
title: z.string().describe('Document title (required). This will be displayed in the Feishu document list and document header.'),
|
|
53
|
+
folderToken: z.string().describe('Folder token (required). Specifies where to create the document. Format is an alphanumeric string like "doxcnOu1ZKYH4RtX1Y5XwL5WGRh".'),
|
|
63
54
|
}, async ({ title, folderToken }) => {
|
|
64
55
|
try {
|
|
65
|
-
Logger.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
56
|
+
Logger.info(`开始创建飞书文档,标题: ${title}${folderToken ? `,文件夹Token: ${folderToken}` : ',使用默认文件夹'}`);
|
|
57
|
+
const newDoc = await this.feishuService?.createDocument(title, folderToken);
|
|
58
|
+
if (!newDoc) {
|
|
59
|
+
throw new Error('创建文档失败,未返回文档信息');
|
|
60
|
+
}
|
|
61
|
+
Logger.info(`飞书文档创建成功,文档ID: ${newDoc.objToken || newDoc.document_id}`);
|
|
69
62
|
return {
|
|
70
63
|
content: [{ type: 'text', text: JSON.stringify(newDoc, null, 2) }],
|
|
71
64
|
};
|
|
72
65
|
}
|
|
73
66
|
catch (error) {
|
|
74
67
|
Logger.error(`创建飞书文档失败:`, error);
|
|
68
|
+
const errorMessage = formatErrorMessage(error);
|
|
75
69
|
return {
|
|
76
|
-
content: [{ type: 'text', text: `创建飞书文档失败: ${
|
|
70
|
+
content: [{ type: 'text', text: `创建飞书文档失败: ${errorMessage}` }],
|
|
77
71
|
};
|
|
78
72
|
}
|
|
79
73
|
});
|
|
80
74
|
// 添加获取飞书文档信息工具
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
// }
|
|
106
|
-
// },
|
|
107
|
-
// );
|
|
75
|
+
this.server.tool('get_feishu_document_info', 'Retrieves basic information about a Feishu document. Use this to verify a document exists, check access permissions, or get metadata like title, type, and creation information.', {
|
|
76
|
+
documentId: DocumentIdSchema,
|
|
77
|
+
}, async ({ documentId }) => {
|
|
78
|
+
try {
|
|
79
|
+
if (!this.feishuService) {
|
|
80
|
+
return {
|
|
81
|
+
content: [{ type: 'text', text: '飞书服务未初始化,请检查配置' }],
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
Logger.info(`开始获取飞书文档信息,文档ID: ${documentId}`);
|
|
85
|
+
const docInfo = await this.feishuService.getDocumentInfo(documentId);
|
|
86
|
+
Logger.info(`飞书文档信息获取成功,标题: ${docInfo.title}`);
|
|
87
|
+
return {
|
|
88
|
+
content: [{ type: 'text', text: JSON.stringify(docInfo, null, 2) }],
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
Logger.error(`获取飞书文档信息失败:`, error);
|
|
93
|
+
const errorMessage = formatErrorMessage(error, '获取飞书文档信息失败');
|
|
94
|
+
return {
|
|
95
|
+
content: [{ type: 'text', text: errorMessage }],
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
});
|
|
108
99
|
// 添加获取飞书文档内容工具
|
|
109
|
-
this.server.tool('
|
|
110
|
-
documentId:
|
|
111
|
-
|
|
112
|
-
.describe('Document ID or URL. Supported formats:\n1. Standard document URL: https://xxx.feishu.cn/docs/xxx or https://xxx.feishu.cn/docx/xxx\n2. API URL: https://open.feishu.cn/open-apis/doc/v2/documents/xxx\n3. Direct document ID (e.g., JcKbdlokYoPIe0xDzJ1cduRXnRf)'),
|
|
113
|
-
lang: z
|
|
114
|
-
.number()
|
|
115
|
-
.optional()
|
|
116
|
-
.default(0)
|
|
117
|
-
.describe('Language code. Default is 0 (Chinese)'),
|
|
100
|
+
this.server.tool('get_feishu_document_content', 'Retrieves the plain text content of a Feishu document. Ideal for content analysis, processing, or when you need to extract text without formatting. The content maintains the document structure but without styling. Note: For Feishu wiki links (https://xxx.feishu.cn/wiki/xxx) you must first use convert_feishu_wiki_to_document_id tool to obtain a compatible document ID.', {
|
|
101
|
+
documentId: DocumentIdSchema,
|
|
102
|
+
lang: z.number().optional().default(0).describe('Language code (optional). Default is 0 (Chinese). Use 1 for English if available.'),
|
|
118
103
|
}, async ({ documentId, lang }) => {
|
|
119
104
|
try {
|
|
120
105
|
if (!this.feishuService) {
|
|
121
106
|
return {
|
|
122
|
-
content: [
|
|
123
|
-
{
|
|
124
|
-
type: 'text',
|
|
125
|
-
text: 'Feishu service is not initialized. Please check the configuration',
|
|
126
|
-
},
|
|
127
|
-
],
|
|
107
|
+
content: [{ type: 'text', text: 'Feishu service is not initialized. Please check the configuration' }],
|
|
128
108
|
};
|
|
129
109
|
}
|
|
130
|
-
Logger.
|
|
110
|
+
Logger.info(`开始获取飞书文档内容,文档ID: ${documentId},语言: ${lang}`);
|
|
131
111
|
const content = await this.feishuService.getDocumentContent(documentId, lang);
|
|
132
|
-
Logger.
|
|
112
|
+
Logger.info(`飞书文档内容获取成功,内容长度: ${content.length}字符`);
|
|
133
113
|
return {
|
|
134
114
|
content: [{ type: 'text', text: content }],
|
|
135
115
|
};
|
|
136
116
|
}
|
|
137
117
|
catch (error) {
|
|
138
118
|
Logger.error(`获取飞书文档内容失败:`, error);
|
|
119
|
+
const errorMessage = formatErrorMessage(error);
|
|
139
120
|
return {
|
|
140
|
-
content: [{ type: 'text', text: `获取飞书文档内容失败: ${
|
|
121
|
+
content: [{ type: 'text', text: `获取飞书文档内容失败: ${errorMessage}` }],
|
|
141
122
|
};
|
|
142
123
|
}
|
|
143
124
|
});
|
|
144
125
|
// 添加获取飞书文档块工具
|
|
145
|
-
this.server.tool('
|
|
146
|
-
documentId:
|
|
147
|
-
|
|
148
|
-
.describe('Document ID or URL. Supported formats:\n1. Standard document URL: https://xxx.feishu.cn/docs/xxx or https://xxx.feishu.cn/docx/xxx\n2. API URL: https://open.feishu.cn/open-apis/doc/v2/documents/xxx\n3. Direct document ID (e.g., JcKbdlokYoPIe0xDzJ1cduRXnRf)'),
|
|
149
|
-
pageSize: z
|
|
150
|
-
.number()
|
|
151
|
-
.optional()
|
|
152
|
-
.default(500)
|
|
153
|
-
.describe('Number of blocks per page. Default is 500'),
|
|
126
|
+
this.server.tool('get_feishu_document_blocks', 'Retrieves the block structure information of a Feishu document. Essential to use before inserting content to understand document structure and determine correct insertion positions. Returns a detailed hierarchy of blocks with their IDs, types, and content. Note: For Feishu wiki links (https://xxx.feishu.cn/wiki/xxx) you must first use convert_feishu_wiki_to_document_id tool to obtain a compatible document ID.', {
|
|
127
|
+
documentId: DocumentIdSchema,
|
|
128
|
+
pageSize: z.number().optional().default(500).describe('Number of blocks per page (optional). Default is 500. Used for paginating large documents. Increase for more blocks at once, decrease for faster response with fewer blocks.'),
|
|
154
129
|
}, async ({ documentId, pageSize }) => {
|
|
155
130
|
try {
|
|
156
131
|
if (!this.feishuService) {
|
|
157
132
|
return {
|
|
158
|
-
content: [
|
|
159
|
-
{
|
|
160
|
-
type: 'text',
|
|
161
|
-
text: 'Feishu service is not initialized. Please check the configuration',
|
|
162
|
-
},
|
|
163
|
-
],
|
|
133
|
+
content: [{ type: 'text', text: 'Feishu service is not initialized. Please check the configuration' }],
|
|
164
134
|
};
|
|
165
135
|
}
|
|
166
|
-
Logger.
|
|
136
|
+
Logger.info(`开始获取飞书文档块,文档ID: ${documentId},页大小: ${pageSize}`);
|
|
167
137
|
const blocks = await this.feishuService.getDocumentBlocks(documentId, pageSize);
|
|
168
|
-
Logger.
|
|
138
|
+
Logger.info(`飞书文档块获取成功,共 ${blocks.length} 个块`);
|
|
169
139
|
return {
|
|
170
140
|
content: [{ type: 'text', text: JSON.stringify(blocks, null, 2) }],
|
|
171
141
|
};
|
|
172
142
|
}
|
|
173
143
|
catch (error) {
|
|
174
144
|
Logger.error(`获取飞书文档块失败:`, error);
|
|
145
|
+
const errorMessage = formatErrorMessage(error);
|
|
175
146
|
return {
|
|
176
|
-
content: [{ type: 'text', text: `获取飞书文档块失败: ${
|
|
147
|
+
content: [{ type: 'text', text: `获取飞书文档块失败: ${errorMessage}` }],
|
|
177
148
|
};
|
|
178
149
|
}
|
|
179
150
|
});
|
|
180
|
-
//
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
// documentId: z.string().describe("Document ID or URL. Supported formats:\n1. Standard document URL: https://xxx.feishu.cn/docs/xxx or https://xxx.feishu.cn/docx/xxx\n2. API URL: https://open.feishu.cn/open-apis/doc/v2/documents/xxx\n3. Direct document ID (e.g., JcKbdlokYoPIe0xDzJ1cduRXnRf)"),
|
|
186
|
-
// parentBlockId: z.string().describe("Parent block ID (NOT URL) where the new block will be added as a child. This should be the raw block ID without any URL prefix. When adding blocks at the page level (root level), use the extracted document ID from documentId parameter"),
|
|
187
|
-
// textContents: z.array(
|
|
188
|
-
// z.object({
|
|
189
|
-
// text: z.string().describe("Text content"),
|
|
190
|
-
// style: z.object({
|
|
191
|
-
// bold: z.boolean().optional().describe("Whether to make text bold. Default is false"),
|
|
192
|
-
// italic: z.boolean().optional().describe("Whether to make text italic. Default is false"),
|
|
193
|
-
// underline: z.boolean().optional().describe("Whether to add underline. Default is false"),
|
|
194
|
-
// strikethrough: z.boolean().optional().describe("Whether to add strikethrough. Default is false"),
|
|
195
|
-
// inline_code: z.boolean().optional().describe("Whether to format as inline code. Default is false"),
|
|
196
|
-
// text_color: z.number().optional().describe("Text color as a number. Default is 0")
|
|
197
|
-
// }).optional().describe("Text style settings")
|
|
198
|
-
// })
|
|
199
|
-
// ).describe("Array of text content objects. A block can contain multiple text segments with different styles"),
|
|
200
|
-
// align: z.number().optional().default(1).describe("Text alignment: 1 for left, 2 for center, 3 for right. Default is 1"),
|
|
201
|
-
// index: z.number().optional().default(0).describe("Insertion position index. Default is 0 (insert at the beginning). If unsure about the position, use the get_feishu_doc_blocks tool first to understand the document structure. For consecutive insertions, calculate the next position as previous_index + 1 to avoid querying document structure repeatedly")
|
|
202
|
-
// },
|
|
203
|
-
// async ({ documentId, parentBlockId, textContents, align = 1, index }) => {
|
|
204
|
-
// try {
|
|
205
|
-
// if (!this.feishuService) {
|
|
206
|
-
// return {
|
|
207
|
-
// content: [{ type: "text", text: "Feishu service is not initialized. Please check the configuration" }],
|
|
208
|
-
// };
|
|
209
|
-
// }
|
|
210
|
-
//
|
|
211
|
-
// // 处理Markdown语法转换
|
|
212
|
-
// const processedTextContents = textContents.map(content => {
|
|
213
|
-
// let { text, style = {} } = content;
|
|
214
|
-
//
|
|
215
|
-
// // 创建一个新的style对象,避免修改原始对象
|
|
216
|
-
// const newStyle = { ...style };
|
|
217
|
-
//
|
|
218
|
-
// // 处理粗体 **text**
|
|
219
|
-
// if (text.match(/\*\*([^*]+)\*\*/g)) {
|
|
220
|
-
// text = text.replace(/\*\*([^*]+)\*\*/g, "$1");
|
|
221
|
-
// newStyle.bold = true;
|
|
222
|
-
// }
|
|
223
|
-
//
|
|
224
|
-
// // 处理斜体 *text*
|
|
225
|
-
// if (text.match(/(?<!\*)\*([^*]+)\*(?!\*)/g)) {
|
|
226
|
-
// text = text.replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, "$1");
|
|
227
|
-
// newStyle.italic = true;
|
|
228
|
-
// }
|
|
229
|
-
//
|
|
230
|
-
// // 处理删除线 ~~text~~
|
|
231
|
-
// if (text.match(/~~([^~]+)~~/g)) {
|
|
232
|
-
// text = text.replace(/~~([^~]+)~~/g, "$1");
|
|
233
|
-
// newStyle.strikethrough = true;
|
|
234
|
-
// }
|
|
235
|
-
//
|
|
236
|
-
// // 处理行内代码 `code`
|
|
237
|
-
// if (text.match(/`([^`]+)`/g)) {
|
|
238
|
-
// text = text.replace(/`([^`]+)`/g, "$1");
|
|
239
|
-
// newStyle.inline_code = true;
|
|
240
|
-
// }
|
|
241
|
-
//
|
|
242
|
-
// return { text, style: newStyle };
|
|
243
|
-
// });
|
|
244
|
-
//
|
|
245
|
-
// Logger.log(`开始创建飞书文本块,文档ID: ${documentId},父块ID: ${parentBlockId},对齐方式: ${align},插入位置: ${index}`);
|
|
246
|
-
// const result = await this.feishuService.createTextBlock(documentId, parentBlockId, processedTextContents, align, index);
|
|
247
|
-
// Logger.log(`飞书文本块创建成功`);
|
|
248
|
-
//
|
|
249
|
-
// return {
|
|
250
|
-
// content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
251
|
-
// };
|
|
252
|
-
// } catch (error) {
|
|
253
|
-
// Logger.error(`创建飞书文本块失败:`, error);
|
|
254
|
-
// return {
|
|
255
|
-
// content: [{ type: "text", text: `创建飞书文本块失败: ${error}` }],
|
|
256
|
-
// };
|
|
257
|
-
// }
|
|
258
|
-
// },
|
|
259
|
-
// );
|
|
260
|
-
// 添加创建飞书代码块工具
|
|
261
|
-
// this.server.tool(
|
|
262
|
-
// "create_feishu_code_block",
|
|
263
|
-
// "Create a new code block in a Feishu document",
|
|
264
|
-
// {
|
|
265
|
-
// documentId: z.string().describe("Document ID or URL. Supported formats:\n1. Standard document URL: https://xxx.feishu.cn/docs/xxx or https://xxx.feishu.cn/docx/xxx\n2. API URL: https://open.feishu.cn/open-apis/doc/v2/documents/xxx\n3. Direct document ID (e.g., JcKbdlokYoPIe0xDzJ1cduRXnRf)"),
|
|
266
|
-
// parentBlockId: z.string().describe("Parent block ID (NOT URL) where the new block will be added as a child. This should be the raw block ID without any URL prefix. When adding blocks at the page level (root level), use the extracted document ID from documentId parameter"),
|
|
267
|
-
// code: z.string().describe("Code content"),
|
|
268
|
-
// language: z.number().optional().default(0).describe("Programming language code as a number. Examples: 1: PlainText; 7: Bash; 8: CSharp; 9: C++; 10: C; 12: CSS; 22: Go; 24: HTML; 29: Java; 30: JavaScript; 32: Kotlin; 43: PHP; 49: Python; 52: Ruby; 53: Rust; 56: SQL; 60: Shell; 61: Swift; 63: TypeScript. Default is 0"),
|
|
269
|
-
// wrap: z.boolean().optional().default(false).describe("Whether to enable automatic line wrapping. Default is false"),
|
|
270
|
-
// index: z.number().optional().default(0).describe("Insertion position index. Default is 0 (insert at the beginning). If unsure about the position, use the get_feishu_doc_blocks tool first to understand the document structure. For consecutive insertions, calculate the next position as previous_index + 1 to avoid querying document structure repeatedly")
|
|
271
|
-
// },
|
|
272
|
-
// async ({ documentId, parentBlockId, code, language = 0, wrap = false, index = 0 }) => {
|
|
273
|
-
// try {
|
|
274
|
-
// if (!this.feishuService) {
|
|
275
|
-
// return {
|
|
276
|
-
// content: [{ type: "text", text: "Feishu service is not initialized. Please check the configuration" }],
|
|
277
|
-
// };
|
|
278
|
-
// }
|
|
279
|
-
//
|
|
280
|
-
// Logger.log(`开始创建飞书代码块,文档ID: ${documentId},父块ID: ${parentBlockId},语言: ${language},自动换行: ${wrap},插入位置: ${index}`);
|
|
281
|
-
// const result = await this.feishuService.createCodeBlock(documentId, parentBlockId, code, language, wrap, index);
|
|
282
|
-
// Logger.log(`飞书代码块创建成功`);
|
|
283
|
-
//
|
|
284
|
-
// return {
|
|
285
|
-
// content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
286
|
-
// };
|
|
287
|
-
// } catch (error) {
|
|
288
|
-
// Logger.error(`创建飞书代码块失败:`, error);
|
|
289
|
-
// return {
|
|
290
|
-
// content: [{ type: "text", text: `创建飞书代码块失败: ${error}` }],
|
|
291
|
-
// };
|
|
292
|
-
// }
|
|
293
|
-
// },
|
|
294
|
-
// );
|
|
295
|
-
// 添加批量创建飞书块工具
|
|
296
|
-
this.server.tool('create_feishu_blocks', 'Create multiple blocks in a Feishu document at once with a single API call', {
|
|
297
|
-
documentId: z
|
|
298
|
-
.string()
|
|
299
|
-
.describe('Document ID or URL. Supported formats:\n1. Standard document URL: https://xxx.feishu.cn/docs/xxx or https://xxx.feishu.cn/docx/xxx\n2. API URL: https://open.feishu.cn/open-apis/doc/v2/documents/xxx\n3. Direct document ID (e.g., JcKbdlokYoPIe0xDzJ1cduRXnRf)'),
|
|
300
|
-
parentBlockId: z
|
|
301
|
-
.string()
|
|
302
|
-
.describe('Parent block ID (NOT URL) where the new blocks will be added as children. This should be the raw block ID without any URL prefix. When adding blocks at the page level (root level), use the extracted document ID from documentId parameter'),
|
|
303
|
-
children: z
|
|
304
|
-
.array(z.object({
|
|
305
|
-
block_type: z
|
|
306
|
-
.number()
|
|
307
|
-
.describe('Block type number: 2 for text, 3-11 for headings (level+2), 14 for code'),
|
|
308
|
-
content: z
|
|
309
|
-
.any()
|
|
310
|
-
.describe('Block content object that follows Feishu API format'),
|
|
311
|
-
}))
|
|
312
|
-
.describe('Array of block objects to create in a single API call'),
|
|
313
|
-
index: z
|
|
314
|
-
.number()
|
|
315
|
-
.optional()
|
|
316
|
-
.default(0)
|
|
317
|
-
.describe('Insertion position index. Default is 0 (insert at the beginning)'),
|
|
318
|
-
}, async ({ documentId, parentBlockId, children, index = 0 }) => {
|
|
151
|
+
// 添加获取块内容工具
|
|
152
|
+
this.server.tool('get_feishu_block_content', 'Retrieves the detailed content and structure of a specific block in a Feishu document. Useful for inspecting block properties, formatting, and content, especially before making updates or for debugging purposes. Note: For Feishu wiki links (https://xxx.feishu.cn/wiki/xxx) you must first use convert_feishu_wiki_to_document_id tool to obtain a compatible document ID.', {
|
|
153
|
+
documentId: DocumentIdSchema,
|
|
154
|
+
blockId: BlockIdSchema,
|
|
155
|
+
}, async ({ documentId, blockId }) => {
|
|
319
156
|
try {
|
|
320
157
|
if (!this.feishuService) {
|
|
321
158
|
return {
|
|
322
|
-
content: [
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
159
|
+
content: [{ type: 'text', text: '飞书服务未初始化,请检查配置' }],
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
Logger.info(`开始获取飞书块内容,文档ID: ${documentId},块ID: ${blockId}`);
|
|
163
|
+
const blockContent = await this.feishuService.getBlockContent(documentId, blockId);
|
|
164
|
+
Logger.info(`飞书块内容获取成功,块类型: ${blockContent.block_type}`);
|
|
165
|
+
return {
|
|
166
|
+
content: [{ type: 'text', text: JSON.stringify(blockContent, null, 2) }],
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
Logger.error(`获取飞书块内容失败:`, error);
|
|
171
|
+
const errorMessage = formatErrorMessage(error);
|
|
172
|
+
return {
|
|
173
|
+
content: [{ type: 'text', text: `获取飞书块内容失败: ${errorMessage}` }],
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
// 添加更新块文本内容工具
|
|
178
|
+
this.server.tool('update_feishu_block_text', 'Updates the text content and styling of a specific block in a Feishu document. Can be used to modify content in existing text, code, or heading blocks while preserving the block type and other properties. Note: For Feishu wiki links (https://xxx.feishu.cn/wiki/xxx) you must first use convert_feishu_wiki_to_document_id tool to obtain a compatible document ID.', {
|
|
179
|
+
documentId: DocumentIdSchema,
|
|
180
|
+
blockId: BlockIdSchema,
|
|
181
|
+
textElements: TextElementsArraySchema,
|
|
182
|
+
}, async ({ documentId, blockId, textElements }) => {
|
|
183
|
+
try {
|
|
184
|
+
if (!this.feishuService) {
|
|
185
|
+
return {
|
|
186
|
+
content: [{ type: 'text', text: '飞书服务未初始化,请检查配置' }],
|
|
328
187
|
};
|
|
329
188
|
}
|
|
330
|
-
Logger.
|
|
331
|
-
const result = await this.feishuService.
|
|
332
|
-
Logger.
|
|
189
|
+
Logger.info(`开始更新飞书块文本内容,文档ID: ${documentId},块ID: ${blockId}`);
|
|
190
|
+
const result = await this.feishuService.updateBlockTextContent(documentId, blockId, textElements);
|
|
191
|
+
Logger.info(`飞书块文本内容更新成功`);
|
|
333
192
|
return {
|
|
334
193
|
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
|
335
194
|
};
|
|
336
195
|
}
|
|
337
196
|
catch (error) {
|
|
338
|
-
Logger.error(
|
|
197
|
+
Logger.error(`更新飞书块文本内容失败:`, error);
|
|
198
|
+
const errorMessage = formatErrorMessage(error);
|
|
339
199
|
return {
|
|
340
|
-
content: [{ type: 'text', text:
|
|
200
|
+
content: [{ type: 'text', text: `更新飞书块文本内容失败: ${errorMessage}` }],
|
|
341
201
|
};
|
|
342
202
|
}
|
|
343
203
|
});
|
|
344
204
|
// 添加通用飞书块创建工具(支持文本、代码、标题)
|
|
345
|
-
this.server.tool('
|
|
346
|
-
documentId:
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
.string()
|
|
351
|
-
.describe('Parent block ID (NOT URL) where the new blocks will be added as children. This should be the raw block ID without any URL prefix. When adding blocks at the page level (root level), use the extracted document ID from documentId parameter'),
|
|
352
|
-
startIndex: z
|
|
353
|
-
.number()
|
|
354
|
-
.optional()
|
|
355
|
-
.default(0)
|
|
356
|
-
.describe('Starting insertion position index. Default is 0 (insert at the beginning). If individual blocks have their own index specified, those will take precedence'),
|
|
357
|
-
blocks: z
|
|
358
|
-
.array(z.object({
|
|
359
|
-
blockType: z
|
|
360
|
-
.enum(['text', 'code', 'heading'])
|
|
361
|
-
.describe("Type of block to create: 'text', 'code', or 'heading'"),
|
|
362
|
-
options: z
|
|
363
|
-
.object({
|
|
364
|
-
// 文本块选项 - 当blockType为'text'时使用
|
|
365
|
-
text: z
|
|
366
|
-
.object({
|
|
367
|
-
// 文本内容数组,每个元素包含文本内容和样式
|
|
368
|
-
textStyles: z
|
|
369
|
-
.array(z.object({
|
|
370
|
-
text: z.string().describe('Text segment content'),
|
|
371
|
-
style: z
|
|
372
|
-
.object({
|
|
373
|
-
bold: z
|
|
374
|
-
.boolean()
|
|
375
|
-
.optional()
|
|
376
|
-
.describe('Whether to make text bold. Default is false'),
|
|
377
|
-
italic: z
|
|
378
|
-
.boolean()
|
|
379
|
-
.optional()
|
|
380
|
-
.describe('Whether to make text italic. Default is false'),
|
|
381
|
-
underline: z
|
|
382
|
-
.boolean()
|
|
383
|
-
.optional()
|
|
384
|
-
.describe('Whether to add underline. Default is false'),
|
|
385
|
-
strikethrough: z
|
|
386
|
-
.boolean()
|
|
387
|
-
.optional()
|
|
388
|
-
.describe('Whether to add strikethrough. Default is false'),
|
|
389
|
-
inline_code: z
|
|
390
|
-
.boolean()
|
|
391
|
-
.optional()
|
|
392
|
-
.describe('Whether to format as inline code. Default is false'),
|
|
393
|
-
text_color: z
|
|
394
|
-
.number()
|
|
395
|
-
.optional()
|
|
396
|
-
.describe('Text color as a number. Default is 0'),
|
|
397
|
-
})
|
|
398
|
-
.optional()
|
|
399
|
-
.describe('Text style settings'),
|
|
400
|
-
}))
|
|
401
|
-
.optional()
|
|
402
|
-
.describe('Array of text content objects with styles. If not provided, content will be used as plain text'),
|
|
403
|
-
align: z
|
|
404
|
-
.number()
|
|
405
|
-
.optional()
|
|
406
|
-
.default(1)
|
|
407
|
-
.describe('Text alignment: 1 for left, 2 for center, 3 for right. Default is 1'),
|
|
408
|
-
})
|
|
409
|
-
.optional()
|
|
410
|
-
.describe("Text block options. Only used when blockType is 'text'"),
|
|
411
|
-
// 代码块选项 - 当blockType为'code'时使用
|
|
412
|
-
code: z
|
|
413
|
-
.object({
|
|
414
|
-
code: z.string().describe('Code content'),
|
|
415
|
-
language: z
|
|
416
|
-
.number()
|
|
417
|
-
.optional()
|
|
418
|
-
.default(0)
|
|
419
|
-
.describe('Programming language code as a number. Available options:\n1: PlainText, 2: ABAP, 3: Ada, 4: Apache, 5: Apex, 6: Assembly Language, 7: Bash, 8: CSharp, 9: C++, 10: C, 11: COBOL, 12: CSS, 13: CoffeeScript, 14: D, 15: Dart, 16: Delphi, 17: Django, 18: Dockerfile, 19: Erlang, 20: Fortran, 22: Go, 23: Groovy, 24: HTML, 25: HTMLBars, 26: HTTP, 27: Haskell, 28: JSON, 29: Java, 30: JavaScript, 31: Julia, 32: Kotlin, 33: LateX, 34: Lisp, 36: Lua, 37: MATLAB, 38: Makefile, 39: Markdown, 40: Nginx, 41: Objective-C, 43: PHP, 44: Perl, 46: Power Shell, 47: Prolog, 48: ProtoBuf, 49: Python, 50: R, 52: Ruby, 53: Rust, 54: SAS, 55: SCSS, 56: SQL, 57: Scala, 58: Scheme, 60: Shell, 61: Swift, 62: Thrift, 63: TypeScript, 64: VBScript, 65: Visual Basic, 66: XML, 67: YAML, 68: CMake, 69: Diff, 70: Gherkin, 71: GraphQL, 72: OpenGL Shading Language, 73: Properties, 74: Solidity, 75: TOML'),
|
|
420
|
-
wrap: z
|
|
421
|
-
.boolean()
|
|
422
|
-
.optional()
|
|
423
|
-
.default(false)
|
|
424
|
-
.describe('Whether to enable automatic line wrapping for code blocks. Default is false'),
|
|
425
|
-
})
|
|
426
|
-
.optional()
|
|
427
|
-
.describe("Code block options. Only used when blockType is 'code'"),
|
|
428
|
-
// 标题块选项 - 当blockType为'heading'时使用
|
|
429
|
-
heading: z
|
|
430
|
-
.object({
|
|
431
|
-
level: z
|
|
432
|
-
.number()
|
|
433
|
-
.min(1)
|
|
434
|
-
.max(9)
|
|
435
|
-
.describe('Heading level from 1 to 9, where 1 is the largest heading (h1) and 9 is the smallest (h9)'),
|
|
436
|
-
content: z.string().describe('Heading text content'),
|
|
437
|
-
align: z
|
|
438
|
-
.number()
|
|
439
|
-
.optional()
|
|
440
|
-
.default(1)
|
|
441
|
-
.describe('Text alignment: 1 for left, 2 for center, 3 for right. Default is 1'),
|
|
442
|
-
})
|
|
443
|
-
.optional()
|
|
444
|
-
.describe("Heading block options. Only used when blockType is 'heading'"),
|
|
445
|
-
})
|
|
446
|
-
.optional()
|
|
447
|
-
.default({}),
|
|
448
|
-
}))
|
|
449
|
-
.describe('Array of block configurations to create in a single API call'),
|
|
205
|
+
this.server.tool('batch_create_feishu_blocks', 'RECOMMENDED: Creates multiple blocks of different types (text, code, heading, list) in a single efficient API call. This tool should be PREFERRED OVER individual block creation tools when creating multiple consecutive blocks at the same position. Significantly improves performance and reduces API calls by up to 90% compared to creating blocks individually. AUTOMATICALLY handles batching for large number of blocks (>50) by splitting into multiple requests. For specific block positioning at different locations, use individual block creation tools instead. For error recovery, use get_feishu_document_blocks to check the document state. Note: For Feishu wiki links (https://xxx.feishu.cn/wiki/xxx) you must first use convert_feishu_wiki_to_document_id tool to obtain a compatible document ID.', {
|
|
206
|
+
documentId: DocumentIdSchema,
|
|
207
|
+
parentBlockId: ParentBlockIdSchema,
|
|
208
|
+
startIndex: StartIndexSchema,
|
|
209
|
+
blocks: z.array(BlockConfigSchema).describe('Array of block configurations (required). Each element contains blockType and options properties. Example: [{blockType:"text",options:{text:{textStyles:[{text:"Hello",style:{bold:true}}]}}},{blockType:"code",options:{code:{code:"console.log(\'Hello\')",language:30}}}]. Handles any number of blocks by automatically batching in groups of 50.'),
|
|
450
210
|
}, async ({ documentId, parentBlockId, startIndex = 0, blocks }) => {
|
|
451
211
|
try {
|
|
452
212
|
if (!this.feishuService) {
|
|
@@ -459,127 +219,281 @@ export class FeishuMcpServer {
|
|
|
459
219
|
],
|
|
460
220
|
};
|
|
461
221
|
}
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
options
|
|
504
|
-
|
|
222
|
+
// 如果块数量不超过50,直接调用一次API
|
|
223
|
+
if (blocks.length <= 50) {
|
|
224
|
+
Logger.info(`开始批量创建飞书块,文档ID: ${documentId},父块ID: ${parentBlockId},块数量: ${blocks.length},起始插入位置: ${startIndex}`);
|
|
225
|
+
// 准备要创建的块内容数组
|
|
226
|
+
const blockContents = [];
|
|
227
|
+
// 处理每个块配置
|
|
228
|
+
for (const blockConfig of blocks) {
|
|
229
|
+
const { blockType, options = {} } = blockConfig;
|
|
230
|
+
// 创建块内容
|
|
231
|
+
const blockContent = this.feishuService.createBlockContent(blockType, options);
|
|
232
|
+
if (blockContent) {
|
|
233
|
+
blockContents.push(blockContent);
|
|
234
|
+
Logger.info(`已准备${blockType}块,内容: ${JSON.stringify(blockContent).substring(0, 100)}...`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
// 批量创建所有块
|
|
238
|
+
const result = await this.feishuService.createDocumentBlocks(documentId, parentBlockId, blockContents, startIndex);
|
|
239
|
+
Logger.info(`飞书块批量创建成功,共创建 ${blockContents.length} 个块`);
|
|
240
|
+
return {
|
|
241
|
+
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
// 如果块数量超过50,需要分批处理
|
|
246
|
+
Logger.info(`块数量(${blocks.length})超过50,将分批创建`);
|
|
247
|
+
const batchSize = 50; // 每批最大50个
|
|
248
|
+
const totalBatches = Math.ceil(blocks.length / batchSize);
|
|
249
|
+
const results = [];
|
|
250
|
+
let currentStartIndex = startIndex;
|
|
251
|
+
let createdBlocksCount = 0;
|
|
252
|
+
let allBatchesSuccess = true;
|
|
253
|
+
// 分批创建块
|
|
254
|
+
for (let batchNum = 0; batchNum < totalBatches; batchNum++) {
|
|
255
|
+
const batchStart = batchNum * batchSize;
|
|
256
|
+
const batchEnd = Math.min((batchNum + 1) * batchSize, blocks.length);
|
|
257
|
+
const currentBatch = blocks.slice(batchStart, batchEnd);
|
|
258
|
+
Logger.info(`处理第 ${batchNum + 1}/${totalBatches} 批,起始位置: ${currentStartIndex},块数量: ${currentBatch.length}`);
|
|
259
|
+
try {
|
|
260
|
+
// 准备当前批次的块内容
|
|
261
|
+
const batchBlockContents = [];
|
|
262
|
+
for (const blockConfig of currentBatch) {
|
|
263
|
+
const { blockType, options = {} } = blockConfig;
|
|
264
|
+
const blockContent = this.feishuService.createBlockContent(blockType, options);
|
|
265
|
+
if (blockContent) {
|
|
266
|
+
batchBlockContents.push(blockContent);
|
|
267
|
+
}
|
|
505
268
|
}
|
|
506
|
-
|
|
507
|
-
const
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
269
|
+
// 批量创建当前批次的块
|
|
270
|
+
const batchResult = await this.feishuService.createDocumentBlocks(documentId, parentBlockId, batchBlockContents, currentStartIndex);
|
|
271
|
+
results.push(batchResult);
|
|
272
|
+
// 计算下一批的起始位置(当前位置+已创建块数量)
|
|
273
|
+
// 注意:每批成功创建后,需要将起始索引更新为当前索引 + 已创建块数量
|
|
274
|
+
createdBlocksCount += batchBlockContents.length;
|
|
275
|
+
currentStartIndex = startIndex + createdBlocksCount;
|
|
276
|
+
Logger.info(`第 ${batchNum + 1}/${totalBatches} 批创建成功,当前已创建 ${createdBlocksCount} 个块`);
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
Logger.error(`第 ${batchNum + 1}/${totalBatches} 批创建失败:`, error);
|
|
280
|
+
allBatchesSuccess = false;
|
|
281
|
+
// 如果有批次失败,返回详细错误信息
|
|
282
|
+
const errorMessage = formatErrorMessage(error);
|
|
283
|
+
return {
|
|
284
|
+
content: [
|
|
285
|
+
{
|
|
286
|
+
type: 'text',
|
|
287
|
+
text: `批量创建飞书块部分失败:第 ${batchNum + 1}/${totalBatches} 批处理时出错。\n\n` +
|
|
288
|
+
`已成功创建 ${createdBlocksCount} 个块,但还有 ${blocks.length - createdBlocksCount} 个块未能创建。\n\n` +
|
|
289
|
+
`错误信息: ${errorMessage}\n\n` +
|
|
290
|
+
`建议使用 get_feishu_document_blocks 工具获取文档最新状态,确认已创建的内容,然后从索引位置 ${currentStartIndex} 继续创建剩余块。`
|
|
291
|
+
}
|
|
292
|
+
],
|
|
293
|
+
};
|
|
294
|
+
}
|
|
512
295
|
}
|
|
513
|
-
if (
|
|
514
|
-
|
|
515
|
-
|
|
296
|
+
if (allBatchesSuccess) {
|
|
297
|
+
Logger.info(`所有批次创建成功,共创建 ${createdBlocksCount} 个块`);
|
|
298
|
+
return {
|
|
299
|
+
content: [
|
|
300
|
+
{
|
|
301
|
+
type: 'text',
|
|
302
|
+
text: `所有飞书块创建成功,共分 ${totalBatches} 批创建了 ${createdBlocksCount} 个块。\n\n` +
|
|
303
|
+
`最后一批结果: ${JSON.stringify(results[results.length - 1], null, 2)}`
|
|
304
|
+
}
|
|
305
|
+
],
|
|
306
|
+
};
|
|
516
307
|
}
|
|
517
308
|
}
|
|
518
|
-
//
|
|
519
|
-
const result = await this.feishuService.createDocumentBlocks(documentId, parentBlockId, blockContents, startIndex);
|
|
520
|
-
Logger.log(`飞书块批量创建成功,共创建 ${blockContents.length} 个块`);
|
|
309
|
+
// 这个return语句是为了避免TypeScript错误,实际上代码永远不会执行到这里
|
|
521
310
|
return {
|
|
522
|
-
content: [{ type: 'text', text:
|
|
311
|
+
content: [{ type: 'text', text: '操作完成' }],
|
|
523
312
|
};
|
|
524
313
|
}
|
|
525
314
|
catch (error) {
|
|
526
315
|
Logger.error(`批量创建飞书块失败:`, error);
|
|
316
|
+
const errorMessage = formatErrorMessage(error);
|
|
317
|
+
return {
|
|
318
|
+
content: [
|
|
319
|
+
{
|
|
320
|
+
type: 'text',
|
|
321
|
+
text: `批量创建飞书块失败: ${errorMessage}\n\n` +
|
|
322
|
+
`建议使用 get_feishu_document_blocks 工具获取文档当前状态,确认是否有部分内容已创建成功。`
|
|
323
|
+
}
|
|
324
|
+
],
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
// 添加创建飞书文本块工具
|
|
329
|
+
this.server.tool("create_feishu_text_block", "Creates a new text block with precise style control. Unlike markdown-based formatting, this tool lets you explicitly set text styles for each text segment. Ideal for formatted documents where exact styling control is needed. NOTE: If creating multiple blocks at once, use batch_create_feishu_blocks tool instead for better efficiency. Note: For Feishu wiki links (https://xxx.feishu.cn/wiki/xxx) you must first use convert_feishu_wiki_to_document_id tool to obtain a compatible document ID.", {
|
|
330
|
+
documentId: DocumentIdSchema,
|
|
331
|
+
parentBlockId: ParentBlockIdSchema,
|
|
332
|
+
textContents: TextElementsArraySchema,
|
|
333
|
+
align: AlignSchema,
|
|
334
|
+
index: IndexSchema
|
|
335
|
+
}, async ({ documentId, parentBlockId, textContents, align = 1, index }) => {
|
|
336
|
+
try {
|
|
337
|
+
if (!this.feishuService) {
|
|
338
|
+
return {
|
|
339
|
+
content: [{ type: "text", text: "Feishu service is not initialized. Please check the configuration" }],
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
Logger.info(`开始创建飞书文本块,文档ID: ${documentId},父块ID: ${parentBlockId},对齐方式: ${align},插入位置: ${index}`);
|
|
343
|
+
const result = await this.feishuService.createTextBlock(documentId, parentBlockId, textContents, align, index);
|
|
344
|
+
Logger.info(`飞书文本块创建成功`);
|
|
345
|
+
return {
|
|
346
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
catch (error) {
|
|
350
|
+
Logger.error(`创建飞书文本块失败:`, error);
|
|
351
|
+
const errorMessage = formatErrorMessage(error);
|
|
352
|
+
return {
|
|
353
|
+
content: [{ type: "text", text: `创建飞书文本块失败: ${errorMessage}` }],
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
// 添加创建飞书代码块工具
|
|
358
|
+
this.server.tool("create_feishu_code_block", "Creates a new code block with syntax highlighting and formatting options. Ideal for technical documentation, tutorials, or displaying code examples with proper formatting and language-specific highlighting. NOTE: If creating multiple blocks at once, use batch_create_feishu_blocks tool instead for better efficiency. Note: For Feishu wiki links (https://xxx.feishu.cn/wiki/xxx) you must first use convert_feishu_wiki_to_document_id tool to obtain a compatible document ID.", {
|
|
359
|
+
documentId: DocumentIdSchema,
|
|
360
|
+
parentBlockId: ParentBlockIdSchema,
|
|
361
|
+
code: z.string().describe("Code content (required). The complete code text to display."),
|
|
362
|
+
language: CodeLanguageSchema,
|
|
363
|
+
wrap: CodeWrapSchema,
|
|
364
|
+
index: IndexSchema
|
|
365
|
+
}, async ({ documentId, parentBlockId, code, language = 1, wrap = false, index = 0 }) => {
|
|
366
|
+
try {
|
|
367
|
+
if (!this.feishuService) {
|
|
368
|
+
return {
|
|
369
|
+
content: [{ type: "text", text: "Feishu service is not initialized. Please check the configuration" }],
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
Logger.info(`开始创建飞书代码块,文档ID: ${documentId},父块ID: ${parentBlockId},语言: ${language},自动换行: ${wrap},插入位置: ${index}`);
|
|
373
|
+
const result = await this.feishuService.createCodeBlock(documentId, parentBlockId, code, language, wrap, index);
|
|
374
|
+
Logger.info(`飞书代码块创建成功`);
|
|
375
|
+
return {
|
|
376
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
Logger.error(`创建飞书代码块失败:`, error);
|
|
381
|
+
const errorMessage = formatErrorMessage(error);
|
|
382
|
+
return {
|
|
383
|
+
content: [{ type: "text", text: `创建飞书代码块失败: ${errorMessage}` }],
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
// 添加创建飞书标题块工具
|
|
388
|
+
this.server.tool("create_feishu_heading_block", "Creates a heading block with customizable level and alignment. Use this tool to add section titles, chapter headings, or any hierarchical structure elements to your document. Supports nine heading levels for different emphasis needs. NOTE: If creating multiple blocks at once, use batch_create_feishu_blocks tool instead for better efficiency. Note: For Feishu wiki links (https://xxx.feishu.cn/wiki/xxx) you must first use convert_feishu_wiki_to_document_id tool to obtain a compatible document ID.", {
|
|
389
|
+
documentId: DocumentIdSchema,
|
|
390
|
+
parentBlockId: ParentBlockIdSchema,
|
|
391
|
+
level: z.number().min(1).max(9).describe("Heading level (required). Integer between 1 and 9, where 1 is the largest heading (h1) and 9 is the smallest (h9)."),
|
|
392
|
+
content: z.string().describe("Heading text content (required). The actual text of the heading."),
|
|
393
|
+
align: AlignSchemaWithValidation,
|
|
394
|
+
index: IndexSchema
|
|
395
|
+
}, async ({ documentId, parentBlockId, level, content, align = 1, index = 0 }) => {
|
|
396
|
+
try {
|
|
397
|
+
if (!this.feishuService) {
|
|
398
|
+
return {
|
|
399
|
+
content: [{ type: "text", text: "Feishu service is not initialized. Please check the configuration" }],
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
// 确保align值在合法范围内(1-3)
|
|
403
|
+
if (align !== 1 && align !== 2 && align !== 3) {
|
|
404
|
+
return {
|
|
405
|
+
content: [{ type: "text", text: "错误: 对齐方式(align)参数必须是1(居左)、2(居中)或3(居右)中的一个值。" }],
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
Logger.info(`开始创建飞书标题块,文档ID: ${documentId},父块ID: ${parentBlockId},标题级别: ${level},对齐方式: ${align},插入位置: ${index}`);
|
|
409
|
+
const result = await this.feishuService.createHeadingBlock(documentId, parentBlockId, content, level, index, align);
|
|
410
|
+
Logger.info(`飞书标题块创建成功`);
|
|
411
|
+
return {
|
|
412
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
catch (error) {
|
|
416
|
+
Logger.error(`创建飞书标题块失败:`, error);
|
|
417
|
+
const errorMessage = formatErrorMessage(error);
|
|
418
|
+
return {
|
|
419
|
+
content: [{ type: "text", text: `创建飞书标题块失败: ${errorMessage}` }],
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
// 添加创建飞书列表块工具
|
|
424
|
+
this.server.tool("create_feishu_list_block", "Creates a list item block (either ordered or unordered). Perfect for creating hierarchical and structured content with bullet points or numbered lists. NOTE: If creating multiple blocks at once, use batch_create_feishu_blocks tool instead for better efficiency. Note: For Feishu wiki links (https://xxx.feishu.cn/wiki/xxx) you must first use convert_feishu_wiki_to_document_id tool to obtain a compatible document ID.", {
|
|
425
|
+
documentId: DocumentIdSchema,
|
|
426
|
+
parentBlockId: ParentBlockIdSchema,
|
|
427
|
+
content: z.string().describe("List item content (required). The actual text of the list item."),
|
|
428
|
+
isOrdered: z.boolean().optional().default(false).describe("Whether this is an ordered (numbered) list item. Default is false (bullet point/unordered)."),
|
|
429
|
+
align: AlignSchemaWithValidation,
|
|
430
|
+
index: IndexSchema
|
|
431
|
+
}, async ({ documentId, parentBlockId, content, isOrdered = false, align = 1, index = 0 }) => {
|
|
432
|
+
try {
|
|
433
|
+
if (!this.feishuService) {
|
|
434
|
+
return {
|
|
435
|
+
content: [{ type: "text", text: "Feishu service is not initialized. Please check the configuration" }],
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
// 确保align值在合法范围内(1-3)
|
|
439
|
+
if (align !== 1 && align !== 2 && align !== 3) {
|
|
440
|
+
return {
|
|
441
|
+
content: [{ type: "text", text: "错误: 对齐方式(align)参数必须是1(居左)、2(居中)或3(居右)中的一个值。" }],
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
const listType = isOrdered ? "有序" : "无序";
|
|
445
|
+
Logger.info(`开始创建飞书${listType}列表块,文档ID: ${documentId},父块ID: ${parentBlockId},对齐方式: ${align},插入位置: ${index}`);
|
|
446
|
+
const result = await this.feishuService.createListBlock(documentId, parentBlockId, content, isOrdered, index, align);
|
|
447
|
+
Logger.info(`飞书${listType}列表块创建成功`);
|
|
448
|
+
return {
|
|
449
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
catch (error) {
|
|
453
|
+
Logger.error(`创建飞书列表块失败:`, error);
|
|
454
|
+
const errorMessage = formatErrorMessage(error);
|
|
455
|
+
return {
|
|
456
|
+
content: [{ type: "text", text: `创建飞书列表块失败: ${errorMessage}` }],
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
// 添加飞书Wiki文档ID转换工具
|
|
461
|
+
this.server.tool('convert_feishu_wiki_to_document_id', 'Converts a Feishu Wiki document link to a compatible document ID. This conversion is required before using wiki links with any other Feishu document tools.', {
|
|
462
|
+
wikiUrl: z.string().describe('Wiki URL or Token (required). Supports complete URL formats like https://xxx.feishu.cn/wiki/xxxxx or direct use of the Token portion'),
|
|
463
|
+
}, async ({ wikiUrl }) => {
|
|
464
|
+
try {
|
|
465
|
+
if (!this.feishuService) {
|
|
466
|
+
return {
|
|
467
|
+
content: [{ type: 'text', text: '飞书服务未初始化,请检查配置' }],
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
Logger.info(`开始转换Wiki文档链接,输入: ${wikiUrl}`);
|
|
471
|
+
const documentId = await this.feishuService.convertWikiToDocumentId(wikiUrl);
|
|
472
|
+
Logger.info(`Wiki文档转换成功,可用的文档ID为: ${documentId}`);
|
|
473
|
+
return {
|
|
474
|
+
content: [
|
|
475
|
+
{ type: 'text', text: `Converted Wiki link to Document ID: ${documentId}\n\nUse this Document ID with other Feishu document tools.` }
|
|
476
|
+
],
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
catch (error) {
|
|
480
|
+
Logger.error(`转换Wiki文档链接失败:`, error);
|
|
481
|
+
const errorMessage = formatErrorMessage(error);
|
|
527
482
|
return {
|
|
528
|
-
content: [{ type: 'text', text:
|
|
483
|
+
content: [{ type: 'text', text: `转换Wiki文档链接失败: ${errorMessage}` }],
|
|
529
484
|
};
|
|
530
485
|
}
|
|
531
486
|
});
|
|
532
|
-
// // 添加创建飞书标题块工具
|
|
533
|
-
// this.server.tool(
|
|
534
|
-
// "create_feishu_heading_block",
|
|
535
|
-
// "Create a heading block in a Feishu document with specified level (1-9)",
|
|
536
|
-
// {
|
|
537
|
-
// documentId: z.string().describe("Document ID or URL. Supported formats:\n1. Standard document URL: https://xxx.feishu.cn/docs/xxx or https://xxx.feishu.cn/docx/xxx\n2. API URL: https://open.feishu.cn/open-apis/doc/v2/documents/xxx\n3. Direct document ID (e.g., JcKbdlokYoPIe0xDzJ1cduRXnRf)"),
|
|
538
|
-
// parentBlockId: z.string().describe("Parent block ID (NOT URL) where the new block will be added as a child. This should be the raw block ID without any URL prefix. When adding blocks at the page level (root level), use the extracted document ID from documentId parameter"),
|
|
539
|
-
// level: z.number().min(1).max(9).describe("Heading level from 1 to 9, where 1 is the largest heading (h1) and 9 is the smallest (h9)"),
|
|
540
|
-
// content: z.string().describe("Heading text content"),
|
|
541
|
-
// index: z.number().optional().default(0).describe("Insertion position index. Default is 0 (insert at the beginning). If unsure about the position, use the get_feishu_doc_blocks tool first to understand the document structure. For consecutive insertions, calculate the next position as previous_index + 1 to avoid querying document structure repeatedly")
|
|
542
|
-
// },
|
|
543
|
-
// async ({ documentId, parentBlockId, level, content, index = 0 }) => {
|
|
544
|
-
// try {
|
|
545
|
-
// if (!this.feishuService) {
|
|
546
|
-
// return {
|
|
547
|
-
// content: [{ type: "text", text: "Feishu service is not initialized. Please check the configuration" }],
|
|
548
|
-
// };
|
|
549
|
-
// }
|
|
550
|
-
//
|
|
551
|
-
// Logger.log(`开始创建飞书标题块,文档ID: ${documentId},父块ID: ${parentBlockId},标题级别: ${level},插入位置: ${index}`);
|
|
552
|
-
// const result = await this.feishuService.createHeadingBlock(documentId, parentBlockId, content, level, index);
|
|
553
|
-
// Logger.log(`飞书标题块创建成功`);
|
|
554
|
-
//
|
|
555
|
-
// return {
|
|
556
|
-
// content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
557
|
-
// };
|
|
558
|
-
// } catch (error) {
|
|
559
|
-
// Logger.error(`创建飞书标题块失败:`, error);
|
|
560
|
-
// return {
|
|
561
|
-
// content: [{ type: "text", text: `创建飞书标题块失败: ${error}` }],
|
|
562
|
-
// };
|
|
563
|
-
// }
|
|
564
|
-
// },
|
|
565
|
-
// );
|
|
566
487
|
}
|
|
567
488
|
async connect(transport) {
|
|
568
|
-
// Logger.log("Connecting to transport...");
|
|
569
489
|
await this.server.connect(transport);
|
|
570
|
-
Logger.
|
|
571
|
-
this.server.server.sendLoggingMessage({
|
|
572
|
-
level: 'info',
|
|
573
|
-
data: args,
|
|
574
|
-
});
|
|
490
|
+
Logger.info = (...args) => {
|
|
491
|
+
this.server.server.sendLoggingMessage({ level: 'info', data: args });
|
|
575
492
|
};
|
|
576
493
|
Logger.error = (...args) => {
|
|
577
|
-
this.server.server.sendLoggingMessage({
|
|
578
|
-
level: 'error',
|
|
579
|
-
data: args,
|
|
580
|
-
});
|
|
494
|
+
this.server.server.sendLoggingMessage({ level: 'error', data: args });
|
|
581
495
|
};
|
|
582
|
-
Logger.
|
|
496
|
+
Logger.info('Server connected and ready to process requests');
|
|
583
497
|
}
|
|
584
498
|
async startHttpServer(port) {
|
|
585
499
|
const app = express();
|
|
@@ -595,12 +509,12 @@ export class FeishuMcpServer {
|
|
|
595
509
|
}
|
|
596
510
|
await this.sseTransport.handlePostMessage(req, res);
|
|
597
511
|
});
|
|
598
|
-
Logger.
|
|
512
|
+
Logger.info = console.log;
|
|
599
513
|
Logger.error = console.error;
|
|
600
514
|
app.listen(port, () => {
|
|
601
|
-
Logger.
|
|
602
|
-
Logger.
|
|
603
|
-
Logger.
|
|
515
|
+
Logger.info(`HTTP server listening on port ${port}`);
|
|
516
|
+
Logger.info(`SSE endpoint available at http://localhost:${port}/sse`);
|
|
517
|
+
Logger.info(`Message endpoint available at http://localhost:${port}/messages`);
|
|
604
518
|
});
|
|
605
519
|
}
|
|
606
520
|
}
|