feat: Add MCP services support and enhance migration scripts

- Introduced new MCP services functionality, including management routes and integration with the agent service.
- Updated Docker Compose configuration to include a new SQL migration for MCP services.
- Enhanced Makefile with new migration commands for versioning and creating migrations.
- Improved chat component to handle selected MCP services in message processing.
- Updated migration documentation to reflect new strategies and added MCP services migration files.
This commit is contained in:
wizardchen
2025-11-09 03:27:24 +08:00
parent 83721f98d4
commit 77b6133e2a
34 changed files with 3577 additions and 1063 deletions
+62
View File
@@ -0,0 +1,62 @@
package interfaces
import (
"context"
"github.com/Tencent/WeKnora/internal/types"
)
// MCPServiceRepository defines the interface for MCP service data access
type MCPServiceRepository interface {
// Create creates a new MCP service
Create(ctx context.Context, service *types.MCPService) error
// GetByID retrieves an MCP service by ID and tenant ID
GetByID(ctx context.Context, tenantID uint, id string) (*types.MCPService, error)
// List retrieves all MCP services for a tenant
List(ctx context.Context, tenantID uint) ([]*types.MCPService, error)
// ListEnabled retrieves all enabled MCP services for a tenant
ListEnabled(ctx context.Context, tenantID uint) ([]*types.MCPService, error)
// ListByIDs retrieves MCP services by multiple IDs for a tenant
ListByIDs(ctx context.Context, tenantID uint, ids []string) ([]*types.MCPService, error)
// Update updates an MCP service
Update(ctx context.Context, service *types.MCPService) error
// Delete deletes an MCP service (soft delete)
Delete(ctx context.Context, tenantID uint, id string) error
}
// MCPServiceService defines the interface for MCP service business logic
type MCPServiceService interface {
// CreateMCPService creates a new MCP service
CreateMCPService(ctx context.Context, service *types.MCPService) error
// GetMCPServiceByID retrieves an MCP service by ID
GetMCPServiceByID(ctx context.Context, tenantID uint, id string) (*types.MCPService, error)
// ListMCPServices lists all MCP services for a tenant
ListMCPServices(ctx context.Context, tenantID uint) ([]*types.MCPService, error)
// ListMCPServicesByIDs retrieves multiple MCP services by IDs
ListMCPServicesByIDs(ctx context.Context, tenantID uint, ids []string) ([]*types.MCPService, error)
// UpdateMCPService updates an MCP service
UpdateMCPService(ctx context.Context, service *types.MCPService) error
// DeleteMCPService deletes an MCP service
DeleteMCPService(ctx context.Context, tenantID uint, id string) error
// TestMCPService tests the connection to an MCP service and returns available tools/resources
TestMCPService(ctx context.Context, tenantID uint, id string) (*types.MCPTestResult, error)
// GetMCPServiceTools retrieves the list of tools from an MCP service
GetMCPServiceTools(ctx context.Context, tenantID uint, id string) ([]*types.MCPTool, error)
// GetMCPServiceResources retrieves the list of resources from an MCP service
GetMCPServiceResources(ctx context.Context, tenantID uint, id string) ([]*types.MCPResource, error)
}
+173
View File
@@ -0,0 +1,173 @@
package types
import (
"database/sql/driver"
"encoding/json"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// MCPTransportType represents the transport type for MCP service
type MCPTransportType string
const (
MCPTransportSSE MCPTransportType = "sse" // Server-Sent Events
MCPTransportHTTPStreamable MCPTransportType = "http-streamable" // HTTP Streamable
)
// MCPService represents an MCP (Model Context Protocol) service configuration
type MCPService struct {
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
TenantID uint `json:"tenant_id" gorm:"index"`
Name string `json:"name" gorm:"type:varchar(255);not null"`
Description string `json:"description" gorm:"type:text"`
Enabled bool `json:"enabled" gorm:"default:true;index"`
TransportType MCPTransportType `json:"transport_type" gorm:"type:varchar(50);not null"`
URL string `json:"url" gorm:"type:varchar(512);not null"`
Headers MCPHeaders `json:"headers" gorm:"type:json"`
AuthConfig *MCPAuthConfig `json:"auth_config" gorm:"type:json"`
AdvancedConfig *MCPAdvancedConfig `json:"advanced_config" gorm:"type:json"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}
// MCPHeaders represents HTTP headers as a map
type MCPHeaders map[string]string
// MCPAuthConfig represents authentication configuration for MCP service
type MCPAuthConfig struct {
APIKey string `json:"api_key,omitempty"`
Token string `json:"token,omitempty"`
CustomHeaders map[string]string `json:"custom_headers,omitempty"`
}
// MCPAdvancedConfig represents advanced configuration for MCP service
type MCPAdvancedConfig struct {
Timeout int `json:"timeout"` // Timeout in seconds, default: 30
RetryCount int `json:"retry_count"` // Number of retries, default: 3
RetryDelay int `json:"retry_delay"` // Delay between retries in seconds, default: 1
}
// MCPTool represents a tool exposed by an MCP service
type MCPTool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema map[string]interface{} `json:"inputSchema"` // JSON Schema for tool parameters
}
// MCPResource represents a resource exposed by an MCP service
type MCPResource struct {
URI string `json:"uri"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
// MCPTestResult represents the result of testing an MCP service connection
type MCPTestResult struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Tools []*MCPTool `json:"tools,omitempty"`
Resources []*MCPResource `json:"resources,omitempty"`
}
// BeforeCreate is a GORM hook that runs before creating a new MCP service
func (m *MCPService) BeforeCreate(tx *gorm.DB) error {
if m.ID == "" {
m.ID = uuid.New().String()
}
return nil
}
// Value implements driver.Valuer interface for MCPHeaders
func (h MCPHeaders) Value() (driver.Value, error) {
if h == nil {
return nil, nil
}
return json.Marshal(h)
}
// Scan implements sql.Scanner interface for MCPHeaders
func (h *MCPHeaders) Scan(value interface{}) error {
if value == nil {
*h = nil
return nil
}
b, ok := value.([]byte)
if !ok {
return nil
}
return json.Unmarshal(b, h)
}
// Value implements driver.Valuer interface for MCPAuthConfig
func (c *MCPAuthConfig) Value() (driver.Value, error) {
if c == nil {
return nil, nil
}
return json.Marshal(c)
}
// Scan implements sql.Scanner interface for MCPAuthConfig
func (c *MCPAuthConfig) Scan(value interface{}) error {
if value == nil {
return nil
}
b, ok := value.([]byte)
if !ok {
return nil
}
return json.Unmarshal(b, c)
}
// Value implements driver.Valuer interface for MCPAdvancedConfig
func (c *MCPAdvancedConfig) Value() (driver.Value, error) {
if c == nil {
return nil, nil
}
return json.Marshal(c)
}
// Scan implements sql.Scanner interface for MCPAdvancedConfig
func (c *MCPAdvancedConfig) Scan(value interface{}) error {
if value == nil {
return nil
}
b, ok := value.([]byte)
if !ok {
return nil
}
return json.Unmarshal(b, c)
}
// GetDefaultAdvancedConfig returns default advanced configuration
func GetDefaultAdvancedConfig() *MCPAdvancedConfig {
return &MCPAdvancedConfig{
Timeout: 30,
RetryCount: 3,
RetryDelay: 1,
}
}
// MaskSensitiveData masks sensitive information in the MCP service for display
func (m *MCPService) MaskSensitiveData() {
if m.AuthConfig != nil {
if m.AuthConfig.APIKey != "" {
m.AuthConfig.APIKey = maskString(m.AuthConfig.APIKey)
}
if m.AuthConfig.Token != "" {
m.AuthConfig.Token = maskString(m.AuthConfig.Token)
}
}
}
// maskString masks a string, showing only first 4 and last 4 characters
func maskString(s string) string {
if len(s) <= 8 {
return "****"
}
return s[:4] + "****" + s[len(s)-4:]
}