symgraph 2026.6.14

Semantic code intelligence library and MCP server - build knowledge graphs of codebases
Documentation
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
//! symgraph: Semantic code intelligence MCP server and CLI.
//!
//! Commands: index, status, search, context, where, prune, serve.
//! Run `symgraph help` for full syntax, arguments, options, and examples.

mod server;

use std::env;

use anyhow::Result;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

use symgraph::cli::{
    context_command, index_command, prune_command, search_command, status_command, tools,
    where_command, OutputFormat,
};

/// First positional argument at `idx` that isn't a `--flag`.
fn positional(args: &[String], idx: usize) -> Option<&str> {
    args.get(idx)
        .map(|s| s.as_str())
        .filter(|s| !s.starts_with("--"))
}

/// Required positional symbol/path argument; prints `usage` and returns None if absent.
fn need(args: &[String], idx: usize, usage: &str) -> Option<String> {
    match positional(args, idx) {
        Some(s) => Some(s.to_string()),
        None => {
            eprintln!("Usage: {usage}");
            None
        }
    }
}

fn flag_value(args: &[String], name: &str) -> Option<String> {
    args.iter()
        .position(|a| a == name)
        .and_then(|i| args.get(i + 1))
        .cloned()
}

fn flag_u32(args: &[String], name: &str) -> Option<u32> {
    flag_value(args, name).and_then(|s| s.parse().ok())
}

fn has_flag(args: &[String], name: &str) -> bool {
    args.iter().any(|a| a == name)
}

