Skip to main content

lean_ctx/engine/
mod.rs

1use std::path::PathBuf;
2use std::sync::atomic::{AtomicI64, Ordering};
3
4use anyhow::{anyhow, Context, Result};
5use rmcp::{
6    model::{
7        CallToolRequest, CallToolRequestParams, CallToolResult, ClientJsonRpcMessage,
8        ClientRequest, JsonRpcRequest, NumberOrString, ServerJsonRpcMessage, ServerResult,
9    },
10    service::serve_directly,
11    service::RoleServer,
12    transport::OneshotTransport,
13};
14use serde_json::{Map, Value};
15
16use crate::tools::LeanCtxServer;
17
18pub struct ContextEngine {
19    server: LeanCtxServer,
20    next_id: AtomicI64,
21}
22
23impl ContextEngine {
24    pub fn new() -> Self {
25        Self {
26            server: LeanCtxServer::new(),
27            next_id: AtomicI64::new(1),
28        }
29    }
30
31    pub fn with_project_root(project_root: impl Into<PathBuf>) -> Self {
32        let root = project_root.into().to_string_lossy().to_string();
33        Self {
34            server: LeanCtxServer::new_with_project_root(Some(&root)),
35            next_id: AtomicI64::new(1),
36        }
37    }
38
39    pub fn from_server(server: LeanCtxServer) -> Self {
40        Self {
41            server,
42            next_id: AtomicI64::new(1),
43        }
44    }
45
46    pub fn server(&self) -> &LeanCtxServer {
47        &self.server
48    }
49
50    pub fn manifest(&self) -> Value {
51        crate::core::mcp_manifest::manifest_value()
52    }
53
54    pub async fn call_tool_value(&self, name: &str, arguments: Option<Value>) -> Result<Value> {
55        let result = self.call_tool_result(name, arguments).await?;
56        serde_json::to_value(result).map_err(|e| anyhow!("serialize CallToolResult: {e}"))
57    }
58
59    pub async fn call_tool_result(
60        &self,
61        name: &str,
62        arguments: Option<Value>,
63    ) -> Result<CallToolResult> {
64        let id = self.next_id.fetch_add(1, Ordering::Relaxed);
65        let req_id = NumberOrString::Number(id);
66
67        let args_obj: Map<String, Value> = match arguments {
68            None => Map::new(),
69            Some(Value::Object(m)) => m,
70            Some(other) => {
71                return Err(anyhow!(
72                    "tool arguments must be a JSON object (got {other})"
73                ))
74            }
75        };
76
77        let params = CallToolRequestParams::new(name.to_string()).with_arguments(args_obj);
78        let call: CallToolRequest = CallToolRequest::new(params);
79        let client_req = ClientRequest::CallToolRequest(call);
80        let msg = ClientJsonRpcMessage::Request(JsonRpcRequest::new(req_id, client_req));
81
82        let (transport, mut rx) = OneshotTransport::<RoleServer>::new(msg);
83        let service = serve_directly(self.server.clone(), transport, None);
84        tokio::spawn(async move {
85            let _ = service.waiting().await;
86        });
87
88        let server_msg =
89            match tokio::time::timeout(std::time::Duration::from_mins(2), rx.recv()).await {
90                Ok(Some(msg)) => msg,
91                Ok(None) => return Err(anyhow!("no response from tool call")),
92                Err(_) => return Err(anyhow!("tool call timed out after 120s")),
93            };
94
95        match server_msg {
96            ServerJsonRpcMessage::Response(r) => match r.result {
97                ServerResult::CallToolResult(result) => Ok(result),
98                other => Err(anyhow!("unexpected server result: {other:?}")),
99            },
100            ServerJsonRpcMessage::Error(e) => Err(anyhow!("{e:?}")).context("tool call error"),
101            ServerJsonRpcMessage::Notification(_) => Err(anyhow!("unexpected notification")),
102            ServerJsonRpcMessage::Request(_) => Err(anyhow!("unexpected request")),
103        }
104    }
105
106    pub async fn call_tool_text(&self, name: &str, arguments: Option<Value>) -> Result<String> {
107        let result = self.call_tool_result(name, arguments).await?;
108        let mut out = String::new();
109        for c in result.content {
110            if let Some(t) = c.as_text() {
111                out.push_str(&t.text);
112            }
113        }
114        if out.is_empty() {
115            if let Some(v) = result.structured_content {
116                out = v.to_string();
117            }
118        }
119        Ok(out)
120    }
121}
122
123impl Default for ContextEngine {
124    fn default() -> Self {
125        Self::new()
126    }
127}