Skip to content

Commit bb88e54

Browse files
committed
Fixed some hardcoded info stuff
1 parent 6b4f23f commit bb88e54

File tree

6 files changed

+106
-26
lines changed

6 files changed

+106
-26
lines changed

mcp-openapi/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -636,27 +636,29 @@ export async function startServer(options?: ServerOptions & { mode?: 'stdio' | '
636636

637637
```typescript
638638
interface ServerOptions {
639-
specsDir?: string; // Directory containing OpenAPI specs
640-
configFile?: string; // Path to MCP configuration file
641-
promptsDir?: string; // Directory containing prompt templates
642-
port?: number; // HTTP server port
643-
verbose?: boolean; // Enable verbose logging
644-
baseUrl?: string; // Base URL for backend APIs (overrides config file)
639+
specsDir?: string; // Directory containing OpenAPI specs
640+
configFile?: string; // Path to MCP configuration file
641+
promptsDir?: string; // Directory containing prompt templates
642+
port?: number; // HTTP server port
643+
verbose?: boolean; // Enable verbose logging
644+
baseUrl?: string; // Base URL for backend APIs (overrides config file)
645+
maxToolNameLength?: number; // Maximum length for generated tool names (default: 48)
645646
}
646647
```
647648

648649
## CLI Options
649650

650651
```
651652
Options:
652-
-s, --specs <dir> Directory containing OpenAPI specifications (default: "./examples/specs")
653-
-c, --config <file> Configuration file path (default: "./examples/mcp-config.json")
654-
-p, --prompts <dir> Directory containing prompt specifications (default: "./examples/prompts")
655-
--port <number> Port for HTTP server mode (default: "4000")
656-
--base-url <url> Base URL for backend APIs (overrides config file)
657-
--http Run in HTTP server mode instead of stdio (default: false)
658-
-v, --verbose Enable verbose logging (default: true)
659-
-h, --help Display help for command
653+
-s, --specs <dir> Directory containing OpenAPI specifications (default: "./examples/specs")
654+
-c, --config <file> Configuration file path (default: "./examples/mcp-config.json")
655+
-p, --prompts <dir> Directory containing prompt specifications (default: "./examples/prompts")
656+
--port <number> Port for HTTP server mode (default: "4000")
657+
--base-url <url> Base URL for backend APIs (overrides config file)
658+
--max-tool-name-length <number> Maximum length for generated tool names (default: "48")
659+
--http Run in HTTP server mode instead of stdio (default: false)
660+
-v, --verbose Enable verbose logging (default: true)
661+
-h, --help Display help for command
660662
```
661663

662664
**Mode Selection:**

mcp-openapi/src/cli.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
import { Command } from 'commander';
44
import { MCPOpenAPIServer } from './server.js';
55
import { ServerOptions } from './types.js';
6+
import { PACKAGE_NAME, PACKAGE_VERSION } from './package-info.js';
67

78
const program = new Command();
89

910
program
10-
.name('mcp-openapi-server')
11+
.name(PACKAGE_NAME)
1112
.description('A generic, configurable MCP server that generates tools, resources, and prompts from OpenAPI specifications')
12-
.version('1.0.0');
13+
.version(PACKAGE_VERSION);
1314

1415
program
1516
.option('-s, --specs <dir>', 'Directory containing OpenAPI specifications', './examples/specs')
1617
.option('-c, --config <file>', 'Configuration file path', './examples/mcp-config.json')
1718
.option('-p, --prompts <dir>', 'Directory containing prompt specifications', './examples/prompts')
1819
.option('--port <number>', 'Port for HTTP server mode', '4000')
1920
.option('--base-url <url>', 'Base URL for backend APIs (overrides config file)')
21+
.option('--max-tool-name-length <number>', 'Maximum length for generated tool names', '48')
2022
.option('--http', 'Run in HTTP server mode instead of stdio', false)
2123
.option('-v, --verbose', 'Enable verbose logging', true)
2224
.action(async (options) => {
@@ -26,7 +28,8 @@ program
2628
promptsDir: options.prompts,
2729
port: parseInt(options.port),
2830
verbose: options.verbose,
29-
...(options.baseUrl && { baseUrl: options.baseUrl })
31+
...(options.baseUrl && { baseUrl: options.baseUrl }),
32+
...(options.maxToolNameLength && { maxToolNameLength: parseInt(options.maxToolNameLength) })
3033
};
3134

3235
const server = new MCPOpenAPIServer(serverOptions);

mcp-openapi/src/package-info.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
// Interface for package.json structure
5+
interface PackageInfo {
6+
name: string;
7+
version: string;
8+
description?: string;
9+
}
10+
11+
let packageInfo: PackageInfo | null = null;
12+
13+
/**
14+
* Reads and caches package.json information
15+
*/
16+
function loadPackageInfo(): PackageInfo {
17+
if (packageInfo) {
18+
return packageInfo;
19+
}
20+
21+
try {
22+
// Look for package.json in the project root
23+
// Try multiple possible locations to be robust
24+
const possiblePaths = [
25+
path.resolve(process.cwd(), 'package.json'),
26+
path.resolve(__dirname, '..', 'package.json'),
27+
path.resolve(__dirname, '..', '..', 'package.json')
28+
];
29+
30+
let packageJsonPath: string | null = null;
31+
for (const possiblePath of possiblePaths) {
32+
if (fs.existsSync(possiblePath)) {
33+
packageJsonPath = possiblePath;
34+
break;
35+
}
36+
}
37+
38+
if (!packageJsonPath) {
39+
throw new Error('package.json not found');
40+
}
41+
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
42+
const packageJson = JSON.parse(packageJsonContent);
43+
44+
packageInfo = {
45+
name: packageJson.name,
46+
version: packageJson.version,
47+
description: packageJson.description
48+
};
49+
50+
return packageInfo;
51+
} catch (error) {
52+
// Fallback to hardcoded values if package.json can't be read
53+
console.warn('Warning: Could not read package.json, using fallback values');
54+
packageInfo = {
55+
name: 'mcp-openapi-server',
56+
version: '1.0.0',
57+
description: 'MCP OpenAPI Server'
58+
};
59+
return packageInfo;
60+
}
61+
}
62+
63+
// Export the package information
64+
export const PACKAGE_NAME = loadPackageInfo().name;
65+
export const PACKAGE_VERSION = loadPackageInfo().version;
66+
export const PACKAGE_DESCRIPTION = loadPackageInfo().description;
67+
68+
// Export function for getting full package info
69+
export function getPackageInfo(): PackageInfo {
70+
return loadPackageInfo();
71+
}

mcp-openapi/src/server.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
ServerOptions
2424
} from './types.js';
2525
import { Telemetry, TelemetryContext } from './telemetry.js';
26+
import { PACKAGE_NAME, PACKAGE_VERSION } from './package-info.js';
2627

2728
export class MCPOpenAPIServer {
2829
private server: Server;
@@ -61,6 +62,7 @@ export class MCPOpenAPIServer {
6162
promptsDir: './examples/prompts',
6263
port: 4000,
6364
verbose: true,
65+
maxToolNameLength: 48,
6466
...options
6567
};
6668

@@ -70,7 +72,7 @@ export class MCPOpenAPIServer {
7072
this.options.promptsDir = path.resolve(this.options.promptsDir!);
7173

7274
this.server = new Server(
73-
{ name: "mcp-openapi-server", version: "1.0.0" },
75+
{ name: PACKAGE_NAME, version: PACKAGE_VERSION },
7476
{
7577
capabilities: {
7678
tools: {},
@@ -329,8 +331,8 @@ export class MCPOpenAPIServer {
329331
private generateShortToolName(specId: string, pathPattern: string, method: string): string {
330332
// Server name is "mcp-openapi" (11 chars), leaving ~49 chars for tool name to stay under 60
331333
// Reason for short tool name is because some MCP clients like Cursor IDE, as of this writing,
332-
// has a limit of 60 chars for tool name
333-
const maxToolNameLength = 48;
334+
// has a limit of 60 chars for tool name + mcp server name
335+
const maxToolNameLength = this.options.maxToolNameLength!;
334336

335337
// Method abbreviations
336338
const methodAbbrev: Record<string, string> = {
@@ -1129,7 +1131,7 @@ export class MCPOpenAPIServer {
11291131

11301132
// Step 2: Create new MCP server with LOADED capabilities
11311133
this.server = new Server(
1132-
{ name: "mcp-openapi-server", version: "1.0.0" },
1134+
{ name: PACKAGE_NAME, version: PACKAGE_VERSION },
11331135
{
11341136
capabilities: {
11351137
tools: this.tools.reduce((acc, tool) => ({ ...acc, [tool.name]: {} }), {}),
@@ -1275,7 +1277,7 @@ export class MCPOpenAPIServer {
12751277
tools: this.tools.length,
12761278
resources: this.resources.length,
12771279
prompts: this.prompts.size,
1278-
version: '1.0.0'
1280+
version: PACKAGE_VERSION
12791281
});
12801282
});
12811283

mcp-openapi/src/telemetry.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
MCPResource,
88
ServerOptions
99
} from './types.js';
10+
import { PACKAGE_NAME } from './package-info.js';
1011

1112
export interface TelemetryContext {
1213
options: ServerOptions;
@@ -31,7 +32,7 @@ export class Telemetry {
3132
method: 'notifications/message',
3233
params: {
3334
level: 'debug',
34-
logger: 'mcp-openapi-server',
35+
logger: PACKAGE_NAME,
3536
data: message
3637
}
3738
}).catch(() => {
@@ -53,7 +54,7 @@ export class Telemetry {
5354
method: 'notifications/message',
5455
params: {
5556
level: 'info',
56-
logger: 'mcp-openapi-server',
57+
logger: PACKAGE_NAME,
5758
data: message
5859
}
5960
}).catch(() => {
@@ -74,7 +75,7 @@ export class Telemetry {
7475
method: 'notifications/message',
7576
params: {
7677
level: 'warning',
77-
logger: 'mcp-openapi-server',
78+
logger: PACKAGE_NAME,
7879
data: message
7980
}
8081
}).catch(() => {
@@ -94,7 +95,7 @@ export class Telemetry {
9495
method: 'notifications/message',
9596
params: {
9697
level: 'error',
97-
logger: 'mcp-openapi-server',
98+
logger: PACKAGE_NAME,
9899
data: message
99100
}
100101
}).catch(() => {

mcp-openapi/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@ export interface ServerOptions {
7878
port?: number;
7979
verbose?: boolean;
8080
baseUrl?: string;
81+
maxToolNameLength?: number;
8182
}

0 commit comments

Comments
 (0)