Skip to content

Commit 91f977d

Browse files
authored
fix mcp (#146)
* fix mcp * Internal url
1 parent f3e5d2f commit 91f977d

5 files changed

Lines changed: 27 additions & 11 deletions

File tree

pkg/hub/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (s *Server) Run(ctx context.Context) error {
222222
}
223223

224224
// Tunnel handlers (kcpConfig is used for SA token verification; nil if kcp not configured)
225-
vws, err := builder.NewVirtualWorkspaces(connManager, kcpConfig, s.opts.StaticAuthTokens, s.opts.HubExternalURL, logger)
225+
vws, err := builder.NewVirtualWorkspaces(connManager, kcpConfig, s.opts.StaticAuthTokens, s.opts.HubExternalURL, s.opts.HubInternalURL, logger)
226226
if err != nil {
227227
return fmt.Errorf("creating virtual workspaces handlers: %w", err)
228228
}

pkg/virtual/builder/mcp_builder.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,14 @@ func (p *virtualWorkspaces) buildMCPHandler(cluster, edgeName string) http.Handl
6969
}
7070

7171
// 2. Build per-request single-edge MCP provider.
72-
// edgeProxyBase is derived from the hub's external URL.
73-
edgeProxyBase := strings.TrimRight(p.hubExternalURL, "/") + "/services/edges-proxy"
72+
// edgeProxyBase uses the internal URL to avoid CDN/proxy loops
73+
// (e.g. Cloudflare loop detection when the MCP handler calls back
74+
// to the edges-proxy on the same hub).
75+
baseURL := p.hubInternalURL
76+
if baseURL == "" {
77+
baseURL = p.hubExternalURL
78+
}
79+
edgeProxyBase := strings.TrimRight(baseURL, "/") + "/services/edges-proxy"
7480

7581
provider := &KedgeEdgeProvider{
7682
cluster: cluster,
@@ -220,7 +226,12 @@ func (p *virtualWorkspaces) buildKubernetesMCPHandler() http.Handler {
220226
}
221227

222228
// 7. Build multi-edge provider.
223-
edgeProxyBase := strings.TrimRight(p.hubExternalURL, "/") + "/services/edges-proxy"
229+
// Use internal URL to avoid CDN/proxy loops (same as single-edge handler).
230+
baseURL := p.hubInternalURL
231+
if baseURL == "" {
232+
baseURL = p.hubExternalURL
233+
}
234+
edgeProxyBase := strings.TrimRight(baseURL, "/") + "/services/edges-proxy"
224235
provider := &MultiEdgeKedgeEdgeProvider{
225236
cluster: cluster,
226237
edgeNames: resolvedEdges,

pkg/virtual/builder/mcp_provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ func (p *KedgeEdgeProvider) GetDerivedKubernetes(_ context.Context, edgeName str
100100
return k8s, nil
101101
}
102102

103-
// GetDefaultTarget returns an empty string — there is no single default edge.
104-
func (p *KedgeEdgeProvider) GetDefaultTarget() string { return "" }
103+
// GetDefaultTarget returns the fixed edge name so that MCP tool calls that
104+
// omit the "cluster" parameter automatically route to the only available edge.
105+
func (p *KedgeEdgeProvider) GetDefaultTarget() string { return p.edgeName }
105106

106107
// GetTargetParameterName returns "cluster" as the MCP target query parameter.
107108
// "cluster" is used rather than "edge" because users think in terms of clusters,

pkg/virtual/builder/mcp_provider_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ func TestGetDerivedKubernetes_correctURL(t *testing.T) {
148148
}
149149
}
150150

151-
// TestGetDefaultTarget asserts that GetDefaultTarget returns an empty string.
151+
// TestGetDefaultTarget asserts that GetDefaultTarget returns the fixed edge name.
152152
func TestGetDefaultTarget(t *testing.T) {
153-
provider := &KedgeEdgeProvider{}
154-
if got := provider.GetDefaultTarget(); got != "" {
155-
t.Errorf("GetDefaultTarget() = %q; want empty string", got)
153+
provider := &KedgeEdgeProvider{edgeName: "my-edge"}
154+
if got := provider.GetDefaultTarget(); got != "my-edge" {
155+
t.Errorf("GetDefaultTarget() = %q; want %q", got, "my-edge")
156156
}
157157
}
158158

pkg/virtual/builder/proxy.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type virtualWorkspaces struct {
4343
kedgeClient *kedgeclient.Client // kedge client for fetching Edge resources
4444
staticTokens map[string]struct{} // static tokens that bypass JWT SA requirement
4545
hubExternalURL string // external URL of the hub (used for kubeconfig generation)
46+
hubInternalURL string // internal URL of the hub (used for MCP→edges-proxy calls to avoid CDN loops)
4647
// authorizeFn performs delegated authentication and authorization against kcp.
4748
// Defaults to the package-level authorize function; injectable for testing.
4849
authorizeFn authorizeFnType
@@ -57,7 +58,9 @@ type VirtualWorkspaceHandlers struct {
5758
// NewVirtualWorkspaces creates a new VirtualWorkspaceHandlers.
5859
// kcpConfig is required for SA token authorization against kcp and for fetching Edge resources/secrets.
5960
// hubExternalURL is the externally reachable URL of the hub, used when building kubeconfigs for agents.
60-
func NewVirtualWorkspaces(cm *connman.ConnectionManager, kcpConfig *rest.Config, staticTokens []string, hubExternalURL string, logger klog.Logger) (*VirtualWorkspaceHandlers, error) {
61+
// hubInternalURL is the internal URL for MCP→edges-proxy calls to avoid CDN/proxy loops.
62+
// If empty, hubExternalURL is used as fallback.
63+
func NewVirtualWorkspaces(cm *connman.ConnectionManager, kcpConfig *rest.Config, staticTokens []string, hubExternalURL, hubInternalURL string, logger klog.Logger) (*VirtualWorkspaceHandlers, error) {
6164
staticTokenSet := make(map[string]struct{}, len(staticTokens))
6265
for _, t := range staticTokens {
6366
staticTokenSet[t] = struct{}{}
@@ -89,6 +92,7 @@ func NewVirtualWorkspaces(cm *connman.ConnectionManager, kcpConfig *rest.Config,
8992
kedgeClient: kedgeClient,
9093
staticTokens: staticTokenSet,
9194
hubExternalURL: hubExternalURL,
95+
hubInternalURL: hubInternalURL,
9296
authorizeFn: authorize,
9397
logger: logger.WithName("virtual-workspaces"),
9498
},

0 commit comments

Comments
 (0)