fn main() -> Result<()> {
    let mut args: Vec<String> = env::args().collect();

    if args.len() < 2 {
        print_usage();
        return Ok(());
    }

    // Global `--db <path>` override: applies to every command (CLI + serve) by
    // seeding SYMGRAPH_DB, which the path resolver consults first. Strip the
    // flag and its value so the remaining positional arguments are unaffected.
    if let Some(i) = args.iter().position(|a| a == "--db") {
        if i + 1 < args.len() {
            env::set_var("SYMGRAPH_DB", &args[i + 1]);
            args.drain(i..=i + 1);
        }
    }

    // Global `--format <text|json>` for command output (default: text).
    let mut format = OutputFormat::Text;
    if let Some(i) = args.iter().position(|a| a == "--format") {
        match args.get(i + 1).map(|s| OutputFormat::parse(s)) {
            Some(Some(f)) => {
                format = f;
                args.drain(i..=i + 1);
            }
            _ => {
                eprintln!("Invalid or missing value for --format (expected: text | json)");
                std::process::exit(2);
            }
        }
    }

    match args[1].as_str() {
        "serve" => {
            let port = args
                .iter()
                .position(|a| a == "--port")
                .and_then(|i| args.get(i + 1))
                .and_then(|p| p.parse::<u16>().ok());

            // Explicit bind override (e.g. `--bind 0.0.0.0:8080`). Takes
            // precedence over --port when both are given.
            let bind = args
                .iter()
                .position(|a| a == "--bind")
                .and_then(|i| args.get(i + 1))
                .cloned();

            let in_memory = args.iter().any(|a| a == "--in-memory");
            let auth_token = env::var("SYMGRAPH_AUTH_TOKEN")
                .ok()
                .filter(|s| !s.is_empty());

            match (bind, port) {
                (Some(bind), _) => server::start_http(server::HttpConfig {
                    bind,
                    in_memory,
                    auth_token,
                })?,
                (None, Some(port)) => server::start_http(server::HttpConfig {
                    bind: format!("127.0.0.1:{}", port),
                    in_memory,
                    auth_token,
                })?,
                (None, None) => server::start_stdio(in_memory)?,
            }
        }
        "index" => {
            let path = args.get(2).map(|s| s.as_str()).unwrap_or(".");
            // Log to a file beside the index so background indexing never
            // writes into the working tree.
            setup_logging(symgraph::cli::index_log_path(path).ok().as_deref());
            index_command(path, format)?;
        }
        "status" => {
            let path = args.get(2).map(|s| s.as_str()).unwrap_or(".");
            status_command(path, format)?;
        }
        "where" => {
            let path = args.get(2).map(|s| s.as_str()).unwrap_or(".");
            where_command(path, format)?;
        }
        "prune" => {
            prune_command(format)?;
        }
        "search" => {
            if args.len() < 3 {
                eprintln!("Usage: symgraph search <query>");
                eprintln!("  <query> is a symbol name or partial name; quote it if it has spaces.");
                eprintln!(
                    "  e.g. symgraph search authenticate   |   symgraph search \"User Service\""
                );
                return Ok(());
            }
            let path = ".";
            let query = &args[2];
            search_command(path, query, format)?;
        }
        "context" => {
            if args.len() < 3 {
                eprintln!("Usage: symgraph context <task...>");
                eprintln!("  <task...> is a free-text description of what you want to work on.");
                eprintln!("  e.g. symgraph context \"add OAuth login to the REST API\"");
                return Ok(());
            }
            let path = ".";
            let task = args[2..].join(" ");
            context_command(path, &task, format)?;
        }

        // ---- MCP-tool parity: symbol relationships ----
        "callers" => {
            if let Some(s) = need(&args, 2, "symgraph callers <symbol>") {
                tools::callers(".", &s, format)?;
            }
        }
        "callees" => {
            if let Some(s) = need(&args, 2, "symgraph callees <symbol>") {
                tools::callees(".", &s, format)?;
            }
        }
        "node" => {
            if let Some(s) = need(&args, 2, "symgraph node <symbol>") {
                tools::node(".", &s, format)?;
            }
        }
        "references" => {
            if let Some(s) = need(&args, 2, "symgraph references <symbol>") {
                tools::references(".", &s, format)?;
            }
        }
        "definition" => {
            if let Some(s) = need(&args, 2, "symgraph definition <symbol> [--context-lines N]") {
                tools::definition(".", &s, flag_u32(&args, "--context-lines"), format)?;
            }
        }
        "hierarchy" => {
            if let Some(s) = need(&args, 2, "symgraph hierarchy <symbol>") {
                tools::hierarchy(".", &s, format)?;
            }
        }
        "implementations" => {
            if let Some(s) = need(&args, 2, "symgraph implementations <symbol>") {
                tools::implementations(".", &s, format)?;
            }
        }
        "unused" => {
            tools::unused(".", format)?;
        }
        "file" => {
            if let Some(f) = need(&args, 2, "symgraph file <path>") {
                tools::file(".", &f, format)?;
            }
        }
        "path" => match (positional(&args, 2), positional(&args, 3)) {
            (Some(from), Some(to)) => tools::path_between(".", from, to, format)?,
            _ => eprintln!("Usage: symgraph path <from> <to>"),
        },

        // ---- impact / change analysis ----
        "impact" => {
            if let Some(s) = need(&args, 2, "symgraph impact <symbol> [--churn] [--days N]") {
                tools::impact(
                    ".",
                    &s,
                    format,
                    has_flag(&args, "--churn"),
                    flag_u32(&args, "--days"),
                )?;
            }
        }
        "diff-impact" => {
            tools::diff_impact(
                ".",
                flag_value(&args, "--file"),
                flag_u32(&args, "--start"),
                flag_u32(&args, "--end"),
                flag_value(&args, "--git-ref"),
            )?;
        }

        // ---- git history ----
        "blame" => {
            if let Some(s) = need(&args, 2, "symgraph blame <symbol>") {
                tools::blame(".", &s)?;
            }
        }
        "churn" => {
            tools::churn(
                ".",
                positional(&args, 2).map(|s| s.to_string()),
                flag_u32(&args, "--days"),
            )?;
        }

        // ---- coupling & architecture ----
        "module-graph" => {
            tools::module_graph(
                ".",
                flag_value(&args, "--granularity"),
                has_flag(&args, "--churn"),
                flag_u32(&args, "--days"),
                flag_u32(&args, "--limit"),
                format,
            )?;
        }
        "coupling-score" => {
            tools::coupling_score(
                ".",
                flag_value(&args, "--granularity"),
                has_flag(&args, "--churn"),
                flag_u32(&args, "--days"),
                flag_u32(&args, "--limit"),
                format,
            )?;
        }
        "god-struct" => {
            tools::god_struct(
                ".",
                has_flag(&args, "--churn"),
                flag_u32(&args, "--days"),
                flag_u32(&args, "--limit"),
                format,
            )?;
        }
        "dispatch-sites" => {
            if let Some(s) = need(&args, 2, "symgraph dispatch-sites <enum>") {
                tools::dispatch_sites(".", &s, format)?;
            }
        }

        "help" | "--help" | "-h" => {
            print_usage();
        }
        "--version" | "-V" | "version" => {
            print_version();
        }
        cmd => {
            eprintln!("Unknown command: {}", cmd);
            print_usage();
        }
    }

    Ok(())
}

