finsh mcp api

This commit is contained in:
Liujian
2025-04-07 11:50:14 +08:00
parent 0bc89cda8c
commit da05525cbb
102 changed files with 2574 additions and 60 deletions
+10 -1
View File
@@ -1,9 +1,14 @@
package core
import (
ai_model "github.com/APIParkLab/APIPark/controller/ai-model"
"net/http"
system_apikey "github.com/APIParkLab/APIPark/controller/system-apikey"
"github.com/APIParkLab/APIPark/controller/mcp"
ai_model "github.com/APIParkLab/APIPark/controller/ai-model"
ai_balance "github.com/APIParkLab/APIPark/controller/ai-balance"
ai_local "github.com/APIParkLab/APIPark/controller/ai-local"
@@ -102,6 +107,8 @@ type plugin struct {
settingController system.ISettingController `autowired:""`
initController system.IInitController `autowired:""`
logController log.ILogController `autowired:""`
mcpController mcp.IMcpController `autowired:""`
systemAPIKeyController system_apikey.IAPIKeyController `autowired:""`
apis []pm3.Api
}
@@ -128,6 +135,8 @@ func (p *plugin) OnComplete() {
p.apis = append(p.apis, p.logApis()...)
p.apis = append(p.apis, p.aiLocalApis()...)
p.apis = append(p.apis, p.aiBalanceAPIs()...)
p.apis = append(p.apis, p.mcpAPIs()...)
p.apis = append(p.apis, p.systemApikeyApis()...)
}
func (p *plugin) Name() string {
+22
View File
@@ -0,0 +1,22 @@
package core
import (
"fmt"
"net/http"
"github.com/eolinker/go-common/ignore"
mcp_server "github.com/APIParkLab/APIPark/mcp-server"
"github.com/eolinker/go-common/pm3"
)
func (p *plugin) mcpAPIs() []pm3.Api {
serviceMCPPath := fmt.Sprintf("%s/:serviceId/:event", mcp_server.ServiceBasePath)
ignore.IgnorePath("login", http.MethodGet, serviceMCPPath)
ignore.IgnorePath("login", http.MethodPost, serviceMCPPath)
return []pm3.Api{
pm3.CreateApiSimple(http.MethodGet, serviceMCPPath, p.mcpController.MCPHandle),
pm3.CreateApiSimple(http.MethodPost, serviceMCPPath, p.mcpController.MCPHandle),
}
}
+2
View File
@@ -11,6 +11,7 @@ import (
func (p *plugin) ServiceApis() []pm3.Api {
ignore.IgnorePath("login", http.MethodGet, "/api/v1/service/swagger/:id")
ignore.IgnorePath("login", http.MethodGet, "/api/v1/service/apidoc/:id")
return []pm3.Api{
// 项目
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/service/info", []string{"context", "query:service"}, []string{"service"}, p.serviceController.Get, access.SystemWorkspaceServiceViewAll, access.TeamTeamServiceView),
@@ -36,6 +37,7 @@ func (p *plugin) ServiceApis() []pm3.Api {
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/service/doc", []string{"context", "query:service"}, []string{"doc"}, p.serviceController.ServiceDoc, access.SystemWorkspaceServiceViewAll, access.TeamServiceServiceIntroView),
pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/service/doc", []string{"context", "query:service", "body"}, nil, p.serviceController.SaveServiceDoc, access.SystemWorkspaceServiceManagerAll, access.TeamServiceServiceIntroManager),
pm3.CreateApiSimple(http.MethodGet, "/api/v1/service/swagger/:id", p.serviceController.Swagger),
pm3.CreateApiSimple(http.MethodGet, "/api/v1/service/apidoc/:id", p.serviceController.Swagger),
pm3.CreateApiSimple(http.MethodGet, "/api/v1/export/openapi/:id", p.serviceController.ExportSwagger),
}
}
+11
View File
@@ -15,3 +15,14 @@ func (p *plugin) systemApis() []pm3.Api {
pm3.CreateApiWidthDoc(http.MethodPost, "/api/v1/system/general", []string{"context", "body"}, nil, p.settingController.Set),
}
}
func (p *plugin) systemApikeyApis() []pm3.Api {
return []pm3.Api{
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/system/apikey", []string{"context", "query:apikey"}, []string{"apikey"}, p.systemAPIKeyController.Get),
pm3.CreateApiWidthDoc(http.MethodPost, "/api/v1/system/apikey", []string{"context", "body"}, nil, p.systemAPIKeyController.Create),
pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/system/apikey", []string{"context", "query:apikey", "body"}, nil, p.systemAPIKeyController.Update),
pm3.CreateApiWidthDoc(http.MethodDelete, "/api/v1/system/apikey", []string{"context", "query:apikey"}, nil, p.systemAPIKeyController.Delete),
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/system/apikeys", []string{"context", "query:keyword"}, []string{"apikeys"}, p.systemAPIKeyController.Search),
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/simple/system/apikeys", []string{"context"}, []string{"apikeys"}, p.systemAPIKeyController.SimpleList),
}
}
+41 -2
View File
@@ -2,6 +2,13 @@ package openapi
import (
"strings"
"time"
"github.com/eolinker/go-common/ignore"
"github.com/eolinker/go-common/autowire"
system_apikey "github.com/APIParkLab/APIPark/module/system-apikey"
"github.com/eolinker/eosc/env"
@@ -14,7 +21,8 @@ var (
)
type openapiCheck struct {
apikey string
apikey string
apikeyModule system_apikey.IAPIKeyModule `autowired:""`
}
func newOpenapiCheck() *openapiCheck {
@@ -22,7 +30,9 @@ func newOpenapiCheck() *openapiCheck {
if !has {
apikey = defaultAPIKey
}
return &openapiCheck{apikey: apikey}
p := &openapiCheck{apikey: apikey}
autowire.Autowired(p)
return p
}
func (o *openapiCheck) Check(method string, path string) (bool, []gin.HandlerFunc) {
@@ -37,9 +47,38 @@ func (o *openapiCheck) Sort() int {
}
func (o *openapiCheck) Handler(ginCtx *gin.Context) {
notIgnore := !ignore.IsIgnorePath("openapi", ginCtx.Request.Method, ginCtx.FullPath())
if !notIgnore {
return
}
authorization := ginCtx.GetHeader("Authorization")
if authorization == "" {
apikey, has := ginCtx.GetQuery("apikey")
if !has {
ginCtx.AbortWithStatusJSON(403, gin.H{"code": -8, "msg": "invalid token", "success": "fail"})
return
}
authorization = apikey
}
if authorization == o.apikey {
return
}
list, err := o.apikeyModule.SimpleList(ginCtx)
if err != nil {
ginCtx.AbortWithStatusJSON(403, gin.H{"code": -8, "msg": "invalid token", "success": "fail"})
return
}
if len(list) == 0 {
ginCtx.AbortWithStatusJSON(403, gin.H{"code": -8, "msg": "invalid token", "success": "fail"})
return
}
for _, item := range list {
if item.Value == authorization {
if item.Expired != 0 && item.Expired < time.Now().Unix() {
continue
}
return
}
}
ginCtx.AbortWithStatusJSON(403, gin.H{"code": -8, "msg": "invalid token", "success": "fail"})
}
+20
View File
@@ -0,0 +1,20 @@
package openapi
import (
"fmt"
"net/http"
"github.com/eolinker/go-common/ignore"
mcp_server "github.com/APIParkLab/APIPark/mcp-server"
"github.com/eolinker/go-common/pm3"
)
func (p *plugin) mcpAPIs() []pm3.Api {
messagePath := fmt.Sprintf("%s/message", mcp_server.GlobalBasePath)
ignore.IgnorePath("openapi", http.MethodPost, messagePath)
return []pm3.Api{
pm3.CreateApiSimple(http.MethodGet, fmt.Sprintf("%s/sse", mcp_server.GlobalBasePath), p.mcpController.GlobalMCPHandle),
pm3.CreateApiSimple(http.MethodPost, messagePath, p.mcpController.GlobalMCPHandle),
}
}
+3
View File
@@ -2,6 +2,7 @@ package openapi
import (
application_authorization "github.com/APIParkLab/APIPark/controller/application-authorization"
"github.com/APIParkLab/APIPark/controller/mcp"
"github.com/eolinker/go-common/pm3"
)
@@ -13,6 +14,7 @@ var (
type plugin struct {
apis []pm3.Api
authorizationController application_authorization.IAuthorizationController `autowired:""`
mcpController mcp.IMcpController `autowired:""`
}
func (p *plugin) Middlewares() []pm3.IMiddleware {
@@ -30,4 +32,5 @@ func (p *plugin) Name() string {
}
func (p *plugin) OnComplete() {
p.apis = p.appAuthorizationApis()
p.apis = append(p.apis, p.mcpAPIs()...)
}