Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import SwaggerParser from '@apidevtools/swagger-parser';
import { OpenAPIV3 } from 'openapi-types';
import { extractToolsFromApi } from './parser/extract-tools.js';
import { McpToolDefinition } from './types/index.js';
import type { McpToolDefinition } from './types/index.js';
import { determineBaseUrl } from './utils/url.js';

/**
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
import { CliOptions, TransportType } from './types/index.js';

// Export programmatic API
export { getToolsFromOpenApi, McpToolDefinition, GetToolsOptions } from './api.js';
export { getToolsFromOpenApi } from './api.js';
export type { McpToolDefinition, GetToolsOptions } from './api.js';

// Configure CLI
const program = new Command();
Expand Down Expand Up @@ -84,6 +85,11 @@ program
// Export the program object for use in bin stub
export { program };

// Run the program if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
program.parse();
}

/**
* Main function to run the generator
*/
Expand Down
28 changes: 21 additions & 7 deletions src/parser/extract-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export function extractToolsFromApi(api: OpenAPIV3.Document): McpToolDefinition[
for (const [path, pathItem] of Object.entries(api.paths)) {
if (!pathItem) continue;

const pathParameters = pathItem.parameters;

for (const method of Object.values(OpenAPIV3.HttpMethods)) {
const operation = pathItem[method];
if (!operation) continue;
Expand All @@ -48,8 +50,10 @@ export function extractToolsFromApi(api: OpenAPIV3.Document): McpToolDefinition[
operation.description || operation.summary || `Executes ${method.toUpperCase()} ${path}`;

// Generate input schema and extract parameters
const { inputSchema, parameters, requestBodyContentType } =
generateInputSchemaAndDetails(operation);
const { inputSchema, parameters, requestBodyContentType } = generateInputSchemaAndDetails(
operation,
pathParameters
);

// Extract parameter details for execution
const executionParameters = parameters.map((p) => ({ name: p.name, in: p.in }));
Expand Down Expand Up @@ -83,18 +87,28 @@ export function extractToolsFromApi(api: OpenAPIV3.Document): McpToolDefinition[
* @param operation OpenAPI operation object
* @returns Input schema, parameters, and request body content type
*/
export function generateInputSchemaAndDetails(operation: OpenAPIV3.OperationObject): {
export function generateInputSchemaAndDetails(
operation: OpenAPIV3.OperationObject,
pathParameters?: (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[]
): {
inputSchema: JSONSchema7 | boolean;
parameters: OpenAPIV3.ParameterObject[];
requestBodyContentType?: string;
} {
const properties: { [key: string]: JSONSchema7 | boolean } = {};
const required: string[] = [];

// Process parameters
const allParameters: OpenAPIV3.ParameterObject[] = Array.isArray(operation.parameters)
? operation.parameters.map((p) => p as OpenAPIV3.ParameterObject)
: [];
const allParameters: OpenAPIV3.ParameterObject[] = [];

// Add Path Item-level params first so that if an Operation-level param with the same name
// exists, the Operation-level param overrides the Path Item-level param.
if (Array.isArray(pathParameters)) {
allParameters.push(...pathParameters.map((p) => p as OpenAPIV3.ParameterObject));
}

if (Array.isArray(operation.parameters)) {
allParameters.push(...operation.parameters.map((p) => p as OpenAPIV3.ParameterObject));
}

allParameters.forEach((param) => {
if (!param.name || !param.schema) return;
Expand Down