hanzo-mcp 0.5.0__py3-none-any.whl → 0.5.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of hanzo-mcp might be problematic. Click here for more details.
- hanzo_mcp/__init__.py +1 -1
- hanzo_mcp/config/settings.py +61 -0
- hanzo_mcp/tools/__init__.py +158 -12
- hanzo_mcp/tools/common/base.py +7 -2
- hanzo_mcp/tools/common/config_tool.py +396 -0
- hanzo_mcp/tools/common/stats.py +261 -0
- hanzo_mcp/tools/common/tool_disable.py +144 -0
- hanzo_mcp/tools/common/tool_enable.py +182 -0
- hanzo_mcp/tools/common/tool_list.py +263 -0
- hanzo_mcp/tools/database/__init__.py +71 -0
- hanzo_mcp/tools/database/database_manager.py +246 -0
- hanzo_mcp/tools/database/graph_add.py +257 -0
- hanzo_mcp/tools/database/graph_query.py +536 -0
- hanzo_mcp/tools/database/graph_remove.py +267 -0
- hanzo_mcp/tools/database/graph_search.py +348 -0
- hanzo_mcp/tools/database/graph_stats.py +345 -0
- hanzo_mcp/tools/database/sql_query.py +229 -0
- hanzo_mcp/tools/database/sql_search.py +296 -0
- hanzo_mcp/tools/database/sql_stats.py +254 -0
- hanzo_mcp/tools/editor/__init__.py +11 -0
- hanzo_mcp/tools/editor/neovim_command.py +272 -0
- hanzo_mcp/tools/editor/neovim_edit.py +290 -0
- hanzo_mcp/tools/editor/neovim_session.py +356 -0
- hanzo_mcp/tools/filesystem/__init__.py +20 -1
- hanzo_mcp/tools/filesystem/batch_search.py +812 -0
- hanzo_mcp/tools/filesystem/find_files.py +348 -0
- hanzo_mcp/tools/filesystem/git_search.py +505 -0
- hanzo_mcp/tools/llm/__init__.py +27 -0
- hanzo_mcp/tools/llm/consensus_tool.py +351 -0
- hanzo_mcp/tools/llm/llm_manage.py +413 -0
- hanzo_mcp/tools/llm/llm_tool.py +346 -0
- hanzo_mcp/tools/llm/provider_tools.py +412 -0
- hanzo_mcp/tools/mcp/__init__.py +11 -0
- hanzo_mcp/tools/mcp/mcp_add.py +263 -0
- hanzo_mcp/tools/mcp/mcp_remove.py +127 -0
- hanzo_mcp/tools/mcp/mcp_stats.py +165 -0
- hanzo_mcp/tools/shell/__init__.py +27 -7
- hanzo_mcp/tools/shell/logs.py +265 -0
- hanzo_mcp/tools/shell/npx.py +194 -0
- hanzo_mcp/tools/shell/npx_background.py +254 -0
- hanzo_mcp/tools/shell/pkill.py +262 -0
- hanzo_mcp/tools/shell/processes.py +279 -0
- hanzo_mcp/tools/shell/run_background.py +326 -0
- hanzo_mcp/tools/shell/uvx.py +187 -0
- hanzo_mcp/tools/shell/uvx_background.py +249 -0
- hanzo_mcp/tools/vector/__init__.py +21 -12
- hanzo_mcp/tools/vector/ast_analyzer.py +459 -0
- hanzo_mcp/tools/vector/git_ingester.py +485 -0
- hanzo_mcp/tools/vector/index_tool.py +358 -0
- hanzo_mcp/tools/vector/infinity_store.py +465 -1
- hanzo_mcp/tools/vector/mock_infinity.py +162 -0
- hanzo_mcp/tools/vector/vector_index.py +7 -6
- hanzo_mcp/tools/vector/vector_search.py +22 -7
- {hanzo_mcp-0.5.0.dist-info → hanzo_mcp-0.5.2.dist-info}/METADATA +68 -20
- hanzo_mcp-0.5.2.dist-info/RECORD +106 -0
- hanzo_mcp-0.5.0.dist-info/RECORD +0 -63
- {hanzo_mcp-0.5.0.dist-info → hanzo_mcp-0.5.2.dist-info}/WHEEL +0 -0
- {hanzo_mcp-0.5.0.dist-info → hanzo_mcp-0.5.2.dist-info}/entry_points.txt +0 -0
- {hanzo_mcp-0.5.0.dist-info → hanzo_mcp-0.5.2.dist-info}/licenses/LICENSE +0 -0
- {hanzo_mcp-0.5.0.dist-info → hanzo_mcp-0.5.2.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"""Database manager for project-specific SQLite and graph databases."""
|
|
2
|
+
|
|
3
|
+
import sqlite3
|
|
4
|
+
import os
|
|
5
|
+
import json
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Dict, Any, Optional, Tuple, List
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
|
|
10
|
+
from hanzo_mcp.tools.common.permissions import PermissionManager
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ProjectDatabase:
|
|
14
|
+
"""Manages SQLite and graph databases for a project."""
|
|
15
|
+
|
|
16
|
+
def __init__(self, project_path: str):
|
|
17
|
+
self.project_path = Path(project_path)
|
|
18
|
+
self.db_dir = self.project_path / ".hanzo" / "db"
|
|
19
|
+
self.db_dir.mkdir(parents=True, exist_ok=True)
|
|
20
|
+
|
|
21
|
+
# SQLite database path
|
|
22
|
+
self.sqlite_path = self.db_dir / "project.db"
|
|
23
|
+
self.graph_path = self.db_dir / "graph.db"
|
|
24
|
+
|
|
25
|
+
# Initialize databases
|
|
26
|
+
self._init_sqlite()
|
|
27
|
+
self._init_graph()
|
|
28
|
+
|
|
29
|
+
# Keep graph in memory for performance
|
|
30
|
+
self.graph_conn = sqlite3.connect(":memory:")
|
|
31
|
+
self._init_graph_schema(self.graph_conn)
|
|
32
|
+
self._load_graph_from_disk()
|
|
33
|
+
|
|
34
|
+
def _init_sqlite(self):
|
|
35
|
+
"""Initialize SQLite database with common tables."""
|
|
36
|
+
conn = sqlite3.connect(self.sqlite_path)
|
|
37
|
+
try:
|
|
38
|
+
# Create metadata table
|
|
39
|
+
conn.execute('''
|
|
40
|
+
CREATE TABLE IF NOT EXISTS metadata (
|
|
41
|
+
key TEXT PRIMARY KEY,
|
|
42
|
+
value TEXT,
|
|
43
|
+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
44
|
+
)
|
|
45
|
+
''')
|
|
46
|
+
|
|
47
|
+
# Create files table
|
|
48
|
+
conn.execute('''
|
|
49
|
+
CREATE TABLE IF NOT EXISTS files (
|
|
50
|
+
path TEXT PRIMARY KEY,
|
|
51
|
+
content TEXT,
|
|
52
|
+
size INTEGER,
|
|
53
|
+
modified_at TIMESTAMP,
|
|
54
|
+
hash TEXT,
|
|
55
|
+
metadata TEXT
|
|
56
|
+
)
|
|
57
|
+
''')
|
|
58
|
+
|
|
59
|
+
# Create symbols table
|
|
60
|
+
conn.execute('''
|
|
61
|
+
CREATE TABLE IF NOT EXISTS symbols (
|
|
62
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
63
|
+
file_path TEXT,
|
|
64
|
+
name TEXT,
|
|
65
|
+
type TEXT,
|
|
66
|
+
line_start INTEGER,
|
|
67
|
+
line_end INTEGER,
|
|
68
|
+
scope TEXT,
|
|
69
|
+
signature TEXT,
|
|
70
|
+
FOREIGN KEY (file_path) REFERENCES files(path)
|
|
71
|
+
)
|
|
72
|
+
''')
|
|
73
|
+
|
|
74
|
+
# Create index for fast searches
|
|
75
|
+
conn.execute('CREATE INDEX IF NOT EXISTS idx_files_path ON files(path)')
|
|
76
|
+
conn.execute('CREATE INDEX IF NOT EXISTS idx_symbols_name ON symbols(name)')
|
|
77
|
+
conn.execute('CREATE INDEX IF NOT EXISTS idx_symbols_type ON symbols(type)')
|
|
78
|
+
|
|
79
|
+
conn.commit()
|
|
80
|
+
finally:
|
|
81
|
+
conn.close()
|
|
82
|
+
|
|
83
|
+
def _init_graph(self):
|
|
84
|
+
"""Initialize graph database on disk."""
|
|
85
|
+
conn = sqlite3.connect(self.graph_path)
|
|
86
|
+
try:
|
|
87
|
+
self._init_graph_schema(conn)
|
|
88
|
+
conn.commit()
|
|
89
|
+
finally:
|
|
90
|
+
conn.close()
|
|
91
|
+
|
|
92
|
+
def _init_graph_schema(self, conn: sqlite3.Connection):
|
|
93
|
+
"""Initialize graph database schema."""
|
|
94
|
+
# Nodes table
|
|
95
|
+
conn.execute('''
|
|
96
|
+
CREATE TABLE IF NOT EXISTS nodes (
|
|
97
|
+
id TEXT PRIMARY KEY,
|
|
98
|
+
type TEXT NOT NULL,
|
|
99
|
+
properties TEXT,
|
|
100
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
101
|
+
)
|
|
102
|
+
''')
|
|
103
|
+
|
|
104
|
+
# Edges table
|
|
105
|
+
conn.execute('''
|
|
106
|
+
CREATE TABLE IF NOT EXISTS edges (
|
|
107
|
+
source TEXT NOT NULL,
|
|
108
|
+
target TEXT NOT NULL,
|
|
109
|
+
relationship TEXT NOT NULL,
|
|
110
|
+
weight REAL DEFAULT 1.0,
|
|
111
|
+
properties TEXT,
|
|
112
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
113
|
+
PRIMARY KEY (source, target, relationship),
|
|
114
|
+
FOREIGN KEY (source) REFERENCES nodes(id),
|
|
115
|
+
FOREIGN KEY (target) REFERENCES nodes(id)
|
|
116
|
+
)
|
|
117
|
+
''')
|
|
118
|
+
|
|
119
|
+
# Indexes for graph traversal
|
|
120
|
+
conn.execute('CREATE INDEX IF NOT EXISTS idx_edges_source ON edges(source)')
|
|
121
|
+
conn.execute('CREATE INDEX IF NOT EXISTS idx_edges_target ON edges(target)')
|
|
122
|
+
conn.execute('CREATE INDEX IF NOT EXISTS idx_edges_relationship ON edges(relationship)')
|
|
123
|
+
conn.execute('CREATE INDEX IF NOT EXISTS idx_nodes_type ON nodes(type)')
|
|
124
|
+
|
|
125
|
+
def _load_graph_from_disk(self):
|
|
126
|
+
"""Load graph from disk into memory."""
|
|
127
|
+
disk_conn = sqlite3.connect(self.graph_path)
|
|
128
|
+
try:
|
|
129
|
+
# Copy nodes
|
|
130
|
+
nodes = disk_conn.execute('SELECT * FROM nodes').fetchall()
|
|
131
|
+
self.graph_conn.executemany(
|
|
132
|
+
'INSERT OR REPLACE INTO nodes VALUES (?, ?, ?, ?)',
|
|
133
|
+
nodes
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
# Copy edges
|
|
137
|
+
edges = disk_conn.execute('SELECT * FROM edges').fetchall()
|
|
138
|
+
self.graph_conn.executemany(
|
|
139
|
+
'INSERT OR REPLACE INTO edges VALUES (?, ?, ?, ?, ?, ?)',
|
|
140
|
+
edges
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
self.graph_conn.commit()
|
|
144
|
+
finally:
|
|
145
|
+
disk_conn.close()
|
|
146
|
+
|
|
147
|
+
def _save_graph_to_disk(self):
|
|
148
|
+
"""Save in-memory graph to disk."""
|
|
149
|
+
disk_conn = sqlite3.connect(self.graph_path)
|
|
150
|
+
try:
|
|
151
|
+
# Clear existing data
|
|
152
|
+
disk_conn.execute('DELETE FROM edges')
|
|
153
|
+
disk_conn.execute('DELETE FROM nodes')
|
|
154
|
+
|
|
155
|
+
# Copy nodes
|
|
156
|
+
nodes = self.graph_conn.execute('SELECT * FROM nodes').fetchall()
|
|
157
|
+
disk_conn.executemany(
|
|
158
|
+
'INSERT INTO nodes VALUES (?, ?, ?, ?)',
|
|
159
|
+
nodes
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
# Copy edges
|
|
163
|
+
edges = self.graph_conn.execute('SELECT * FROM edges').fetchall()
|
|
164
|
+
disk_conn.executemany(
|
|
165
|
+
'INSERT INTO edges VALUES (?, ?, ?, ?, ?, ?)',
|
|
166
|
+
edges
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
disk_conn.commit()
|
|
170
|
+
finally:
|
|
171
|
+
disk_conn.close()
|
|
172
|
+
|
|
173
|
+
def get_sqlite_connection(self) -> sqlite3.Connection:
|
|
174
|
+
"""Get SQLite connection."""
|
|
175
|
+
conn = sqlite3.connect(self.sqlite_path)
|
|
176
|
+
conn.row_factory = sqlite3.Row
|
|
177
|
+
return conn
|
|
178
|
+
|
|
179
|
+
def get_graph_connection(self) -> sqlite3.Connection:
|
|
180
|
+
"""Get in-memory graph connection."""
|
|
181
|
+
return self.graph_conn
|
|
182
|
+
|
|
183
|
+
def close(self):
|
|
184
|
+
"""Close connections and save graph to disk."""
|
|
185
|
+
self._save_graph_to_disk()
|
|
186
|
+
self.graph_conn.close()
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class DatabaseManager:
|
|
190
|
+
"""Manages databases for multiple projects."""
|
|
191
|
+
|
|
192
|
+
def __init__(self, permission_manager: PermissionManager):
|
|
193
|
+
self.permission_manager = permission_manager
|
|
194
|
+
self.projects: Dict[str, ProjectDatabase] = {}
|
|
195
|
+
self.search_paths: List[str] = []
|
|
196
|
+
|
|
197
|
+
def add_search_path(self, path: str):
|
|
198
|
+
"""Add a path to search for projects."""
|
|
199
|
+
if path not in self.search_paths:
|
|
200
|
+
self.search_paths.append(path)
|
|
201
|
+
|
|
202
|
+
def get_project_db(self, project_path: str) -> ProjectDatabase:
|
|
203
|
+
"""Get or create database for a project."""
|
|
204
|
+
project_path = os.path.abspath(project_path)
|
|
205
|
+
|
|
206
|
+
# Check permissions
|
|
207
|
+
if not self.permission_manager.has_permission(project_path):
|
|
208
|
+
raise PermissionError(f"No permission to access: {project_path}")
|
|
209
|
+
|
|
210
|
+
# Create database if not exists
|
|
211
|
+
if project_path not in self.projects:
|
|
212
|
+
self.projects[project_path] = ProjectDatabase(project_path)
|
|
213
|
+
|
|
214
|
+
return self.projects[project_path]
|
|
215
|
+
|
|
216
|
+
def get_project_for_path(self, file_path: str) -> Optional[ProjectDatabase]:
|
|
217
|
+
"""Find the project database for a given file path."""
|
|
218
|
+
file_path = os.path.abspath(file_path)
|
|
219
|
+
|
|
220
|
+
# Check if file is in a known project
|
|
221
|
+
for project_path in self.projects:
|
|
222
|
+
if file_path.startswith(project_path):
|
|
223
|
+
return self.projects[project_path]
|
|
224
|
+
|
|
225
|
+
# Search up the directory tree for a project
|
|
226
|
+
current = Path(file_path)
|
|
227
|
+
if current.is_file():
|
|
228
|
+
current = current.parent
|
|
229
|
+
|
|
230
|
+
while current != current.parent:
|
|
231
|
+
# Check for project markers
|
|
232
|
+
if (current / ".git").exists() or (current / "LLM.md").exists():
|
|
233
|
+
return self.get_project_db(str(current))
|
|
234
|
+
current = current.parent
|
|
235
|
+
|
|
236
|
+
# No project found, use the directory of the file
|
|
237
|
+
if Path(file_path).is_file():
|
|
238
|
+
return self.get_project_db(str(Path(file_path).parent))
|
|
239
|
+
else:
|
|
240
|
+
return self.get_project_db(file_path)
|
|
241
|
+
|
|
242
|
+
def close_all(self):
|
|
243
|
+
"""Close all project databases."""
|
|
244
|
+
for db in self.projects.values():
|
|
245
|
+
db.close()
|
|
246
|
+
self.projects.clear()
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"""Graph add tool for adding nodes and edges to the graph database."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from typing import Annotated, Optional, TypedDict, Unpack, final, override
|
|
5
|
+
|
|
6
|
+
from fastmcp import Context as MCPContext
|
|
7
|
+
from pydantic import Field
|
|
8
|
+
|
|
9
|
+
from hanzo_mcp.tools.common.base import BaseTool
|
|
10
|
+
from hanzo_mcp.tools.common.context import create_tool_context
|
|
11
|
+
from hanzo_mcp.tools.common.permissions import PermissionManager
|
|
12
|
+
from hanzo_mcp.tools.database.database_manager import DatabaseManager
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
NodeId = Annotated[
|
|
16
|
+
Optional[str],
|
|
17
|
+
Field(
|
|
18
|
+
description="Node ID to add (required for nodes)",
|
|
19
|
+
default=None,
|
|
20
|
+
),
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
NodeType = Annotated[
|
|
24
|
+
Optional[str],
|
|
25
|
+
Field(
|
|
26
|
+
description="Node type (e.g., 'file', 'function', 'class')",
|
|
27
|
+
default=None,
|
|
28
|
+
),
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
Properties = Annotated[
|
|
32
|
+
Optional[dict],
|
|
33
|
+
Field(
|
|
34
|
+
description="Properties as JSON object",
|
|
35
|
+
default=None,
|
|
36
|
+
),
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
Source = Annotated[
|
|
40
|
+
Optional[str],
|
|
41
|
+
Field(
|
|
42
|
+
description="Source node ID (required for edges)",
|
|
43
|
+
default=None,
|
|
44
|
+
),
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
Target = Annotated[
|
|
48
|
+
Optional[str],
|
|
49
|
+
Field(
|
|
50
|
+
description="Target node ID (required for edges)",
|
|
51
|
+
default=None,
|
|
52
|
+
),
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
Relationship = Annotated[
|
|
56
|
+
Optional[str],
|
|
57
|
+
Field(
|
|
58
|
+
description="Edge relationship type (e.g., 'imports', 'calls', 'inherits')",
|
|
59
|
+
default=None,
|
|
60
|
+
),
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
Weight = Annotated[
|
|
64
|
+
float,
|
|
65
|
+
Field(
|
|
66
|
+
description="Edge weight (default 1.0)",
|
|
67
|
+
default=1.0,
|
|
68
|
+
),
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
ProjectPath = Annotated[
|
|
72
|
+
Optional[str],
|
|
73
|
+
Field(
|
|
74
|
+
description="Project path (defaults to current directory)",
|
|
75
|
+
default=None,
|
|
76
|
+
),
|
|
77
|
+
]
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class GraphAddParams(TypedDict, total=False):
|
|
81
|
+
"""Parameters for graph add tool."""
|
|
82
|
+
|
|
83
|
+
node_id: Optional[str]
|
|
84
|
+
node_type: Optional[str]
|
|
85
|
+
properties: Optional[dict]
|
|
86
|
+
source: Optional[str]
|
|
87
|
+
target: Optional[str]
|
|
88
|
+
relationship: Optional[str]
|
|
89
|
+
weight: float
|
|
90
|
+
project_path: Optional[str]
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@final
|
|
94
|
+
class GraphAddTool(BaseTool):
|
|
95
|
+
"""Tool for adding nodes and edges to graph database."""
|
|
96
|
+
|
|
97
|
+
def __init__(self, permission_manager: PermissionManager, db_manager: DatabaseManager):
|
|
98
|
+
"""Initialize the graph add tool.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
permission_manager: Permission manager for access control
|
|
102
|
+
db_manager: Database manager instance
|
|
103
|
+
"""
|
|
104
|
+
self.permission_manager = permission_manager
|
|
105
|
+
self.db_manager = db_manager
|
|
106
|
+
|
|
107
|
+
@property
|
|
108
|
+
@override
|
|
109
|
+
def name(self) -> str:
|
|
110
|
+
"""Get the tool name."""
|
|
111
|
+
return "graph_add"
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
@override
|
|
115
|
+
def description(self) -> str:
|
|
116
|
+
"""Get the tool description."""
|
|
117
|
+
return """Add nodes and edges to the project's graph database.
|
|
118
|
+
|
|
119
|
+
To add a node:
|
|
120
|
+
- Provide node_id and node_type
|
|
121
|
+
- Optionally add properties as JSON
|
|
122
|
+
|
|
123
|
+
To add an edge:
|
|
124
|
+
- Provide source, target, and relationship
|
|
125
|
+
- Optionally add weight and properties
|
|
126
|
+
|
|
127
|
+
Common node types:
|
|
128
|
+
- file, function, class, module, variable
|
|
129
|
+
|
|
130
|
+
Common relationships:
|
|
131
|
+
- imports, calls, inherits, contains, references, depends_on
|
|
132
|
+
|
|
133
|
+
Examples:
|
|
134
|
+
- graph_add --node-id "main.py" --node-type "file" --properties '{"size": 1024}'
|
|
135
|
+
- graph_add --node-id "MyClass" --node-type "class" --properties '{"file": "main.py"}'
|
|
136
|
+
- graph_add --source "main.py" --target "utils.py" --relationship "imports"
|
|
137
|
+
- graph_add --source "func1" --target "func2" --relationship "calls" --weight 5.0
|
|
138
|
+
"""
|
|
139
|
+
|
|
140
|
+
@override
|
|
141
|
+
async def call(
|
|
142
|
+
self,
|
|
143
|
+
ctx: MCPContext,
|
|
144
|
+
**params: Unpack[GraphAddParams],
|
|
145
|
+
) -> str:
|
|
146
|
+
"""Add nodes or edges to graph.
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
ctx: MCP context
|
|
150
|
+
**params: Tool parameters
|
|
151
|
+
|
|
152
|
+
Returns:
|
|
153
|
+
Result of add operation
|
|
154
|
+
"""
|
|
155
|
+
tool_ctx = create_tool_context(ctx)
|
|
156
|
+
await tool_ctx.set_tool_info(self.name)
|
|
157
|
+
|
|
158
|
+
# Extract parameters
|
|
159
|
+
node_id = params.get("node_id")
|
|
160
|
+
node_type = params.get("node_type")
|
|
161
|
+
properties = params.get("properties", {})
|
|
162
|
+
source = params.get("source")
|
|
163
|
+
target = params.get("target")
|
|
164
|
+
relationship = params.get("relationship")
|
|
165
|
+
weight = params.get("weight", 1.0)
|
|
166
|
+
project_path = params.get("project_path")
|
|
167
|
+
|
|
168
|
+
# Determine if adding node or edge
|
|
169
|
+
is_node = node_id is not None
|
|
170
|
+
is_edge = source is not None and target is not None
|
|
171
|
+
|
|
172
|
+
if not is_node and not is_edge:
|
|
173
|
+
return "Error: Must provide either (node_id and node_type) for a node, or (source, target, relationship) for an edge"
|
|
174
|
+
|
|
175
|
+
if is_node and is_edge:
|
|
176
|
+
return "Error: Cannot add both node and edge in one operation"
|
|
177
|
+
|
|
178
|
+
# Get project database
|
|
179
|
+
try:
|
|
180
|
+
if project_path:
|
|
181
|
+
project_db = self.db_manager.get_project_db(project_path)
|
|
182
|
+
else:
|
|
183
|
+
import os
|
|
184
|
+
project_db = self.db_manager.get_project_for_path(os.getcwd())
|
|
185
|
+
|
|
186
|
+
if not project_db:
|
|
187
|
+
return "Error: Could not find project database"
|
|
188
|
+
|
|
189
|
+
except PermissionError as e:
|
|
190
|
+
return str(e)
|
|
191
|
+
except Exception as e:
|
|
192
|
+
return f"Error accessing project database: {str(e)}"
|
|
193
|
+
|
|
194
|
+
# Get graph connection
|
|
195
|
+
graph_conn = project_db.get_graph_connection()
|
|
196
|
+
|
|
197
|
+
try:
|
|
198
|
+
if is_node:
|
|
199
|
+
# Add node
|
|
200
|
+
if not node_type:
|
|
201
|
+
return "Error: node_type is required when adding a node"
|
|
202
|
+
|
|
203
|
+
await tool_ctx.info(f"Adding node: {node_id} (type: {node_type})")
|
|
204
|
+
|
|
205
|
+
# Serialize properties
|
|
206
|
+
properties_json = json.dumps(properties) if properties else None
|
|
207
|
+
|
|
208
|
+
# Insert or update node
|
|
209
|
+
graph_conn.execute("""
|
|
210
|
+
INSERT OR REPLACE INTO nodes (id, type, properties)
|
|
211
|
+
VALUES (?, ?, ?)
|
|
212
|
+
""", (node_id, node_type, properties_json))
|
|
213
|
+
|
|
214
|
+
graph_conn.commit()
|
|
215
|
+
|
|
216
|
+
return f"Successfully added node '{node_id}' of type '{node_type}'"
|
|
217
|
+
|
|
218
|
+
else:
|
|
219
|
+
# Add edge
|
|
220
|
+
if not relationship:
|
|
221
|
+
return "Error: relationship is required when adding an edge"
|
|
222
|
+
|
|
223
|
+
await tool_ctx.info(f"Adding edge: {source} --[{relationship}]--> {target}")
|
|
224
|
+
|
|
225
|
+
# Check if nodes exist
|
|
226
|
+
cursor = graph_conn.cursor()
|
|
227
|
+
cursor.execute("SELECT id FROM nodes WHERE id IN (?, ?)", (source, target))
|
|
228
|
+
existing = [row[0] for row in cursor.fetchall()]
|
|
229
|
+
|
|
230
|
+
if source not in existing:
|
|
231
|
+
return f"Error: Source node '{source}' does not exist"
|
|
232
|
+
if target not in existing:
|
|
233
|
+
return f"Error: Target node '{target}' does not exist"
|
|
234
|
+
|
|
235
|
+
# Serialize properties
|
|
236
|
+
properties_json = json.dumps(properties) if properties else None
|
|
237
|
+
|
|
238
|
+
# Insert or update edge
|
|
239
|
+
graph_conn.execute("""
|
|
240
|
+
INSERT OR REPLACE INTO edges (source, target, relationship, weight, properties)
|
|
241
|
+
VALUES (?, ?, ?, ?, ?)
|
|
242
|
+
""", (source, target, relationship, weight, properties_json))
|
|
243
|
+
|
|
244
|
+
graph_conn.commit()
|
|
245
|
+
|
|
246
|
+
# Save to disk
|
|
247
|
+
project_db._save_graph_to_disk()
|
|
248
|
+
|
|
249
|
+
return f"Successfully added edge: {source} --[{relationship}]--> {target} (weight: {weight})"
|
|
250
|
+
|
|
251
|
+
except Exception as e:
|
|
252
|
+
await tool_ctx.error(f"Failed to add to graph: {str(e)}")
|
|
253
|
+
return f"Error adding to graph: {str(e)}"
|
|
254
|
+
|
|
255
|
+
def register(self, mcp_server) -> None:
|
|
256
|
+
"""Register this tool with the MCP server."""
|
|
257
|
+
pass
|