save_memory
Store conversation memories in a vector database for persistent recall across sessions, enabling AI assistants to maintain context and reference past interactions.
Instructions
Save a memory to the vector store.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| memory | Yes |
Implementation Reference
- server.py:24-36 (handler)The main handler function for the 'save_memory' tool. It creates or retrieves a vector store named 'memories', writes the input memory to a temporary file, uploads it to the vector store using OpenAI's API, and returns a success status with the vector store ID.@mcp.tool() def save_memory(memory: str): """Save a memory to the vector store.""" vector_store = get_or_create_vector_store() with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".txt") as f: f.write(memory) f.flush() client.vector_stores.files.upload_and_poll( vector_store_id=vector_store.id, file=open(f.name, "rb"), ) return {"status": "saved", "vector store id": vector_store.id}
- server.py:24-24 (registration)The @mcp.tool() decorator registers the save_memory function as an MCP tool.@mcp.tool()
- server.py:16-21 (helper)Helper function to get an existing vector store named 'memories' or create a new one if it doesn't exist. Used by save_memory.def get_or_create_vector_store(): stores = client.vector_stores.list() for store in stores: if store.name == VECTOR_STORE_NAME: return store return client.vector_stores.create(name=VECTOR_STORE_NAME)