From 6dfd8b1e752f78842a521bd4be38868a177b9a23 Mon Sep 17 00:00:00 2001 From: David Howden Date: Fri, 11 Apr 2025 10:14:46 +1000 Subject: [PATCH] add indexName optional argument to all calls --- cmd/mcp/main.go | 12 ++++++------ pkg/mcputil/index.go | 17 +++++++++++++++++ pkg/search/indices/getsettings.go | 8 +++++++- pkg/search/query/runquery.go | 8 ++------ pkg/search/records/getobject.go | 7 ++++++- pkg/search/records/insertobject.go | 9 +++++++-- pkg/search/records/insertsobjects.go | 9 +++++++-- pkg/search/rules/searchrules.go | 7 ++++++- pkg/search/synonyms/searchsynonym.go | 7 ++++++- 9 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 pkg/mcputil/index.go diff --git a/cmd/mcp/main.go b/cmd/mcp/main.go index b995b99..c84aabc 100644 --- a/cmd/mcp/main.go +++ b/cmd/mcp/main.go @@ -55,21 +55,21 @@ func main() { // SEARCH TOOLS // Tools for managing indices - indices.RegisterGetSettings(mcps, index) + indices.RegisterGetSettings(mcps, client, index) // Tools for managing records - records.RegisterGetObject(mcps, index) - records.RegisterInsertObject(mcps, writeIndex) - records.RegisterInsertObjects(mcps, writeIndex) + records.RegisterGetObject(mcps, client, index) + records.RegisterInsertObject(mcps, writeClient, writeIndex) + records.RegisterInsertObjects(mcps, writeClient, writeIndex) // Tools for searching query.RegisterRunQuery(mcps, client, index) // Tools for managing rules - rules.RegisterSearchRules(mcps, index) + rules.RegisterSearchRules(mcps, client, index) // Tools for managing synonyms - synonyms.RegisterSearchSynonym(mcps, index) + synonyms.RegisterSearchSynonym(mcps, client, index) if err := server.ServeStdio(mcps); err != nil { fmt.Printf("Server error: %v\n", err) diff --git a/pkg/mcputil/index.go b/pkg/mcputil/index.go new file mode 100644 index 0000000..1031106 --- /dev/null +++ b/pkg/mcputil/index.go @@ -0,0 +1,17 @@ +package mcputil + +import ( + "github.com/mark3labs/mcp-go/mcp" + + "github.com/algolia/algoliasearch-client-go/v3/algolia/search" +) + +// Index is a convenience method that returns the index specified in the request, +// or defaultIndex if no index is specified. +func Index(client *search.Client, defaultIndex *search.Index, req mcp.CallToolRequest) *search.Index { + indexName, ok := req.Params.Arguments["indexName"].(string) + if !ok { + return defaultIndex + } + return client.InitIndex(indexName) +} diff --git a/pkg/search/indices/getsettings.go b/pkg/search/indices/getsettings.go index f43dac0..6c4766e 100644 --- a/pkg/search/indices/getsettings.go +++ b/pkg/search/indices/getsettings.go @@ -10,13 +10,19 @@ import ( "github.com/algolia/mcp/pkg/mcputil" ) -func RegisterGetSettings(mcps *server.MCPServer, index *search.Index) { +func RegisterGetSettings(mcps *server.MCPServer, client *search.Client, index *search.Index) { getSettingsTool := mcp.NewTool( "get_settings", mcp.WithDescription("Get the settings for the Algolia index"), + mcp.WithString( + "indexName", + mcp.Description("The index to retrieve settings from"), + ), ) mcps.AddTool(getSettingsTool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { + index = mcputil.Index(client, index, req) + settings, err := index.GetSettings() if err != nil { return nil, err diff --git a/pkg/search/query/runquery.go b/pkg/search/query/runquery.go index b8383e3..b5f33ad 100644 --- a/pkg/search/query/runquery.go +++ b/pkg/search/query/runquery.go @@ -34,7 +34,6 @@ func RegisterRunQuery(mcps *server.MCPServer, client *search.Client, index *sear ) mcps.AddTool(runQueryTool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { - indexName, _ := req.Params.Arguments["indexName"].(string) query, _ := req.Params.Arguments["query"].(string) opts := []any{} @@ -42,13 +41,10 @@ func RegisterRunQuery(mcps *server.MCPServer, client *search.Client, index *sear opts = append(opts, opt.HitsPerPage(int(hitsPerPage))) } - currentIndex := index - if indexName != "" { - currentIndex = client.InitIndex(indexName) - } + index = mcputil.Index(client, index, req) start := time.Now() - resp, err := currentIndex.Search(query, opts...) + resp, err := index.Search(query, opts...) if err != nil { return nil, fmt.Errorf("could not search: %w", err) } diff --git a/pkg/search/records/getobject.go b/pkg/search/records/getobject.go index 382a363..43677dc 100644 --- a/pkg/search/records/getobject.go +++ b/pkg/search/records/getobject.go @@ -11,7 +11,7 @@ import ( "github.com/algolia/mcp/pkg/mcputil" ) -func RegisterGetObject(mcps *server.MCPServer, index *search.Index) { +func RegisterGetObject(mcps *server.MCPServer, client *search.Client, index *search.Index) { getObjectTool := mcp.NewTool( "get_object", mcp.WithDescription("Get an object by its object ID"), @@ -20,10 +20,15 @@ func RegisterGetObject(mcps *server.MCPServer, index *search.Index) { mcp.Description("The object ID to look up"), mcp.Required(), ), + mcp.WithString( + "indexName", + mcp.Description("The index to get the object from"), + ), ) mcps.AddTool(getObjectTool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { objectID, _ := req.Params.Arguments["objectID"].(string) + index = mcputil.Index(client, index, req) var x map[string]any if err := index.GetObject(objectID, &x); err != nil { diff --git a/pkg/search/records/insertobject.go b/pkg/search/records/insertobject.go index f235807..0a2da68 100644 --- a/pkg/search/records/insertobject.go +++ b/pkg/search/records/insertobject.go @@ -12,7 +12,7 @@ import ( "github.com/algolia/mcp/pkg/mcputil" ) -func RegisterInsertObject(mcps *server.MCPServer, writeIndex *search.Index) { +func RegisterInsertObject(mcps *server.MCPServer, writeClient *search.Client, writeIndex *search.Index) { insertObjectTool := mcp.NewTool( "insert_object", mcp.WithDescription("Insert or update an object in the Algolia index"), @@ -21,12 +21,17 @@ func RegisterInsertObject(mcps *server.MCPServer, writeIndex *search.Index) { mcp.Description("The object to insert or update as a JSON string (must include an objectID field)"), mcp.Required(), ), + mcp.WithString( + "indexName", + mcp.Description("The index to insert objects into"), + ), ) mcps.AddTool(insertObjectTool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { - if writeIndex == nil { + if writeIndex == nil || writeClient == nil { return mcp.NewToolResultError("write API key not set, cannot insert objects"), nil } + writeIndex = mcputil.Index(writeClient, writeIndex, req) objStr, ok := req.Params.Arguments["object"].(string) if !ok { diff --git a/pkg/search/records/insertsobjects.go b/pkg/search/records/insertsobjects.go index f874fb1..bddc6c7 100644 --- a/pkg/search/records/insertsobjects.go +++ b/pkg/search/records/insertsobjects.go @@ -12,7 +12,7 @@ import ( "github.com/algolia/mcp/pkg/mcputil" ) -func RegisterInsertObjects(mcps *server.MCPServer, writeIndex *search.Index) { +func RegisterInsertObjects(mcps *server.MCPServer, writeClient *search.Client, writeIndex *search.Index) { insertObjectsTool := mcp.NewTool( "insert_objects", mcp.WithDescription("Insert or update multiple objects in the Algolia index"), @@ -21,12 +21,17 @@ func RegisterInsertObjects(mcps *server.MCPServer, writeIndex *search.Index) { mcp.Description("Array of objects to insert or update as a JSON string (each must include an objectID field)"), mcp.Required(), ), + mcp.WithString( + "indexName", + mcp.Description("The index to insert objects into"), + ), ) mcps.AddTool(insertObjectsTool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { - if writeIndex == nil { + if writeIndex == nil || writeClient == nil { return mcp.NewToolResultError("write API key not set, cannot insert objects"), nil } + writeIndex = mcputil.Index(writeClient, writeIndex, req) objsStr, ok := req.Params.Arguments["objects"].(string) if !ok { diff --git a/pkg/search/rules/searchrules.go b/pkg/search/rules/searchrules.go index a8123da..684c502 100644 --- a/pkg/search/rules/searchrules.go +++ b/pkg/search/rules/searchrules.go @@ -12,7 +12,7 @@ import ( "github.com/algolia/mcp/pkg/mcputil" ) -func RegisterSearchRules(mcps *server.MCPServer, index *search.Index) { +func RegisterSearchRules(mcps *server.MCPServer, client *search.Client, index *search.Index) { searchRulesTool := mcp.NewTool( "search_rules", mcp.WithDescription("Search for rules in the Algolia index"), @@ -34,10 +34,15 @@ func RegisterSearchRules(mcps *server.MCPServer, index *search.Index) { "enabled", mcp.Description("When specified, restricts matches to rules with a specific enabled status. When omitted, all enabled statuses may match."), ), + mcp.WithString( + "indexName", + mcp.Description("The index to retrieve search rules from"), + ), ) mcps.AddTool(searchRulesTool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { query, _ := req.Params.Arguments["query"].(string) + index = mcputil.Index(client, index, req) opts := []any{} if anchoring, ok := req.Params.Arguments["anchoring"].(string); ok { diff --git a/pkg/search/synonyms/searchsynonym.go b/pkg/search/synonyms/searchsynonym.go index 5376f90..aeb80df 100644 --- a/pkg/search/synonyms/searchsynonym.go +++ b/pkg/search/synonyms/searchsynonym.go @@ -11,7 +11,7 @@ import ( "github.com/algolia/mcp/pkg/mcputil" ) -func RegisterSearchSynonym(mcps *server.MCPServer, index *search.Index) { +func RegisterSearchSynonym(mcps *server.MCPServer, client *search.Client, index *search.Index) { searchSynonymTool := mcp.NewTool( "search_synonyms", mcp.WithDescription("Search for synonyms in the Algolia index that match a query"), @@ -19,10 +19,15 @@ func RegisterSearchSynonym(mcps *server.MCPServer, index *search.Index) { "query", mcp.Description("The query to find synonyms for"), ), + mcp.WithString( + "indexName", + mcp.Description("The index to find synonms in"), + ), ) mcps.AddTool(searchSynonymTool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { query, _ := req.Params.Arguments["query"].(string) + index = mcputil.Index(client, index, req) resp, err := index.SearchSynonyms(query) if err != nil {