Skip to main content
Glama
yzfly

MCP Python Interpreter

by yzfly

read_file

Read file contents safely with configurable size limits to prevent memory issues when accessing files in Python environments.

Instructions

Read the content of any file, with size limits for safety.

Args:
    file_path: Path to the file
    max_size_kb: Maximum file size to read in KB

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
max_size_kbNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The @mcp.tool()-decorated function implementing the 'read_file' tool. It reads file contents with security checks (path allowance, size limits), handles text and binary files, and formats output with syntax highlighting for source files.
    @mcp.tool()
    def read_file(file_path: str, max_size_kb: int = 1024) -> str:
        """
        Read the content of any file, with size limits for safety.
        
        Args:
            file_path: Path to the file
            max_size_kb: Maximum file size to read in KB
        """
        path = Path(file_path)
        if path.is_absolute():
            if not is_path_allowed(path):
                return f"Access denied: Can only read files in working directory: {WORKING_DIR}"
        else:
            path = WORKING_DIR / path
        
        try:
            if not path.exists():
                return f"Error: File '{file_path}' not found"
            
            file_size_kb = path.stat().st_size / 1024
            if file_size_kb > max_size_kb:
                return f"Error: File size ({file_size_kb:.2f} KB) exceeds maximum ({max_size_kb} KB)"
            
            try:
                with open(path, 'r', encoding='utf-8') as f:
                    content = f.read()
                
                source_extensions = ['.py', '.js', '.html', '.css', '.json', '.xml', '.md', '.txt', '.sh', '.bat', '.ps1']
                if path.suffix.lower() in source_extensions:
                    file_type = path.suffix[1:] if path.suffix else 'plain'
                    return f"File: {file_path}\n\n```{file_type}\n{content}\n```"
                
                return f"File: {file_path}\n\n{content}"
            
            except UnicodeDecodeError:
                with open(path, 'rb') as f:
                    content = f.read()
                    hex_content = content.hex()
                    return f"Binary file: {file_path}\nSize: {len(content)} bytes\nHex (first 1024 chars):\n{hex_content[:1024]}"
        
        except Exception as e:
            return f"Error reading file: {str(e)}"
Behavior3/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 adds context about 'size limits for safety', which is useful for understanding constraints, but it doesn't cover other behavioral traits such as error handling, permissions required, or what happens with large files beyond the limit. This leaves gaps in transparency.

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 appropriately sized and front-loaded with the core purpose in the first sentence, followed by parameter details. Every sentence adds value, but the structure could be slightly improved by integrating the parameter explanations more seamlessly rather than as a separate 'Args' section, though this is minor.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/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 (2 parameters, no annotations, but with an output schema), the description is mostly complete. It covers the purpose and parameters, and since an output schema exists, it doesn't need to explain return values. However, it lacks details on error cases or performance, which could enhance completeness for a file-reading tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 0%, so the description must compensate. It adds meaning by explaining 'file_path' as 'Path to the file' and 'max_size_kb' as 'Maximum file size to read in KB', which clarifies the parameters beyond the schema's basic titles. However, it doesn't detail format specifics (e.g., absolute vs. relative paths) or default behavior, keeping it from a perfect score.

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 as 'Read the content of any file' with a specific verb ('Read') and resource ('file'), making it immediately understandable. However, it doesn't explicitly differentiate from sibling tools like 'list_directory' or 'run_python_file', which prevents a perfect score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage by mentioning 'size limits for safety', suggesting it's for reading files within safe bounds, but it doesn't provide explicit guidance on when to use this tool versus alternatives like 'list_directory' for file metadata or 'run_python_file' for executing code. No exclusions or clear alternatives are stated.

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/yzfly/mcp-python-interpreter'

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