tools.ts•2.65 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { pokeAPI } from "./client.js";
import {
formatPokemon,
formatTypeEffectiveness,
formatPokemonEncounters,
formatPokemonSearchResults,
formatCaughtError,
} from "./formatters.js";
export function register(server: McpServer) {
// Fetch Pokemon tool
server.tool(
"fetch_pokemon",
"Fetch detailed information about a Pokémon by name or ID",
{
name: z.string().min(1).describe("The name or ID of the Pokémon"),
},
async ({ name }) => {
try {
const pokemon = await pokeAPI.getPokemon(name.toLowerCase().trim());
return formatPokemon(pokemon);
} catch (error) {
return formatCaughtError(error, "fetching Pokémon");
}
},
);
// Get type effectiveness tool
server.tool(
"get_type_effectiveness",
"Get type effectiveness information including strengths, weaknesses, and resistances",
{
name: z.string().min(1).describe("The name or ID of the type"),
},
async ({ name }) => {
try {
const effectiveness = await pokeAPI.getTypeEffectiveness(
name.toLowerCase().trim(),
);
return formatTypeEffectiveness(effectiveness);
} catch (error) {
return formatCaughtError(error, "fetching type information");
}
},
);
// Get Pokemon encounters tool
server.tool(
"get_pokemon_encounters",
"Get location encounter information for a Pokémon",
{ name: z.string().min(1).describe("The name or ID of the Pokémon") },
async ({ name }) => {
try {
const { pokemon, encounters } = await pokeAPI.getPokemonWithEncounters(
name.toLowerCase().trim(),
);
return formatPokemonEncounters(pokemon, encounters);
} catch (error) {
return formatCaughtError(error, "fetching encounter information");
}
},
);
// Search Pokemon tool
server.tool(
"search_pokemon",
"Search for Pokémon by partial name match",
{ query: z.string().min(1).describe("Partial name to search for") },
async ({ query }) => {
try {
// Get a larger list to search through
const pokemonList = await pokeAPI.getPokemonList(1500);
const matches = pokemonList.results
.filter((p) =>
p.name.toLowerCase().includes(query.toLowerCase().trim()),
)
.slice(0, 20); // Limit to 20 results
return formatPokemonSearchResults(query, matches);
} catch (error) {
return formatCaughtError(error, "searching Pokémon");
}
},
);
}