@@ -3,6 +3,7 @@ package loader
33import (
44 "encoding/json"
55 "fmt"
6+ "net/url"
67 "slices"
78 "strings"
89
@@ -15,9 +16,19 @@ import (
1516// Each operation will become a tool definition.
1617// The tool's Instructions will be in the format "#!sys.openapi '{JSON Instructions}'",
1718// where the JSON Instructions are a JSON-serialized engine.OpenAPIInstructions struct.
18- func getOpenAPITools (t * openapi3.T ) ([]types.Tool , error ) {
19+ func getOpenAPITools (t * openapi3.T , defaultHost string ) ([]types.Tool , error ) {
20+ // Determine the default server.
1921 if len (t .Servers ) == 0 {
20- return nil , fmt .Errorf ("no servers found in OpenAPI spec" )
22+ if defaultHost != "" {
23+ u , err := url .Parse (defaultHost )
24+ if err != nil {
25+ return nil , fmt .Errorf ("invalid default host URL: %w" , err )
26+ }
27+ u .Path = "/"
28+ t .Servers = []* openapi3.Server {{URL : u .String ()}}
29+ } else {
30+ return nil , fmt .Errorf ("no servers found in OpenAPI spec" )
31+ }
2132 }
2233 defaultServer , err := parseServer (t .Servers [0 ])
2334 if err != nil {
@@ -39,6 +50,7 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
3950 }
4051 }
4152
53+ // Generate a tool for each operation.
4254 var (
4355 toolNames []string
4456 tools []types.Tool
@@ -54,6 +66,7 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
5466 }
5567 }
5668
69+ operations:
5770 for method , operation := range pathObj .Operations () {
5871 // Handle operation-level server override, if one exists
5972 operationServer := pathServer
@@ -103,10 +116,9 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
103116 },
104117 }
105118
106- toolNames = append (toolNames , tool .Parameters .Name )
107-
108- // Handle query, path, and header parameters
109- for _ , param := range operation .Parameters {
119+ // Handle query, path, and header parameters, based on the parameters for this operation
120+ // and the parameters for this path.
121+ for _ , param := range append (operation .Parameters , pathObj .Parameters ... ) {
110122 arg := param .Value .Schema .Value
111123
112124 if arg .Description == "" {
@@ -161,7 +173,8 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
161173 }
162174
163175 if bodyMIME == "" {
164- return nil , fmt .Errorf ("no supported MIME types found for request body in operation %s" , operation .OperationID )
176+ // No supported MIME types found, so just skip this operation and move on.
177+ continue operations
165178 }
166179 }
167180
@@ -226,6 +239,8 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
226239 return nil , err
227240 }
228241
242+ // Register
243+ toolNames = append (toolNames , tool .Parameters .Name )
229244 tools = append (tools , tool )
230245 operationNum ++
231246 }
0 commit comments