✨ feat(kebab-mcp): search tool (fb-30)
Third tool — `search` (input: query / mode / k). First tool with non-empty input — establishes the pattern: SearchInput struct with JsonSchema derive + Tool::new uses rmcp::handler::server::common::schema_for_type::<SearchInput>() for inputSchema + call_tool match arm parses request.arguments via serde_json::from_value. search_with_config takes owned Config, so state.config (Arc<Config>) is cloned via (*state.config).clone(). Output: search_hit.v1 array — SearchHit (kebab-core) does not carry schema_version field, so each element is tagged inline before serialising. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
use rmcp::ServerHandler;
|
use rmcp::ServerHandler;
|
||||||
use rmcp::handler::server::common::schema_for_empty_input;
|
use rmcp::handler::server::common::{schema_for_empty_input, schema_for_type};
|
||||||
use rmcp::model::{
|
use rmcp::model::{
|
||||||
CallToolRequestParams, CallToolResult, Implementation, ListToolsResult, ServerCapabilities,
|
CallToolRequestParams, CallToolResult, Implementation, ListToolsResult, ServerCapabilities,
|
||||||
ServerInfo, Tool,
|
ServerInfo, Tool,
|
||||||
@@ -60,6 +60,11 @@ impl ServerHandler for KebabHandler {
|
|||||||
"Health check — verifies config, storage, models, and Ollama connectivity.",
|
"Health check — verifies config, storage, models, and Ollama connectivity.",
|
||||||
schema_for_empty_input(),
|
schema_for_empty_input(),
|
||||||
),
|
),
|
||||||
|
Tool::new(
|
||||||
|
"search",
|
||||||
|
"Full-text / vector / hybrid search over the knowledge base. Returns search_hit.v1 array.",
|
||||||
|
schema_for_type::<tools::search::SearchInput>(),
|
||||||
|
),
|
||||||
]))
|
]))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,6 +82,17 @@ impl ServerHandler for KebabHandler {
|
|||||||
let input = tools::doctor::DoctorInput::default();
|
let input = tools::doctor::DoctorInput::default();
|
||||||
Ok(tools::doctor::handle(&self.state, input))
|
Ok(tools::doctor::handle(&self.state, input))
|
||||||
}
|
}
|
||||||
|
"search" => {
|
||||||
|
let args = request.arguments.unwrap_or_default();
|
||||||
|
let input: tools::search::SearchInput =
|
||||||
|
match serde_json::from_value(serde_json::Value::Object(args)) {
|
||||||
|
Ok(i) => i,
|
||||||
|
Err(e) => {
|
||||||
|
return Ok(error::to_tool_error(&anyhow::Error::from(e)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(tools::search::handle(&self.state, input))
|
||||||
|
}
|
||||||
_other => Err(ErrorData::method_not_found::<
|
_other => Err(ErrorData::method_not_found::<
|
||||||
rmcp::model::CallToolRequestMethod,
|
rmcp::model::CallToolRequestMethod,
|
||||||
>()),
|
>()),
|
||||||
|
|||||||
@@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
pub mod doctor;
|
pub mod doctor;
|
||||||
// pub mod search; // wired in Plan Task 6
|
pub mod search;
|
||||||
// pub mod ask; // wired in Plan Task 7
|
// pub mod ask; // wired in Plan Task 7
|
||||||
|
|||||||
71
crates/kebab-mcp/src/tools/search.rs
Normal file
71
crates/kebab-mcp/src/tools/search.rs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
//! `search` tool — wraps `kebab_app::search_with_config`.
|
||||||
|
//! Input: { query, mode?, k? }. Output: search_hit.v1 array JSON.
|
||||||
|
//!
|
||||||
|
//! First tool with a non-empty `inputSchema`: `SearchInput` derives
|
||||||
|
//! `JsonSchema` and `Tool::new` uses
|
||||||
|
//! `rmcp::handler::server::common::schema_for_type::<SearchInput>()`.
|
||||||
|
|
||||||
|
use rmcp::model::CallToolResult;
|
||||||
|
use schemars::JsonSchema;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::error::{to_tool_error, to_tool_success};
|
||||||
|
use crate::state::KebabAppState;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
|
||||||
|
pub struct SearchInput {
|
||||||
|
/// User query (free text).
|
||||||
|
pub query: String,
|
||||||
|
/// Retrieval mode: "hybrid" (default), "lexical", or "vector".
|
||||||
|
#[serde(default = "default_mode")]
|
||||||
|
pub mode: String,
|
||||||
|
/// Top-K results. Defaults to 10. Clamped to 1–100.
|
||||||
|
#[serde(default = "default_k")]
|
||||||
|
pub k: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_mode() -> String {
|
||||||
|
"hybrid".to_string()
|
||||||
|
}
|
||||||
|
fn default_k() -> usize {
|
||||||
|
10
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle(state: &KebabAppState, input: SearchInput) -> CallToolResult {
|
||||||
|
let k = input.k.clamp(1, 100);
|
||||||
|
let mode = match input.mode.as_str() {
|
||||||
|
"lexical" => kebab_core::SearchMode::Lexical,
|
||||||
|
"vector" => kebab_core::SearchMode::Vector,
|
||||||
|
_ => kebab_core::SearchMode::Hybrid,
|
||||||
|
};
|
||||||
|
let query = kebab_core::SearchQuery {
|
||||||
|
text: input.query,
|
||||||
|
mode,
|
||||||
|
k,
|
||||||
|
filters: kebab_core::SearchFilters::default(),
|
||||||
|
};
|
||||||
|
match kebab_app::search_with_config((*state.config).clone(), query) {
|
||||||
|
Ok(hits) => {
|
||||||
|
// SearchHit (kebab-core) does not carry a `schema_version` field,
|
||||||
|
// so we tag each element inline before serialising.
|
||||||
|
let tagged: Vec<serde_json::Value> = hits
|
||||||
|
.iter()
|
||||||
|
.map(|h| {
|
||||||
|
let mut v = serde_json::to_value(h).unwrap_or_default();
|
||||||
|
if let serde_json::Value::Object(ref mut map) = v {
|
||||||
|
map.insert(
|
||||||
|
"schema_version".to_string(),
|
||||||
|
serde_json::Value::String("search_hit.v1".to_string()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
v
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
match serde_json::to_string(&serde_json::Value::Array(tagged)) {
|
||||||
|
Ok(json) => to_tool_success(json),
|
||||||
|
Err(e) => to_tool_error(&anyhow::anyhow!(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => to_tool_error(&e),
|
||||||
|
}
|
||||||
|
}
|
||||||
90
crates/kebab-mcp/tests/tools_call_search.rs
Normal file
90
crates/kebab-mcp/tests/tools_call_search.rs
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
//! Integration: tools/call name=search — verify response is search_hit.v1 array.
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
use kebab_config::Config;
|
||||||
|
use kebab_core::SourceScope;
|
||||||
|
use kebab_mcp::{KebabAppState, KebabHandler};
|
||||||
|
use rmcp::model::RawContent;
|
||||||
|
|
||||||
|
fn minimal_config(data_dir: &std::path::Path, workspace_root: &std::path::Path) -> Config {
|
||||||
|
let mut cfg = Config::defaults();
|
||||||
|
cfg.storage.data_dir = data_dir.to_string_lossy().into_owned();
|
||||||
|
cfg.storage.model_dir = data_dir
|
||||||
|
.join("models")
|
||||||
|
.to_string_lossy()
|
||||||
|
.into_owned();
|
||||||
|
cfg.workspace.root = workspace_root.to_string_lossy().into_owned();
|
||||||
|
cfg.workspace.exclude.clear();
|
||||||
|
cfg.models.embedding.provider = "none".to_string();
|
||||||
|
cfg.models.embedding.dimensions = 0;
|
||||||
|
cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn search_tool_returns_search_hits_array() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let data_dir = dir.path().join("data");
|
||||||
|
let workspace_root = dir.path().join("notes");
|
||||||
|
fs::create_dir_all(&data_dir).unwrap();
|
||||||
|
fs::create_dir_all(&workspace_root).unwrap();
|
||||||
|
|
||||||
|
let config = minimal_config(&data_dir, &workspace_root);
|
||||||
|
|
||||||
|
// Write a markdown document containing the query term.
|
||||||
|
fs::write(
|
||||||
|
workspace_root.join("a.md"),
|
||||||
|
"# Alpha\n\nThis document mentions kebab and bread.",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Seed kebab.sqlite via ingest so search has indexed content.
|
||||||
|
let scope = SourceScope {
|
||||||
|
root: workspace_root.clone(),
|
||||||
|
include: vec![],
|
||||||
|
exclude: vec![],
|
||||||
|
};
|
||||||
|
let _ = kebab_app::ingest_with_config(config.clone(), scope, false).unwrap();
|
||||||
|
|
||||||
|
let state = KebabAppState::new(config, None);
|
||||||
|
let handler = KebabHandler::new(state);
|
||||||
|
|
||||||
|
let result = kebab_mcp::tools::search::handle(
|
||||||
|
handler.state(),
|
||||||
|
kebab_mcp::tools::search::SearchInput {
|
||||||
|
query: "kebab".to_string(),
|
||||||
|
mode: "lexical".to_string(),
|
||||||
|
k: 5,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
!result.is_error.unwrap_or(false),
|
||||||
|
"expected isError=false, got {:?}",
|
||||||
|
result
|
||||||
|
);
|
||||||
|
|
||||||
|
let content = result
|
||||||
|
.content
|
||||||
|
.first()
|
||||||
|
.expect("expected at least one content item");
|
||||||
|
|
||||||
|
let text = match &content.raw {
|
||||||
|
RawContent::Text(t) => &t.text,
|
||||||
|
other => panic!("expected text content, got {other:?}"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let v: serde_json::Value = serde_json::from_str(text).unwrap();
|
||||||
|
let arr = v.as_array().expect("search returns a JSON array");
|
||||||
|
assert!(
|
||||||
|
!arr.is_empty(),
|
||||||
|
"expected at least one hit for 'kebab' in 'a.md'"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
arr[0]
|
||||||
|
.get("schema_version")
|
||||||
|
.and_then(|s| s.as_str()),
|
||||||
|
Some("search_hit.v1"),
|
||||||
|
"first hit should carry schema_version=search_hit.v1"
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user