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
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;

use rmcp::{
    ErrorData as McpError, Peer, RoleServer, ServerHandler,
    model::{
        CallToolRequestParams, CallToolResponse, CallToolResult, ContentBlock, Implementation,
        InitializeRequestParams, InitializeResult, ListToolsResult, PaginatedRequestParams,
        ServerCapabilities, ServerConfig,
    },
    service::{NotificationContext, RequestContext},
};
use scryer_db::{ProjectRegistry, ScryerDb};
use scryer_engine::EngineService;
use serde_json::Value;

use crate::context::ProjectContextResolver;
use crate::telemetry::{TokenSavingsMiddleware, ToolCategory};
use crate::tools::{
    admin, adr, all_tool_definitions, dependency, graph, inspect, navigation, search,
};

/// Shared state accessible across tool routers and session life-cycle.
#[derive(Clone)]
pub struct ScryerServerState {
    pub db: ScryerDb,
    pub engine: EngineService,
    pub context: ProjectContextResolver,
    pub telemetry: TokenSavingsMiddleware,
    pub session_id: String,
}

/// The Scryer Model Context Protocol (MCP) server.
#[derive(Clone)]
pub struct ScryerMcpServer {
    state: ScryerServerState,
}

impl ScryerMcpServer {
    /// Create a new `ScryerMcpServer` instance.
    pub fn new(
        db: ScryerDb,
        engine: EngineService,
        registry: Arc<ProjectRegistry>,
        session_id: Option<String>,
    ) -> anyhow::Result<Self> {
        let telemetry = TokenSavingsMiddleware::new(db.clone())?;
        let context = ProjectContextResolver::new(registry);
        let session_id = session_id.unwrap_or_else(|| {
            format!(
                "scryer-sess-{}",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_millis()
            )
        });

        Ok(Self {
            state: ScryerServerState {
                db,
                engine,
                context,
                telemetry,
                session_id,
            },
        })
    }

    /// Access inner server state.
    pub fn state(&self) -> &ScryerServerState {
        &self.state
    }

    /// Bind this session to the client's working directory.
    pub async fn set_session_cwd(&self, cwd: PathBuf) {
        self.state.context.set_session_cwd(cwd).await;
    }

    /// Size of a tool's target file, resolving relative paths against the session cwd.
    async fn file_size(&self, file_path: &str) -> Option<usize> {
        let path = self.state.context.absolutize(Path::new(file_path)).await;
        std::fs::metadata(path).ok().map(|meta| meta.len() as usize)
    }

    /// Replace session roots with the client's announced `roots/list`, when supported.
    ///
    /// Roots are deprecated by SEP-2577 but still sent by current clients. The cwd from the
    /// daemon handshake remains the primary source of session context.
    #[allow(deprecated)]
    async fn refresh_session_roots(&self, peer: &Peer<RoleServer>) {
        let supports_roots = peer
            .peer_info()
            .is_some_and(|info| info.capabilities.roots.is_some());
        if !supports_roots {
            return;
        }

        match peer.list_roots().await {
            Ok(result) => {
                let roots: Vec<PathBuf> = result
                    .roots
                    .iter()
                    .filter_map(|root| file_uri_to_path(&root.uri))
                    .collect();
                if !roots.is_empty() {
                    self.state.context.set_session_roots(roots).await;
                }
            }
            Err(e) => tracing::debug!("Client roots/list failed: {e}"),
        }
    }

