lean-host-mcp 0.4.1

MCP server hosting Lean 4 via a supervised lean-rs-worker child
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
//! Private `lean-semantic-search` runtime adapter.
//!
//! The MCP tool surface stays proof-agent shaped. This module hides the
//! downstream capability export names, command envelopes, feature rows, and
//! retrieval policy behind one operation that returns candidate declarations
//! plus key-free evidence labels.

use std::collections::HashMap;

use lean_rs_worker_parent::{LeanWorkerJsonCommand, LeanWorkerSession};
use lean_semantic_search_contract::{
    CAPABILITY_SCHEMA_VERSION, CommandResponse, DECLARATION_FEATURE_COMMAND_VERSION, DeclarationFeatureRow, Diagnostic,
    DiagnosticSeverity, ModuleSpec, PROOF_GOAL_FEATURE_COMMAND_VERSION, ProofGoalFeatureRequest, ProofGoalFeatureRow,
    SEMANTIC_FEATURE_VERSION,
};
use lean_semantic_search_retrieval::{Anchor, SemanticIndex, retrieve_across};
use serde::Serialize;

use crate::error::{Result, ServerError};
use crate::projections::SourceRange;

/// Source-backed semantic proof-search request built by `search_for_proof`.
#[derive(Debug, Clone)]
pub(crate) struct SemanticProofSearchRequest {
    pub(crate) goal: ProofGoalFeatureRequest,
    pub(crate) candidate_modules: Vec<String>,
    pub(crate) limit: usize,
}

/// Candidate declaration returned after storage-neutral semantic retrieval.
#[derive(Debug, Clone)]
pub(crate) struct SemanticProofCandidate {
    pub(crate) name: String,
    pub(crate) module: Option<String>,
    pub(crate) source: Option<SourceRange>,
    pub(crate) score: i32,
    pub(crate) evidence: Vec<String>,
}

/// Semantic proof-search result, with public-safe diagnostic strings.
#[derive(Debug, Clone)]
pub(crate) struct SemanticProofSearchResult {
    pub(crate) candidates: Vec<SemanticProofCandidate>,
    pub(crate) diagnostics: Vec<String>,
    pub(crate) declaration_rows: usize,
    pub(crate) goal_rows: usize,
}

type DeclarationResponse = CommandResponse<DeclarationFeatureRow>;
type ProofGoalResponse = CommandResponse<ProofGoalFeatureRow>;

#[derive(Debug, Serialize)]
struct DeclarationFeatureCommandRequest {
    modules: Vec<ModuleSpec>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    declaration_ids: Vec<String>,
}

/// Run source-backed semantic proof search in one manifest-backed worker session.
///
/// # Errors
///
/// Returns [`ServerError::Lean`] for worker command failures and
/// [`ServerError::Internal`] for malformed semantic-search envelopes.
pub(crate) fn run_semantic_proof_search(
    session: &mut LeanWorkerSession<'_>,
    request: &SemanticProofSearchRequest,
) -> Result<SemanticProofSearchResult> {
    let goal_command = LeanWorkerJsonCommand::<ProofGoalFeatureRequest, ProofGoalResponse>::new(
        lean_semantic_search_capability::PROOF_GOAL_FEATURES_EXPORT,
    );
    let declaration_command = LeanWorkerJsonCommand::<DeclarationFeatureCommandRequest, DeclarationResponse>::new(
        lean_semantic_search_capability::DECLARATION_FEATURES_EXPORT,
    );

    let goal_response = session
        .run_json_command(&goal_command, &request.goal, None, None)
        .map_err(crate::error::map_worker_err)?;
    validate_response(
        &goal_response,
        lean_semantic_search_capability::PROOF_GOAL_FEATURES_COMMAND,
        PROOF_GOAL_FEATURE_COMMAND_VERSION,
    )?;

    let declaration_request = DeclarationFeatureCommandRequest {
        modules: request
            .candidate_modules
            .iter()
            .map(|module| ModuleSpec {
                module: module.clone(),
                origin: Some("lean-host-mcp".to_owned()),
                source_root: None,
            })
            .collect(),
        declaration_ids: Vec::new(),
    };
    let declaration_response = session
        .run_json_command(&declaration_command, &declaration_request, None, None)
        .map_err(crate::error::map_worker_err)?;
    validate_response(
        &declaration_response,
        lean_semantic_search_capability::DECLARATION_FEATURES_COMMAND,
        DECLARATION_FEATURE_COMMAND_VERSION,
    )?;

    Ok(rank_semantic_rows(&goal_response, &declaration_response, request.limit))
}

