fix(memory): read serverInfo.version from package.json (#4407)

Fixes #4406. The memory server previously hardcoded serverInfo.version as
"0.6.3", which no longer matched published calendar versions. Resolve
package.json from both source and dist layouts so Docker and npm installs
report the installed package version.

Co-authored-by: syf2211 <syf2211@users.noreply.github.com>
This commit is contained in:
石岳峰 2026-09-02 08:04:28 -07:00 • committed by GitHub
commit 96c49c7364
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 54 additions and 2 deletions

View file

@ -0,0 +1,25 @@
import { describe, it, expect } from 'vitest';
import { createRequire } from 'node:module';
import path from 'path';
import { fileURLToPath } from 'url';
import { resolvePackageVersion, SERVER_VERSION } from '../version.js';
const packageJson = createRequire(import.meta.url)('../package.json') as { version: string };
describe('server version', () => {
it('uses package.json version for serverInfo', () => {
expect(SERVER_VERSION).toBe(packageJson.version);
expect(resolvePackageVersion()).toBe(packageJson.version);
});
it('resolves package.json from the dist layout', () => {
const distDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'dist');
const distVersionPath = path.join(distDir, 'version.js');
expect(() => createRequire(distVersionPath)('./version.js')).not.toThrow();
const distModule = createRequire(distVersionPath)('./version.js') as {
SERVER_VERSION: string;
};
expect(distModule.SERVER_VERSION).toBe(packageJson.version);
});
});

View file

@ -8,6 +8,7 @@ import { promises as fs } from 'fs';
import path from 'path';
import { randomBytes } from 'crypto';
import { fileURLToPath } from 'url';
import { SERVER_VERSION } from './version.js';
// Define memory file path using environment variable with fallback
export const defaultMemoryPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'memory.jsonl');
@ -276,10 +277,9 @@ const RelationSchema = z.object({
relationType: z.string().describe("The type of the relation")
});
// The server instance and tools exposed to Claude
const server = new McpServer({
name: "memory-server",
version: "0.6.3",
version: SERVER_VERSION,
});
const RESOURCE_URI = "memory://knowledge-graph";

27
src/memory/version.ts Normal file
View file

@ -0,0 +1,27 @@
import { createRequire } from 'node:module';
import path from 'path';
import { fileURLToPath } from 'url';
export function resolvePackageVersion(): string {
const require = createRequire(import.meta.url);
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
const candidates = [
path.join(moduleDir, 'package.json'),
path.join(moduleDir, '..', 'package.json'),
];
for (const candidate of candidates) {
try {
const pkg = require(candidate) as { version?: string };
if (pkg.version) {
return pkg.version;
}
} catch {
// Try the next candidate when running from dist/ or source.
}
}
throw new Error('Could not locate package.json for server version');
}
export const SERVER_VERSION = resolvePackageVersion();