Skip to main content
Glama

Create Text File

create_text_file
Destructive

Create or overwrite text files by specifying a relative path and content. This tool writes files and returns success or failure messages.

Instructions

Write a new file or overwrite an existing file. Returns a message indicating success or failure.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
relative_pathYesThe relative path to the file to create.
contentYesThe (appropriately encoded) content to write to the file.

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The handler implementation of the 'create_text_file' tool. The 'apply' method validates the relative path, creates necessary directories, writes the content to the file, and returns a success message indicating if it was a new file or overwrite.
    class CreateTextFileTool(Tool, ToolMarkerCanEdit):
        """
        Creates/overwrites a file in the project directory.
        """
    
        def apply(self, relative_path: str, content: str) -> str:
            """
            Write a new file or overwrite an existing file.
    
            :param relative_path: the relative path to the file to create
            :param content: the (appropriately encoded) content to write to the file
            :return: a message indicating success or failure
            """
            project_root = self.get_project_root()
            abs_path = (Path(project_root) / relative_path).resolve()
            will_overwrite_existing = abs_path.exists()
    
            if will_overwrite_existing:
                self.project.validate_relative_path(relative_path, require_not_ignored=True)
            else:
                assert abs_path.is_relative_to(
                    self.get_project_root()
                ), f"Cannot create file outside of the project directory, got {relative_path=}"
    
            abs_path.parent.mkdir(parents=True, exist_ok=True)
            abs_path.write_text(content, encoding=self.project.project_config.encoding)
            answer = f"File created: {relative_path}."
            if will_overwrite_existing:
                answer += " Overwrote existing file."
            return answer
  • The ToolRegistry automatically discovers all subclasses of Tool in serena.tools modules (including CreateTextFileTool) and registers them with snake_case names derived from the class name (e.g., 'create_text_file').
    self._tool_dict: dict[str, RegisteredTool] = {}
    for cls in iter_subclasses(Tool):
        if not any(cls.__module__.startswith(pkg) for pkg in tool_packages):
            continue
        is_optional = issubclass(cls, ToolMarkerOptional)
        name = cls.get_name_from_cls()
        if name in self._tool_dict:
            raise ValueError(f"Duplicate tool name found: {name}. Tool classes must have unique names.")
        self._tool_dict[name] = RegisteredTool(tool_class=cls, is_optional=is_optional, tool_name=name)
  • Extracts function metadata (including JSON schema from type hints and docstring) from the tool's 'apply' method for use in MCP tool definitions.
    def get_apply_fn_metadata_from_cls(cls) -> FuncMetadata:
        """Get the metadata for the apply method from the class (static metadata).
        Needed for creating MCP tools in a separate process without running into serialization issues.
        """
        # First try to get from __dict__ to handle dynamic docstring changes
        if "apply" in cls.__dict__:
            apply_fn = cls.__dict__["apply"]
        else:
            # Fall back to getattr for inherited methods
            apply_fn = getattr(cls, "apply", None)
            if apply_fn is None:
                raise AttributeError(f"apply method not defined in {cls}. Did you forget to implement it?")
    
        return func_metadata(apply_fn, skip_names=["self", "cls"])
  • Derives the tool name 'create_text_file' from the class name 'CreateTextFileTool' by removing 'Tool' suffix and converting CamelCase to snake_case.
    def get_name_from_cls(cls) -> str:
        name = cls.__name__
        if name.endswith("Tool"):
            name = name[:-4]
        # convert to snake_case
        name = "".join(["_" + c.lower() if c.isupper() else c for c in name]).lstrip("_")
        return name
Behavior4/5

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

Annotations already indicate destructiveHint=true and readOnlyHint=false, but the description adds valuable context by specifying that it can 'overwrite an existing file' and returns 'success or failure' messages. This clarifies the destructive nature beyond the annotation and provides outcome expectations, though it doesn't mention permissions, rate limits, or file encoding details.

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 consists of two tightly focused sentences that efficiently convey the core functionality and outcome. Every word serves a purpose with zero redundancy, and the information is front-loaded with the primary action stated immediately.

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 destructive nature indicated by annotations, the presence of an output schema, and 100% parameter coverage, the description provides adequate context. It covers the tool's primary behavior and outcome expectations, though it could benefit from mentioning encoding requirements or error scenarios for a more complete picture.

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?

With 100% schema description coverage, the input schema fully documents both parameters. The description doesn't add any parameter-specific information beyond what's in the schema, so it meets the baseline score of 3. No additional semantic context is provided for 'relative_path' or 'content' parameters.

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 clearly states the specific action ('Write a new file or overwrite an existing file') and resource ('file'), distinguishing it from sibling tools like 'read_file' or 'replace_content'. It precisely communicates both creation and overwrite capabilities in a single concise statement.

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 for file creation/overwriting but provides no explicit guidance on when to use this tool versus alternatives like 'replace_content' or 'write_memory'. It mentions the tool's behavior but doesn't specify scenarios where it's preferred over other file manipulation tools in the sibling list.

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/oraios/serena'

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