メモリMCPサーバー
データの一貫性を維持するための厳密な検証ルールを備え、メモリ内のエンティティ、リレーション、および観測を管理するためのナレッジ グラフ機能を提供するモデル コンテキスト プロトコル (MCP) サーバー。
インストール
Claude Desktop にサーバーをインストールします。
mcp install main.py -v MEMORY_FILE_PATH=/path/to/memory.jsonl
Related MCP server: Qualitative Researcher MCP Server
データ検証ルール
エンティティ名
エンティティタイプ
次のエンティティ タイプがサポートされています。
観察
空でない文字列
最大500文字
エンティティごとに一意である必要があります
事実と客観的な記述であるべきである
関連する場合はタイムスタンプを含める
関係
次の関係タイプがサポートされています。
追加の関係ルール:
使用法
サーバーは、ナレッジ グラフを管理するためのツールを提供します。
エンティティを取得
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}")
グラフを取得
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}")
エンティティを作成する
# Valid entity creation
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}")
観察を追加
# Valid observation
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}")
関係を作成する
# Valid relation
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}")
検索メモリ
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}")
検索機能は以下をサポートします:
エンティティの削除
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}")
関係を削除
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}")
フラッシュメモリ
result = await session.call_tool("flush_memory", {})
if not result.success:
print(f"Error flushing memory: {result.error}")
エラーの種類
サーバーは次のエラー タイプを使用します。
NOT_FOUND : エンティティまたはリソースが見つかりません
VALIDATION_ERROR : 入力データが無効です
INTERNAL_ERROR : サーバー側エラー
ALREADY_EXISTS : リソースが既に存在します
INVALID_RELATION : エンティティ間の関係が無効です
応答モデル
すべてのツールは、次のモデルを使用して入力された応答を返します。
エンティティレスポンス
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
発達
テストの実行
新機能の追加
validation.pyの検証ルールを更新する
tests/test_validation.pyにテストを追加する
knowledge_graph_manager.pyに変更を実装する