55 "encoding/json"
66 "fmt"
77 "io"
8- "log"
98 "net/http"
109 "net/url"
1110 "os"
@@ -15,6 +14,7 @@ import (
1514
1615 "github.com/ckanthony/openapi-mcp/pkg/config"
1716 "github.com/ckanthony/openapi-mcp/pkg/mcp"
17+ "github.com/ckanthony/openapi-mcp/pkg/utils"
1818 "github.com/getkin/kin-openapi/openapi3"
1919 "github.com/go-openapi/loads"
2020 "github.com/go-openapi/spec"
@@ -38,7 +38,7 @@ func LoadSwagger(location string) (interface{}, string, error) {
3838 var absPath string // Store absolute path if it's a file
3939
4040 if ! isURL {
41- log . Printf ("Detected file path location: %s" , location )
41+ utils . SafeLogPrintf ("Detected file path location: %s" , location )
4242 absPath , err = filepath .Abs (location )
4343 if err != nil {
4444 return nil , "" , fmt .Errorf ("failed to get absolute path for '%s': %w" , location , err )
@@ -49,7 +49,7 @@ func LoadSwagger(location string) (interface{}, string, error) {
4949 return nil , "" , fmt .Errorf ("failed reading file path '%s': %w" , absPath , err )
5050 }
5151 } else {
52- log . Printf ("Detected URL location: %s" , location )
52+ utils . SafeLogPrintf ("Detected URL location: %s" , location )
5353 // Read data first for version detection
5454 resp , err := http .Get (location )
5555 if err != nil {
@@ -81,11 +81,11 @@ func LoadSwagger(location string) (interface{}, string, error) {
8181
8282 if ! isURL {
8383 // Use LoadFromFile for local files
84- log . Printf ("Loading V3 spec using LoadFromFile: %s" , absPath )
84+ utils . SafeLogPrintf ("Loading V3 spec using LoadFromFile: %s" , absPath )
8585 doc , loadErr = loader .LoadFromFile (absPath )
8686 } else {
8787 // Use LoadFromURI for URLs
88- log . Printf ("Loading V3 spec using LoadFromURI: %s" , location )
88+ utils . SafeLogPrintf ("Loading V3 spec using LoadFromURI: %s" , location )
8989 doc , loadErr = loader .LoadFromURI (locationURL )
9090 }
9191
@@ -99,7 +99,7 @@ func LoadSwagger(location string) (interface{}, string, error) {
9999 return doc , VersionV3 , nil
100100 } else if _ , ok := detector ["swagger" ]; ok {
101101 // Swagger 2.0 - Still load from data as loads.Analyzed expects bytes
102- log . Printf ("Loading V2 spec using loads.Analyzed from data (source: %s)" , location )
102+ utils . SafeLogPrintf ("Loading V2 spec using loads.Analyzed from data (source: %s)" , location )
103103 doc , err := loads .Analyzed (data , "2.0" )
104104 if err != nil {
105105 return nil , "" , fmt .Errorf ("failed to load or validate Swagger v2 spec from '%s': %w" , location , err )
@@ -139,7 +139,7 @@ func generateToolSetV3(doc *openapi3.T, cfg *config.Config) (*mcp.ToolSet, error
139139 // Determine Base URL once
140140 baseURL , err := determineBaseURLV3 (doc , cfg )
141141 if err != nil {
142- log . Printf ("Warning: Could not determine base URL for V3 spec: %v. Operations might fail if base URL override is not set." , err )
142+ utils . SafeLogPrintf ("Warning: Could not determine base URL for V3 spec: %v. Operations might fail if base URL override is not set." , err )
143143 baseURL = "" // Allow proceeding if override is set
144144 }
145145
@@ -175,7 +175,7 @@ func generateToolSetV3(doc *openapi3.T, cfg *config.Config) (*mcp.ToolSet, error
175175 // Handle request body
176176 requestBody , err := requestBodyToMCPV3 (op .RequestBody )
177177 if err != nil {
178- log . Printf ("Warning: skipping request body for %s %s due to error: %v" , method , rawPath , err )
178+ utils . SafeLogPrintf ("Warning: skipping request body for %s %s due to error: %v" , method , rawPath , err )
179179 } else {
180180 // Merge request body schema into the main parameter schema
181181 if requestBody .Content != nil {
@@ -189,7 +189,7 @@ func generateToolSetV3(doc *openapi3.T, cfg *config.Config) (*mcp.ToolSet, error
189189 }
190190 } else {
191191 // If body is not an object, represent as 'requestBody'
192- log . Printf ("Warning: V3 request body for %s %s is not an object schema. Representing as 'requestBody' field." , method , rawPath )
192+ utils . SafeLogPrintf ("Warning: V3 request body for %s %s is not an object schema. Representing as 'requestBody' field." , method , rawPath )
193193 parametersSchema .Properties ["requestBody" ] = mediaTypeSchema
194194 }
195195 break // Only process the first content type
@@ -219,7 +219,7 @@ func generateToolSetV3(doc *openapi3.T, cfg *config.Config) (*mcp.ToolSet, error
219219 // Optionally, add a note if the requestBody itself was marked as required
220220 if requestBody .Required { // Check the boolean field
221221 // How to indicate this? Maybe add to description?
222- log . Printf ("Note: Request body for %s %s is marked as required." , method , rawPath )
222+ utils . SafeLogPrintf ("Note: Request body for %s %s is marked as required." , method , rawPath )
223223 // Or add all top-level body props to required? Needs decision.
224224 }
225225 }
@@ -309,18 +309,18 @@ func parametersToMCPSchemaAndDetailsV3(params openapi3.Parameters, cfg *config.C
309309 opParams := []mcp.ParameterDetail {}
310310 for _ , paramRef := range params {
311311 if paramRef .Value == nil {
312- log . Printf ("Warning: Skipping parameter with nil value." )
312+ utils . SafeLogPrintf ("Warning: Skipping parameter with nil value." )
313313 continue
314314 }
315315 param := paramRef .Value
316316 if param .Schema == nil {
317- log . Printf ("Warning: Skipping parameter '%s' with nil schema." , param .Name )
317+ utils . SafeLogPrintf ("Warning: Skipping parameter '%s' with nil schema." , param .Name )
318318 continue
319319 }
320320
321321 // Skip the API key parameter if configured
322322 if cfg .APIKeyName != "" && param .Name == cfg .APIKeyName && param .In == string (cfg .APIKeyLocation ) {
323- log . Printf ("Parser V3: Skipping API key parameter '%s' ('%s') from input schema generation." , param .Name , param .In )
323+ utils . SafeLogPrintf ("Parser V3: Skipping API key parameter '%s' ('%s') from input schema generation." , param .Name , param .In )
324324 continue
325325 }
326326
@@ -442,7 +442,7 @@ func generateToolSetV2(doc *spec.Swagger, cfg *config.Config) (*mcp.ToolSet, err
442442 // Determine Base URL once
443443 baseURL , err := determineBaseURLV2 (doc , cfg )
444444 if err != nil {
445- log . Printf ("Warning: Could not determine base URL for V2 spec: %v. Operations might fail if base URL override is not set." , err )
445+ utils . SafeLogPrintf ("Warning: Could not determine base URL for V2 spec: %v. Operations might fail if base URL override is not set." , err )
446446 baseURL = "" // Allow proceeding if override is set
447447 }
448448
@@ -455,7 +455,7 @@ func generateToolSetV2(doc *spec.Swagger, cfg *config.Config) (*mcp.ToolSet, err
455455 if secDef .Type == "apiKey" {
456456 apiKeyName = secDef .Name
457457 apiKeyIn = secDef .In // "query" or "header"
458- log . Printf ("Parser V2: Detected API key from security definition '%s': Name='%s', In='%s'" , name , apiKeyName , apiKeyIn )
458+ utils . SafeLogPrintf ("Parser V2: Detected API key from security definition '%s': Name='%s', In='%s'" , name , apiKeyName , apiKeyIn )
459459 break // Assume only one apiKey definition for simplicity
460460 }
461461 }
@@ -519,7 +519,7 @@ func generateToolSetV2(doc *spec.Swagger, cfg *config.Config) (*mcp.ToolSet, err
519519 }
520520 } else {
521521 // If body is not an object, represent as 'requestBody'
522- log . Printf ("Warning: V2 request body for %s %s is not an object schema. Representing as 'requestBody' field." , method , rawPath )
522+ utils . SafeLogPrintf ("Warning: V2 request body for %s %s is not an object schema. Representing as 'requestBody' field." , method , rawPath )
523523 if parametersSchema .Properties == nil {
524524 parametersSchema .Properties = make (map [string ]mcp.Schema )
525525 }
@@ -629,7 +629,7 @@ func parametersToMCPSchemaAndDetailsV2(params []spec.Parameter, definitions spec
629629 for _ , param := range params {
630630 // Skip the API key parameter if it's configured/detected
631631 if apiKeyName != "" && param .Name == apiKeyName && (param .In == "query" || param .In == "header" ) {
632- log . Printf ("Parser V2: Skipping API key parameter '%s' ('%s') from input schema generation." , param .Name , param .In )
632+ utils . SafeLogPrintf ("Parser V2: Skipping API key parameter '%s' ('%s') from input schema generation." , param .Name , param .In )
633633 continue
634634 }
635635
@@ -643,7 +643,7 @@ func parametersToMCPSchemaAndDetailsV2(params []spec.Parameter, definitions spec
643643 }
644644
645645 if param .In != "query" && param .In != "path" && param .In != "header" && param .In != "formData" {
646- log . Printf ("Parser V2: Skipping unsupported parameter type '%s' for parameter '%s'" , param .In , param .Name )
646+ utils . SafeLogPrintf ("Parser V2: Skipping unsupported parameter type '%s' for parameter '%s'" , param .In , param .Name )
647647 continue
648648 }
649649
@@ -702,7 +702,7 @@ func parametersToMCPSchemaAndDetailsV2(params []spec.Parameter, definitions spec
702702
703703 } else {
704704 // Body param defined without a schema? Treat as simple string.
705- log . Printf ("Warning: V2 body parameter '%s' defined without a schema. Treating as string." , bodyParam .Name )
705+ utils . SafeLogPrintf ("Warning: V2 body parameter '%s' defined without a schema. Treating as string." , bodyParam .Name )
706706 bodySchema .Type = "string"
707707 mcpSchema .Properties [bodyParam .Name ] = bodySchema
708708 if bodyParam .Required {
0 commit comments