fn validate_response<Row>(response: &CommandResponse<Row>, command: &str, command_version: &str) -> Result<()> {
    if response.schema_version != CAPABILITY_SCHEMA_VERSION {
        return Err(ServerError::Internal(format!(
            "semantic search returned schema version {}, expected {}",
            response.schema_version, CAPABILITY_SCHEMA_VERSION
        )));
    }
    if response.command != command {
        return Err(ServerError::Internal(format!(
            "semantic search returned command {}, expected {}",
            response.command, command
        )));
    }
    if response.command_version != command_version {
        return Err(ServerError::Internal(format!(
            "semantic search returned command version {}, expected {}",
            response.command_version, command_version
        )));
    }
    if response.feature_version != SEMANTIC_FEATURE_VERSION {
        return Err(ServerError::Internal(format!(
            "semantic search returned feature version {}, expected {}",
            response.feature_version, SEMANTIC_FEATURE_VERSION
        )));
    }
    Ok(())
}

fn rank_semantic_rows(
    goal_response: &ProofGoalResponse,
    declaration_response: &DeclarationResponse,
    limit: usize,
) -> SemanticProofSearchResult {
    let mut diagnostics = Vec::new();
    diagnostics.extend(diagnostic_strings(&goal_response.diagnostics));
    diagnostics.extend(diagnostic_strings(&declaration_response.diagnostics));

    let Some(goal) = goal_response.rows.first() else {
        return SemanticProofSearchResult {
            candidates: Vec::new(),
            diagnostics,
            declaration_rows: declaration_response.rows.len(),
            goal_rows: 0,
        };
    };
    if declaration_response.rows.is_empty() {
        return SemanticProofSearchResult {
            candidates: Vec::new(),
            diagnostics,
            declaration_rows: 0,
            goal_rows: goal_response.rows.len(),
        };
    }

    let source_by_id = declaration_response
        .rows
        .iter()
        .map(|row| (row.declaration_id.clone(), row.source.map(source_range)))
        .collect::<HashMap<_, _>>();
    let index = SemanticIndex::from_declarations(&declaration_response.rows);
    let anchor = Anchor::from_proof_goal(goal);
    let retrieval = retrieve_across(&[&index], &anchor, limit);
    diagnostics.extend(diagnostic_strings(&retrieval.diagnostics));

    let candidates = retrieval
        .candidates
        .into_iter()
        .map(|candidate| {
            let rank = i32::try_from(candidate.rank).unwrap_or(i32::MAX);
            let evidence = candidate
                .explanations
                .into_iter()
                .map(|explanation| format!("semantic:{}:{}", explanation.family.label(), explanation.match_count))
                .collect::<Vec<_>>();
            let source = source_by_id.get(&candidate.declaration_id).cloned().flatten();
            let (module, name) = split_declaration_id(&candidate.declaration_id);
            SemanticProofCandidate {
                name,
                module,
                source,
                // Keep semantic candidates above lexical-only fallback while
                // still allowing proof-agent boosts/penalties to reorder them.
                score: 150_i32.saturating_sub(rank.saturating_mul(4)),
                evidence,
            }
        })
        .collect();

    SemanticProofSearchResult {
        candidates,
        diagnostics,
        declaration_rows: declaration_response.rows.len(),
        goal_rows: goal_response.rows.len(),
    }
}

fn diagnostic_strings(diagnostics: &[Diagnostic]) -> Vec<String> {
    diagnostics
        .iter()
        .filter(|diagnostic| !matches!(diagnostic.severity, DiagnosticSeverity::Pass))
        .map(|diagnostic| {
            format!(
                "{}:{}:{}",
                severity_label(diagnostic.severity),
                diagnostic.code,
                diagnostic.message
            )
        })
        .collect()
}

