Skip to content

Commit 767be41

Browse files
QuantGeekDevclaude
andcommitted
fix: dynamically resolve CLI and template version from package.json
Closes #117 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent dde5519 commit 767be41

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/cli/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
import { createRequire } from 'module';
23
import { Command } from 'commander';
34
import { createProject } from './project/create.js';
45
import { addTool } from './project/add-tool.js';
@@ -7,9 +8,12 @@ import { addResource } from './project/add-resource.js';
78
import { buildFramework } from './framework/build.js';
89
import { validateCommand } from './commands/validate.js';
910

11+
const require = createRequire(import.meta.url);
12+
const frameworkPackageJson = require('../../package.json');
13+
1014
const program = new Command();
1115

12-
program.name('mcp').description('CLI for managing MCP server projects').version('0.2.2');
16+
program.name('mcp').description('CLI for managing MCP server projects').version(frameworkPackageJson.version);
1317

1418
program.command('build').description('Build the MCP project').action(buildFramework);
1519

src/cli/project/create.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { createRequire } from 'module';
12
import { spawnSync } from 'child_process';
23
import { mkdir, writeFile } from 'fs/promises';
34
import { join } from 'path';
45
import prompts from 'prompts';
56
import { generateReadme } from '../templates/readme.js';
67
import { execa } from 'execa';
78

9+
const require = createRequire(import.meta.url);
10+
const frameworkPackageJson = require('../../../package.json');
11+
812
export async function createProject(
913
name?: string,
1014
options?: { http?: boolean; cors?: boolean; port?: number; oauth?: boolean; install?: boolean; example?: boolean }
@@ -74,7 +78,7 @@ export async function createProject(
7478
start: 'node dist/index.js',
7579
},
7680
dependencies: {
77-
'mcp-framework': '^0.2.2',
81+
'mcp-framework': `^${frameworkPackageJson.version}`,
7882
...(options?.oauth && { dotenv: '^16.3.1' }),
7983
},
8084
devDependencies: {

tests/cli/cli-version.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, it, expect } from '@jest/globals';
2+
import { createRequire } from 'module';
3+
4+
const require = createRequire(import.meta.url);
5+
6+
describe('CLI version consistency', () => {
7+
it('should have consistent version across package.json and CLI', () => {
8+
const packageJson = require('../../package.json');
9+
expect(packageJson.version).toBeDefined();
10+
expect(typeof packageJson.version).toBe('string');
11+
// Version should be a valid semver
12+
expect(packageJson.version).toMatch(/^\d+\.\d+\.\d+/);
13+
});
14+
15+
it('should use the framework version for created projects', async () => {
16+
// Read the create.ts source to verify it references frameworkPackageJson.version
17+
const { readFileSync } = await import('fs');
18+
const createSource = readFileSync('src/cli/project/create.ts', 'utf-8');
19+
20+
// Should NOT contain hardcoded mcp-framework version
21+
expect(createSource).not.toMatch(/'mcp-framework':\s*'\^0\.2\.2'/);
22+
// Should reference frameworkPackageJson.version dynamically
23+
expect(createSource).toContain('frameworkPackageJson.version');
24+
});
25+
26+
it('should use dynamic version in CLI program definition', async () => {
27+
const { readFileSync } = await import('fs');
28+
const cliSource = readFileSync('src/cli/index.ts', 'utf-8');
29+
30+
// Should NOT contain hardcoded version
31+
expect(cliSource).not.toMatch(/\.version\(['"]0\.2\.\d+['"]\)/);
32+
// Should reference frameworkPackageJson.version
33+
expect(cliSource).toContain('frameworkPackageJson.version');
34+
});
35+
});

0 commit comments

Comments
 (0)