Skip to main content
Glama
yzfly

MCP Python Interpreter

by yzfly

write_file

Write content to files in Python environments. Specify file path, content, and whether to overwrite existing files.

Instructions

Write content to a file.

Args:
    file_path: Path to the file to write
    content: Content to write
    overwrite: Whether to overwrite if exists

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
contentYes
overwriteNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The main handler function for the 'write_file' tool, decorated with @mcp.tool() for registration in the MCP framework. It handles writing string content to a file path relative to the working directory, with security checks, overwrite option, directory creation, and detailed success/error responses.
    @mcp.tool()
    def write_file(
        file_path: str,
        content: str,
        overwrite: bool = False
    ) -> str:
        """
        Write content to a file.
        
        Args:
            file_path: Path to the file to write
            content: Content to write
            overwrite: Whether to overwrite if exists
        """
        path = Path(file_path)
        if path.is_absolute():
            if not is_path_allowed(path):
                return f"Access denied: Can only write files in working directory: {WORKING_DIR}"
        else:
            path = WORKING_DIR / path
        
        try:
            if path.exists() and not overwrite:
                return f"File '{path}' exists. Use overwrite=True to replace."
            
            path.parent.mkdir(parents=True, exist_ok=True)
            
            with open(path, 'w', encoding='utf-8') as f:
                f.write(content)
                f.flush()
                os.fsync(f.fileno())
            
            file_size_kb = path.stat().st_size / 1024
            return f"Successfully wrote to {path}. Size: {file_size_kb:.2f} KB"
        
        except Exception as e:
            return f"Error writing file: {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 'overwrite' behavior, which is useful, but fails to cover critical aspects like error handling (e.g., if the file path is invalid or permissions are insufficient), side effects, or response format details.

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, followed by a structured Args list. Every sentence adds value, but the formatting could be slightly more polished for readability.

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 complexity (a write operation with 3 parameters) and no annotations, the description is moderately complete. It covers parameters well and an output schema exists, but it lacks behavioral context like error cases or system dependencies, leaving gaps for safe usage.

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?

Schema description coverage is 0%, so the description must compensate. It provides clear semantics for all three parameters ('file_path', 'content', 'overwrite') in the Args section, adding meaningful context beyond the bare schema, though it could include more details like path format or content encoding.

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 with 'Write content to a file', specifying the verb 'write' and resource 'file'. However, it doesn't explicitly differentiate from sibling tools like 'read_file', though the action is distinct by nature.

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. The description lacks context about prerequisites, such as file permissions or system constraints, and doesn't mention sibling tools like 'read_file' for comparison.

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