Skip to main content
Glama

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
NameRequiredDescriptionDefault
keyYesThe file key/name
contentYesThe content to write

Implementation Reference

  • 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) }] }; }
  • 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'], }, },
  • 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(); } }
  • 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) }] }; }

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Amana03/universal-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server