Skip to content

Feature Request: Add excel_create_file tool to create new Excel files #67

@vielhuber

Description

@vielhuber

Problem

excel_write_to_sheet fails with the following error when the target file does not exist yet:

Error calling tool 'excel_write_to_sheet': zip: not a valid zip file

Root Cause

writeSheet() in internal/tools/excel_write_to_sheet.go always opens an existing file first:

// excel_write_to_sheet.go:101
workbook, closeFn, err := excel.OpenFile(fileAbsolutePath)
if err != nil {
    return nil, err  // <-- error is thrown here when file doesn't exist
}

excel.OpenFile internally calls excelize.OpenFile(absoluteFilePath), which requires a valid .xlsx ZIP archive to already exist on disk. If the file is missing, excelize returns zip: not a valid zip file.

The newSheet: true parameter of excel_write_to_sheet only creates a new sheet within an existing workbook — it does not create a new file.

There is no Excel.CreateNewFile() method in the Excel interface (internal/excel/excel.go), and no corresponding tool exposed via MCP.

Expected Behavior

It should be possible to create a new .xlsx file from scratch via an MCP tool call, without requiring the file to pre-exist on disk.

Proposed Solution

Add a new tool excel_create_file that creates an empty Excel workbook at a given path:

Tool definition:

  • fileAbsolutePath (required) — Absolute path where the new .xlsx file should be created
  • sheetName (optional, default: "Sheet1") — Name of the initial worksheet

Implementation sketch:

// internal/tools/excel_create_file.go

func createFile(fileAbsolutePath string, sheetName string) (*mcp.CallToolResult, error) {
    f := excelize.NewFile()
    defer f.Close()

    if sheetName != "Sheet1" {
        f.SetSheetName("Sheet1", sheetName)
    }

    file, err := os.OpenFile(filepath.Clean(fileAbsolutePath), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
    if err != nil {
        return nil, err
    }
    defer file.Close()

    if err := f.Write(file); err != nil {
        return nil, err
    }

    result := "# Notice\n"
    result += fmt.Sprintf("File [%s] created with sheet [%s].\n", fileAbsolutePath, sheetName)
    return mcp.NewToolResultText(result), nil
}

Alternatively, excel_write_to_sheet with newSheet: true could be extended to automatically create the file if it does not exist yet, which would be a non-breaking improvement to the existing API.

Workaround

Until this is implemented, a new file can be created via an external tool (e.g. a Shell MCP) using Python:

python3 -c "import openpyxl; wb = openpyxl.Workbook(); wb.save('/path/to/file.xlsx')"

Environment

  • @negokaz/excel-mcp-server (latest via npx)
  • Linux (no OLE/Windows backend available, using excelize backend)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions