scryer-mcp 0.2.1

Model Context Protocol (MCP) server for Scryer code intelligence
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use rmcp::model::Tool;
use scryer_db::{DependencyPackage, Project, ProjectDependency, SourceFile, Symbol};
use scryer_engine::EngineService;

use super::admin::{make_tool, read_only};
use crate::context::ProjectContextResolver;

// --- Input & Output Types ---

#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct GetCrateOutlineParams {
    /// Name of a crate the project depends on, exactly as in Cargo.toml (e.g. "axum", "serde").
    pub crate_name: String,
    /// Version to pick when several are indexed (e.g. "1.0.200"). Default: the one the project uses.
    pub version: Option<String>,
    /// Optional submodule path to narrow the outline (e.g., 'extract' in axum).
    pub module_path: Option<String>,
    /// Optional project slug or path context.
    pub project: Option<String>,
    /// Custom token budget for this response.
    pub max_tokens: Option<usize>,
    /// If true, bypasses token budget truncation.
    pub no_truncate: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct CrateOutlineResult {
    pub found: bool,
    pub crate_name: String,
    pub version: Option<String>,
    pub modules: Vec<String>,
    pub core_types: Vec<String>,
    pub formatted_markdown: String,
    /// Modules and core types left out to fit the token budget.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub omitted: Option<usize>,
}

// --- Tool Handlers ---

/// A symbol definition matched by name, with where its source lives.
#[derive(Debug, Clone)]
pub(crate) struct ResolvedSymbol {
    pub symbol: Symbol,
    /// Project-relative path (workspace) or package-relative path (dependency).
    pub rel_path: Option<String>,
    /// Absolute path of the defining file on disk.
    pub abs_path: Option<PathBuf>,
    /// BLAKE3 hash of the file contents at index time.
    pub content_hash: Option<String>,
    /// The dependency package for symbols from the project 0 cache.
    pub package: Option<DependencyPackage>,
}

impl ResolvedSymbol {
    pub(crate) fn is_external(&self) -> bool {
        self.symbol.project_id == 0
    }

    /// Path shown to agents: project-relative for workspace symbols, absolute for
    /// dependency symbols (they live in the Cargo cache, outside the project).
    pub(crate) fn display_path(&self) -> String {
        if self.is_external()
            && let Some(abs) = &self.abs_path
        {
            return abs.to_string_lossy().into_owned();
        }
        self.rel_path
            .clone()
            .unwrap_or_else(|| "unknown".to_string())
    }
}

/// Symbol kinds that are types (they have members rather than callers).
pub(crate) const TYPE_KINDS: [&str; 6] = ["struct", "enum", "trait", "type", "class", "interface"];

/// One definition offered when a name is ambiguous.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SymbolCandidate {
    pub qualified_name: String,
    pub kind: String,
    /// `path:line` of the definition.
    pub location: String,
}

impl ResolvedSymbol {
    pub(crate) fn candidate(&self) -> SymbolCandidate {
        SymbolCandidate {
            qualified_name: self.symbol.qualified_name.clone(),
            kind: self.symbol.kind.clone(),
            location: format!("{}:{}", self.display_path(), self.symbol.start_line),
        }
    }

    /// Whether this definition lives in `hint` (exact, or either path a suffix of the other
    /// so both relative and absolute hints work).
    pub(crate) fn in_file(&self, hint: &str) -> bool {
        let hint = hint.trim_start_matches("./");
        if hint.is_empty() {
            return false;
        }
        let rel = self.rel_path.as_deref().unwrap_or("");
        let abs = self
            .abs_path
            .as_ref()
            .map(|p| p.to_string_lossy().into_owned())
            .unwrap_or_default();
        (!rel.is_empty() && (rel == hint || hint.ends_with(&format!("/{rel}"))))
            || abs == hint
            || rel.ends_with(&format!("/{hint}"))
    }
}

/// The candidates defined in any of `hints`, or `None` when none are.
pub(crate) fn narrow_by_file(
    candidates: &[ResolvedSymbol],
    hints: &[&str],
) -> Option<Vec<ResolvedSymbol>> {
    let hinted: Vec<ResolvedSymbol> = candidates
        .iter()
        .filter(|c| hints.iter().any(|h| c.in_file(h)))
        .cloned()
        .collect();
    (!hinted.is_empty()).then_some(hinted)
}

