use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use serde_json::Value;
use super::connection::McpConnection;
use super::error::McpError;
use crate::providers::http::tools::{Tool, ToolError, ToolOutput};
pub struct McpBridgeTool {
connection: Arc<McpConnection>,
registry_name: String,
mcp_name: String,
description: String,
parameters_schema: Value,
}
impl McpBridgeTool {
pub fn new(
connection: Arc<McpConnection>,
registry_name: String,
mcp_name: String,
description: String,
parameters_schema: Value,
) -> Self {
Self {
connection,
registry_name,
mcp_name,
description,
parameters_schema,
}
}
}
impl Tool for McpBridgeTool {
fn name(&self) -> &str {
&self.registry_name
}
fn description(&self) -> &str {
&self.description
}
fn parameters_schema(&self) -> Value {
self.parameters_schema.clone()
}
fn execute(
&self,
input: Value,
) -> Pin<Box<dyn Future<Output = Result<ToolOutput, ToolError>> + Send + '_>> {
Box::pin(async move {
match self.connection.call_tool(&self.mcp_name, input).await {
Ok(content) => Ok(ToolOutput::success(content)),
Err(McpError::Timeout { tool_name }) => Ok(ToolOutput::error(format!(
"MCP tool '{tool_name}' timed out"
))),
Err(McpError::ToolCallFailed { tool_name, message }) => Ok(ToolOutput::error(
format!("MCP tool '{tool_name}' error: {message}"),
)),
Err(e) => Err(ToolError::new(format!("MCP infrastructure error: {e}"))),
}
})
}
}