@nyuta/estat-mcp
Version:
MCP server for e-Stat API integration - Access Japanese government statistical data
65 lines • 2.42 kB
JavaScript
const E_STAT_BASE_URL = "https://api.e-stat.go.jp/rest/3.0/app/json/";
const REQUEST_TIMEOUT = 600000;
export class EStatAPIClient {
appId;
constructor(appId) {
if (!appId) {
throw new Error("E_STAT_APP_ID environment variable is not set");
}
this.appId = appId;
}
async request(endpoint, params) {
const url = this.buildURL(endpoint, params);
try {
const response = await this.fetchWithTimeout(url);
if (!response.ok) {
return this.createErrorResponse(`HTTP error! status: ${response.status}`, String(response.status));
}
return await response.json();
}
catch (error) {
return this.handleError(error);
}
}
buildURL(endpoint, params) {
const url = new URL(`${E_STAT_BASE_URL}${endpoint}`);
url.searchParams.append("appId", this.appId);
for (const [key, value] of Object.entries(params)) {
if (value !== undefined && value !== null && value !== "") {
url.searchParams.append(key, String(value));
}
}
return url;
}
async fetchWithTimeout(url) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), REQUEST_TIMEOUT);
try {
const response = await fetch(url.toString(), {
signal: controller.signal,
headers: {
"Accept": "*/*",
"User-Agent": "estat-mcp/1.0",
},
});
return response;
}
finally {
clearTimeout(timeoutId);
}
}
handleError(error) {
const err = error;
if (err.name === "AbortError") {
return this.createErrorResponse("Request timeout. Please try again.", "timeout");
}
if (err.code === "ENOTFOUND" || err.code === "ECONNREFUSED") {
return this.createErrorResponse("Cannot connect to e-Stat API. Please check your internet connection.", "connection_error");
}
return this.createErrorResponse(`Error occurred: ${err.message || String(error)}`, "unknown_error", String(error));
}
createErrorResponse(error, status, details) {
return { error, status, ...(details && { details }) };
}
}
//# sourceMappingURL=api-client.js.map