hanzo-mcp 0.3.8__py3-none-any.whl → 0.5.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 -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 +388 -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 +105 -32
- 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/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 +88 -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/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 +95 -0
- hanzo_mcp/tools/vector/infinity_store.py +365 -0
- hanzo_mcp/tools/vector/project_manager.py +361 -0
- hanzo_mcp/tools/vector/vector_index.py +115 -0
- hanzo_mcp/tools/vector/vector_search.py +215 -0
- {hanzo_mcp-0.3.8.dist-info → hanzo_mcp-0.5.0.dist-info}/METADATA +33 -1
- hanzo_mcp-0.5.0.dist-info/RECORD +63 -0
- {hanzo_mcp-0.3.8.dist-info → hanzo_mcp-0.5.0.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/RECORD +0 -53
- {hanzo_mcp-0.3.8.dist-info → hanzo_mcp-0.5.0.dist-info}/entry_points.txt +0 -0
- {hanzo_mcp-0.3.8.dist-info → hanzo_mcp-0.5.0.dist-info}/licenses/LICENSE +0 -0
- {hanzo_mcp-0.3.8.dist-info → hanzo_mcp-0.5.0.dist-info}/top_level.txt +0 -0
|
@@ -3,202 +3,355 @@
|
|
|
3
3
|
This module provides the RunCommandTool for running shell commands.
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
|
-
import
|
|
7
|
-
from typing import Any, final, override
|
|
6
|
+
from typing import Annotated, Any, TypedDict, Unpack, final, override
|
|
8
7
|
|
|
9
|
-
from
|
|
10
|
-
from
|
|
8
|
+
from fastmcp import Context as MCPContext
|
|
9
|
+
from fastmcp import FastMCP
|
|
10
|
+
from fastmcp.server.dependencies import get_context
|
|
11
|
+
from pydantic import Field
|
|
11
12
|
|
|
13
|
+
from hanzo_mcp.tools.common.base import handle_connection_errors
|
|
12
14
|
from hanzo_mcp.tools.common.context import create_tool_context
|
|
13
15
|
from hanzo_mcp.tools.shell.base import ShellBaseTool
|
|
14
|
-
from hanzo_mcp.tools.shell.
|
|
16
|
+
from hanzo_mcp.tools.shell.bash_session_executor import BashSessionExecutor
|
|
17
|
+
|
|
18
|
+
Command = Annotated[
|
|
19
|
+
str,
|
|
20
|
+
Field(
|
|
21
|
+
description="The shell command to execute",
|
|
22
|
+
min_length=1,
|
|
23
|
+
),
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
SessionID = Annotated[
|
|
28
|
+
str,
|
|
29
|
+
Field(
|
|
30
|
+
description="Session ID for persistent shell sessions (generate using timestamp command).",
|
|
31
|
+
),
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
TimeOut = Annotated[
|
|
35
|
+
int,
|
|
36
|
+
Field(
|
|
37
|
+
description="Timeout in seconds for session-based commands with no output changes (default: 30)",
|
|
38
|
+
default=30,
|
|
39
|
+
),
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
IsInput = Annotated[
|
|
43
|
+
bool,
|
|
44
|
+
Field(
|
|
45
|
+
description="Whether this is input to a running process rather than a new command (default: False)",
|
|
46
|
+
default=False,
|
|
47
|
+
),
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
Blocking = Annotated[
|
|
51
|
+
bool,
|
|
52
|
+
Field(
|
|
53
|
+
description="Whether to run in blocking mode, ignoring no-change timeout (default: False)",
|
|
54
|
+
default=False,
|
|
55
|
+
),
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class RunCommandToolParams(TypedDict):
|
|
60
|
+
"""Parameters for the RunCommandTool.
|
|
61
|
+
|
|
62
|
+
Attributes:
|
|
63
|
+
command: The shell command to execute
|
|
64
|
+
session_id: Optional session ID for persistent shell sessions
|
|
65
|
+
time_out: Timeout in seconds for session-based commands with no output changes
|
|
66
|
+
is_input: Whether this is input to a running process rather than a new command
|
|
67
|
+
blocking: Whether to run in blocking mode, ignoring no-change timeout
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
command: Command
|
|
71
|
+
session_id: SessionID
|
|
72
|
+
time_out: TimeOut
|
|
73
|
+
is_input: IsInput
|
|
74
|
+
blocking: Blocking
|
|
15
75
|
|
|
16
76
|
|
|
17
77
|
@final
|
|
18
78
|
class RunCommandTool(ShellBaseTool):
|
|
19
79
|
"""Tool for executing shell commands."""
|
|
20
|
-
|
|
21
|
-
def __init__(
|
|
80
|
+
|
|
81
|
+
def __init__(
|
|
82
|
+
self, permission_manager: Any, command_executor: BashSessionExecutor
|
|
83
|
+
) -> None:
|
|
22
84
|
"""Initialize the run command tool.
|
|
23
|
-
|
|
85
|
+
|
|
24
86
|
Args:
|
|
25
87
|
permission_manager: Permission manager for access control
|
|
26
88
|
command_executor: Command executor for running commands
|
|
27
89
|
"""
|
|
28
90
|
super().__init__(permission_manager)
|
|
29
|
-
self.command_executor:
|
|
30
|
-
|
|
91
|
+
self.command_executor: BashSessionExecutor = command_executor
|
|
92
|
+
|
|
31
93
|
@property
|
|
32
94
|
@override
|
|
33
95
|
def name(self) -> str:
|
|
34
96
|
"""Get the tool name.
|
|
35
|
-
|
|
97
|
+
|
|
36
98
|
Returns:
|
|
37
99
|
Tool name
|
|
38
100
|
"""
|
|
39
101
|
return "run_command"
|
|
40
|
-
|
|
102
|
+
|
|
41
103
|
@property
|
|
42
104
|
@override
|
|
43
105
|
def description(self) -> str:
|
|
44
106
|
"""Get the tool description.
|
|
45
|
-
|
|
107
|
+
|
|
46
108
|
Returns:
|
|
47
109
|
Tool description
|
|
48
110
|
"""
|
|
49
|
-
return """
|
|
111
|
+
return """Executes a given bash command in a persistent shell session with advanced interactive process handling, ensuring proper handling and security measures.
|
|
50
112
|
|
|
51
|
-
|
|
52
|
-
command: The shell command to execute
|
|
53
|
-
cwd: Working directory for the command. MUST be a subdirectory of one of the allowed paths, not a parent directory. Specify the most specific path possible.
|
|
113
|
+
Before executing the command, please follow these steps:
|
|
54
114
|
|
|
55
|
-
|
|
115
|
+
1. Directory Verification:
|
|
116
|
+
- 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
|
|
117
|
+
- For example, before running \"mkdir foo/bar\", first use directory_tree to check that \"foo\" exists and is the intended parent directory
|
|
118
|
+
|
|
119
|
+
2. Command Execution:
|
|
120
|
+
- After ensuring proper quoting, execute the command.
|
|
121
|
+
- Capture the output of the command.
|
|
122
|
+
|
|
123
|
+
Usage notes:
|
|
124
|
+
- The command argument is required.
|
|
125
|
+
- session_id: Optional string for persistent sessions (e.g., "my-session-123")
|
|
126
|
+
- time_out: Timeout in seconds for commands with no output changes (default: 30)
|
|
127
|
+
- is_input: Set to true when sending input to a running process rather than executing a new command
|
|
128
|
+
- blocking: Set to true to ignore no-change timeout for long-running commands
|
|
129
|
+
- It is very helpful if you write a clear, concise description of what this command does in 5-10 words.
|
|
130
|
+
- If the output exceeds 30000 characters, output will be truncated before being returned to you.
|
|
131
|
+
|
|
132
|
+
Interactive Process Handling:
|
|
133
|
+
- When a command is running and waiting for input, use is_input=true to send responses
|
|
134
|
+
- Commands like vim, nano, python REPL, and other interactive programs require is_input=true for interaction
|
|
135
|
+
- Use blocking=true for commands that legitimately take a long time without output
|
|
136
|
+
- Send control sequences (\"C-c\", \"C-z\", \"C-d\") as commands with is_input=true to interrupt processes
|
|
137
|
+
- The tool prevents sending new commands while a previous command is still running (use is_input=true to interact)
|
|
138
|
+
|
|
139
|
+
VERY IMPORTANT: You MUST avoid using search commands like `grep`. Instead use grep, or dispatch_agent to search. You MUST avoid read tools like `cat`, `head`, `tail`, and `ls`, and use read and directory_tree to read files.
|
|
140
|
+
- If you _still_ need to run `grep`, STOP. ALWAYS USE ripgrep at `rg` first, which all Hanzo users have pre-installed.
|
|
141
|
+
- When issuing multiple commands, use the ';' or '&&' operator to separate them. DO NOT use newlines (newlines are ok in quoted strings).
|
|
142
|
+
- Working Directory: New sessions start in the user's home directory. Use 'cd' commands to navigate to different directories within a session. Directory changes persist within the same session.
|
|
143
|
+
<good-example>
|
|
144
|
+
cd /foo/bar && pytest tests
|
|
145
|
+
</good-example>
|
|
146
|
+
<good-example>
|
|
147
|
+
pytest tests # Second running do not need cd
|
|
148
|
+
</good-example>
|
|
149
|
+
<bad-example>
|
|
150
|
+
pytest /foo/bar/tests # This works but doesn't change the session's working directory
|
|
151
|
+
</bad-example>
|
|
152
|
+
|
|
153
|
+
# Committing changes with git
|
|
154
|
+
|
|
155
|
+
When the user asks you to create a new git commit, follow these steps carefully:
|
|
156
|
+
|
|
157
|
+
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:
|
|
158
|
+
- Run a git status command to see all untracked files.
|
|
159
|
+
- Run a git diff command to see both staged and unstaged changes that will be committed.
|
|
160
|
+
- Run a git log command to see recent commit messages, so that you can follow this repository's commit message style.
|
|
161
|
+
|
|
162
|
+
2. Analyze all staged changes (both previously staged and newly added) and draft a commit message. Wrap your analysis process in <commit_analysis> tags:
|
|
163
|
+
|
|
164
|
+
<commit_analysis>
|
|
165
|
+
- List the files that have been changed or added
|
|
166
|
+
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
|
167
|
+
- Brainstorm the purpose or motivation behind these changes
|
|
168
|
+
- Assess the impact of these changes on the overall project
|
|
169
|
+
- Check for any sensitive information that shouldn't be committed
|
|
170
|
+
- Draft a concise (1-2 sentences) commit message that focuses on the \"why\" rather than the \"what\"
|
|
171
|
+
- Ensure your language is clear, concise, and to the point
|
|
172
|
+
- 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.)
|
|
173
|
+
- Ensure the message is not generic (avoid words like \"Update\" or \"Fix\" without context)
|
|
174
|
+
- Review the draft message to ensure it accurately reflects the changes and their purpose
|
|
175
|
+
</commit_analysis>
|
|
176
|
+
|
|
177
|
+
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:
|
|
178
|
+
- Add relevant untracked files to the staging area.
|
|
179
|
+
- Create the commit with a message ending with:
|
|
180
|
+
🤖 Generated with [Hanzo](https://hanzo.ai)
|
|
181
|
+
|
|
182
|
+
Co-Authored-By: Hanzo Dev <dev@hanzo.ai>
|
|
183
|
+
- Run git status to make sure the commit succeeded.
|
|
184
|
+
|
|
185
|
+
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.
|
|
186
|
+
|
|
187
|
+
Important notes:
|
|
188
|
+
- 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.
|
|
189
|
+
- NEVER update the git config
|
|
190
|
+
- DO NOT run additional commands to read or explore code, beyond what is available in the git context
|
|
191
|
+
- DO NOT push to the remote repository
|
|
192
|
+
- 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.
|
|
193
|
+
- If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit
|
|
194
|
+
- Ensure your commit message is meaningful and concise. It should explain the purpose of the changes, not just describe them.
|
|
195
|
+
- Return an empty response - the user will see the git output directly
|
|
196
|
+
- In order to ensure good formatting, ALWAYS pass the commit message via a HEREDOC, a la this example:
|
|
197
|
+
<example>
|
|
198
|
+
git commit -m \"$(cat <<'EOF'
|
|
199
|
+
Commit message here.
|
|
200
|
+
|
|
201
|
+
🤖 Generated with [Hanzo](https://hanzo.ai)
|
|
202
|
+
|
|
203
|
+
Co-Authored-By: Hanzo Dev <dev@hanzo.ai>
|
|
204
|
+
EOF
|
|
205
|
+
)\"
|
|
206
|
+
</example>
|
|
207
|
+
|
|
208
|
+
# Creating pull requests
|
|
209
|
+
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.
|
|
210
|
+
|
|
211
|
+
IMPORTANT: When the user asks you to create a pull request, follow these steps carefully:
|
|
212
|
+
|
|
213
|
+
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:
|
|
214
|
+
- Run a git status command to see all untracked files
|
|
215
|
+
- Run a git diff command to see both staged and unstaged changes that will be committed
|
|
216
|
+
- 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
|
|
217
|
+
- 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)
|
|
218
|
+
|
|
219
|
+
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:
|
|
220
|
+
|
|
221
|
+
<pr_analysis>
|
|
222
|
+
- List the commits since diverging from the main branch
|
|
223
|
+
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
|
224
|
+
- Brainstorm the purpose or motivation behind these changes
|
|
225
|
+
- Assess the impact of these changes on the overall project
|
|
226
|
+
- Do not use tools to explore code, beyond what is available in the git context
|
|
227
|
+
- Check for any sensitive information that shouldn't be committed
|
|
228
|
+
- Draft a concise (1-2 bullet points) pull request summary that focuses on the \"why\" rather than the \"what\"
|
|
229
|
+
- Ensure the summary accurately reflects all changes since diverging from the main branch
|
|
230
|
+
- Ensure your language is clear, concise, and to the point
|
|
231
|
+
- 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.)
|
|
232
|
+
- Ensure the summary is not generic (avoid words like \"Update\" or \"Fix\" without context)
|
|
233
|
+
- Review the draft summary to ensure it accurately reflects the changes and their purpose
|
|
234
|
+
</pr_analysis>
|
|
235
|
+
|
|
236
|
+
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:
|
|
237
|
+
- Create new branch if needed
|
|
238
|
+
- Push to remote with -u flag if needed
|
|
239
|
+
- Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting.
|
|
240
|
+
<example>
|
|
241
|
+
gh pr create --title \"the pr title\" --body \"$(cat <<'EOF'
|
|
242
|
+
## Summary
|
|
243
|
+
<1-3 bullet points>
|
|
244
|
+
|
|
245
|
+
## Test plan
|
|
246
|
+
[Checklist of TODOs for testing the pull request...]
|
|
247
|
+
|
|
248
|
+
🤖 Generated with [Hanzo Dev](https://hanzo.ai)
|
|
249
|
+
EOF
|
|
250
|
+
)\"
|
|
251
|
+
</example>
|
|
252
|
+
|
|
253
|
+
Important:
|
|
254
|
+
- NEVER update the git config
|
|
255
|
+
- Return the PR URL when you're done, so the user can see it
|
|
256
|
+
|
|
257
|
+
# Other common operations
|
|
258
|
+
- View comments on a Github PR: gh api repos/foo/bar/pulls/123/comments"""
|
|
56
259
|
|
|
57
|
-
Returns:
|
|
58
|
-
The output of the command
|
|
59
|
-
"""
|
|
60
|
-
|
|
61
|
-
@property
|
|
62
|
-
@override
|
|
63
|
-
def parameters(self) -> dict[str, Any]:
|
|
64
|
-
"""Get the parameter specifications for the tool.
|
|
65
|
-
|
|
66
|
-
Returns:
|
|
67
|
-
Parameter specifications
|
|
68
|
-
"""
|
|
69
|
-
return {
|
|
70
|
-
"properties": {
|
|
71
|
-
"command": {
|
|
72
|
-
"title": "Command",
|
|
73
|
-
"type": "string"
|
|
74
|
-
},
|
|
75
|
-
"cwd": {
|
|
76
|
-
"title": "Cwd",
|
|
77
|
-
"type": "string"
|
|
78
|
-
},
|
|
79
|
-
"use_login_shell": {
|
|
80
|
-
"default": True,
|
|
81
|
-
"title": "Use Login Shell",
|
|
82
|
-
"type": "boolean"
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
"required": ["command", "cwd"],
|
|
86
|
-
"title": "run_commandArguments",
|
|
87
|
-
"type": "object"
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
@property
|
|
91
|
-
@override
|
|
92
|
-
def required(self) -> list[str]:
|
|
93
|
-
"""Get the list of required parameter names.
|
|
94
|
-
|
|
95
|
-
Returns:
|
|
96
|
-
List of required parameter names
|
|
97
|
-
"""
|
|
98
|
-
return ["command", "cwd"]
|
|
99
|
-
|
|
100
260
|
@override
|
|
101
261
|
async def prepare_tool_context(self, ctx: MCPContext) -> Any:
|
|
102
262
|
"""Create and prepare the tool context.
|
|
103
|
-
|
|
263
|
+
|
|
104
264
|
Args:
|
|
105
265
|
ctx: MCP context
|
|
106
|
-
|
|
266
|
+
|
|
107
267
|
Returns:
|
|
108
268
|
Prepared tool context
|
|
109
269
|
"""
|
|
110
270
|
tool_ctx = create_tool_context(ctx)
|
|
111
271
|
tool_ctx.set_tool_info(self.name)
|
|
112
272
|
return tool_ctx
|
|
113
|
-
|
|
273
|
+
|
|
114
274
|
@override
|
|
115
|
-
async def call(
|
|
275
|
+
async def call(
|
|
276
|
+
self,
|
|
277
|
+
ctx: MCPContext,
|
|
278
|
+
**params: Unpack[RunCommandToolParams],
|
|
279
|
+
) -> str:
|
|
116
280
|
"""Execute the tool with the given parameters.
|
|
117
|
-
|
|
281
|
+
|
|
118
282
|
Args:
|
|
119
283
|
ctx: MCP context
|
|
120
284
|
**params: Tool parameters
|
|
121
|
-
|
|
285
|
+
|
|
122
286
|
Returns:
|
|
123
287
|
Tool result
|
|
124
288
|
"""
|
|
125
289
|
tool_ctx = await self.prepare_tool_context(ctx)
|
|
126
|
-
|
|
290
|
+
|
|
127
291
|
# Extract parameters
|
|
128
|
-
command = params
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if not cwd:
|
|
142
|
-
await tool_ctx.error("Parameter 'cwd' is required but was None")
|
|
143
|
-
return "Error: Parameter 'cwd' is required but was None"
|
|
144
|
-
|
|
145
|
-
if cwd.strip() == "":
|
|
146
|
-
await tool_ctx.error("Parameter 'cwd' cannot be empty")
|
|
147
|
-
return "Error: Parameter 'cwd' cannot be empty"
|
|
148
|
-
|
|
149
|
-
await tool_ctx.info(f"Executing command: {command}")
|
|
150
|
-
|
|
151
|
-
# Check if command is allowed
|
|
152
|
-
if not self.command_executor.is_command_allowed(command):
|
|
292
|
+
command = params["command"]
|
|
293
|
+
session_id = params.get("session_id")
|
|
294
|
+
time_out = params.get("time_out", 30)
|
|
295
|
+
is_input = params.get("is_input", False)
|
|
296
|
+
blocking = params.get("blocking", False)
|
|
297
|
+
|
|
298
|
+
await tool_ctx.info(
|
|
299
|
+
f"Executing command: {command} ( session_id={session_id}, is_input={is_input}, blocking={blocking} )"
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
# Check if command is allowed (skip for input to running processes)
|
|
303
|
+
if not is_input and not self.command_executor.is_command_allowed(command):
|
|
153
304
|
await tool_ctx.error(f"Command not allowed: {command}")
|
|
154
305
|
return f"Error: Command not allowed: {command}"
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
await
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
timeout=30.0,
|
|
171
|
-
use_login_shell=use_login_shell
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
# Report result
|
|
175
|
-
if result.is_success:
|
|
176
|
-
await tool_ctx.info("Command executed successfully")
|
|
177
|
-
else:
|
|
178
|
-
await tool_ctx.error(f"Command failed with exit code {result.return_code}")
|
|
179
|
-
|
|
180
|
-
# Format the result
|
|
306
|
+
|
|
307
|
+
try:
|
|
308
|
+
# Execute command using BashSessionExecutor with enhanced parameters
|
|
309
|
+
result = await self.command_executor.execute_command(
|
|
310
|
+
command=command,
|
|
311
|
+
timeout=time_out,
|
|
312
|
+
session_id=session_id,
|
|
313
|
+
is_input=is_input,
|
|
314
|
+
blocking=blocking,
|
|
315
|
+
)
|
|
316
|
+
except RuntimeError as e:
|
|
317
|
+
await tool_ctx.error(f"Session execution failed: {str(e)}")
|
|
318
|
+
return f"Error: Session execution failed: {str(e)}"
|
|
319
|
+
|
|
320
|
+
# Format the result using the new enhanced formatting
|
|
181
321
|
if result.is_success:
|
|
182
|
-
#
|
|
183
|
-
|
|
184
|
-
return f"Command executed successfully.\n\nSTDOUT:\n{result.stdout}\n\nSTDERR:\n{result.stderr}"
|
|
185
|
-
return result.stdout
|
|
322
|
+
# Use the enhanced agent observation format for better user experience
|
|
323
|
+
return result.to_agent_observation()
|
|
186
324
|
else:
|
|
187
|
-
# For failed commands,
|
|
325
|
+
# For failed or running commands, provide comprehensive output
|
|
188
326
|
return result.format_output()
|
|
189
|
-
|
|
327
|
+
|
|
190
328
|
@override
|
|
191
329
|
def register(self, mcp_server: FastMCP) -> None:
|
|
192
330
|
"""Register this run command tool with the MCP server.
|
|
193
|
-
|
|
331
|
+
|
|
194
332
|
Creates a wrapper function with explicitly defined parameters that match
|
|
195
333
|
the tool's parameter schema and registers it with the MCP server.
|
|
196
|
-
|
|
334
|
+
|
|
197
335
|
Args:
|
|
198
336
|
mcp_server: The FastMCP server instance
|
|
199
337
|
"""
|
|
200
338
|
tool_self = self # Create a reference to self for use in the closure
|
|
201
|
-
|
|
202
|
-
@mcp_server.tool(name=self.name, description=self.
|
|
203
|
-
|
|
204
|
-
|
|
339
|
+
|
|
340
|
+
@mcp_server.tool(name=self.name, description=self.description)
|
|
341
|
+
@handle_connection_errors
|
|
342
|
+
async def run_command(
|
|
343
|
+
command: Command,
|
|
344
|
+
session_id: SessionID,
|
|
345
|
+
time_out: TimeOut,
|
|
346
|
+
is_input: IsInput,
|
|
347
|
+
blocking: Blocking,
|
|
348
|
+
) -> str:
|
|
349
|
+
ctx = get_context()
|
|
350
|
+
return await tool_self.call(
|
|
351
|
+
ctx,
|
|
352
|
+
command=command,
|
|
353
|
+
session_id=session_id,
|
|
354
|
+
time_out=time_out,
|
|
355
|
+
is_input=is_input,
|
|
356
|
+
blocking=blocking,
|
|
357
|
+
)
|