メモリMCPサーバー
データの一貫性を維持するための厳密な検証ルールを備え、メモリ内のエンティティ、リレーション、および観測を管理するためのナレッジ グラフ機能を提供するモデル コンテキスト プロトコル (MCP) サーバー。
インストール
Claude Desktop にサーバーをインストールします。
mcp install main.py -v MEMORY_FILE_PATH=/path/to/memory.jsonl
データ検証ルール
エンティティ名
- 小文字で始まる必要があります
- 小文字、数字、ハイフンを含めることができます
- 最大100文字
- グラフ内で一意である必要があります
- 有効な名前の例:
python-project
、 meeting-notes-2024
、 user-john
エンティティタイプ
次のエンティティ タイプがサポートされています。
person
:人間の実体concept
: 抽象的な考えや原則project
: 仕事の取り組みまたはタスクdocument
: あらゆる形式の文書tool
: ソフトウェアツールまたはユーティリティorganization
: 企業またはグループlocation
: 物理的または仮想的な場所event
: 時間制限のある発生
観察
- 空でない文字列
- 最大500文字
- エンティティごとに一意である必要があります
- 事実と客観的な記述であるべきである
- 関連する場合はタイムスタンプを含める
関係
次の関係タイプがサポートされています。
knows
:人と人とのつながりcontains
: 親子関係uses
: 別のエンティティを利用するエンティティcreated
:著作者/創作関係belongs-to
:メンバーシップ/所有権depends-on
:依存関係related-to
:一般的な関係
追加の関係ルール:
- ソースエンティティとターゲットエンティティの両方が存在する必要があります
- 自己参照関係は許可されません
- 循環依存は許可されません
- 定義済みの関係タイプを使用する必要があります
使用法
サーバーは、ナレッジ グラフを管理するためのツールを提供します。
エンティティを取得
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}")
検索機能は以下をサポートします:
- 時間的なクエリ(例:「最新」、「最後」、「最新」)
- アクティビティクエリ(例:「ワークアウト」、「エクササイズ」)
- 一般的なエンティティ検索
- 80%の類似度閾値を持つファジーマッチング
- 重み付け検索:
- エンティティ名(重み: 1.0)
- エンティティタイプ(重み: 0.8)
- 観察結果(重み:0.6)
エンティティの削除
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
に変更を実装する