Skip to content

Commit 5da613f

Browse files
committed
try primary and legacy mcp urls before returning an error
- latest url `.api/mcp` - legacy url `.api/mcp/v1` If neither work, we just return an error that the mcp endpoint is not available
1 parent 7393a82 commit 5da613f

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

internal/mcp/mcp_request.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,20 @@ import (
1313
"github.com/sourcegraph/sourcegraph/lib/errors"
1414
)
1515

16-
const MCPURLPath = ".api/mcp/v1"
16+
const (
17+
MCPURLPath = ".api/mcp"
18+
MCPURLPathLegacy = ".api/mcp/v1"
19+
)
20+
21+
func resolveMCPEndpoint(ctx context.Context, client api.Client) (string, error) {
22+
for _, endpoint := range []string{MCPURLPath, MCPURLPathLegacy} {
23+
_, err := doJSONRPC(ctx, client, endpoint, "tools/list", nil)
24+
if err == nil {
25+
return endpoint, nil
26+
}
27+
}
28+
return "", errors.Newf("MCP endpoint not available: tried %s and %s", MCPURLPath, MCPURLPathLegacy)
29+
}
1730

1831
func fetchToolDefinitions(ctx context.Context, client api.Client, endpoint string) (map[string]*ToolDef, error) {
1932
resp, err := doJSONRPC(ctx, client, endpoint, "tools/list", nil)

internal/mcp/registry.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212

1313
// ToolRegistry keeps track of tools and the endpoints they originated from
1414
type ToolRegistry struct {
15-
tools map[string]*ToolDef
15+
tools map[string]*ToolDef
16+
endpoint string
1617
}
1718

1819
func NewToolRegistry() *ToolRegistry {
@@ -21,9 +22,16 @@ func NewToolRegistry() *ToolRegistry {
2122
}
2223
}
2324

24-
// LoadTools loads the tool definitions from the Mcp tool endpoints constants McpURLPath
25+
// LoadTools loads the tool definitions from the MCP endpoint.
26+
// It tries the new endpoint first (.api/mcp), then falls back to legacy (.api/mcp/v1).
2527
func (r *ToolRegistry) LoadTools(ctx context.Context, client api.Client) error {
26-
tools, err := fetchToolDefinitions(ctx, client, MCPURLPath)
28+
endpoint, err := resolveMCPEndpoint(ctx, client)
29+
if err != nil {
30+
return err
31+
}
32+
r.endpoint = endpoint
33+
34+
tools, err := fetchToolDefinitions(ctx, client, endpoint)
2735
if err != nil {
2836
return err
2937
}
@@ -40,7 +48,7 @@ func (r *ToolRegistry) Get(name string) (*ToolDef, bool) {
4048
// CallTool calls the given tool with the given arguments. It constructs the Tool request and decodes the Tool response
4149
func (r *ToolRegistry) CallTool(ctx context.Context, client api.Client, name string, args map[string]any) (map[string]json.RawMessage, error) {
4250
tool := r.tools[name]
43-
resp, err := doToolCall(ctx, client, MCPURLPath, tool.RawName, args)
51+
resp, err := doToolCall(ctx, client, r.endpoint, tool.RawName, args)
4452
if err != nil {
4553
return nil, err
4654
}

0 commit comments

Comments
 (0)