Created
January 14, 2025 09:27
-
-
Save samjarman/ad76c175834f05227ef5915f4c44a451 to your computer and use it in GitHub Desktop.
Pokemon MCP Server snipped
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Server } from "@modelcontextprotocol/sdk/server/index.js"; | |
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; | |
import { ListToolsRequestSchema, CallToolRequestSchema} from "@modelcontextprotocol/sdk/types.js"; | |
async function getPokemonDetails(pokemonName: string): Promise<string> { | |
try { | |
const url = `https://pokeapi.co/api/v2/pokemon/${pokemonName.toLowerCase()}`; | |
console.error("Fetching Pokemon details from:", url); | |
const response = await fetch(url); | |
const data = await response.json(); | |
return JSON.stringify(data); | |
} catch (error) { | |
return "Error: Could not fetch pokemon details " + (error as Error).message; | |
} | |
} | |
async function main() { | |
console.error("Starting server..."); // Debug log | |
const server = new Server({ | |
name: "pokemon", | |
version: "1.0.0", | |
}, { | |
capabilities: { | |
resources: {}, | |
tools: {} // You don't need to define tools here, they are defined in the setRequestHandler | |
} | |
}); | |
server.setRequestHandler(ListToolsRequestSchema, async () => { | |
console.error("Received ListTools request"); // Debug log | |
return { | |
tools: [ | |
{ | |
name: "getPokemonDetails", | |
description: "Get the details of a pokemon", | |
inputSchema: { | |
type: "object", | |
properties: { | |
pokemonName: { | |
type: "string", | |
description: "Name of the pokemon to look up" | |
} | |
}, | |
required: ["pokemonName"] | |
} | |
} | |
] | |
}; | |
}); | |
server.setRequestHandler(CallToolRequestSchema, async (request) => { | |
console.error("Received CallTool request", JSON.stringify(request, null, 2)); // Debug log | |
switch(request.params.name) { | |
case "getPokemonDetails": | |
const pokemonName = request.params.arguments?.pokemonName; | |
if (typeof pokemonName !== 'string') { | |
throw new Error("pokemonName parameter must be a string"); | |
} | |
const pokemonData = await getPokemonDetails(pokemonName); | |
return { | |
content: [{ | |
type: "text", | |
text: pokemonData | |
}] | |
}; | |
default: | |
throw new Error("Tool not found"); | |
} | |
}); | |
const transport = new StdioServerTransport(); | |
await server.connect(transport); | |
console.error("Server is running and connected to transport"); // Debug log | |
} | |
void main().catch((error) => { | |
console.error("Server error:", error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment