@@ -3,6 +3,7 @@ package loader
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
+ "net/url"
6
7
"slices"
7
8
"strings"
8
9
@@ -15,9 +16,19 @@ import (
15
16
// Each operation will become a tool definition.
16
17
// The tool's Instructions will be in the format "#!sys.openapi '{JSON Instructions}'",
17
18
// 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.
19
21
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
+ }
21
32
}
22
33
defaultServer , err := parseServer (t .Servers [0 ])
23
34
if err != nil {
@@ -39,6 +50,7 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
39
50
}
40
51
}
41
52
53
+ // Generate a tool for each operation.
42
54
var (
43
55
toolNames []string
44
56
tools []types.Tool
@@ -54,6 +66,7 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
54
66
}
55
67
}
56
68
69
+ operations:
57
70
for method , operation := range pathObj .Operations () {
58
71
// Handle operation-level server override, if one exists
59
72
operationServer := pathServer
@@ -103,10 +116,9 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
103
116
},
104
117
}
105
118
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 ... ) {
110
122
arg := param .Value .Schema .Value
111
123
112
124
if arg .Description == "" {
@@ -161,7 +173,8 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
161
173
}
162
174
163
175
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
165
178
}
166
179
}
167
180
@@ -226,6 +239,8 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) {
226
239
return nil , err
227
240
}
228
241
242
+ // Register
243
+ toolNames = append (toolNames , tool .Parameters .Name )
229
244
tools = append (tools , tool )
230
245
operationNum ++
231
246
}
0 commit comments