Skip to content

Commit 96b2183

Browse files
committed
update examples for client roots
1 parent fadfa70 commit 96b2183

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

examples/roots_client/main.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (
1820
type MockRootsHandler struct{}
1921

2022
func (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
}
@@ -119,6 +124,8 @@ func main() {
119124
result, err := mcpClient.CallTool(ctx, request)
120125
if err != nil {
121126
log.Fatalf("failed to call tool roots: %v", err)
127+
} else if result.IsError {
128+
log.Printf("tool reported error")
122129
} else if len(result.Content) > 0 {
123130
resultStr := ""
124131
for _, content := range result.Content {
@@ -133,4 +140,12 @@ func main() {
133140
if err := mcpClient.RootListChanges(ctx); err != nil {
134141
log.Printf("fail to notify root list change: %v", err)
135142
}
143+
144+
// Keep running until cancelled by signal
145+
select {
146+
case <-ctx.Done():
147+
log.Println("Client context cancelled")
148+
case <-sigChan:
149+
log.Println("Received shutdown signal")
150+
}
136151
}

examples/roots_http_client/main.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (
1820
type MockRootsHandler struct{}
1921

2022
func (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
}

examples/roots_server/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func handleNotification(ctx context.Context, notification mcp.JSONRPCNotificatio
1515
}
1616

1717
// main sets up and runs an MCP stdio server named "roots-stdio-server" with tool and roots capabilities.
18-
// It registers a handler for ToolsListChanged notifications, enables sampling, and adds a "roots" tool
19-
// that requests and returns the current root list. The program serves the MCP server over stdio and
18+
// It registers a handler for RootsListChanged notifications and adds a "roots" tool
19+
// that requests and returns the current roots list. The program serves the MCP server over stdio and
2020
// logs a fatal error if the server fails to start.
2121
func main() {
2222
// Enable roots capability
@@ -42,7 +42,6 @@ func main() {
4242
"description": "is this test only?",
4343
},
4444
},
45-
Required: []string{"testonly"},
4645
},
4746
}, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
4847
rootRequest := mcp.ListRootsRequest{}
@@ -55,6 +54,9 @@ func main() {
5554
Text: fmt.Sprintf("Root list: %v", result.Roots),
5655
},
5756
},
57+
StructuredContent: map[string]any{
58+
"roots": result.Roots,
59+
},
5860
}, nil
5961

6062
} else {

0 commit comments

Comments
 (0)