UNPKG

uk_ons_mcp_server

Version:

MCP server for UK Office for National Statistics (ONS) API - providing access to UK government statistics

189 lines 6.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ONSDatasetService = void 0; class ONSDatasetService { constructor(apiClient) { this.apiClient = apiClient; } /** * List datasets with pagination */ async listDatasets(options) { return this.apiClient.listDatasets(options.limit, options.offset); } /** * Get a specific dataset by ID */ async getDataset(datasetId) { return this.apiClient.getDataset(datasetId); } /** * Search datasets by query */ async searchDatasets(query, limit = 10) { return this.apiClient.searchDatasets(query, limit); } /** * Get observations with dimension filters */ async getObservations(datasetId, edition = 'time-series', version = 'latest', dimensions) { return this.apiClient.getObservations(datasetId, edition, version, dimensions); } /** * Get the latest available data for a dataset with common filters */ async getLatestData(datasetId, geography, timePeriod) { try { // Get dataset info to understand available dimensions const dataset = await this.getDataset(datasetId); // Build dimension filters const dimensions = {}; // Add geography filter if provided if (geography) { dimensions.geography = geography; } // Add time filter if provided if (timePeriod) { dimensions.time = timePeriod; } // If no specific dimensions provided, try to get recent data if (Object.keys(dimensions).length === 0) { // For time-series data, try to get latest time period if (dataset.dimensions?.some(d => d.id === 'time')) { dimensions.time = '*'; // Use wildcard to get all time periods } // Default to UK-wide data if geography dimension exists if (dataset.dimensions?.some(d => d.id === 'geography')) { dimensions.geography = 'K02000001'; // UK country code } } // Get observations const observations = await this.getObservations(datasetId, 'time-series', 'latest', dimensions); // Process and format the response return { dataset_id: datasetId, dataset_title: dataset.title, dataset_description: dataset.description, filters_applied: { geography, time_period: timePeriod, }, dimensions_used: dimensions, total_observations: observations.total_observations, observations: observations.observations, metadata: { dimensions: observations.dimensions, links: observations.links, }, }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Failed to get latest data for ${datasetId}: ${errorMessage}`); } } /** * Get popular datasets with metadata */ async getPopularDatasets() { const popularIds = this.apiClient.getPopularDatasets(); const datasets = []; for (const id of popularIds.slice(0, 10)) { // Limit to first 10 to avoid too many requests try { const dataset = await this.getDataset(id); datasets.push(dataset); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); console.warn(`Failed to fetch popular dataset ${id}:`, errorMessage); } } return datasets; } /** * Get dataset dimensions and their options */ async getDatasetDimensions(datasetId) { const dataset = await this.getDataset(datasetId); const dimensions = dataset.dimensions || []; const dimensionDetails = await Promise.all(dimensions.map(async (dim) => { try { const options = await this.apiClient.getDimensionOptions(datasetId, dim.id); return { id: dim.id, name: dim.name, label: dim.label, options: options.items || [], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { id: dim.id, name: dim.name, label: dim.label, options: [], error: errorMessage, }; } })); return { dataset_id: datasetId, dataset_title: dataset.title, dimensions: dimensionDetails, }; } /** * Get time series data for a specific geography */ async getTimeSeriesData(datasetId, geography = 'K02000001' // Default to UK ) { const dimensions = { geography, time: '*', // Get all time periods }; const observations = await this.getObservations(datasetId, 'time-series', 'latest', dimensions); // Sort observations by time if time dimension exists const sortedObservations = observations.observations.sort((a, b) => { const timeA = a.dimensions.time || ''; const timeB = b.dimensions.time || ''; return timeA.localeCompare(timeB); }); return { dataset_id: datasetId, geography, time_series: sortedObservations, total_observations: observations.total_observations, }; } /** * Get regional comparison data */ async getRegionalData(datasetId, timePeriod) { const dimensions = { geography: '*', // Get all geographies }; if (timePeriod) { dimensions.time = timePeriod; } const observations = await this.getObservations(datasetId, 'time-series', 'latest', dimensions); return { dataset_id: datasetId, time_period: timePeriod, regional_data: observations.observations, total_observations: observations.total_observations, }; } /** * Health check for the service */ async healthCheck() { const apiAccessible = await this.apiClient.healthCheck(); return { status: apiAccessible ? 'healthy' : 'unhealthy', api_accessible: apiAccessible, }; } } exports.ONSDatasetService = ONSDatasetService; //# sourceMappingURL=dataset-service.js.map