unsplash.ts•2.89 kB
import { BaseApiClient } from "../../core/base-api.js";
import { loadConfig } from "../../core/config.js";
import { ToolRegistration } from "../../core/types.js";
class UnsplashClient extends BaseApiClient {
private readonly accessKey: string;
constructor(accessKey: string) {
super("https://api.unsplash.com");
this.accessKey = accessKey;
}
private headers() {
return { Authorization: `Client-ID ${this.accessKey}` };
}
searchPhotos(query: string, perPage?: number) {
return this.request("/search/photos", { headers: this.headers(), query: { query, per_page: perPage ?? 10 } });
}
getRandomPhoto(category?: string) {
return this.request("/photos/random", { headers: this.headers(), query: category ? { query: category } : undefined });
}
getPhotoDetails(photoId: string) {
return this.request(`/photos/${photoId}`, { headers: this.headers() });
}
}
export function registerUnsplash(): ToolRegistration {
const cfg = loadConfig();
const client = new UnsplashClient(cfg.unsplashAccessKey || "");
return {
tools: [
{
name: "search_photos",
description: "Search Unsplash photos",
inputSchema: {
type: "object",
properties: { query: { type: "string" }, per_page: { type: "number" } },
required: ["query"],
},
},
{
name: "get_random_photo",
description: "Get a random photo optionally filtered by category",
inputSchema: {
type: "object",
properties: { category: { type: "string" } },
},
},
{
name: "get_photo_details",
description: "Get photo details by ID",
inputSchema: {
type: "object",
properties: { photo_id: { type: "string" } },
required: ["photo_id"],
},
},
],
handlers: {
async search_photos(args: Record<string, unknown>) {
if (!cfg.unsplashAccessKey) throw new Error("UNSPLASH_ACCESS_KEY is not configured");
const query = String(args.query || "");
if (!query) throw new Error("query is required");
const perPage = args.per_page ? Number(args.per_page) : undefined;
return client.searchPhotos(query, perPage);
},
async get_random_photo(args: Record<string, unknown>) {
if (!cfg.unsplashAccessKey) throw new Error("UNSPLASH_ACCESS_KEY is not configured");
const category = args.category ? String(args.category) : undefined;
return client.getRandomPhoto(category);
},
async get_photo_details(args: Record<string, unknown>) {
if (!cfg.unsplashAccessKey) throw new Error("UNSPLASH_ACCESS_KEY is not configured");
const photoId = String(args.photo_id || "");
if (!photoId) throw new Error("photo_id is required");
return client.getPhotoDetails(photoId);
},
},
};
}