use rust_mcp_schema::schema_utils::CallToolError;
use rust_mcp_schema::CallToolResult;
use rust_mcp_sdk::macros::{mcp_tool, JsonSchema};
use serde::{Deserialize, Serialize};
use crate::op::OpClient;
use crate::tools::enums::{permissions_to_string, VaultPermission};
use crate::tools::{json_result, op_error_to_tool_error, text_result};
#[mcp_tool(
name = "vault_list",
description = "List all vaults accessible to the current user. Returns vault ID, name, and description for each vault."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultListTool {}
impl VaultListTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let result = client.vault_list().await.map_err(op_error_to_tool_error)?;
json_result(&result)
}
}
#[mcp_tool(
name = "vault_get",
description = "Get detailed information about a specific vault by name or ID. Returns vault metadata including item count, creation date, and permissions."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultGetTool {
pub vault: String,
}
impl VaultGetTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let result = client
.vault_get(&self.vault)
.await
.map_err(op_error_to_tool_error)?;
json_result(&result)
}
}
#[mcp_tool(
name = "vault_create",
description = "Create a new vault in 1Password. Requires appropriate permissions in the account."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultCreateTool {
pub name: String,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub icon: Option<String>,
#[serde(default)]
pub allow_admins_to_manage: Option<bool>,
}
impl VaultCreateTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let result = client
.vault_create(
&self.name,
self.description.as_deref(),
self.icon.as_deref(),
self.allow_admins_to_manage,
)
.await
.map_err(op_error_to_tool_error)?;
json_result(&result)
}
}
#[mcp_tool(
name = "vault_edit",
description = "Edit an existing vault's name, description, or icon. Requires appropriate permissions."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultEditTool {
pub vault: String,
#[serde(default)]
pub name: Option<String>,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub icon: Option<String>,
#[serde(default)]
pub travel_mode: Option<bool>,
}
impl VaultEditTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let result = client
.vault_edit(
&self.vault,
self.name.as_deref(),
self.description.as_deref(),
self.icon.as_deref(),
self.travel_mode,
)
.await
.map_err(op_error_to_tool_error)?;
json_result(&result)
}
}
#[mcp_tool(
name = "vault_delete",
description = "Delete a vault and all its contents. WARNING: This action is irreversible. All items in the vault will be permanently deleted."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultDeleteTool {
pub vault: String,
}
impl VaultDeleteTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let result = client
.vault_delete(&self.vault)
.await
.map_err(op_error_to_tool_error)?;
text_result(result)
}
}
#[mcp_tool(
name = "vault_user_list",
description = "List all users who have access to a specific vault and their permission levels."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultUserListTool {
pub vault: String,
}
impl VaultUserListTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let result = client
.vault_user_list(&self.vault)
.await
.map_err(op_error_to_tool_error)?;
json_result(&result)
}
}
#[mcp_tool(
name = "vault_user_grant",
description = "Grant a user access to a vault with specified permissions. Multiple permissions can be combined."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultUserGrantTool {
pub vault: String,
pub user: String,
#[serde(default)]
pub permissions: Option<Vec<VaultPermission>>,
}
impl VaultUserGrantTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let permissions_str = self.permissions.as_ref().map(|p| permissions_to_string(p));
let result = client
.vault_user_grant(&self.vault, &self.user, permissions_str.as_deref())
.await
.map_err(op_error_to_tool_error)?;
text_result(result)
}
}
#[mcp_tool(
name = "vault_user_revoke",
description = "Revoke a user's access to a vault. The user will no longer be able to view or modify items in this vault."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultUserRevokeTool {
pub vault: String,
pub user: String,
}
impl VaultUserRevokeTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let result = client
.vault_user_revoke(&self.vault, &self.user)
.await
.map_err(op_error_to_tool_error)?;
text_result(result)
}
}
#[mcp_tool(
name = "vault_group_list",
description = "List all groups that have access to a specific vault and their permission levels."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultGroupListTool {
pub vault: String,
}
impl VaultGroupListTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let result = client
.vault_group_list(&self.vault)
.await
.map_err(op_error_to_tool_error)?;
json_result(&result)
}
}
#[mcp_tool(
name = "vault_group_grant",
description = "Grant a group access to a vault with specified permissions. Multiple permissions can be combined."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultGroupGrantTool {
pub vault: String,
pub group: String,
#[serde(default)]
pub permissions: Option<Vec<VaultPermission>>,
}
impl VaultGroupGrantTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let permissions_str = self.permissions.as_ref().map(|p| permissions_to_string(p));
let result = client
.vault_group_grant(&self.vault, &self.group, permissions_str.as_deref())
.await
.map_err(op_error_to_tool_error)?;
text_result(result)
}
}
#[mcp_tool(
name = "vault_group_revoke",
description = "Revoke a group's access to a vault. Members of the group will no longer be able to access this vault through group membership."
)]
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct VaultGroupRevokeTool {
pub vault: String,
pub group: String,
}
impl VaultGroupRevokeTool {
pub async fn call(&self, client: &OpClient) -> Result<CallToolResult, CallToolError> {
let result = client
.vault_group_revoke(&self.vault, &self.group)
.await
.map_err(op_error_to_tool_error)?;
text_result(result)
}
}