list-views.ts•3.58 kB
/**
* MCP Tool: tableau_list_views
*
* Purpose: List all views (worksheets/dashboards) in a specific workbook
* Parameters:
* - workbookId (required): Workbook identifier
* Returns: Array of views with id, name, contentUrl, workbookId, viewUrlName
*/
import { z } from 'zod';
import { TableauClient } from '../tableau-client.js';
// Zod schema for input validation
export const ListViewsArgsSchema = z.object({
workbookId: z.string().min(1).describe('Workbook identifier (required)'),
});
export type ListViewsArgs = z.infer<typeof ListViewsArgsSchema>;
/**
* Handler for tableau_list_views tool
*
* Lists all views (worksheets and dashboards) within a specific workbook.
*
* @param args - Tool arguments (workbookId)
* @param tableauClient - Initialized Tableau client instance
* @returns Formatted response with view list or error
*/
export async function listViewsHandler(
args: ListViewsArgs,
tableauClient: TableauClient
): Promise<{ content: Array<{ type: string; text: string }> }> {
try {
// Validate workbookId is provided
if (!args.workbookId || args.workbookId.trim() === '') {
return {
content: [{
type: 'text',
text: 'Error: workbookId is required and cannot be empty.'
}]
};
}
// Call TableauClient to get views
const views = await tableauClient.listViews(args.workbookId);
// Format response for LLM consumption
if (views.length === 0) {
return {
content: [{
type: 'text',
text: `No views found in workbook ${args.workbookId}. The workbook may be empty or the ID may be incorrect.`
}]
};
}
// Create a summary with count and details
const summary = `Found ${views.length} view(s) in workbook ${args.workbookId}:\n\n` +
views.map((view, index) => {
return `${index + 1}. ${view.name}\n` +
` View ID: ${view.id}\n` +
` Content URL: ${view.contentUrl}\n` +
` View URL Name: ${view.viewUrlName}\n` +
` Workbook ID: ${view.workbookId}`;
}).join('\n\n');
// Also include raw JSON data for structured access
const jsonData = JSON.stringify(views, null, 2);
return {
content: [
{
type: 'text',
text: summary
},
{
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: Workbook with ID '${args.workbookId}' not found. Please verify the workbook ID is correct and you have access to it.`
}]
};
}
return {
content: [{
type: 'text',
text: `Error listing views: ${errorMessage}`
}]
};
}
}
// Tool metadata for MCP server registration
export const listViewsTool = {
name: 'tableau_list_views',
description: 'List all views (worksheets and dashboards) within a specific workbook. Returns view metadata including ID, name, content URL, view URL name, and parent workbook ID. Use this after listing workbooks to discover available views.',
inputSchema: {
type: 'object',
properties: {
workbookId: {
type: 'string',
description: 'Required: Workbook identifier to list views from'
}
},
required: ['workbookId']
}
};