Skip to main content
Glama
th3w1zard1

Cedardiff MCP Server

by th3w1zard1

edit_file

Modify file content using CEDARScript commands. Specify working directory and script to execute precise code changes for efficient file manipulation.

Instructions

Edit a file using CEDARScript syntax

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesCEDARScript commands to execute
workingDirYesWorking directory for resolving file paths

Implementation Reference

  • Main handler for the 'edit_file' tool within the CallToolRequestSchema. Validates the tool name, parses arguments, reads the target file, executes the CEDARScript, writes changes back, and returns success/error content.
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
      if (request.params.name !== 'edit_file') {
        throw new McpError(
          ErrorCode.MethodNotFound,
          `Unknown tool: ${request.params.name}`
        );
      }
    
      if (!request.params.arguments || 
          typeof request.params.arguments.script !== 'string' ||
          typeof request.params.arguments.workingDir !== 'string') {
        throw new McpError(
          ErrorCode.InvalidParams,
          'Missing or invalid file/script arguments'
        );
      }
    
      const { script, workingDir } = request.params.arguments;
      
      try {
        // Extract file path from script
        const fileMatch = script.match(/FILE\s+['"]([^'"]+)['"]/i);
        if (!fileMatch) {
          throw new Error('Script must specify a file using FILE "path"');
        }
        const filePath = path.resolve(workingDir, fileMatch[1]);
    
        // Read the file
        const content = fs.readFileSync(filePath, 'utf8');
    
        // Parse and execute the CEDARScript
        const newContent = this.executeCedarScript(content, script);
    
        // Write back to file
        fs.writeFileSync(filePath, newContent);
    
        return {
          content: [
            {
              type: 'text',
              text: `Successfully edited ${filePath}`,
            },
          ],
        };
      } catch (error: unknown) {
        const errorMessage = error instanceof Error ? error.message : String(error);
        return {
          content: [
            {
              type: 'text',
              text: `Error editing file: ${errorMessage}`,
            },
          ],
          isError: true,
        };
      }
    });
  • Input schema definition for the 'edit_file' tool, specifying required 'script' and 'workingDir' parameters.
    inputSchema: {
      type: 'object',
      properties: {
        script: {
          type: 'string',
          description: 'CEDARScript commands to execute'
        },
        workingDir: {
          type: 'string',
          description: 'Working directory for resolving file paths'
        }
      },
      required: ['script', 'workingDir'],
    },
  • src/index.ts:65-86 (registration)
    Registers the 'edit_file' tool in the ListToolsRequestSchema handler, providing name, description, and schema.
    this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
      tools: [
        {
          name: 'edit_file',
          description: 'Edit a file using CEDARScript syntax',
          inputSchema: {
            type: 'object',
            properties: {
              script: {
                type: 'string',
                description: 'CEDARScript commands to execute'
              },
              workingDir: {
                type: 'string',
                description: 'Working directory for resolving file paths'
              }
            },
            required: ['script', 'workingDir'],
          },
        },
      ],
    }));
  • Helper function that splits file content into lines, parses the CEDARScript into commands, executes them, and rejoins the lines.
    private executeCedarScript(content: string, script: string): string {
      const lines = content.split('\n');
      let result = [...lines];
    
      // Parse the script into commands
      const commands = this.parseScript(script);
    
      // Execute each command
      for (const command of commands) {
        result = this.executeCommand(command, result);
      }
    
      return result.join('\n');
    }
  • Helper function that parses the CEDARScript string into an array of command objects by splitting on semicolons and dispatching to specific parsers.
    private parseScript(script: string): Array<{
      type: 'update' | 'create' | 'rm_file' | 'mv_file';
      caseStatement?: CaseStatement;
      filePath?: string;
      content?: string;
      targetPath?: string;
    }> {
      const commands: Array<{
        type: 'update' | 'create' | 'rm_file' | 'mv_file';
        targetPath?: string;
      }> = [];
      
      // Split into individual commands (separated by semicolons)
      const commandStrings = script.split(';').map(cmd => cmd.trim()).filter(Boolean);
    
      for (const cmdStr of commandStrings) {
        if (cmdStr.startsWith('UPDATE')) {
          commands.push(this.parseUpdateCommand(cmdStr));
        } else if (cmdStr.startsWith('CREATE')) {
          commands.push(this.parseCreateCommand(cmdStr));
        } else if (cmdStr.startsWith('RM')) {
          commands.push(this.parseRmCommand(cmdStr));
        } else if (cmdStr.startsWith('MV')) {
          commands.push(this.parseMvCommand(cmdStr));
        }
      }
    
      return commands;
    }
Install Server

Other Tools

Related 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/th3w1zard1/cedarscript-mcp'

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