const fn severity_label(severity: DiagnosticSeverity) -> &'static str {
    match severity {
        DiagnosticSeverity::Pass => "pass",
        DiagnosticSeverity::Warning => "warning",
        DiagnosticSeverity::Error => "error",
    }
}

/// Split a downstream `origin:module:declName` declaration id into its module
/// and the clean Lean declaration name.
///
/// The extractor builds ids as `s!"{origin}:{module}:{declName}"`, and none of
/// the three parts contains a colon (Lean names use `.`, the origin is a fixed
/// label). An id that does not match that shape is returned whole as the name,
/// so a future format change degrades to a still-usable name rather than an
/// error.
fn split_declaration_id(id: &str) -> (Option<String>, String) {
    let mut parts = id.splitn(3, ':');
    match (parts.next(), parts.next(), parts.next()) {
        (Some(_origin), Some(module), Some(decl)) => (Some(module.to_owned()), decl.to_owned()),
        _ => (None, id.to_owned()),
    }
}

fn source_range(span: lean_semantic_search_contract::SourceSpan) -> SourceRange {
    SourceRange {
        file: String::new(),
        start_line: span.start.line,
        start_column: span.start.column,
        end_line: span.end.line,
        end_column: span.end.column,
    }
}

#[cfg(test)]
mod tests {
    use lean_semantic_search_contract::{
        CAPABILITY_SCHEMA_VERSION, CommandResponse, DECLARATION_FEATURE_COMMAND_VERSION, Diagnostic, Fingerprints,
        OpaqueFeatureKey, PROOF_GOAL_FEATURE_COMMAND_VERSION, RoleFeature, SEMANTIC_FEATURE_VERSION, SourcePosition,
        SourceSpan,
    };

    use super::{rank_semantic_rows, validate_response};

    fn fingerprints(seed: &str) -> Fingerprints {
        Fingerprints {
            statement: OpaqueFeatureKey::new(format!("stmt:{seed}")),
            safe_binder_permutation: OpaqueFeatureKey::new(format!("safe:{seed}")),
            connective_shape: OpaqueFeatureKey::new(format!("conn:{seed}")),
            conclusion_shape: OpaqueFeatureKey::new(format!("concl:{seed}")),
        }
    }

    #[test]
    fn validation_rejects_wrong_command_version() -> Result<(), String> {
        let response = CommandResponse::<lean_semantic_search_contract::DeclarationFeatureRow> {
            schema_version: CAPABILITY_SCHEMA_VERSION.to_owned(),
            command: "declaration_features".to_owned(),
            command_version: "old".to_owned(),
            feature_version: SEMANTIC_FEATURE_VERSION.to_owned(),
            rows: Vec::new(),
            diagnostics: Vec::new(),
        };
        let err = match validate_response(&response, "declaration_features", DECLARATION_FEATURE_COMMAND_VERSION) {
            Ok(()) => return Err("wrong command version must fail".to_owned()),
            Err(err) => err,
        };
        assert!(err.to_string().contains("command version"));
        Ok(())
    }

