Skip to content

Open-source status page, incident response and uptime monitoring

6.1 KB
Raw Blame
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
import type { Workspace } from "@openstatus/db/src/schema";
import { afterEach, describe, expect, test } from "vitest";

import { toServiceCtx } from "./adapter";
import { PUBLIC_RESOURCES, readPublicResource } from "./resources";
import { createMcpServer } from "./server";

const realFetch = globalThis.fetch;

afterEach(() => {
  globalThis.fetch = realFetch;
});

function stubFetch(impl: () => Promise<Response>) {
  globalThis.fetch = impl as unknown as typeof fetch;
}

// Fails on a missing or non-string `text` instead of coercing it to "undefined".
function textOf(result: { contents: object[] }): string {
  const first = result.contents[0];
  const text = first && "text" in first ? first.text : undefined;
  expect(text).toBeTypeOf("string");
  return text as string;
}

describe("public MCP resources", () => {
  test("every entry is addressable and typed", () => {
    expect(PUBLIC_RESOURCES.length).toBeGreaterThan(0);
    const names = new Set<string>();
    for (const resource of PUBLIC_RESOURCES) {
      expect(resource.uri.startsWith("https://")).toBe(true);
      expect(resource.mimeType.length).toBeGreaterThan(0);
      expect(resource.description.length).toBeGreaterThan(0);
      expect(names.has(resource.name)).toBe(false);
      names.add(resource.name);
    }
  });

  test("reads echo the declared uri and mimeType", async () => {
    stubFetch(() => Promise.resolve(new Response("# doc")));
    for (const resource of PUBLIC_RESOURCES) {
      const result = await readPublicResource(resource);
      expect(result.contents).toHaveLength(1);
      expect(result.contents[0].uri).toBe(resource.uri);
      expect(result.contents[0].mimeType).toBe(resource.mimeType);
    }
  });

  test("never returns an empty body when the document is unreachable", async () => {
    stubFetch(() => Promise.reject(new Error("network down")));
    for (const resource of PUBLIC_RESOURCES) {
      const result = await readPublicResource(resource);
      expect(textOf(result).length).toBeGreaterThan(0);
    }
  });

  test("falls back rather than serving a blank document", async () => {
    const remote = PUBLIC_RESOURCES.find((r) => r.name === "site-index");
    if (!remote) throw new Error("site-index resource missing");

    stubFetch(() => Promise.resolve(new Response("   ")));
    const blank = await readPublicResource(remote);
    expect(textOf(blank).trim().length).toBeGreaterThan(0);

    stubFetch(() =>
      Promise.resolve(new Response("not found", { status: 404 })),
    );
    const missing = await readPublicResource(remote);
    expect(textOf(missing).trim().length).toBeGreaterThan(0);
  });

  test("serves the fetched document when it resolves", async () => {
    const remote = PUBLIC_RESOURCES.find(
      (r) => r.name === "mcp-server-reference",
    );
    if (!remote) throw new Error("mcp-server-reference resource missing");

    stubFetch(() => Promise.resolve(new Response("# MCP server\n\nlive copy")));
    const result = await readPublicResource(remote);
    expect(textOf(result)).toContain("live copy");
  });

  test("ships the OpenAPI document without a network call", async () => {
    stubFetch(() => Promise.reject(new Error("must not be called")));
    const spec = PUBLIC_RESOURCES.find(
      (r) => r.name === "openapi-specification",
    );
    if (!spec) throw new Error("openapi resource missing");

    const result = await readPublicResource(spec);
    const parsed = JSON.parse(textOf(result));
    expect(parsed.openapi).toBe("3.1.0");
    expect(parsed.servers[0].url).toBe("https://api.openstatus.dev");
  });
});

/**
 * Connect a client to the server the route actually builds. The public
 * documents live on the authenticated server — `/mcp` has no anonymous
 * surface, so there is no other server to connect to. `limits` is real
 * because tool registration reads it (`registerAuditTools` gates on
 * `audit-log`); the rest of the workspace is never touched by a
 * `resources/*` call.
 */
async function connectServer() {
  const server = createMcpServer(
    toServiceCtx({
      workspace: {
        id: 1,
        slug: "ws",
        name: "ws",
        limits: {},
      } as unknown as Workspace,
      apiKey: { id: "k1", scopes: ["*"] },
      requestId: "req-resources",
    }),
  );
  const client = new Client({ name: "probe", version: "test" });
  const [clientTransport, serverTransport] =
    InMemoryTransport.createLinkedPair();
  await Promise.all([
    server.connect(serverTransport),
    client.connect(clientTransport),
  ]);
  return { client, close: () => client.close() };
}

describe("public MCP documents over the transport", () => {
  test("advertises the resources capability on initialize", async () => {
    stubFetch(() => Promise.resolve(new Response("# doc")));
    const { client, close } = await connectServer();
    try {
      expect(client.getServerCapabilities()?.resources).toBeDefined();
    } finally {
      await close();
    }
  });

  test("resources/list returns every public document", async () => {
    stubFetch(() => Promise.resolve(new Response("# doc")));
    const { client, close } = await connectServer();
    try {
      const { resources } = await client.listResources();
      expect(resources.map((r) => r.uri).sort()).toEqual(
        PUBLIC_RESOURCES.map((r) => r.uri).sort(),
      );
      for (const resource of resources) {
        expect(resource.name.length).toBeGreaterThan(0);
        expect(resource.description?.length).toBeGreaterThan(0);
        expect(resource.mimeType?.length).toBeGreaterThan(0);
      }
    } finally {
      await close();
    }
  });

  test("resources/read returns a non-empty body for each", async () => {
    stubFetch(() => Promise.resolve(new Response("# doc")));
    const { client, close } = await connectServer();
    try {
      for (const resource of PUBLIC_RESOURCES) {
        const result = await client.readResource({ uri: resource.uri });
        expect(textOf(result).length).toBeGreaterThan(0);
      }
    } finally {
      await close();
    }
  });
});