|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `serverInfo.version` — what an MCP server answers in `initialize`. |
| 5 | + * |
| 6 | + * `MCPServerPluginOptions.version` is published as "Defaults to package |
| 7 | + * version". Two literal `'1.0.0'` defaults (the plugin's, and |
| 8 | + * `MCPServerRuntime`'s) made every deployment built without the option answer |
| 9 | + * `1.0.0` while the package was elsewhere, so the registry listing in |
| 10 | + * `server.json` and every running server disagreed. Both now read the |
| 11 | + * package's own manifest; an explicit `version` still overrides it. |
| 12 | + * |
| 13 | + * The expected value is read from `package.json` HERE, never written down: a |
| 14 | + * literal in this file would rot at the next release, and would let a |
| 15 | + * regression that re-introduces some other constant pass on the day the two |
| 16 | + * happen to agree. |
| 17 | + */ |
| 18 | + |
| 19 | +import { readFileSync } from 'node:fs'; |
| 20 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 21 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 22 | +import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'; |
| 23 | +import { LiteKernel } from '@objectstack/core'; |
| 24 | + |
| 25 | +import { MCPServerPlugin } from './plugin.js'; |
| 26 | +import { MCPServerRuntime } from './mcp-server-runtime.js'; |
| 27 | + |
| 28 | +const MANIFEST_VERSION: string = JSON.parse( |
| 29 | + readFileSync(new URL('../package.json', import.meta.url), 'utf8'), |
| 30 | +).version; |
| 31 | + |
| 32 | +const OVERRIDE = '9.9.9-override.1'; |
| 33 | + |
| 34 | +const INITIALIZE = { |
| 35 | + jsonrpc: '2.0', |
| 36 | + id: 0, |
| 37 | + method: 'initialize', |
| 38 | + params: { protocolVersion: '2025-03-26', capabilities: {}, clientInfo: { name: 'pin', version: '0' } }, |
| 39 | +}; |
| 40 | + |
| 41 | +/** `initialize` over the Streamable HTTP door (`POST /api/v1/mcp`). */ |
| 42 | +async function serverInfoOverHttp(runtime: MCPServerRuntime): Promise<{ name: string; version: string }> { |
| 43 | + const res = await runtime.handleHttpRequest( |
| 44 | + new Request('http://localhost/api/v1/mcp', { |
| 45 | + method: 'POST', |
| 46 | + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, |
| 47 | + body: JSON.stringify(INITIALIZE), |
| 48 | + }), |
| 49 | + { parsedBody: INITIALIZE }, |
| 50 | + ); |
| 51 | + const json = (await res.json()) as { result: { serverInfo: { name: string; version: string } } }; |
| 52 | + return json.result.serverInfo; |
| 53 | +} |
| 54 | + |
| 55 | +/** `initialize` against the long-lived server (the stdio composition), over an in-memory pair. */ |
| 56 | +async function serverVersionOnLongLivedServer(runtime: MCPServerRuntime): Promise<string | undefined> { |
| 57 | + const [clientSide, serverSide] = InMemoryTransport.createLinkedPair(); |
| 58 | + const client = new Client({ name: 'pin', version: '0' }); |
| 59 | + await runtime.server.connect(serverSide); |
| 60 | + await client.connect(clientSide); |
| 61 | + try { |
| 62 | + return client.getServerVersion()?.version; |
| 63 | + } finally { |
| 64 | + await client.close(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function pluginContext() { |
| 69 | + const services = new Map<string, unknown>(); |
| 70 | + return { |
| 71 | + services, |
| 72 | + ctx: { |
| 73 | + registerService: vi.fn((name: string, service: unknown) => { |
| 74 | + services.set(name, service); |
| 75 | + }), |
| 76 | + getService: vi.fn((name: string) => { |
| 77 | + if (!services.has(name)) throw new Error(`Service "${name}" not found`); |
| 78 | + return services.get(name); |
| 79 | + }), |
| 80 | + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, |
| 81 | + }, |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +async function runtimeOf(plugin: MCPServerPlugin): Promise<MCPServerRuntime> { |
| 86 | + const { ctx, services } = pluginContext(); |
| 87 | + await plugin.init(ctx as never); |
| 88 | + return services.get('mcp') as MCPServerRuntime; |
| 89 | +} |
| 90 | + |
| 91 | +describe('serverInfo.version — the default is the package version', () => { |
| 92 | + it('MCPServerRuntime built with no config answers the package version', async () => { |
| 93 | + expect((await serverInfoOverHttp(new MCPServerRuntime())).version).toBe(MANIFEST_VERSION); |
| 94 | + }); |
| 95 | + |
| 96 | + it('MCPServerPlugin built with no options answers the package version (the `os serve` auto-registration shape)', async () => { |
| 97 | + const runtime = await runtimeOf(new MCPServerPlugin()); |
| 98 | + expect((await serverInfoOverHttp(runtime)).version).toBe(MANIFEST_VERSION); |
| 99 | + }); |
| 100 | + |
| 101 | + it('the long-lived (stdio) server answers the package version too', async () => { |
| 102 | + expect(await serverVersionOnLongLivedServer(new MCPServerRuntime())).toBe(MANIFEST_VERSION); |
| 103 | + expect(await serverVersionOnLongLivedServer(await runtimeOf(new MCPServerPlugin()))).toBe(MANIFEST_VERSION); |
| 104 | + }); |
| 105 | + |
| 106 | + it("the kernel plugin's own `version` is the same one value", () => { |
| 107 | + expect(new MCPServerPlugin().version).toBe(MANIFEST_VERSION); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe('serverInfo.version — an explicit override is answered as given', () => { |
| 112 | + it('MCPServerRuntime config.version', async () => { |
| 113 | + const runtime = new MCPServerRuntime({ version: OVERRIDE }); |
| 114 | + expect((await serverInfoOverHttp(runtime)).version).toBe(OVERRIDE); |
| 115 | + expect(await serverVersionOnLongLivedServer(new MCPServerRuntime({ version: OVERRIDE }))).toBe(OVERRIDE); |
| 116 | + }); |
| 117 | + |
| 118 | + it('MCPServerPlugin options.version', async () => { |
| 119 | + const runtime = await runtimeOf(new MCPServerPlugin({ version: OVERRIDE })); |
| 120 | + expect((await serverInfoOverHttp(runtime)).version).toBe(OVERRIDE); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe('an unreadable manifest degrades to an honest answer, never to a plugin the kernel refuses', () => { |
| 125 | + afterEach(() => { |
| 126 | + vi.doUnmock('node:module'); |
| 127 | + vi.resetModules(); |
| 128 | + }); |
| 129 | + |
| 130 | + it("serverInfo.version says 'unknown' and the plugin still loads", async () => { |
| 131 | + // A bundle with no `package.json` beside it: `createRequire(...)('../package.json')` throws. |
| 132 | + vi.resetModules(); |
| 133 | + vi.doMock('node:module', async (importOriginal) => { |
| 134 | + const actual = await importOriginal<typeof import('node:module')>(); |
| 135 | + return { |
| 136 | + ...actual, |
| 137 | + createRequire: () => { |
| 138 | + throw new Error('ENOENT: no manifest beside this bundle'); |
| 139 | + }, |
| 140 | + }; |
| 141 | + }); |
| 142 | + const { MCPServerPlugin: BlindPlugin } = await import('./plugin.js'); |
| 143 | + const plugin = new BlindPlugin(); |
| 144 | + |
| 145 | + // The wire answer is a free string: it says it does not know. |
| 146 | + const { ctx, services } = pluginContext(); |
| 147 | + await plugin.init(ctx as never); |
| 148 | + expect((await serverInfoOverHttp(services.get('mcp') as MCPServerRuntime)).version).toBe('unknown'); |
| 149 | + |
| 150 | + // The kernel plugin's `version` has a grammar (SemVer 2.0.0): a placeholder string there would |
| 151 | + // be refused by both kernels. `use()` throws on a refused contract. |
| 152 | + const kernel = new LiteKernel({ logger: { level: 'silent' } }); |
| 153 | + expect(() => kernel.use(plugin)).not.toThrow(); |
| 154 | + }); |
| 155 | +}); |
0 commit comments