Skip to main content
Glama

delete_symbol

Remove a TypeScript/JavaScript symbol (variable, function, class) and optionally all its references across files for cleaner, refactored code.

Instructions

Delete a TypeScript/JavaScript symbol (variable, function, class, etc.) and all its references

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYesFile path containing the symbol (relative to root)
lineYesLine number (1-based) or string to match in the line
removeReferencesNoAlso delete all references to the symbol
rootYesRoot directory for resolving relative paths
symbolNameYesName of the symbol to delete

Implementation Reference

  • The core handler function that implements the delete symbol logic using LSP. It finds the symbol position, locates references if requested, creates workspace edits to delete occurrences, and applies the edits.
    async function handleDeleteSymbol(
      {
        root,
        relativePath,
        line,
        textTarget,
        removeReferences = true,
      }: z.infer<typeof schema>,
      client: LSPClient,
    ): Promise<DeleteSymbolResult> {
      if (!client) {
        throw new Error("LSP client not initialized");
      }
    
      // Convert to absolute path
      const absolutePath = path.isAbsolute(relativePath)
        ? relativePath
        : path.join(root, relativePath);
    
      // Convert to file URI
      const fileUri = pathToFileURL(absolutePath).toString();
    
      // Read the file content
      const content = await fs.readFile(absolutePath, "utf-8");
    
      // Open the document in LSP
      client.openDocument(fileUri, content);
    
      let locations: Location[] = [];
    
      try {
        // Resolve line parameter
        const lines = content.split("\n");
        const lineIndex = resolveLineParameter(lines, line);
        const resolvedLine = lineIndex + 1; // Convert back to 1-based
    
        // Find the symbol position on the line
        const lineContent = lines[lineIndex];
        const symbolIndex = lineContent.indexOf(textTarget);
    
        if (symbolIndex === -1) {
          throw new Error(
            `Symbol "${textTarget}" not found on line ${resolvedLine}: "${lineContent.trim()}"`,
          );
        }
    
        // Create position for the symbol
        const position: Position = {
          line: lineIndex, // LSP uses 0-based line numbers
          character: symbolIndex,
        };
    
        // First, find all references if removeReferences is true
        locations = removeReferences
          ? await client.findReferences(fileUri, position)
          : [{ uri: fileUri, range: { start: position, end: position } }];
    
        if (locations.length === 0) {
          return {
            applied: false,
            deletedFromFiles: new Set(),
            totalDeleted: 0,
            failureReason: "No references found for the symbol",
          };
        }
    
        // Create workspace edit
        const workspaceEdit: WorkspaceEdit = {
          changes: {},
        };
    
        // Group locations by file
        const fileChanges = new Map<string, Range[]>();
    
        for (const location of locations) {
          const ranges = fileChanges.get(location.uri) || [];
          ranges.push(location.range);
          fileChanges.set(location.uri, ranges);
        }
    
        // Create text edits for each file
        for (const [uri, ranges] of fileChanges) {
          // Sort ranges in reverse order to avoid position shifts
          const sortedRanges = ranges.sort((a, b) => {
            if (a.start.line !== b.start.line) {
              return b.start.line - a.start.line;
            }
            return b.start.character - a.start.character;
          });
    
          // Read file content if it's different from the current file
          let fileContent: string;
          let fileLines: string[];
    
          if (uri === fileUri) {
            fileContent = content;
            fileLines = lines;
          } else {
            const absoluteFilePath = fileURLToPath(uri);
            fileContent = await fs.readFile(absoluteFilePath, "utf-8");
            fileLines = fileContent.split("\n");
            // Open document for editing
            client.openDocument(uri, fileContent);
          }
    
          const edits: TextEdit[] = [];
    
          for (const range of sortedRanges) {
            // Check if this is a complete line deletion
            const lineText = fileLines[range.start.line];
            const beforeSymbol = lineText
              .substring(0, range.start.character)
              .trim();
            const afterSymbol = lineText.substring(range.end.character).trim();
    
            if (!beforeSymbol && !afterSymbol) {
              // Delete the entire line
              edits.push({
                range: {
                  start: { line: range.start.line, character: 0 },
                  end: { line: range.start.line + 1, character: 0 },
                },
                newText: "",
              });
            } else {
              // Delete just the symbol
              edits.push({
                range,
                newText: "",
              });
            }
          }
    
          workspaceEdit.changes![uri] = edits;
        }
    
        // Apply the workspace edit
        const result = await client.applyEdit(
          workspaceEdit,
          `Delete symbol "${textTarget}"`,
        );
    
        if (!result.applied) {
          return {
            applied: false,
            deletedFromFiles: new Set(),
            totalDeleted: 0,
            failureReason: result.failureReason || "Failed to apply workspace edit",
          };
        }
    
        return {
          applied: true,
          deletedFromFiles: new Set(fileChanges.keys()),
          totalDeleted: locations.length,
        };
      } finally {
        // Close all opened documents
        for (const uri of new Set([fileUri, ...locations.map((l) => l.uri)])) {
          client.closeDocument(uri);
        }
      }
    }
  • Zod schema defining the input parameters for the delete symbol tool.
    const schemaShape = {
      root: z.string().describe("Root directory for resolving relative paths"),
      relativePath: z
        .string()
        .describe("File path containing the symbol (relative to root)"),
      line: z
        .union([z.number(), z.string()])
        .describe("Line number (1-based) or string to match in the line"),
      textTarget: z.string().describe("Name of the symbol to delete"),
      removeReferences: z
        .boolean()
        .optional()
        .default(true)
        .describe("Also delete all references to the symbol"),
    };
    
    const schema = z.object(schemaShape);
  • Factory function creating the MCP tool definition named 'lsp_delete_symbol', which wraps the handler.
    export function createDeleteSymbolTool(
      client: LSPClient,
    ): McpToolDef<typeof schema> {
      return {
        name: "lsp_delete_symbol",
        description:
          "Delete a symbol and optionally all its references using LSP. Requires exact line:column position of the symbol.",
        schema,
        execute: async (args) => {
          const result = await handleDeleteSymbol(args, client);
          return formatDeleteSymbolResult(result);
        },
      };
    }
  • The delete symbol tool is included in the list of all LSP tools returned by createLSPTools.
    createDeleteSymbolTool(client),
  • Helper function in capability checker that determines if the delete_symbol tool is supported based on codeActionProvider capability.
    delete_symbol: (checker) => {
      return checker.hasCapability("codeActionProvider");
    },
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions the destructive action ('Delete') and the scope of deletion ('all its references'), but lacks critical details such as whether the operation is reversible, what permissions are needed, how errors are handled, or what the output looks like. For a destructive tool with zero annotation coverage, this is insufficient.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core action and resource without unnecessary words. Every part of the sentence contributes essential information about the tool's purpose and scope.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of a destructive operation with no annotations and no output schema, the description is incomplete. It fails to address critical aspects like error handling, confirmation prompts, side effects on dependent code, or return values, leaving significant gaps for an AI agent to understand the tool's full behavior.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all five parameters thoroughly. The description adds no additional meaning about parameters beyond implying that 'symbolName' refers to a TypeScript/JavaScript symbol and 'removeReferences' relates to deleting references. This meets the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Delete') and target resource ('TypeScript/JavaScript symbol'), including what types of symbols are affected ('variable, function, class, etc.') and the scope ('and all its references'). It distinguishes itself from sibling tools like 'rename_symbol' by specifying deletion rather than modification.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'rename_symbol' or 'find_references', nor does it mention prerequisites, constraints, or typical scenarios for deletion. It states what the tool does but not when it should be selected.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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/mizchi/typescript-mcp'

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