mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-12 18:11:34 +08:00
Optimizing the parameter tiling of MCP Tool to facilitate AI understanding
This commit is contained in:
+24
-15
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
func genOpenAPI3Template(title string, description string) *openapi3.T {
|
||||
result := new(openapi3.T)
|
||||
result.OpenAPI = "3.1.0"
|
||||
result.OpenAPI = "3.0.1"
|
||||
result.Info = &openapi3.Info{
|
||||
Title: title,
|
||||
Description: description,
|
||||
@@ -37,6 +37,8 @@ func genOperation(summary string, description string, variables []*ai_api_dto.Ai
|
||||
|
||||
func genRequestBody(variables []*ai_api_dto.AiPromptVariable) *openapi3.RequestBodyRef {
|
||||
requestBody := openapi3.NewRequestBody()
|
||||
requestBody.Description = "Request body"
|
||||
requestBody.Required = true
|
||||
requestBody.Content = openapi3.NewContentWithSchema(genRequestBodySchema(variables), []string{"application/json"})
|
||||
return &openapi3.RequestBodyRef{
|
||||
Value: requestBody,
|
||||
@@ -55,10 +57,14 @@ func genResponse() *openapi3.ResponseRef {
|
||||
|
||||
func genRequestBodySchema(variables []*ai_api_dto.AiPromptVariable) *openapi3.Schema {
|
||||
result := openapi3.NewObjectSchema()
|
||||
required := make([]string, 0, 2)
|
||||
required = append(required, "messages")
|
||||
if len(variables) > 0 {
|
||||
result.WithProperty("variables", genVariableSchema(variables))
|
||||
result.WithRequired([]string{"variables", "messages"})
|
||||
required = append(required, "variables")
|
||||
}
|
||||
|
||||
result.WithRequired(required)
|
||||
streamSchema := openapi3.NewBoolSchema()
|
||||
streamSchema.Title = "stream"
|
||||
streamSchema.Description = "Whether to stream the response"
|
||||
@@ -129,6 +135,8 @@ func genMessageSchema() *openapi3.Schema {
|
||||
"role": roleSchema,
|
||||
"content": contentSchema,
|
||||
})
|
||||
|
||||
result.WithRequired([]string{"role", "content"})
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -137,20 +145,21 @@ func genMessagesSchema() *openapi3.Schema {
|
||||
result.Title = "Messages"
|
||||
result.Description = "Chat Messages"
|
||||
result.Items = openapi3.NewSchemaRef("#/components/schemas/Message", messageSchema)
|
||||
result.Required = []string{"content", "role"}
|
||||
return result
|
||||
}
|
||||
|
||||
func genResponseSchema() *openapi3.Schema {
|
||||
result := openapi3.NewObjectSchema()
|
||||
result.Description = "Response from the server"
|
||||
|
||||
|
||||
// 创建 choices 数组
|
||||
choicesSchema := openapi3.NewArraySchema()
|
||||
choiceItemSchema := openapi3.NewObjectSchema()
|
||||
|
||||
|
||||
// choice 中的 message 字段
|
||||
choiceItemSchema.WithPropertyRef("message", messageSchemaRef)
|
||||
|
||||
|
||||
// finish_reason 字段
|
||||
finishReasonSchema := openapi3.NewStringSchema().WithEnum(
|
||||
"stop",
|
||||
@@ -160,41 +169,41 @@ func genResponseSchema() *openapi3.Schema {
|
||||
"null",
|
||||
)
|
||||
choiceItemSchema.WithProperty("finish_reason", finishReasonSchema)
|
||||
|
||||
|
||||
// index 字段
|
||||
choiceItemSchema.WithProperty("index", openapi3.NewIntegerSchema())
|
||||
|
||||
|
||||
// logprobs 字段,可以为 null
|
||||
choiceItemSchema.WithProperty("logprobs", openapi3.NewSchema().WithNullable())
|
||||
|
||||
|
||||
choicesSchema.Items = &openapi3.SchemaRef{Value: choiceItemSchema}
|
||||
result.WithProperty("choices", choicesSchema)
|
||||
|
||||
|
||||
// object 字段
|
||||
result.WithProperty("object", openapi3.NewStringSchema().WithEnum("chat.completion"))
|
||||
|
||||
|
||||
// usage 字段
|
||||
usageSchema := openapi3.NewObjectSchema()
|
||||
usageSchema.WithProperty("prompt_tokens", openapi3.NewIntegerSchema())
|
||||
usageSchema.WithProperty("completion_tokens", openapi3.NewIntegerSchema())
|
||||
usageSchema.WithProperty("total_tokens", openapi3.NewIntegerSchema())
|
||||
|
||||
|
||||
// prompt_tokens_details 字段
|
||||
promptTokensDetailsSchema := openapi3.NewObjectSchema()
|
||||
promptTokensDetailsSchema.WithProperty("cached_tokens", openapi3.NewIntegerSchema())
|
||||
usageSchema.WithProperty("prompt_tokens_details", promptTokensDetailsSchema)
|
||||
|
||||
|
||||
result.WithProperty("usage", usageSchema)
|
||||
|
||||
|
||||
// 其他字段
|
||||
result.WithProperty("created", openapi3.NewIntegerSchema())
|
||||
result.WithProperty("system_fingerprint", openapi3.NewStringSchema().WithNullable())
|
||||
result.WithProperty("model", openapi3.NewStringSchema())
|
||||
result.WithProperty("id", openapi3.NewStringSchema())
|
||||
|
||||
|
||||
// 保留原有的错误字段
|
||||
result.WithProperty("code", openapi3.NewIntegerSchema())
|
||||
result.WithProperty("error", openapi3.NewStringSchema())
|
||||
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -78,55 +78,6 @@ type imlCatalogueModule struct {
|
||||
root *Root
|
||||
}
|
||||
|
||||
//func (i *imlCatalogueModule) OnInit() {
|
||||
// register.Handle(func(v server.Server) {
|
||||
// ctx := context.Background()
|
||||
// list, err := i.releaseService.GetRunningList(ctx)
|
||||
// if err != nil {
|
||||
// log.Errorf("onInit: get running list failed:%s", err.Error())
|
||||
// return
|
||||
// }
|
||||
// if len(list) < 1 || list[0].APICount > 0 {
|
||||
// return
|
||||
// }
|
||||
// serviceMap := make(map[string]*release.Release)
|
||||
// serviceIds := make([]string, 0, len(list))
|
||||
// for _, v := range list {
|
||||
// if _, ok := serviceMap[v.Service]; !ok {
|
||||
// serviceMap[v.Service] = v
|
||||
// serviceIds = append(serviceIds, v.Service)
|
||||
// }
|
||||
// }
|
||||
// if len(serviceIds) < 1 {
|
||||
// return
|
||||
// }
|
||||
// commitIds, err := i.releaseService.GetRunningApiDocCommits(ctx, serviceIds...)
|
||||
// if err != nil {
|
||||
// log.Errorf("onInit: get running api doc commits failed:%s", err.Error())
|
||||
// return
|
||||
// }
|
||||
// if len(commitIds) < 1 {
|
||||
// return
|
||||
// }
|
||||
// listCommits, err := i.apiDocService.ListDocCommit(ctx, commitIds...)
|
||||
// if err != nil {
|
||||
// log.Error("onInit: list doc commit failed:", err.Error())
|
||||
// return
|
||||
// }
|
||||
// for _, v := range listCommits {
|
||||
// m, ok := serviceMap[v.Target]
|
||||
// if !ok {
|
||||
// continue
|
||||
// }
|
||||
//
|
||||
// i.releaseService.UpdateRelease(ctx, m.UUID, &release.Update{
|
||||
// APICount: &v.Data.APICount,
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
//
|
||||
//}
|
||||
|
||||
func (i *imlCatalogueModule) DefaultCatalogue(ctx context.Context) (*catalogue_dto.Catalogue, error) {
|
||||
catalogues, err := i.catalogueService.List(ctx)
|
||||
if err != nil {
|
||||
|
||||
+72
-73
@@ -10,7 +10,6 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/subscribe"
|
||||
|
||||
@@ -48,7 +47,7 @@ type imlMcpModule struct {
|
||||
}
|
||||
|
||||
func (i *imlMcpModule) Services(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
keyword, _ := req.Params.Arguments["keyword"].(string)
|
||||
keyword, _ := req.GetArguments()["keyword"].(string)
|
||||
list, err := i.serviceService.Search(ctx, keyword, map[string]interface{}{
|
||||
"as_server": true,
|
||||
}, "update_at desc")
|
||||
@@ -116,34 +115,34 @@ func (i *imlMcpModule) Services(ctx context.Context, req mcp.CallToolRequest) (*
|
||||
|
||||
}
|
||||
|
||||
func (i *imlMcpModule) Apps(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
keyword := req.Params.Arguments["keyword"].(string)
|
||||
condition := make(map[string]interface{})
|
||||
condition["as_app"] = true
|
||||
list, err := i.serviceService.Search(ctx, keyword, condition, "update_at desc")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("search service error: %w", err)
|
||||
}
|
||||
if len(list) == 0 {
|
||||
list, err = i.serviceService.Search(ctx, "", condition, "update_at desc")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("search service error: %w", err)
|
||||
}
|
||||
}
|
||||
data, _ := json.Marshal(utils.SliceToSlice(list, func(s *service.Service) *mcp_dto.App {
|
||||
return &mcp_dto.App{
|
||||
Id: s.Id,
|
||||
Name: s.Name,
|
||||
Description: s.Name,
|
||||
CreateTime: s.CreateTime,
|
||||
UpdateTime: s.UpdateTime,
|
||||
}
|
||||
}))
|
||||
return mcp.NewToolResultText(string(data)), nil
|
||||
}
|
||||
//func (i *imlMcpModule) Apps(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
// keyword := req.GetArguments()["keyword"].(string)
|
||||
// condition := make(map[string]interface{})
|
||||
// condition["as_app"] = true
|
||||
// list, err := i.serviceService.Search(ctx, keyword, condition, "update_at desc")
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("search service error: %w", err)
|
||||
// }
|
||||
// if len(list) == 0 {
|
||||
// list, err = i.serviceService.Search(ctx, "", condition, "update_at desc")
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("search service error: %w", err)
|
||||
// }
|
||||
// }
|
||||
// data, _ := json.Marshal(utils.SliceToSlice(list, func(s *service.Service) *mcp_dto.App {
|
||||
// return &mcp_dto.App{
|
||||
// Id: s.Id,
|
||||
// Name: s.Name,
|
||||
// Description: s.Name,
|
||||
// CreateTime: s.CreateTime,
|
||||
// UpdateTime: s.UpdateTime,
|
||||
// }
|
||||
// }))
|
||||
// return mcp.NewToolResultText(string(data)), nil
|
||||
//}
|
||||
|
||||
func (i *imlMcpModule) APIs(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
serviceId, _ := req.Params.Arguments["service"].(string)
|
||||
serviceId, _ := req.GetArguments()["service"].(string)
|
||||
serviceIds := make([]string, 0, 1)
|
||||
if serviceId == "" {
|
||||
serviceIds = append(serviceIds, serviceId)
|
||||
@@ -190,45 +189,45 @@ func (i *imlMcpModule) APIs(ctx context.Context, req mcp.CallToolRequest) (*mcp.
|
||||
return mcp.NewToolResultText(string(data)), nil
|
||||
}
|
||||
|
||||
func (i *imlMcpModule) SubscriberAuthorizations(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
serviceId, ok := req.Params.Arguments["service"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("service id is required")
|
||||
}
|
||||
subscribes, err := i.subscriberService.Subscribers(ctx, serviceId, subscribe.ApplyStatusSubscribe)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get subscriber error: %w,service id is %s", err, serviceId)
|
||||
}
|
||||
appIds := utils.SliceToSlice(subscribes, func(s *subscribe.Subscribe) string {
|
||||
return s.Application
|
||||
})
|
||||
if len(appIds) == 0 {
|
||||
return nil, fmt.Errorf("no subscriber found,service id is %s", serviceId)
|
||||
}
|
||||
list, err := i.appAuthorizationService.ListByApp(ctx, appIds...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get app authorization error: %w,app ids is %s", err, appIds)
|
||||
}
|
||||
result := utils.SliceToSlice(list, func(a *application_authorization.Authorization) *mcp_dto.AppAuthorization {
|
||||
return &mcp_dto.AppAuthorization{
|
||||
Id: a.UUID,
|
||||
Name: a.Name,
|
||||
Position: a.Position,
|
||||
TokenName: a.TokenName,
|
||||
Config: a.Config,
|
||||
}
|
||||
}, func(a *application_authorization.Authorization) bool {
|
||||
if a.Type != "apikey" {
|
||||
return false
|
||||
}
|
||||
if a.ExpireTime != 0 && a.ExpireTime < time.Now().Unix() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
data, _ := json.Marshal(result)
|
||||
return mcp.NewToolResultText(string(data)), nil
|
||||
}
|
||||
//func (i *imlMcpModule) SubscriberAuthorizations(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
// serviceId, ok := req.GetArguments()["service"].(string)
|
||||
// if !ok {
|
||||
// return nil, fmt.Errorf("service id is required")
|
||||
// }
|
||||
// subscribes, err := i.subscriberService.Subscribers(ctx, serviceId, subscribe.ApplyStatusSubscribe)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("get subscriber error: %w,service id is %s", err, serviceId)
|
||||
// }
|
||||
// appIds := utils.SliceToSlice(subscribes, func(s *subscribe.Subscribe) string {
|
||||
// return s.Application
|
||||
// })
|
||||
// if len(appIds) == 0 {
|
||||
// return nil, fmt.Errorf("no subscriber found,service id is %s", serviceId)
|
||||
// }
|
||||
// list, err := i.appAuthorizationService.ListByApp(ctx, appIds...)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("get app authorization error: %w,app ids is %s", err, appIds)
|
||||
// }
|
||||
// result := utils.SliceToSlice(list, func(a *application_authorization.Authorization) *mcp_dto.AppAuthorization {
|
||||
// return &mcp_dto.AppAuthorization{
|
||||
// Id: a.UUID,
|
||||
// Name: a.Name,
|
||||
// position: a.position,
|
||||
// TokenName: a.TokenName,
|
||||
// Config: a.Config,
|
||||
// }
|
||||
// }, func(a *application_authorization.Authorization) bool {
|
||||
// if a.Type != "apikey" {
|
||||
// return false
|
||||
// }
|
||||
// if a.ExpireTime != 0 && a.ExpireTime < time.Now().Unix() {
|
||||
// return false
|
||||
// }
|
||||
// return true
|
||||
// })
|
||||
// data, _ := json.Marshal(result)
|
||||
// return mcp.NewToolResultText(string(data)), nil
|
||||
//}
|
||||
|
||||
var (
|
||||
client = &http.Client{}
|
||||
@@ -248,18 +247,18 @@ func (i *imlMcpModule) Invoke(ctx context.Context, req mcp.CallToolRequest) (*mc
|
||||
u.Scheme = "http"
|
||||
}
|
||||
|
||||
path, ok := req.Params.Arguments["path"].(string)
|
||||
path, ok := req.GetArguments()["path"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid path")
|
||||
}
|
||||
u.Path = fmt.Sprintf("%s/%s", strings.TrimSuffix(u.Path, "/"), strings.TrimPrefix(path, "/"))
|
||||
|
||||
method, ok := req.Params.Arguments["method"].(string)
|
||||
method, ok := req.GetArguments()["method"].(string)
|
||||
if !ok {
|
||||
method = "GET"
|
||||
}
|
||||
queryParam := url.Values{}
|
||||
query, ok := req.Params.Arguments["query"].(map[string]interface{})
|
||||
query, ok := req.GetArguments()["query"].(map[string]interface{})
|
||||
if ok {
|
||||
for k, v := range query {
|
||||
switch v := v.(type) {
|
||||
@@ -278,7 +277,7 @@ func (i *imlMcpModule) Invoke(ctx context.Context, req mcp.CallToolRequest) (*mc
|
||||
}
|
||||
u.RawQuery = queryParam.Encode()
|
||||
headerParam := http.Header{}
|
||||
header, ok := req.Params.Arguments["header"].(map[string]interface{})
|
||||
header, ok := req.GetArguments()["header"].(map[string]interface{})
|
||||
if ok {
|
||||
for k, v := range header {
|
||||
switch v := v.(type) {
|
||||
@@ -294,12 +293,12 @@ func (i *imlMcpModule) Invoke(ctx context.Context, req mcp.CallToolRequest) (*mc
|
||||
}
|
||||
}
|
||||
|
||||
body, ok := req.Params.Arguments["body"].(string)
|
||||
body, ok := req.GetArguments()["body"].(string)
|
||||
if !ok {
|
||||
body = ""
|
||||
}
|
||||
|
||||
contentType, ok := req.Params.Arguments["content-type"].(string)
|
||||
contentType, ok := req.GetArguments()["content-type"].(string)
|
||||
if !ok {
|
||||
contentType = "application/json"
|
||||
}
|
||||
|
||||
@@ -13,12 +13,12 @@ type IMcpModule interface {
|
||||
// Services 获取服务列表
|
||||
Services(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error)
|
||||
// Apps 获取应用列表
|
||||
Apps(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error)
|
||||
//Apps(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error)
|
||||
// APIs 获取API列表
|
||||
APIs(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error)
|
||||
|
||||
// SubscriberAuthorizations 获取订阅者授权
|
||||
SubscriberAuthorizations(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error)
|
||||
//SubscriberAuthorizations(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error)
|
||||
Invoke(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error)
|
||||
}
|
||||
|
||||
|
||||
+1
-64
@@ -15,8 +15,6 @@ import (
|
||||
|
||||
mcp_server "github.com/APIParkLab/APIPark/mcp-server"
|
||||
api_doc "github.com/APIParkLab/APIPark/service/api-doc"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
|
||||
strategy_driver "github.com/APIParkLab/APIPark/module/strategy/driver"
|
||||
strategy_dto "github.com/APIParkLab/APIPark/module/strategy/dto"
|
||||
@@ -657,68 +655,7 @@ func (i *imlPublishModule) updateMCPServer(ctx context.Context, sid string, name
|
||||
if err != nil {
|
||||
return fmt.Errorf("get api doc commit error: %w", err)
|
||||
}
|
||||
mcpInfo, err := mcp_server.ConvertMCPFromOpenAPI3Data([]byte(commitDoc.Data.Content))
|
||||
if err != nil {
|
||||
return fmt.Errorf("convert mcp from openapi3 data error: %w", err)
|
||||
}
|
||||
tools := make([]mcp_server.ITool, 0, len(mcpInfo.Apis))
|
||||
for _, a := range mcpInfo.Apis {
|
||||
toolOptions := make([]mcp.ToolOption, 0, len(a.Params)+2)
|
||||
toolOptions = append(toolOptions, mcp.WithDescription(a.Description))
|
||||
headers := make(map[string]interface{})
|
||||
queries := make(map[string]interface{})
|
||||
path := make(map[string]interface{})
|
||||
for _, v := range a.Params {
|
||||
p := map[string]interface{}{
|
||||
"type": "string",
|
||||
"required": v.Required,
|
||||
"description": v.Description,
|
||||
}
|
||||
switch v.In {
|
||||
case "header":
|
||||
headers[v.Name] = p
|
||||
case "query":
|
||||
queries[v.Name] = p
|
||||
case "path":
|
||||
path[v.Name] = p
|
||||
}
|
||||
}
|
||||
if len(headers) > 0 {
|
||||
toolOptions = append(toolOptions, mcp.WithObject(mcp_server.MCPHeader, mcp.Properties(headers), mcp.Description("request headers.")))
|
||||
}
|
||||
if len(queries) > 0 {
|
||||
toolOptions = append(toolOptions, mcp.WithObject(mcp_server.MCPQuery, mcp.Properties(queries), mcp.Description("request queries.")))
|
||||
}
|
||||
if len(path) > 0 {
|
||||
toolOptions = append(toolOptions, mcp.WithObject(mcp_server.MCPPath, mcp.Properties(path), mcp.Description("request path params.")))
|
||||
}
|
||||
if a.Body != nil {
|
||||
type Schema struct {
|
||||
Type string `mapstructure:"type"`
|
||||
Properties map[string]interface{} `mapstructure:"properties"`
|
||||
Items interface{} `mapstructure:"items"`
|
||||
}
|
||||
var tmp Schema
|
||||
err = mapstructure.Decode(a.Body, &tmp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//switch a.ContentType {
|
||||
//case "application/json":
|
||||
switch tmp.Type {
|
||||
case "object":
|
||||
toolOptions = append(toolOptions, mcp.WithObject(mcp_server.MCPBody, mcp.Properties(tmp.Properties), mcp.Description("request body,it is avalible when method is POST、PUT、PATCH.")))
|
||||
case "array":
|
||||
toolOptions = append(toolOptions, mcp.WithArray(mcp_server.MCPBody, mcp.Items(tmp.Items), mcp.Description("request body,it is avalible when method is POST、PUT、PATCH.")))
|
||||
}
|
||||
//case "application/x-www-form-urlencoded":
|
||||
// toolOptions = append(toolOptions, mcp.WithString(mcp_server.MCPBody, mcp.Items(tmp.Items), mcp.Description("request body,it is avalible when method is POST、PUT、PATCH.")))
|
||||
|
||||
}
|
||||
tools = append(tools, mcp_server.NewTool(a.Summary, a.Path, a.Method, a.ContentType, toolOptions...))
|
||||
}
|
||||
mcp_server.SetSSEServer(sid, name, version, tools...)
|
||||
return nil
|
||||
return mcp_server.SetServerByOpenapi(sid, name, version, commitDoc.Data.Content)
|
||||
}
|
||||
|
||||
func (i *imlPublishModule) Detail(ctx context.Context, serviceId string, id string) (*dto.PublishDetail, error) {
|
||||
|
||||
+2
-62
@@ -14,12 +14,8 @@ import (
|
||||
|
||||
"github.com/APIParkLab/APIPark/common"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
|
||||
"github.com/eolinker/go-common/register"
|
||||
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
|
||||
mcp_server "github.com/APIParkLab/APIPark/mcp-server"
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/release"
|
||||
@@ -395,67 +391,11 @@ func (i *imlServiceModule) updateMCPServer(ctx context.Context, sid string, name
|
||||
if err != nil {
|
||||
return fmt.Errorf("get api doc commit error: %w", err)
|
||||
}
|
||||
mcpInfo, err := mcp_server.ConvertMCPFromOpenAPI3Data([]byte(commitDoc.Data.Content))
|
||||
if err != nil {
|
||||
return fmt.Errorf("convert mcp from openapi3 data error: %w", err)
|
||||
}
|
||||
tools := make([]mcp_server.ITool, 0, len(mcpInfo.Apis))
|
||||
for _, a := range mcpInfo.Apis {
|
||||
toolOptions := make([]mcp.ToolOption, 0, len(a.Params)+2)
|
||||
toolOptions = append(toolOptions, mcp.WithDescription(a.Description))
|
||||
headers := make(map[string]interface{})
|
||||
queries := make(map[string]interface{})
|
||||
path := make(map[string]interface{})
|
||||
for _, v := range a.Params {
|
||||
p := map[string]interface{}{
|
||||
"type": "string",
|
||||
"required": v.Required,
|
||||
"description": v.Description,
|
||||
}
|
||||
switch v.In {
|
||||
case "header":
|
||||
headers[v.Name] = p
|
||||
case "query":
|
||||
queries[v.Name] = p
|
||||
case "path":
|
||||
path[v.Name] = p
|
||||
}
|
||||
}
|
||||
if len(headers) > 0 {
|
||||
toolOptions = append(toolOptions, mcp.WithObject(mcp_server.MCPHeader, mcp.Properties(headers), mcp.Description("request headers.")))
|
||||
}
|
||||
if len(queries) > 0 {
|
||||
toolOptions = append(toolOptions, mcp.WithObject(mcp_server.MCPQuery, mcp.Properties(queries), mcp.Description("request queries.")))
|
||||
}
|
||||
if len(path) > 0 {
|
||||
toolOptions = append(toolOptions, mcp.WithObject(mcp_server.MCPPath, mcp.Properties(path), mcp.Description("request path params.")))
|
||||
}
|
||||
if a.Body != nil {
|
||||
type Schema struct {
|
||||
Type string `mapstructure:"type"`
|
||||
Properties map[string]interface{} `mapstructure:"properties"`
|
||||
Items interface{} `mapstructure:"items"`
|
||||
}
|
||||
var tmp Schema
|
||||
err = mapstructure.Decode(a.Body, &tmp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch tmp.Type {
|
||||
case "object":
|
||||
toolOptions = append(toolOptions, mcp.WithObject(mcp_server.MCPBody, mcp.Properties(tmp.Properties), mcp.Description("request body,it is avalible when method is POST、PUT、PATCH.")))
|
||||
case "array":
|
||||
toolOptions = append(toolOptions, mcp.WithArray(mcp_server.MCPBody, mcp.Items(tmp.Items), mcp.Description("request body,it is avalible when method is POST、PUT、PATCH.")))
|
||||
}
|
||||
}
|
||||
tools = append(tools, mcp_server.NewTool(a.Summary, a.Path, a.Method, a.ContentType, toolOptions...))
|
||||
}
|
||||
mcp_server.SetSSEServer(sid, name, version, tools...)
|
||||
return nil
|
||||
return mcp_server.SetServerByOpenapi(sid, name, version, commitDoc.Data.Content)
|
||||
}
|
||||
|
||||
func (i *imlServiceModule) deleteMCPServer(ctx context.Context, sid string) {
|
||||
mcp_server.DelSSEServer(sid)
|
||||
mcp_server.DelServer(sid)
|
||||
}
|
||||
|
||||
func (i *imlServiceModule) ExportAll(ctx context.Context) ([]*service_dto.ExportService, error) {
|
||||
|
||||
Reference in New Issue
Block a user