/// Crate name a qualified name starts with (`tokio::sync::Mutex` → `tokio`).
pub(crate) fn crate_root(qualified_name: &str) -> &str {
    qualified_name.split("::").next().unwrap_or(qualified_name)
}

/// Full path a `use` stub imports, from its signature: `use tokio::sync::` named `Mutex`
/// (one item of a grouped import) → `tokio::sync::Mutex`. `None` for non-stubs.
pub(crate) fn import_path(stub: &Symbol) -> Option<String> {
    if stub.kind != "reexport" {
        return None;
    }
    let sig = stub.signature.trim().trim_end_matches(';');
    let sig = sig
        .strip_prefix("pub(crate) ")
        .or_else(|| sig.strip_prefix("pub "))
        .unwrap_or(sig);
    let path = sig.strip_prefix("use ")?.trim();
    let path = path.split(" as ").next().unwrap_or(path);
    let path = path.split('{').next().unwrap_or(path).trim();
    if path.ends_with("::") {
        Some(format!("{path}{}", stub.name))
    } else {
        Some(path.to_string())
    }
}

/// The definition a `use` stub imports, when the index has it: a workspace item
/// (`use crate::engine::step` → `…::engine::step`) or a dependency item, including public
/// paths (`use tokio::sync::Mutex` → `tokio::sync::mutex::Mutex`).
pub(crate) async fn follow_import(
    db: &mut toasty::Db,
    project: &Project,
    stub: &Symbol,
) -> anyhow::Result<Option<Symbol>> {
    let Some(path) = import_path(stub) else {
        return Ok(None);
    };
    // `crate::` / `self::` / `super::` are relative to the importing module; drop them and
    // match the rest as a qualified-name suffix.
    let segments: Vec<&str> = path
        .trim_start_matches("::")
        .split("::")
        .skip_while(|s| matches!(*s, "crate" | "self" | "super"))
        .collect();
    if segments.is_empty() {
        return Ok(None);
    }
    let candidates = lookup_symbol_candidates(db, project, &segments.join("::")).await?;
    Ok(candidates
        .into_iter()
        .map(|c| c.symbol)
        .find(|s| s.id != stub.id))
}

/// Whether workspace `use` stub `stub` imports dependency definition `def`. Public paths often
/// differ from definition paths (`use tokio::sync::Mutex` imports
/// `tokio::sync::mutex::Mutex`), so this matches on crate and item name.
pub(crate) fn imports_symbol(stub: &Symbol, def: &Symbol) -> bool {
    import_path(stub).is_some_and(|path| {
        crate_root(&path) == crate_root(&def.qualified_name) && last_segment(&path) == def.name
    })
}

/// Last path segment of a `::`- or `.`-qualified name.
pub(crate) fn last_segment(symbol: &str) -> &str {
    let after_colons = symbol.rsplit("::").next().unwrap_or(symbol);
    after_colons.rsplit('.').next().unwrap_or(after_colons)
}

/// Whether `sym` matches the user-supplied `query` (bare or qualified name).
fn name_matches(sym: &Symbol, query: &str) -> bool {
    if query.contains("::") || query.contains('.') {
        sym.qualified_name == query
            || sym.qualified_name.ends_with(&format!("::{query}"))
            || sym.qualified_name.ends_with(&format!(".{query}"))
    } else {
        sym.name == query
    }
}

