hanzo-mcp 0.8.11__py3-none-any.whl → 0.9.0__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 -3
- hanzo_mcp/analytics/posthog_analytics.py +3 -9
- hanzo_mcp/bridge.py +9 -25
- hanzo_mcp/cli.py +6 -15
- hanzo_mcp/cli_enhanced.py +5 -14
- hanzo_mcp/cli_plugin.py +3 -9
- hanzo_mcp/config/settings.py +6 -20
- hanzo_mcp/config/tool_config.py +1 -3
- hanzo_mcp/core/base_agent.py +88 -88
- hanzo_mcp/core/model_registry.py +238 -210
- hanzo_mcp/dev_server.py +5 -15
- hanzo_mcp/prompts/__init__.py +2 -6
- hanzo_mcp/prompts/project_todo_reminder.py +3 -9
- hanzo_mcp/prompts/tool_explorer.py +1 -3
- hanzo_mcp/prompts/utils.py +7 -21
- hanzo_mcp/server.py +2 -6
- hanzo_mcp/tools/__init__.py +26 -27
- hanzo_mcp/tools/agent/__init__.py +2 -1
- hanzo_mcp/tools/agent/agent.py +10 -30
- hanzo_mcp/tools/agent/agent_tool.py +22 -15
- hanzo_mcp/tools/agent/claude_desktop_auth.py +3 -9
- hanzo_mcp/tools/agent/cli_agent_base.py +7 -24
- hanzo_mcp/tools/agent/cli_tools.py +75 -74
- hanzo_mcp/tools/agent/code_auth.py +1 -3
- hanzo_mcp/tools/agent/code_auth_tool.py +2 -6
- hanzo_mcp/tools/agent/critic_tool.py +8 -24
- hanzo_mcp/tools/agent/iching_tool.py +12 -36
- hanzo_mcp/tools/agent/network_tool.py +7 -18
- hanzo_mcp/tools/agent/prompt.py +1 -5
- hanzo_mcp/tools/agent/review_tool.py +10 -25
- hanzo_mcp/tools/agent/swarm_alias.py +1 -3
- hanzo_mcp/tools/agent/unified_cli_tools.py +38 -38
- hanzo_mcp/tools/common/batch_tool.py +15 -45
- hanzo_mcp/tools/common/config_tool.py +9 -28
- hanzo_mcp/tools/common/context.py +1 -3
- hanzo_mcp/tools/common/critic_tool.py +1 -3
- hanzo_mcp/tools/common/decorators.py +2 -6
- hanzo_mcp/tools/common/enhanced_base.py +2 -6
- hanzo_mcp/tools/common/fastmcp_pagination.py +4 -12
- hanzo_mcp/tools/common/forgiving_edit.py +9 -28
- hanzo_mcp/tools/common/mode.py +1 -5
- hanzo_mcp/tools/common/paginated_base.py +3 -11
- hanzo_mcp/tools/common/paginated_response.py +10 -30
- hanzo_mcp/tools/common/pagination.py +3 -9
- hanzo_mcp/tools/common/path_utils.py +34 -0
- hanzo_mcp/tools/common/permissions.py +14 -13
- hanzo_mcp/tools/common/personality.py +983 -701
- hanzo_mcp/tools/common/plugin_loader.py +3 -15
- hanzo_mcp/tools/common/stats.py +6 -18
- hanzo_mcp/tools/common/thinking_tool.py +1 -3
- hanzo_mcp/tools/common/tool_disable.py +2 -6
- hanzo_mcp/tools/common/tool_list.py +2 -6
- hanzo_mcp/tools/common/validation.py +1 -3
- hanzo_mcp/tools/compiler/__init__.py +8 -0
- hanzo_mcp/tools/compiler/sandboxed_compiler.py +681 -0
- hanzo_mcp/tools/config/config_tool.py +7 -13
- hanzo_mcp/tools/config/index_config.py +1 -3
- hanzo_mcp/tools/config/mode_tool.py +5 -15
- hanzo_mcp/tools/database/database_manager.py +3 -9
- hanzo_mcp/tools/database/graph.py +1 -3
- hanzo_mcp/tools/database/graph_add.py +3 -9
- hanzo_mcp/tools/database/graph_query.py +11 -34
- hanzo_mcp/tools/database/graph_remove.py +3 -9
- hanzo_mcp/tools/database/graph_search.py +6 -20
- hanzo_mcp/tools/database/graph_stats.py +11 -33
- hanzo_mcp/tools/database/sql.py +4 -12
- hanzo_mcp/tools/database/sql_query.py +6 -10
- hanzo_mcp/tools/database/sql_search.py +2 -6
- hanzo_mcp/tools/database/sql_stats.py +5 -15
- hanzo_mcp/tools/editor/neovim_command.py +1 -3
- hanzo_mcp/tools/editor/neovim_session.py +7 -13
- hanzo_mcp/tools/environment/__init__.py +8 -0
- hanzo_mcp/tools/environment/environment_detector.py +594 -0
- hanzo_mcp/tools/filesystem/__init__.py +28 -26
- hanzo_mcp/tools/filesystem/ast_multi_edit.py +14 -43
- hanzo_mcp/tools/filesystem/ast_tool.py +3 -0
- hanzo_mcp/tools/filesystem/base.py +20 -12
- hanzo_mcp/tools/filesystem/content_replace.py +7 -12
- hanzo_mcp/tools/filesystem/diff.py +2 -10
- hanzo_mcp/tools/filesystem/directory_tree.py +285 -51
- hanzo_mcp/tools/filesystem/edit.py +10 -18
- hanzo_mcp/tools/filesystem/find.py +312 -179
- hanzo_mcp/tools/filesystem/git_search.py +12 -24
- hanzo_mcp/tools/filesystem/multi_edit.py +10 -18
- hanzo_mcp/tools/filesystem/read.py +14 -30
- hanzo_mcp/tools/filesystem/rules_tool.py +9 -17
- hanzo_mcp/tools/filesystem/search.py +1160 -0
- hanzo_mcp/tools/filesystem/watch.py +2 -4
- hanzo_mcp/tools/filesystem/write.py +7 -10
- hanzo_mcp/tools/framework/__init__.py +8 -0
- hanzo_mcp/tools/framework/framework_modes.py +714 -0
- hanzo_mcp/tools/jupyter/base.py +6 -20
- hanzo_mcp/tools/jupyter/jupyter.py +4 -12
- hanzo_mcp/tools/llm/consensus_tool.py +8 -24
- hanzo_mcp/tools/llm/llm_manage.py +2 -6
- hanzo_mcp/tools/llm/llm_tool.py +17 -58
- hanzo_mcp/tools/llm/llm_unified.py +18 -59
- hanzo_mcp/tools/llm/provider_tools.py +1 -3
- hanzo_mcp/tools/lsp/lsp_tool.py +621 -481
- hanzo_mcp/tools/mcp/mcp_add.py +1 -3
- hanzo_mcp/tools/mcp/mcp_stats.py +1 -3
- hanzo_mcp/tools/mcp/mcp_tool.py +9 -23
- hanzo_mcp/tools/memory/__init__.py +10 -27
- hanzo_mcp/tools/memory/conversation_memory.py +636 -0
- hanzo_mcp/tools/memory/knowledge_tools.py +7 -25
- hanzo_mcp/tools/memory/memory_tools.py +6 -18
- hanzo_mcp/tools/search/find_tool.py +12 -34
- hanzo_mcp/tools/search/unified_search.py +24 -78
- hanzo_mcp/tools/shell/__init__.py +16 -4
- hanzo_mcp/tools/shell/auto_background.py +2 -6
- hanzo_mcp/tools/shell/base.py +1 -5
- hanzo_mcp/tools/shell/base_process.py +5 -7
- hanzo_mcp/tools/shell/bash_session.py +7 -24
- hanzo_mcp/tools/shell/bash_session_executor.py +5 -15
- hanzo_mcp/tools/shell/bash_tool.py +3 -7
- hanzo_mcp/tools/shell/command_executor.py +26 -79
- hanzo_mcp/tools/shell/logs.py +4 -16
- hanzo_mcp/tools/shell/npx.py +2 -8
- hanzo_mcp/tools/shell/npx_tool.py +1 -3
- hanzo_mcp/tools/shell/pkill.py +4 -12
- hanzo_mcp/tools/shell/process_tool.py +2 -8
- hanzo_mcp/tools/shell/processes.py +5 -17
- hanzo_mcp/tools/shell/run_background.py +1 -3
- hanzo_mcp/tools/shell/run_command.py +1 -3
- hanzo_mcp/tools/shell/run_command_windows.py +1 -3
- hanzo_mcp/tools/shell/run_tool.py +56 -0
- hanzo_mcp/tools/shell/session_manager.py +2 -6
- hanzo_mcp/tools/shell/session_storage.py +2 -6
- hanzo_mcp/tools/shell/streaming_command.py +7 -23
- hanzo_mcp/tools/shell/uvx.py +4 -14
- hanzo_mcp/tools/shell/uvx_background.py +2 -6
- hanzo_mcp/tools/shell/uvx_tool.py +1 -3
- hanzo_mcp/tools/shell/zsh_tool.py +12 -20
- hanzo_mcp/tools/todo/todo.py +1 -3
- hanzo_mcp/tools/vector/__init__.py +97 -50
- hanzo_mcp/tools/vector/ast_analyzer.py +6 -20
- hanzo_mcp/tools/vector/git_ingester.py +10 -30
- hanzo_mcp/tools/vector/index_tool.py +3 -9
- hanzo_mcp/tools/vector/infinity_store.py +7 -27
- hanzo_mcp/tools/vector/mock_infinity.py +1 -3
- hanzo_mcp/tools/vector/node_tool.py +538 -0
- hanzo_mcp/tools/vector/project_manager.py +4 -12
- hanzo_mcp/tools/vector/unified_vector.py +384 -0
- hanzo_mcp/tools/vector/vector.py +2 -6
- hanzo_mcp/tools/vector/vector_index.py +8 -8
- hanzo_mcp/tools/vector/vector_search.py +7 -21
- {hanzo_mcp-0.8.11.dist-info → hanzo_mcp-0.9.0.dist-info}/METADATA +2 -2
- hanzo_mcp-0.9.0.dist-info/RECORD +191 -0
- hanzo_mcp/tools/agent/agent_tool_v1_deprecated.py +0 -645
- hanzo_mcp/tools/agent/swarm_tool.py +0 -718
- hanzo_mcp/tools/agent/swarm_tool_v1_deprecated.py +0 -577
- hanzo_mcp/tools/filesystem/batch_search.py +0 -900
- hanzo_mcp/tools/filesystem/directory_tree_paginated.py +0 -350
- hanzo_mcp/tools/filesystem/find_files.py +0 -369
- hanzo_mcp/tools/filesystem/grep.py +0 -467
- hanzo_mcp/tools/filesystem/search_tool.py +0 -767
- hanzo_mcp/tools/filesystem/symbols_tool.py +0 -515
- hanzo_mcp/tools/filesystem/tree.py +0 -270
- hanzo_mcp/tools/jupyter/notebook_edit.py +0 -317
- hanzo_mcp/tools/jupyter/notebook_read.py +0 -147
- hanzo_mcp/tools/todo/todo_read.py +0 -143
- hanzo_mcp/tools/todo/todo_write.py +0 -374
- hanzo_mcp-0.8.11.dist-info/RECORD +0 -193
- {hanzo_mcp-0.8.11.dist-info → hanzo_mcp-0.9.0.dist-info}/WHEEL +0 -0
- {hanzo_mcp-0.8.11.dist-info → hanzo_mcp-0.9.0.dist-info}/entry_points.txt +0 -0
- {hanzo_mcp-0.8.11.dist-info → hanzo_mcp-0.9.0.dist-info}/top_level.txt +0 -0
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
"""Tree tool implementation.
|
|
2
|
-
|
|
3
|
-
Unix-style tree command for directory visualization.
|
|
4
|
-
"""
|
|
5
|
-
|
|
6
|
-
from typing import List, Unpack, Optional, Annotated, TypedDict, final, override
|
|
7
|
-
from pathlib import Path
|
|
8
|
-
|
|
9
|
-
from pydantic import Field
|
|
10
|
-
from mcp.server.fastmcp import Context as MCPContext
|
|
11
|
-
|
|
12
|
-
from hanzo_mcp.tools.filesystem.base import FilesystemBaseTool
|
|
13
|
-
|
|
14
|
-
# Parameter types
|
|
15
|
-
TreePath = Annotated[
|
|
16
|
-
str,
|
|
17
|
-
Field(
|
|
18
|
-
description="Directory path to display",
|
|
19
|
-
default=".",
|
|
20
|
-
),
|
|
21
|
-
]
|
|
22
|
-
|
|
23
|
-
Depth = Annotated[
|
|
24
|
-
Optional[int],
|
|
25
|
-
Field(
|
|
26
|
-
description="Maximum depth to display",
|
|
27
|
-
default=None,
|
|
28
|
-
),
|
|
29
|
-
]
|
|
30
|
-
|
|
31
|
-
ShowHidden = Annotated[
|
|
32
|
-
bool,
|
|
33
|
-
Field(
|
|
34
|
-
description="Show hidden files (starting with .)",
|
|
35
|
-
default=False,
|
|
36
|
-
),
|
|
37
|
-
]
|
|
38
|
-
|
|
39
|
-
DirsOnly = Annotated[
|
|
40
|
-
bool,
|
|
41
|
-
Field(
|
|
42
|
-
description="Show only directories",
|
|
43
|
-
default=False,
|
|
44
|
-
),
|
|
45
|
-
]
|
|
46
|
-
|
|
47
|
-
ShowSize = Annotated[
|
|
48
|
-
bool,
|
|
49
|
-
Field(
|
|
50
|
-
description="Show file sizes",
|
|
51
|
-
default=False,
|
|
52
|
-
),
|
|
53
|
-
]
|
|
54
|
-
|
|
55
|
-
Pattern = Annotated[
|
|
56
|
-
Optional[str],
|
|
57
|
-
Field(
|
|
58
|
-
description="Only show files matching pattern",
|
|
59
|
-
default=None,
|
|
60
|
-
),
|
|
61
|
-
]
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
class TreeParams(TypedDict, total=False):
|
|
65
|
-
"""Parameters for tree tool."""
|
|
66
|
-
|
|
67
|
-
path: str
|
|
68
|
-
depth: Optional[int]
|
|
69
|
-
show_hidden: bool
|
|
70
|
-
dirs_only: bool
|
|
71
|
-
show_size: bool
|
|
72
|
-
pattern: Optional[str]
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
@final
|
|
76
|
-
class TreeTool(FilesystemBaseTool):
|
|
77
|
-
"""Unix-style tree command for directory visualization."""
|
|
78
|
-
|
|
79
|
-
@property
|
|
80
|
-
@override
|
|
81
|
-
def name(self) -> str:
|
|
82
|
-
"""Get the tool name."""
|
|
83
|
-
return "tree"
|
|
84
|
-
|
|
85
|
-
@property
|
|
86
|
-
@override
|
|
87
|
-
def description(self) -> str:
|
|
88
|
-
"""Get the tool description."""
|
|
89
|
-
return """Directory tree visualization.
|
|
90
|
-
|
|
91
|
-
Usage:
|
|
92
|
-
tree
|
|
93
|
-
tree ./src --depth 2
|
|
94
|
-
tree --dirs-only
|
|
95
|
-
tree --pattern "*.py" --show-size"""
|
|
96
|
-
|
|
97
|
-
@override
|
|
98
|
-
async def call(
|
|
99
|
-
self,
|
|
100
|
-
ctx: MCPContext,
|
|
101
|
-
**params: Unpack[TreeParams],
|
|
102
|
-
) -> str:
|
|
103
|
-
"""Execute tree command."""
|
|
104
|
-
tool_ctx = self.create_tool_context(ctx)
|
|
105
|
-
|
|
106
|
-
# Extract parameters
|
|
107
|
-
path = params.get("path", ".")
|
|
108
|
-
max_depth = params.get("depth")
|
|
109
|
-
show_hidden = params.get("show_hidden", False)
|
|
110
|
-
dirs_only = params.get("dirs_only", False)
|
|
111
|
-
show_size = params.get("show_size", False)
|
|
112
|
-
pattern = params.get("pattern")
|
|
113
|
-
|
|
114
|
-
# Validate path
|
|
115
|
-
path_validation = self.validate_path(path)
|
|
116
|
-
if path_validation.is_error:
|
|
117
|
-
return f"Error: {path_validation.error_message}"
|
|
118
|
-
|
|
119
|
-
# Check permissions
|
|
120
|
-
allowed, error_msg = await self.check_path_allowed(path, tool_ctx)
|
|
121
|
-
if not allowed:
|
|
122
|
-
return error_msg
|
|
123
|
-
|
|
124
|
-
# Check existence
|
|
125
|
-
exists, error_msg = await self.check_path_exists(path, tool_ctx)
|
|
126
|
-
if not exists:
|
|
127
|
-
return error_msg
|
|
128
|
-
|
|
129
|
-
path_obj = Path(path)
|
|
130
|
-
if not path_obj.is_dir():
|
|
131
|
-
return f"Error: {path} is not a directory"
|
|
132
|
-
|
|
133
|
-
# Build tree
|
|
134
|
-
output = [str(path_obj)]
|
|
135
|
-
stats = {"dirs": 0, "files": 0}
|
|
136
|
-
|
|
137
|
-
self._build_tree(
|
|
138
|
-
path_obj,
|
|
139
|
-
output,
|
|
140
|
-
stats,
|
|
141
|
-
prefix="",
|
|
142
|
-
is_last=True,
|
|
143
|
-
current_depth=0,
|
|
144
|
-
max_depth=max_depth,
|
|
145
|
-
show_hidden=show_hidden,
|
|
146
|
-
dirs_only=dirs_only,
|
|
147
|
-
show_size=show_size,
|
|
148
|
-
pattern=pattern,
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
# Add summary
|
|
152
|
-
output.append("")
|
|
153
|
-
if dirs_only:
|
|
154
|
-
output.append(f"{stats['dirs']} directories")
|
|
155
|
-
else:
|
|
156
|
-
output.append(f"{stats['dirs']} directories, {stats['files']} files")
|
|
157
|
-
|
|
158
|
-
return "\n".join(output)
|
|
159
|
-
|
|
160
|
-
def _build_tree(
|
|
161
|
-
self,
|
|
162
|
-
path: Path,
|
|
163
|
-
output: List[str],
|
|
164
|
-
stats: dict,
|
|
165
|
-
prefix: str,
|
|
166
|
-
is_last: bool,
|
|
167
|
-
current_depth: int,
|
|
168
|
-
max_depth: Optional[int],
|
|
169
|
-
show_hidden: bool,
|
|
170
|
-
dirs_only: bool,
|
|
171
|
-
show_size: bool,
|
|
172
|
-
pattern: Optional[str],
|
|
173
|
-
) -> None:
|
|
174
|
-
"""Recursively build tree structure."""
|
|
175
|
-
# Check depth limit
|
|
176
|
-
if max_depth is not None and current_depth >= max_depth:
|
|
177
|
-
return
|
|
178
|
-
|
|
179
|
-
try:
|
|
180
|
-
# Get entries
|
|
181
|
-
entries = list(path.iterdir())
|
|
182
|
-
|
|
183
|
-
# Filter hidden files
|
|
184
|
-
if not show_hidden:
|
|
185
|
-
entries = [e for e in entries if not e.name.startswith(".")]
|
|
186
|
-
|
|
187
|
-
# Filter by pattern
|
|
188
|
-
if pattern:
|
|
189
|
-
import fnmatch
|
|
190
|
-
|
|
191
|
-
entries = [
|
|
192
|
-
e for e in entries if fnmatch.fnmatch(e.name, pattern) or e.is_dir()
|
|
193
|
-
]
|
|
194
|
-
|
|
195
|
-
# Filter dirs only
|
|
196
|
-
if dirs_only:
|
|
197
|
-
entries = [e for e in entries if e.is_dir()]
|
|
198
|
-
|
|
199
|
-
# Sort entries (dirs first, then alphabetically)
|
|
200
|
-
entries.sort(key=lambda e: (not e.is_dir(), e.name.lower()))
|
|
201
|
-
|
|
202
|
-
# Process each entry
|
|
203
|
-
for i, entry in enumerate(entries):
|
|
204
|
-
is_last_entry = i == len(entries) - 1
|
|
205
|
-
|
|
206
|
-
# Skip if not allowed
|
|
207
|
-
if not self.is_path_allowed(str(entry)):
|
|
208
|
-
continue
|
|
209
|
-
|
|
210
|
-
# Build the tree branch
|
|
211
|
-
if prefix:
|
|
212
|
-
if is_last_entry:
|
|
213
|
-
branch = prefix + "└── "
|
|
214
|
-
extension = prefix + " "
|
|
215
|
-
else:
|
|
216
|
-
branch = prefix + "├── "
|
|
217
|
-
extension = prefix + "│ "
|
|
218
|
-
else:
|
|
219
|
-
branch = ""
|
|
220
|
-
extension = ""
|
|
221
|
-
|
|
222
|
-
# Build entry line
|
|
223
|
-
line = branch + entry.name
|
|
224
|
-
|
|
225
|
-
# Add size if requested
|
|
226
|
-
if show_size and entry.is_file():
|
|
227
|
-
try:
|
|
228
|
-
size = entry.stat().st_size
|
|
229
|
-
line += f" ({self._format_size(size)})"
|
|
230
|
-
except Exception:
|
|
231
|
-
pass
|
|
232
|
-
|
|
233
|
-
output.append(line)
|
|
234
|
-
|
|
235
|
-
# Update stats
|
|
236
|
-
if entry.is_dir():
|
|
237
|
-
stats["dirs"] += 1
|
|
238
|
-
# Recurse into directory
|
|
239
|
-
self._build_tree(
|
|
240
|
-
entry,
|
|
241
|
-
output,
|
|
242
|
-
stats,
|
|
243
|
-
prefix=extension,
|
|
244
|
-
is_last=is_last_entry,
|
|
245
|
-
current_depth=current_depth + 1,
|
|
246
|
-
max_depth=max_depth,
|
|
247
|
-
show_hidden=show_hidden,
|
|
248
|
-
dirs_only=dirs_only,
|
|
249
|
-
show_size=show_size,
|
|
250
|
-
pattern=pattern,
|
|
251
|
-
)
|
|
252
|
-
else:
|
|
253
|
-
stats["files"] += 1
|
|
254
|
-
|
|
255
|
-
except PermissionError:
|
|
256
|
-
output.append(prefix + "[Permission Denied]")
|
|
257
|
-
except Exception as e:
|
|
258
|
-
output.append(prefix + f"[Error: {str(e)}]")
|
|
259
|
-
|
|
260
|
-
def _format_size(self, size: int) -> str:
|
|
261
|
-
"""Format file size in human-readable format."""
|
|
262
|
-
for unit in ["B", "KB", "MB", "GB", "TB"]:
|
|
263
|
-
if size < 1024.0:
|
|
264
|
-
return f"{size:.1f}{unit}"
|
|
265
|
-
size /= 1024.0
|
|
266
|
-
return f"{size:.1f}PB"
|
|
267
|
-
|
|
268
|
-
def register(self, mcp_server) -> None:
|
|
269
|
-
"""Register this tool with the MCP server."""
|
|
270
|
-
pass
|
|
@@ -1,317 +0,0 @@
|
|
|
1
|
-
"""Edit notebook tool implementation.
|
|
2
|
-
|
|
3
|
-
This module provides the NoteBookEditTool for editing Jupyter notebook files.
|
|
4
|
-
"""
|
|
5
|
-
|
|
6
|
-
import json
|
|
7
|
-
from typing import Any, Unpack, Literal, Annotated, TypedDict, final, override
|
|
8
|
-
from pathlib import Path
|
|
9
|
-
|
|
10
|
-
from pydantic import Field
|
|
11
|
-
from mcp.server import FastMCP
|
|
12
|
-
from mcp.server.fastmcp import Context as MCPContext
|
|
13
|
-
|
|
14
|
-
from hanzo_mcp.tools.jupyter.base import JupyterBaseTool
|
|
15
|
-
|
|
16
|
-
NotebookPath = Annotated[
|
|
17
|
-
str,
|
|
18
|
-
Field(
|
|
19
|
-
description="The absolute path to the Jupyter notebook file to edit (must be absolute, not relative)",
|
|
20
|
-
),
|
|
21
|
-
]
|
|
22
|
-
|
|
23
|
-
CellNumber = Annotated[
|
|
24
|
-
int,
|
|
25
|
-
Field(
|
|
26
|
-
description="The index of the cell to edit (0-based)",
|
|
27
|
-
ge=0,
|
|
28
|
-
),
|
|
29
|
-
]
|
|
30
|
-
|
|
31
|
-
NewSource = Annotated[
|
|
32
|
-
str,
|
|
33
|
-
Field(
|
|
34
|
-
description="The new source for the cell",
|
|
35
|
-
default="",
|
|
36
|
-
),
|
|
37
|
-
]
|
|
38
|
-
|
|
39
|
-
CellType = Annotated[
|
|
40
|
-
Literal["code", "markdown"],
|
|
41
|
-
Field(
|
|
42
|
-
description="The of the cell (code or markdown). If not specified, it defaults to the current cell type. If using edit_mode=insert, this is required.",
|
|
43
|
-
default="code",
|
|
44
|
-
),
|
|
45
|
-
]
|
|
46
|
-
|
|
47
|
-
EditMode = Annotated[
|
|
48
|
-
Literal["replace", "insert", "delete"],
|
|
49
|
-
Field(
|
|
50
|
-
description="The of edit to make (replace, insert, delete). Defaults to replace.",
|
|
51
|
-
default="replace",
|
|
52
|
-
),
|
|
53
|
-
]
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
class NotebookEditToolParams(TypedDict):
|
|
57
|
-
"""Parameters for the NotebookEditTool.
|
|
58
|
-
|
|
59
|
-
Attributes:
|
|
60
|
-
notebook_path: The absolute path to the Jupyter notebook file to edit (must be absolute, not relative)
|
|
61
|
-
cell_number: The index of the cell to edit (0-based)
|
|
62
|
-
new_source: The new source for the cell
|
|
63
|
-
cell_type: The of the cell (code or markdown). If not specified, it defaults to the current cell type. If using edit_mode=insert, this is required.
|
|
64
|
-
edit_mode: The of edit to make (replace, insert, delete). Defaults to replace.
|
|
65
|
-
"""
|
|
66
|
-
|
|
67
|
-
notebook_path: NotebookPath
|
|
68
|
-
cell_number: CellNumber
|
|
69
|
-
new_source: NewSource
|
|
70
|
-
cell_type: CellType
|
|
71
|
-
edit_mode: EditMode
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
@final
|
|
75
|
-
class NoteBookEditTool(JupyterBaseTool):
|
|
76
|
-
"""Tool for editing Jupyter notebook files."""
|
|
77
|
-
|
|
78
|
-
@property
|
|
79
|
-
@override
|
|
80
|
-
def name(self) -> str:
|
|
81
|
-
"""Get the tool name.
|
|
82
|
-
|
|
83
|
-
Returns:
|
|
84
|
-
Tool name
|
|
85
|
-
"""
|
|
86
|
-
return "notebook_edit"
|
|
87
|
-
|
|
88
|
-
@property
|
|
89
|
-
@override
|
|
90
|
-
def description(self) -> str:
|
|
91
|
-
"""Get the tool description.
|
|
92
|
-
|
|
93
|
-
Returns:
|
|
94
|
-
Tool description
|
|
95
|
-
"""
|
|
96
|
-
return "Completely replaces the contents of a specific cell in a Jupyter notebook (.ipynb file) with new source. Jupyter notebooks are interactive documents that combine code, text, and visualizations, commonly used for data analysis and scientific computing. The notebook_path parameter must be an absolute path, not a relative path. The cell_number is 0-indexed. Use edit_mode=insert to add a new cell at the index specified by cell_number. Use edit_mode=delete to delete the cell at the index specified by cell_number."
|
|
97
|
-
|
|
98
|
-
@override
|
|
99
|
-
async def call(
|
|
100
|
-
self,
|
|
101
|
-
ctx: MCPContext,
|
|
102
|
-
**params: Unpack[NotebookEditToolParams],
|
|
103
|
-
) -> str:
|
|
104
|
-
"""Execute the tool with the given parameters.
|
|
105
|
-
|
|
106
|
-
Args:
|
|
107
|
-
ctx: MCP context
|
|
108
|
-
**params: Tool parameters
|
|
109
|
-
|
|
110
|
-
Returns:
|
|
111
|
-
Tool result
|
|
112
|
-
"""
|
|
113
|
-
tool_ctx = self.create_tool_context(ctx)
|
|
114
|
-
self.set_tool_context_info(tool_ctx)
|
|
115
|
-
|
|
116
|
-
# Extract parameters
|
|
117
|
-
notebook_path = params.get("notebook_path")
|
|
118
|
-
cell_number = params.get("cell_number")
|
|
119
|
-
new_source = params.get("new_source")
|
|
120
|
-
cell_type = params.get("cell_type")
|
|
121
|
-
edit_mode = params.get("edit_mode", "replace")
|
|
122
|
-
|
|
123
|
-
path_validation = self.validate_path(notebook_path)
|
|
124
|
-
if path_validation.is_error:
|
|
125
|
-
await tool_ctx.error(path_validation.error_message)
|
|
126
|
-
return f"Error: {path_validation.error_message}"
|
|
127
|
-
|
|
128
|
-
# Validate edit_mode
|
|
129
|
-
if edit_mode not in ["replace", "insert", "delete"]:
|
|
130
|
-
await tool_ctx.error("Edit mode must be replace, insert, or delete")
|
|
131
|
-
return "Error: Edit mode must be replace, insert, or delete"
|
|
132
|
-
|
|
133
|
-
# In insert mode, cell_type is required
|
|
134
|
-
if edit_mode == "insert" and cell_type is None:
|
|
135
|
-
await tool_ctx.error("Cell type is required when using insert mode")
|
|
136
|
-
return "Error: Cell type is required when using insert mode"
|
|
137
|
-
|
|
138
|
-
# Don't validate new_source for delete mode
|
|
139
|
-
if edit_mode != "delete" and not new_source:
|
|
140
|
-
await tool_ctx.error(
|
|
141
|
-
"New source is required for replace or insert operations"
|
|
142
|
-
)
|
|
143
|
-
return "Error: New source is required for replace or insert operations"
|
|
144
|
-
|
|
145
|
-
await tool_ctx.info(
|
|
146
|
-
f"Editing notebook: {notebook_path} (cell: {cell_number}, mode: {edit_mode})"
|
|
147
|
-
)
|
|
148
|
-
|
|
149
|
-
# Check if path is allowed
|
|
150
|
-
if not self.is_path_allowed(notebook_path):
|
|
151
|
-
await tool_ctx.error(
|
|
152
|
-
f"Access denied - path outside allowed directories: {notebook_path}"
|
|
153
|
-
)
|
|
154
|
-
return f"Error: Access denied - path outside allowed directories: {notebook_path}"
|
|
155
|
-
|
|
156
|
-
try:
|
|
157
|
-
file_path = Path(notebook_path)
|
|
158
|
-
|
|
159
|
-
if not file_path.exists():
|
|
160
|
-
await tool_ctx.error(f"File does not exist: {notebook_path}")
|
|
161
|
-
return f"Error: File does not exist: {notebook_path}"
|
|
162
|
-
|
|
163
|
-
if not file_path.is_file():
|
|
164
|
-
await tool_ctx.error(f"Path is not a file: {notebook_path}")
|
|
165
|
-
return f"Error: Path is not a file: {notebook_path}"
|
|
166
|
-
|
|
167
|
-
# Check file extension
|
|
168
|
-
if file_path.suffix.lower() != ".ipynb":
|
|
169
|
-
await tool_ctx.error(f"File is not a Jupyter notebook: {notebook_path}")
|
|
170
|
-
return f"Error: File is not a Jupyter notebook: {notebook_path}"
|
|
171
|
-
|
|
172
|
-
# Read and parse the notebook
|
|
173
|
-
try:
|
|
174
|
-
with open(file_path, "r", encoding="utf-8") as f:
|
|
175
|
-
content = f.read()
|
|
176
|
-
notebook = json.loads(content)
|
|
177
|
-
except json.JSONDecodeError:
|
|
178
|
-
await tool_ctx.error(f"Invalid notebook format: {notebook_path}")
|
|
179
|
-
return f"Error: Invalid notebook format: {notebook_path}"
|
|
180
|
-
except UnicodeDecodeError:
|
|
181
|
-
await tool_ctx.error(f"Cannot read notebook file: {notebook_path}")
|
|
182
|
-
return f"Error: Cannot read notebook file: {notebook_path}"
|
|
183
|
-
|
|
184
|
-
# Check cell_number is valid
|
|
185
|
-
cells = notebook.get("cells", [])
|
|
186
|
-
|
|
187
|
-
if edit_mode == "insert":
|
|
188
|
-
if cell_number > len(cells):
|
|
189
|
-
await tool_ctx.error(
|
|
190
|
-
f"Cell number {cell_number} is out of bounds for insert (max: {len(cells)})"
|
|
191
|
-
)
|
|
192
|
-
return f"Error: Cell number {cell_number} is out of bounds for insert (max: {len(cells)})"
|
|
193
|
-
else: # replace or delete
|
|
194
|
-
if cell_number >= len(cells):
|
|
195
|
-
await tool_ctx.error(
|
|
196
|
-
f"Cell number {cell_number} is out of bounds (max: {len(cells) - 1})"
|
|
197
|
-
)
|
|
198
|
-
return f"Error: Cell number {cell_number} is out of bounds (max: {len(cells) - 1})"
|
|
199
|
-
|
|
200
|
-
# Get notebook language (needed for context but not directly used in this block)
|
|
201
|
-
_ = (
|
|
202
|
-
notebook.get("metadata", {})
|
|
203
|
-
.get("language_info", {})
|
|
204
|
-
.get("name", "python")
|
|
205
|
-
)
|
|
206
|
-
|
|
207
|
-
# Perform the requested operation
|
|
208
|
-
if edit_mode == "replace":
|
|
209
|
-
# Get the target cell
|
|
210
|
-
target_cell = cells[cell_number]
|
|
211
|
-
|
|
212
|
-
# Store previous contents for reporting
|
|
213
|
-
old_type = target_cell.get("cell_type", "code")
|
|
214
|
-
old_source = target_cell.get("source", "")
|
|
215
|
-
|
|
216
|
-
# Fix for old_source which might be a list of strings
|
|
217
|
-
if isinstance(old_source, list):
|
|
218
|
-
old_source = "".join([str(item) for item in old_source])
|
|
219
|
-
|
|
220
|
-
# Update source
|
|
221
|
-
target_cell["source"] = new_source
|
|
222
|
-
|
|
223
|
-
# Update type if specified
|
|
224
|
-
if cell_type is not None:
|
|
225
|
-
target_cell["cell_type"] = cell_type
|
|
226
|
-
|
|
227
|
-
# If changing to markdown, remove code-specific fields
|
|
228
|
-
if cell_type == "markdown":
|
|
229
|
-
if "outputs" in target_cell:
|
|
230
|
-
del target_cell["outputs"]
|
|
231
|
-
if "execution_count" in target_cell:
|
|
232
|
-
del target_cell["execution_count"]
|
|
233
|
-
|
|
234
|
-
# If code cell, reset execution
|
|
235
|
-
if target_cell["cell_type"] == "code":
|
|
236
|
-
target_cell["outputs"] = []
|
|
237
|
-
target_cell["execution_count"] = None
|
|
238
|
-
|
|
239
|
-
change_description = f"Replaced cell {cell_number}"
|
|
240
|
-
if cell_type is not None and cell_type != old_type:
|
|
241
|
-
change_description += (
|
|
242
|
-
f" (changed type from {old_type} to {cell_type})"
|
|
243
|
-
)
|
|
244
|
-
|
|
245
|
-
elif edit_mode == "insert":
|
|
246
|
-
# Create new cell
|
|
247
|
-
new_cell: dict[str, Any] = {
|
|
248
|
-
"cell_type": cell_type,
|
|
249
|
-
"source": new_source,
|
|
250
|
-
"metadata": {},
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
# Add code-specific fields
|
|
254
|
-
if cell_type == "code":
|
|
255
|
-
new_cell["outputs"] = []
|
|
256
|
-
new_cell["execution_count"] = None
|
|
257
|
-
|
|
258
|
-
# Insert the cell
|
|
259
|
-
cells.insert(cell_number, new_cell)
|
|
260
|
-
change_description = (
|
|
261
|
-
f"Inserted new {cell_type} cell at position {cell_number}"
|
|
262
|
-
)
|
|
263
|
-
|
|
264
|
-
else: # delete
|
|
265
|
-
# Store deleted cell info for reporting
|
|
266
|
-
deleted_cell = cells[cell_number]
|
|
267
|
-
deleted_type = deleted_cell.get("cell_type", "code")
|
|
268
|
-
|
|
269
|
-
# Remove the cell
|
|
270
|
-
del cells[cell_number]
|
|
271
|
-
change_description = (
|
|
272
|
-
f"Deleted {deleted_type} cell at position {cell_number}"
|
|
273
|
-
)
|
|
274
|
-
|
|
275
|
-
# Write the updated notebook back to file
|
|
276
|
-
with open(file_path, "w", encoding="utf-8") as f:
|
|
277
|
-
json.dump(notebook, f, indent=1)
|
|
278
|
-
|
|
279
|
-
await tool_ctx.info(
|
|
280
|
-
f"Successfully edited notebook: {notebook_path} - {change_description}"
|
|
281
|
-
)
|
|
282
|
-
return (
|
|
283
|
-
f"Successfully edited notebook: {notebook_path} - {change_description}"
|
|
284
|
-
)
|
|
285
|
-
except Exception as e:
|
|
286
|
-
await tool_ctx.error(f"Error editing notebook: {str(e)}")
|
|
287
|
-
return f"Error editing notebook: {str(e)}"
|
|
288
|
-
|
|
289
|
-
@override
|
|
290
|
-
def register(self, mcp_server: FastMCP) -> None:
|
|
291
|
-
"""Register this edit notebook tool with the MCP server.
|
|
292
|
-
|
|
293
|
-
Creates a wrapper function with explicitly defined parameters that match
|
|
294
|
-
the tool's parameter schema and registers it with the MCP server.
|
|
295
|
-
|
|
296
|
-
Args:
|
|
297
|
-
mcp_server: The FastMCP server instance
|
|
298
|
-
"""
|
|
299
|
-
tool_self = self # Create a reference to self for use in the closure
|
|
300
|
-
|
|
301
|
-
@mcp_server.tool(name=self.name, description=self.description)
|
|
302
|
-
async def notebook_edit(
|
|
303
|
-
notebook_path: NotebookPath,
|
|
304
|
-
cell_number: CellNumber,
|
|
305
|
-
new_source: NewSource,
|
|
306
|
-
cell_type: CellType,
|
|
307
|
-
edit_mode: EditMode,
|
|
308
|
-
ctx: MCPContext,
|
|
309
|
-
) -> str:
|
|
310
|
-
return await tool_self.call(
|
|
311
|
-
ctx,
|
|
312
|
-
notebook_path=notebook_path,
|
|
313
|
-
cell_number=cell_number,
|
|
314
|
-
new_source=new_source,
|
|
315
|
-
cell_type=cell_type,
|
|
316
|
-
edit_mode=edit_mode,
|
|
317
|
-
)
|