import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import {
createErrorResponse,
debugLog,
getSearchcraftConfig,
makeSearchcraftRequest,
} from "../../../helpers.js";
export const registerGetAllIndexStats = (server: McpServer) => {
/**
* Tool: get_all_index_stats
* GET /index/stats - Returns document counts for every index
*/
server.tool(
"get_all_index_stats",
"Get document counts and statistics for all indexes.",
{},
async () => {
debugLog("[Tool Call] get_all_index_stats");
try {
const config = getSearchcraftConfig();
if (config.error) {
return config.error;
}
const { endpointUrl, apiKey } = config;
const endpoint = `${endpointUrl.replace(/\/$/, "")}/index/stats`;
const response = await makeSearchcraftRequest(
endpoint,
"GET",
apiKey,
);
return {
content: [
{
type: "resource",
resource: {
uri: `searchcraft://index-stats/${Date.now()}`,
mimeType: "application/json",
text: JSON.stringify(response, null, 2),
},
},
],
};
} catch (error) {
const errorMessage =
error instanceof Error
? error.message
: "Unknown error occurred";
return createErrorResponse(
`Failed to get index stats: ${errorMessage}`,
);
}
},
);
};