Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/application/service/mcp_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (s *mcpServiceService) DeleteMCPService(ctx context.Context, tenantID uint6
return fmt.Errorf("failed to get MCP service: %w", err)
}
if existing == nil {
return fmt.Errorf("MCP service not found")
return types.ErrMCPServiceNotFound
}

// Builtin MCP services cannot be deleted
Expand Down Expand Up @@ -487,7 +487,7 @@ func (s *mcpServiceService) GetMCPServiceTools(
return nil, fmt.Errorf("failed to get MCP service: %w", err)
}
if service == nil {
return nil, fmt.Errorf("MCP service not found")
return nil, types.ErrMCPServiceNotFound
}

// Get or create client
Expand Down Expand Up @@ -634,7 +634,7 @@ func (s *mcpServiceService) GetMCPServiceResources(
return nil, fmt.Errorf("failed to get MCP service: %w", err)
}
if service == nil {
return nil, fmt.Errorf("MCP service not found")
return nil, types.ErrMCPServiceNotFound
}

// Get or create client
Expand Down
18 changes: 15 additions & 3 deletions internal/handler/mcp_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,11 @@ func (h *MCPServiceHandler) DeleteMCPService(c *gin.Context) {

if err := h.mcpServiceService.DeleteMCPService(ctx, tenantID, serviceID); err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{"service_id": secutils.SanitizeForLog(serviceID)})
c.Error(errors.NewInternalServerError("Failed to delete MCP service: " + err.Error()))
if stderrors.Is(err, types.ErrMCPServiceNotFound) {
_ = c.Error(errors.NewNotFoundError("MCP service not found"))
return
}
_ = c.Error(errors.NewInternalServerError("Failed to delete MCP service"))
return
}

Expand Down Expand Up @@ -714,7 +718,11 @@ func (h *MCPServiceHandler) GetMCPServiceTools(c *gin.Context) {
tools, err := h.mcpServiceService.GetMCPServiceTools(ctx, tenantID, serviceID)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{"service_id": secutils.SanitizeForLog(serviceID)})
c.Error(errors.NewInternalServerError("Failed to get MCP service tools: " + err.Error()))
if stderrors.Is(err, types.ErrMCPServiceNotFound) {
_ = c.Error(errors.NewNotFoundError("MCP service not found"))
return
}
_ = c.Error(errors.NewInternalServerError("Failed to get MCP service tools"))
return
}

Expand Down Expand Up @@ -750,7 +758,11 @@ func (h *MCPServiceHandler) GetMCPServiceResources(c *gin.Context) {
resources, err := h.mcpServiceService.GetMCPServiceResources(ctx, tenantID, serviceID)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{"service_id": secutils.SanitizeForLog(serviceID)})
c.Error(errors.NewInternalServerError("Failed to get MCP service resources: " + err.Error()))
if stderrors.Is(err, types.ErrMCPServiceNotFound) {
_ = c.Error(errors.NewNotFoundError("MCP service not found"))
return
}
_ = c.Error(errors.NewInternalServerError("Failed to get MCP service resources"))
return
}

Expand Down
72 changes: 72 additions & 0 deletions internal/handler/mcp_service_not_found_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package handler

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/application/service"
"github.com/Tencent/WeKnora/internal/middleware"
"github.com/Tencent/WeKnora/internal/types"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

func newMCPRegistrationRouter(t *testing.T) (*gin.Engine, *gorm.DB) {
t.Helper()
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
require.NoError(t, err)
sqlDB, err := db.DB()
require.NoError(t, err)
sqlDB.SetMaxOpenConns(1)
t.Cleanup(func() { _ = sqlDB.Close() })
require.NoError(t, db.AutoMigrate(&types.MCPService{}))

repo := repository.NewMCPServiceRepository(db)
svc := service.NewMCPServiceService(repo, nil, nil)
h := NewMCPServiceHandler(svc, nil, nil, nil, nil)
r := gin.New()
r.Use(middleware.ErrorHandler(), func(c *gin.Context) {
c.Set(types.TenantIDContextKey.String(), uint64(1))
})
r.DELETE("/mcp-services/:id", h.DeleteMCPService)
r.GET("/mcp-services/:id/tools", h.GetMCPServiceTools)
r.GET("/mcp-services/:id/resources", h.GetMCPServiceResources)
return r, db
}

func TestMCPServiceMissingRegistrationReturnsNotFound(t *testing.T) {
router, _ := newMCPRegistrationRouter(t)
for _, tc := range []struct{ method, path string }{
{http.MethodDelete, "/mcp-services/missing"},
{http.MethodGet, "/mcp-services/missing/tools"},
{http.MethodGet, "/mcp-services/missing/resources"},
} {
rec := httptest.NewRecorder()
router.ServeHTTP(rec, httptest.NewRequest(tc.method, tc.path, nil))
require.Equal(t, http.StatusNotFound, rec.Code, "%s %s: %s", tc.method, tc.path, rec.Body.String())
require.NotContains(t, rec.Body.String(), "Internal server error")
}
}

func TestMCPServiceLookupFailureStaysInternalError(t *testing.T) {
router, db := newMCPRegistrationRouter(t)
require.NoError(t, db.Create(&types.MCPService{ID: "svc", Name: "Orders", TenantID: 1}).Error)
sqlDB, err := db.DB()
require.NoError(t, err)
require.NoError(t, sqlDB.Close())

for _, tc := range []struct{ method, path string }{
{http.MethodDelete, "/mcp-services/svc"},
{http.MethodGet, "/mcp-services/svc/tools"},
{http.MethodGet, "/mcp-services/svc/resources"},
} {
rec := httptest.NewRecorder()
router.ServeHTTP(rec, httptest.NewRequest(tc.method, tc.path, nil))
require.Equal(t, http.StatusInternalServerError, rec.Code, "%s %s: %s", tc.method, tc.path, rec.Body.String())
require.NotContains(t, rec.Body.String(), "database is closed")
}
}