import { readFile } from "node:fs/promises"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it, vi } from "vitest"; import { type CapabilityRegistration, EXAMPLE_ECHO_TOOL_NAME, EXAMPLE_SUMMARY_PROMPT_NAME, TEMPLATE_STATUS_RESOURCE_URI, } from "../src/capabilities/index.js"; import { createMcpCore } from "../src/core/index.js"; type McpServerInternals = { _registeredTools: Record; _registeredResources: Record; _registeredPrompts: Record; }; describe("MCP core factory", () => { it("wires example capabilities through the shared core path", () => { const core = createMcpCore(); const serverInternals = core.server as unknown as McpServerInternals; expect(core.registry.tools.map((entry) => entry.name)).toEqual([ EXAMPLE_ECHO_TOOL_NAME, ]); expect(core.registry.resources.map((entry) => entry.name)).toEqual([ TEMPLATE_STATUS_RESOURCE_URI, ]); expect(core.registry.prompts.map((entry) => entry.name)).toEqual([ EXAMPLE_SUMMARY_PROMPT_NAME, ]); expect(Object.keys(serverInternals._registeredTools)).toEqual([ EXAMPLE_ECHO_TOOL_NAME, ]); expect(Object.keys(serverInternals._registeredResources)).toEqual([ TEMPLATE_STATUS_RESOURCE_URI, ]); expect(Object.keys(serverInternals._registeredPrompts)).toEqual([ EXAMPLE_SUMMARY_PROMPT_NAME, ]); }); it("creates an unconnected core that does not require transport wiring", () => { const core = createMcpCore(); expect(core.server.isConnected()).toBe(false); }); it("supports explicit registry composition and server capability wiring", () => { const registerCustom: CapabilityRegistration = (registry) => { registry.tools.push({ name: "custom.tool", kind: "tool", description: "Custom test capability", }); }; const registerServerCapabilities = vi.fn<(server: unknown) => void>(); const core = createMcpCore({ capabilityRegistrations: [registerCustom], registerServerCapabilities, }); expect(core.registry.tools.map((entry) => entry.name)).toEqual(["custom.tool"]); expect(core.registry.resources).toEqual([]); expect(core.registry.prompts).toEqual([]); expect(registerServerCapabilities).toHaveBeenCalledTimes(1); expect(registerServerCapabilities).toHaveBeenCalledWith(core.server); }); it("does not import stdio/http transport wrappers in shared core", async () => { const here = dirname(fileURLToPath(import.meta.url)); const coreModulePath = resolve(here, "../src/core/mcp-core.ts"); const source = await readFile(coreModulePath, "utf8"); expect(source).not.toMatch(/server\/(stdio|sse|streamableHttp)\.js/); expect(source).not.toMatch(/(?:express|hono)/); }); });