Enhance tool documentation and add new features
- Added module-level documentation for memory tools (`remember`, `recall`, `forget`) to clarify their purpose. - Improved documentation in `recall.rs` and `remember.rs` to describe the functionality and flow of memory entry operations. - Updated `mod.rs` to include descriptions for the tool trait and execution context. - Enhanced `plan.rs` with detailed comments on plan-mode signaling tools. - Documented text search tools in `search.rs` to explain their functionality. - Improved sequential-thinking tool documentation in `seqthink.rs`. - Added safety filter documentation in `shell_filter` for credential and git operations. - Enhanced utility tools documentation, including `cd`, `dir_cache_update`, and `todowrite`. - Improved rendering documentation in view modules (`chat`, `markdown`, `status`, `workflow`) to clarify rendering flows and purposes.
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
//! MCP server connection management: spawning/talking to stdio child
|
||||
//! processes and HTTP endpoints, and adapting their advertised tools to
|
||||
//! the crate's `Tool` trait.
|
||||
|
||||
use serde_json::{json, Value};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
@@ -21,6 +25,8 @@ fn mcp_static_str(s: &str) -> &'static str {
|
||||
leaked
|
||||
}
|
||||
|
||||
/// How an MCP server is reached: a spawned child process talking
|
||||
/// newline-delimited JSON-RPC over stdio, or a remote HTTP endpoint.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum McpTransport {
|
||||
Stdio {
|
||||
@@ -32,6 +38,7 @@ pub enum McpTransport {
|
||||
},
|
||||
}
|
||||
|
||||
/// A single tool advertised by an MCP server, as returned by `tools/list`.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct McpToolInfo {
|
||||
pub name: String,
|
||||
@@ -39,6 +46,8 @@ pub struct McpToolInfo {
|
||||
pub input_schema: Value,
|
||||
}
|
||||
|
||||
/// A connected MCP server: its transport, advertised tools, and (for stdio)
|
||||
/// a live handle to the child process.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct McpServer {
|
||||
pub name: String,
|
||||
@@ -51,6 +60,8 @@ pub struct McpServer {
|
||||
pub child_handle: Option<Arc<Mutex<StdioChild>>>,
|
||||
}
|
||||
|
||||
/// Live handle to an MCP server child process communicating over stdio
|
||||
/// via newline-delimited JSON-RPC 2.0.
|
||||
#[derive(Debug)]
|
||||
pub struct StdioChild {
|
||||
stdin: std::process::ChildStdin,
|
||||
@@ -59,6 +70,18 @@ pub struct StdioChild {
|
||||
}
|
||||
|
||||
impl StdioChild {
|
||||
/// Send a JSON-RPC request to the child and block for its matching response.
|
||||
///
|
||||
/// Flow: assign the next request id → write request + newline to stdin →
|
||||
/// loop reading lines from stdout until one has a matching `id` or the
|
||||
/// timeout elapses → return its `result` (or error out on an `error` field).
|
||||
///
|
||||
/// Why: the child may interleave unrelated/malformed lines, so blank
|
||||
/// lines are skipped and non-matching ids are ignored rather than
|
||||
/// treated as a protocol violation.
|
||||
///
|
||||
/// Return: the `result` value of the matching response, or `Err` on
|
||||
/// timeout, EOF, JSON-RPC error, or I/O failure.
|
||||
pub fn call(&mut self, method: &str, params: Value) -> anyhow::Result<Value> {
|
||||
self.next_id += 1;
|
||||
let id = self.next_id;
|
||||
@@ -255,11 +278,14 @@ fn extract_text_content(result: &Value) -> anyhow::Result<String> {
|
||||
}))
|
||||
}
|
||||
|
||||
/// Registry of connected MCP servers and their tools for the current session.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct McpManager {
|
||||
pub servers: Vec<McpServer>,
|
||||
}
|
||||
|
||||
/// Adapts a single MCP-advertised tool to the crate's `Tool` trait so it can
|
||||
/// be dispatched through the same execution path as built-in tools.
|
||||
pub struct McpToolAdapter {
|
||||
pub tool_name: String,
|
||||
pub server_name: String,
|
||||
@@ -296,12 +322,22 @@ impl crate::tool::Tool for McpToolAdapter {
|
||||
}
|
||||
|
||||
impl McpManager {
|
||||
/// Create an empty manager with no connected servers.
|
||||
pub fn new() -> Self {
|
||||
McpManager {
|
||||
servers: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Flatten all connected servers' tools into a single list of `Tool` trait objects.
|
||||
///
|
||||
/// Flow: for each server, clone its child handle → wrap each of its
|
||||
/// `McpToolInfo` entries in an `McpToolAdapter` sharing that handle.
|
||||
///
|
||||
/// Why: the handle is cloned (Arc) per tool so every adapter for a given
|
||||
/// stdio server reuses the same persistent child process/connection.
|
||||
///
|
||||
/// Return: boxed `Tool` trait objects ready to merge into the harness's tool list.
|
||||
pub fn as_tools(&self) -> Vec<Box<dyn crate::tool::Tool>> {
|
||||
self.servers.iter().flat_map(|server| {
|
||||
let handle = server.child_handle.clone();
|
||||
|
||||
Reference in New Issue
Block a user