-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathindex.js
More file actions
282 lines (270 loc) · 8.5 KB
/
index.js
File metadata and controls
282 lines (270 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
// Import tool modules
import * as fsTools from "./tools/fsTools.js";
import * as spiderTools from "./tools/spiderTools.js";
import * as dbTools from "./tools/dbTools.js";
import * as systemTools from "./tools/systemTools.js";
const server = new Server(
{
name: "drpy-node-mcp",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);
// Map tool names to handler functions
const toolHandlers = {
// FS Tools
list_directory: fsTools.list_directory,
read_file: fsTools.read_file,
write_file: fsTools.write_file,
delete_file: fsTools.delete_file,
// Spider Tools
list_sources: spiderTools.list_sources,
get_routes_info: spiderTools.get_routes_info,
fetch_spider_url: spiderTools.fetch_spider_url,
debug_spider_rule: spiderTools.debug_spider_rule,
get_spider_template: spiderTools.get_spider_template,
get_drpy_libs_info: spiderTools.get_drpy_libs_info,
validate_spider: spiderTools.validate_spider,
check_syntax: spiderTools.check_syntax,
// DB Tools
sql_query: dbTools.sql_query,
// System Tools
read_logs: systemTools.read_logs,
manage_config: systemTools.manage_config,
restart_service: systemTools.restart_service,
};
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "list_directory",
description: "List files and directories in the project",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Directory path relative to project root (default: '.')",
},
},
},
},
{
name: "read_file",
description: "Read the content of a file (automatically decodes DS sources)",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "File path relative to project root",
},
},
required: ["path"],
},
},
{
name: "write_file",
description: "Write content to a file (creates directories if needed)",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "File path relative to project root",
},
content: {
type: "string",
description: "Content to write",
},
},
required: ["path", "content"],
},
},
{
name: "delete_file",
description: "Delete a file or directory",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Path relative to project root",
},
},
required: ["path"],
},
},
{
name: "list_sources",
description: "List all spider sources (js and catvod)",
inputSchema: {
type: "object",
properties: {}
},
},
{
name: "get_routes_info",
description: "Get information about registered routes/controllers",
inputSchema: {
type: "object",
properties: {}
},
},
{
name: "fetch_spider_url",
description: "Fetch a URL using drpy-node's request library to debug connectivity and anti-crawling measures (UA/headers).",
inputSchema: zodToJsonSchema(
z.object({
url: z.string().describe("URL to fetch"),
options: z.object({
method: z.string().optional().describe("HTTP method (GET, POST, etc.)"),
headers: z.record(z.string()).optional().describe("HTTP headers (User-Agent, Cookie, Referer, etc.)"),
data: z.any().optional().describe("Request body for POST/PUT"),
}).optional().describe("Request options"),
})
),
},
{
name: "debug_spider_rule",
description: "Debug drpy spider rules by parsing HTML or fetching URL",
inputSchema: zodToJsonSchema(
z.object({
html: z.string().optional().describe("HTML content to parse"),
url: z.string().optional().describe("URL to fetch and parse"),
rule: z.string().describe("The drpy rule to apply (e.g. '.list li', 'a&&href')"),
mode: z.enum(["pdfa", "pdfh", "pd"]).describe("Parsing mode: pdfa (list), pdfh (html), pd (url)"),
baseUrl: z.string().optional().describe("Base URL for resolving relative links"),
options: z.object({
method: z.string().optional(),
headers: z.record(z.string()).optional(),
data: z.any().optional(),
}).optional().describe("Request options for URL fetch"),
})
),
},
{
name: "get_spider_template",
description: "Get a standard template for creating a new drpy JS source",
inputSchema: {
type: "object",
properties: {}
},
},
{
name: "get_drpy_libs_info",
description: "Get information about available global helper functions and libraries in drpy environment",
inputSchema: {
type: "object",
properties: {}
},
},
{
name: "read_logs",
description: "Read the latest application logs",
inputSchema: {
type: "object",
properties: {
lines: { type: "number", description: "Number of lines to read (default: 50)" }
}
}
},
{
name: "sql_query",
description: "Execute a read-only SQL query on the project database",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "SQL query to execute" }
},
required: ["query"]
}
},
{
name: "manage_config",
description: "Read or update the project configuration (config/env.json)",
inputSchema: {
type: "object",
properties: {
action: { type: "string", enum: ["get", "set"], description: "Action to perform" },
key: { type: "string", description: "Config key (dot notation supported for nested keys, optional for 'get')" },
value: { type: "string", description: "Value to set (required for 'set', JSON string supported)" }
},
required: ["action"]
}
},
{
name: "validate_spider",
description: "Validate a drpy spider file (syntax check + structure validation)",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "File path relative to project root",
},
},
required: ["path"],
},
},
{
name: "check_syntax",
description: "Check syntax of a JavaScript file",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "File path relative to project root",
},
},
required: ["path"],
},
},
{
name: "restart_service",
description: "Restart the drpy-node service (assumes PM2 is used with name 'drpys')",
inputSchema: {
type: "object",
properties: {},
},
},
],
};
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
const handler = toolHandlers[name];
if (!handler) {
throw new Error(`Tool not found: ${name}`);
}
try {
return await handler(args);
} catch (error) {
return {
isError: true,
content: [{ type: "text", text: `Error executing ${name}: ${error.message}` }]
};
}
});
async function runServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Drpy Node MCP Server running on stdio");
}
runServer().catch((error) => {
console.error("Fatal error running server:", error);
process.exit(1);
});