use mcp_host::prelude::*;
use mcp_host::registry::router::McpToolRouter;
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::json;
use toml::{Table, Value as TomlValue};
use super::super::McpState;
use super::insert_string;
use super::json_output;
use super::super::elicit;
use crate::blueprint::parser::{JailfileFormat, parse_jailfile_as};
#[derive(Debug, Deserialize, JsonSchema)]
pub struct JailfileCreateParams {
pub path: String,
pub metadata: Option<MetadataParams>,
pub build: Option<BuildParams>,
pub start: Option<StartParams>,
pub stop: Option<String>,
}
#[derive(Debug, Deserialize, JsonSchema)]
pub struct MetadataParams {
pub name: Option<String>,
pub version: Option<String>,
pub description: Option<String>,
pub author: Option<String>,
#[serde(default)]
pub labels: std::collections::BTreeMap<String, String>,
}
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BuildParams {
pub from: Option<String>,
pub workdir: Option<String>,
#[serde(default)]
pub args: Vec<ArgParams>,
#[serde(default)]
pub env: std::collections::BTreeMap<String, String>,
#[serde(default)]
pub run: Vec<String>,
#[serde(default)]
pub copy: Vec<CopyParams>,
#[serde(default)]
pub expose: Vec<ExposeParams>,
}
#[derive(Debug, Deserialize, JsonSchema)]
pub struct ArgParams {
pub name: String,
pub default: Option<String>,
}
#[derive(Debug, Deserialize, JsonSchema)]
pub struct CopyParams {
pub src: String,
pub dest: String,
pub mode: Option<u32>,
pub owner: Option<String>,
}
#[derive(Debug, Deserialize, JsonSchema)]
pub struct ExposeParams {
pub port: u16,
pub protocol: Option<String>,
}
#[derive(Debug, Deserialize, JsonSchema)]
pub struct StartParams {
pub cmd: Option<String>,
pub entrypoint: Option<String>,
pub user: Option<String>,
}
impl McpState {
#[mcp_tool(
name = "jailfile_create",
description = "Render a Jailfile in the TOML format, validate it with the same parser \
`blackship build` uses, and write it only after the operator approves an \
elicitation. Returns the diagnostics and writes nothing when the rendered \
Jailfile does not parse, and never replaces an existing file unless the \
operator grants `overwrite`. Read blackship:///reference/jailfile first: \
several Dockerfile constructs have no equivalent here.",
destructive = true,
open_world = false
)]
async fn jailfile_create(
&self,
ctx: Ctx<'_>,
params: Parameters<JailfileCreateParams>,
) -> ToolResult {
let params = params.0;
let path = std::path::PathBuf::from(¶ms.path);
let rendered = render(¶ms)
.map_err(|e| ToolError::Execution(format!("Failed to render Jailfile: {e}")))?;
if let Err(e) = parse_jailfile_as(&rendered, JailfileFormat::Toml) {
return json_output(&json!({
"path": path.display().to_string(),
"written": false,
"reason": "The rendered Jailfile does not parse, so the operator was not asked. \
Fix the diagnostics and call this tool again.",
"format": "toml",
"diagnostics": [{ "message": e.to_string() }],
"rendered": rendered,
}));
}
let outcome =
elicit::write_with_approval(&ctx, &path, "a TOML Jailfile", &rendered).await?;
let mut payload = outcome;
if let Some(object) = payload.as_object_mut() {
object.insert("format".to_string(), json!("toml"));
object.insert("rendered".to_string(), json!(rendered));
}
json_output(&payload)
}
}
pub(in crate::mcp) fn render(params: &JailfileCreateParams) -> Result<String, toml::ser::Error> {
let mut root = Table::new();
if let Some(metadata) = ¶ms.metadata {
let table = metadata_table(metadata);
if !table.is_empty() {
root.insert("metadata".to_string(), TomlValue::Table(table));
}
}
if let Some(build) = ¶ms.build {
let table = build_table(build);
if !table.is_empty() {
root.insert("build".to_string(), TomlValue::Table(table));
}
}
if let Some(start) = ¶ms.start {
let table = start_table(start);
if !table.is_empty() {
root.insert("start".to_string(), TomlValue::Table(table));
}
}
if let Some(stop) = ¶ms.stop {
let mut table = Table::new();
table.insert("cmd".to_string(), TomlValue::String(stop.clone()));
root.insert("stop".to_string(), TomlValue::Table(table));
}
if root.is_empty() {
root.insert("build".to_string(), TomlValue::Table(Table::new()));
}
toml::to_string_pretty(&root)
}
fn metadata_table(metadata: &MetadataParams) -> Table {
let mut table = Table::new();
insert_string(&mut table, "name", metadata.name.as_deref());
insert_string(&mut table, "version", metadata.version.as_deref());
insert_string(&mut table, "description", metadata.description.as_deref());
insert_string(&mut table, "author", metadata.author.as_deref());
if !metadata.labels.is_empty() {
let labels = metadata
.labels
.iter()
.map(|(key, value)| (key.clone(), TomlValue::String(value.clone())))
.collect();
table.insert("labels".to_string(), TomlValue::Table(labels));
}
table
}
fn build_table(build: &BuildParams) -> Table {
let mut table = Table::new();
insert_string(&mut table, "from", build.from.as_deref());
insert_string(&mut table, "workdir", build.workdir.as_deref());
if !build.env.is_empty() {
let env = build
.env
.iter()
.map(|(key, value)| (key.clone(), TomlValue::String(value.clone())))
.collect();
table.insert("env".to_string(), TomlValue::Table(env));
}
if !build.args.is_empty() {
table.insert(
"args".to_string(),
TomlValue::Array(
build
.args
.iter()
.map(|arg| {
let mut entry = Table::new();
entry.insert("name".to_string(), TomlValue::String(arg.name.clone()));
insert_string(&mut entry, "default", arg.default.as_deref());
TomlValue::Table(entry)
})
.collect(),
),
);
}
if !build.run.is_empty() {
table.insert(
"run".to_string(),
TomlValue::Array(
build
.run
.iter()
.map(|command| {
let mut entry = Table::new();
entry.insert(
"command".to_string(),
TomlValue::String(command.clone()),
);
TomlValue::Table(entry)
})
.collect(),
),
);
}
if !build.copy.is_empty() {
table.insert(
"copy".to_string(),
TomlValue::Array(build.copy.iter().map(copy_entry).collect()),
);
}
if !build.expose.is_empty() {
table.insert(
"expose".to_string(),
TomlValue::Array(
build
.expose
.iter()
.map(|expose| {
let mut entry = Table::new();
entry.insert(
"port".to_string(),
TomlValue::Integer(expose.port.into()),
);
insert_string(&mut entry, "protocol", expose.protocol.as_deref());
TomlValue::Table(entry)
})
.collect(),
),
);
}
table
}
fn copy_entry(copy: &CopyParams) -> TomlValue {
let mut entry = Table::new();
entry.insert("src".to_string(), TomlValue::String(copy.src.clone()));
entry.insert("dest".to_string(), TomlValue::String(copy.dest.clone()));
if let Some(mode) = copy.mode {
entry.insert("mode".to_string(), TomlValue::Integer(mode.into()));
}
insert_string(&mut entry, "owner", copy.owner.as_deref());
TomlValue::Table(entry)
}
fn start_table(start: &StartParams) -> Table {
let mut table = Table::new();
insert_string(&mut table, "cmd", start.cmd.as_deref());
insert_string(&mut table, "entrypoint", start.entrypoint.as_deref());
insert_string(&mut table, "user", start.user.as_deref());
table
}
pub fn mount(router: McpToolRouter<McpState>) -> McpToolRouter<McpState> {
router.with_tool(
McpState::jailfile_create_tool_info(),
McpState::jailfile_create_handler,
None,
)
}
#[cfg(test)]
#[path = "jailfile_create/tests.rs"]
mod tests;