/**
* FeedMob API Client
* Handles HTTP requests to the FeedMob Assistant Internal API
*/
const API_BASE_URL = process.env.FEEDMOB_API_BASE_URL || "https://assistant.feedmob.ai";
const API_TOKEN = process.env.FEEDMOB_API_TOKEN;
export interface ApiResponse<T> {
data?: T;
metadata?: {
web_url?: string;
};
error?: string;
}
/**
* Make an authenticated request to the FeedMob API
*/
export async function apiRequest<T>(
endpoint: string,
params?: Record<string, string | number | boolean | undefined>
): Promise<ApiResponse<T>> {
if (!API_TOKEN) {
return { error: "FEEDMOB_API_TOKEN environment variable is not set" };
}
// Build URL with query parameters
const url = new URL(endpoint, API_BASE_URL);
if (params) {
for (const [key, value] of Object.entries(params)) {
if (value !== undefined && value !== null) {
url.searchParams.set(key, String(value));
}
}
}
try {
const response = await fetch(url.toString(), {
method: "GET",
headers: {
Authorization: `Bearer ${API_TOKEN}`,
Accept: "application/json",
"Content-Type": "application/json",
},
});
if (!response.ok) {
const errorText = await response.text();
return {
error: `API request failed (${response.status}): ${errorText}`,
};
}
const data = await response.json();
return data as ApiResponse<T>;
} catch (error) {
return {
error: `Network error: ${error instanceof Error ? error.message : String(error)}`,
};
}
}
/**
* Format API response for MCP tool output
*/
export function formatResponse(data: unknown): string {
return JSON.stringify(data, null, 2);
}
/**
* Create standard MCP text content response
*/
export function textContent(text: string) {
return {
content: [{ type: "text" as const, text }],
};
}