import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; import { afterEach, describe, expect, it } from "vitest"; import type { McpToolContext } from "./context.js"; import { createHiveGridMcpServer } from "./server-factory.js"; const EXPECTED_TOOL_NAMES = [ "hg_lead_search", "hg_lead_get", "hg_contact_search", "hg_contact_get", "hg_project_search", "hg_project_get", "hg_pipeline_list", "hg_activity_list", "hg_task_search", "hg_task_get", "hg_lead_create", "hg_project_set_stage", "hg_activity_add", "hg_task_create", "hg_task_set_status", ] as const; const connections: Array<{ client: Client; server: ReturnType }> = []; afterEach(async () => { await Promise.all(connections.splice(0).map(async ({ client, server }) => { await client.close().catch(() => undefined); await server.close().catch(() => undefined); })); }); describe("HiveGrid MCP tool registry", () => { it("publishes the complete 15-tool contract", async () => { const { client } = await connect(["mcp:read", "mcp:write"]); const result = await client.listTools(); expect(result.tools.map((tool) => tool.name)).toEqual(EXPECTED_TOOL_NAMES); }); it("marks task reads and writes correctly and requires explicit write confirmation", async () => { const { client } = await connect(["mcp:read", "mcp:write"]); const { tools } = await client.listTools(); const taskSearch = tools.find((tool) => tool.name === "hg_task_search"); const taskCreate = tools.find((tool) => tool.name === "hg_task_create"); const taskSetStatus = tools.find((tool) => tool.name === "hg_task_set_status"); expect(taskSearch?.annotations).toMatchObject({ readOnlyHint: true, destructiveHint: false, idempotentHint: true }); expect(taskCreate?.annotations).toMatchObject({ readOnlyHint: false, destructiveHint: false, idempotentHint: false }); expect(taskSetStatus?.annotations).toMatchObject({ readOnlyHint: false, destructiveHint: true, idempotentHint: true }); expect(taskCreate?.inputSchema.required).toEqual(expect.arrayContaining(["confirm", "title"])); expect(taskSetStatus?.inputSchema.required).toEqual(expect.arrayContaining(["confirm", "taskId", "status"])); expect(taskCreate?.inputSchema.properties).toMatchObject({ confirm: { const: true } }); expect(taskSetStatus?.inputSchema.properties).toMatchObject({ confirm: { const: true } }); }); it("rejects a task read before database access when the granular scope is missing", async () => { const { client } = await connect(["mcp:lead:read"]); const result = await client.callTool({ name: "hg_task_search", arguments: {} }); expect(result.isError).toBe(true); expect(result.content).toEqual([ expect.objectContaining({ type: "text", text: expect.stringContaining("insufficient_scope") }), ]); }); }); async function connect(scopes: readonly string[]): Promise<{ client: Client; server: ReturnType }> { const server = createHiveGridMcpServer(context(scopes)); const client = new Client({ name: "hivegrid-mcp-registry-test", version: "1.0.0" }); const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); await server.connect(serverTransport); await client.connect(clientTransport); const connection = { client, server }; connections.push(connection); return connection; } function context(scopes: readonly string[]): McpToolContext { return { principal: { credential: { kind: "oauth", id: "token_1", name: "Test client" }, tenantId: "tenant_1", tenantSlug: "demo", userId: "user_1", scopes, permissions: [], }, readDb: {} as McpToolContext["readDb"], writeDb: {} as McpToolContext["writeDb"], request: { id: "request_1", ip: "127.0.0.1" }, }; }