    /// Execute a tool invocation through the dispatcher with token telemetry interceptor.
    pub async fn dispatch_tool(
        &self,
        name: &str,
        mut arguments: Value,
    ) -> Result<CallToolResponse, McpError> {
        let start_time = Instant::now();
        let tool_name = name.to_string();
        // Agents send "12" for 12 and null for "absent"; make that mean what they intended.
        crate::tools::schema::coerce_arguments(name, &mut arguments);

        let mut file_bytes = None;
        let mut match_count = None;
        let mut project_id = None;

        let max_tokens = arguments
            .get("max_tokens")
            .and_then(|v| v.as_u64())
            .map(|v| v as usize);
        let no_truncate = arguments
            .get("no_truncate")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        // Execute tool within an async block returning anyhow::Result<Value>
        let dispatch_result: anyhow::Result<Value> = async {
            match name {
                // Administration Router (6 tools)
                "list_projects" => {
                    let params: admin::ListProjectsParams = serde_json::from_value(arguments)?;
                    let res =
                        admin::handle_list_projects(&self.state.context, &self.state.db, params)
                            .await?;
                    Ok(serde_json::to_value(res)?)
                }
                "switch_active_project" => {
                    let params: admin::SwitchActiveProjectParams =
                        serde_json::from_value(arguments)?;
                    let res =
                        admin::handle_switch_active_project(&self.state.context, params).await?;
                    project_id = Some(res.active_project_id);
                    Ok(serde_json::to_value(res)?)
                }
                "delete_project" => {
                    let params: admin::DeleteProjectParams = serde_json::from_value(arguments)?;
                    let res = admin::handle_delete_project(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    Ok(serde_json::to_value(res)?)
                }
                "index_workspace" => {
                    let params: admin::IndexWorkspaceParams = serde_json::from_value(arguments)?;
                    let res = admin::handle_index_workspace(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    Ok(serde_json::json!({
                        "scanned_files": res.scanned_files,
                        "added_files": res.added_files,
                        "modified_files": res.modified_files,
                        "unchanged_files": res.unchanged_files,
                        "deleted_files": res.deleted_files,
                        "total_scopes": res.total_scopes,
                        "total_symbols": res.total_symbols,
                        "total_references": res.total_references,
                        "total_edges": res.total_edges,
                    }))
                }
                "get_indexing_status" => {
                    let params: admin::GetIndexingStatusParams = serde_json::from_value(arguments)?;
                    let res = admin::handle_get_indexing_status(
                        &self.state.context,
                        &self.state.db,
                        params,
                    )
                    .await?;
                    project_id = Some(res.project_id);
                    Ok(serde_json::to_value(res)?)
                }
                "get_token_savings_metrics" => {
                    let params: admin::GetTokenSavingsMetricsParams =
                        serde_json::from_value(arguments)?;
                    let res =
                        admin::handle_get_token_savings_metrics(&self.state.telemetry, params)
                            .await?;
                    Ok(serde_json::to_value(res)?)
                }

                // Navigation Router (5 tools)
                "resolve_definition" => {
                    let params: navigation::ResolveDefinitionParams =
                        serde_json::from_value(arguments)?;
                    file_bytes = self.file_size(&params.file_path).await;
                    let res = navigation::handle_resolve_definition(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    Ok(serde_json::to_value(res)?)
                }
                "find_references" => {
                    let params: navigation::FindReferencesParams =
                        serde_json::from_value(arguments)?;
                    if let Some(fp) = &params.file_path {
                        file_bytes = self.file_size(fp).await;
                    }
                    let res = navigation::handle_find_references(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    match_count = Some(res.total_references);
                    Ok(serde_json::to_value(res)?)
                }
                "get_enclosing_scope" => {
                    let params: navigation::GetEnclosingScopeParams =
                        serde_json::from_value(arguments)?;
                    file_bytes = self.file_size(&params.file_path).await;
                    let res = navigation::handle_get_enclosing_scope(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    Ok(serde_json::to_value(res)?)
                }
                "get_file_outline" => {
                    let params: navigation::GetFileOutlineParams =
                        serde_json::from_value(arguments)?;
                    file_bytes = self.file_size(&params.file_path).await;
                    let res = navigation::handle_get_file_outline(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    match_count = Some(res.symbols.len());
                    Ok(serde_json::to_value(res)?)
                }
                "get_type_contract" => {
                    let params: navigation::GetTypeContractParams =
                        serde_json::from_value(arguments)?;
                    if let Some(fp) = &params.file_path {
                        file_bytes = self.file_size(fp).await;
                    }
                    let res = navigation::handle_get_type_contract(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    Ok(serde_json::to_value(res)?)
                }

                // Graph Traversal Router (2 tools)
                "trace_call_hierarchy" => {
                    let params: graph::TraceCallHierarchyParams =
                        serde_json::from_value(arguments)?;
                    let res = graph::handle_trace_call_hierarchy(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    match_count = Some(res.nodes.len());
                    Ok(serde_json::to_value(res)?)
                }
                "calculate_blast_radius" => {
                    let params: graph::CalculateBlastRadiusParams =
                        serde_json::from_value(arguments)?;
                    let res = graph::handle_calculate_blast_radius(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    match_count = Some(res.affected_symbols_count);
                    Ok(serde_json::to_value(res)?)
                }

                // Architectural Memory Router (3 tools)
                "query_adrs" => {
                    let params: adr::QueryAdrsParams = serde_json::from_value(arguments)?;
                    let res =
                        adr::handle_query_adrs(&self.state.context, &self.state.engine, params)
                            .await?;
                    match_count = Some(res.total_matches);
                    Ok(serde_json::to_value(res)?)
                }
                "record_adr" => {
                    let params: adr::RecordAdrParams = serde_json::from_value(arguments)?;
                    let res =
                        adr::handle_record_adr(&self.state.context, &self.state.engine, params)
                            .await?;
                    Ok(serde_json::to_value(res)?)
                }
                "delete_adr" => {
                    let params: adr::DeleteAdrParams = serde_json::from_value(arguments)?;
                    let res =
                        adr::handle_delete_adr(&self.state.context, &self.state.engine, params)
                            .await?;
                    Ok(serde_json::to_value(res)?)
                }

                // Dependency Discovery & Search (2 tools)
                "get_crate_outline" => {
                    let params: dependency::GetCrateOutlineParams =
                        serde_json::from_value(arguments)?;
                    let mut res = dependency::handle_get_crate_outline(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    dependency::fit_crate_outline_to_budget(
                        &mut res,
                        max_tokens,
                        no_truncate,
                        |s| self.state.telemetry.count_tokens(s),
                    );
                    match_count = Some(res.modules.len() + res.core_types.len());
                    Ok(serde_json::to_value(res)?)
                }
                "search_symbols" => {
                    let params: search::SearchSymbolsParams = serde_json::from_value(arguments)?;
                    let res = search::handle_search_symbols(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    match_count = Some(res.total);
                    Ok(serde_json::to_value(res)?)
                }

                // Symbol Inspection (1 tool)
                "inspect_symbol" => {
                    let params: inspect::InspectSymbolParams = serde_json::from_value(arguments)?;
                    let mut res = inspect::handle_inspect_symbol(
                        &self.state.context,
                        &self.state.engine,
                        params,
                    )
                    .await?;
                    inspect::fit_snippet_to_budget(&mut res, max_tokens, no_truncate, |s| {
                        self.state.telemetry.count_tokens(s)
                    });
                    match_count = res.reference_count;
                    Ok(serde_json::to_value(res)?)
                }

                unknown => {
                    anyhow::bail!("Unknown tool: '{unknown}'")
                }
            }
        }
        .await;

        match dispatch_result {
            Ok(val) => {
                let json_str =
                    serde_json::to_string_pretty(&val).unwrap_or_else(|_| "{}".to_string());
                let (limited_payload, payload_tokens, _) =
                    self.state
                        .telemetry
                        .enforce_limit(&json_str, max_tokens, no_truncate);

                let category = ToolCategory::for_tool(&tool_name);
                let naive_tokens =
                    TokenSavingsMiddleware::calculate_naive_cost(category, file_bytes, match_count);

                let active_pid = self.state.context.get_active_project().await;

                // Intercept & persist telemetry asynchronously
                self.state.telemetry.record_invocation(
                    self.state.session_id.clone(),
                    project_id.or(active_pid),
                    tool_name,
                    payload_tokens as u64,
                    naive_tokens,
                    start_time,
                );

                Ok(CallToolResult::success(vec![ContentBlock::text(limited_payload)]).into())
            }
            Err(e) => {
                let err_msg = format!("Tool execution error for '{tool_name}': {e}");
                Ok(CallToolResult::error(vec![ContentBlock::text(err_msg)]).into())
            }
        }
    }
}

impl ServerHandler for ScryerMcpServer {
    fn get_info(&self) -> ServerConfig {
        ServerConfig::new(ServerCapabilities::builder().enable_tools().build())
            .with_server_info(Implementation::new("scryer-mcp", env!("CARGO_PKG_VERSION")))
            .with_instructions(SERVER_INSTRUCTIONS)
    }

    async fn initialize(
        &self,
        request: InitializeRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<InitializeResult, McpError> {
        context.peer.set_peer_info(request.clone());
        self.negotiate_initialize(&request)
    }

    async fn list_tools(
        &self,
        _request: Option<PaginatedRequestParams>,
        _context: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, McpError> {
        Ok(ListToolsResult {
            tools: all_tool_definitions(),
            ..Default::default()
        })
    }

    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        _context: RequestContext<RoleServer>,
    ) -> Result<CallToolResponse, McpError> {
        let args_value = request
            .arguments
            .map(Value::Object)
            .unwrap_or(Value::Object(Default::default()));

        self.dispatch_tool(&request.name, args_value).await
    }

    async fn on_initialized(&self, context: NotificationContext<RoleServer>) {
        self.refresh_session_roots(&context.peer).await;
    }

    async fn on_roots_list_changed(&self, context: NotificationContext<RoleServer>) {
        self.refresh_session_roots(&context.peer).await;
    }
}

/// Convert a `file://` URI into a local path, decoding percent escapes.
fn file_uri_to_path(uri: &str) -> Option<PathBuf> {
    let rest = uri.strip_prefix("file://")?;
    // Skip an optional authority ("file://localhost/path").
    let path = &rest[rest.find('/')?..];

    let bytes = path.as_bytes();
    let mut decoded = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%'
            && i + 2 < bytes.len()
            && let Ok(byte) = u8::from_str_radix(&path[i + 1..i + 3], 16)
        {
            decoded.push(byte);
            i += 3;
        } else {
            decoded.push(bytes[i]);
            i += 1;
        }
    }
    let decoded = String::from_utf8(decoded).ok()?;

    // Windows drive paths arrive as "/C:/...".
    let trimmed = match decoded.as_bytes() {
        [b'/', drive, b':', ..] if drive.is_ascii_alphabetic() => &decoded[1..],
        _ => decoded.as_str(),
    };
    Some(PathBuf::from(trimmed))
}

#[cfg(test)]
mod tests {
    use super::file_uri_to_path;
    use std::path::PathBuf;

    #[test]
    fn file_uris_decode_to_paths() {
        assert_eq!(
            file_uri_to_path("file:///home/me/my%20project"),
            Some(PathBuf::from("/home/me/my project"))
        );
        assert_eq!(
            file_uri_to_path("file://localhost/srv/repo"),
            Some(PathBuf::from("/srv/repo"))
        );
        assert_eq!(file_uri_to_path("https://example.com/x"), None);
    }
}

/// What a client reads at `initialize`: how to choose among the tools and the conventions
/// every tool shares. Keep in step with the decision tree in `scryer init`'s instructions.
pub const SERVER_INSTRUCTIONS: &str = r#"Scryer is code intelligence over a parsed index of Rust, Python and TypeScript projects. Prefer it to grep and whole-file reads: answers come from parsed code, with exact line spans.

Pick a tool by what you already know:
- Don't know the name ("where is the retry logic?"): search_symbols(query: "retry backoff"); like_symbol finds code similar to a known symbol.
- Know the name: inspect_symbol(symbol) returns the definition, a snippet, 1-hop callers/callees, a reference count and related ADRs in one call. Qualify the name (Config::new, tokio::sync::Mutex) or pass file_path when it is ambiguous.
- Know a location (stack trace, diff, compiler error): get_enclosing_scope(file_path, line) names the containing function or type; resolve_definition(file_path, line, col) jumps from a reference to its definition.
- Need structure: get_file_outline(file_path) before reading a file; get_crate_outline(crate_name) for a dependency; get_type_contract(type_name) for a type's fields, variants and methods.
- About to change code: calculate_blast_radius(symbol or file_path) for what depends on it, and query_adrs(query) for decisions and invariants that govern it.
- Going deeper: find_references (every usage), trace_call_hierarchy (multi-hop callers or callees).

Conventions:
- Lines are 1-based, columns 0-based. file_path is project-relative or absolute and must lie inside a registered project.
- `project` is optional and defaults to the session's active project. When you pass it, it must be a project's slug, ID or root path; anything else is an error (list_projects shows them).
- Responses are capped at about 1,000 tokens. Page with offset/limit, narrow with filters (kinds, file_filter, role), or raise max_tokens / set no_truncate.
- A response's `notes` explain why it is empty, and `see_also` names the next call to make. An empty result with a note means the name or path didn't match; it does not mean "no usages".
- If results look stale after edits, run index_workspace."#;

#[cfg(test)]
mod instructions_tests {
    use super::SERVER_INSTRUCTIONS;

    /// Every tool the instructions name must exist, and every tool must be reachable from them.
    #[test]
    fn instructions_name_exactly_the_real_tools() {
        let tools: Vec<String> = crate::all_tool_definitions()
            .iter()
            .map(|t| t.name.to_string())
            .collect();

        // Words that look like tool names (snake_case with an underscore) in the text.
        for word in SERVER_INSTRUCTIONS.split(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) {
            let looks_like_tool = word.contains('_')
                && word.chars().all(|c| c.is_ascii_lowercase() || c == '_')
                && [
                    "search_",
                    "inspect_",
                    "get_",
                    "resolve_",
                    "calculate_",
                    "trace_",
                    "find_",
                    "query_",
                    "list_",
                    "index_",
                ]
                .iter()
                .any(|p| word.starts_with(p));
            if looks_like_tool {
                assert!(
                    tools.iter().any(|t| t == word),
                    "instructions mention unknown tool '{word}'"
                );
            }
        }

        // The administration tools that agents rarely need are deliberately not listed.
        let optional = [
            "switch_active_project",
            "delete_project",
            "get_indexing_status",
            "get_token_savings_metrics",
            "record_adr",
            "delete_adr",
        ];
        for tool in &tools {
            assert!(
                SERVER_INSTRUCTIONS.contains(tool.as_str()) || optional.contains(&tool.as_str()),
                "tool '{tool}' is not reachable from the server instructions"
            );
        }
    }
}