html-parser-mcp 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/index.js +65 -26
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,11 +1,11 @@
1
- // index.js
1
+ #!/usr/bin/env node
2
2
  const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
3
3
  const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
4
4
  const cheerio = require('cheerio');
5
5
  const fs = require('fs');
6
6
 
7
7
  const server = new Server({
8
- name: 'html-parser',
8
+ name: 'html-parser-mcp',
9
9
  version: '1.0.0'
10
10
  }, {
11
11
  capabilities: {
@@ -30,35 +30,74 @@ server.setRequestHandler('tools/list', async () => ({
30
30
 
31
31
  // 处理工具调用
32
32
  server.setRequestHandler('tools/call', async (request) => {
33
- if (request.params.name === 'parse_html') {
34
- const { filePath } = request.params.arguments;
35
- const html = fs.readFileSync(filePath, 'utf-8');
36
- const $ = cheerio.load(html);
37
-
38
- const groups = [];
39
- $('.section-header, h2, h3, [class*="header"]').each((i, header) => {
40
- const group = { name: $(header).text().trim(), fields: [] };
41
- let next = $(header).next();
42
- while (next.length && !next.is('.section-header, h2, h3, [class*="header"]')) {
43
- if (next.is('.form-row, .form-group, [class*="form"]')) {
44
- const label = next.find('label').first().text().replace('*', '').trim();
45
- const input = next.find('input, select').first();
46
- const name = input.attr('name');
47
- if (name) {
48
- group.fields.push({ name, label });
33
+ const { name, arguments: args } = request.params;
34
+
35
+ if (name === 'parse_html') {
36
+ try {
37
+ const { filePath } = args;
38
+
39
+ // 检查文件是否存在
40
+ if (!fs.existsSync(filePath)) {
41
+ throw new Error(`文件不存在: ${filePath}`);
42
+ }
43
+
44
+ const html = fs.readFileSync(filePath, 'utf-8');
45
+ const $ = cheerio.load(html);
46
+
47
+ const groups = [];
48
+ $('.section-header, h2, h3, [class*="header"]').each((i, header) => {
49
+ const group = { name: $(header).text().trim(), fields: [] };
50
+ let next = $(header).next();
51
+ while (next.length && !next.is('.section-header, h2, h3, [class*="header"]')) {
52
+ if (next.is('.form-row, .form-group, [class*="form"]')) {
53
+ const label = next.find('label').first().text().replace('*', '').trim();
54
+ const input = next.find('input, select').first();
55
+ const name = input.attr('name');
56
+ if (name) {
57
+ group.fields.push({ name, label });
58
+ }
49
59
  }
60
+ next = next.next();
50
61
  }
51
- next = next.next();
62
+ if (group.fields.length) groups.push(group);
63
+ });
64
+
65
+ // 如果没有找到任何分组,返回默认分组
66
+ if (groups.length === 0) {
67
+ groups.push({ name: '表单字段', fields: [] });
52
68
  }
53
- if (group.fields.length) groups.push(group);
54
- });
55
-
56
- return {
57
- content: [{ type: 'text', text: JSON.stringify(groups, null, 2) }]
58
- };
69
+
70
+ return {
71
+ content: [{ type: 'text', text: JSON.stringify(groups, null, 2) }]
72
+ };
73
+
74
+ } catch (error) {
75
+ // 返回错误信息
76
+ return {
77
+ content: [{ type: 'text', text: JSON.stringify({ error: error.message }, null, 2) }],
78
+ isError: true
79
+ };
80
+ }
59
81
  }
82
+
83
+ // 未知工具
84
+ throw new Error(`未知工具: ${name}`);
85
+ });
86
+
87
+ // 错误处理:捕获未处理的异常
88
+ process.on('uncaughtException', (error) => {
89
+ console.error('Uncaught Exception:', error);
90
+ process.exit(1);
91
+ });
92
+
93
+ process.on('unhandledRejection', (error) => {
94
+ console.error('Unhandled Rejection:', error);
95
+ process.exit(1);
60
96
  });
61
97
 
62
98
  // 启动
63
99
  const transport = new StdioServerTransport();
64
- server.connect(transport);
100
+ server.connect(transport).catch(error => {
101
+ console.error('Failed to connect:', error);
102
+ process.exit(1);
103
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "html-parser-mcp",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {