Skip to main content
Glama
Amana03

Universal MCP Server

by Amana03

write_file

Save text content to a file by specifying a filename and content, enabling file creation and updates within the Universal MCP Server environment.

Instructions

Write content to a file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
keyYesThe file key/name
contentYesThe content to write

Implementation Reference

  • Input schema for the 'write_file' tool defining required 'key' and 'content' string parameters.
    {
      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'],
      },
    },
  • Handler for 'write_file' tool: extracts arguments, logs request, invokes 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)
        }]
      };
    }
  • FileStorage.write() method: acquires lock, handles versioning and metadata, performs fs.writeFile to persist data as JSON.
    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 input schema for 'write_file' tool in the multi-mode server entry point.
    {
      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'],
      },
    },
  • Handler for 'write_file' tool in multi-mode server: identical to src/index.ts implementation.
    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)
        }]
      };
    }
Install Server

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