Skip to content

Commit 106faeb

Browse files
committed
fix: ensure stdin reads always fail for mcp invocations
1 parent 3ae3a40 commit 106faeb

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

mcp.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"context"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io"
910
"path"
@@ -362,7 +363,7 @@ func (s *MCPServer) handleListTools(req JSONRPC2Request) {
362363

363364
tools = append(tools, Tool{
364365
Name: name,
365-
Description: cmd.Short,
366+
Description: cmd.Use + " -- " + cmd.Short,
366367
InputSchema: schema,
367368
})
368369
}
@@ -426,6 +427,17 @@ func (s *MCPServer) handleListResourceTemplates(req JSONRPC2Request) {
426427
s.sendSuccessResponse(req.ID, response)
427428
}
428429

430+
// errReader is an io.Reader that never reads successfully, returning a predefined error.
431+
type errReader string
432+
433+
// Read implements io.Reader
434+
func (r errReader) Read([]byte) (int, error) {
435+
return 0, errors.New(string(r))
436+
}
437+
438+
// Commands may attempt to read stdin. This error is returned on any attempted read from stdin from a command invocation.
439+
var dontReadStdin = errReader("This command is attempting to read from stdin, which indicates that it is missing one or more required arguments.")
440+
429441
// handleCallTool handles the tools/call method
430442
func (s *MCPServer) handleCallTool(req JSONRPC2Request) {
431443
params, err := UnmarshalParamsLenient[CallToolParams](req.Params)
@@ -443,6 +455,7 @@ func (s *MCPServer) handleCallTool(req JSONRPC2Request) {
443455
// Create a new invocation with captured stdout/stderr
444456
var stdout, stderr strings.Builder
445457
inv := cmd.Invoke()
458+
inv.Stdin = dontReadStdin // MCP tools have no stdin.
446459
inv.Stdout = &stdout
447460
inv.Stderr = &stderr
448461

@@ -510,8 +523,8 @@ func (s *MCPServer) handleCallTool(req JSONRPC2Request) {
510523
})
511524
}
512525

513-
// If no content but error, add error message
514-
if len(content) == 0 && err != nil {
526+
// Add error, if present.
527+
if err != nil {
515528
content = append(content, ToolContent{
516529
Type: "text",
517530
Text: err.Error(),

0 commit comments

Comments
 (0)