use std::borrow::Cow;
use dbmcp_server::types::{QueryRequest, QueryResponse};
use dbmcp_sql::Connection as _;
use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
use rmcp::model::{ErrorData, ToolAnnotations};
use crate::MysqlHandler;
pub(crate) struct WriteQueryTool;
impl WriteQueryTool {
const NAME: &'static str = "writeQuery";
const TITLE: &'static str = "Write Query";
const DESCRIPTION: &'static str = include_str!("../../assets/tools/write_query.md");
}
impl ToolBase for WriteQueryTool {
type Parameter = QueryRequest;
type Output = QueryResponse;
type Error = ErrorData;
fn name() -> Cow<'static, str> {
Self::NAME.into()
}
fn title() -> Option<String> {
Some(Self::TITLE.into())
}
fn description() -> Option<Cow<'static, str>> {
Some(Self::DESCRIPTION.into())
}
fn annotations() -> Option<ToolAnnotations> {
Some(
ToolAnnotations::new()
.read_only(false)
.destructive(true)
.idempotent(false)
.open_world(true),
)
}
}
impl AsyncTool<MysqlHandler> for WriteQueryTool {
async fn invoke(handler: &MysqlHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
handler.write_query(params).await
}
}
impl MysqlHandler {
pub async fn write_query(
&self,
QueryRequest { query, database }: QueryRequest,
) -> Result<QueryResponse, ErrorData> {
let database = database.as_deref().map(str::trim).filter(|s| !s.is_empty());
let mut rows = self.connection.fetch_json(query.as_str(), database).await?;
if let Some(r) = &self.redactor {
r.apply(&mut rows)?;
}
Ok(QueryResponse { rows })
}
}