client.ts•5.03 kB
import axios, { AxiosInstance, AxiosResponse } from 'axios';
import { HuggingFaceConfig } from './types.js';
/**
* Client for interacting with Hugging Face Hub API
*/
export class HuggingFaceClient {
private httpClient: AxiosInstance;
private config: HuggingFaceConfig;
constructor(config: HuggingFaceConfig) {
this.config = config;
const headers: Record<string, string> = {
'User-Agent': 'hf-mcp/0.1.0'
};
if (config.token) {
headers['Authorization'] = `Bearer ${config.token}`;
}
this.httpClient = axios.create({
baseURL: config.baseUrl,
timeout: 30000,
headers
});
}
/**
* Get all models from the Hub
*/
async getModels(params: Record<string, any> = {}): Promise<string> {
try {
const response: AxiosResponse = 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: string, revision?: string): Promise<string> {
try {
const endpoint = revision
? `/api/models/${repoId}/revision/${revision}`
: `/api/models/${repoId}`;
const response: AxiosResponse = 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(): Promise<string> {
try {
const response: AxiosResponse = 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: Record<string, any> = {}): Promise<string> {
try {
const response: AxiosResponse = 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: string, revision?: string, params: Record<string, any> = {}): Promise<string> {
try {
const endpoint = revision
? `/api/datasets/${repoId}/revision/${revision}`
: `/api/datasets/${repoId}`;
const response: AxiosResponse = 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: string, subset?: string, split?: string, n?: number): Promise<string> {
try {
let endpoint = `/api/datasets/${repoId}/parquet`;
if (subset) {
endpoint += `/${subset}`;
if (split) {
endpoint += `/${split}`;
if (n !== undefined) {
endpoint += `/${n}.parquet`;
}
}
}
const response: AxiosResponse = 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: string): Promise<string> {
try {
const response: AxiosResponse = 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(): Promise<string> {
try {
const response: AxiosResponse = 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)}`);
}
}
}