2ch commited on
Commit
b44a9de
·
verified ·
1 Parent(s): f306bce

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +25 -72
server.js CHANGED
@@ -1,7 +1,4 @@
1
- import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2
- import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3
  import express from 'express';
4
- import { ErrorCode, McpError, ListToolsRequestSchema, CallToolRequestSchema } from "@modelcontextprotocol/sdk/types.js";
5
  import axios from 'axios';
6
  import { Readability } from '@mozilla/readability';
7
  import { JSDOM } from 'jsdom';
@@ -51,77 +48,40 @@ class WebsiteParser {
51
  }
52
  }
53
 
54
- // Create MCP server instance
55
- const server = new Server({
56
- name: "server-readability-parser",
57
- version: "1.0.0"
58
- }, {
59
- capabilities: { tools: {} }
60
- });
61
-
62
  const parser = new WebsiteParser();
63
 
64
- // Define available tools
65
- server.setRequestHandler(ListToolsRequestSchema, async () => ({
66
- tools: [{
67
- name: "parse",
68
- description: "Extracts and transforms webpage content into clean, LLM-optimized Markdown. Returns article title, main content, excerpt, byline and site name. Uses Mozilla's Readability algorithm to remove ads, navigation, footers and non-essential elements while preserving the core content structure.",
69
- inputSchema: {
70
- type: "object",
71
- properties: {
72
- url: {
73
- type: "string",
74
- description: "The website URL to parse"
75
- }
76
- },
77
- required: ["url"]
78
- }
79
- }]
80
- }));
81
-
82
- // Handle tool execution
83
- server.setRequestHandler(CallToolRequestSchema, async (request) => {
84
- const { name, arguments: args } = request.params;
85
-
86
- if (name !== "parse") {
87
- throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`);
88
- }
89
-
90
- if (!args?.url) {
91
- throw new McpError(ErrorCode.InvalidParams, "URL is required");
92
- }
93
 
 
 
94
  try {
95
- const result = await parser.fetchAndParse(args.url);
 
 
 
 
96
 
97
- return {
98
- content: [{
99
- type: "text",
100
- text: JSON.stringify({
101
- title: result.title,
102
- content: result.content,
103
- metadata: {
104
- excerpt: result.excerpt,
105
- byline: result.byline,
106
- siteName: result.siteName
107
- }
108
- }, null, 2)
109
- }]
110
- };
111
  } catch (error) {
112
- return {
113
- isError: true,
114
- content: [{
115
- type: "text",
116
- text: `Error: ${error.message}`
117
- }]
118
- };
119
  }
120
  });
121
 
122
- const app = express();
123
- const PORT = process.env.PORT || 7860;
124
-
125
  app.get('/', (req, res) => {
126
  res.send('Website Parser Service is running');
127
  });
@@ -129,10 +89,3 @@ app.get('/', (req, res) => {
129
  app.listen(PORT, () => {
130
  console.log(`Server running on port ${PORT}`);
131
  });
132
-
133
-
134
- const transport = new StdioServerTransport();
135
- server.connect(transport).catch(error => {
136
- console.error(`Server failed to start: ${error.message}`);
137
- process.exit(1);
138
- });
 
 
 
1
  import express from 'express';
 
2
  import axios from 'axios';
3
  import { Readability } from '@mozilla/readability';
4
  import { JSDOM } from 'jsdom';
 
48
  }
49
  }
50
 
51
+ const app = express();
52
+ const PORT = process.env.PORT || 7860;
 
 
 
 
 
 
53
  const parser = new WebsiteParser();
54
 
55
+ // Парсинг JSON тела запроса
56
+ app.use(express.json());
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
+ // Эндпоинт для парсинга URL
59
+ app.post('/parse', async (req, res) => {
60
  try {
61
+ const { url } = req.body;
62
+
63
+ if (!url) {
64
+ return res.status(400).json({ error: 'URL is required' });
65
+ }
66
 
67
+ const result = await parser.fetchAndParse(url);
68
+
69
+ res.json({
70
+ title: result.title,
71
+ content: result.content,
72
+ metadata: {
73
+ excerpt: result.excerpt,
74
+ byline: result.byline,
75
+ siteName: result.siteName
76
+ }
77
+ });
 
 
 
78
  } catch (error) {
79
+ res.status(500).json({
80
+ error: error.message
81
+ });
 
 
 
 
82
  }
83
  });
84
 
 
 
 
85
  app.get('/', (req, res) => {
86
  res.send('Website Parser Service is running');
87
  });
 
89
  app.listen(PORT, () => {
90
  console.log(`Server running on port ${PORT}`);
91
  });