Repository navigation
Expand file tree
/
Copy pathcommand_mcp.go
More file actions
586 lines (538 loc) · 20.4 KB
/
Copy pathcommand_mcp.go
File metadata and controls
586 lines (538 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package contexting
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/spf13/cobra"
)
// MCP search tool input.
type searchToolArgs struct {
Query string `json:"query" jsonschema:"Search query - keywords, filenames, or concepts"`
Limit int `json:"limit,omitempty" jsonschema:"Max results (default 10)"`
Type string `json:"type,omitempty" jsonschema:"Filter: all, files, or dirs"`
Explain bool `json:"explain,omitempty" jsonschema:"Include score breakdown"`
Hybrid bool `json:"hybrid,omitempty" jsonschema:"Enable content fallback via ripgrep when index results are sparse"`
}
// MCP status tool has no input.
type statusToolArgs struct{}
// defaultMCPLimit and maxMCPResults bound the search tool's result count so a client
// cannot dump the whole index (or an unbounded response) into an agent's context.
const (
defaultMCPLimit = 10
maxMCPResults = 50
)
// searchToolSchema is the JSON Schema for the search tool's input. It is hand-written
// rather than inferred because the go-sdk's inferred schemas only carry field
// descriptions; enum and maximum constraints require an explicit schema.
var searchToolSchema = map[string]any{
"type": "object",
"properties": map[string]any{
"query": map[string]any{
"type": "string",
"description": "Search query - keywords, filenames, or concepts",
},
"limit": map[string]any{
"type": "integer",
"description": "Max results (default 10, maximum 50)",
"default": defaultMCPLimit,
"minimum": 1,
"maximum": maxMCPResults,
},
"type": map[string]any{
"type": "string",
"description": "Filter: all, files, or dirs",
"enum": []any{"all", "files", "dirs"},
},
"explain": map[string]any{
"type": "boolean",
"description": "Include score breakdown",
},
"hybrid": map[string]any{
"type": "boolean",
"description": "Enable content fallback via ripgrep when index results are sparse",
},
},
"required": []any{"query"},
}
// registerMCPSearchTool exposes concept-based ranked file search over MCP. Search is
// strictly read-only (the manager keeps the index in memory), so it is annotated
// read-onlyHint to let clients auto-approve it.
func registerMCPSearchTool(server *mcp.Server, manager *IndexManager) {
mcp.AddTool(server, &mcp.Tool{
Name: "search",
Description: "Search a codebase for files using concept-based ranked search. Faster and more relevant than grep or find for locating WHERE code lives. " +
"Query with plain keywords, concepts, partial filenames, or symbol names (e.g. \"auth login\", \"jwt token refresh\", \"payment handler\", \"createUser\"). " +
"Results are ranked by relevance score (exact basename +15 and exact symbol +24 rank highest; partial path and basename matches add +4..+7; synonym matches add +5..+8). " +
"The live index updates automatically as files change, so no separate watch or init step is required while this server is running. " +
"Set hybrid=true to enable content fallback via ripgrep when index results are sparse. " +
"Use this instead of grep when you need to find which file handles a concept — grep finds what's INSIDE files, this finds WHICH files matter. " +
"Do not use for searching file contents (use grep) or file metadata like size/date/permissions (use find).",
InputSchema: searchToolSchema,
Annotations: &mcp.ToolAnnotations{ReadOnlyHint: true},
}, searchToolHandler(manager))
}
func searchToolHandler(manager *IndexManager) func(ctx context.Context, req *mcp.CallToolRequest, args searchToolArgs) (*mcp.CallToolResult, any, error) {
return func(ctx context.Context, req *mcp.CallToolRequest, args searchToolArgs) (*mcp.CallToolResult, any, error) {
if args.Query == "" {
return mcpErrorResult("missing required argument: query"), nil, nil
}
limit := args.Limit
if limit <= 0 {
limit = defaultMCPLimit
}
if limit > maxMCPResults {
limit = maxMCPResults
}
typeFilter := args.Type
if typeFilter == "" {
typeFilter = "all"
}
switch typeFilter {
case "all", "files", "dirs":
default:
return mcpErrorResult(fmt.Sprintf("invalid type %q; must be one of all, files, or dirs", typeFilter)), nil, nil
}
results := manager.Search(args.Query, SearchOptions{
Limit: limit,
MinScore: 1,
TypeFilter: typeFilter,
IncludeDebug: args.Explain,
ContentFallback: args.Hybrid,
})
return &mcp.CallToolResult{
Content: []mcp.Content{mcpTextResult(formatMCPSearchResults(results, args.Explain))},
}, nil, nil
}
}
func registerMCPStatusTool(server *mcp.Server, manager *IndexManager) {
mcp.AddTool(server, &mcp.Tool{
Name: "status",
Description: "Report the health and coverage of the ctxt codebase index. " +
"Returns total indexed files, directories, index generation time, and root path. " +
"The server keeps an in-memory index current as files change; use this to check what the live index covers before searching. " +
"This tool is read-only and never modifies project state.",
Annotations: &mcp.ToolAnnotations{ReadOnlyHint: true},
}, func(ctx context.Context, req *mcp.CallToolRequest, _ statusToolArgs) (*mcp.CallToolResult, any, error) {
stats := manager.SnapshotStats()
root := manager.RootPath()
generatedAt := manager.IndexGeneratedAt()
var sb strings.Builder
fmt.Fprintf(&sb, "Root: %s\n", root)
fmt.Fprintf(&sb, "Total nodes: %d\n", stats.TotalNodes)
fmt.Fprintf(&sb, "Files: %d\n", stats.TotalFiles)
fmt.Fprintf(&sb, "Directories: %d\n", stats.TotalDirs)
fmt.Fprintf(&sb, "Generated: %s\n", generatedAt.Format(time.RFC3339))
return &mcp.CallToolResult{
Content: []mcp.Content{mcpTextResult(sb.String())},
}, nil, nil
})
}
func mcpErrorResult(text string) *mcp.CallToolResult {
return &mcp.CallToolResult{
IsError: true,
Content: []mcp.Content{mcpTextResult(text)},
}
}
func mcpTextResult(text string) *mcp.TextContent {
return &mcp.TextContent{Text: text}
}
func formatMCPSearchResults(results []SearchResult, explain bool) string {
var sb strings.Builder
if len(results) == 0 {
sb.WriteString("No results found.")
return sb.String()
}
for i, r := range results {
fmt.Fprintf(&sb, "%d. %s (score: %d, type: %s)", i+1, r.Path, r.Score, r.Type)
if len(r.Matches) > 0 {
fmt.Fprintf(&sb, " matches: %s", strings.Join(r.Matches, ", "))
}
sb.WriteString("\n")
if explain && len(r.Breakdown) > 0 {
for _, b := range r.Breakdown {
fmt.Fprintf(&sb, " - %s\n", b)
}
}
}
return sb.String()
}
func newMCPCommand() *cobra.Command {
flags := CommonFlags{}
var debounce time.Duration
var llmOnWatch bool
var persist string
var persistInterval time.Duration
var searchLog bool
var searchLogQueryMax int
var maxBatchSize int
var enableHTTP bool
cmd := &cobra.Command{
Use: "mcp [path]",
Short: "Start MCP server — watch directory and expose search+status tools over stdio for AI assistants",
Long: `Starts a Model Context Protocol (MCP) server that AI assistants (Claude Desktop, Cursor, etc.) can use to search your codebase.
Exposes two tools:
search - Concept-based ranked file search using the precomputed index (symbols, synonyms, paths).
status - Index health check (file count, generation time, root path).
The server watches for file changes and keeps the index current. All communication is via stdin/stdout JSON-RPC — no network, fully local.
Pass the project's absolute path so the server targets the right project no matter what working directory the AI client launches it from. If several ctxt servers run on one project, the first becomes the writer and later ones serve read-only (live in-memory search, no snapshot writes).
Setup: Add to your AI client's MCP config:
ctxt mcp setup . Configure detected local clients and start the server
{"command": "ctxt", "args": ["mcp", "/abs/path/to/project"]}`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// MCP uses stdout for JSON-RPC; all logging must go to stderr.
logToStderr = true
rootArg := ""
if len(args) == 1 {
rootArg = args[0]
}
absConfigPath, err := resolveStartupConfigPath(configPath, rootArg, cmd.Flags().Changed("config"))
if err != nil {
return err
}
if absConfigPath != "" && !cmd.Flags().Changed("config") {
if _, statErr := os.Stat(absConfigPath); os.IsNotExist(statErr) {
LogWarnf("Config file not found at %s; using defaults.", absConfigPath)
}
}
cfg, err := LoadContextingConfig(absConfigPath)
if err != nil {
return err
}
applyCommonConfig(cmd, &flags, cfg.Common)
if flags.SymbolExtractor != "" {
SymbolsExtractorMode = flags.SymbolExtractor
}
if cfg.Watch.UseLLM != nil && !cmd.Flags().Changed("llm-on-watch") {
llmOnWatch = *cfg.Watch.UseLLM
}
if d, err := cfg.Watch.DebounceDuration(); err != nil {
return err
} else if d > 0 && !cmd.Flags().Changed("debounce") {
debounce = d
}
if cfg.Watch.Persist != "" && !cmd.Flags().Changed("persist") {
persist = cfg.Watch.Persist
}
if d, err := cfg.Watch.PersistIntervalDuration(); err != nil {
return err
} else if d > 0 && !cmd.Flags().Changed("persist-interval") {
persistInterval = d
}
if cfg.Watch.SearchLog != nil {
applyBoolFlag(cmd, "search-log", &searchLog, *cfg.Watch.SearchLog)
}
applyIntFlag(cmd, "search-log-query-max", &searchLogQueryMax, cfg.Watch.SearchLogQueryMax)
applyIntFlag(cmd, "max-batch-size", &maxBatchSize, cfg.Watch.MaxBatchSize)
flags.normalize()
persistMode, err := parsePersistMode(persist)
if err != nil {
return err
}
if persistMode != PersistShutdown {
return fmt.Errorf("persistence mode %q is unsupported; use --persist=shutdown", persistMode)
}
if persistInterval <= 0 {
persistInterval = 45 * time.Second
}
rootPath := "."
if rootArg != "" {
rootPath = rootArg
} else if cfg.Watch.RootPath != "" {
rootPath = cfg.Watch.RootPath
}
absRoot, err := filepath.Abs(rootPath)
if err != nil {
return fmt.Errorf("resolve mcp path: %w", err)
}
releaseWriter, writeMode, err := acquireWriterGuardAllowShared(absRoot)
if err != nil {
return err
}
if writeMode {
defer releaseWriter()
}
outputPath := resolveProjectPath(absRoot, flags.OutputPath)
if _, statErr := os.Stat(outputPath); os.IsNotExist(statErr) {
return fmt.Errorf("no index found at %s; run 'ctxt init' from the project root first", outputPath)
}
cachePath := resolveProjectPath(absRoot, flags.SynonymCache)
runtimeFile := resolveProjectPath(absRoot, ".ctxt/ctx_runtime.json")
ignored, err := BuildIgnoreMapForRoot(absRoot, flags.ExtraIgnores)
if err != nil {
return err
}
EmbedDotWhitelist(ignored, BuildDotWhitelist(cfg.Common.DotWhitelist))
if isInsideProject(absConfigPath, absRoot) {
ignored[filepath.Base(absConfigPath)] = true
ignored[filepath.Base(absConfigPath)+".example"] = true
}
if isInsideProject(outputPath, absRoot) {
ignored[filepath.Base(outputPath)] = true
}
llmEndpoint, llmModel, llmKey, llmTemp, llmMaxTokens, llmProvider := resolveLLMConfig(flags, cfg.LLM)
LogInfof("LLM: provider=%s model=%s endpoint=%s api_key=%s", llmProvider, llmModel, endpointForLog(llmEndpoint), maskAPIKey(llmKey))
if !llmOnWatch {
llmKey = ""
LogInfof("MCP LLM mode is off. Using cache + lexical synonyms only.")
}
if llmOnWatch && llmKey == "" {
LogInfof("LLM API key not configured. Using cache + lexical synonyms only.")
}
ctx, stop := signalAwareContext()
defer stop()
manager := NewIndexManager(IndexManagerOptions{
RootPath: absRoot,
OutputPath: outputPath,
CachePath: cachePath,
IgnoredPaths: ignored,
Model: llmModel,
BatchSize: flags.BatchSize,
SynonymsPerName: flags.SynonymsPerName,
SynonymsMax: flags.SynonymsMax,
APIKey: llmKey,
UseLLM: llmOnWatch && llmKey != "",
MaxBatchSize: maxBatchSize,
Endpoint: llmEndpoint,
Temperature: llmTemp,
MaxTokens: llmMaxTokens,
})
bootstrapStats, err := manager.Bootstrap(ctx)
if err != nil {
if isCanceledError(err) {
LogInfof("Startup indexing canceled.")
return nil
}
return err
}
LogInfof("In-memory index ready: %d nodes (%d files, %d directories).", bootstrapStats.TotalNodes, bootstrapStats.TotalFiles, bootstrapStats.TotalDirs)
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("create watcher: %w", err)
}
defer watcher.Close()
watchedDirs := make(map[string]struct{})
if err := syncWatchDirectories(watcher, absRoot, ignored, watchedDirs); err != nil {
return err
}
LogInfof("Watching %s for changes...", absRoot)
LogInfof("MCP settings: debounce=%s verbose=%t persist=%s output=%s cache=%s http=%t", debounce.String(), flags.Verbose, persistMode, outputPath, cachePath, enableHTTP)
var memoryServer *memorySearchServer
if enableHTTP {
if searchLogQueryMax <= 0 {
searchLogQueryMax = defaultSearchLogQueryMax
}
var err error
memoryServer, err = startMemorySearchServer(ctx, manager, runtimeFile, MemorySearchLogOptions{
Enabled: searchLog,
QueryMax: searchLogQueryMax,
})
if err != nil {
return err
}
defer func() {
_ = memoryServer.Close()
}()
LogInfof("Memory search endpoint ready at %s", memoryServer.Address())
}
var persistTicker *time.Ticker
if persistMode == PersistInterval {
persistTicker = time.NewTicker(persistInterval)
defer persistTicker.Stop()
LogInfof("Periodic flush enabled: interval=%s", persistInterval.String())
}
pendingChanges := make(map[string]fsnotify.Op)
var pendingMu sync.Mutex
applyTrigger := make(chan struct{}, 1)
drainPending := func() map[string]fsnotify.Op {
pendingMu.Lock()
defer pendingMu.Unlock()
if len(pendingChanges) == 0 {
return nil
}
copyMap := make(map[string]fsnotify.Op, len(pendingChanges))
for path, op := range pendingChanges {
copyMap[path] = op
}
pendingChanges = make(map[string]fsnotify.Op)
return copyMap
}
addPending := func(path string, op fsnotify.Op) {
pendingMu.Lock()
pendingChanges[path] = pendingChanges[path] | op
pendingMu.Unlock()
}
enqueueApply := func() {
select {
case applyTrigger <- struct{}{}:
default:
}
}
go func() {
for {
select {
case <-ctx.Done():
return
case <-applyTrigger:
changes := drainPending()
if len(changes) == 0 {
continue
}
result, applyErr := manager.ApplyChanges(ctx, changes)
if applyErr != nil {
if !isCanceledError(applyErr) {
LogErrorf("Apply changes failed: %v", applyErr)
}
continue
}
emitSynonymWarning(result.SynonymError)
if result.Changed {
if flags.Verbose {
logChangeSummary(result.ChangedPaths, flags.Verbose)
LogInfof("In-memory index updated: %d nodes (%d files, %d directories).", result.Stats.TotalNodes, result.Stats.TotalFiles, result.Stats.TotalDirs)
}
if persistMode == PersistChange {
flushed, flushErr := manager.FlushIfDirty()
if flushErr != nil {
LogErrorf("Change-triggered flush failed: %v", flushErr)
} else if flushed && flags.Verbose {
LogInfof("Saved snapshot after change to %s", outputPath)
}
}
}
}
}
}()
// Run one startup apply trigger to process any pending setup events quickly.
enqueueApply()
dirty := false
timer := time.NewTimer(debounce)
if !timer.Stop() {
<-timer.C
}
go func() {
for {
select {
case <-ctx.Done():
return
case err := <-watcher.Errors:
if err != nil {
LogErrorf("Watcher error: %v", err)
}
case event, ok := <-watcher.Events:
if !ok {
continue
}
if shouldSkipEvent(absRoot, event, ignored, outputPath, cachePath, absConfigPath) {
continue
}
op := event.Op &^ fsnotify.Chmod
if op == 0 {
continue
}
relName := event.Name
if rel, relErr := filepath.Rel(absRoot, event.Name); relErr == nil {
relName = rel
}
addPending(relName, op)
if flags.Verbose {
LogInfof("Event: %s %s", op, event.Name)
}
if op&(fsnotify.Create|fsnotify.Rename) != 0 {
if err := syncWatchDirectories(watcher, absRoot, ignored, watchedDirs); err != nil {
LogErrorf("Sync watch dirs failed: %v", err)
}
}
dirty = true
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
timer.Reset(debounce)
case <-tickerChan(persistTicker):
flushed, flushErr := manager.FlushIfDirty()
if flushErr != nil {
LogErrorf("Periodic flush failed: %v", flushErr)
continue
}
if flushed {
LogInfof("Periodic flush wrote snapshot to %s", outputPath)
}
case <-timer.C:
if !dirty {
continue
}
dirty = false
enqueueApply()
}
}
}()
server := mcp.NewServer(&mcp.Implementation{
Name: "ctxt",
Version: currentVersion(),
}, nil)
registerMCPSearchTool(server, manager)
registerMCPStatusTool(server, manager)
LogInfof("MCP server ready on stdio.")
serverErr := server.Run(ctx, &mcp.StdioTransport{})
remaining := drainPending()
if len(remaining) > 0 {
result, applyErr := manager.ApplyChanges(context.Background(), remaining)
if applyErr != nil {
LogErrorf("Final apply failed: %v", applyErr)
} else {
emitSynonymWarning(result.SynonymError)
if result.Changed && flags.Verbose {
logChangeSummary(result.ChangedPaths, true)
}
}
}
if writeMode {
flushed, flushErr := manager.FlushIfDirty()
if flushErr != nil {
LogErrorf("Failed to flush snapshot on shutdown: %v", flushErr)
}
if flushed {
LogInfof("Flushed snapshot to %s and %s", outputPath, cachePath)
}
} else {
LogInfof("Read-only MCP server: skipping snapshot flush.")
}
LogInfof("Stopping MCP server.")
if serverErr != nil {
return fmt.Errorf("mcp server: %w", serverErr)
}
return nil
},
}
cmd.Flags().StringVarP(&flags.OutputPath, "output", "o", ".ctxt/ctx_index.json", "Output JSON path")
cmd.Flags().StringVar(&flags.Model, "llm-model", "", "LLM model used for synonym generation")
cmd.Flags().StringVar(&flags.APIKey, "api-key", "", "LLM API key (falls back to config api_key_env, LLM_API_KEY, OPENROUTER_API_KEY)")
cmd.Flags().StringVar(&flags.Endpoint, "llm-endpoint", "", "LLM API endpoint URL")
cmd.Flags().IntVar(&flags.BatchSize, "batch-size", 0, "Names per LLM request (0 = automatic, up to 60)")
cmd.Flags().IntVar(&maxBatchSize, "max-batch-size", 0, "Maximum names per LLM request (0 = send all at once, default)")
cmd.Flags().IntVar(&flags.SynonymsPerName, "synonyms", defaultSynonyms, "Desired synonyms per name")
cmd.Flags().IntVar(&flags.SynonymsMax, "synonyms-max", 0, "Max search phrases per name (0 = default 12)")
cmd.Flags().StringVar(&flags.SynonymCache, "synonym-cache", ".ctxt/ctx_cache.json", "Path to persistent synonym cache JSON")
cmd.Flags().StringSliceVar(&flags.ExtraIgnores, "ignore", nil, "Additional ignore entries (name or relative path)")
cmd.Flags().StringVar(&flags.SymbolExtractor, "symbol-extractor", "auto", "Symbol extraction engine: auto, treesitter, regex")
cmd.Flags().BoolVarP(&flags.Verbose, "verbose", "v", false, "Enable verbose logging")
cmd.Flags().DurationVar(&debounce, "debounce", defaultDebounce, "Debounce interval for coalescing fs events")
cmd.Flags().BoolVar(&llmOnWatch, "llm-on-watch", true, "Generate live LLM synonyms when an API key is configured; set false to disable")
cmd.Flags().StringVar(&persist, "persist", string(PersistShutdown), "Persistence mode (only shutdown is supported)")
cmd.Flags().DurationVar(&persistInterval, "persist-interval", defaultPersistInterval, "Reserved for compatibility; unused with shutdown persistence")
_ = cmd.Flags().MarkHidden("persist-interval")
cmd.Flags().BoolVar(&searchLog, "search-log", false, "Log incoming queries (may contain sensitive data)")
cmd.Flags().IntVar(&searchLogQueryMax, "search-log-query-max", defaultSearchLogQueryMax, "Maximum query characters shown in search logs")
cmd.Flags().BoolVar(&enableHTTP, "http", false, "Also serve HTTP memory search endpoint alongside MCP")
cmd.AddCommand(newMCPSetupCommand(cmd))
return cmd
}