Файли
openidea/crates/ide-plugin/tests/fixtures/data-tools/src/lib.rs
T
葛俊таgejun123456 b7c2d4eacb feat(plugins): expose database table menus and read-only schema (#7372)
Plugin API 1.20 adds wit/database.wit with describe-table behind the database.schema capability: a read-only view of a connected source's table (columns, keys, indexes) through DbManager::connected_table_details. Plugins can contribute database.table.context menu entries (needs_api 20) that receive the non-secret table identity and the when keys databaseDriver, databaseObjectKind, databaseSchema, databaseTable and databaseSelectionCount.

Co-authored-by: gejun123456 <[email protected]>
2026-12-02 14:25:35 +08:00

50 рядки
2.3 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())),
"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);