Skip to content
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kontent-ai/mcp-server",
"version": "0.19.0",
"version": "0.20.0",
"type": "module",
"scripts": {
"build": "rimraf build && tsc",
Expand Down
47 changes: 20 additions & 27 deletions src/tools/search-variants-mapi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import pRetry from "p-retry";
import pRetry, { AbortError } from "p-retry";
import { createMapiClient } from "../clients/kontentClients.js";
import { searchOperationSchema } from "../schemas/searchOperationSchemas.js";
import { handleMcpToolError } from "../utils/errorHandler.js";
Expand All @@ -10,11 +10,6 @@ interface AiOperationResponse {
operationId: string;
}

interface AiOperationResultResponse {
message: string;
result?: unknown;
}

export const registerTool = (server: McpServer): void => {
server.tool(
"search-variants-mapi",
Expand Down Expand Up @@ -91,38 +86,36 @@ export const registerTool = (server: McpServer): void => {
// Step 2: Poll for results with exponential backoff
const resultData = await pRetry(
async () => {
const pollResponse = await client
.get()
.withAction(
`projects/${environmentId}/early-access/ai-operation/${operationId}`,
)
.toPromise();
try {
const pollResponse = await client
.get()
.withAction(
`projects/${environmentId}/early-access/ai-operation-result/${operationId}`,
)
.toPromise();

const data: AiOperationResultResponse = pollResponse.data;

if (data.message.includes("still in progress")) {
throw new Error("Operation still in progress");
return pollResponse.data;
} catch (error: any) {
if (error?.response?.status === 404) {
throw new Error(
"Operation result not available yet. Retrying.",
);
}
throw new AbortError(error);
}

return data;
},
{
// Worst-case retry time: ~1 minute
retries: 10,
minTimeout: 1000,
maxTimeout: 10000,
factor: 1.5,
},
);

if (resultData.message.includes("completed successfully")) {
return createMcpToolSuccessResponse({
result: resultData.result,
});
}

throw new Error(
`Search operation error: ${resultData.message}. Operation ID: ${operationId}`,
);
return createMcpToolSuccessResponse({
result: resultData,
});
} catch (error: unknown) {
return handleMcpToolError(error, "AI-powered Variant Search");
}
Expand Down