Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (42 loc) · 1.53 KB
/
Copy pathserver.js
File metadata and controls
53 lines (42 loc) · 1.53 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
import express from 'express';
import cors from 'cors';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(cors());
app.use(express.json({ limit: '10mb' }));
const vaultPath = path.join(__dirname, 'vault');
if (!fs.existsSync(vaultPath)) fs.mkdirSync(vaultPath, { recursive: true });
app.get('/', (req, res) => {
res.send('🧠 MCP Server is running!');
});
app.post('/', async (req, res) => {
const { action, path: filePath, content } = req.body;
if (!action || !filePath || !content) {
return res.status(400).json({ error: 'Missing action, path, or content' });
}
const fullPath = path.join(vaultPath, filePath);
try {
if (action === 'write') {
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
fs.writeFileSync(fullPath, content);
return res.json({ status: 'ok', savedTo: fullPath });
}
if (action === 'write-binary') {
const buffer = Buffer.from(content, 'base64');
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
fs.writeFileSync(fullPath, buffer);
return res.json({ status: 'ok', savedTo: fullPath });
}
return res.status(400).json({ error: 'Unknown action' });
} catch (e) {
return res.status(500).json({ error: e.toString() });
}
});
const port = process.env.PORT || 3000;
app.listen(port, '0.0.0.0', () => {
console.log(`✅ MCP server running at http://localhost:${port}`);
});