list-workbooks.ts•3.46 kB
/**
* MCP Tool: tableau_list_workbooks
*
* Purpose: List all workbooks accessible to the authenticated user
* Parameters:
* - projectName (optional): Filter by project
* - tags (optional): Filter by tags
* Returns: Array of workbooks with id, name, contentUrl, projectName, createdAt, updatedAt
*/
import { z } from 'zod';
import { TableauClient } from '../tableau-client.js';
// Zod schema for input validation
export const ListWorkbooksArgsSchema = z.object({
projectName: z.string().optional().describe('Filter workbooks by project name'),
tags: z.array(z.string()).optional().describe('Filter workbooks by tags'),
});
export type ListWorkbooksArgs = z.infer<typeof ListWorkbooksArgsSchema>;
/**
* Handler for tableau_list_workbooks tool
*
* Lists all workbooks accessible to the authenticated user with optional filtering
* by project name and/or tags.
*
* @param args - Tool arguments (projectName, tags)
* @param tableauClient - Initialized Tableau client instance
* @returns Formatted response with workbook list or error
*/
export async function listWorkbooksHandler(
args: ListWorkbooksArgs,
tableauClient: TableauClient
): Promise<{ content: Array<{ type: string; text: string }> }> {
try {
// Call TableauClient to get workbooks
const workbooks = await tableauClient.listWorkbooks(
args.projectName,
args.tags
);
// Format response for LLM consumption
if (workbooks.length === 0) {
return {
content: [{
type: 'text',
text: 'No workbooks found matching the specified criteria.'
}]
};
}
// Create a summary with count and details
const summary = `Found ${workbooks.length} workbook(s):\n\n` +
workbooks.map((wb, index) => {
return `${index + 1}. ${wb.name}\n` +
` ID: ${wb.id}\n` +
` Content URL: ${wb.contentUrl}\n` +
` Project: ${wb.projectName || 'Default'}\n` +
` Created: ${new Date(wb.createdAt).toLocaleDateString()}\n` +
` Updated: ${new Date(wb.updatedAt).toLocaleDateString()}` +
(wb.tags && wb.tags.length > 0 ? `\n Tags: ${wb.tags.join(', ')}` : '');
}).join('\n\n');
// Also include raw JSON data for structured access
const jsonData = JSON.stringify(workbooks, 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';
return {
content: [{
type: 'text',
text: `Error listing workbooks: ${errorMessage}`
}]
};
}
}
// Tool metadata for MCP server registration
export const listWorkbooksTool = {
name: 'tableau_list_workbooks',
description: 'List all workbooks accessible to the authenticated user. Supports filtering by project name and tags. Returns workbook metadata including ID, name, content URL, project, creation/update timestamps, and tags.',
inputSchema: {
type: 'object',
properties: {
projectName: {
type: 'string',
description: 'Optional: Filter workbooks by project name'
},
tags: {
type: 'array',
items: { type: 'string' },
description: 'Optional: Filter workbooks by tags (array of tag names)'
}
}
}
};