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 registerUpdateEnrichmentTool(server: McpServer, config?: { exaApiKey?: string }): void {
server.tool(
"update_enrichment",
"Update an enrichment's metadata. You can associate custom key-value pairs with the enrichment.",
{
websetId: z.string().describe("The ID or externalId of the webset"),
enrichmentId: z.string().describe("The ID of the enrichment to update"),
metadata: z.record(z.string(), z.string()).describe("Key-value pairs to associate with this enrichment. Each value must be a string.")
},
async ({ websetId, enrichmentId, metadata }) => {
const requestId = `update_enrichment-${Date.now()}-${Math.random().toString(36).substring(2, 7)}`;
const logger = createRequestLogger(requestId, 'update_enrichment');
logger.start(`Updating enrichment ${enrichmentId} from webset: ${websetId}`);
try {
const axiosInstance = axios.create({
baseURL: API_CONFIG.BASE_URL,
headers: {
'accept': 'application/json',
'content-type': 'application/json',
'x-api-key': config?.exaApiKey || process.env.EXA_API_KEY || ''
},
timeout: 30000
});
const params = { metadata };
logger.log("Sending update enrichment request to API");
const response = await axiosInstance.patch<WebsetEnrichment>(
API_CONFIG.ENDPOINTS.WEBSET_ENRICHMENT_BY_ID(websetId, enrichmentId),
params
);
logger.log(`Updated enrichment: ${response.data.id}`);
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 updating enrichment (${statusCode}): ${errorMessage}`
}],
isError: true,
};
}
return {
content: [{
type: "text" as const,
text: `Error updating enrichment: ${error instanceof Error ? error.message : String(error)}`
}],
isError: true,
};
}
}
);
}