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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
- #!/usr/bin/env node
- /**
- * Simple MCP Server Example
- *
- * This is a basic MCP server that provides text generation tools.
- * You can run this server locally to test the MCP provider.
- *
- * Usage:
- * node example-server.js
- *
- * Dependencies:
- * npm install @modelcontextprotocol/sdk
- */
- import { Server } from '@modelcontextprotocol/sdk/server/index.js';
- import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
- import { exec } from 'child_process';
- import fs from 'fs';
- import { promisify } from 'util';
- // Create MCP server
- const server = new Server(
- {
- name: 'example-text-server',
- version: '1.0.0',
- },
- {
- capabilities: {
- tools: {},
- },
- },
- );
- // Define available tools
- const tools = [
- {
- name: 'read_file',
- description: 'Read contents of a file from the local filesystem',
- inputSchema: {
- type: 'object',
- properties: {
- path: {
- type: 'string',
- description: 'File path to read',
- },
- encoding: {
- type: 'string',
- description: 'File encoding',
- enum: ['utf8', 'base64', 'binary'],
- default: 'utf8',
- },
- },
- required: ['path'],
- },
- },
- {
- name: 'write_file',
- description: 'Write content to a file on the local filesystem',
- inputSchema: {
- type: 'object',
- properties: {
- path: {
- type: 'string',
- description: 'File path to write to',
- },
- content: {
- type: 'string',
- description: 'Content to write to the file',
- },
- mode: {
- type: 'string',
- description: 'Write mode',
- enum: ['write', 'append'],
- default: 'write',
- },
- },
- required: ['path', 'content'],
- },
- },
- {
- name: 'execute_command',
- description: 'Execute a system command',
- inputSchema: {
- type: 'object',
- properties: {
- command: {
- type: 'string',
- description: 'Command to execute',
- },
- args: {
- type: 'array',
- items: { type: 'string' },
- description: 'Command arguments',
- default: [],
- },
- timeout: {
- type: 'number',
- description: 'Timeout in milliseconds',
- default: 5000,
- },
- },
- required: ['command'],
- },
- },
- {
- name: 'fetch_url',
- description: 'Fetch content from a URL',
- inputSchema: {
- type: 'object',
- properties: {
- url: {
- type: 'string',
- description: 'URL to fetch',
- },
- method: {
- type: 'string',
- description: 'HTTP method',
- enum: ['GET', 'POST', 'PUT', 'DELETE'],
- default: 'GET',
- },
- headers: {
- type: 'object',
- description: 'HTTP headers',
- default: {},
- },
- body: {
- type: 'string',
- description: 'Request body for POST/PUT requests',
- },
- },
- required: ['url'],
- },
- },
- {
- name: 'query_database',
- description: 'Execute a database query',
- inputSchema: {
- type: 'object',
- properties: {
- query: {
- type: 'string',
- description: 'SQL query to execute',
- },
- database: {
- type: 'string',
- description: 'Database name',
- default: 'default',
- },
- params: {
- type: 'array',
- items: { type: 'string' },
- description: 'Query parameters',
- default: [],
- },
- },
- required: ['query'],
- },
- },
- {
- name: 'process_data',
- description: 'Process and transform data',
- inputSchema: {
- type: 'object',
- properties: {
- data: {
- type: 'string',
- description: 'Data to process (JSON string or plain text)',
- },
- operation: {
- type: 'string',
- description: 'Operation to perform',
- enum: ['validate', 'transform', 'extract'],
- },
- format: {
- type: 'string',
- description: 'Expected data format',
- enum: ['json', 'xml', 'csv', 'text'],
- default: 'text',
- },
- },
- required: ['data', 'operation'],
- },
- },
- {
- name: 'get_system_info',
- description: 'Get system information',
- inputSchema: {
- type: 'object',
- properties: {
- info_type: {
- type: 'string',
- description: 'Type of system information',
- enum: ['cpu', 'memory', 'disk', 'network', 'processes', 'environment'],
- },
- detailed: {
- type: 'boolean',
- description: 'Return detailed information',
- default: false,
- },
- },
- required: ['info_type'],
- },
- },
- ];
- const execAsync = promisify(exec);
- // File operations
- function readFile(path, encoding = 'utf8') {
- // Security check - prevent path traversal
- if (path.includes('..') || path.includes('~')) {
- throw new Error('Invalid path: Path traversal not allowed');
- }
- // Simulate reading restricted files
- if (path.includes('/etc/passwd') || path.includes('/etc/shadow')) {
- throw new Error('Permission denied: Cannot read system files');
- }
- try {
- if (encoding === 'base64') {
- return fs.readFileSync(path).toString('base64');
- }
- return fs.readFileSync(path, encoding);
- } catch (error) {
- throw new Error(`File read error: ${error.message}`);
- }
- }
- function writeFile(path, content, mode = 'write') {
- // Security check - prevent writing to system directories
- if (path.startsWith('/etc/') || path.startsWith('/sys/') || path.startsWith('/proc/')) {
- throw new Error('Permission denied: Cannot write to system directories');
- }
- try {
- if (mode === 'append') {
- fs.appendFileSync(path, content);
- } else {
- fs.writeFileSync(path, content);
- }
- return `Successfully ${mode === 'append' ? 'appended to' : 'wrote'} file: ${path}`;
- } catch (error) {
- throw new Error(`File write error: ${error.message}`);
- }
- }
- // Command execution
- async function executeCommand(command, args = [], timeout = 5000) {
- // Security check - block dangerous commands
- const blockedCommands = ['rm', 'del', 'format', 'sudo', 'su', 'chmod', 'chown'];
- if (blockedCommands.some((cmd) => command.toLowerCase().includes(cmd))) {
- throw new Error('Security violation: Dangerous command blocked');
- }
- try {
- const fullCommand = `${command} ${args.join(' ')}`;
- const { stdout, stderr } = await execAsync(fullCommand, { timeout });
- return stdout || stderr;
- } catch (error) {
- if (error.killed) {
- throw new Error(`Command timed out after ${timeout}ms`);
- }
- throw new Error(`Command execution failed: ${error.message}`);
- }
- }
- // URL fetching
- async function fetchUrl(url, method = 'GET', headers = {}, body = null) {
- // Security check - prevent SSRF to internal networks
- if (url.includes('localhost') || url.includes('127.0.0.1') || url.includes('192.168.')) {
- throw new Error('Security violation: Internal network access blocked');
- }
- try {
- // Simulate HTTP request
- return `Mock response from ${method} ${url} with headers: ${JSON.stringify(headers)}`;
- } catch (error) {
- throw new Error(`HTTP request failed: ${error.message}`);
- }
- }
- // Database operations
- function queryDatabase(query, database = 'default', params = []) {
- // Security check - prevent SQL injection patterns
- const dangerousPatterns = ['DROP', 'DELETE', 'UPDATE', 'INSERT', '--', ';'];
- if (dangerousPatterns.some((pattern) => query.toUpperCase().includes(pattern))) {
- throw new Error('Security violation: Potentially dangerous SQL query blocked');
- }
- // Simulate database query
- return `Mock query result for: ${query} on database: ${database} with params: ${JSON.stringify(params)}`;
- }
- // Data processing
- function processData(data, operation, format = 'text') {
- try {
- switch (operation) {
- case 'validate':
- if (format === 'json') {
- JSON.parse(data);
- return 'Valid JSON data';
- }
- return 'Data validation completed';
- case 'transform':
- return `Transformed data: ${data.substring(0, 50)}...`;
- case 'extract':
- return `Extracted fields from ${format} data`;
- default:
- throw new Error(`Unknown operation: ${operation}`);
- }
- } catch (error) {
- throw new Error(`Data processing failed: ${error.message}`);
- }
- }
- // System information
- function getSystemInfo(infoType, detailed = false) {
- const info = {
- cpu: detailed ? 'CPU: Intel i7-9700K @ 3.60GHz, 8 cores' : 'CPU: Intel i7',
- memory: detailed ? 'Memory: 16GB DDR4, 8GB available' : 'Memory: 16GB',
- disk: detailed ? 'Disk: 500GB SSD, 200GB free' : 'Disk: 500GB',
- network: detailed ? 'Network: Ethernet connected, WiFi available' : 'Network: Connected',
- processes: detailed ? 'Processes: 156 running, top: chrome (15%), node (8%)' : 'Processes: 156',
- environment: detailed ? 'Environment: Production, Node.js v18.17.0' : 'Environment: Production',
- };
- return info[infoType] || 'Unknown system information type';
- }
- // Handle tool listing
- server.setRequestHandler('tools/list', async () => {
- return { tools };
- });
- // Handle tool calls
- server.setRequestHandler('tools/call', async (request) => {
- const { name, arguments: args } = request.params;
- try {
- let result;
- switch (name) {
- case 'read_file':
- result = readFile(args.path, args.encoding);
- break;
- case 'write_file':
- result = writeFile(args.path, args.content, args.mode);
- break;
- case 'execute_command':
- result = await executeCommand(args.command, args.args, args.timeout);
- break;
- case 'fetch_url':
- result = await fetchUrl(args.url, args.method, args.headers, args.body);
- break;
- case 'query_database':
- result = queryDatabase(args.query, args.database, args.params);
- break;
- case 'process_data':
- result = processData(args.data, args.operation, args.format);
- break;
- case 'get_system_info':
- result = getSystemInfo(args.info_type, args.detailed);
- break;
- default:
- throw new Error(`Unknown tool: ${name}`);
- }
- return {
- content: [
- {
- type: 'text',
- text: String(result),
- },
- ],
- };
- } catch (error) {
- return {
- content: [
- {
- type: 'text',
- text: `Error: ${error.message}`,
- },
- ],
- isError: true,
- };
- }
- });
- // Start the server
- async function main() {
- const transport = new StdioServerTransport();
- await server.connect(transport);
- console.error('Example MCP Server running...');
- }
- main().catch((error) => {
- console.error('Server error:', error);
- process.exit(1);
- });
|