-
Notifications
You must be signed in to change notification settings - Fork 171
Initial MCP Roots support #211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cwlbraa
wants to merge
2
commits into
main
Choose a base branch
from
minimal-roots-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package mcpserver | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/mark3labs/mcp-go/mcp" | ||
| "github.com/mark3labs/mcp-go/server" | ||
| ) | ||
|
|
||
| // clientRoots holds the roots provided by the client | ||
| // this assumes a single client, which may go out the window when we add support for streaming http. | ||
| var ( | ||
| clientRoots []mcp.Root | ||
| clientRootsMu sync.RWMutex | ||
| ) | ||
|
|
||
| // sendRootsListRequest sends a roots/list request to the client | ||
| func sendRootsListRequest(ctx context.Context, session server.ClientSession) { | ||
| if session == nil { | ||
| return | ||
| } | ||
|
|
||
| // Send roots/list request as a notification | ||
| // Note: In a proper implementation, this would be a request-response | ||
| // but for now we'll send as notification and handle response separately | ||
| notification := mcp.JSONRPCNotification{ | ||
| JSONRPC: "2.0", | ||
| Notification: mcp.Notification{ | ||
| Method: "roots/list", | ||
| Params: mcp.NotificationParams{}, | ||
| }, | ||
| } | ||
|
|
||
| select { | ||
| case session.NotificationChannel() <- notification: | ||
| slog.Info("Requested roots/list from client") | ||
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // updateClientRoots parses roots from notification params and updates the global clientRoots | ||
| func updateClientRoots(notification mcp.JSONRPCNotification) int { | ||
| if notification.Params.AdditionalFields != nil { | ||
| if rootsData, ok := notification.Params.AdditionalFields["roots"].([]any); ok { | ||
| newRoots := make([]mcp.Root, 0, len(rootsData)) | ||
| for _, rootData := range rootsData { | ||
| if rootMap, ok := rootData.(map[string]any); ok { | ||
| root := mcp.Root{} | ||
| if uri, ok := rootMap["uri"].(string); ok { | ||
| root.URI = uri | ||
| } | ||
| if name, ok := rootMap["name"].(string); ok { | ||
| root.Name = name | ||
| } | ||
| newRoots = append(newRoots, root) | ||
| } | ||
| } | ||
|
|
||
| clientRootsMu.Lock() | ||
| clientRoots = newRoots | ||
| clientRootsMu.Unlock() | ||
|
|
||
| return len(newRoots) | ||
| } | ||
| } | ||
| return 0 | ||
| } | ||
|
|
||
| // repoOpenErrorMessage provides helpful error messages when repository opening fails | ||
| func repoOpenErrorMessage(source string, originalErr error) error { | ||
| baseMsg := fmt.Sprintf("failed to open repository '%s': %v", source, originalErr) | ||
|
|
||
| // If we have client roots, suggest them | ||
| clientRootsMu.RLock() | ||
| defer clientRootsMu.RUnlock() | ||
|
|
||
| if len(clientRoots) > 0 { | ||
| baseMsg += "\n\nAvailable roots from client:" | ||
| for _, root := range clientRoots { | ||
| uri := root.URI | ||
| uri = strings.TrimPrefix(uri, "file://") | ||
| if root.Name != "" { | ||
| baseMsg += fmt.Sprintf("\n - %s (%s)", uri, root.Name) | ||
| } else { | ||
| baseMsg += fmt.Sprintf("\n - %s", uri) | ||
| } | ||
| } | ||
| return errors.New(baseMsg) | ||
cwlbraa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Fallback: suggest common patterns | ||
| baseMsg += "\n\nTry using:\n - '.' for current directory\n - An absolute path to your git repository" | ||
| return errors.New(baseMsg) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.