дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
* feat(plugins): add database schema discovery and query execution APIs (API 1.21) * docs(plugins): list API 1.21 database discovery and query additions in versioning * fix(plugins): address database discovery/query review findings and rebuild dist --------- Co-authored-by: l0ng-ai <[email protected]>
70 рядки
3.6 KiB
Rust
70 рядки
3.6 KiB
Rust
use openidea_plugin::{Error, Invocation, McpProvider, Plugin, PluginDataSource, host, register_plugin};
|
|
#[derive(Default)]
|
|
struct DataTools;
|
|
impl Plugin for DataTools {
|
|
fn run_command(&mut self, id: &str, ctx: &Invocation, arg: Option<&str>) -> Result<Option<String>, Error> {
|
|
if id == "stdio" {
|
|
return Ok(Some(serde_json::to_string(&host::mcp_stdio_command()).unwrap()));
|
|
}
|
|
let p = ctx.project().ok_or("project required")?;
|
|
match id {
|
|
"upsert" => {
|
|
let source: PluginDataSource =
|
|
serde_json::from_str(arg.unwrap()).map_err(|e| Error::Invalid(e.to_string()))?;
|
|
p.upsert_data_source(&source)?;
|
|
}
|
|
"remove" => p.remove_data_source(arg.unwrap_or("local"))?,
|
|
"table-schema" => {
|
|
let value: serde_json::Value = serde_json::from_str(arg.ok_or("table reference required")?)
|
|
.map_err(|e| Error::Invalid(e.to_string()))?;
|
|
// Accept both the table-menu selection and a direct reference for callers/tests.
|
|
let table =
|
|
serde_json::from_value(value.get("tables").and_then(|v| v.get(0)).unwrap_or(&value).clone())
|
|
.map_err(|e| Error::Invalid(e.to_string()))?;
|
|
let schema = p.database_table_schema(&table)?;
|
|
return Ok(Some(serde_json::to_string(&schema).unwrap()));
|
|
}
|
|
"sources" => return Ok(Some(serde_json::to_string(&p.data_sources()?).unwrap())),
|
|
"connected-sources" => return Ok(Some(serde_json::to_string(&p.connected_data_sources()?).unwrap())),
|
|
"list-schemas" => {
|
|
let source_id = arg.ok_or("sourceId required")?;
|
|
return Ok(Some(serde_json::to_string(&p.database_schemas(source_id)?).unwrap()));
|
|
}
|
|
"list-tables" => {
|
|
let value: serde_json::Value = serde_json::from_str(arg.ok_or("sourceId and schema required")?)
|
|
.map_err(|e| Error::Invalid(e.to_string()))?;
|
|
let source_id = value["sourceId"].as_str().ok_or("sourceId required")?;
|
|
let schema = value["schema"].as_str().ok_or("schema required")?;
|
|
return Ok(Some(serde_json::to_string(&p.database_tables(source_id, schema)?).unwrap()));
|
|
}
|
|
"execute-query" => {
|
|
let value: serde_json::Value = serde_json::from_str(arg.ok_or("query request required")?)
|
|
.map_err(|e| Error::Invalid(e.to_string()))?;
|
|
let source_id = value["sourceId"].as_str().ok_or("sourceId required")?;
|
|
let sql = value["sql"].as_str().ok_or("sql required")?;
|
|
let max_rows = value["maxRows"].as_u64().map(|n| n as u32);
|
|
return Ok(Some(serde_json::to_string(&p.database_execute_query(source_id, sql, max_rows)?).unwrap()));
|
|
}
|
|
"credential" => p.set_data_source_credential("local", arg.unwrap_or("secret"))?,
|
|
"connect" => p.connect_data_source(arg.unwrap_or("local"))?,
|
|
_ => return Err(Error::NotFound(id.into())),
|
|
}
|
|
Ok(None)
|
|
}
|
|
}
|
|
impl McpProvider for DataTools {
|
|
fn call(&mut self, tool: &str, arguments: &str, ctx: &Invocation) -> Result<String, Error> {
|
|
match tool {
|
|
"spin" => loop {
|
|
std::hint::spin_loop();
|
|
},
|
|
"denied" => {
|
|
host::run_process("sh", &[], None)?;
|
|
Ok("unexpected".into())
|
|
}
|
|
_ => Ok(format!("{}:{}:{}", tool, ctx.project().ok_or("project required")?.root()?, arguments)),
|
|
}
|
|
}
|
|
}
|
|
register_plugin!(DataTools, mcp);
|