/// Every definition of `query` in `project`, falling back to the project 0 dependency cache
/// when the workspace has none. Dependency candidates of crates the project depends on sort
/// first. Re-export stubs are skipped.
pub(crate) async fn lookup_symbol_candidates(
    db: &mut toasty::Db,
    project: &Project,
    query: &str,
) -> anyhow::Result<Vec<ResolvedSymbol>> {
    let target_name = last_segment(query);
    let root = Path::new(&project.root_path);

    let ws_symbols = Symbol::filter(
        Symbol::fields()
            .project_id()
            .eq(project.id)
            .and(Symbol::fields().name().eq(target_name)),
    )
    .exec(&mut *db)
    .await?;

    let mut out = Vec::new();
    for sym in ws_symbols
        .into_iter()
        .filter(|s| s.kind != "reexport" && name_matches(s, query))
    {
        let file = SourceFile::filter(
            SourceFile::fields()
                .project_id()
                .eq(project.id)
                .and(SourceFile::fields().id().eq(sym.file_id)),
        )
        .first()
        .exec(&mut *db)
        .await?;
        out.push(ResolvedSymbol {
            abs_path: file.as_ref().map(|f| root.join(&f.path)),
            rel_path: file.as_ref().map(|f| f.path.clone()),
            content_hash: file.map(|f| f.content_hash),
            package: None,
            symbol: sym,
        });
    }
    if !out.is_empty() {
        return Ok(out);
    }

    // Dependency cache, scoped by the crate prefix when the query is qualified.
    let linked_pkg_ids: Vec<u64> =
        ProjectDependency::filter(ProjectDependency::fields().project_id().eq(project.id))
            .exec(&mut *db)
            .await?
            .into_iter()
            .map(|l| l.dependency_package_id)
            .collect();

    let root_crate = query
        .contains("::")
        .then(|| query.split("::").next())
        .flatten();
    let target_pkg = match root_crate {
        Some(crate_name) => {
            DependencyPackage::filter(DependencyPackage::fields().name().eq(crate_name))
                .exec(&mut *db)
                .await?
                .into_iter()
                .max_by_key(|p| linked_pkg_ids.contains(&p.id))
        }
        None => None,
    };

    let by_name = Symbol::fields()
        .project_id()
        .eq(0)
        .and(Symbol::fields().name().eq(target_name));
    let dep_symbols = match &target_pkg {
        Some(pkg) => {
            Symbol::filter(by_name.and(Symbol::fields().dependency_package_id().eq(Some(pkg.id))))
                .exec(&mut *db)
                .await?
        }
        None => Symbol::filter(by_name).exec(&mut *db).await?,
    };

    let definitions: Vec<Symbol> = dep_symbols
        .into_iter()
        .filter(|s| s.kind != "reexport")
        .collect();
    let mut matches: Vec<Symbol> = definitions
        .iter()
        .filter(|s| name_matches(s, query))
        .cloned()
        .collect();
    // Public paths often differ from definition paths (`tokio::sync::Mutex` is defined at
    // `tokio::sync::mutex::Mutex` and re-exported), so within a named crate fall back to
    // the bare name.
    if matches.is_empty() && target_pkg.is_some() {
        matches = definitions;
    }
    matches.sort_by_key(|s| {
        !s.dependency_package_id
            .is_some_and(|id| linked_pkg_ids.contains(&id))
    });

    for sym in matches {
        let package = match sym.dependency_package_id {
            Some(pkg_id) => {
                DependencyPackage::filter(DependencyPackage::fields().id().eq(pkg_id))
                    .first()
                    .exec(&mut *db)
                    .await?
            }
            None => None,
        };
        let file = SourceFile::filter(
            SourceFile::fields()
                .project_id()
                .eq(0)
                .and(SourceFile::fields().id().eq(sym.file_id)),
        )
        .first()
        .exec(&mut *db)
        .await?;
        let abs_path = match (&package, &file) {
            (Some(pkg), Some(f)) => Some(Path::new(&pkg.root_path).join(&f.path)),
            (None, Some(f)) => Some(PathBuf::from(&f.path)),
            _ => None,
        };
        out.push(ResolvedSymbol {
            abs_path,
            rel_path: file.as_ref().map(|f| f.path.clone()),
            content_hash: file.map(|f| f.content_hash),
            package,
            symbol: sym,
        });
    }
    Ok(out)
}

