Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion internal/mcp/mcp_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@
"github.com/sourcegraph/sourcegraph/lib/errors"
)

const MCPURLPath = ".api/mcp/v1"
const (
MCPURLPath = ".api/mcp"
MCPURLPathLegacy = ".api/mcp/v1"
)

func resolveMCPEndpoint(ctx context.Context, client api.Client) (string, error) {
for _, endpoint := range []string{MCPURLPath, MCPURLPathLegacy} {
_, err := doJSONRPC(ctx, client, endpoint, "tools/list", nil)

Check failure on line 23 in internal/mcp/mcp_request.go

View workflow job for this annotation

GitHub Actions / go-lint

response body must be closed (bodyclose)
if err == nil {
return endpoint, nil
}
}
return "", errors.Newf("MCP endpoint not available: tried %s and %s", MCPURLPath, MCPURLPathLegacy)
}

func fetchToolDefinitions(ctx context.Context, client api.Client, endpoint string) (map[string]*ToolDef, error) {
resp, err := doJSONRPC(ctx, client, endpoint, "tools/list", nil)
Expand Down
16 changes: 12 additions & 4 deletions internal/mcp/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

// ToolRegistry keeps track of tools and the endpoints they originated from
type ToolRegistry struct {
tools map[string]*ToolDef
tools map[string]*ToolDef
endpoint string
}

func NewToolRegistry() *ToolRegistry {
Expand All @@ -21,9 +22,16 @@ func NewToolRegistry() *ToolRegistry {
}
}

// LoadTools loads the tool definitions from the Mcp tool endpoints constants McpURLPath
// LoadTools loads the tool definitions from the MCP endpoint.
// It tries the new endpoint first (.api/mcp), then falls back to legacy (.api/mcp/v1).
func (r *ToolRegistry) LoadTools(ctx context.Context, client api.Client) error {
tools, err := fetchToolDefinitions(ctx, client, MCPURLPath)
endpoint, err := resolveMCPEndpoint(ctx, client)
if err != nil {
return err
}
r.endpoint = endpoint

tools, err := fetchToolDefinitions(ctx, client, endpoint)
if err != nil {
return err
}
Expand All @@ -40,7 +48,7 @@ func (r *ToolRegistry) Get(name string) (*ToolDef, bool) {
// CallTool calls the given tool with the given arguments. It constructs the Tool request and decodes the Tool response
func (r *ToolRegistry) CallTool(ctx context.Context, client api.Client, name string, args map[string]any) (map[string]json.RawMessage, error) {
tool := r.tools[name]
resp, err := doToolCall(ctx, client, MCPURLPath, tool.RawName, args)
resp, err := doToolCall(ctx, client, r.endpoint, tool.RawName, args)
if err != nil {
return nil, err
}
Expand Down
Loading