Skip to main content
Glama

smart_refactor

DestructiveIdempotent

Refactor code by renaming symbols across multiple files to maintain consistency and improve readability.

Instructions

Intelligently refactor code by renaming symbols across multiple files

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filesYesList of files to search and refactor
oldNameYesThe symbol name to replace
newNameYesThe new symbol name

Implementation Reference

  • src/index.ts:335-362 (registration)
    Registers the 'smart_refactor' tool with the MCP server, including its description, input schema, and annotations.
    mcpServer.registerTool({
      name: 'smart_refactor',
      description: 'Intelligently refactor code by renaming symbols across multiple files',
      inputSchema: {
        type: 'object',
        properties: {
          files: {
            type: 'array',
            description: 'List of files to search and refactor'
          },
          oldName: {
            type: 'string',
            description: 'The symbol name to replace'
          },
          newName: {
            type: 'string',
            description: 'The new symbol name'
          }
        },
        required: ['files', 'oldName', 'newName']
      },
      annotations: {
        readOnlyHint: false,
        destructiveHint: true,
        idempotentHint: true,
        openWorldHint: false
      }
    });
  • Defines the input schema for the 'smart_refactor' tool, specifying parameters for files, oldName, and newName.
    inputSchema: {
      type: 'object',
      properties: {
        files: {
          type: 'array',
          description: 'List of files to search and refactor'
        },
        oldName: {
          type: 'string',
          description: 'The symbol name to replace'
        },
        newName: {
          type: 'string',
          description: 'The new symbol name'
        }
      },
      required: ['files', 'oldName', 'newName']
  • Handler for 'smart_refactor': Searches for oldName regex in affected files using fileSystemManager.findInFile, filters files with matches, and if any, uses editInstanceManager.coordinateMultiFileEdit to replace with newName.
    case 'smart_refactor':
      // Use file system to find occurrences, then Edit for precision
      const searchResults = await Promise.all(
        operation.affectedFiles.map(file =>
          this.fileSystemManager.findInFile(
            file,
            new RegExp(operation.params.oldName, 'g')
          )
        )
      );
      
      // If we found occurrences, use Edit to refactor
      const filesToEdit = operation.affectedFiles.filter((file, index) => searchResults[index].length > 0);
      
      if (filesToEdit.length > 0) {
        return this.editInstanceManager.coordinateMultiFileEdit({
          files: filesToEdit,
          operation: {
            type: 'replace',
            params: {
              pattern: operation.params.oldName,
              replacement: operation.params.newName
            }
          }
        });
      }
      
      return { message: 'No occurrences found to refactor' };
  • Classifies 'smart_refactor' as a hybrid operation in the analyzeComplexity method.
    const hybridOperations = [
      'smart_refactor',
      'validate_and_edit',
      'backup_and_edit',
      'atomic_multi_file_edit'
    ];

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observed

TDQS

A3.5/5.0
Behavior4/5

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

Annotations provide key hints: destructiveHint=true (indicating changes), idempotentHint=true (safe to retry), readOnlyHint=false (not read-only), and openWorldHint=false (limited scope). The description adds value by specifying 'intelligently' (implying smart matching) and 'across multiple files' (scope), which aren't covered by annotations. It doesn't contradict annotations, as 'refactor' aligns with destructive and non-read-only hints, and it provides useful context beyond the structured data.

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 ('Intelligently refactor code') and specifies the method ('by renaming symbols across multiple files'). There is no wasted verbiage, and every word contributes to understanding the tool's purpose, making it highly concise and well-structured.

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

Completeness3/5

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

Given the tool's complexity (refactoring across files with destructive changes), annotations cover safety and idempotency, but there's no output schema. The description lacks details on return values, error handling, or what 'intelligently' entails (e.g., language-specific rules). It's adequate as a high-level overview but incomplete for full operational understanding, especially without output information, warranting a mid-range score.

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%, with clear descriptions for 'files', 'oldName', and 'newName'. The description adds minimal semantics by implying 'intelligently' might affect how symbols are matched, but it doesn't elaborate on parameter usage (e.g., file formats, symbol types). Since the schema does the heavy lifting, a baseline score of 3 is appropriate, as the description doesn't significantly enhance parameter understanding beyond what's already documented.

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

Purpose4/5

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

The description clearly states the verb ('refactor') and resource ('code by renaming symbols across multiple files'), making the purpose evident. It distinguishes from siblings like 'format_code' or 'find_in_file' by focusing on intelligent refactoring with symbol renaming across files. However, it doesn't explicitly differentiate from 'complex_find_replace' or 'backup_and_edit', which might have overlapping functionality, preventing a perfect score.

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. It doesn't mention when-not to use it (e.g., for simple single-file edits) or name specific siblings like 'complex_find_replace' or 'backup_and_edit' as alternatives. Without such context, users might struggle to choose appropriately among the available tools.

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