pub async fn handle_get_crate_outline(
    context: &ProjectContextResolver,
    engine: &EngineService,
    params: GetCrateOutlineParams,
) -> anyhow::Result<CrateOutlineResult> {
    let mut guard = engine.db().lock().await;

    let (project, _) = context
        .resolve_project(None, params.project.as_deref())
        .await?;

    let dep_links =
        ProjectDependency::filter(ProjectDependency::fields().project_id().eq(project.id))
            .exec(&mut *guard)
            .await?;
    let linked_pkg_ids: Vec<u64> = dep_links
        .into_iter()
        .map(|l| l.dependency_package_id)
        .collect();

    let all_pkgs =
        DependencyPackage::filter(DependencyPackage::fields().name().eq(&params.crate_name))
            .exec(&mut *guard)
            .await?;

    let matching_pkg = all_pkgs.into_iter().find(|p| {
        if let Some(v) = &params.version {
            p.version == *v
        } else {
            linked_pkg_ids.contains(&p.id) || true
        }
    });

    let Some(pkg) = matching_pkg else {
        return Ok(CrateOutlineResult {
            found: false,
            crate_name: params.crate_name.clone(),
            version: params.version,
            modules: Vec::new(),
            core_types: Vec::new(),
            formatted_markdown: format!(
                "Crate '{0}' is not indexed in the dependency cache. Only crates the project's Cargo.toml depends on are indexed (run index_workspace after adding one); check the spelling with search_symbols(query: \"{0}\", scope_level: \"dependencies\").",
                params.crate_name
            ),
            omitted: None,
        });
    };

    let symbols = Symbol::filter(
        Symbol::fields()
            .project_id()
            .eq(0)
            .and(Symbol::fields().dependency_package_id().eq(Some(pkg.id))),
    )
    .exec(&mut *guard)
    .await?;

    let mut module_items: BTreeMap<String, Vec<String>> = BTreeMap::new();
    let mut core_types = Vec::new();

    let module_filter = params.module_path.as_deref();

    for s in &symbols {
        if let Some(mf) = module_filter {
            let matches_mod = s.qualified_name.contains(&format!("::{mf}::"))
                || s.qualified_name.ends_with(&format!("::{mf}"))
                || s.qualified_name.starts_with(&format!("{}::{mf}", pkg.name));
            if !matches_mod {
                continue;
            }
        }

        // Group into modules
        if let Some((mod_prefix, _)) = s.qualified_name.rsplit_once("::") {
            let entry = module_items.entry(mod_prefix.to_string()).or_default();
            if entry.len() < 5 && !entry.contains(&s.name) {
                entry.push(s.name.clone());
            }
        }

        // Identify core types
        if matches!(
            s.kind.as_str(),
            "struct" | "enum" | "trait" | "type" | "interface"
        ) {
            let doc_summary = s
                .docstring
                .as_deref()
                .and_then(|d| d.lines().next())
                .unwrap_or("")
                .trim();
            let summary_str = if doc_summary.is_empty() {
                String::new()
            } else {
                format!(": {}", doc_summary.trim_end_matches('.'))
            };
            core_types.push(format!("`{} {}`{}", s.kind, s.qualified_name, summary_str));
        }
    }

    let mut modules_vec = Vec::new();
    for (mod_path, items) in module_items {
        if items.is_empty() {
            modules_vec.push(format!("`{mod_path}`"));
        } else {
            modules_vec.push(format!("`{mod_path}` ({})", items.join(", ")));
        }
    }

    let md = crate_outline_markdown(&pkg.name, &pkg.version, &modules_vec, &core_types);

    Ok(CrateOutlineResult {
        found: true,
        crate_name: pkg.name,
        version: Some(pkg.version),
        modules: modules_vec,
        core_types,
        formatted_markdown: md,
        omitted: None,
    })
}

fn crate_outline_markdown(
    name: &str,
    version: &str,
    modules: &[String],
    core_types: &[String],
) -> String {
    let mut md = format!("# Crate: {name} ({version})\n\n");

    if !modules.is_empty() {
        md.push_str("## Modules\n");
        for m in modules {
            md.push_str(&format!("- {m}\n"));
        }
        md.push('\n');
    }

    if !core_types.is_empty() {
        md.push_str("## Core Types\n");
        for t in core_types {
            md.push_str(&format!("- {t}\n"));
        }
    }
    md
}