fn print_usage() {
    println!(
        r#"symgraph: Semantic code intelligence — a searchable knowledge graph of your code

USAGE:
    symgraph <COMMAND> [ARGUMENTS] [OPTIONS]

    Build the index once with `symgraph index`, then query it with the commands
    below. The same on-disk index is shared by the CLI and the MCP server.

CORE COMMANDS:
    index [PATH]             Build or refresh the index for a project
    status [PATH]            Show index statistics (files, symbols, languages)
    search <QUERY>           Find symbols whose name matches QUERY
    context <TASK...>        Build focused context for a coding task
    where [PATH]             Show where this project's index is stored
    prune                    Delete cached indexes whose repo no longer exists
    serve [OPTIONS]          Run the MCP server (see SERVE OPTIONS)
    help, version            Show this help / the version

SYMBOL COMMANDS (query the current project's index):
    callers <SYMBOL>         Functions/methods that call SYMBOL
    callees <SYMBOL>         Functions/methods that SYMBOL calls
    references <SYMBOL>      All references to SYMBOL
    node <SYMBOL>            Detailed info about a symbol
    definition <SYMBOL>     Source of SYMBOL        [--context-lines N]
    hierarchy <SYMBOL>      Parent/child (contains) hierarchy
    implementations <SYMBOL> Implementations of an interface/trait
    file <PATH>              List symbols defined in a file
    path <FROM> <TO>         Call path(s) from FROM to TO
    unused                   Symbols with no incoming references (dead code)

ANALYSIS COMMANDS:
    impact <SYMBOL>          Change impact + coupling breakdown  [--churn] [--days N]
    diff-impact              Impact of a region/diff
                             [--file F --start N --end N --git-ref REF]
    blame <SYMBOL>           git blame over a symbol's definition lines
    churn [PATH]             File change frequency (volatility)  [--days N]
    module-graph             Module dependency graph: fan-in/out + cycles
                             [--granularity file|dir|module] [--churn] [--limit N]
    coupling-score           Rank coupling by strength × distance × volatility
                             [--granularity ...] [--churn] [--limit N]
    god-struct               Structs ranked by architectural debt  [--churn] [--limit N]
    dispatch-sites <ENUM>    Files that match/switch on an enum's members

ARGUMENTS:
    <QUERY>      A symbol name or partial name (case-insensitive, prefix/
                 substring match). Returns matching functions, types, methods,
                 etc. with their file:line. Quote it if it contains spaces:
                     symgraph search authenticate
                     symgraph search "User Service"

    <TASK...>    A free-text description of what you want to work on. Every word
                 after the command becomes the task (quotes optional but clearer).
                 symgraph returns the relevant entry points and related code:
                     symgraph context "add OAuth login to the REST API"
                     symgraph context why does indexing skip generated files

    [PATH]       Project root to act on (default: current directory). Point it
                 at another checkout to index/query that one instead:
                     symgraph index ~/code/myapp
                     symgraph status ~/code/myapp

SERVE OPTIONS:
    serve                    stdio transport (for editors / Claude Code)
    serve --port <PORT>      HTTP on 127.0.0.1:<PORT>
    serve --bind <ADDR:PORT> HTTP on an explicit address (e.g. 0.0.0.0:8080)
    serve --in-memory        Ephemeral in-memory index (no filesystem writes)

GLOBAL OPTIONS:
    --format <text|json>    Output format (default: text). `json` emits structured
                            output for scripts/agents (pipe to `jq`). Supported by
                            all commands except blame, churn, and diff-impact.
    --db <PATH>             Use an explicit index database file (any command)

ENVIRONMENT:
    SYMGRAPH_ROOT           Project root directory (default: current directory)
    SYMGRAPH_DB             Explicit index database path (overrides storage)
    SYMGRAPH_STORAGE        Index location strategy: git | cache | local.
                            Default: reuse existing .symgraph/, else the git dir
                            (<git-common-dir>/symgraph), else an OS cache dir.
                            `symgraph index` writes its progress log to
                            index.log in this same directory (never the worktree).
    SYMGRAPH_IN_MEMORY=1    Use in-memory database (same as serve --in-memory)
    SYMGRAPH_AUTH_TOKEN     Bearer token required on /mcp (required for non-
                            loopback binds; optional on 127.0.0.1)

EXAMPLES:
    symgraph index                       # index the current project
    symgraph index ~/projects/myapp      # index a specific project
    symgraph status                      # how much is indexed?
    symgraph search authenticate         # find symbols named like "authenticate"
    symgraph search "User Service"       # quote multi-word queries
    symgraph search auth --format json   # machine-readable output for scripts
    symgraph status --format json        # JSON stats (pipe to jq)
    symgraph context "fix the login bug" # gather context for a task
    symgraph where                       # where is this project's index stored?
    symgraph serve                       # start the MCP server (stdio)
    symgraph serve --port 8080           # start the MCP server over HTTP

NOTE:
    Query commands (status, search, context, where) need an existing index —
    run `symgraph index` first, and re-run it after code changes to refresh.
"#
    );
}

fn print_version() {
    println!("symgraph {}", env!("CARGO_PKG_VERSION"));
}

/// Install the global tracing subscriber. When `log_file` is `Some` and can be
/// created, index progress is written there (co-located with the index, never
/// the working tree); otherwise it falls back to stderr.
fn setup_logging(log_file: Option<&std::path::Path>) {
    let builder = FmtSubscriber::builder()
        .with_max_level(Level::INFO)
        .with_target(false);
    match log_file.and_then(|p| std::fs::File::create(p).ok()) {
        Some(file) => {
            let subscriber = builder
                .with_ansi(false)
                .with_writer(std::sync::Mutex::new(file))
                .finish();
            tracing::subscriber::set_global_default(subscriber).ok();
        }
        None => {
            let subscriber = builder.with_writer(std::io::stderr).finish();
            tracing::subscriber::set_global_default(subscriber).ok();
        }
    }
}