| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- import { mkdtemp, rm } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import type { McpPort } from "@zcode/contracts";
- import { describe, expect, it, vi } from "vitest";
- import {
- zcodeMcpListResultSchema,
- zcodeMcpListParamsSchema,
- zcodeProtocolMethods,
- zcodeProtocolSessionMethodContracts,
- } from "@zcode/shared";
- import { listMcpServers } from "../src/zcode-protocol/mcp.js";
- import type { ZCodeProtocolAgentServerContext } from "../src/zcode-protocol/server-types.js";
- describe("mcp protocol contracts", () => {
- it("registers mcp/list", () => {
- expect(zcodeProtocolMethods.mcpList).toBe("mcp/list");
- expect(zcodeProtocolSessionMethodContracts[zcodeProtocolMethods.mcpList]).toBeDefined();
- });
- it("parses an MCP status snapshot", () => {
- const parsed = zcodeMcpListResultSchema.parse({
- statuses: {
- filesystem: {
- status: "disconnected",
- transport: "stdio",
- toolCount: 0,
- updatedAt: "2026-06-23T00:00:00.000Z",
- },
- },
- });
- expect(parsed.statuses.filesystem?.status).toBe("disconnected");
- });
- it("defaults mcp/list mode to connect and accepts status-only mode", () => {
- expect(
- zcodeMcpListParamsSchema.parse({
- workspace: {
- workspaceKey: "/tmp/project",
- workspacePath: "/tmp/project",
- },
- }).mode,
- ).toBe("connect");
- expect(
- zcodeMcpListParamsSchema.parse({
- workspace: {
- workspaceKey: "/tmp/project",
- workspacePath: "/tmp/project",
- },
- mode: "status",
- }).mode,
- ).toBe("status");
- });
- it("rejects invalid MCP status values", () => {
- expect(() =>
- zcodeMcpListResultSchema.parse({
- statuses: {
- broken: {
- status: "unknown",
- transport: "stdio",
- toolCount: 0,
- updatedAt: "2026-06-23T00:00:00.000Z",
- },
- },
- }),
- ).toThrow();
- });
- it("parses HTTP MCP OAuth client credentials in protocol params", () => {
- const parsed = zcodeMcpListParamsSchema.parse({
- workspace: {
- workspaceKey: "/tmp/project",
- workspacePath: "/tmp/project",
- },
- mcpServers: [
- {
- name: "protected",
- type: "http",
- url: "https://mcp.example.test/mcp",
- headers: [],
- oauth: {
- type: "client_credentials",
- clientId: "zcode-client",
- clientSecret: "secret",
- scope: "mcp:tools",
- },
- },
- ],
- });
- expect(parsed.mcpServers?.[0]).toMatchObject({
- name: "protected",
- oauth: {
- type: "client_credentials",
- clientId: "zcode-client",
- },
- });
- });
- it("parses HTTP MCP OAuth authorization code in protocol params", () => {
- const parsed = zcodeMcpListParamsSchema.parse({
- workspace: {
- workspaceKey: "/tmp/project",
- workspacePath: "/tmp/project",
- },
- mcpServers: [
- {
- name: "figma",
- type: "http",
- url: "https://mcp.figma.com/mcp",
- headers: [],
- oauth: {
- type: "authorization_code",
- clientName: "ZCode",
- redirectPath: "/oauth/callback/mcp/figma",
- scope: "mcp:connect",
- },
- },
- ],
- });
- expect(parsed.mcpServers?.[0]).toMatchObject({
- name: "figma",
- oauth: {
- type: "authorization_code",
- clientName: "ZCode",
- redirectPath: "/oauth/callback/mcp/figma",
- scope: "mcp:connect",
- },
- });
- });
- });
- // Bugfix 回归:设置页刷新的 mcp/list 必须带 revalidate,否则进程级 `protocol-settings` lease
- // 会命中连接池里的旧 entry 并回放陈旧快照(见 adapters/src/mcp/pool.ts revalidateEntry)。
- describe("mcp/list connection revalidation", () => {
- it("asks the pool to revalidate on the default connect mode and skips it for status mode", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-mcp-list-revalidate-"));
- const previousStorageDir = process.env.ZCODE_STORAGE_DIR;
- process.env.ZCODE_STORAGE_DIR = join(root, "cli");
- const connectConfiguredServers = vi.fn(async () => ({ statuses: {}, tools: [] }));
- const status = vi.fn(async () => ({}));
- const mcpPort = {
- callTool: vi.fn(),
- close: vi.fn(async () => undefined),
- connectConfiguredServers,
- connectServer: vi.fn(),
- disconnectServer: vi.fn(),
- listTools: vi.fn(async () => []),
- status,
- } as unknown as McpPort;
- const context = {
- deps: { env: process.env, mcpPort },
- } as unknown as ZCodeProtocolAgentServerContext;
- const params = {
- mcpServers: [],
- workspace: { workspaceKey: root, workspacePath: root },
- };
- try {
- await listMcpServers(context, params);
- expect(connectConfiguredServers).toHaveBeenCalledWith(
- expect.anything(),
- expect.objectContaining({ revalidate: true }),
- );
- connectConfiguredServers.mockClear();
- await listMcpServers(context, { ...params, mode: "status" });
- expect(connectConfiguredServers).not.toHaveBeenCalled();
- } finally {
- if (previousStorageDir === undefined) delete process.env.ZCODE_STORAGE_DIR;
- else process.env.ZCODE_STORAGE_DIR = previousStorageDir;
- await rm(root, { force: true, recursive: true });
- }
- });
- });
|