Skip to content

Commit b6f9cd7

Browse files
committed
Fix the executeTool function to use tool meta data instead of trying to reverse engineer from toolName
1 parent 62be365 commit b6f9cd7

File tree

4 files changed

+72
-97
lines changed

4 files changed

+72
-97
lines changed

mcp-openapi/src/package-info.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import { fileURLToPath } from 'url';
34

45
// Interface for package.json structure
56
interface PackageInfo {
@@ -18,15 +19,19 @@ function loadPackageInfo(): PackageInfo {
1819
return packageInfo;
1920
}
2021

22+
// Get ES module equivalent of __dirname
23+
const __filename = fileURLToPath(import.meta.url);
24+
const __dirname = path.dirname(__filename);
25+
26+
// Look for package.json in the project root
27+
// Try multiple possible locations to be robust
28+
const possiblePaths = [
29+
path.resolve(process.cwd(), 'package.json'),
30+
path.resolve(__dirname, '..', 'package.json'),
31+
path.resolve(__dirname, '..', '..', 'package.json')
32+
];
33+
2134
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-
3035
let packageJsonPath: string | null = null;
3136
for (const possiblePath of possiblePaths) {
3237
if (fs.existsSync(possiblePath)) {

mcp-openapi/src/server.ts

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,13 @@ export class MCPOpenAPIServer {
215215
// Add server info resource for querying capabilities
216216
this.addServerInfoResource();
217217

218+
// Update telemetry context with the newly generated tools and resources
219+
this.updateTelemetryContext();
220+
218221
// DEBUG: Show detailed MCP capabilities generated from OpenAPI specs
219222
this.telemetry.printMCPCapabilitiesDebug(
220223
this.getSpecFileName.bind(this),
221-
this.determineMCPType.bind(this),
222-
this.getToolName.bind(this),
223-
this.hasOverride.bind(this),
224-
this.isHttpMethod.bind(this)
224+
this.hasOverride.bind(this)
225225
);
226226
}
227227

@@ -390,17 +390,7 @@ export class MCPOpenAPIServer {
390390
return toolName;
391391
}
392392

393-
private getActualHttpMethod(abbreviatedMethod: string): string {
394-
// Convert abbreviated method names (from tool names) back to HTTP methods
395-
const methodMap: Record<string, string> = {
396-
'get': 'get',
397-
'create': 'post',
398-
'update': 'put',
399-
'patch': 'patch',
400-
'delete': 'delete'
401-
};
402-
return methodMap[abbreviatedMethod] || abbreviatedMethod;
403-
}
393+
404394

405395
private hasOverride(specId: string, path: string, method: string): boolean {
406396
return this.config.overrides.some(o =>
@@ -478,7 +468,13 @@ export class MCPOpenAPIServer {
478468
return {
479469
name: toolName,
480470
description: description,
481-
inputSchema: this.buildInputSchema(operation, pathPattern)
471+
inputSchema: this.buildInputSchema(operation, pathPattern),
472+
_metadata: {
473+
specId,
474+
pathPattern,
475+
method,
476+
operation
477+
}
482478
};
483479
}
484480

@@ -667,26 +663,10 @@ export class MCPOpenAPIServer {
667663
method = toolOverride.method;
668664
specId = toolOverride.specId;
669665
} else {
670-
// Parse tool name to get spec, method, and path (fallback for non-override tools)
671-
const toolParts = toolName.split('_');
672-
specId = toolParts[0];
673-
const abbreviatedMethod = toolParts[1];
674-
675-
// Convert abbreviated method back to actual HTTP method
676-
method = this.getActualHttpMethod(abbreviatedMethod);
677-
const pathParts = toolParts.slice(2);
678-
679-
// Find the original path pattern
680-
const spec = this.specs.get(specId);
681-
if (!spec) {
682-
throw new Error(`Spec ${specId} not found`);
683-
}
684-
685-
const foundPattern = this.findPathPattern(spec, pathParts);
686-
if (!foundPattern) {
687-
throw new Error(`Could not determine path pattern for tool ${toolName}`);
688-
}
689-
pathPattern = foundPattern;
666+
// Use stored metadata instead of parsing tool name
667+
specId = tool._metadata.specId;
668+
pathPattern = tool._metadata.pathPattern;
669+
method = tool._metadata.method;
690670
}
691671

692672
// Verify spec exists
@@ -838,17 +818,7 @@ export class MCPOpenAPIServer {
838818
}
839819
}
840820

841-
private findPathPattern(spec: OpenAPISpec, pathParts: string[]): string | null {
842-
// This is a simplified approach - in practice, you might want more sophisticated matching
843-
for (const pathPattern of Object.keys(spec.paths)) {
844-
const sanitized = this.sanitizePath(pathPattern);
845-
const reconstructed = pathParts.join('_');
846-
if (sanitized.includes(reconstructed) || reconstructed.includes(sanitized.replace(/^_+|_+$/g, ''))) {
847-
return pathPattern;
848-
}
849-
}
850-
return null;
851-
}
821+
852822

853823
private async readResource(uri: string, userContext?: { token?: string }, resourceParams?: Record<string, any>) {
854824
// Handle special server info resource

mcp-openapi/src/telemetry.ts

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ export class Telemetry {
110110

111111
printMCPCapabilitiesDebug(
112112
getSpecFileName: (specId: string) => string,
113-
determineMCPType: (specId: string, path: string, method: string, operation: any) => 'tool' | 'resource',
114-
getToolName: (specId: string, pathPattern: string, method: string, operation: any) => string,
115-
hasOverride: (specId: string, path: string, method: string) => boolean,
116-
isHttpMethod: (method: string) => boolean
113+
hasOverride: (specId: string, path: string, method: string) => boolean
117114
): void {
118115
if (!this.context.options.verbose) {
119116
return;
@@ -135,46 +132,42 @@ export class Telemetry {
135132
isOverridden: boolean;
136133
}> = [];
137134

138-
// Collect tool details
139-
for (const [specId, spec] of this.context.specs) {
135+
// Collect tool details - use actual created tools with their metadata
136+
for (const tool of this.context.tools) {
137+
const { specId, pathPattern, method } = tool._metadata;
140138
const specFile = getSpecFileName(specId);
141139

142-
for (const [pathPattern, pathItem] of Object.entries(spec.paths)) {
143-
for (const [method, operation] of Object.entries(pathItem)) {
144-
if (isHttpMethod(method)) {
145-
const mcpType = determineMCPType(specId, pathPattern, method, operation);
146-
147-
if (mcpType === 'tool') {
148-
const tool = this.context.tools.find(t => t.name === getToolName(specId, pathPattern, method, operation));
149-
if (tool) {
150-
generationDetails.push({
151-
specId,
152-
specFile,
153-
path: pathPattern,
154-
method: method.toUpperCase(),
155-
mcpType: 'tool',
156-
mcpName: tool.name,
157-
description: tool.description,
158-
isOverridden: hasOverride(specId, pathPattern, method)
159-
});
160-
}
161-
} else if (mcpType === 'resource') {
162-
const resource = this.context.resources.find(r => r.uri === `${specId}://${pathPattern.startsWith('/') ? pathPattern.substring(1) : pathPattern}`);
163-
if (resource) {
164-
generationDetails.push({
165-
specId,
166-
specFile,
167-
path: pathPattern,
168-
method: method.toUpperCase(),
169-
mcpType: 'resource',
170-
mcpName: resource.name,
171-
description: resource.description,
172-
isOverridden: hasOverride(specId, pathPattern, method)
173-
});
174-
}
175-
}
176-
}
177-
}
140+
generationDetails.push({
141+
specId,
142+
specFile,
143+
path: pathPattern,
144+
method: method.toUpperCase(),
145+
mcpType: 'tool',
146+
mcpName: tool.name,
147+
description: tool.description,
148+
isOverridden: hasOverride(specId, pathPattern, method)
149+
});
150+
}
151+
152+
// Collect resource details - use actual created resources
153+
for (const resource of this.context.resources) {
154+
// Extract specId and path from resource URI (format: specId://path)
155+
const uriParts = resource.uri.split('://');
156+
if (uriParts.length === 2) {
157+
const specId = uriParts[0];
158+
const pathPattern = '/' + uriParts[1];
159+
const specFile = getSpecFileName(specId);
160+
161+
generationDetails.push({
162+
specId,
163+
specFile,
164+
path: pathPattern,
165+
method: 'GET', // Resources are typically GET operations
166+
mcpType: 'resource',
167+
mcpName: resource.name,
168+
description: resource.description,
169+
isOverridden: false // For now, assume resources don't have overrides in this context
170+
});
178171
}
179172
}
180173

mcp-openapi/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export interface MCPTool {
5656
properties: Record<string, any>;
5757
required: string[];
5858
};
59+
// Metadata for tool execution (not exposed in MCP protocol)
60+
_metadata: {
61+
specId: string;
62+
pathPattern: string;
63+
method: string;
64+
operation: any;
65+
};
5966
}
6067

6168
export interface MCPResource {

0 commit comments

Comments
 (0)