File size: 7,832 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import * as fs from 'fs/promises';
import * as path from 'path';
import { DROPZONE_PATH, VIDENSARKIV_PATH as CONFIG_VIDENSARKIV_PATH } from '../../config.js';

// Constants - use config paths for Docker/HF compatibility
export const SAFE_DESKTOP_PATH = DROPZONE_PATH;
export const SNAPSHOTS_PATH = path.join(SAFE_DESKTOP_PATH, 'snapshots');
export const VIDENSARKIV_PATH = CONFIG_VIDENSARKIV_PATH;
export const ALLOWED_EXTENSIONS = ['.txt', '.md', '.json', '.csv', '.yaml', '.yml', '.xml', '.log'];

interface FileInfo {
    name: string;
    path: string;
    size: number;
    modified: string;
    type: string;
}

// Helpers
export async function ensureSafeZoneExists(): Promise<void> {
    try {
        await fs.access(SAFE_DESKTOP_PATH);
    } catch {
        await fs.mkdir(SAFE_DESKTOP_PATH, { recursive: true });
        console.error(`📁 Created DropZone at: ${SAFE_DESKTOP_PATH}`);
    }
}

export async function listSafeFiles(dirPath: string, recursive: boolean = false): Promise<FileInfo[]> {
    const files: FileInfo[] = [];

    try {
        const entries = await fs.readdir(dirPath, { withFileTypes: true });

        for (const entry of entries) {
            const fullPath = path.join(dirPath, entry.name);

            if (entry.isFile()) {
                const ext = path.extname(entry.name).toLowerCase();
                if (ALLOWED_EXTENSIONS.includes(ext)) {
                    const stats = await fs.stat(fullPath);
                    files.push({
                        name: entry.name,
                        path: fullPath.replace(VIDENSARKIV_PATH, '').replace(SAFE_DESKTOP_PATH, ''),
                        size: stats.size,
                        modified: stats.mtime.toISOString(),
                        type: ext
                    });
                }
            } else if (entry.isDirectory() && recursive) {
                const subFiles = await listSafeFiles(fullPath, true);
                files.push(...subFiles);
            }
        }
    } catch (error: any) {
        if (error.code !== 'ENOENT') {
            throw error;
        }
    }

    return files;
}

// Handlers
async function handleListDropzoneFiles(args: any) {
    const filter = args?.filter;

    await ensureSafeZoneExists();
    const files = await listSafeFiles(SAFE_DESKTOP_PATH);

    const filtered = filter
        ? files.filter(f => f.name.endsWith(filter))
        : files;

    return {
        content: [{
            type: 'text',
            text: JSON.stringify({
                path: SAFE_DESKTOP_PATH,
                files: filtered,
                count: filtered.length
            }, null, 2)
        }]
    };
}

async function handleReadDropzoneFile(args: any) {
    const { filename } = args;

    if (!filename) {
        throw new Error('Filename is required');
    }

    const safePath = path.join(SAFE_DESKTOP_PATH, path.basename(filename));

    if (!safePath.startsWith(SAFE_DESKTOP_PATH)) {
        throw new Error('Access denied: File outside safe zone');
    }

    const ext = path.extname(filename).toLowerCase();
    if (!ALLOWED_EXTENSIONS.includes(ext)) {
        throw new Error(`File type not allowed: ${ext}`);
    }

    try {
        const content = await fs.readFile(safePath, 'utf-8');
        return {
            content: [{
                type: 'text',
                text: content
            }]
        };
    } catch (error: any) {
        if (error.code === 'ENOENT') {
            throw new Error(`File not found: ${filename}`);
        }
        throw error;
    }
}

async function handleListVidensarkiv(args: any) {
    const subfolder = args?.subfolder || '';
    const recursive = args?.recursive ?? false;

    const targetPath = path.join(VIDENSARKIV_PATH, subfolder);

    if (!targetPath.startsWith(VIDENSARKIV_PATH)) {
        throw new Error('Access denied: Path outside vidensarkiv');
    }

    const files = await listSafeFiles(targetPath, recursive);

    return {
        content: [{
            type: 'text',
            text: JSON.stringify({
                path: targetPath,
                files: files,
                count: files.length
            }, null, 2)
        }]
    };
}

async function handleReadVidensarkivFile(args: any) {
    const { filepath } = args;

    if (!filepath) {
        throw new Error('Filepath is required');
    }

    const safePath = path.join(VIDENSARKIV_PATH, filepath);

    if (!safePath.startsWith(VIDENSARKIV_PATH)) {
        throw new Error('Access denied: Path outside vidensarkiv');
    }

    const ext = path.extname(filepath).toLowerCase();
    if (!ALLOWED_EXTENSIONS.includes(ext)) {
        throw new Error(`File type not allowed: ${ext}`);
    }

    try {
        const content = await fs.readFile(safePath, 'utf-8');
        return {
            content: [{
                type: 'text',
                text: content
            }]
        };
    } catch (error: any) {
        if (error.code === 'ENOENT') {
            throw new Error(`File not found: ${filepath}`);
        }
        throw error;
    }
}

export function registerFileSystemTools(server: Server) {
    server.setRequestHandler(ListToolsRequestSchema, async (request) => {
        // Since we are appending to an existing list in the main server, 
        // we can't easily "append" here if the main server handles the schema.
        // Instead, the main server should aggregate definitions.
        // But MCP SDK allows adding handlers? No, `setRequestHandler` overwrites.
        // Strategy: The main server handles `ListToolsRequestSchema` and returns a combined list.
        // We export the tool definitions here.
        return { tools: [] }; // Dummy return, unused by aggregator
    });
}

export const FILE_SYSTEM_TOOLS = [
    {
        name: 'list_dropzone_files',
        description: 'List files in the WidgeTDC DropZone (safe zone for file access)',
        inputSchema: {
            type: 'object',
            properties: {
                filter: {
                    type: 'string',
                    description: 'File extension filter (e.g., ".txt", ".json")'
                }
            }
        },
        handler: handleListDropzoneFiles
    },
    {
        name: 'read_dropzone_file',
        description: 'Read a file from the WidgeTDC DropZone',
        inputSchema: {
            type: 'object',
            properties: {
                filename: {
                    type: 'string',
                    description: 'Name of the file to read'
                }
            },
            required: ['filename']
        },
        handler: handleReadDropzoneFile
    },
    {
        name: 'list_vidensarkiv',
        description: 'List files in the vidensarkiv (knowledge archive)',
        inputSchema: {
            type: 'object',
            properties: {
                subfolder: {
                    type: 'string',
                    description: 'Subfolder path within vidensarkiv'
                },
                recursive: {
                    type: 'boolean',
                    description: 'List files recursively'
                }
            }
        },
        handler: handleListVidensarkiv
    },
    {
        name: 'read_vidensarkiv_file',
        description: 'Read a file from the vidensarkiv (knowledge archive)',
        inputSchema: {
            type: 'object',
            properties: {
                filepath: {
                    type: 'string',
                    description: 'Relative path within vidensarkiv'
                }
            },
            required: ['filepath']
        },
        handler: handleReadVidensarkivFile
    }
];