Skip to main content
Glama

write_memory

Store project information in markdown format for future reference. Create meaningful memory files to retain useful data across tasks.

Instructions

Write some information (utf-8-encoded) about this project that can be useful for future tasks to a memory in md format. The memory name should be meaningful.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
memory_file_nameYes
contentYes
max_answer_charsNo

Implementation Reference

  • The WriteMemoryTool class provides the core handler logic for the 'write_memory' tool. Its 'apply' method validates input length and saves the content to a project-specific memory file using the agent's memories_manager.
    class WriteMemoryTool(Tool, ToolMarkerCanEdit):
        """
        Writes a named memory (for future reference) to Serena's project-specific memory store.
        """
    
        def apply(self, memory_file_name: str, content: str, max_answer_chars: int = -1) -> str:
            """
            Write some information (utf-8-encoded) about this project that can be useful for future tasks to a memory in md format.
            The memory name should be meaningful.
            """
            # NOTE: utf-8 encoding is configured in the MemoriesManager
            if max_answer_chars == -1:
                max_answer_chars = self.agent.serena_config.default_max_tool_answer_chars
            if len(content) > max_answer_chars:
                raise ValueError(
                    f"Content for {memory_file_name} is too long. Max length is {max_answer_chars} characters. "
                    + "Please make the content shorter."
                )
    
            return self.memories_manager.save_memory(memory_file_name, content)
  • ToolRegistry automatically discovers and registers all subclasses of Tool (including WriteMemoryTool) from the 'serena.tools' package, mapping them to tool names like 'write_memory'.
    class ToolRegistry:
        def __init__(self) -> None:
            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)
  • The get_name_from_cls method in the Tool base class derives the tool name 'write_memory' from the class name 'WriteMemoryTool' by removing 'Tool' suffix and converting to snake_case.
    @classmethod
    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
  • Import of memory_tools.py in tools/__init__.py ensures WriteMemoryTool is discoverable by ToolRegistry via module scanning.
    from .memory_tools import *
  • The 'apply' method signature and docstring define the input schema (parameters: memory_file_name:str, content:str, max_answer_chars:int=-1) and output (str) for the write_memory tool, used for validation and MCP tool description.
    def apply(self, memory_file_name: str, content: str, max_answer_chars: int = -1) -> str:
        """
        Write some information (utf-8-encoded) about this project that can be useful for future tasks to a memory in md format.
        The memory name should be meaningful.
        """
        # NOTE: utf-8 encoding is configured in the MemoriesManager
        if max_answer_chars == -1:
            max_answer_chars = self.agent.serena_config.default_max_tool_answer_chars
        if len(content) > max_answer_chars:
            raise ValueError(
                f"Content for {memory_file_name} is too long. Max length is {max_answer_chars} characters. "
                + "Please make the content shorter."
            )
    
        return self.memories_manager.save_memory(memory_file_name, content)

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