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.

7.8 KB
Raw Blame
import { isGoogleProvider } from "@/lib/infra/google-native-config";
import { SETUP_METHOD_IDS } from "@/lib/infra/setup-guidance";
import {metadataOnly} from "@/lib/infra/identity";
import { selectionFrom, safeActionInput } from "@/lib/infra/connection-dispatch";
import { integrationSnapshot } from "@/lib/infra/connection-service";
import { openIntegrationSetup } from "@/lib/infra/setup-capability";
import { INFRA_PROVIDER_IDS } from "@/lib/infra/types";
import { MSO_ORIGIN } from "./ui-config";
import { type McpTool, S, str, opt, READ_ONLY, mcpDirect } from "./tool-kit";
import { MSO_PAGE_URI } from "./ui-surface";
import { publicSurfaceApps, resolveSurfaceRoute, type ResolvedSurface } from "./surface-catalog";

const APP_SCHEMA = {
  type: "object",
  properties: {
    id: { type: "string" }, title: { type: "string" }, description: { type: "string" }, origin: { type: "string" },
    startPath: { type: "string" }, renderer: { type: "string", enum: ["iframe", "remote"] },
    presentation: { type: "string", enum: ["inline", "fullscreen", "pip"] },
    environment: { type: "string", enum: ["development", "preview", "production", "other"] }, reason: { type: "string" }, project: { type: "string" },
  },
  required: ["id", "title", "description", "origin", "startPath", "renderer", "presentation", "environment"],
  additionalProperties: false,
} as const;

const PAGE_OUTPUT = {
  type: "object",
  properties: {
    route: { type: "string" }, kind: { type: "string", enum: ["home", "monitor", "project", "diff", "browser", "app", "integrations", "sessions", "assets"] },
    title: { type: "string" }, openPath: { type: "string" }, project: { type: "string" }, sha: { type: "string" },
    setup: { type: "object" }, integrations: {type:"object"},
    app: { type: "object" }, catalog: { type: "array", items: APP_SCHEMA },
  },
  required: ["route", "kind", "title", "openPath", "catalog"],
  additionalProperties: false,
} as const;

const PAGE_INPUT = S({
  route: { type: "string", minLength: 1, maxLength: 1024, description: "MSO Page route. External URLs are rejected." },
  project: { type: "string", description: "Project id/path/name for project/diff/assets, or exact reviewed project value for /apps/<id>." },
  sha: { type: "string", description: "Optional commit SHA for /diff." },
}, ["route"]);

function publicResolved(resolved: ResolvedSurface): Record<string, unknown> {
  const { app, ...rest } = resolved;
  if (!app) return rest;
  const { sandbox: _sandbox, externalAuthPath: _externalAuthPath, ...safeApp } = app;
  return { ...rest, app: safeApp };
}

const renderPage = async (input:Record<string,unknown>) => {
  const resolved=publicResolved(await resolveSurfaceRoute(str(input,"route"),{project:opt(input,"project"),sha:opt(input,"sha")}));
  return {...resolved,catalog:await publicSurfaceApps(),...(resolved.kind==="integrations"?{integrations:await integrationSnapshot()}: {})};
};

