Skip to main content
Glama

read_and_summarize_text_file

Read and summarize text files up to 2,000 characters by specifying a target compression ratio between 0.1-1.0, streamlining content extraction and quick understanding.

Instructions

读取txt等格式的文本文件并总结内容(限制2k字符)

Args:
    filepath: 文本文件路径
    target_ratio: 目标压缩比例,0.1-1.0之间

Returns:
    文件内容总结

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filepathYes
target_ratioNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The main execution handler for the read_and_summarize_text_file tool, registered with @mcp.tool(). Validates input, reads the file using file_processor.read_text_file(), summarizes using summarizer.summarize_content(), and returns formatted result.
    @mcp.tool()
    async def read_and_summarize_text_file(filepath: str, ctx: Context, target_ratio: float = 0.2) -> str:
        """
        读取txt等格式的文本文件并总结内容(限制2k字符)
        
        Args:
            filepath: 文本文件路径
            target_ratio: 目标压缩比例,0.1-1.0之间
        
        Returns:
            文件内容总结
        """
        try:
            # 验证参数
            if not 0.1 <= target_ratio <= 1.0:
                return "错误: target_ratio 必须在 0.1 到 1.0 之间"
            
            ctx.info(f"开始读取文本文件: {filepath}")
            
            # 读取文件
            content = file_processor.read_text_file(filepath)
            
            # 总结内容
            summary = await summarizer.summarize_content(content, target_ratio)
            
            ctx.info("文本文件读取和总结完成")
            return f"文件: {filepath}\n\n总结:\n{summary}"
            
        except Exception as e:
            logger.error(f"文本文件总结失败: {e}")
            return f"文本文件总结失败: {str(e)}"
  • FileProcessor class with static method read_text_file used by the tool to read the content of the specified text file.
    class FileProcessor:
        """文件处理器"""
        
        @staticmethod
        def read_text_file(filepath: str) -> str:
            """读取文本文件"""
            try:
                with open(filepath, 'r', encoding='utf-8') as f:
                    content = f.read()
                
                return content
            except Exception as e:
                logger.error(f"读取文本文件失败 {filepath}: {e}")
                raise Exception(f"无法读取文件: {str(e)}")
        
        @staticmethod
        def read_pdf_file(filepath: str) -> str:
            """读取PDF文件"""
            try:
                content = ""
                with open(filepath, 'rb') as f:
                    pdf_reader = PyPDF2.PdfReader(f)
                    
                    for page in pdf_reader.pages:
                        content += page.extract_text() + "\n"
                
                return content.strip()
            except Exception as e:
                logger.error(f"读取PDF文件失败 {filepath}: {e}")
                raise Exception(f"无法读取PDF文件: {str(e)}")
  • ContentSummarizer class providing the summarize_content method, which uses an OpenAI-compatible client to generate summaries based on target_ratio. This is the core summarization logic called by the tool.
    class ContentSummarizer:
        """内容总结器,使用MiniMax API"""
        
        def __init__(self):
            if not OPENAI_API_KEY:
                raise ValueError("需要设置OPENAI_API_KEY环境变量")
            
            self.client = OpenAI(
                api_key=OPENAI_API_KEY,
                base_url=OPENAI_BASE_URL
            )
        
        async def summarize_content(self, content: str, target_ratio: float = 0.2, 
                                  custom_prompt: str = None) -> str:
            """
            使用大模型总结内容
            
            Args:
                content: 要总结的内容
                target_ratio: 目标压缩比例 (默认20%)
                custom_prompt: 自定义总结提示词
            
            Returns:
                总结后的内容
            """
            try:
                # 检查内容长度,避免超出限制
                if len(content) > MAX_INPUT_TOKENS * 3:  # 粗略估算token
                    content = content[:MAX_INPUT_TOKENS * 3]
                    logger.warning("内容过长,已截断")
                
                # 构建总结提示词
                if custom_prompt:
                    prompt = custom_prompt
                else:
                    target_length = min(max(int(len(content) * target_ratio), 100), 1000)
    
                    prompt = f"""请将以下内容总结为约{target_length}字的精炼版本,保留核心信息和关键要点:
    
    {content}
    
    总结要求:
    1. 保持原文的主要观点和逻辑结构
    2. 去除冗余和次要信息
    3. 使用简洁明了的语言
    4. 确保信息的准确性和完整性"""
    
                response = self.client.chat.completions.create(
                    model=OPENAI_MODEL,
                    messages=[
                        {"role": "system", "content": "你是一个专业的内容总结专家,擅长将长文本压缩为精炼的摘要。"},
                        {"role": "user", "content": prompt}
                    ],
                    max_tokens=MAX_OUTPUT_TOKENS,
                    temperature=0.1
                )
                
                return response.choices[0].message.content.strip()
                
            except Exception as e:
                logger.error(f"内容总结失败: {e}")
                return f"总结失败: {str(e)}"
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 mentions the character limit constraint and that it returns a summary, but lacks critical behavioral details: whether it handles large files, what happens with unsupported formats, error conditions, performance characteristics, or authentication requirements. For a file-reading tool with zero annotation coverage, this leaves significant gaps.

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 efficiently structured with a clear purpose statement followed by Args and Returns sections. The Chinese text is concise and front-loaded with the core functionality. However, the character limit note in parentheses feels slightly tacked on rather than integrated, and the overall description could be more polished in its flow.

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 moderate complexity (file reading + summarization), no annotations, and an output schema that presumably documents the return value, the description is minimally adequate. It covers the basic what and how but lacks important context about limitations, error handling, and differentiation from similar tools. The presence of an output schema helps, but the description should do more to guide effective use.

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 meaningful context for both parameters: it explains that 'filepath' is for text files (beyond just being a string path) and that 'target_ratio' controls compression (0.1-1.0 range). However, with 0% schema description coverage, the schema provides only basic type information. The description compensates somewhat but doesn't fully explain parameter interactions or provide examples of effective target_ratio values.

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: '读取txt等格式的文本文件并总结内容' (read text files like txt and summarize content). It specifies the resource (text files) and action (read and summarize). However, it doesn't explicitly differentiate from sibling tools like 'summarize_content' or 'read_and_summarize_pdf_file', which reduces clarity about when to choose this specific tool.

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 minimal usage guidance. It mentions file format support (txt等格式) and a character limit (限制2k字符), but offers no explicit advice on when to use this tool versus alternatives like 'summarize_content' or 'read_and_summarize_pdf_file'. There's no mention of prerequisites, typical use cases, or comparisons with sibling tools.

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

Related 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/yzfly/fullscope-mcp-server'

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