getRandomThing.ts•1.16 kB
import ThingiversePuppeteer from '@/class/thingiverser.class';
import { mcpServer } from '@/index';
mcpServer.tool('get-random-thing', 'Get random things from thingiverse', {}, async () => {
const thingiverser = new ThingiversePuppeteer(process.env.APP_TOKEN as string);
await thingiverser.init();
try {
const things = await thingiverser.getRandomThings();
if (typeof things === 'string') {
await thingiverser.close();
return {
isError: true,
content: [{ type: 'text', text: things }],
};
}
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}` }],
};
}
});