Skip to content

Add -open flag to trigger initial opening of workspace files #42

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ This is an [MCP](https://modelcontextprotocol.io/introduction) server that runs
<div>
<p><strong>Install clangd</strong>: Download prebuilt binaries from the <a href="https://github.com/clangd/clangd/releases">official LLVM releases page</a> or install via your system's package manager (e.g., <code>apt install clangd</code>, <code>brew install clangd</code>).</p>
<p><strong>Configure your MCP client</strong>: This will be different but similar for each client. For Claude Desktop, add the following to <code>~/Library/Application\\ Support/Claude/claude_desktop_config.json</code></p>
<p><strong>NOTE</strong>: clangd will not resolve symbols until the first file is opened. Use the `-open` argument to trigger indexing.</p>

<pre>
{
Expand All @@ -140,6 +141,8 @@ This is an [MCP](https://modelcontextprotocol.io/introduction) server that runs
"/Users/you/dev/yourproject/",
"--lsp",
"/path/to/your/clangd_binary",
"--open",
"/Users/you/dev/yourproject/main.cpp",
"--",
"--compile-commands-dir=/path/to/yourproject/build_or_compile_commands_dir"
]
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/isaacphi/mcp-language-server
go 1.24.0

require (
github.com/bmatcuk/doublestar/v4 v4.8.1
github.com/davecgh/go-spew v1.1.1
github.com/fsnotify/fsnotify v1.9.0
github.com/mark3labs/mcp-go v0.25.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
49 changes: 49 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/bmatcuk/doublestar/v4"
"github.com/isaacphi/mcp-language-server/internal/logging"
"github.com/isaacphi/mcp-language-server/internal/lsp"
"github.com/isaacphi/mcp-language-server/internal/watcher"
Expand All @@ -23,6 +25,7 @@ var coreLogger = logging.NewLogger(logging.Core)
type config struct {
workspaceDir string
lspCommand string
openGlobs StringArrayFlag
lspArgs []string
}

Expand All @@ -35,10 +38,25 @@ type mcpServer struct {
workspaceWatcher *watcher.WorkspaceWatcher
}

// StringArrayFlag is a custom flag type to handle an array of strings
type StringArrayFlag []string

// Set appends a new value to the custom flag value
func (s *StringArrayFlag) Set(value string) error {
*s = append(*s, value)
return nil
}

// String returns the string representation of the custom flag value
func (s *StringArrayFlag) String() string {
return strings.Join(*s, ",")
}

func parseConfig() (*config, error) {
cfg := &config{}
flag.StringVar(&cfg.workspaceDir, "workspace", "", "Path to workspace directory")
flag.StringVar(&cfg.lspCommand, "lsp", "", "LSP command to run (args should be passed after --)")
flag.Var(&cfg.openGlobs, "open", "Glob of files to open by default (can specify more than once)")
flag.Parse()

// Get remaining args after -- as LSP arguments
Expand Down Expand Up @@ -99,10 +117,41 @@ func (s *mcpServer) initializeLSP() error {

coreLogger.Debug("Server capabilities: %+v", initResult.Capabilities)

if len(s.config.openGlobs) > 0 {
s.openInitialFiles()
}

go s.workspaceWatcher.WatchWorkspace(s.ctx, s.config.workspaceDir)
return client.WaitForServerReady(s.ctx)
}

func (s *mcpServer) openInitialFiles() {

filepath.WalkDir(s.config.workspaceDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}

if !d.IsDir() {
for _, pattern := range s.config.openGlobs {
match, err := doublestar.PathMatch(pattern, path)
if err != nil {
return err
}

if match {
if err := s.lspClient.OpenFile(s.ctx, path); err != nil {
coreLogger.Error("Failed to open file %s: %v", path, err)
}
break
}
}
}

return nil
})
}

func (s *mcpServer) start() error {
if err := s.initializeLSP(); err != nil {
return err
Expand Down
Loading