@@ -33,6 +33,7 @@ export interface OpenApiConverterOptions {
3333 specUrl ?: string ;
3434 callTemplateName ?: string ;
3535 authTools ?: Auth ;
36+ baseUrl ?: string ;
3637}
3738
3839/**
@@ -47,6 +48,7 @@ export interface OpenApiConverterOptions {
4748export class OpenApiConverter {
4849 private spec : Record < string , any > ;
4950 private spec_url : string | undefined ;
51+ private base_url : string | undefined ;
5052 private auth_tools : Auth | undefined ;
5153 private placeholder_counter : number = 0 ;
5254 private call_template_name : string ;
@@ -55,14 +57,16 @@ export class OpenApiConverter {
5557 * Initializes the OpenAPI converter.
5658 *
5759 * @param openapi_spec Parsed OpenAPI specification as a dictionary.
58- * @param options Optional settings including spec_url, call_template_name, and auth_tools .
60+ * @param options Optional settings including spec_url, call_template_name, auth_tools, and baseUrl .
5961 * - specUrl: URL where the specification was retrieved from.
6062 * - callTemplateName: Custom name for the call_template if spec title not provided.
6163 * - authTools: Optional auth configuration for generated tools.
64+ * - baseUrl: Optional base URL override for all API endpoints.
6265 */
6366 constructor ( openapi_spec : Record < string , any > , options ?: OpenApiConverterOptions ) {
6467 this . spec = openapi_spec ;
6568 this . spec_url = options ?. specUrl ;
69+ this . base_url = options ?. baseUrl ;
6670 this . auth_tools = options ?. authTools ;
6771 this . placeholder_counter = 0 ;
6872
@@ -105,18 +109,30 @@ export class OpenApiConverter {
105109 const tools : Tool [ ] = [ ] ;
106110 let baseUrl = '/' ;
107111
108- const servers = this . spec . servers ;
109- if ( servers && Array . isArray ( servers ) && servers . length > 0 && servers [ 0 ] . url ) {
110- baseUrl = servers [ 0 ] . url ;
111- } else if ( this . spec_url ) {
112+ // 1. Explicit baseUrl option (highest priority)
113+ if ( this . base_url ) {
114+ baseUrl = this . base_url ;
115+ }
116+ // 2. OpenAPI 3.0 servers field
117+ else if ( this . spec . servers && Array . isArray ( this . spec . servers ) && this . spec . servers . length > 0 && this . spec . servers [ 0 ] . url ) {
118+ baseUrl = this . spec . servers [ 0 ] . url ;
119+ }
120+ // 3. OpenAPI 2.0 host and basePath fields
121+ else if ( this . spec . host ) {
122+ const scheme = this . spec . schemes ?. [ 0 ] || 'https' ;
123+ const basePath = this . spec . basePath || '' ;
124+ baseUrl = `${ scheme } ://${ this . spec . host } ${ basePath } ` ;
125+ }
126+ // 4. Fallback to spec_url ONLY if it's an HTTP/HTTPS URL
127+ else if ( this . spec_url && ( this . spec_url . startsWith ( 'http://' ) || this . spec_url . startsWith ( 'https://' ) ) ) {
112128 try {
113129 const parsedUrl = new URL ( this . spec_url ) ;
114130 baseUrl = `${ parsedUrl . protocol } //${ parsedUrl . host } ` ;
115131 } catch ( e ) {
116132 console . error ( `[OpenApiConverter] Invalid spec_url provided: ${ this . spec_url } ` ) ;
117133 }
118134 } else {
119- console . warn ( "[OpenApiConverter] No server info or spec URL provided . Using fallback base URL: '/'" ) ;
135+ console . warn ( "[OpenApiConverter] No server info found in OpenAPI spec . Using fallback base URL: '/'. Tools may not work correctly without a valid base URL. " ) ;
120136 }
121137
122138 const paths = this . spec . paths || { } ;
0 commit comments