{"record":{"id":"8cc43c9944872ea5","repo":"Tencent/WeKnora","slug":"failed-to-create-sse-client-w","errorCode":null,"errorMessage":"failed to create SSE client: %w","messagePattern":"failed to create SSE client: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/mcp/client.go","lineNumber":203,"sourceCode":"\tvar mcpClient *client.Client\n\tswitch config.Service.TransportType {\n\tcase types.MCPTransportSSE:\n\t\tif config.Service.URL == nil || *config.Service.URL == \"\" {\n\t\t\treturn nil, fmt.Errorf(\"URL is required for SSE transport\")\n\t\t}\n\t\tif useOAuth {\n\t\t\tmcpClient, err = client.NewOAuthSSEClient(*config.Service.URL, oauthConfig,\n\t\t\t\ttransport.WithHTTPClient(httpClient),\n\t\t\t\ttransport.WithHeaders(headers),\n\t\t\t)\n\t\t} else {\n\t\t\tmcpClient, err = client.NewSSEMCPClient(*config.Service.URL,\n\t\t\t\tclient.WithHTTPClient(httpClient),\n\t\t\t\tclient.WithHeaders(headers),\n\t\t\t)\n\t\t}\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to create SSE client: %w\", err)\n\t\t}\n\tcase types.MCPTransportHTTPStreamable:\n\t\tif config.Service.URL == nil || *config.Service.URL == \"\" {\n\t\t\treturn nil, fmt.Errorf(\"URL is required for HTTP Streamable transport\")\n\t\t}\n\t\tif useOAuth {\n\t\t\tmcpClient, err = client.NewOAuthStreamableHttpClient(*config.Service.URL, oauthConfig,\n\t\t\t\ttransport.WithHTTPBasicClient(httpClient),\n\t\t\t\ttransport.WithHTTPHeaders(headers),\n\t\t\t)\n\t\t} else {\n\t\t\t// For HTTP streamable, we need to use transport options\n\t\t\tmcpClient, err = client.NewStreamableHttpClient(*config.Service.URL,\n\t\t\t\ttransport.WithHTTPBasicClient(httpClient),\n\t\t\t\ttransport.WithHTTPHeaders(headers),\n\t\t\t)\n\t\t}\n\t\tif err != nil {","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/mcp/client.go#L185-L221","documentation":"NewMCPClient wraps any error returned by mark3labs NewSSEMCPClient or NewOAuthSSEClient with 'failed to create SSE client'. The constructor itself rarely fails — it mainly fails on URL parsing/invalid URL schemes, since the SSRF-safe HTTP client and headers are built beforehand. The underlying cause is always in the wrapped error.","triggerScenarios":"NewMCPClient called with config.Service.TransportType == types.MCPTransportSSE and a service URL that the mark3labs client package rejects when parsing (e.g. malformed URL, missing scheme, unsupported scheme). Also fires when NewOAuthSSEClient rejects the URL or OAuth config.","commonSituations":"Admin saved an MCP service URL like 'example.com/sse' without https://; URL contains spaces or invalid characters; stored URL corrupted by migration; OAuth-registered service passing an invalid redirect/base URL into the OAuth SSE client.","solutions":["Inspect the wrapped %w cause printed after 'failed to create SSE client:' — it names the actual failure","Verify config.Service.URL parses with url.Parse and has an http/https scheme before calling NewMCPClient","Fix the stored service URL (must be absolute, e.g. https://host/sse)","If OAuth, confirm the OAuthConfig (client id/secret, token store) is well-formed before constructing"],"exampleFix":"// before\nmcpClient, err = client.NewSSEMCPClient(\"example.com/sse\", ...)\n// after\nu, perr := url.Parse(cfg.URL)\nif perr != nil || (u.Scheme != \"http\" && u.Scheme != \"https\") {\n    return nil, fmt.Errorf(\"invalid SSE service URL %q\", cfg.URL)\n}\nmcpClient, err = client.NewSSEMCPClient(cfg.URL, ...)","handlingStrategy":"validation","validationCode":"func validSSEURL(raw *string) bool {\n    if raw == nil || *raw == \"\" { return false }\n    u, err := url.Parse(*raw)\n    return err == nil && (u.Scheme == \"http\" || u.Scheme == \"https\") && u.Host != \"\"\n}\n// call before NewMCPClient when TransportType == MCPTransportSSE","typeGuard":"func asCreateSSEClientErr(err error) (urlParseErr error, ok bool) {\n    if err == nil || !strings.Contains(err.Error(), \"failed to create SSE client\") { return nil, false }\n    return errors.Unwrap(err), true\n}","tryCatchPattern":"mcpClient, err := NewMCPClient(cfg)\nif err != nil {\n    var cause error\n    if strings.Contains(err.Error(), \"failed to create SSE client\") { cause = errors.Unwrap(err) }\n    return fmt.Errorf(\"SSE client config invalid: %v\", cause)\n}","preventionTips":["Validate service URLs (absolute, http/https) at service create/update time","Normalize/trim URLs before persisting","Add a startup check that all configured SSE service URLs parse"],"tags":["go","mcp","sse","url-parsing"],"backgroundTag":"invalid-url","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}