MCPcopy Index your code
hub / github.com/APIParkLab/APIPark / SetServerByOpenapi

Function SetServerByOpenapi

mcp-server/server.go:163–276  ·  view source on GitHub ↗
(sid, name, version, content string)

Source from the content-addressed store, hash-verified

161}
162
163func SetServerByOpenapi(sid, name, version, content string) error {
164 mcpInfo, err := ConvertMCPFromOpenAPI3Data([]byte(content))
165 if err != nil {
166 return fmt.Errorf("convert mcp from openapi3 data error: %w", err)
167 }
168 tools := make([]ITool, 0, len(mcpInfo.Apis))
169 for _, a := range mcpInfo.Apis {
170 toolOptions := make([]mcp.ToolOption, 0, len(a.Params)+2)
171 toolOptions = append(toolOptions, mcp.WithDescription(a.Description))
172 params := make(map[string]*Param)
173 for _, v := range a.Params {
174 if v.In == "header" && v.Name == "Authorization" {
175 continue
176 }
177 params[v.Name] = NewParam(Position(v.In), v.Required, v.Description)
178 options := make([]mcp.PropertyOption, 0, 2)
179 if v.Required {
180 options = append(options, mcp.Required())
181 }
182 options = append(options, mcp.Description(v.Description))
183 toolOptions = append(toolOptions, mcp.WithString(v.Name, options...))
184 }
185 if a.Body != nil {
186 type Schema struct {
187 Type string `mapstructure:"type"`
188 Properties map[string]interface{} `mapstructure:"properties"`
189 Items interface{} `mapstructure:"items"`
190 Required interface{} `mapstructure:"required"`
191 }
192 var tmp Schema
193 err = mapstructure.Decode(a.Body, &tmp)
194 if err != nil {
195 return err
196 }
197 required := map[string]struct{}{}
198 switch t := tmp.Required.(type) {
199 case []interface{}:
200 for _, v := range t {
201 i, ok := v.(string)
202 if !ok {
203 continue
204 }
205 required[i] = struct{}{}
206 }
207 }
208 for k, v := range tmp.Properties {
209 description := ""
210 typ := "string"
211 isRequired := false
212 if _, ok := required[k]; ok {
213 isRequired = true
214 }
215 var props map[string]interface{}
216 var items interface{}
217 switch t := v.(type) {
218 case map[string]interface{}:
219 if m, ok := t["type"]; ok {
220 n, ok := m.(string)

Callers

nothing calls this directly

Calls 7

NewParamFunction · 0.85
PositionTypeAlias · 0.85
NewToolFunction · 0.85
SetServerFunction · 0.85
RequiredMethod · 0.80
DescriptionMethod · 0.80

Tested by

no test coverage detected