Skip to main content
Glama
aigo666

MCP Development Framework

parse_excel

Parse an Excel file to extract data from all sheets and return the content.

Instructions

Parses an Excel file and returns its content including all sheets

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYesPath to the Excel file to parse

Implementation Reference

  • The core handler function `execute` for the parse_excel tool. It reads an Excel file using pandas, iterates over all sheets, converts each to dict format, and returns the structured JSON result with metadata (file_name, sheet_count, columns, row/column counts, and data).
    async def execute(self, arguments: dict) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]:
        """解析Excel文件并返回内容"""
        if "file_path" not in arguments:
            return [types.TextContent(
                type="text",
                text="Error: Missing required argument 'file_path'"
            )]
        
        file_path = arguments["file_path"]
        # 处理文件路径,支持挂载目录的转换
        file_path = self.process_file_path(file_path)
        
        if not os.path.exists(file_path):
            return [types.TextContent(
                type="text",
                text=f"Error: File not found at path: {file_path}"
            )]
        
        if not file_path.lower().endswith(('.xlsx', '.xls', '.xlsm')):
            return [types.TextContent(
                type="text",
                text=f"Error: File is not an Excel file: {file_path}"
            )]
        
        try:
            # 读取Excel文件中的所有sheet
            excel_file = pd.ExcelFile(file_path)
            sheet_names = excel_file.sheet_names
            
            result = {
                "file_name": os.path.basename(file_path),
                "sheet_count": len(sheet_names),
                "sheets": {}
            }
            
            # 解析每个sheet
            for sheet_name in sheet_names:
                df = pd.read_excel(excel_file, sheet_name=sheet_name)
                
                # 将DataFrame转换为字典
                sheet_data = df.to_dict(orient='records')
                
                # 获取列名
                columns = df.columns.tolist()
                
                # 获取行数和列数
                row_count = len(df)
                column_count = len(columns)
                
                result["sheets"][sheet_name] = {
                    "row_count": row_count,
                    "column_count": column_count,
                    "columns": columns,
                    "data": sheet_data
                }
            
            # 将结果转换为JSON字符串,并格式化输出
            result_json = json.dumps(result, ensure_ascii=False, indent=2, default=str)
            
            return [types.TextContent(
                type="text",
                text=result_json
            )]
            
        except Exception as e:
            return [types.TextContent(
                type="text",
                text=f"Error: Failed to parse Excel file: {str(e)}"
            )] 
  • Input schema for parse_excel. Defines a required 'file_path' string property (the path to the Excel file).
    input_schema = {
        "type": "object",
        "required": ["file_path"],
        "properties": {
            "file_path": {
                "type": "string",
                "description": "Path to the Excel file to parse",
            }
        },
    }
  • Registration decorator @ToolRegistry.register on the ExcelTool class with name='parse_excel'. This registers the tool into the ToolRegistry so it can be discovered and invoked.
    @ToolRegistry.register
    class ExcelTool(BaseTool):
  • The process_file_path helper method inherited from BaseTool. It handles host mount directory path conversion (HOST_MOUNT_SOURCE -> HOST_MOUNT_TARGET) so the tool can resolve user-provided file paths correctly.
    def process_file_path(self, file_path: str) -> str:
        """
        处理文件路径,支持挂载目录的转换
        
        如果路径以HOST_MOUNT_SOURCE环境变量开头,则将其转换为容器内的路径
        """
        host_mount_source = os.environ.get('HOST_MOUNT_SOURCE', '')
        host_mount_target = os.environ.get('HOST_MOUNT_TARGET', '/host_files')
        
        # 如果路径以挂载源目录开头,则替换为挂载目标目录
        if host_mount_source and file_path.startswith(host_mount_source):
            return file_path.replace(host_mount_source, host_mount_target, 1)
        
        return file_path
  • The load_tools function that discovers and imports all tool modules (including excel_tool.py) from the tools directory, triggering their registration with ToolRegistry.
    def load_tools() -> List[Type[BaseTool]]:
        """
        自动加载tools目录下的所有工具模块
        
        Returns:
            List[Type[BaseTool]]: 已加载的工具类列表
        """
        # 获取当前模块的路径
        package_path = os.path.dirname(__file__)
        
        # 获取所有子模块
        for _, name, is_pkg in pkgutil.iter_modules([package_path]):
            # 跳过__init__.py和loader.py
            if name in ['__init__', 'loader']:
                continue
            
            # 导入模块
            module_name = f"{__package__}.{name}"
            try:
                importlib.import_module(module_name)
            except ImportError as e:
                print(f"Warning: Failed to import module {module_name}: {e}")
        
        # 收集所有已注册的工具类
        tools = list(ToolRegistry._tools.values())
        
        return tools
Behavior2/5

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

With no annotations, the description should disclose behavioral traits like read-only nature, file size limits, or format requirements, but it only states the basic output.

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?

A single sentence efficiently conveys the core function with no redundancy.

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 no output schema, the description should elaborate on the returned content format (e.g., as tables or text) and supported Excel versions, but it remains vague.

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 coverage is 100% with file_path already described; the description adds no extra meaning beyond what the schema provides.

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 uses a clear verb 'Parses' and resource 'Excel file', and specifies 'returns its content including all sheets', which distinguishes it from sibling tools like parse_csv and parse_pdf.

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?

No guidance is provided on when to use this tool versus alternatives, nor any context about prerequisites or limitations.

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/aigo666/mcp-framework'

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