Skip to main content
Glama
guangxiangdebizi

XLSX Reader MCP

read_xlsx

Extract and convert data from Excel files by reading specific sheets, cell ranges, or searching for values in columns, with output in JSON, CSV, or table format.

Instructions

Read and extract data from Excel (xlsx) files. Supports reading specific sheets, cell ranges, and converting data to various formats.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYesPath to the Excel file to read
sheetNameNoName of the specific sheet to read (optional, defaults to first sheet)
rangeNoCell range to read (e.g., 'A1:C10', optional, defaults to entire sheet)
formatNoOutput format for the data (default: table)
includeHeadersNoWhether to include headers in the output (default: true)
maxRowsNoMaximum number of rows to return (default: 100, max: 1000)
searchColumnNoColumn name or index to search in (e.g., 'A', '1', or 'Name')
searchValueNoValue to search for in the specified column
searchTypeNoType of search to perform (default: exact)

Implementation Reference

  • The async run() function that implements the core tool logic: input validation, file existence check, workbook reading with XLSX library, sheet/range selection, data filtering (empty rows, search), row limiting, output formatting (json/csv/table), and error handling with detailed summary.
    async run(args: { 
      filePath: string; 
      sheetName?: string; 
      range?: string; 
      format?: "json" | "csv" | "table";
      includeHeaders?: boolean;
      maxRows?: number;
      searchColumn?: string;
      searchValue?: string;
      searchType?: "exact" | "contains" | "startsWith" | "endsWith";
    }) {
      try {
        // Parameter validation
        if (!args.filePath) {
          throw new Error("File path is required");
        }
    
        // Check if file exists
        if (!fs.existsSync(args.filePath)) {
          throw new Error(`File not found: ${args.filePath}`);
        }
    
        // Check file extension
        const ext = path.extname(args.filePath).toLowerCase();
        if (!['.xlsx', '.xls', '.xlsm'].includes(ext)) {
          throw new Error("File must be an Excel file (.xlsx, .xls, or .xlsm)");
        }
    
        // Read the workbook
        const workbook = XLSX.readFile(args.filePath);
        
        // Get sheet names
        const sheetNames = workbook.SheetNames;
        if (sheetNames.length === 0) {
          throw new Error("No sheets found in the workbook");
        }
    
        // Determine which sheet to read
        let targetSheet: string;
        if (args.sheetName) {
          if (!sheetNames.includes(args.sheetName)) {
            throw new Error(`Sheet '${args.sheetName}' not found. Available sheets: ${sheetNames.join(', ')}`);
          }
          targetSheet = args.sheetName;
        } else {
          targetSheet = sheetNames[0];
        }
    
        // Get the worksheet
        const worksheet = workbook.Sheets[targetSheet];
        
        // Extract data based on format
        const format = args.format || "table";
        const includeHeaders = args.includeHeaders !== false;
        const maxRows = Math.min(args.maxRows || 100, 1000); // Default 100, max 1000
        const skipEmptyRows = true; // Always skip empty rows
        
        let data: any;
        let output: string;
        let originalTotalRows = 0;
        let nonEmptyDataRows = 0;
        let skippedEmptyRows = 0;
        let isDataTruncated = false;
        let filteredData: any[] = [];
        
        // Helper function to check if a row is empty
        const isEmptyRow = (row: any[]): boolean => {
          return row.every(cell => cell === '' || cell == null || cell === undefined);
        };
        
        // Helper function to perform search
        const performSearch = (data: any[][], searchColumn: string, searchValue: string, searchType: string) => {
          if (data.length === 0) return { filteredData: data, matchedRows: 0 };
          
          const headers = data[0];
          let columnIndex = -1;
          
          // Find column index
          if (/^\d+$/.test(searchColumn)) {
            // Numeric index (1-based)
            columnIndex = parseInt(searchColumn) - 1;
          } else if (/^[A-Z]+$/i.test(searchColumn)) {
            // Excel column letter (A, B, C, etc.)
            columnIndex = searchColumn.toUpperCase().charCodeAt(0) - 65;
          } else {
            // Column name
            columnIndex = headers.findIndex((header: any) => 
              String(header).toLowerCase() === searchColumn.toLowerCase()
            );
          }
          
          if (columnIndex === -1 || columnIndex >= headers.length) {
            throw new Error(`Column '${searchColumn}' not found. Available columns: ${headers.join(', ')}`);
          }
          
          // Filter rows based on search criteria
          const headerRow = [headers];
          const dataRows = data.slice(1);
          const matchedRows = dataRows.filter((row: any[]) => {
            const cellValue = String(row[columnIndex] || '').toLowerCase();
            const searchVal = searchValue.toLowerCase();
            
            switch (searchType) {
              case "contains":
                return cellValue.includes(searchVal);
              case "startsWith":
                return cellValue.startsWith(searchVal);
              case "endsWith":
                return cellValue.endsWith(searchVal);
              case "exact":
              default:
                return cellValue === searchVal;
            }
          });
          
          return {
            filteredData: [...headerRow, ...matchedRows],
            matchedRows: matchedRows.length
          };
        };
    
        if (args.range) {
          // Read specific range
          const range = XLSX.utils.decode_range(args.range);
          const rangeData: any[][] = [];
          
          for (let row = range.s.r; row <= range.e.r; row++) {
            const rowData: any[] = [];
            for (let col = range.s.c; col <= range.e.c; col++) {
              const cellAddress = XLSX.utils.encode_cell({ r: row, c: col });
              const cell = worksheet[cellAddress];
              rowData.push(cell ? cell.v : '');
            }
            rangeData.push(rowData);
          }
          data = rangeData;
        } else {
          // Read entire sheet
          let rawData = XLSX.utils.sheet_to_json(worksheet, { 
            header: 1,
            defval: ''
          }) as any[][];
          
          // Get sheet statistics before filtering
           originalTotalRows = rawData.length;
           
           // Always filter empty rows (except header)
           const headerRow = rawData.length > 0 ? [rawData[0]] : [];
           const dataRows = rawData.slice(1).filter(row => !isEmptyRow(row));
           filteredData = [...headerRow, ...dataRows];
           nonEmptyDataRows = dataRows.length;
           skippedEmptyRows = originalTotalRows - filteredData.length;
           
           // Apply search filter if specified
           if (args.searchColumn && args.searchValue) {
             const searchResults = performSearch(filteredData, args.searchColumn, args.searchValue, args.searchType || "exact");
             filteredData = searchResults.filteredData;
             nonEmptyDataRows = searchResults.matchedRows;
           }
           
           // Limit the number of rows for display
           const totalRowsToShow = includeHeaders ? maxRows + 1 : maxRows;
           data = filteredData.slice(0, totalRowsToShow);
           isDataTruncated = filteredData.length > totalRowsToShow;
        }
    
        // Format output
        switch (format) {
          case "json":
            if (includeHeaders && data.length > 0) {
              const headers = data[0];
              const rows = data.slice(1);
              const jsonData = rows.map((row: any[]) => {
                const obj: any = {};
                headers.forEach((header: string, index: number) => {
                  obj[header || `Column${index + 1}`] = row[index] || '';
                });
                return obj;
              });
              output = JSON.stringify(jsonData, null, 2);
            } else {
              output = JSON.stringify(data, null, 2);
            }
            break;
            
          case "csv":
            output = data.map((row: any[]) => 
              row.map((cell: any) => 
                typeof cell === 'string' && cell.includes(',') ? `"${cell}"` : cell
              ).join(',')
            ).join('\n');
            break;
            
          default: // table
            if (data.length === 0) {
              output = "No data found in the specified range.";
            } else {
              const maxCols = Math.max(...data.map((row: any[]) => row.length));
              const colWidths = Array(maxCols).fill(0);
              
              // Calculate column widths
              data.forEach((row: any[]) => {
                row.forEach((cell: any, index: number) => {
                  const cellStr = String(cell || '');
                  colWidths[index] = Math.max(colWidths[index], cellStr.length);
                });
              });
              
              // Format table
              const formatRow = (row: any[]) => {
                return '| ' + row.map((cell: any, index: number) => {
                  const cellStr = String(cell || '');
                  return cellStr.padEnd(colWidths[index]);
                }).join(' | ') + ' |';
              };
              
              const separator = '|' + colWidths.map(width => '-'.repeat(width + 2)).join('|') + '|';
              
              output = data.map((row: any[], index: number) => {
                const formattedRow = formatRow(row);
                if (index === 0 && includeHeaders) {
                  return formattedRow + '\n' + separator;
                }
                return formattedRow;
              }).join('\n');
            }
            break;
        }
    
        // Prepare summary with sheet statistics
        const summaryParts = [
          `πŸ“Š **Excel File Analysis**`,
          ``,
          `**File:** ${path.basename(args.filePath)}`,
          `**Sheet:** ${targetSheet}`,
          `**Available Sheets:** ${sheetNames.join(', ')}`,
          `**Format:** ${format}`,
          args.range ? `**Range:** ${args.range}` : `**Range:** Entire sheet`,
          ``,
          `## πŸ“Š Sheet Information`
        ];
        
        if (!args.range) {
            summaryParts.push(
              `- **Total Rows:** ${originalTotalRows} (including header)`,
              `- **Data Rows:** ${nonEmptyDataRows} (excluding header)`
            );
            if (skippedEmptyRows > 0) {
              summaryParts.push(`- **Empty Rows Filtered:** ${skippedEmptyRows}`);
            }
            if (args.searchColumn && args.searchValue) {
              summaryParts.push(
                `- **πŸ” Search Applied:** Column '${args.searchColumn}' ${args.searchType || 'exact'} '${args.searchValue}'`,
                `- **Search Results:** ${nonEmptyDataRows} matching rows found`
              );
            }
            if (isDataTruncated) {
              summaryParts.push(`- **⚠️ Display Limit:** Showing first ${maxRows} rows (${filteredData.length - (includeHeaders ? maxRows + 1 : maxRows)} more rows available)`);
            }
        } else {
          summaryParts.push(
            `- **Displayed Rows:** ${data.length}`,
            `- **Displayed Columns:** ${data.length > 0 ? Math.max(...data.map((row: any[]) => row.length)) : 0}`
          );
        }
        
        summaryParts.push(
          ``,
          `## Data Content`,
          ``,
          output
        );
        
        const summary = summaryParts.join('\n');
        
        return {
          content: [{
            type: "text",
            text: summary
          }]
        };
        
      } catch (error: any) {
        return {
          content: [{
            type: "text",
            text: `❌ **Error reading Excel file:** ${error.message}`
          }],
          isError: true
        };
      }
    }
  • JSON Schema defining the input parameters for the read_xlsx tool, including required filePath and optional parameters for sheet, range, output format, headers, row limit, and search criteria.
    parameters: {
      type: "object",
      properties: {
        filePath: {
          type: "string",
          description: "Path to the Excel file to read"
        },
        sheetName: {
          type: "string",
          description: "Name of the specific sheet to read (optional, defaults to first sheet)"
        },
        range: {
          type: "string",
          description: "Cell range to read (e.g., 'A1:C10', optional, defaults to entire sheet)"
        },
        format: {
          type: "string",
          enum: ["json", "csv", "table"],
          description: "Output format for the data (default: table)"
        },
        includeHeaders: {
          type: "boolean",
          description: "Whether to include headers in the output (default: true)"
        },
        maxRows: {
          type: "number",
          description: "Maximum number of rows to return (default: 100, max: 1000)"
        },
        searchColumn: {
          type: "string",
          description: "Column name or index to search in (e.g., 'A', '1', or 'Name')"
        },
        searchValue: {
          type: "string",
          description: "Value to search for in the specified column"
        },
        searchType: {
          type: "string",
          enum: ["exact", "contains", "startsWith", "endsWith"],
          description: "Type of search to perform (default: exact)"
        }
      },
      required: ["filePath"]
    },
  • src/index.ts:21-25 (registration)
    Registration of read_xlsx tool in the ListToolsRequestSchema handler, providing name, description, and inputSchema.
    {
      name: xlsxReader.name,
      description: xlsxReader.description,
      inputSchema: xlsxReader.parameters
    },
  • src/index.ts:38-39 (registration)
    Dispatch of read_xlsx tool calls to the xlsxReader.run() method in the CallToolRequestSchema handler.
    case "read_xlsx":
      return await xlsxReader.run(request.params.arguments as any);
  • src/index.ts:6-6 (registration)
    Import of the xlsxReader tool implementation.
    import { xlsxReader } from "./tools/xlsx-reader.js";
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 states the tool reads and extracts data, implying it's a read-only operation, but doesn't mention potential side effects, error conditions, or performance limits (e.g., file size constraints). It hints at format conversion but lacks details on output structure or any rate limits. This is inadequate for a tool with 9 parameters and no annotation coverage.

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

