Skip to content

Commit 4388217

Browse files
committed
Add -open flag to trigger initial opening of workspace files
1 parent e439584 commit 4388217

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ This is an [MCP](https://modelcontextprotocol.io/introduction) server that runs
129129
<div>
130130
<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>
131131
<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>
132+
<p><strong>NOTE</strong>: clangd will not resolve symbols until the first file is opened. Use the `-open` argument to trigger indexing.</p>
132133

133134
<pre>
134135
{
@@ -140,6 +141,8 @@ This is an [MCP](https://modelcontextprotocol.io/introduction) server that runs
140141
"/Users/you/dev/yourproject/",
141142
"--lsp",
142143
"/path/to/your/clangd_binary",
144+
"--open",
145+
"/Users/you/dev/yourproject/main.cpp",
143146
"--",
144147
"--compile-commands-dir=/path/to/yourproject/build_or_compile_commands_dir"
145148
]

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/isaacphi/mcp-language-server
33
go 1.24.0
44

55
require (
6+
github.com/bmatcuk/doublestar/v4 v4.8.1
67
github.com/davecgh/go-spew v1.1.1
78
github.com/fsnotify/fsnotify v1.9.0
89
github.com/mark3labs/mcp-go v0.25.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
22
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
3+
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
4+
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
35
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
46
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
57
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"os/exec"
99
"os/signal"
1010
"path/filepath"
11+
"strings"
1112
"syscall"
1213
"time"
1314

15+
"github.com/bmatcuk/doublestar/v4"
1416
"github.com/isaacphi/mcp-language-server/internal/logging"
1517
"github.com/isaacphi/mcp-language-server/internal/lsp"
1618
"github.com/isaacphi/mcp-language-server/internal/watcher"
@@ -23,6 +25,7 @@ var coreLogger = logging.NewLogger(logging.Core)
2325
type config struct {
2426
workspaceDir string
2527
lspCommand string
28+
openGlobs StringArrayFlag
2629
lspArgs []string
2730
}
2831

@@ -35,10 +38,25 @@ type mcpServer struct {
3538
workspaceWatcher *watcher.WorkspaceWatcher
3639
}
3740

41+
// StringArrayFlag is a custom flag type to handle an array of strings
42+
type StringArrayFlag []string
43+
44+
// Set appends a new value to the custom flag value
45+
func (s *StringArrayFlag) Set(value string) error {
46+
*s = append(*s, value)
47+
return nil
48+
}
49+
50+
// String returns the string representation of the custom flag value
51+
func (s *StringArrayFlag) String() string {
52+
return strings.Join(*s, ",")
53+
}
54+
3855
func parseConfig() (*config, error) {
3956
cfg := &config{}
4057
flag.StringVar(&cfg.workspaceDir, "workspace", "", "Path to workspace directory")
4158
flag.StringVar(&cfg.lspCommand, "lsp", "", "LSP command to run (args should be passed after --)")
59+
flag.Var(&cfg.openGlobs, "open", "Glob of files to open by default (can specify more than once)")
4260
flag.Parse()
4361

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

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

120+
if len(s.config.openGlobs) > 0 {
121+
s.openInitialFiles()
122+
}
123+
102124
go s.workspaceWatcher.WatchWorkspace(s.ctx, s.config.workspaceDir)
103125
return client.WaitForServerReady(s.ctx)
104126
}
105127

128+
func (s *mcpServer) openInitialFiles() {
129+
130+
filepath.WalkDir(s.config.workspaceDir, func(path string, d os.DirEntry, err error) error {
131+
if err != nil {
132+
return err
133+
}
134+
135+
if !d.IsDir() {
136+
for _, pattern := range s.config.openGlobs {
137+
match, err := doublestar.PathMatch(pattern, path)
138+
if err != nil {
139+
return err
140+
}
141+
142+
if match {
143+
if err := s.lspClient.OpenFile(s.ctx, path); err != nil {
144+
coreLogger.Error("Failed to open file %s: %v", path, err)
145+
}
146+
break
147+
}
148+
}
149+
}
150+
151+
return nil
152+
})
153+
}
154+
106155
func (s *mcpServer) start() error {
107156
if err := s.initializeLSP(); err != nil {
108157
return err

0 commit comments

Comments
 (0)