refresh-extract.ts•4.69 kB
/**
* MCP Tool: tableau_refresh_extract
*
* Purpose: Trigger a data source extract refresh (full or incremental)
* Parameters:
* - datasourceId (required): Data source identifier
* - refreshType (optional): 'full' or 'incremental' (default: 'full')
* Returns: Job tracking information
*/
import { z } from 'zod';
import { TableauClient } from '../tableau-client.js';
// Zod schema for input validation
export const RefreshExtractArgsSchema = z.object({
datasourceId: z.string().min(1).describe('Data source identifier (required)'),
refreshType: z.enum(['full', 'incremental']).optional().default('full').describe('Type of refresh: full or incremental (default: full)'),
});
export type RefreshExtractArgs = z.infer<typeof RefreshExtractArgsSchema>;
/**
* Handler for tableau_refresh_extract tool
*
* Triggers a data source extract refresh. This is an asynchronous operation
* that returns immediately with a job ID. The actual refresh happens in the background.
*
* @param args - Tool arguments (datasourceId, refreshType)
* @param tableauClient - Initialized Tableau client instance
* @returns Formatted response with job information or error
*/
export async function refreshExtractHandler(
args: RefreshExtractArgs,
tableauClient: TableauClient
): Promise<{ content: Array<{ type: string; text: string }> }> {
try {
// Validate datasourceId is provided
if (!args.datasourceId || args.datasourceId.trim() === '') {
return {
content: [{
type: 'text',
text: 'Error: datasourceId is required and cannot be empty.'
}]
};
}
const refreshType = args.refreshType || 'full';
// Call TableauClient to trigger refresh
const job = await tableauClient.refreshExtract(
args.datasourceId,
refreshType
);
// Format response with job tracking information
const responseText = `Extract refresh initiated successfully!\n\n` +
`Refresh Type: ${refreshType}\n` +
`Data Source ID: ${args.datasourceId}\n` +
`Job ID: ${job.id}\n` +
`Job Type: ${job.type}\n` +
`Status: ${job.status}\n` +
`Created At: ${new Date(job.createdAt).toLocaleString()}\n\n` +
`Note: This is an asynchronous operation. The refresh is running in the background.\n` +
`You can use the job ID to check the status of the refresh operation.`;
// Also include raw JSON data
const jsonData = JSON.stringify(job, null, 2);
return {
content: [
{
type: 'text',
text: responseText
},
{
type: 'text',
text: `\n\nRaw JSON data:\n${jsonData}`
}
]
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
// Provide helpful error messages
if (errorMessage.includes('404') || errorMessage.includes('not found')) {
return {
content: [{
type: 'text',
text: `Error: Data source with ID '${args.datasourceId}' not found. Please verify the data source ID is correct and you have access to it.`
}]
};
}
if (errorMessage.includes('permission') || errorMessage.includes('forbidden')) {
return {
content: [{
type: 'text',
text: `Error: Insufficient permissions to refresh data source '${args.datasourceId}'. You may not have the required permissions to trigger extract refreshes.`
}]
};
}
if (errorMessage.includes('extract')) {
return {
content: [{
type: 'text',
text: `Error: The data source may not support extract refresh. Only data sources with extracts can be refreshed. Error: ${errorMessage}`
}]
};
}
return {
content: [{
type: 'text',
text: `Error triggering extract refresh: ${errorMessage}`
}]
};
}
}
// Tool metadata for MCP server registration
export const refreshExtractTool = {
name: 'tableau_refresh_extract',
description: 'Trigger a data source extract refresh. Supports both full and incremental refresh types. This is an asynchronous operation that returns a job ID for tracking. Use this to update extract data sources with the latest data from the underlying connection.',
inputSchema: {
type: 'object',
properties: {
datasourceId: {
type: 'string',
description: 'Required: Data source identifier to refresh'
},
refreshType: {
type: 'string',
enum: ['full', 'incremental'],
description: 'Optional: Type of refresh (full or incremental). Default: full'
}
},
required: ['datasourceId']
}
};