Skip to main content
Glama

write_file

Write content to files using specified paths and encoding options to manage file data.

Instructions

Write content to a file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to the file to write
contentYesContent to write to the file
encodingNoEncoding to use when writing the file (default: utf8)

Implementation Reference

  • Generic handler for all MCP tools/call requests. Validates tool name (including 'write_file'), retrieves tool metadata, and provides placeholder execution (to be replaced with actual logic).
    /**
     * Handles the tools/call request
     */
    private async handleToolsCall(params: { name: string, arguments?: any }): Promise<CallToolResult> {
      const { name, arguments: args } = params;
      
      if (!name) {
        throw new Error('Tool name is required');
      }
      
      const tool = this.tools.get(name);
      
      if (!tool) {
        throw new Error(`Tool not found: ${name}`);
      }
      
      // In a real implementation, we would execute the tool here
      // For now, we'll just return a placeholder
      
      return {
        content: [
          {
            type: 'text',
            text: `Executed tool ${name} with arguments: ${JSON.stringify(args)}`
          } as TextContent
        ]
      };
    }
  • src/index.ts:161-188 (registration)
    Registers the 'write_file' MCP tool on the server with full schema definition, description, and annotations.
    mcpServer.registerTool({
      name: 'write_file',
      description: 'Write content to a file',
      inputSchema: {
        type: 'object',
        properties: {
          path: {
            type: 'string',
            description: 'Path to the file to write'
          },
          content: {
            type: 'string',
            description: 'Content to write to the file'
          },
          encoding: {
            type: 'string',
            description: 'Encoding to use when writing the file (default: utf8)'
          }
        },
        required: ['path', 'content']
      },
      annotations: {
        readOnlyHint: false,
        destructiveHint: true,
        idempotentHint: true,
        openWorldHint: false
      }
    });
  • Input schema definition for the 'write_file' tool specifying path, content, and optional encoding.
      inputSchema: {
        type: 'object',
        properties: {
          path: {
            type: 'string',
            description: 'Path to the file to write'
          },
          content: {
            type: 'string',
            description: 'Content to write to the file'
          },
          encoding: {
            type: 'string',
            description: 'Encoding to use when writing the file (default: utf8)'
          }
        },
        required: ['path', 'content']
      },
      annotations: {
        readOnlyHint: false,
        destructiveHint: true,
        idempotentHint: true,
        openWorldHint: false
      }
    });
  • FileSystemManager.writeFile method: core implementation for writing file content, ensuring directory exists, using promisified fs.writeFile, and emitting change events.
    public async writeFile(filePath: string, content: string, encoding: BufferEncoding = 'utf8'): Promise<void> {
      try {
        // Ensure the directory exists
        await this.ensureDirectoryExists(path.dirname(filePath));
        await writeFile(filePath, content, { encoding });
        this.emitChangeEvent('update', filePath);
      } catch (error: any) {
        throw new Error(`Failed to write file ${filePath}: ${error.message}`);
      }
    }

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/mixelpixx/edit-mcp'

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