File size: 1,757 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Browser, BrowserContext, Page } from "@playwright/test";
import { TEST_USERS } from "../constants/test-users";

export const createMcpServer = async (
  access: {
    browser?: Browser;
    context?: BrowserContext;
    page?: Page;
  },
  server: {
    name: string;
    config?: {
      command?: string;
      args?: string[];
      [key: string]: any;
    };
    visibility?: "public" | "private";
  },
) => {
  try {
    let page: Page;
    if (!access.browser && !access.page && !access.context) {
      throw new Error("Browser, context, or page is required");
    }
    if (access.page) {
      page = access.page;
    }
    if (access.context) {
      page = await access.context.newPage();
    }
    if (access.browser) {
      const browserContext = await access.browser.newContext({
        storageState: TEST_USERS.admin.authFile,
      });
      page = await browserContext.newPage();
    }
    const response = await page!.request.post("/api/mcp", {
      headers: { "Content-Type": "application/json" },
      data: {
        name: server.name,
        config: server.config ?? {
          command: "node",
          args: ["tests/fixtures/test-mcp-server.js"],
        },
        visibility: server.visibility ?? "private",
      },
      timeout: 15000,
    });
    if (!response.ok()) {
      const errorBody = await response.text();
      throw new Error(
        `Failed to create MCP server: Status ${response.status()} - ${errorBody}`,
      );
    }
    const serverInfo = (await response.json()) as { id: string };
    if (!serverInfo.id) {
      throw new Error("Failed to create MCP server");
    }
    return serverInfo;
  } catch (error) {
    console.error("Error creating MCP server", error);
    throw error;
  }
};