    #[test]
    fn retrieval_mapping_hides_raw_keys() -> Result<(), String> {
        let key = OpaqueFeatureKey::new("opaque-key-that-must-not-leak");
        let goal = lean_semantic_search_contract::ProofGoalFeatureRow {
            goal_id: "g".to_owned(),
            feature_version: SEMANTIC_FEATURE_VERSION.to_owned(),
            fingerprints: fingerprints("goal"),
            role_features: vec![RoleFeature {
                role: "conclusion_const".to_owned(),
                key: key.clone(),
                display: Some("Target.const".to_owned()),
            }],
            low_signal_markers: Vec::new(),
        };
        let row = lean_semantic_search_contract::DeclarationFeatureRow {
            declaration_id: "Fixture.target_helper".to_owned(),
            feature_version: SEMANTIC_FEATURE_VERSION.to_owned(),
            fingerprints: fingerprints("row"),
            role_features: vec![RoleFeature {
                role: "conclusion_const".to_owned(),
                key,
                display: Some("Target.const".to_owned()),
            }],
            binder_count: 0,
            low_signal_markers: Vec::new(),
            source: Some(SourceSpan {
                start: SourcePosition { line: 2, column: 3 },
                end: SourcePosition { line: 2, column: 30 },
            }),
        };
        let goal_response = CommandResponse {
            schema_version: CAPABILITY_SCHEMA_VERSION.to_owned(),
            command: "proof_goal_features".to_owned(),
            command_version: PROOF_GOAL_FEATURE_COMMAND_VERSION.to_owned(),
            feature_version: SEMANTIC_FEATURE_VERSION.to_owned(),
            rows: vec![goal],
            diagnostics: Vec::<Diagnostic>::new(),
        };
        let declaration_response = CommandResponse {
            schema_version: CAPABILITY_SCHEMA_VERSION.to_owned(),
            command: "declaration_features".to_owned(),
            command_version: DECLARATION_FEATURE_COMMAND_VERSION.to_owned(),
            feature_version: SEMANTIC_FEATURE_VERSION.to_owned(),
            rows: vec![row],
            diagnostics: Vec::<Diagnostic>::new(),
        };
        let result = rank_semantic_rows(&goal_response, &declaration_response, 5);
        assert_eq!(result.candidates.len(), 1);
        let Some(candidate) = result.candidates.first() else {
            return Err("expected a semantic candidate".to_owned());
        };
        let evidence = candidate.evidence.join(",");
        assert_eq!(candidate.name, "Fixture.target_helper");
        // A colonless id is not in `origin:module:decl` shape, so it stays whole
        // as the name and carries no module.
        assert!(candidate.module.is_none());
        assert!(evidence.contains("semantic:role_conclusion_const"));
        assert!(!evidence.contains("opaque-key-that-must-not-leak"));
        assert!(candidate.source.is_some());
        Ok(())
    }

    #[test]
    fn candidate_name_strips_origin_module_prefix() -> Result<(), String> {
        let key = OpaqueFeatureKey::new("shared-conclusion-key");
        let goal = lean_semantic_search_contract::ProofGoalFeatureRow {
            goal_id: "g".to_owned(),
            feature_version: SEMANTIC_FEATURE_VERSION.to_owned(),
            fingerprints: fingerprints("goal"),
            role_features: vec![RoleFeature {
                role: "conclusion_const".to_owned(),
                key: key.clone(),
                display: Some("Target.const".to_owned()),
            }],
            low_signal_markers: Vec::new(),
        };
        // The downstream extractor emits ids as `origin:module:declName`.
        let row = lean_semantic_search_contract::DeclarationFeatureRow {
            declaration_id: "lean-host-mcp:KanProofs.Foo:My.Decl.name".to_owned(),
            feature_version: SEMANTIC_FEATURE_VERSION.to_owned(),
            fingerprints: fingerprints("row"),
            role_features: vec![RoleFeature {
                role: "conclusion_const".to_owned(),
                key,
                display: Some("Target.const".to_owned()),
            }],
            binder_count: 0,
            low_signal_markers: Vec::new(),
            source: None,
        };
        let goal_response = CommandResponse {
            schema_version: CAPABILITY_SCHEMA_VERSION.to_owned(),
            command: "proof_goal_features".to_owned(),
            command_version: PROOF_GOAL_FEATURE_COMMAND_VERSION.to_owned(),
            feature_version: SEMANTIC_FEATURE_VERSION.to_owned(),
            rows: vec![goal],
            diagnostics: Vec::<Diagnostic>::new(),
        };
        let declaration_response = CommandResponse {
            schema_version: CAPABILITY_SCHEMA_VERSION.to_owned(),
            command: "declaration_features".to_owned(),
            command_version: DECLARATION_FEATURE_COMMAND_VERSION.to_owned(),
            feature_version: SEMANTIC_FEATURE_VERSION.to_owned(),
            rows: vec![row],
            diagnostics: Vec::<Diagnostic>::new(),
        };
        let result = rank_semantic_rows(&goal_response, &declaration_response, 5);
        let Some(candidate) = result.candidates.first() else {
            return Err("expected a semantic candidate".to_owned());
        };
        // The public name is the clean Lean name; the origin/module prefix is
        // lifted into `module` rather than leaking into `name`.
        assert_eq!(candidate.name, "My.Decl.name");
        assert_eq!(candidate.module.as_deref(), Some("KanProofs.Foo"));
        assert!(!candidate.name.contains("lean-host-mcp"));
        Ok(())
    }
}