export const SURFACE_TOOLS: McpTool[] = [
  {
    name: "integration_setup_open",
    title: "Private Setup",
    description: "Open a native MSO Integrations credential form in ChatGPT. Requires write scope. The user enters a key directly in the form, which sends it only to the MSO HTTPS server for validation and owner-only storage. Never pass credentials in tool arguments or chat. The provider-bound session expires after ten minutes and is consumed after a successful save. MSO needs no external credential plugin. Composio project and organization keys use different methods. Native Google OAuth app setup stores client configuration only; Google account consent uses the native browser authorization flow.",
    chatgptDescription: "Open private credential setup.",
    scope: "write",
    annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: false, idempotentHint: false },
    audit: { action: "infra.write", targetArg: "provider" },
    limit: { key: "integration.setup", max: 10, windowMs: 60_000 },
    inputSchema: S({ user:{type:"string",maxLength:64},connection:{type:"string",maxLength:64}, provider: { type: "string", enum: INFRA_PROVIDER_IDS.filter(provider => !isGoogleProvider(provider)) }, method: { type: "string", enum: SETUP_METHOD_IDS.filter(method => method !== "oauth2"), description: "Must match this connection; omitted uses its stored auth method." } }, ["user","provider","connection"]),
    outputSchema: PAGE_OUTPUT,
    meta: {
      ui: { resourceUri: MSO_PAGE_URI, visibility: ["model", "app"] },
      "ui/resourceUri": MSO_PAGE_URI,
      "openai/widgetAccessible": true,
      "openai/toolInvocation/invoking": "Opening setup…",
      "openai/toolInvocation/invoked": "Setup opened",
    },
    run: async (input, context) => {
      metadataOnly(safeActionInput(input));
      const grant = await openIntegrationSetup(str(input, "provider"), context.principal ?? context.actor ?? "", opt(input, "method"), selectionFrom(input));
      const output = { route: "/integrations", kind: "integrations", title: "Integrations", openPath: "/integrations", catalog: await publicSurfaceApps(), setup: grant.setup };
      return mcpDirect([{ type: "text", text: `Secure ${grant.setup.title} setup opened. Enter the credential in the form, never in chat.` }], false, output,
        { integrationSetup: { token: grant.token, endpoint: `${MSO_ORIGIN}/api/integrations/setup` } });
    },
  },
  {
    name: "mso_surface_apps_list",
    title: "List MSO Page Apps",
    description: "List the server-owned catalog of development or production apps available from the ChatGPT MSO Page. The model cannot add URLs or HTML. Reviewed iframe apps render sandboxed previews; remote apps retain a browser handoff. Use the returned project identity when opening a project demo.",
    chatgptDescription: "List reviewed apps available from the ChatGPT MSO Page. Use renderer, project, origin and startPath to choose a sandboxed demo or remote-browser handoff.",
    scope: "read", annotations: READ_ONLY,
    inputSchema: S({}),
    outputSchema: { type: "object", properties: { apps: { type: "array", items: APP_SCHEMA } }, required: ["apps"], additionalProperties: false },
    run: async () => ({ apps: await publicSurfaceApps() }),
  },
  {
    name: "render_mso_page",
    title: "Render MSO Page",
    description: "Render the full MSO Page MCP App for native operator views or registry-backed external app handoff. Use an MSO-style route such as /, /integrations, /monitor, /project, /diff, /sessions, /assets, /browser, or /apps/<reviewed-app-id>. For project/diff views pass project separately. This tool never accepts raw HTML or arbitrary external URLs; external apps are resolved from the server-owned registry, embedded only when renderer=iframe, otherwise handed to the browser.",
    chatgptDescription: "Render the full secure MSO Page in ChatGPT. Native routes: /, /integrations, /monitor, /project, /diff, /sessions, /assets, /browser; configured app route: /apps/<configured-app-id>. Raw HTML and arbitrary URLs are rejected.",
    scope: "read", annotations: READ_ONLY,
    inputSchema: PAGE_INPUT,
    outputSchema: PAGE_OUTPUT,
    meta: {
      ui: { resourceUri: MSO_PAGE_URI, visibility: ["model", "app"] },
      "ui/resourceUri": MSO_PAGE_URI,
      "openai/toolInvocation/invoking": "Opening MSO Page…",
      "openai/toolInvocation/invoked": "MSO Page opened",
      "openai/widgetAccessible": true,
    },
    run: renderPage,
  },
  {
    name: "render_mso_surface",
    title: "Render MSO Surface (Compatibility)",
    description: "Compatibility alias for older MSO Page widgets. New model calls must use render_mso_page.",
    chatgptDescription: "Compatibility-only app bridge for a cached MSO Surface. Use render_mso_page for new user-visible pages.",
    scope: "read", annotations: READ_ONLY,
    inputSchema: PAGE_INPUT,
    outputSchema: PAGE_OUTPUT,
    meta: {
      ui: { visibility: ["app"] },
      "openai/widgetAccessible": true,
    },
    run: renderPage,
  },
];