fix: snapshot MCP configuration and make tool deadline failures consistent

This commit is contained in:
Sycun committed 2026-10-09 10:58:12 +08:00
1 parent e8a5392b37
commit b841296f0e
7 files changed
+83 -8

No files matched your search

+7 -7
View File
@@ -172,13 +172,6 @@ func (h *ExternalMCPHandler) AddOrUpdateExternalMCP(c *gin.Context) {
h.mu.Lock()
defer h.mu.Unlock()
// 添加或更新配置
if err := h.manager.AddOrUpdateConfig(name, req.Config); err != nil {
h.logger.Error("添加或更新外部MCP配置失败", zap.Error(err))
c.JSON(http.StatusInternalServerError, gin.H{"error": "添加或更新配置失败: " + err.Error()})
return
}
// 更新内存中的配置
if h.config.ExternalMCP.Servers == nil {
h.config.ExternalMCP.Servers = make(map[string]config.ExternalMCPServerConfig)
@@ -197,6 +190,13 @@ func (h *ExternalMCPHandler) AddOrUpdateExternalMCP(c *gin.Context) {
// 展开 ${VAR} 环境变量
config.ExpandConfigEnv(&cfg)
// 添加或更新配置
if err := h.manager.AddOrUpdateConfig(name, cfg); err != nil {
h.logger.Error("添加或更新外部MCP配置失败", zap.Error(err))
c.JSON(http.StatusInternalServerError, gin.H{"error": "添加或更新配置失败: " + err.Error()})
return
}
h.config.ExternalMCP.Servers[name] = cfg
// 保存到配置文件
+28
View File
@@ -56,6 +56,34 @@ func cleanupTestConfig(configPath string) {
os.Remove(configPath + ".backup")
}
func TestExternalMCPConnectionReceivesExpandedSnapshot(t *testing.T) {
t.Setenv("CSAI_TEST_MCP_COMMAND", "not-executed")
t.Setenv("CSAI_TEST_MCP_VALUE", "expanded-test-value")
router, h, configPath := setupTestRouter()
defer cleanupTestConfig(configPath)
body, err := json.Marshal(AddOrUpdateExternalMCPRequest{Config: config.ExternalMCPServerConfig{
Command: "${CSAI_TEST_MCP_COMMAND}", Args: []string{"${CSAI_TEST_MCP_VALUE}"}, Disabled: true,
Env: map[string]string{"VALUE": "${CSAI_TEST_MCP_VALUE}"}, Headers: map[string]string{"X-Value": "${CSAI_TEST_MCP_VALUE}"},
}})
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodPut, "/api/external-mcp/test-env", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("save: %d %s", w.Code, w.Body)
}
cfg := h.manager.GetConfigs()["test-env"]
if cfg.Command != "not-executed" || len(cfg.Args) != 1 || cfg.Args[0] != "expanded-test-value" || cfg.Env["VALUE"] != "expanded-test-value" || cfg.Headers["X-Value"] != "expanded-test-value" {
t.Fatalf("connection received unexpanded snapshot: %#v", cfg)
}
if !cfg.Disabled || cfg.ExternalMCPEnable {
t.Fatal("disabled connection was activated")
}
}
func TestExternalMCPHandler_AddOrUpdateExternalMCP_Stdio(t *testing.T) {
router, _, configPath := setupTestRouter()
defer cleanupTestConfig(configPath)