@@ -4,8 +4,10 @@ import (
44 "context"
55 "fmt"
66 "log"
7+ "net/url"
78 "os"
89 "os/signal"
10+ "path/filepath"
911 "syscall"
1012
1113 "github.com/mark3labs/mcp-go/client"
@@ -18,15 +20,18 @@ import (
1820type MockRootsHandler struct {}
1921
2022func (h * MockRootsHandler ) ListRoots (ctx context.Context , request mcp.ListRootsRequest ) (* mcp.ListRootsResult , error ) {
23+ home , _ := os .UserHomeDir ()
24+ app := filepath .ToSlash (filepath .Join (home , "app" ))
25+ proj := filepath .ToSlash (filepath .Join (home , "projects" , "test-project" ))
2126 result := & mcp.ListRootsResult {
2227 Roots : []mcp.Root {
2328 {
2429 Name : "app" ,
25- URI : "file:///User/haxxx/app" ,
30+ URI : ( & url. URL { Scheme : "file" , Path : "/" + app }). String () ,
2631 },
2732 {
2833 Name : "test-project" ,
29- URI : "file:///User/haxxx/projects/test-project" ,
34+ URI : ( & url. URL { Scheme : "file" , Path : "/" + proj }). String () ,
3035 },
3136 },
3237 }
@@ -62,6 +67,11 @@ func main() {
6267 if err != nil {
6368 log .Fatalf ("Failed to start client: %v" , err )
6469 }
70+ defer func () {
71+ if cerr := mcpClient .Close (); cerr != nil {
72+ log .Printf ("Error closing client: %v" , cerr )
73+ }
74+ }()
6575
6676 // Initialize the MCP session
6777 initRequest := mcp.InitializeRequest {
@@ -101,6 +111,8 @@ func main() {
101111 result , err := mcpClient .CallTool (ctx , request )
102112 if err != nil {
103113 log .Fatalf ("failed to call tool roots: %v" , err )
114+ } else if result .IsError {
115+ log .Printf ("tool reported error" )
104116 } else if len (result .Content ) > 0 {
105117 resultStr := ""
106118 for _ , content := range result .Content {
@@ -115,10 +127,13 @@ func main() {
115127 sigChan := make (chan os.Signal , 1 )
116128 signal .Notify (sigChan , os .Interrupt , syscall .SIGTERM )
117129
118- select {
119- case <- ctx .Done ():
120- log .Println ("Client context cancelled" )
121- case <- sigChan :
130+ ctx , cancel := context .WithCancel (ctx )
131+ defer cancel ()
132+
133+ go func () {
134+ <- sigChan
122135 log .Println ("Received shutdown signal" )
123- }
136+ cancel ()
137+ }()
138+ <- ctx .Done ()
124139}
0 commit comments