/// Shrink a crate outline so the whole response fits the token budget.
///
/// The outline carries every module and type twice (as lists and in `formatted_markdown`)
/// and the markdown is one JSON line, so a big crate would otherwise be cut mid-list by the
/// line-based payload limiter. This keeps the first entries of each list that fit, rebuilds
/// the markdown from them and reports how many were left out; `module_path` narrows to the
/// rest. `count_tokens` is the limiter's tokenizer.
pub fn fit_crate_outline_to_budget(
    out: &mut CrateOutlineResult,
    max_tokens: Option<usize>,
    no_truncate: bool,
    count_tokens: impl Fn(&str) -> usize,
) {
    let budget = max_tokens.unwrap_or(crate::telemetry::MAX_PAYLOAD_TOKENS);
    if no_truncate || budget == 0 || !out.found {
        return;
    }
    let fits = |r: &CrateOutlineResult| {
        serde_json::to_string_pretty(r).is_ok_and(|json| count_tokens(&json) <= budget)
    };
    if fits(out) {
        return;
    }

    let total = out.modules.len() + out.core_types.len();
    let version = out.version.clone().unwrap_or_default();
    let with_share = |n: usize| {
        let mut r = out.clone();
        // Keep the same fraction of both lists so neither crowds out the other.
        let keep = |len: usize| (len * n).div_ceil(total.max(1)).min(len);
        r.modules.truncate(keep(out.modules.len()));
        r.core_types.truncate(keep(out.core_types.len()));
        r.formatted_markdown =
            crate_outline_markdown(&r.crate_name, &version, &r.modules, &r.core_types);
        r.omitted = Some(total - r.modules.len() - r.core_types.len());
        r
    };

    // Largest number of entries (out of `total`) that fits; zero entries is the floor.
    let (mut lo, mut hi) = (0, total);
    while lo < hi {
        let mid = (lo + hi).div_ceil(2);
        if fits(&with_share(mid)) {
            lo = mid;
        } else {
            hi = mid - 1;
        }
    }
    *out = with_share(lo);
}

// --- Tool Definitions ---

pub fn tool_definitions() -> Vec<Tool> {
    vec![make_tool::<GetCrateOutlineParams>(
        "get_crate_outline",
        "Use when exploring a third-party crate for the first time, deciding which module or item to import, or discovering available types and traits, without browsing the Cargo cache or web docs. Pass `crate_name` exactly as in Cargo.toml and narrow large crates with `module_path`; if the crate isn't indexed the reply says how to find the right name.",
        read_only(),
    )]
}

#[cfg(test)]
mod fit_tests {
    use super::*;

    fn outline(modules: usize, types: usize) -> CrateOutlineResult {
        let modules: Vec<String> = (0..modules)
            .map(|i| format!("`krate::m{i}` (a, b)"))
            .collect();
        let core_types: Vec<String> = (0..types)
            .map(|i| format!("`struct krate::T{i}`: does a thing"))
            .collect();
        let md = crate_outline_markdown("krate", "1.0.0", &modules, &core_types);
        CrateOutlineResult {
            found: true,
            crate_name: "krate".into(),
            version: Some("1.0.0".into()),
            modules,
            core_types,
            formatted_markdown: md,
            omitted: None,
        }
    }

    /// Roughly one token per four characters.
    fn tokens(s: &str) -> usize {
        s.len().div_ceil(4)
    }

    #[test]
    fn small_outline_is_untouched() {
        let mut out = outline(2, 3);
        let before = serde_json::to_string(&out).unwrap();
        fit_crate_outline_to_budget(&mut out, None, false, tokens);
        assert_eq!(serde_json::to_string(&out).unwrap(), before);
        assert!(out.omitted.is_none());
    }

    #[test]
    fn large_outline_shrinks_to_fit_with_consistent_markdown() {
        let mut out = outline(200, 200);
        fit_crate_outline_to_budget(&mut out, Some(600), false, tokens);

        let json = serde_json::to_string_pretty(&out).unwrap();
        assert!(tokens(&json) <= 600, "{} tokens", tokens(&json));
        let omitted = out.omitted.expect("entries were left out");
        assert_eq!(omitted, 400 - out.modules.len() - out.core_types.len());
        assert!(!out.modules.is_empty() && !out.core_types.is_empty());
        // The markdown lists exactly what is kept.
        assert_eq!(
            out.formatted_markdown,
            crate_outline_markdown("krate", "1.0.0", &out.modules, &out.core_types)
        );
    }

    #[test]
    fn no_truncate_and_zero_budget_leave_it_alone() {
        for (max, no_truncate) in [(Some(10), true), (Some(0), false)] {
            let mut out = outline(200, 200);
            fit_crate_outline_to_budget(&mut out, max, no_truncate, tokens);
            assert_eq!(out.modules.len(), 200);
            assert!(out.omitted.is_none());
        }
    }
}