Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

3.4 KB
Raw Blame
import { executeCapabilityCall } from "@/lib/capabilities/execute";
import { validateFederationArguments, FEDERATION_WORKER_ID } from "@/lib/federation/security";
import { executeSiCoderFederation } from "@/lib/federation/si-coder-runtime";
import { consumeFederationApproval } from "@/lib/federation/local-approval";
import { resolveProjectHint } from "@/lib/host/projects-api";
import { msoCapabilityRuntime } from "../capability-runtime";
import { TOOLS_BY_NAME } from "../tools";
import { BATON_SERVER, type FederationRequest } from "./types";

const ACTOR = "federation:batonly";

function recursiveMsoRequest(operation: string, args: Record<string, unknown>): boolean {
  if (operation !== "project_mcp_call") return false;
  return typeof args.server === "string" && args.server === BATON_SERVER;
}

export async function executeMsoFederation(request: FederationRequest) {
  const args = validateFederationArguments(request.arguments ?? {});
  const tool = TOOLS_BY_NAME.get(request.operation);
  if (!tool) throw new Error(`unknown MSO federation tool: ${request.operation}`);
  if (tool.scope !== request.scope) {
    throw new Error(`MSO scope mismatch: request=${request.scope}, runtime=${tool.scope}`);
  }
  if (tool.scope !== "read" && !request.confirmed) {
    throw new Error("MSO write/exec federation requires explicit confirmation");
  }
  if (recursiveMsoRequest(request.operation, args)) {
    throw new Error("recursive Batonly project MCP federation is not allowed");
  }
  // shortcut: durable federation jobs/delegation stay closed until they propagate a revocable local grant.
  if (["exec_job_start", "agent_subagent_run", "local_agent_request", "project_agent_run", "a2a_agent_spawn", "workflow_graph_run", "flow_run"].includes(tool.name)) throw new Error("federation durable delegation requires a propagated local grant");
  const projectPath = await consumeFederationApproval(request);
  if (typeof args.project === "string" && (await resolveProjectHint(args.project))?.path !== projectPath) throw new Error("federation project argument differs from the approved project");
  if (["exec_run", "exec_job_start"].includes(tool.name)) {
    if (args.cwd !== undefined && args.cwd !== projectPath) throw new Error("federation cwd differs from the approved project");
    args.cwd = projectPath;
  }

  const outcome = await executeCapabilityCall({
    tool,
    args,
    scope: tool.scope,
    actor: ACTOR,
    context: {
      principal: FEDERATION_WORKER_ID,
      capabilities: msoCapabilityRuntime,
    },
  });
  if (outcome.kind === "protocol_error") throw new Error(`MSO invalid arguments: ${outcome.message}`);
  if (outcome.kind === "error") throw new Error(outcome.message);
  return outcome.result;
}

export async function executeFederationRequest(request: FederationRequest, cwd = process.cwd()) {
  if (request.scope !== "read" && !request.confirmed) {
    throw new Error("federation write/exec request is missing explicit confirmation");
  }
  const args = validateFederationArguments(request.arguments ?? {});
  return request.source === "mso"
    ? executeMsoFederation({ ...request, arguments: args })
    : executeSiCoderFederation({
        cwd,
        id: request.id,
        projectId: request.projectId,
        operation: request.operation,
        arguments: args,
        scope: request.scope,
        confirmed: request.confirmed,
      });
}

export const msoFederationToolCount = () => TOOLS_BY_NAME.size;