Skip to content

Commit f4dd391

Browse files
authored
add entity type validation handling (#320)
* add entity type validation handling * bump version
1 parent fb37565 commit f4dd391

File tree

5 files changed

+86
-27
lines changed

5 files changed

+86
-27
lines changed

graphiti_core/errors.py

+8
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,11 @@ class SearchRerankerError(GraphitiError):
6565
def __init__(self, text: str):
6666
self.message = text
6767
super().__init__(self.message)
68+
69+
70+
class EntityTypeValidationError(GraphitiError):
71+
"""Raised when an entity type uses protected attribute names."""
72+
73+
def __init__(self, entity_type: str, entity_type_attribute: str):
74+
self.message = f'{entity_type_attribute} cannot be used as an attribute for {entity_type} as it is a protected attribute name.'
75+
super().__init__(self.message)

graphiti_core/graphiti.py

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
resolve_extracted_nodes,
7777
)
7878
from graphiti_core.utils.maintenance.temporal_operations import get_edge_contradictions
79+
from graphiti_core.utils.ontology_utils.entity_types_utils import validate_entity_types
7980

8081
logger = logging.getLogger(__name__)
8182

@@ -319,6 +320,8 @@ async def add_episode_endpoint(episode_data: EpisodeData):
319320
entity_edges: list[EntityEdge] = []
320321
now = utc_now()
321322

