write_file
Save text content to files using this file writing tool. Create or update documents by specifying file names and content for storage and organization.
Instructions
Write content to a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | The file key/name | |
| content | Yes | The content to write |
Implementation Reference
- src/index.ts:176-198 (handler)Handler for the 'write_file' tool within the CallToolRequestSchema switch statement. Extracts parameters, logs, delegates to storage.write, and returns JSON success response.case 'write_file': { const { key, content } = args as { key: string; content: string }; logger.info('Tool request received', { operation: 'tool:write', toolName: 'write_file', key, requestId }); await storage.write(key, content, requestId); return { content: [{ type: 'text', text: JSON.stringify({ success: true, message: `File '${key}' written successfully`, key }, null, 2) }] }; }
- src/index.ts:122-135 (schema)Input schema for the 'write_file' tool, defining required 'key' and 'content' string parameters.inputSchema: { type: 'object', properties: { key: { type: 'string', description: 'The file key/name', }, content: { type: 'string', description: 'The content to write', }, }, required: ['key', 'content'], },
- src/index.ts:119-136 (registration)Registration of the 'write_file' tool in the ListToolsRequestSchema response, including name, description, and schema.{ name: 'write_file', description: 'Write content to a file', inputSchema: { type: 'object', properties: { key: { type: 'string', description: 'The file key/name', }, content: { type: 'string', description: 'The content to write', }, }, required: ['key', 'content'], }, },
- src/storage.ts:86-128 (helper)FileStorage.write method: implements file writing with locking mechanism, versioning, metadata management, and actual fs.writeFile call.async write(key: string, content: string, requestId: string): Promise<void> { const releaseLock = await this.acquireLock(key); const filePath = this.getFilePath(key); try { logger.debug('Writing file', { operation: 'write', key, filePath, requestId }); let currentVersion = 0; let createdAt = new Date().toISOString(); try { const existing = await fs.readFile(filePath, 'utf-8'); const existingItem: StorageItem = JSON.parse(existing); currentVersion = existingItem.metadata.version; createdAt = existingItem.metadata.createdAt; } catch { // ファイルが存在しない場合は新規作成 } const item: StorageItem = { content, metadata: { createdAt, updatedAt: new Date().toISOString(), version: currentVersion + 1 } }; await fs.writeFile(filePath, JSON.stringify(item, null, 2), 'utf-8'); logger.info('File written successfully', { operation: 'write', key, version: item.metadata.version, requestId }); } catch (error) { logger.error('Failed to write file', error as Error, { operation: 'write', key, requestId }); throw error; } finally { releaseLock(); } }
- src/index-multimode.ts:185-207 (handler)Identical handler for 'write_file' tool in the multi-mode server entrypoint.case 'write_file': { const { key, content } = args as { key: string; content: string }; logger.info('Tool request received', { operation: 'tool:write', toolName: 'write_file', key, requestId }); await storage.write(key, content, requestId); return { content: [{ type: 'text', text: JSON.stringify({ success: true, message: `File '${key}' written successfully`, key }, null, 2) }] }; }