Skip to main content
Glama

ContextSave

Saves file contents and descriptions from specified paths into a single text file. Generates a unique ID or uses a custom one. Ideal for organizing code context and project details efficiently.

Instructions

Saves provided description and file contents of all the relevant file paths or globs in a single text file.

  • Provide random 3 word unqiue id or whatever user provided.

  • Leave project path as empty string if no project path

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
descriptionYes
idYes
project_root_pathYes
relevant_file_globsYes

Implementation Reference

  • Handler logic for ContextSave tool within get_tool_output function. Processes globs to find relevant files, reads their contents, saves the context memory to a file using save_memory, and returns the saved file path.
    elif isinstance(arg, ContextSave): context.console.print("Calling task memory tool") relevant_files = [] warnings = "" # Expand user in project root path arg.project_root_path = os.path.expanduser(arg.project_root_path) for fglob in arg.relevant_file_globs: # Expand user in glob pattern before checking if it's absolute fglob = expand_user(fglob) # If not absolute after expansion, join with project root path if not os.path.isabs(fglob) and arg.project_root_path: fglob = os.path.join(arg.project_root_path, fglob) globs = glob.glob(fglob, recursive=True) relevant_files.extend(globs[:1000]) if not globs: warnings += f"Warning: No files found for the glob: {fglob}\n" relevant_files_data, _, _ = read_files( relevant_files[:10_000], None, None, context ) save_path = save_memory( arg, relevant_files_data, context.bash_state.serialize() ) if not relevant_files and arg.relevant_file_globs: output_ = f'Error: No files found for the given globs. Context file successfully saved at "{save_path}", but please fix the error.' elif warnings: output_ = warnings + "\nContext file successfully saved at " + save_path else: output_ = save_path # Try to open the saved file try_open_file(save_path) output = output_, 0.0 else:
  • Pydantic model defining the input schema for ContextSave tool.
    class ContextSave(BaseModel): id: str project_root_path: str description: str relevant_file_globs: list[str]
  • MCP Tool registration including schema, name, description, and annotations. TOOL_PROMPTS list is returned by server.list_tools().
    Tool( inputSchema=remove_titles_from_schema(ContextSave.model_json_schema()), name="ContextSave", description=""" Saves provided description and file contents of all the relevant file paths or globs in a single text file. - Provide random 3 word unqiue id or whatever user provided. - Leave project path as empty string if no project path""", annotations=ToolAnnotations(readOnlyHint=True, openWorldHint=False), ),
  • Helper function to save the context memory to a file named after the task id, including formatted description and relevant files content. Called by ContextSave handler.
    def save_memory( task_memory: ContextSave, relevant_files: str, bash_state_dict: Optional[dict[str, Any]] = None, ) -> str: app_dir = get_app_dir_xdg() memory_dir = os.path.join(app_dir, "memory") os.makedirs(memory_dir, exist_ok=True) task_id = task_memory.id if not task_id: raise Exception("Task id can not be empty") memory_data = format_memory(task_memory, relevant_files) memory_file_full = os.path.join(memory_dir, f"{task_id}.txt") with open(memory_file_full, "w") as f: f.write(memory_data) # Save bash state if provided if bash_state_dict is not None: state_file = os.path.join(memory_dir, f"{task_id}_bash_state.json") with open(state_file, "w") as f: json.dump(bash_state_dict, f, indent=2) return memory_file_full
  • Helper function to format the task memory string including project root, description, relevant globs, and file contents. Used by save_memory.
    def format_memory(task_memory: ContextSave, relevant_files: str) -> str: memory_data = "" if task_memory.project_root_path: memory_data += ( f"# PROJECT ROOT = {shlex.quote(task_memory.project_root_path)}\n" ) memory_data += task_memory.description memory_data += ( "\n\n" + "# Relevant file paths\n" + ", ".join(map(shlex.quote, task_memory.relevant_file_globs)) ) memory_data += "\n\n# Relevant Files:\n" + relevant_files return memory_data

Other Tools

Related 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/rusiaaman/wcgw'

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