Skip to content

Commit fadfa70

Browse files
committed
update godoc and data format
1 parent 896a4c2 commit fadfa70

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

client/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ func (c *Client) Complete(
481481
return &result, nil
482482
}
483483

484+
// RootListChanges sends a roots list-changed notification to the server.
484485
func (c *Client) RootListChanges(
485486
ctx context.Context,
486487
) error {
@@ -602,7 +603,7 @@ func (c *Client) handleListRootsRequestTransport(ctx context.Context, request tr
602603
}
603604

604605
// Create the transport response
605-
response := transport.NewJSONRPCResultResponse(request.ID, resultBytes)
606+
response := transport.NewJSONRPCResultResponse(request.ID, json.RawMessage(resultBytes))
606607

607608
return response, nil
608609
}

examples/roots_client/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// MockRootsHandler implements client.RootsHandler for demonstration.
17-
// In a real implementation, this would integrate with an actual LLM API.
17+
// In a real implementation, this would enumerate workspace/project roots.
1818
type MockRootsHandler struct{}
1919

2020
func (h *MockRootsHandler) ListRoots(ctx context.Context, request mcp.ListRootsRequest) (*mcp.ListRootsResult, error) {
@@ -115,7 +115,7 @@ func main() {
115115
// call server tool
116116
request := mcp.CallToolRequest{}
117117
request.Params.Name = "roots"
118-
request.Params.Arguments = "{\"testonly\": \"yes\"}"
118+
request.Params.Arguments = map[string]any{"testonly": "yes"}
119119
result, err := mcpClient.CallTool(ctx, request)
120120
if err != nil {
121121
log.Fatalf("failed to call tool roots: %v", err)
@@ -126,7 +126,7 @@ func main() {
126126
resultStr += fmt.Sprintf("%s\n", textContent.Text)
127127
}
128128
}
129-
fmt.Printf("client call tool result: %s", resultStr)
129+
fmt.Printf("client call tool result: %s\n", resultStr)
130130
}
131131

132132
// mock the root change

examples/roots_http_client/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// MockRootsHandler implements client.RootsHandler for demonstration.
17-
// In a real implementation, this would integrate with an actual LLM API.
17+
// In a real implementation, this would enumerate workspace/project roots.
1818
type MockRootsHandler struct{}
1919

2020
func (h *MockRootsHandler) ListRoots(ctx context.Context, request mcp.ListRootsRequest) (*mcp.ListRootsResult, error) {
@@ -33,9 +33,9 @@ func (h *MockRootsHandler) ListRoots(ctx context.Context, request mcp.ListRootsR
3333
return result, nil
3434
}
3535

36-
// main starts a mock MCP roots client that communicates with a MCP server over http.
37-
// client will call server tool to get the root list. in server tool hander, it will send the list root request to client.
38-
// shuts down the client gracefully on SIGINT or SIGTERM.
36+
// main starts a mock MCP roots client over HTTP.
37+
// The server tool triggers a roots/list request on the client.
38+
// The client shuts down gracefully on SIGINT or SIGTERM.
3939
func main() {
4040
// Create roots handler
4141
rootsHandler := &MockRootsHandler{}
@@ -97,7 +97,7 @@ func main() {
9797
// call server tool
9898
request := mcp.CallToolRequest{}
9999
request.Params.Name = "roots"
100-
request.Params.Arguments = "{\"testonly\": \"yes\"}"
100+
request.Params.Arguments = map[string]any{"testonly": "yes"}
101101
result, err := mcpClient.CallTool(ctx, request)
102102
if err != nil {
103103
log.Fatalf("failed to call tool roots: %v", err)
@@ -108,7 +108,7 @@ func main() {
108108
resultStr += fmt.Sprintf("%s\n", textContent.Text)
109109
}
110110
}
111-
fmt.Printf("client call tool result: %s", resultStr)
111+
fmt.Printf("client call tool result: %s\n", resultStr)
112112
}
113113

114114
// Keep the client running (in a real app, you'd have your main application logic here)

examples/roots_server/main.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// handleNotification handles JSON-RPC notifications by printing the notification method to standard output.
1313
func handleNotification(ctx context.Context, notification mcp.JSONRPCNotification) {
14-
fmt.Printf("notification received: %v", notification.Notification.Method)
14+
fmt.Printf("notification received: %v\n", notification.Notification.Method)
1515
}
1616

1717
// main sets up and runs an MCP stdio server named "roots-stdio-server" with tool and roots capabilities.
@@ -27,9 +27,8 @@ func main() {
2727
// Create MCP server with roots capability
2828
mcpServer := server.NewMCPServer("roots-stdio-server", "1.0.0", opts...)
2929

30-
// Add list root list change notification
30+
// Register roots list-change notification handler
3131
mcpServer.AddNotificationHandler(mcp.MethodNotificationRootsListChanged, handleNotification)
32-
mcpServer.EnableSampling()
3332

3433
// Add a simple tool to test roots list
3534
mcpServer.AddTool(mcp.Tool{
@@ -59,7 +58,15 @@ func main() {
5958
}, nil
6059

6160
} else {
62-
return nil, err
61+
return &mcp.CallToolResult{
62+
Content: []mcp.Content{
63+
mcp.TextContent{
64+
Type: "text",
65+
Text: fmt.Sprintf("Fail to list roots: %v", err),
66+
},
67+
},
68+
IsError: true,
69+
}, nil
6370
}
6471
})
6572

mcp/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const (
7474
MethodNotificationPromptsListChanged = "notifications/prompts/list_changed"
7575

7676
// MethodNotificationToolsListChanged notifies when the list of available tools changes.
77-
// https://modelcontextprotocol.io/specification/2025-06-18/client/roots#root-list-changes
77+
// https://modelcontextprotocol.io/specification/2025-06-18/server/tools#list-changed-notification
7878
MethodNotificationToolsListChanged = "notifications/tools/list_changed"
7979

8080
// MethodNotificationRootsListChanged notifies when the list of available roots changes.

0 commit comments

Comments
 (0)