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: 4 additions & 8 deletions cmd/mcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/algolia/mcp/pkg/recommend"
searchpkg "github.com/algolia/mcp/pkg/search"
"github.com/algolia/mcp/pkg/usage"
"github.com/algolia/mcp/pkg/mcputil/client"

"github.com/mark3labs/mcp-go/server"
)
Expand Down Expand Up @@ -52,17 +53,12 @@ func main() {
}
}


// Initialize Algolia client
var searchClient *search.Client
var searchIndex *search.Index

// Get Algolia credentials from environment variables
appID := os.Getenv("ALGOLIA_APP_ID")
apiKey := os.Getenv("ALGOLIA_API_KEY")
indexName := os.Getenv("ALGOLIA_INDEX_NAME")

searchClient = search.NewClient(appID, apiKey)
searchIndex = searchClient.InitIndex(indexName)
searchClient, searchIndex := client.clientInstance()

// Register tools from enabled packages.
if enabled["abtesting"] {
Expand All @@ -84,7 +80,7 @@ func main() {
recommend.RegisterAll(mcps)
}
if enabled["search"] {
searchpkg.RegisterAll(mcps)
searchpkg.RegisterAll(mcps, searchClient, searchIndex)
} else {
// Only register specific search tools if "search" is not enabled
if enabled["search_read"] {
Expand Down
22 changes: 22 additions & 0 deletions pkg/mcputil/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mcputil

import (
"os"
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
)

// JSONToolResult is a convenience method that creates a named JSON-encoded MCP tool result
// from a Go value.
func clientInstance() (*search.Client, *search.Index) {
var searchClient *search.Client
var searchIndex *search.Index

appID := os.Getenv("ALGOLIA_APP_ID")
apiKey := os.Getenv("ALGOLIA_API_KEY")
indexName := os.Getenv("ALGOLIA_INDEX_NAME")

client = search.NewClient(appID, apiKey)
index = client.InitIndex(indexName)

return client, index
}
13 changes: 7 additions & 6 deletions pkg/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
"github.com/algolia/mcp/pkg/search/query"
"github.com/algolia/mcp/pkg/search/records"
"github.com/mark3labs/mcp-go/server"
"github.com/algolia/mcp/pkg/mcputil/client"
)

// RegisterAll registers all Search tools with the MCP server (both read and write).
func RegisterAll(mcps *server.MCPServer) {
// Initialize Algolia client.
// Note: In a real implementation, you would get the app ID and API key from environment variables.
client := search.NewClient("", "")
index := client.InitIndex("default_index")
var searchClient *search.Client
var searchIndex *search.Index

searchClient, searchIndex := client.clientInstance()

// Register both read and write operations.
RegisterReadAll(mcps, client, index)
RegisterWriteAll(mcps, client, index)
RegisterReadAll(mcps, searchClient, searchIndex)
RegisterWriteAll(mcps, searchClient, searchIndex)
}

// RegisterReadAll registers read-only Search tools with the MCP server.
Expand Down