getThings.ts•1.53 kB
import ThingiversePuppeteer from '@/class/thingiverser.class';
import { mcpServer } from '@/index';
import z from 'zod';
mcpServer.tool(
'get-things',
'Get things based on term',
{
term: z.string().describe("term usable for search inside thingiverse's database"),
categoryId: z.string().optional().describe('category to search in if know'),
},
async ({ term, categoryId }) => {
const thingiverser = new ThingiversePuppeteer(process.env.APP_TOKEN as string);
await thingiverser.init();
try {
const thingiverseSearchResp = await thingiverser.searchThings(term, categoryId);
if (thingiverseSearchResp.total === 0) {
await thingiverser.close();
return {
isError: true,
content: [{ type: 'text', text: 'No things found' }],
};
}
await thingiverser.close();
const things = thingiverseSearchResp.hits;
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}` }],
};
}
},
);