openapi.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package aiproxyopenapi
  2. import (
  3. "fmt"
  4. "net/url"
  5. "sync"
  6. "github.com/labring/aiproxy/core/docs"
  7. mcpservers "github.com/labring/aiproxy/mcp-servers"
  8. "github.com/labring/aiproxy/openapi-mcp/convert"
  9. )
  10. var configTemplates = map[string]mcpservers.ConfigTemplate{
  11. "host": {
  12. Name: "Host",
  13. Required: mcpservers.ConfigRequiredTypeInitOnly,
  14. Example: "http://localhost:3000",
  15. Description: "The host of the OpenAPI server",
  16. Validator: func(value string) error {
  17. u, err := url.Parse(value)
  18. if err != nil {
  19. return err
  20. }
  21. if u.Scheme != "http" && u.Scheme != "https" {
  22. return fmt.Errorf("invalid scheme: %s", u.Scheme)
  23. }
  24. return nil
  25. },
  26. },
  27. "authorization": {
  28. Name: "Authorization",
  29. Required: mcpservers.ConfigRequiredTypeReusingOptional,
  30. Example: "aiproxy-admin-key",
  31. Description: "The admin key of the OpenAPI server",
  32. },
  33. }
  34. var (
  35. parser *convert.Parser
  36. parseOnce sync.Once
  37. )
  38. func getParser() *convert.Parser {
  39. parseOnce.Do(func() {
  40. parser = convert.NewParser()
  41. err := parser.Parse([]byte(docs.SwaggerInfo.ReadDoc()))
  42. if err != nil {
  43. panic(err)
  44. }
  45. })
  46. return parser
  47. }
  48. func NewServer(config, reusingConfig map[string]string) (mcpservers.Server, error) {
  49. converter := convert.NewConverter(getParser(), convert.Options{
  50. OpenAPIFrom: config["host"],
  51. Authorization: reusingConfig["authorization"],
  52. })
  53. return converter.Convert()
  54. }