import { describe, expect, test } from "bun:test"; import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; import { createServer, resolveMcpEnabled, SEAL_CLI_TOOL_NAME, SEAL_MCP_ENABLED_ENV, tokenizeCommand } from "./server.js"; describe("seal-use MCP server", () => { test("tokenizes quoted JSON without invoking a shell", () => { expect(tokenizeCommand( `seal-use tool seal_approval_search --json '{"keywords":["差旅 发票"]}' --corp corp-a` )).toEqual([ "seal-use", "tool", "seal_approval_search", "--json", '{"keywords":["差旅 发票"]}', "--corp", "corp-a" ]); }); test("defaults MCP tools to disabled and parses the plugin boolean setting", () => { const original = process.env[SEAL_MCP_ENABLED_ENV]; delete process.env[SEAL_MCP_ENABLED_ENV]; try { expect(resolveMcpEnabled()).toBe(false); expect(resolveMcpEnabled("false")).toBe(false); expect(resolveMcpEnabled("0")).toBe(false); expect(resolveMcpEnabled("true")).toBe(true); expect(resolveMcpEnabled("1")).toBe(true); expect(resolveMcpEnabled("yes")).toBe(true); } finally { if (original === undefined) delete process.env[SEAL_MCP_ENABLED_ENV]; else process.env[SEAL_MCP_ENABLED_ENV] = original; } }); test("advertises one tool and executes version through the CLI runner", async () => { const server = createServer({ enableCli: true }); const client = new Client({ name: "seal-use-test", version: "1.0.0" }); const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); try { await Promise.all([ server.connect(serverTransport), client.connect(clientTransport) ]); const listed = await client.listTools(); expect(listed.tools).toHaveLength(1); expect(listed.tools[0]?.name).toBe(SEAL_CLI_TOOL_NAME); expect(listed.tools[0]?.inputSchema).toMatchObject({ type: "object", required: ["command"] }); const result = await client.callTool({ name: SEAL_CLI_TOOL_NAME, arguments: { command: "seal-use version" } }); const content = result.content as Array<{ type: "text"; text: string }>; const text = content[0]; expect(result.isError).not.toBe(true); expect(text?.type).toBe("text"); if (text?.type === "text") { expect(JSON.parse(text.text)).toMatchObject({ name: "seal-use" }); } const invalid = await client.callTool({ name: SEAL_CLI_TOOL_NAME, arguments: { command: "not-a-command" } }); expect(invalid.isError).toBe(true); const invalidContent = invalid.content as Array<{ type: "text"; text: string }>; const errorText = invalidContent[0]; expect(errorText?.type).toBe("text"); if (errorText?.type === "text") { expect(errorText.text).toContain("Unknown command: not-a-command"); } } finally { await Promise.allSettled([client.close(), server.close()]); } }); test("does not advertise tools when MCP is disabled", async () => { const server = createServer({ enableCli: false }); const client = new Client({ name: "seal-home-disabled-test", version: "1.0.0" }); const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); try { await Promise.all([ server.connect(serverTransport), client.connect(clientTransport) ]); const listed = await client.listTools(); expect(listed.tools).toEqual([]); } finally { await Promise.allSettled([client.close(), server.close()]); } }); });