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
| Name | Required | Description | Default |
|---|---|---|---|
| memory_file_name | Yes | ||
| content | Yes | ||
| max_answer_chars | No |
Implementation Reference
- src/serena/tools/memory_tools.py:6-27 (handler)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)
- src/serena/tools/tools_base.py:356-367 (registration)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
- src/serena/tools/__init__.py:5-5 (registration)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)