Skip to content

Commit a3a0f5a

Browse files
authored
Merge branch 'dev' into test/metrics_in_adapters
2 parents 1bc55f9 + df163b0 commit a3a0f5a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+369
-333
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ This script will run the default pipeline:
130130
```python
131131
import cognee
132132
import asyncio
133-
from cognee.api.v1.search import SearchType
133+
from cognee.modules.search.types import SearchType
134134

135135
async def main():
136136
# Create a clean slate for cognee -- reset data and system state

cognee-mcp/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ readme = "README.md"
66
requires-python = ">=3.10"
77

88
dependencies = [
9-
"cognee",
10-
"mcp==1.2.0",
9+
"cognee[codegraph]",
10+
"mcp==1.1.3",
1111
]
1212

1313
[[project.authors]]

cognee-mcp/src/server.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import cognee
34
import logging
@@ -8,8 +9,10 @@
89
import mcp.types as types
910
from mcp.server import Server, NotificationOptions
1011
from mcp.server.models import InitializationOptions
11-
from cognee.api.v1.search import SearchType
12+
from cognee.api.v1.cognify.code_graph_pipeline import run_code_graph_pipeline
13+
from cognee.modules.search.types import SearchType
1214
from cognee.shared.data_models import KnowledgeGraph
15+
from cognee.modules.storage.utils import JSONEncoder
1316

1417
mcp = Server("cognee")
1518

@@ -41,6 +44,19 @@ async def list_tools() -> list[types.Tool]:
4144
"required": ["text"],
4245
},
4346
),
47+
types.Tool(
48+
name="codify",
49+
description="Transforms codebase into knowledge graph",
50+
inputSchema={
51+
"type": "object",
52+
"properties": {
53+
"repo_path": {
54+
"type": "string",
55+
},
56+
},
57+
"required": ["repo_path"],
58+
},
59+
),
4460
types.Tool(
4561
name="search",
4662
description="Searches for information in knowledge graph",
@@ -51,6 +67,10 @@ async def list_tools() -> list[types.Tool]:
5167
"type": "string",
5268
"description": "The query to search for",
5369
},
70+
"search_type": {
71+
"type": "string",
72+
"description": "The type of search to perform (e.g., INSIGHTS, CODE)",
73+
},
5474
},
5575
"required": ["search_query"],
5676
},
@@ -72,15 +92,21 @@ async def call_tools(name: str, arguments: dict) -> list[types.TextContent]:
7292
with open(os.devnull, "w") as fnull:
7393
with redirect_stdout(fnull), redirect_stderr(fnull):
7494
if name == "cognify":
75-
await cognify(
95+
cognify(
7696
text=arguments["text"],
7797
graph_model_file=arguments.get("graph_model_file", None),
7898
graph_model_name=arguments.get("graph_model_name", None),
7999
)
80100

81101
return [types.TextContent(type="text", text="Ingested")]
102+
if name == "codify":
103+
await codify(arguments.get("repo_path"))
104+
105+
return [types.TextContent(type="text", text="Indexed")]
82106
elif name == "search":
83-
search_results = await search(arguments["search_query"])
107+
search_results = await search(
108+
arguments["search_query"], arguments["search_type"]
109+
)
84110

85111
return [types.TextContent(type="text", text=search_results)]
86112
elif name == "prune":
@@ -102,21 +128,30 @@ async def cognify(text: str, graph_model_file: str = None, graph_model_name: str
102128
await cognee.add(text)
103129

104130
try:
105-
await cognee.cognify(graph_model=graph_model)
131+
asyncio.create_task(cognee.cognify(graph_model=graph_model))
106132
except Exception as e:
107133
raise ValueError(f"Failed to cognify: {str(e)}")
108134

109135

110-
async def search(search_query: str) -> str:
111-
"""Search the knowledge graph"""
112-
search_results = await cognee.search(SearchType.INSIGHTS, query_text=search_query)
136+
async def codify(repo_path: str):
137+
async for result in run_code_graph_pipeline(repo_path, False):
138+
print(result)
113139

114-
results = retrieved_edges_to_string(search_results)
115140

116-
return results
141+
async def search(search_query: str, search_type: str) -> str:
142+
"""Search the knowledge graph"""
143+
search_results = await cognee.search(
144+
query_type=SearchType[search_type.upper()], query_text=search_query
145+
)
146+
147+
if search_type.upper() == "CODE":
148+
return json.dumps(search_results, cls=JSONEncoder)
149+
else:
150+
results = retrieved_edges_to_string(search_results)
151+
return results
117152

118153

119-
async def prune() -> str:
154+
async def prune():
120155
"""Reset the knowledge graph"""
121156
await cognee.prune.prune_data()
122157
await cognee.prune.prune_system(metadata=True)

cognee-mcp/uv.lock

Lines changed: 36 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cognee/api/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def start_api_server(host: str = "0.0.0.0", port: int = 8000):
188188
except Exception as e:
189189
logger.exception(f"Failed to start server: {e}")
190190
# Here you could add any cleanup code or error recovery code.
191+
raise e
191192

192193

193194
if __name__ == "__main__":

cognee/api/v1/add/add_v2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,22 @@ async def add(
1616
dataset_name: str = "main_dataset",
1717
user: User = None,
1818
):
19+
# Create tables for databases
1920
await create_relational_db_and_tables()
2021
await create_pgvector_db_and_tables()
2122

23+
# Initialize first_run attribute if it doesn't exist
24+
if not hasattr(add, "first_run"):
25+
add.first_run = True
26+
27+
if add.first_run:
28+
from cognee.infrastructure.llm.utils import test_llm_connection, test_embedding_connection
29+
30+
# Test LLM and Embedding configuration once before running Cognee
31+
await test_llm_connection()
32+
await test_embedding_connection()
33+
add.first_run = False # Update flag after first run
34+
2235
if user is None:
2336
user = await get_default_user()
2437

cognee/api/v1/cognify/routers/get_code_pipeline_router.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
from fastapi import APIRouter
2-
from pydantic import BaseModel
2+
from cognee.api.DTO import InDTO
33
from cognee.api.v1.cognify.code_graph_pipeline import run_code_graph_pipeline
44
from cognee.modules.retrieval.description_to_codepart_search import (
55
code_description_to_code_part_search,
66
)
77
from fastapi.responses import JSONResponse
88

99

10-
class CodePipelineIndexPayloadDTO(BaseModel):
10+
class CodePipelineIndexPayloadDTO(InDTO):
1111
repo_path: str
1212
include_docs: bool = False
1313

1414

15-
class CodePipelineRetrievePayloadDTO(BaseModel):
15+
class CodePipelineRetrievePayloadDTO(InDTO):
1616
query: str
17-
fullInput: str
17+
full_input: str
1818

1919

2020
def get_code_pipeline_router() -> APIRouter:
@@ -34,9 +34,9 @@ async def code_pipeline_retrieve(payload: CodePipelineRetrievePayloadDTO):
3434
"""This endpoint is responsible for retrieving the context."""
3535
try:
3636
query = (
37-
payload.fullInput.replace("cognee ", "")
38-
if payload.fullInput.startswith("cognee ")
39-
else payload.fullInput
37+
payload.full_input.replace("cognee ", "")
38+
if payload.full_input.startswith("cognee ")
39+
else payload.full_input
4040
)
4141

4242
retrieved_codeparts, __ = await code_description_to_code_part_search(
@@ -45,8 +45,8 @@ async def code_pipeline_retrieve(payload: CodePipelineRetrievePayloadDTO):
4545

4646
return [
4747
{
48-
"name": codepart.attributes["id"],
49-
"description": codepart.attributes["id"],
48+
"name": codepart.attributes["file_path"],
49+
"description": codepart.attributes["file_path"],
5050
"content": codepart.attributes["source_code"],
5151
}
5252
for codepart in retrieved_codeparts

cognee/api/v1/search/routers/get_search_router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import datetime
33
from fastapi import Depends, APIRouter
44
from fastapi.responses import JSONResponse
5-
from cognee.api.v1.search import SearchType
5+
from cognee.modules.search.types import SearchType
66
from cognee.api.DTO import InDTO, OutDTO
77
from cognee.modules.users.models import User
88
from cognee.modules.search.operations import get_history

0 commit comments

Comments
 (0)