client.js•4.38 kB
import axios from 'axios';
/**
* Client for interacting with Hugging Face Hub API
*/
export class HuggingFaceClient {
httpClient;
config;
constructor(config) {
this.config = config;
this.httpClient = axios.create({
baseURL: config.baseUrl,
timeout: 30000,
headers: {
'User-Agent': 'hf-mcp/0.1.0'
}
});
}
/**
* Get all models from the Hub
*/
async getModels(params = {}) {
try {
const response = await this.httpClient.get('/api/models', { params });
return JSON.stringify(response.data, null, 2);
}
catch (error) {
throw new Error(`Failed to fetch models: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get specific model information
*/
async getModelInfo(repoId, revision) {
try {
const endpoint = revision
? `/api/models/${repoId}/revision/${revision}`
: `/api/models/${repoId}`;
const response = await this.httpClient.get(endpoint);
return JSON.stringify(response.data, null, 2);
}
catch (error) {
throw new Error(`Failed to fetch model info: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get model tags by type
*/
async getModelTags() {
try {
const response = await this.httpClient.get('/api/models-tags-by-type');
return JSON.stringify(response.data, null, 2);
}
catch (error) {
throw new Error(`Failed to fetch model tags: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get all datasets from the Hub
*/
async getDatasets(params = {}) {
try {
const response = await this.httpClient.get('/api/datasets', { params });
return JSON.stringify(response.data, null, 2);
}
catch (error) {
throw new Error(`Failed to fetch datasets: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get specific dataset information
*/
async getDatasetInfo(repoId, revision, params = {}) {
try {
const endpoint = revision
? `/api/datasets/${repoId}/revision/${revision}`
: `/api/datasets/${repoId}`;
const response = await this.httpClient.get(endpoint, { params });
return JSON.stringify(response.data, null, 2);
}
catch (error) {
throw new Error(`Failed to fetch dataset info: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get dataset parquet files
*/
async getDatasetParquet(repoId, subset, split, n) {
try {
let endpoint = `/api/datasets/${repoId}/parquet`;
if (subset) {
endpoint += `/${subset}`;
if (split) {
endpoint += `/${split}`;
if (n !== undefined) {
endpoint += `/${n}.parquet`;
}
}
}
const response = await this.httpClient.get(endpoint);
return JSON.stringify(response.data, null, 2);
}
catch (error) {
throw new Error(`Failed to fetch dataset parquet: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get Croissant metadata for dataset
*/
async getDatasetCroissant(repoId) {
try {
const response = await this.httpClient.get(`/api/datasets/${repoId}/croissant`);
return JSON.stringify(response.data, null, 2);
}
catch (error) {
throw new Error(`Failed to fetch Croissant metadata: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get dataset tags by type
*/
async getDatasetTags() {
try {
const response = await this.httpClient.get('/api/datasets-tags-by-type');
return JSON.stringify(response.data, null, 2);
}
catch (error) {
throw new Error(`Failed to fetch dataset tags: ${error instanceof Error ? error.message : String(error)}`);
}
}
}