getThingsFromCategory.ts•1.4 kB
import ThingiversePuppeteer from '@/class/thingiverser.class';
import { mcpServer } from '@/index';
import z from 'zod';
mcpServer.tool(
'get-random-thing-from-category',
'Get random items from the given category',
{
categorySlug: z.string().describe('category slug to search in'),
},
async ({ categorySlug }) => {
const thingiverser = new ThingiversePuppeteer(process.env.APP_TOKEN as string);
await thingiverser.init();
try {
const things = await thingiverser.getRandomThingsFromCategory(categorySlug);
if (things.length === 0) {
await thingiverser.close();
return {
isError: true,
content: [{ type: 'text', text: 'No things found' }],
};
}
await thingiverser.close();
const returnThings = [];
for (const thing of things) {
returnThings.push({
name: thing.name,
url: thing.public_url,
likeCount: thing.like_count,
descrtipion: thing.description,
filesUrl: thing.files_url,
tags: thing.tags,
});
}
return {
content: [{ type: 'text', text: JSON.stringify(returnThings, null, 2) }],
};
} catch (error) {
await thingiverser.close();
return {
isError: true,
content: [{ type: 'text', text: `Error fetching random things: ${error}` }],
};
}
},
);