Knowledge Graph MCP server

Enables persistent memory and structured knowledge management for enhanced personalization and context retention in natural language interactions through a local graph database.
Back to servers
Provider
Shane Holloman
Release date
Dec 26, 2024
Language
TypeScript
Stats
194 stars

The MCP Knowledge Graph server provides persistent memory for AI models by implementing a local knowledge graph that allows models to remember information across conversations. This server works with any AI model supporting the Model Context Protocol (MCP) or function calling capabilities.

Installation

To use the MCP Knowledge Graph server, you need to configure your AI platform to connect to it. The server is available via NPX.

Setup with Claude Desktop

Add this configuration to your claude_desktop_config.json file:

{
  "mcpServers": {
    "memory": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-knowledge-graph",
        "--memory-path",
        "/path/to/your/memory.jsonl"
      ],
      "autoapprove": [
        "create_entities",
        "create_relations",
        "add_observations",
        "delete_entities",
        "delete_observations",
        "delete_relations",
        "read_graph",
        "search_nodes",
        "open_nodes"
      ]
    }
  }
}

Make sure to replace /path/to/your/memory.jsonl with your preferred storage location. If no path is specified, the server will default to memory.jsonl in its installation directory.

Core Concepts

Entities

Entities are the primary nodes in the knowledge graph. Each entity has:

  • A unique name (identifier)
  • An entity type (e.g., "person", "organization", "event")
  • A list of observations

Example entity:

{
  "name": "John_Smith",
  "entityType": "person",
  "observations": ["Speaks fluent Spanish"]
}

Relations

Relations define directed connections between entities in active voice.

Example relation:

{
  "from": "John_Smith",
  "to": "ExampleCorp",
  "relationType": "works_at"
}

Observations

Observations are discrete pieces of information about an entity:

  • Stored as strings
  • Attached to specific entities
  • Can be added or removed independently
  • Should be atomic (one fact per observation)

Example:

{
  "entityName": "John_Smith",
  "observations": [
    "Speaks fluent Spanish",
    "Graduated in 2019",
    "Prefers morning meetings"
  ]
}

Available API Tools

create_entities

Creates multiple new entities in the knowledge graph.

{
  "entities": [
    {
      "name": "John_Smith",
      "entityType": "person",
      "observations": ["Speaks Spanish", "Lives in New York"]
    },
    {
      "name": "ExampleCorp",
      "entityType": "organization",
      "observations": ["Founded in 2010"]
    }
  ]
}

create_relations

Creates new relations between entities.

{
  "relations": [
    {
      "from": "John_Smith",
      "to": "ExampleCorp",
      "relationType": "works_at"
    }
  ]
}

add_observations

Adds new observations to existing entities.

{
  "observations": [
    {
      "entityName": "John_Smith",
      "contents": ["Enjoys hiking", "Has a dog named Max"]
    }
  ]
}

delete_entities

Removes entities and their relations.

{
  "entityNames": ["John_Smith"]
}

delete_observations

Removes specific observations from entities.

{
  "deletions": [
    {
      "entityName": "John_Smith",
      "observations": ["Enjoys hiking"]
    }
  ]
}

delete_relations

Removes specific relations from the graph.

{
  "relations": [
    {
      "from": "John_Smith",
      "to": "ExampleCorp",
      "relationType": "works_at"
    }
  ]
}

read_graph

Reads the entire knowledge graph. No input required.

search_nodes

Searches for nodes based on a query.

{
  "query": "Smith"
}

open_nodes

Retrieves specific nodes by name.

{
  "names": ["John_Smith", "ExampleCorp"]
}

Using With AI Models

System Prompt Example

Here's an example prompt for chat personalization that you can adapt for any AI model:

Follow these steps for each interaction:

1. User Identification:
   - You should assume that you are interacting with default_user
   - If you have not identified default_user, proactively try to do so.

2. Memory Retrieval:
   - Always begin your chat by saying only "Remembering..." and retrieve all relevant information from your knowledge graph
   - Always refer to your knowledge graph as your "memory"

3. Memory Gathering:
   - While conversing with the user, be attentive to any new information that falls into these categories:
     a) Basic Identity (age, gender, location, job title, education level, etc.)
     b) Behaviors (interests, habits, etc.)
     c) Preferences (communication style, preferred language, etc.)
     d) Goals (goals, targets, aspirations, etc.)
     e) Relationships (personal and professional relationships up to 3 degrees of separation)

4. Memory Update:
   - If any new information was gathered during the interaction, update your memory as follows:
     a) Create entities for recurring organizations, people, and significant events
     b) Connect them to the current entities using relations
     c) Store facts about them as observations

For Claude users, you could use this prompt in the "Custom Instructions" field of a Claude.ai Project. For other models, adapt it to their respective instruction formats.

Integration with Other AI Models

To integrate with any AI model that supports function calling:

  1. Configure the model to access the MCP server
  2. Ensure the model can make function calls to the exposed tools
  3. Adapt the system prompt to the specific model's instruction format
  4. Use the same knowledge graph operations regardless of the model

The server's knowledge graph structure and API are model-agnostic, allowing for flexible integration with various AI platforms.

How to add this MCP server to Cursor

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.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

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.

How to use the MCP server

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 explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later