diff --git a/cmd/mcp/main.go b/cmd/mcp/main.go index 7200589..9500ce3 100644 --- a/cmd/mcp/main.go +++ b/cmd/mcp/main.go @@ -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" ) @@ -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"] { @@ -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"] { diff --git a/pkg/mcputil/client.go b/pkg/mcputil/client.go new file mode 100644 index 0000000..5610039 --- /dev/null +++ b/pkg/mcputil/client.go @@ -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 +} diff --git a/pkg/search/search.go b/pkg/search/search.go index 9649d0a..78b4b78 100644 --- a/pkg/search/search.go +++ b/pkg/search/search.go @@ -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.