hanzo-mcp 0.3.8__py3-none-any.whl → 0.5.1__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/cli.py +118 -170
- hanzo_mcp/cli_enhanced.py +438 -0
- hanzo_mcp/config/__init__.py +19 -0
- hanzo_mcp/config/settings.py +449 -0
- hanzo_mcp/config/tool_config.py +197 -0
- hanzo_mcp/prompts/__init__.py +117 -0
- hanzo_mcp/prompts/compact_conversation.py +77 -0
- hanzo_mcp/prompts/create_release.py +38 -0
- hanzo_mcp/prompts/project_system.py +120 -0
- hanzo_mcp/prompts/project_todo_reminder.py +111 -0
- hanzo_mcp/prompts/utils.py +286 -0
- hanzo_mcp/server.py +117 -99
- hanzo_mcp/tools/__init__.py +121 -33
- hanzo_mcp/tools/agent/__init__.py +8 -11
- hanzo_mcp/tools/agent/agent_tool.py +290 -224
- hanzo_mcp/tools/agent/prompt.py +16 -13
- hanzo_mcp/tools/agent/tool_adapter.py +9 -9
- hanzo_mcp/tools/common/__init__.py +17 -16
- hanzo_mcp/tools/common/base.py +79 -110
- hanzo_mcp/tools/common/batch_tool.py +330 -0
- hanzo_mcp/tools/common/config_tool.py +396 -0
- hanzo_mcp/tools/common/context.py +26 -292
- hanzo_mcp/tools/common/permissions.py +12 -12
- hanzo_mcp/tools/common/thinking_tool.py +153 -0
- hanzo_mcp/tools/common/validation.py +1 -63
- hanzo_mcp/tools/filesystem/__init__.py +97 -57
- hanzo_mcp/tools/filesystem/base.py +32 -24
- hanzo_mcp/tools/filesystem/content_replace.py +114 -107
- hanzo_mcp/tools/filesystem/directory_tree.py +129 -105
- hanzo_mcp/tools/filesystem/edit.py +279 -0
- hanzo_mcp/tools/filesystem/grep.py +458 -0
- hanzo_mcp/tools/filesystem/grep_ast_tool.py +250 -0
- hanzo_mcp/tools/filesystem/multi_edit.py +362 -0
- hanzo_mcp/tools/filesystem/read.py +255 -0
- hanzo_mcp/tools/filesystem/unified_search.py +689 -0
- hanzo_mcp/tools/filesystem/write.py +156 -0
- hanzo_mcp/tools/jupyter/__init__.py +41 -29
- hanzo_mcp/tools/jupyter/base.py +66 -57
- hanzo_mcp/tools/jupyter/{edit_notebook.py → notebook_edit.py} +162 -139
- hanzo_mcp/tools/jupyter/notebook_read.py +152 -0
- hanzo_mcp/tools/shell/__init__.py +29 -20
- hanzo_mcp/tools/shell/base.py +87 -45
- hanzo_mcp/tools/shell/bash_session.py +731 -0
- hanzo_mcp/tools/shell/bash_session_executor.py +295 -0
- hanzo_mcp/tools/shell/command_executor.py +435 -384
- hanzo_mcp/tools/shell/run_command.py +284 -131
- hanzo_mcp/tools/shell/run_command_windows.py +328 -0
- hanzo_mcp/tools/shell/session_manager.py +196 -0
- hanzo_mcp/tools/shell/session_storage.py +325 -0
- hanzo_mcp/tools/todo/__init__.py +66 -0
- hanzo_mcp/tools/todo/base.py +319 -0
- hanzo_mcp/tools/todo/todo_read.py +148 -0
- hanzo_mcp/tools/todo/todo_write.py +378 -0
- hanzo_mcp/tools/vector/__init__.py +99 -0
- hanzo_mcp/tools/vector/ast_analyzer.py +459 -0
- hanzo_mcp/tools/vector/git_ingester.py +482 -0
- hanzo_mcp/tools/vector/infinity_store.py +731 -0
- hanzo_mcp/tools/vector/mock_infinity.py +162 -0
- hanzo_mcp/tools/vector/project_manager.py +361 -0
- hanzo_mcp/tools/vector/vector_index.py +116 -0
- hanzo_mcp/tools/vector/vector_search.py +225 -0
- hanzo_mcp-0.5.1.dist-info/METADATA +276 -0
- hanzo_mcp-0.5.1.dist-info/RECORD +68 -0
- {hanzo_mcp-0.3.8.dist-info → hanzo_mcp-0.5.1.dist-info}/WHEEL +1 -1
- hanzo_mcp/tools/agent/base_provider.py +0 -73
- hanzo_mcp/tools/agent/litellm_provider.py +0 -45
- hanzo_mcp/tools/agent/lmstudio_agent.py +0 -385
- hanzo_mcp/tools/agent/lmstudio_provider.py +0 -219
- hanzo_mcp/tools/agent/provider_registry.py +0 -120
- hanzo_mcp/tools/common/error_handling.py +0 -86
- hanzo_mcp/tools/common/logging_config.py +0 -115
- hanzo_mcp/tools/common/session.py +0 -91
- hanzo_mcp/tools/common/think_tool.py +0 -123
- hanzo_mcp/tools/common/version_tool.py +0 -120
- hanzo_mcp/tools/filesystem/edit_file.py +0 -287
- hanzo_mcp/tools/filesystem/get_file_info.py +0 -170
- hanzo_mcp/tools/filesystem/read_files.py +0 -199
- hanzo_mcp/tools/filesystem/search_content.py +0 -275
- hanzo_mcp/tools/filesystem/write_file.py +0 -162
- hanzo_mcp/tools/jupyter/notebook_operations.py +0 -514
- hanzo_mcp/tools/jupyter/read_notebook.py +0 -165
- hanzo_mcp/tools/project/__init__.py +0 -64
- hanzo_mcp/tools/project/analysis.py +0 -886
- hanzo_mcp/tools/project/base.py +0 -66
- hanzo_mcp/tools/project/project_analyze.py +0 -173
- hanzo_mcp/tools/shell/run_script.py +0 -215
- hanzo_mcp/tools/shell/script_tool.py +0 -244
- hanzo_mcp-0.3.8.dist-info/METADATA +0 -196
- hanzo_mcp-0.3.8.dist-info/RECORD +0 -53
- {hanzo_mcp-0.3.8.dist-info → hanzo_mcp-0.5.1.dist-info}/entry_points.txt +0 -0
- {hanzo_mcp-0.3.8.dist-info → hanzo_mcp-0.5.1.dist-info}/licenses/LICENSE +0 -0
- {hanzo_mcp-0.3.8.dist-info → hanzo_mcp-0.5.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"""Run command tool implementation for Windows compatibility.
|
|
2
|
+
|
|
3
|
+
This module provides the RunCommandTool for running shell commands on Windows.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
from typing import Annotated, Any, final, override
|
|
8
|
+
|
|
9
|
+
from fastmcp import Context as MCPContext
|
|
10
|
+
from fastmcp import FastMCP
|
|
11
|
+
from fastmcp.server.dependencies import get_context
|
|
12
|
+
from pydantic import Field
|
|
13
|
+
|
|
14
|
+
from hanzo_mcp.tools.common.base import handle_connection_errors
|
|
15
|
+
from hanzo_mcp.tools.common.context import create_tool_context
|
|
16
|
+
from hanzo_mcp.tools.shell.base import ShellBaseTool
|
|
17
|
+
from hanzo_mcp.tools.shell.command_executor import CommandExecutor
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@final
|
|
21
|
+
class RunCommandTool(ShellBaseTool):
|
|
22
|
+
"""Tool for executing shell commands."""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self, permission_manager: Any, command_executor: CommandExecutor
|
|
26
|
+
) -> None:
|
|
27
|
+
"""Initialize the run command tool.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
permission_manager: Permission manager for access control
|
|
31
|
+
command_executor: Command executor for running commands
|
|
32
|
+
"""
|
|
33
|
+
super().__init__(permission_manager)
|
|
34
|
+
self.command_executor: CommandExecutor = command_executor
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
@override
|
|
38
|
+
def name(self) -> str:
|
|
39
|
+
"""Get the tool name.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
Tool name
|
|
43
|
+
"""
|
|
44
|
+
return "run_command"
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
@override
|
|
48
|
+
def description(self) -> str:
|
|
49
|
+
"""Get the tool description.
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
Tool description
|
|
53
|
+
"""
|
|
54
|
+
return """Executes a given bash command in a shell with optional timeout, ensuring proper handling and security measures.
|
|
55
|
+
|
|
56
|
+
Before executing the command, please follow these steps:
|
|
57
|
+
|
|
58
|
+
1. Directory Verification:
|
|
59
|
+
- If the command will create new directories or files, first use the directory_tree tool to verify the parent directory exists and is the correct location
|
|
60
|
+
- For example, before running \"mkdir foo/bar\", first use directory_tree to check that \"foo\" exists and is the intended parent directory
|
|
61
|
+
|
|
62
|
+
2. Command Execution:
|
|
63
|
+
- After ensuring proper quoting, execute the command.
|
|
64
|
+
- Capture the output of the command.
|
|
65
|
+
|
|
66
|
+
Usage notes:
|
|
67
|
+
- The command argument is required.
|
|
68
|
+
- You can specify an optional timeout in milliseconds (up to 600000ms / 10 minutes). If not specified, commands will timeout after 120000ms (2 minutes).
|
|
69
|
+
- It is very helpful if you write a clear, concise description of what this command does in 5-10 words.
|
|
70
|
+
- If the output exceeds 30000 characters, output will be truncated before being returned to you.
|
|
71
|
+
- VERY IMPORTANT: You MUST avoid using search commands like `find` and `grep`. Instead use Grep, Glob, or Task to search. You MUST avoid read tools like `cat`, `head`, `tail`, and `ls`, and use read and directory_tree to read files.
|
|
72
|
+
- If you _still_ need to run `grep`, STOP. ALWAYS USE ripgrep at `rg` (or /opt/homebrew/Cellar/ripgrep/14.1.1/bin/rg) first, which all Hanzo Code users have pre-installed.
|
|
73
|
+
- When issuing multiple commands, use the ';' or '&&' operator to separate them. DO NOT use newlines (newlines are ok in quoted strings).
|
|
74
|
+
<good-example>
|
|
75
|
+
cd /foo/bar && pytest tests
|
|
76
|
+
</good-example>
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# Committing changes with git
|
|
80
|
+
|
|
81
|
+
When the user asks you to create a new git commit, follow these steps carefully:
|
|
82
|
+
|
|
83
|
+
1. You have the capability to call multiple tools in a single response. When multiple independent pieces of information are requested, batch your tool calls together for optimal performance. ALWAYS run the following bash commands in parallel, each using the Bash tool:
|
|
84
|
+
- Run a git status command to see all untracked files.
|
|
85
|
+
- Run a git diff command to see both staged and unstaged changes that will be committed.
|
|
86
|
+
- Run a git log command to see recent commit messages, so that you can follow this repository's commit message style.
|
|
87
|
+
|
|
88
|
+
2. Analyze all staged changes (both previously staged and newly added) and draft a commit message. Wrap your analysis process in <commit_analysis> tags:
|
|
89
|
+
|
|
90
|
+
<commit_analysis>
|
|
91
|
+
- List the files that have been changed or added
|
|
92
|
+
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
|
93
|
+
- Brainstorm the purpose or motivation behind these changes
|
|
94
|
+
- Assess the impact of these changes on the overall project
|
|
95
|
+
- Check for any sensitive information that shouldn't be committed
|
|
96
|
+
- Draft a concise (1-2 sentences) commit message that focuses on the \"why\" rather than the \"what\"
|
|
97
|
+
- Ensure your language is clear, concise, and to the point
|
|
98
|
+
- Ensure the message accurately reflects the changes and their purpose (i.e. \"add\" means a wholly new feature, \"update\" means an enhancement to an existing feature, \"fix\" means a bug fix, etc.)
|
|
99
|
+
- Ensure the message is not generic (avoid words like \"Update\" or \"Fix\" without context)
|
|
100
|
+
- Review the draft message to ensure it accurately reflects the changes and their purpose
|
|
101
|
+
</commit_analysis>
|
|
102
|
+
|
|
103
|
+
3. You have the capability to call multiple tools in a single response. When multiple independent pieces of information are requested, batch your tool calls together for optimal performance. ALWAYS run the following commands in parallel:
|
|
104
|
+
- Add relevant untracked files to the staging area.
|
|
105
|
+
- Create the commit with a message ending with:
|
|
106
|
+
🤖 Generated with [Hanzo](https://hanzo.ai)
|
|
107
|
+
|
|
108
|
+
Co-Authored-By: Hanzo Dev <dev@hanzo.ai>
|
|
109
|
+
- Run git status to make sure the commit succeeded.
|
|
110
|
+
|
|
111
|
+
4. If the commit fails due to pre-commit hook changes, retry the commit ONCE to include these automated changes. If it fails again, it usually means a pre-commit hook is preventing the commit. If the commit succeeds but you notice that files were modified by the pre-commit hook, you MUST amend your commit to include them.
|
|
112
|
+
|
|
113
|
+
Important notes:
|
|
114
|
+
- Use the git context at the start of this conversation to determine which files are relevant to your commit. Be careful not to stage and commit files (e.g. with `git add .`) that aren't relevant to your commit.
|
|
115
|
+
- NEVER update the git config
|
|
116
|
+
- DO NOT run additional commands to read or explore code, beyond what is available in the git context
|
|
117
|
+
- DO NOT push to the remote repository
|
|
118
|
+
- IMPORTANT: Never use git commands with the -i flag (like git rebase -i or git add -i) since they require interactive input which is not supported.
|
|
119
|
+
- If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit
|
|
120
|
+
- Ensure your commit message is meaningful and concise. It should explain the purpose of the changes, not just describe them.
|
|
121
|
+
- Return an empty response - the user will see the git output directly
|
|
122
|
+
- In order to ensure good formatting, ALWAYS pass the commit message via a HEREDOC, a la this example:
|
|
123
|
+
<example>
|
|
124
|
+
git commit -m \"$(cat <<'EOF'
|
|
125
|
+
Commit message here.
|
|
126
|
+
|
|
127
|
+
🤖 Generated with [Hanzo MCP](https://github.com/SDGLBL/hanzo-mcp)
|
|
128
|
+
|
|
129
|
+
EOF
|
|
130
|
+
)\"
|
|
131
|
+
</example>
|
|
132
|
+
|
|
133
|
+
# Creating pull requests
|
|
134
|
+
Use the gh command via the Bash tool for ALL GitHub-related tasks including working with issues, pull requests, checks, and releases. If given a Github URL use the gh command to get the information needed.
|
|
135
|
+
|
|
136
|
+
IMPORTANT: When the user asks you to create a pull request, follow these steps carefully:
|
|
137
|
+
|
|
138
|
+
1. You have the capability to call multiple tools in a single response. When multiple independent pieces of information are requested, batch your tool calls together for optimal performance. ALWAYS run the following bash commands in parallel using the Bash tool, in order to understand the current state of the branch since it diverged from the main branch:
|
|
139
|
+
- Run a git status command to see all untracked files
|
|
140
|
+
- Run a git diff command to see both staged and unstaged changes that will be committed
|
|
141
|
+
- Check if the current branch tracks a remote branch and is up to date with the remote, so you know if you need to push to the remote
|
|
142
|
+
- Run a git log command and `git diff main...HEAD` to understand the full commit history for the current branch (from the time it diverged from the `main` branch)
|
|
143
|
+
|
|
144
|
+
2. Analyze all changes that will be included in the pull request, making sure to look at all relevant commits (NOT just the latest commit, but ALL commits that will be included in the pull request!!!), and draft a pull request summary. Wrap your analysis process in <pr_analysis> tags:
|
|
145
|
+
|
|
146
|
+
<pr_analysis>
|
|
147
|
+
- List the commits since diverging from the main branch
|
|
148
|
+
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
|
149
|
+
- Brainstorm the purpose or motivation behind these changes
|
|
150
|
+
- Assess the impact of these changes on the overall project
|
|
151
|
+
- Do not use tools to explore code, beyond what is available in the git context
|
|
152
|
+
- Check for any sensitive information that shouldn't be committed
|
|
153
|
+
- Draft a concise (1-2 bullet points) pull request summary that focuses on the \"why\" rather than the \"what\"
|
|
154
|
+
- Ensure the summary accurately reflects all changes since diverging from the main branch
|
|
155
|
+
- Ensure your language is clear, concise, and to the point
|
|
156
|
+
- Ensure the summary accurately reflects the changes and their purpose (ie. \"add\" means a wholly new feature, \"update\" means an enhancement to an existing feature, \"fix\" means a bug fix, etc.)
|
|
157
|
+
- Ensure the summary is not generic (avoid words like \"Update\" or \"Fix\" without context)
|
|
158
|
+
- Review the draft summary to ensure it accurately reflects the changes and their purpose
|
|
159
|
+
</pr_analysis>
|
|
160
|
+
|
|
161
|
+
3. You have the capability to call multiple tools in a single response. When multiple independent pieces of information are requested, batch your tool calls together for optimal performance. ALWAYS run the following commands in parallel:
|
|
162
|
+
- Create new branch if needed
|
|
163
|
+
- Push to remote with -u flag if needed
|
|
164
|
+
- Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting.
|
|
165
|
+
<example>
|
|
166
|
+
gh pr create --title \"the pr title\" --body \"$(cat <<'EOF'
|
|
167
|
+
## Summary
|
|
168
|
+
<1-3 bullet points>
|
|
169
|
+
|
|
170
|
+
## Test plan
|
|
171
|
+
[Checklist of TODOs for testing the pull request...]
|
|
172
|
+
|
|
173
|
+
🤖 Generated with [Hanzo](https://hanzo.ai)
|
|
174
|
+
EOF
|
|
175
|
+
)\"
|
|
176
|
+
</example>
|
|
177
|
+
|
|
178
|
+
Important:
|
|
179
|
+
- NEVER update the git config
|
|
180
|
+
- Return the PR URL when you're done, so the user can see it
|
|
181
|
+
|
|
182
|
+
# Other common operations
|
|
183
|
+
- View comments on a Github PR: gh api repos/foo/bar/pulls/123/comments"""
|
|
184
|
+
|
|
185
|
+
@override
|
|
186
|
+
async def prepare_tool_context(self, ctx: MCPContext) -> Any:
|
|
187
|
+
"""Create and prepare the tool context.
|
|
188
|
+
|
|
189
|
+
Args:
|
|
190
|
+
ctx: MCP context
|
|
191
|
+
|
|
192
|
+
Returns:
|
|
193
|
+
Prepared tool context
|
|
194
|
+
"""
|
|
195
|
+
tool_ctx = create_tool_context(ctx)
|
|
196
|
+
tool_ctx.set_tool_info(self.name)
|
|
197
|
+
return tool_ctx
|
|
198
|
+
|
|
199
|
+
@override
|
|
200
|
+
async def call(self, ctx: MCPContext, **params: Any) -> str:
|
|
201
|
+
"""Execute the tool with the given parameters.
|
|
202
|
+
|
|
203
|
+
Args:
|
|
204
|
+
ctx: MCP context
|
|
205
|
+
**params: Tool parameters
|
|
206
|
+
|
|
207
|
+
Returns:
|
|
208
|
+
Tool result
|
|
209
|
+
"""
|
|
210
|
+
tool_ctx = await self.prepare_tool_context(ctx)
|
|
211
|
+
|
|
212
|
+
# Extract parameters
|
|
213
|
+
command = params.get("command")
|
|
214
|
+
cwd = params.get("cwd")
|
|
215
|
+
shell_type = params.get("shell_type")
|
|
216
|
+
use_login_shell = params.get("use_login_shell", True)
|
|
217
|
+
|
|
218
|
+
# Validate required parameters
|
|
219
|
+
if not command:
|
|
220
|
+
await tool_ctx.error("Parameter 'command' is required but was None")
|
|
221
|
+
return "Error: Parameter 'command' is required but was None"
|
|
222
|
+
|
|
223
|
+
if command.strip() == "":
|
|
224
|
+
await tool_ctx.error("Parameter 'command' cannot be empty")
|
|
225
|
+
return "Error: Parameter 'command' cannot be empty"
|
|
226
|
+
|
|
227
|
+
if not cwd:
|
|
228
|
+
await tool_ctx.error("Parameter 'cwd' is required but was None")
|
|
229
|
+
return "Error: Parameter 'cwd' is required but was None"
|
|
230
|
+
|
|
231
|
+
if cwd.strip() == "":
|
|
232
|
+
await tool_ctx.error("Parameter 'cwd' cannot be empty")
|
|
233
|
+
return "Error: Parameter 'cwd' cannot be empty"
|
|
234
|
+
|
|
235
|
+
await tool_ctx.info(f"Executing command: {command}")
|
|
236
|
+
|
|
237
|
+
# Check if command is allowed
|
|
238
|
+
if not self.command_executor.is_command_allowed(command):
|
|
239
|
+
await tool_ctx.error(f"Command not allowed: {command}")
|
|
240
|
+
return f"Error: Command not allowed: {command}"
|
|
241
|
+
|
|
242
|
+
# Check if working directory is allowed
|
|
243
|
+
if not self.is_path_allowed(cwd):
|
|
244
|
+
await tool_ctx.error(f"Working directory not allowed: {cwd}")
|
|
245
|
+
return f"Error: Working directory not allowed: {cwd}"
|
|
246
|
+
|
|
247
|
+
# Check if working directory exists
|
|
248
|
+
if not os.path.isdir(cwd):
|
|
249
|
+
await tool_ctx.error(f"Working directory does not exist: {cwd}")
|
|
250
|
+
return f"Error: Working directory does not exist: {cwd}"
|
|
251
|
+
|
|
252
|
+
# Execute the command
|
|
253
|
+
result = await self.command_executor.execute_command(
|
|
254
|
+
command,
|
|
255
|
+
cwd=cwd,
|
|
256
|
+
shell_type=shell_type,
|
|
257
|
+
timeout=120.0, # Increased from 30s to 120s for better compatibility
|
|
258
|
+
use_login_shell=use_login_shell,
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
# Report result
|
|
262
|
+
if result.is_success:
|
|
263
|
+
await tool_ctx.info("Command executed successfully")
|
|
264
|
+
else:
|
|
265
|
+
await tool_ctx.error(f"Command failed with exit code {result.return_code}")
|
|
266
|
+
|
|
267
|
+
# Format the result
|
|
268
|
+
if result.is_success:
|
|
269
|
+
# For successful commands, just return stdout unless stderr has content
|
|
270
|
+
if result.stderr:
|
|
271
|
+
return f"Command executed successfully.\n\nSTDOUT:\n{result.stdout}\n\nSTDERR:\n{result.stderr}"
|
|
272
|
+
return result.stdout
|
|
273
|
+
else:
|
|
274
|
+
# For failed commands, include all available information
|
|
275
|
+
return result.format_output()
|
|
276
|
+
|
|
277
|
+
@override
|
|
278
|
+
def register(self, mcp_server: FastMCP) -> None:
|
|
279
|
+
"""Register this run command tool with the MCP server.
|
|
280
|
+
|
|
281
|
+
Creates a wrapper function with explicitly defined parameters that match
|
|
282
|
+
the tool's parameter schema and registers it with the MCP server.
|
|
283
|
+
|
|
284
|
+
Args:
|
|
285
|
+
mcp_server: The FastMCP server instance
|
|
286
|
+
"""
|
|
287
|
+
tool_self = self # Create a reference to self for use in the closure
|
|
288
|
+
|
|
289
|
+
@mcp_server.tool(name=self.name, description=self.description)
|
|
290
|
+
@handle_connection_errors
|
|
291
|
+
async def run_command(
|
|
292
|
+
command: Annotated[
|
|
293
|
+
str,
|
|
294
|
+
Field(
|
|
295
|
+
description="The shell command to execute",
|
|
296
|
+
min_length=1,
|
|
297
|
+
),
|
|
298
|
+
],
|
|
299
|
+
cwd: Annotated[
|
|
300
|
+
str,
|
|
301
|
+
Field(
|
|
302
|
+
description="Working directory where the command should be executed",
|
|
303
|
+
min_length=1,
|
|
304
|
+
),
|
|
305
|
+
],
|
|
306
|
+
shell_type: Annotated[
|
|
307
|
+
str | None,
|
|
308
|
+
Field(
|
|
309
|
+
description="Type of shell to use (e.g., bash, zsh). Defaults to system default",
|
|
310
|
+
default=None,
|
|
311
|
+
),
|
|
312
|
+
] = None,
|
|
313
|
+
use_login_shell: Annotated[
|
|
314
|
+
bool,
|
|
315
|
+
Field(
|
|
316
|
+
description="Whether to use a login shell (default: True)",
|
|
317
|
+
default=True,
|
|
318
|
+
),
|
|
319
|
+
] = True,
|
|
320
|
+
) -> str:
|
|
321
|
+
ctx = get_context()
|
|
322
|
+
return await tool_self.call(
|
|
323
|
+
ctx,
|
|
324
|
+
command=command,
|
|
325
|
+
cwd=cwd,
|
|
326
|
+
shell_type=shell_type,
|
|
327
|
+
use_login_shell=use_login_shell,
|
|
328
|
+
)
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"""Session manager for coordinating bash sessions.
|
|
2
|
+
|
|
3
|
+
This module provides the SessionManager class which manages the lifecycle
|
|
4
|
+
of BashSession instances, handling creation, retrieval, and cleanup.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import shutil
|
|
8
|
+
import threading
|
|
9
|
+
from typing import Self, final
|
|
10
|
+
|
|
11
|
+
from hanzo_mcp.tools.shell.bash_session import BashSession
|
|
12
|
+
from hanzo_mcp.tools.shell.session_storage import SessionStorage
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@final
|
|
16
|
+
class SessionManager:
|
|
17
|
+
"""Manager for bash sessions with tmux support.
|
|
18
|
+
|
|
19
|
+
This class manages the creation, retrieval, and cleanup
|
|
20
|
+
of persistent bash sessions. By default, it uses a singleton pattern,
|
|
21
|
+
but can be instantiated directly for dependency injection scenarios.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
_instance: Self | None = None
|
|
25
|
+
_lock = threading.Lock()
|
|
26
|
+
|
|
27
|
+
def __new__(
|
|
28
|
+
cls, use_singleton: bool = True, session_storage: SessionStorage | None = None
|
|
29
|
+
) -> "SessionManager":
|
|
30
|
+
"""Create SessionManager instance.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
use_singleton: If True, use singleton pattern. If False, create new instance.
|
|
34
|
+
"""
|
|
35
|
+
if not use_singleton:
|
|
36
|
+
# Create a new instance without singleton behavior
|
|
37
|
+
instance = super().__new__(cls)
|
|
38
|
+
instance._initialized = False
|
|
39
|
+
return instance
|
|
40
|
+
|
|
41
|
+
if cls._instance is None:
|
|
42
|
+
with cls._lock:
|
|
43
|
+
if cls._instance is None:
|
|
44
|
+
cls._instance = super().__new__(cls)
|
|
45
|
+
cls._instance._initialized = False
|
|
46
|
+
return cls._instance
|
|
47
|
+
|
|
48
|
+
def __init__(
|
|
49
|
+
self, use_singleton: bool = True, session_storage: SessionStorage | None = None
|
|
50
|
+
) -> None:
|
|
51
|
+
"""Initialize the session manager.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
use_singleton: If True, use singleton pattern (for backward compatibility)
|
|
55
|
+
session_storage: Optional session storage instance for dependency injection
|
|
56
|
+
"""
|
|
57
|
+
if hasattr(self, "_initialized") and self._initialized:
|
|
58
|
+
return
|
|
59
|
+
self._initialized = True
|
|
60
|
+
self.default_timeout_seconds = 30
|
|
61
|
+
self.default_session_timeout = 1800 # 30 minutes
|
|
62
|
+
|
|
63
|
+
# Allow dependency injection of session storage for isolation
|
|
64
|
+
if session_storage is not None:
|
|
65
|
+
self._session_storage = session_storage
|
|
66
|
+
elif use_singleton:
|
|
67
|
+
# Use the default global SessionStorage for singleton instances
|
|
68
|
+
from hanzo_mcp.tools.shell.session_storage import SessionStorage
|
|
69
|
+
|
|
70
|
+
self._session_storage = SessionStorage
|
|
71
|
+
else:
|
|
72
|
+
# Use isolated instance storage for non-singleton instances
|
|
73
|
+
from hanzo_mcp.tools.shell.session_storage import (
|
|
74
|
+
SessionStorageInstance,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
self._session_storage = SessionStorageInstance()
|
|
78
|
+
|
|
79
|
+
def is_tmux_available(self) -> bool:
|
|
80
|
+
"""Check if tmux is available on the system.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
True if tmux is available, False otherwise
|
|
84
|
+
"""
|
|
85
|
+
return shutil.which("tmux") is not None
|
|
86
|
+
|
|
87
|
+
def get_or_create_session(
|
|
88
|
+
self,
|
|
89
|
+
session_id: str,
|
|
90
|
+
work_dir: str,
|
|
91
|
+
username: str | None = None,
|
|
92
|
+
no_change_timeout_seconds: int | None = None,
|
|
93
|
+
max_memory_mb: int | None = None,
|
|
94
|
+
poll_interval: float | None = None,
|
|
95
|
+
) -> BashSession:
|
|
96
|
+
"""Get an existing session or create a new one.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
session_id: Unique identifier for the session
|
|
100
|
+
work_dir: Working directory for the session
|
|
101
|
+
username: Username to run commands as
|
|
102
|
+
no_change_timeout_seconds: Timeout for commands with no output changes
|
|
103
|
+
max_memory_mb: Memory limit for the session
|
|
104
|
+
poll_interval: Polling interval in seconds (default 0.5, use 0.1 for tests)
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
BashSession instance
|
|
108
|
+
|
|
109
|
+
Raises:
|
|
110
|
+
RuntimeError: If tmux is not available
|
|
111
|
+
"""
|
|
112
|
+
# Check if tmux is available
|
|
113
|
+
if not self.is_tmux_available():
|
|
114
|
+
raise RuntimeError(
|
|
115
|
+
"tmux is not available on this system. Please install tmux to use session-based command execution."
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# Try to get existing session
|
|
119
|
+
session = self._session_storage.get_session(session_id)
|
|
120
|
+
if session is not None:
|
|
121
|
+
return session
|
|
122
|
+
|
|
123
|
+
# Create new session
|
|
124
|
+
timeout = no_change_timeout_seconds or self.default_timeout_seconds
|
|
125
|
+
interval = poll_interval if poll_interval is not None else 0.5
|
|
126
|
+
session = BashSession(
|
|
127
|
+
id=session_id,
|
|
128
|
+
work_dir=work_dir,
|
|
129
|
+
username=username,
|
|
130
|
+
no_change_timeout_seconds=timeout,
|
|
131
|
+
max_memory_mb=max_memory_mb,
|
|
132
|
+
poll_interval=interval,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
# Store the session
|
|
136
|
+
self._session_storage.set_session(session_id, session)
|
|
137
|
+
|
|
138
|
+
return session
|
|
139
|
+
|
|
140
|
+
def get_session(self, session_id: str) -> BashSession | None:
|
|
141
|
+
"""Get an existing session.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
session_id: Unique identifier for the session
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
BashSession instance if found, None otherwise
|
|
148
|
+
"""
|
|
149
|
+
return self._session_storage.get_session(session_id)
|
|
150
|
+
|
|
151
|
+
def remove_session(self, session_id: str) -> bool:
|
|
152
|
+
"""Remove a session.
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
session_id: Unique identifier for the session
|
|
156
|
+
|
|
157
|
+
Returns:
|
|
158
|
+
True if session was removed, False if not found
|
|
159
|
+
"""
|
|
160
|
+
return self._session_storage.remove_session(session_id)
|
|
161
|
+
|
|
162
|
+
def cleanup_expired_sessions(self, max_age_seconds: int | None = None) -> int:
|
|
163
|
+
"""Clean up sessions that haven't been accessed recently.
|
|
164
|
+
|
|
165
|
+
Args:
|
|
166
|
+
max_age_seconds: Maximum age in seconds before cleanup
|
|
167
|
+
|
|
168
|
+
Returns:
|
|
169
|
+
Number of sessions cleaned up
|
|
170
|
+
"""
|
|
171
|
+
max_age = max_age_seconds or self.default_session_timeout
|
|
172
|
+
return self._session_storage.cleanup_expired_sessions(max_age)
|
|
173
|
+
|
|
174
|
+
def get_session_count(self) -> int:
|
|
175
|
+
"""Get the number of active sessions.
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
Number of active sessions
|
|
179
|
+
"""
|
|
180
|
+
return self._session_storage.get_session_count()
|
|
181
|
+
|
|
182
|
+
def get_all_session_ids(self) -> list[str]:
|
|
183
|
+
"""Get all active session IDs.
|
|
184
|
+
|
|
185
|
+
Returns:
|
|
186
|
+
List of active session IDs
|
|
187
|
+
"""
|
|
188
|
+
return self._session_storage.get_all_session_ids()
|
|
189
|
+
|
|
190
|
+
def clear_all_sessions(self) -> int:
|
|
191
|
+
"""Clear all sessions.
|
|
192
|
+
|
|
193
|
+
Returns:
|
|
194
|
+
Number of sessions cleared
|
|
195
|
+
"""
|
|
196
|
+
return self._session_storage.clear_all_sessions()
|