Skip to main content
Glama
zhiwei5576

Excel MCP Server

by zhiwei5576

writeSheetData

Create Excel files by writing structured data to worksheets, generating spreadsheets from JSON data with custom sheet and column names.

Instructions

Create a new Excel file with provided data

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileAbsolutePathYesThe absolute path for the new Excel file
dataYesData object with dynamic sheet names and column names

Implementation Reference

  • The core handler function that creates a new Excel workbook, iterates over the data object to create worksheets for each sheet, and writes the file using XLSX library.
    export async function writeSheetData(
        filePathWithName: string,
        data: SheetData
    ): Promise<boolean> {
        try {
            // 创建新的工作簿
            const workbook = XLSX.utils.book_new();
    
            // 遍历数据对象的每个工作表
            for (const [sheetName, sheetData] of Object.entries(data)) {
                // 将数据转换为工作表
                const worksheet = XLSX.utils.json_to_sheet(sheetData);
    
                // 将工作表添加到工作簿
                XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
            }
    
            // 写入文件
            XLSX.writeFile(workbook, filePathWithName);
    
            return true;
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            throw new Error(`Failed to write Excel data: ${errorMessage}`);
        }
    }
  • Registers the writeSheetData tool with Zod input schema, input validation, path normalization, file existence check, and calls the core writeSheetData handler.
        server.tool("writeSheetData", 'Create a new Excel file with provided data',
            {
                fileAbsolutePath: z.string().describe("The absolute path for the new Excel file"),
                data: z.record(
                    z.string(), // 表名(动态)
                    z.array(    // 表数据数组
                        z.record( // 每行数据对象
                            z.string(), // 字段名(动态)
                            z.any()     // 字段值(任意类型)
                        )
                    )
                ).describe("Data object with dynamic sheet names and column names")
            },
            async (params: {
                fileAbsolutePath: string;
                data: Record<string, Record<string, any>[]>;
            }) => {
                try {
                    const normalizedPath = await normalizePath(params.fileAbsolutePath);
                    if (normalizedPath === 'error') {
                        return {
                            content: [{
                                type: "text",
                                text: JSON.stringify({
                                    error: `Invalid file path: ${params.fileAbsolutePath}`,
                                    suggestion: "Please verify the file path and name"
                                })
                            }]
                        };
                    }
    
                    // 校验数据结构
                    if (Object.keys(params.data).length === 0) {
                        return {
                            content: [{
                                type: "text",
                                text: JSON.stringify({
                                    error: "Empty data object provided",
                                    suggestion: "Please provide at least one sheet with data"
                                })
                            }]
                        };
                    }
    
                    // 校验每个表的数据
                    for (const [sheetName, sheetData] of Object.entries(params.data)) {
                        if (!Array.isArray(sheetData) || sheetData.length === 0) {
                            return {
                                content: [{
                                    type: "text",
                                    text: JSON.stringify({
                                        error: `Invalid data format in sheet "${sheetName}": Data must be a non-empty array`,
                                        suggestion: "Please check the data format of each sheet"
                                    })
                                }]
                            };
                        }
                    }
    
                    if (await fileExists(normalizedPath)) {
                        return {
                            content: [{
                                type: "text",
                                text: JSON.stringify({
                                    error: `File already exists: ${params.fileAbsolutePath}`,
                                    suggestion: "Please specify a different file path"
                                })
                            }]
                        };
                    }
    
                    await writeSheetData(normalizedPath, params.data);
                    return {
                        content: [{
                            type: "text",
                            text: JSON.stringify({
                                success: true,
                                message: `Excel file created successfully: ${normalizedPath}`
                            })
                        }]
                    };
    
                } catch (error) {
                    return {
                        content: [{
                            type: "text",
                            text: JSON.stringify({
                                error: `Failed to write Excel data: ${error}`,
                                suggestion: "Please verify the data format and file path"
                            })
                        }]
                    };
                }
            }
        );
    }
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. It states 'Create a new Excel file' which implies a write/mutation operation, but doesn't mention permissions needed, whether it overwrites existing files, error handling, or any side effects. This is inadequate for a tool that creates files.

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, clear sentence with zero wasted words. It's appropriately sized for a tool with only 2 parameters and gets straight to the point without unnecessary elaboration.

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?

For a file creation tool with no annotations and no output schema, the description is insufficient. It doesn't explain what happens if the file already exists, what format the Excel file will be in, whether there are size limits, or what the tool returns. The agent is left with significant gaps in understanding this tool's 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 both parameters thoroughly. The description adds no additional parameter semantics beyond what's in the schema - it doesn't explain the data structure format, provide examples, or clarify the relationship between parameters.

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 action ('Create') and resource ('a new Excel file with provided data'), making the purpose immediately understandable. However, it doesn't differentiate from sibling tools like 'writeDataBySheetName' or 'exportExcelStructure', which appear to have overlapping functionality with Excel files.

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 'writeDataBySheetName' or 'exportExcelStructure'. It doesn't mention prerequisites, constraints, or typical use cases, leaving the agent to guess based on tool names alone.

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/zhiwei5576/excel-mcp-server'

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