Skip to content
Open
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
12 changes: 6 additions & 6 deletions cmd/mcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions pkg/mcputil/index.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 7 additions & 1 deletion pkg/search/indices/getsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions pkg/search/query/runquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,17 @@ 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{}
if hitsPerPage, ok := req.Params.Arguments["hitsPerPage"].(float64); ok {
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)
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/search/records/getobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions pkg/search/records/insertobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions pkg/search/records/insertsobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion pkg/search/rules/searchrules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion pkg/search/synonyms/searchsynonym.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ 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"),
mcp.WithString(
"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 {
Expand Down