feat: add built-in MCP service support

This commit is contained in:
AndyYang
2026-03-05 20:31:29 +08:00
committed by lyingbug
parent ead6712955
commit 6dde1330c2
14 changed files with 257 additions and 9 deletions
@@ -25,10 +25,12 @@ func (r *mcpServiceRepository) Create(ctx context.Context, service *types.MCPSer
}
// GetByID retrieves an MCP service by ID and tenant ID
// Builtin MCP services are visible to all tenants
func (r *mcpServiceRepository) GetByID(ctx context.Context, tenantID uint64, id string) (*types.MCPService, error) {
var service types.MCPService
err := r.db.WithContext(ctx).
Where("id = ? AND tenant_id = ?", id, tenantID).
Where("id = ?", id).
Where("tenant_id = ? OR is_builtin = true", tenantID).
First(&service).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -41,10 +43,11 @@ func (r *mcpServiceRepository) GetByID(ctx context.Context, tenantID uint64, id
}
// List retrieves all MCP services for a tenant
// Includes builtin MCP services visible to all tenants
func (r *mcpServiceRepository) List(ctx context.Context, tenantID uint64) ([]*types.MCPService, error) {
var services []*types.MCPService
err := r.db.WithContext(ctx).
Where("tenant_id = ?", tenantID).
Where("tenant_id = ? OR is_builtin = true", tenantID).
Order("created_at DESC").
Find(&services).Error
if err != nil {
@@ -55,10 +58,11 @@ func (r *mcpServiceRepository) List(ctx context.Context, tenantID uint64) ([]*ty
}
// ListEnabled retrieves all enabled MCP services for a tenant
// Includes enabled builtin MCP services visible to all tenants
func (r *mcpServiceRepository) ListEnabled(ctx context.Context, tenantID uint64) ([]*types.MCPService, error) {
var services []*types.MCPService
err := r.db.WithContext(ctx).
Where("tenant_id = ? AND enabled = ?", tenantID, true).
Where("(tenant_id = ? OR is_builtin = true) AND enabled = ?", tenantID, true).
Order("created_at DESC").
Find(&services).Error
if err != nil {
@@ -69,6 +73,7 @@ func (r *mcpServiceRepository) ListEnabled(ctx context.Context, tenantID uint64)
}
// ListByIDs retrieves MCP services by multiple IDs for a tenant
// Includes builtin MCP services visible to all tenants
func (r *mcpServiceRepository) ListByIDs(
ctx context.Context,
tenantID uint64,
@@ -80,7 +85,7 @@ func (r *mcpServiceRepository) ListByIDs(
var services []*types.MCPService
err := r.db.WithContext(ctx).
Where("tenant_id = ? AND id IN ?", tenantID, ids).
Where("(tenant_id = ? OR is_builtin = true) AND id IN ?", tenantID, ids).
Find(&services).Error
if err != nil {
return nil, err
+16 -2
View File
@@ -81,8 +81,12 @@ func (s *mcpServiceService) ListMCPServices(ctx context.Context, tenantID uint64
}
// Mask sensitive data for list view
for _, service := range services {
service.MaskSensitiveData()
for i, service := range services {
if service.IsBuiltin {
services[i] = service.HideSensitiveInfo()
} else {
service.MaskSensitiveData()
}
}
return services, nil
@@ -118,6 +122,11 @@ func (s *mcpServiceService) UpdateMCPService(ctx context.Context, service *types
return fmt.Errorf("MCP service not found")
}
// Builtin MCP services cannot be updated
if existing.IsBuiltin {
return fmt.Errorf("builtin MCP services cannot be updated")
}
// Determine the final transport type after merge
finalTransportType := existing.TransportType
if service.TransportType != "" {
@@ -232,6 +241,11 @@ func (s *mcpServiceService) DeleteMCPService(ctx context.Context, tenantID uint6
return fmt.Errorf("MCP service not found")
}
// Builtin MCP services cannot be deleted
if existing.IsBuiltin {
return fmt.Errorf("builtin MCP services cannot be deleted")
}
// Close client connection
s.mcpManager.CloseClient(id)
+7 -1
View File
@@ -129,9 +129,15 @@ func (h *MCPServiceHandler) GetMCPService(c *gin.Context) {
return
}
// Hide sensitive information for builtin MCP services
responseService := service
if service.IsBuiltin {
responseService = service.HideSensitiveInfo()
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": service,
"data": responseService,
})
}
+16
View File
@@ -32,6 +32,7 @@ type MCPService struct {
AdvancedConfig *MCPAdvancedConfig `json:"advanced_config" gorm:"type:json"`
StdioConfig *MCPStdioConfig `json:"stdio_config,omitempty" gorm:"type:json"` // Required for stdio transport
EnvVars MCPEnvVars `json:"env_vars,omitempty" gorm:"type:json"` // Environment variables for stdio
IsBuiltin bool `json:"is_builtin" gorm:"default:false"` // Whether this is a builtin MCP service (visible to all tenants)
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
@@ -217,6 +218,21 @@ func (m *MCPService) MaskSensitiveData() {
}
}
// HideSensitiveInfo returns a copy of the MCP service with sensitive fields cleared for builtin services
func (m *MCPService) HideSensitiveInfo() *MCPService {
if !m.IsBuiltin {
return m
}
copy := *m
copy.URL = nil
copy.AuthConfig = nil
copy.Headers = nil
copy.EnvVars = nil
copy.StdioConfig = nil
return &copy
}
// maskString masks a string, showing only first 4 and last 4 characters
func maskString(s string) string {
if len(s) <= 8 {