From efea8bdbf712f88f4eea69dad43c2e0c125c93d1 Mon Sep 17 00:00:00 2001 From: cwz_eikoh Date: Tue, 3 Mar 2026 16:06:07 +0800 Subject: [PATCH] feat(mcp-server): add mcp-server config (#24354) * feat(mcp-server): add mcp-server config * fix(mcp-agent): add claude mcp-server-config --- pkg/apigateway/handler/mcp_agent.go | 40 +++++++++++++++++++++++++++++ pkg/apigateway/handler/misc.go | 3 +++ 2 files changed, 43 insertions(+) diff --git a/pkg/apigateway/handler/mcp_agent.go b/pkg/apigateway/handler/mcp_agent.go index a1fdaa78ab..dada2d9a1f 100644 --- a/pkg/apigateway/handler/mcp_agent.go +++ b/pkg/apigateway/handler/mcp_agent.go @@ -2,6 +2,7 @@ package handler import ( "context" + "encoding/json" "fmt" "io" "net/http" @@ -10,13 +11,52 @@ import ( "yunion.io/x/log" "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/util/httputils" + "yunion.io/x/onecloud/pkg/apigateway/options" "yunion.io/x/onecloud/pkg/appsrv" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient/auth" modules "yunion.io/x/onecloud/pkg/mcclient/modules/llm" + mcpServerOption "yunion.io/x/onecloud/pkg/mcp-server/options" ) +func mcpServersConfigHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { + serviceName := "mcp-server" + url, err := auth.GetPublicServiceURL(serviceName, options.Options.Region, "", httputils.GET) + if err != nil { + log.Warningf("GetPublicServiceURL for %s failed: %v", serviceName, err) + } + sseURL := fmt.Sprintf("%s/sse", url) + + responseType := r.URL.Query().Get("type") + switch responseType { + case "claude": + cmd := fmt.Sprintf("claude mcp add --transport sse %s --header \"X-API-Key: your-key-here\"", sseURL) + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.Write([]byte(cmd)) + return + case "cursor": + // fall through to JSON + default: + // default: return JSON (cursor format) + } + + config := map[string]interface{}{ + "mcpServers": map[string]interface{}{ + mcpServerOption.Options.MCPServerName: map[string]interface{}{ + "url": sseURL, + "headers": map[string]string{ + "AK": "value", + "SK": "value", + }, + }, + }, + } + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(config) +} + func chatHandlerInfo(method, prefix string, handler func(context.Context, http.ResponseWriter, *http.Request)) *appsrv.SHandlerInfo { log.Debugf("%s - %s", method, prefix) hi := appsrv.SHandlerInfo{} diff --git a/pkg/apigateway/handler/misc.go b/pkg/apigateway/handler/misc.go index 134e9eb6ec..8b82eef5da 100644 --- a/pkg/apigateway/handler/misc.go +++ b/pkg/apigateway/handler/misc.go @@ -114,6 +114,9 @@ func (h *MiscHandler) Bind(app *appsrv.Application) { app.AddHandler(POST, prefix+"syslog/message", handleSyslogWebServiceMessage) // service settings app.AddHandler(GET, prefix+"service_settings", h.getServiceSettings) + + // mcp servers config + app.AddHandler(GET, prefix+"mcp-servers-config", mcpServersConfigHandler) } func UploadHandlerInfo(method, prefix string, handler func(context.Context, http.ResponseWriter, *http.Request)) *appsrv.SHandlerInfo {