Skip to content

Commit a189f77

Browse files
author
Marek Safarik
committed
Fix after sergio review
Signed-off-by: Marek Safarik <[email protected]>
1 parent 435b952 commit a189f77

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

mcp-client/main

12.8 MB
Binary file not shown.

mcp-client/main.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"log"
77
"os"
88
"os/exec"
9+
"os/signal"
910
"strings"
11+
"syscall"
1012

1113
"github.com/anthropics/anthropic-sdk-go"
1214
"github.com/anthropics/anthropic-sdk-go/option"
@@ -35,7 +37,17 @@ You have a maximum of 5 conversation turns to complete the task. When given a ta
3537
)
3638

3739
func main() {
38-
ctx := context.Background()
40+
ctx, cancel := context.WithCancel(context.Background())
41+
defer cancel()
42+
43+
// Handle shutdown signals
44+
sigChan := make(chan os.Signal, 1)
45+
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
46+
go func() {
47+
<-sigChan
48+
log.Println("\nShutting down gracefully...")
49+
cancel()
50+
}()
3951

4052
if err := godotenv.Load("../.env"); err != nil {
4153
log.Printf("Warning: .env file not loaded: %v", err)
@@ -55,12 +67,19 @@ func main() {
5567
log.Fatal("Error: user query cannot be empty")
5668
}
5769

58-
session, err := connectToMCPServer(ctx)
70+
session, cmd, err := connectToMCPServer(ctx)
5971
if err != nil {
6072
log.Fatalf("Failed to connect to MCP server: %v", err)
6173
}
6274
defer session.Close()
6375

76+
// In main(), ensure process is killed on exit
77+
defer func() {
78+
if cmd != nil && cmd.Process != nil {
79+
cmd.Process.Kill()
80+
}
81+
}()
82+
6483
claudeTools, err := getMCPTools(ctx, session)
6584
if err != nil {
6685
log.Fatalf("Failed to get MCP tools: %v", err)
@@ -74,14 +93,14 @@ func main() {
7493
}
7594

7695
// connectToMCPServer establishes connection to the MCP server
77-
func connectToMCPServer(ctx context.Context) (*mcp.ClientSession, error) {
96+
func connectToMCPServer(ctx context.Context) (*mcp.ClientSession, *exec.Cmd, error) {
7897
serverPath := os.Getenv("MCP_SERVER_PATH")
7998
if serverPath == "" {
8099
serverPath = defaultServerPath
81100
}
82101

83102
if _, err := os.Stat(serverPath); os.IsNotExist(err) {
84-
return nil, fmt.Errorf("server not found: %s", serverPath)
103+
return nil, nil, fmt.Errorf("server not found: %s", serverPath)
85104
}
86105

87106
client := mcp.NewClient(&mcp.Implementation{
@@ -93,7 +112,7 @@ func connectToMCPServer(ctx context.Context) (*mcp.ClientSession, error) {
93112
transport := &mcp.CommandTransport{Command: cmd}
94113
session, err := client.Connect(ctx, transport, nil)
95114
if err != nil {
96-
return nil, fmt.Errorf("failed to connect: %w", err)
115+
return nil, nil, fmt.Errorf("failed to connect: %w", err)
97116
}
98117

99118
// Monitor server process for unexpected exits
@@ -114,7 +133,7 @@ func connectToMCPServer(ctx context.Context) (*mcp.ClientSession, error) {
114133
}
115134

116135
log.Printf("Connected to MCP server: %s", serverPath)
117-
return session, nil
136+
return session, cmd, nil
118137
}
119138

120139
// getMCPTools retrieves and converts MCP tools to Claude format

mcp-client/mcp-client

13.5 MB
Binary file not shown.

0 commit comments

Comments
 (0)