getDogImage.ts•1.14 kB
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
export function registerGetDogImage(server: McpServer) {
server.tool("get-dog-image", "Fetch a random dog image", {}, async () => {
const ctrl = new AbortController();
const id = setTimeout(() => ctrl.abort(), 8000);
try {
const response = await fetch(`https://dog.ceo/api/breeds/image/random`, {
signal: ctrl.signal,
});
const data = await response.json();
if (data.length === 0) {
return {
content: [
{
type: "text",
text: `No results image found.`,
},
],
};
}
const { message: imageUrl } = data;
return {
content: [
{
type: "text",
text: JSON.stringify(imageUrl, null, 2),
},
],
};
} catch (err: any) {
return {
content: [
{
type: "text",
text: JSON.stringify({ error: err.message }),
},
],
};
} finally {
clearTimeout(id);
}
});
}