import { z } from "zod";
import axios from "axios";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { API_CONFIG } from "./config.js";
import { WebsetEnrichment } from "../types.js";
import { createRequestLogger } from "../utils/logger.js";
export function registerGetEnrichmentTool(server: McpServer, config?: { exaApiKey?: string }): void {
server.tool(
"get_enrichment",
"Get details about a specific enrichment, including its status and progress.",
{
websetId: z.string().describe("The ID or externalId of the webset"),
enrichmentId: z.string().describe("The ID of the enrichment to retrieve")
},
async ({ websetId, enrichmentId }) => {
const requestId = `get_enrichment-${Date.now()}-${Math.random().toString(36).substring(2, 7)}`;
const logger = createRequestLogger(requestId, 'get_enrichment');
logger.start(`Getting enrichment ${enrichmentId} from webset: ${websetId}`);
try {
const axiosInstance = axios.create({
baseURL: API_CONFIG.BASE_URL,
headers: {
'accept': 'application/json',
'x-api-key': config?.exaApiKey || process.env.EXA_API_KEY || ''
},
timeout: 30000
});
logger.log("Sending get enrichment request to API");
const response = await axiosInstance.get<WebsetEnrichment>(
API_CONFIG.ENDPOINTS.WEBSET_ENRICHMENT_BY_ID(websetId, enrichmentId)
);
logger.log(`Retrieved enrichment: ${response.data.id} (status: ${response.data.status})`);
const result = {
content: [{
type: "text" as const,
text: JSON.stringify(response.data, null, 2)
}]
};
logger.complete();
return result;
} catch (error) {
logger.error(error);
if (axios.isAxiosError(error)) {
const statusCode = error.response?.status || 'unknown';
const errorMessage = error.response?.data?.message || error.message;
logger.log(`API error (${statusCode}): ${errorMessage}`);
return {
content: [{
type: "text" as const,
text: `Error getting enrichment (${statusCode}): ${errorMessage}`
}],
isError: true,
};
}
return {
content: [{
type: "text" as const,
text: `Error getting enrichment: ${error instanceof Error ? error.message : String(error)}`
}],
isError: true,
};
}
}
);
}