The Memory MCP Server provides knowledge graph functionality for managing entities, relations, and observations in memory. It enforces strict validation rules to maintain data consistency, making it ideal for applications that need structured knowledge representation.
To install the server in Claude Desktop:
mcp install main.py -v MEMORY_FILE_PATH=/path/to/memory.jsonl
python-project
, meeting-notes-2024
, user-john
The following entity types are supported:
person
: Human entitiesconcept
: Abstract ideas or principlesproject
: Work initiatives or tasksdocument
: Any form of documentationtool
: Software tools or utilitiesorganization
: Companies or groupslocation
: Physical or virtual placesevent
: Time-bound occurrencesThe following relation types are supported:
knows
: Person to person connectioncontains
: Parent/child relationshipuses
: Entity utilizing another entitycreated
: Authorship/creation relationshipbelongs-to
: Membership/ownershipdepends-on
: Dependency relationshiprelated-to
: Generic relationshipAdditional relation rules:
To retrieve information about a specific entity:
result = await session.call_tool("get_entity", {
"entity_name": "example"
})
if not result.success:
if result.error_type == "NOT_FOUND":
print(f"Entity not found: {result.error}")
elif result.error_type == "VALIDATION_ERROR":
print(f"Invalid input: {result.error}")
else:
print(f"Error: {result.error}")
else:
entity = result.data
print(f"Found entity: {entity}")
To retrieve the entire knowledge graph:
result = await session.call_tool("get_graph", {})
if result.success:
graph = result.data
print(f"Graph data: {graph}")
else:
print(f"Error retrieving graph: {result.error}")
To create new entities in the knowledge graph:
entities = [
Entity(
name="python-project", # Lowercase with hyphens
entityType="project", # Must be a valid type
observations=["Started development on 2024-01-29"]
),
Entity(
name="john-doe",
entityType="person",
observations=["Software engineer", "Joined team in 2024"]
)
]
result = await session.call_tool("create_entities", {
"entities": entities
})
if not result.success:
if result.error_type == "VALIDATION_ERROR":
print(f"Invalid entity data: {result.error}")
else:
print(f"Error creating entities: {result.error}")
To add a new observation to an existing entity:
result = await session.call_tool("add_observation", {
"entity": "python-project",
"observation": "Completed initial prototype" # Must be unique for entity
})
if not result.success:
if result.error_type == "NOT_FOUND":
print(f"Entity not found: {result.error}")
elif result.error_type == "VALIDATION_ERROR":
print(f"Invalid observation: {result.error}")
else:
print(f"Error adding observation: {result.error}")
To establish a relationship between two entities:
result = await session.call_tool("create_relation", {
"from_entity": "john-doe",
"to_entity": "python-project",
"relation_type": "created" # Must be a valid type
})
if not result.success:
if result.error_type == "NOT_FOUND":
print(f"Entity not found: {result.error}")
elif result.error_type == "VALIDATION_ERROR":
print(f"Invalid relation data: {result.error}")
else:
print(f"Error creating relation: {result.error}")
To search for information within the knowledge graph:
result = await session.call_tool("search_memory", {
"query": "most recent workout" # Supports natural language queries
})
if result.success:
if result.error_type == "NO_RESULTS":
print(f"No results found: {result.error}")
else:
results = result.data
print(f"Search results: {results}")
else:
print(f"Error searching memory: {result.error}")
The search functionality supports:
To remove entities from the knowledge graph:
result = await session.call_tool("delete_entities", {
"names": ["python-project", "john-doe"]
})
if not result.success:
if result.error_type == "NOT_FOUND":
print(f"Entity not found: {result.error}")
else:
print(f"Error deleting entities: {result.error}")
To remove a relationship between entities:
result = await session.call_tool("delete_relation", {
"from_entity": "john-doe",
"to_entity": "python-project"
})
if not result.success:
if result.error_type == "NOT_FOUND":
print(f"Entity not found: {result.error}")
else:
print(f"Error deleting relation: {result.error}")
To clear all data from the knowledge graph:
result = await session.call_tool("flush_memory", {})
if not result.success:
print(f"Error flushing memory: {result.error}")
The server uses the following error types:
NOT_FOUND
: Entity or resource not foundVALIDATION_ERROR
: Invalid input dataINTERNAL_ERROR
: Server-side errorALREADY_EXISTS
: Resource already existsINVALID_RELATION
: Invalid relation between entitiesAll tools return typed responses using these models:
class EntityResponse(BaseModel):
success: bool
data: Optional[Dict[str, Any]] = None
error: Optional[str] = None
error_type: Optional[str] = None
class GraphResponse(BaseModel):
success: bool
data: Optional[Dict[str, Any]] = None
error: Optional[str] = None
error_type: Optional[str] = None
class OperationResponse(BaseModel):
success: bool
error: Optional[str] = None
error_type: Optional[str] = None
To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "knowledge-graph-memory" '{"command":"python","args":["-m","main.py"],"env":{"MEMORY_FILE_PATH":"/path/to/memory.jsonl"}}'
See the official Claude Code MCP documentation for more details.
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"knowledge-graph-memory": {
"command": "python",
"args": [
"-m",
"main.py"
],
"env": {
"MEMORY_FILE_PATH": "/path/to/memory.jsonl"
}
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"knowledge-graph-memory": {
"command": "python",
"args": [
"-m",
"main.py"
],
"env": {
"MEMORY_FILE_PATH": "/path/to/memory.jsonl"
}
}
}
}
3. Restart Claude Desktop for the changes to take effect