-
Notifications
You must be signed in to change notification settings - Fork 3
SANDBOX-1813: Implement bash MCP tool handler #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fbm3307
merged 3 commits into
codeready-toolchain:master
from
fbm3307:feat/SANDBOX-1813-bash-mcp-tool-handler
Jul 16, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package tools | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/codeready-toolchain/cli-mcp-server/pkg/agent" | ||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
| ) | ||
|
|
||
| // BashInput is the tool input schema for the bash MCP tool. | ||
| type BashInput struct { | ||
| Command string `json:"command" jsonschema:"Shell command to execute (full bash — pipes, redirects, chaining supported)"` | ||
| Timeout *int `json:"timeout,omitempty" jsonschema:"Max execution time in seconds (default 60, max 300)"` | ||
| } | ||
|
|
||
| // BashOutput is the structured result returned to the LLM. | ||
| type BashOutput struct { | ||
| Stdout string `json:"stdout"` | ||
| Stderr string `json:"stderr"` | ||
| ExitCode int `json:"exit_code"` | ||
| DurationMs int64 `json:"duration_ms"` | ||
| } | ||
|
|
||
| // CommandExecutor abstracts the session manager's ExecuteCommand for testability. | ||
| // *session.SessionManager satisfies this interface without an adapter. | ||
| type CommandExecutor interface { | ||
| ExecuteCommand(ctx context.Context, sessionID, command string, timeoutSec int) (*agent.ExecResponse, error) | ||
| } | ||
|
|
||
| const ( | ||
| defaultTimeout = 60 | ||
| maxTimeout = 300 | ||
| ) | ||
|
|
||
| // BashTool implements the bash MCP tool handler. | ||
| type BashTool struct { | ||
| executor CommandExecutor | ||
| tool *mcp.Tool | ||
| } | ||
|
|
||
| // NewBashTool creates a BashTool with the given executor. | ||
| func NewBashTool(executor CommandExecutor) *BashTool { | ||
| return &BashTool{ | ||
| executor: executor, | ||
| tool: &mcp.Tool{ | ||
| Name: "bash", | ||
| Description: "Execute a shell command in a persistent sandbox bash session. Supports full bash syntax: pipes, redirects, chaining, and standard Unix tools.", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // RegisterWith registers the bash tool on the given MCP server. | ||
| func (t *BashTool) RegisterWith(s *mcp.Server) { | ||
| mcp.AddTool(s, t.tool, t.handle) | ||
| } | ||
|
|
||
| // Tool returns the underlying mcp.Tool definition. | ||
| func (t *BashTool) Tool() *mcp.Tool { | ||
| return t.tool | ||
| } | ||
|
|
||
| func (t *BashTool) handle(ctx context.Context, req *mcp.CallToolRequest, input BashInput) (*mcp.CallToolResult, BashOutput, error) { | ||
| sessionID, err := extractSessionID(req) | ||
| if err != nil { | ||
| return nil, BashOutput{}, err | ||
| } | ||
|
|
||
| if input.Command == "" { | ||
| return nil, BashOutput{}, fmt.Errorf("command is required") | ||
| } | ||
|
|
||
| timeout := clampTimeout(input.Timeout) | ||
|
|
||
| resp, err := t.executor.ExecuteCommand(ctx, sessionID, input.Command, timeout) | ||
| if err != nil { | ||
| return nil, BashOutput{}, fmt.Errorf("sandbox exec failed: %w", err) | ||
| } | ||
|
|
||
| output := BashOutput{ | ||
| Stdout: resp.Stdout, | ||
| Stderr: resp.Stderr, | ||
| ExitCode: resp.ExitCode, | ||
| DurationMs: resp.DurationMs, | ||
| } | ||
|
|
||
| if resp.ExitCode != 0 { | ||
| return &mcp.CallToolResult{IsError: true}, output, nil | ||
| } | ||
| return nil, output, nil | ||
| } | ||
|
|
||
| func extractSessionID(req *mcp.CallToolRequest) (string, error) { | ||
| if req.Extra == nil || req.Extra.Header == nil { | ||
| return "", fmt.Errorf("missing X-Session-ID header") | ||
| } | ||
| sid := req.Extra.Header.Get("X-Session-ID") | ||
| if sid == "" { | ||
| return "", fmt.Errorf("missing X-Session-ID header") | ||
| } | ||
| return sid, nil | ||
| } | ||
|
|
||
| func clampTimeout(t *int) int { | ||
| if t == nil || *t <= 0 { | ||
| return defaultTimeout | ||
| } | ||
| if *t > maxTimeout { | ||
| return maxTimeout | ||
| } | ||
| return *t | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.