323+
validate_entity_types(entity_types)
324+
322325
previous_episodes = (
323326
await self.retrieve_episodes(
324327
reference_time, last_n=RELEVANT_SCHEMA_LIMIT, group_ids=[group_id]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Copyright 2024, Zep Software, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
from pydantic import BaseModel
18+
19+
from graphiti_core.errors import EntityTypeValidationError
20+
from graphiti_core.nodes import EntityNode
21+
22+
23+
def validate_entity_types(
24+
entity_types: dict[str, BaseModel] | None,
25+
) -> bool:
26+
if entity_types is None:
27+
return True
28+
29+
entity_node_field_names = EntityNode.model_fields.keys()
30+
31+
for entity_type_name, entity_type_model in entity_types.items():
32+
entity_type_field_names = entity_type_model.model_fields.keys()
33+
for entity_type_field_name in entity_type_field_names:
34+
if entity_type_field_name in entity_node_field_names:
35+
raise EntityTypeValidationError(entity_type_name, entity_type_field_name)
36+
37+
return True

mcp_server/README.md

+37-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
# Graphiti MCP Server
22

3-
Graphiti is a framework for building and querying temporally-aware knowledge graphs, specifically tailored for AI agents operating in dynamic environments. Unlike traditional retrieval-augmented generation (RAG) methods, Graphiti continuously integrates user interactions, structured and unstructured enterprise data, and external information into a coherent, queryable graph. The framework supports incremental data updates, efficient retrieval, and precise historical queries without requiring complete graph recomputation, making it suitable for developing interactive, context-aware AI applications.
3+
Graphiti is a framework for building and querying temporally-aware knowledge graphs, specifically tailored for AI agents
4+
operating in dynamic environments. Unlike traditional retrieval-augmented generation (RAG) methods, Graphiti
5+
continuously integrates user interactions, structured and unstructured enterprise data, and external information into a
6+
coherent, queryable graph. The framework supports incremental data updates, efficient retrieval, and precise historical
7+
queries without requiring complete graph recomputation, making it suitable for developing interactive, context-aware AI
8+
applications.
49

5-
This is an experimental Model Context Protocol (MCP) server implementation for Graphiti. The MCP server exposes Graphiti's key functionality through the MCP protocol, allowing AI assistants to interact with Graphiti's knowledge graph capabilities.
10+
This is an experimental Model Context Protocol (MCP) server implementation for Graphiti. The MCP server exposes
11+
Graphiti's key functionality through the MCP protocol, allowing AI assistants to interact with Graphiti's knowledge
12+
graph capabilities.
613

714
## Features
815

@@ -59,7 +66,7 @@ uv run graphiti_mcp_server.py
5966
With options:
6067

6168
```bash
62-
uv run graphiti_mcp_server.py --model gpt-4o --transport sse
69+
uv run graphiti_mcp_server.py --model gpt-4o-mini --transport sse
6370
```
6471

6572
Available arguments:
@@ -72,33 +79,34 @@ Available arguments:
7279

7380
### Docker Deployment
7481

75-
The Graphiti MCP server can be deployed using Docker. The Dockerfile uses `uv` for package management, ensuring consistent dependency installation.
82+
The Graphiti MCP server can be deployed using Docker. The Dockerfile uses `uv` for package management, ensuring
83+
consistent dependency installation.
7684

7785
#### Environment Configuration
7886

7987
Before running the Docker Compose setup, you need to configure the environment variables. You have two options:
8088

8189
1. **Using a .env file** (recommended):
8290

83-
- Copy the provided `.env.example` file to create a `.env` file:
84-
```bash
85-
cp .env.example .env
86-
```
87-
- Edit the `.env` file to set your OpenAI API key and other configuration options:
88-
```
89-
# Required for LLM operations
90-
OPENAI_API_KEY=your_openai_api_key_here
91-
MODEL_NAME=gpt-4o
92-
# Optional: OPENAI_BASE_URL only needed for non-standard OpenAI endpoints
93-
# OPENAI_BASE_URL=https://api.openai.com/v1
94-
```
95-
- The Docker Compose setup is configured to use this file if it exists (it's optional)
91+
- Copy the provided `.env.example` file to create a `.env` file:
92+
```bash
93+
cp .env.example .env
94+
```
95+
- Edit the `.env` file to set your OpenAI API key and other configuration options:
96+
```
97+
# Required for LLM operations
98+
OPENAI_API_KEY=your_openai_api_key_here
99+
MODEL_NAME=gpt-4o-mini
100+
# Optional: OPENAI_BASE_URL only needed for non-standard OpenAI endpoints
101+
# OPENAI_BASE_URL=https://api.openai.com/v1
102+
```
103+
- The Docker Compose setup is configured to use this file if it exists (it's optional)
96104
97105
2. **Using environment variables directly**:
98-
- You can also set the environment variables when running the Docker Compose command:
99-
```bash
100-
OPENAI_API_KEY=your_key MODEL_NAME=gpt-4o docker compose up
101-
```
106+
- You can also set the environment variables when running the Docker Compose command:
107+
```bash
108+
OPENAI_API_KEY=your_key MODEL_NAME=gpt-4o-mini docker compose up
109+
```
102110
103111
#### Neo4j Configuration
104112
@@ -154,7 +162,7 @@ To use the Graphiti MCP server with an MCP-compatible client, configure it to co
154162
"NEO4J_USER": "neo4j",
155163
"NEO4J_PASSWORD": "demodemo",
156164
"OPENAI_API_KEY": "${OPENAI_API_KEY}",
157-
"MODEL_NAME": "gpt-4o"
165+
"MODEL_NAME": "gpt-4o-mini"
158166
}
159167
}
160168
}
@@ -192,7 +200,7 @@ Or start the server with uv and connect to it:
192200
"NEO4J_USER": "neo4j",
193201
"NEO4J_PASSWORD": "demodemo",
194202
"OPENAI_API_KEY": "${OPENAI_API_KEY}",
195-
"MODEL_NAME": "gpt-4o"
203+
"MODEL_NAME": "gpt-4o-mini"
196204
}
197205
}
198206
}
@@ -215,7 +223,8 @@ The Graphiti MCP server exposes the following tools:
215223
216224
## Working with JSON Data
217225
218-
The Graphiti MCP server can process structured JSON data through the `add_episode` tool with `source="json"`. This allows you to automatically extract entities and relationships from structured data:
226+
The Graphiti MCP server can process structured JSON data through the `add_episode` tool with `source="json"`. This
227+
allows you to automatically extract entities and relationships from structured data:
219228
220229
```
221230
add_episode(
@@ -236,7 +245,8 @@ To integrate the Graphiti MCP Server with the Cursor IDE, follow these steps:
236245
python graphiti_mcp_server.py --transport sse --use-custom-entities --group-id <your_group_id>
237246
```
238247
239-
Hint: specify a `group_id` to retain prior graph data. If you do not specify a `group_id`, the server will create a new graph
248+
Hint: specify a `group_id` to retain prior graph data. If you do not specify a `group_id`, the server will create a new
249+
graph
240250
241251
2. Configure Cursor to connect to the Graphiti MCP server.
242252
@@ -254,7 +264,8 @@ Hint: specify a `group_id` to retain prior graph data. If you do not specify a `
254264

255265
4. Kick off an agent session in Cursor.
256266

257-
The integration enables AI assistants in Cursor to maintain persistent memory through Graphiti's knowledge graph capabilities.
267+
The integration enables AI assistants in Cursor to maintain persistent memory through Graphiti's knowledge graph
268+
capabilities.
258269
259270
## Requirements
260271

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "graphiti-core"
3-
version = "0.8.7"
3+
version = "0.8.8"
44
description = "A temporal graph building library"
55
authors = [
66
"Paul Paliychuk <[email protected]>",

0 commit comments

Comments
 (0)