uk_ons_mcp_server
Version:
MCP server for UK Office for National Statistics (ONS) API - providing access to UK government statistics
145 lines • 5.32 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ONSApiClient = void 0;
const axios_1 = __importDefault(require("axios"));
class ONSApiClient {
constructor() {
this.baseUrl = 'https://api.beta.ons.gov.uk/v1';
this.client = axios_1.default.create({
baseURL: this.baseUrl,
timeout: 10000,
headers: {
'Accept': 'application/json',
'User-Agent': 'uk_ons_mcp_server/1.0.0',
},
});
// Add response interceptor for better error handling
this.client.interceptors.response.use((response) => response, (error) => {
if (error.response) {
const status = error.response.status;
const message = error.response.data?.message || error.message;
switch (status) {
case 404:
throw new Error(`ONS API: Resource not found - ${message}`);
case 400:
throw new Error(`ONS API: Bad request - ${message}`);
case 429:
throw new Error(`ONS API: Rate limit exceeded - ${message}`);
case 500:
throw new Error(`ONS API: Server error - ${message}`);
default:
throw new Error(`ONS API: HTTP ${status} - ${message}`);
}
}
throw new Error(`ONS API: Network error - ${error.message}`);
});
}
/**
* List all available datasets
*/
async listDatasets(limit = 20, offset = 0) {
const response = await this.client.get(`/datasets`, {
params: { limit, offset },
});
return response.data;
}
/**
* Get detailed information about a specific dataset
*/
async getDataset(datasetId) {
const response = await this.client.get(`/datasets/${datasetId}`);
return response.data;
}
/**
* Get the latest version of a dataset
*/
async getLatestVersion(datasetId, edition = 'time-series') {
const response = await this.client.get(`/datasets/${datasetId}/editions/${edition}/versions/latest`);
return response.data;
}
/**
* Get observations for a dataset with specific dimensions
*/
async getObservations(datasetId, edition = 'time-series', version = 'latest', dimensions) {
// Build query string for dimensions
const dimensionParams = Object.entries(dimensions)
.map(([key, value]) => `${key}=${value}`)
.join('&');
const url = `/datasets/${datasetId}/editions/${edition}/versions/${version}/observations?${dimensionParams}`;
const response = await this.client.get(url);
return response.data;
}
/**
* Get CSV download URL for a dataset
*/
async getDownloadUrl(datasetId, edition = 'time-series') {
const latestVersion = await this.getLatestVersion(datasetId, edition);
return latestVersion.downloads?.csv?.href || '';
}
/**
* Search datasets (simple text search across titles and descriptions)
*/
async searchDatasets(query, limit = 10) {
// Note: ONS API doesn't have native search, so we'll get all datasets and filter
const allDatasets = await this.listDatasets(100, 0);
const searchTerm = query.toLowerCase();
const filteredItems = allDatasets.items.filter(dataset => dataset.title.toLowerCase().includes(searchTerm) ||
dataset.description.toLowerCase().includes(searchTerm) ||
dataset.id.toLowerCase().includes(searchTerm)).slice(0, limit);
return {
count: filteredItems.length,
items: filteredItems,
limit,
offset: 0,
total_count: filteredItems.length,
};
}
/**
* Get popular/commonly used datasets
*/
getPopularDatasets() {
return [
'cpih01',
'regional-gdp-by-year',
'wellbeing-local-authority',
'uk-spending-on-cards',
'weekly-deaths-region',
'trade',
'ageing-population-estimates',
'wellbeing-quarterly',
'traffic-camera-activity',
'tax-benefits-statistics',
];
}
/**
* Get dimensions for a dataset
*/
async getDatasetDimensions(datasetId) {
const dataset = await this.getDataset(datasetId);
return dataset.dimensions || [];
}
/**
* Get dimension options for a specific dimension
*/
async getDimensionOptions(datasetId, dimensionId) {
const response = await this.client.get(`/datasets/${datasetId}/dimensions/${dimensionId}/options`);
return response.data;
}
/**
* Health check - verify API is accessible
*/
async healthCheck() {
try {
await this.client.get('/datasets?limit=1');
return true;
}
catch {
return false;
}
}
}
exports.ONSApiClient = ONSApiClient;
//# sourceMappingURL=ons-client.js.map