Conciseness4/5

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

The description is concise and front-loaded, stating the core purpose in the first sentence. The second sentence adds useful details without redundancy. However, it could be more structured by explicitly listing key parameters or use cases, but it avoids unnecessary verbosity, earning a high score for efficiency.

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 (9 parameters, no output schema, no annotations), the description is moderately complete. It covers the basic purpose and some capabilities but lacks details on behavioral traits, error handling, and output format specifics. Without annotations or output schema, the agent might struggle with full usage context, making this description adequate but with clear gaps for a data extraction tool.

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?

The description adds minimal value beyond the input schema, which has 100% coverage. It mentions 'supports reading specific sheets, cell ranges, and converting data to various formats,' which loosely maps to parameters like sheetName, range, and format, but doesn't provide additional context or usage examples. With high schema coverage, the baseline is 3, as the description doesn't significantly enhance parameter understanding.

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 tool's purpose: 'Read and extract data from Excel (xlsx) files.' It specifies the verb ('read and extract') and resource ('Excel files'), and mentions key capabilities like reading specific sheets and cell ranges. However, it doesn't explicitly differentiate from its sibling 'analyze_xlsx'β€”the description implies this is for data extraction while analysis might involve different operations, but the distinction isn't stated.

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 mentions capabilities but doesn't specify scenarios, prerequisites, or exclusions. For example, it doesn't clarify if this is for simple data reading versus more complex analysis that 'analyze_xlsx' might handle, leaving the agent without context for tool selection.

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

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/guangxiangdebizi/xlsx-mcp'

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