get_word_definition
Retrieve word definitions, pronunciations, and meanings using dictionary API to understand vocabulary and improve language skills.
Instructions
Get the definition, pronunciation, and meanings of a word using the Dictionary API
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| language | No | Language code (default: en) | en |
| word | Yes | The word to get the definition for |
Implementation Reference
- src/index.ts:138-235 (handler)The main handler function for 'get_word_definition' tool. Parses input with Zod schema, fetches definition from Dictionary API, processes response data including phonetics, meanings, synonyms, antonyms, examples, and audio, then formats into Markdown text response.private async getWordDefinition(args: unknown) { const { word, language = 'en' } = GetDefinitionArgsSchema.parse(args); try { const response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/${language}/${encodeURIComponent(word)}`); if (!response.ok) { if (response.status === 404) { return { content: [ { type: 'text', text: `No definition found for "${word}". Please check the spelling or try a different word.`, }, ], }; } throw new Error(`Dictionary API error: ${response.status} ${response.statusText}`); } const data: WordEntry[] = await response.json(); if (!data || data.length === 0) { return { content: [ { type: 'text', text: `No definition found for "${word}".`, }, ], }; } const entry = data[0]; let result = `**${entry.word}**\n\n`; // Add phonetic information if (entry.phonetic) { result += `**Pronunciation:** ${entry.phonetic}\n\n`; } else if (entry.phonetics && entry.phonetics.length > 0) { const phoneticTexts = entry.phonetics .filter(p => p.text) .map(p => p.text) .join(', '); if (phoneticTexts) { result += `**Pronunciation:** ${phoneticTexts}\n\n`; } } // Add origin if available if (entry.origin) { result += `**Origin:** ${entry.origin}\n\n`; } // Add meanings result += `**Meanings:**\n\n`; entry.meanings.forEach((meaning, index) => { result += `${index + 1}. **${meaning.partOfSpeech}**\n`; meaning.definitions.forEach((def, defIndex) => { result += ` ${defIndex + 1}. ${def.definition}\n`; if (def.example) { result += ` *Example: "${def.example}"*\n`; } if (def.synonyms && def.synonyms.length > 0) { result += ` *Synonyms: ${def.synonyms.join(', ')}*\n`; } if (def.antonyms && def.antonyms.length > 0) { result += ` *Antonyms: ${def.antonyms.join(', ')}*\n`; } }); result += '\n'; }); // Add audio pronunciation links if available const audioLinks = entry.phonetics .filter(p => p.audio) .map(p => p.audio) .filter(Boolean); if (audioLinks.length > 0) { result += `**Audio Pronunciation:**\n`; audioLinks.forEach((audio, index) => { result += `${index + 1}. ${audio}\n`; }); } return { content: [ { type: 'text', text: result, }, ], }; } catch (error) { throw new Error(`Failed to fetch definition for "${word}": ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/index.ts:39-42 (schema)Zod schema used for validating and parsing the input arguments (word and optional language) in the getWordDefinition handler.const GetDefinitionArgsSchema = z.object({ word: z.string().min(1, 'Word cannot be empty'), language: z.string().default('en').optional(), });
- src/index.ts:72-90 (registration)Tool registration in the ListTools response, including name, description, and JSON input schema matching the Zod schema.{ name: 'get_word_definition', description: 'Get the definition, pronunciation, and meanings of a word using the Dictionary API', inputSchema: { type: 'object', properties: { word: { type: 'string', description: 'The word to get the definition for', }, language: { type: 'string', description: 'Language code (default: en)', default: 'en', }, }, required: ['word'], }, },
- src/index.ts:116-124 (handler)Tool dispatcher switch case in CallToolRequest handler that routes 'get_word_definition' calls to the getWordDefinition method.switch (name) { case 'get_word_definition': return await this.getWordDefinition(args); case 'get_random_word': return await this.getRandomWord(args); default: throw new Error(`Unknown tool: ${name}`); } } catch (error) {
- src/index.ts:13-36 (helper)TypeScript interfaces defining the structure of Dictionary API response used in getWordDefinition handler.interface PhoneticInfo { text: string; audio?: string; } interface Definition { definition: string; example?: string; synonyms: string[]; antonyms: string[]; } interface Meaning { partOfSpeech: string; definitions: Definition[]; } interface WordEntry { word: string; phonetic?: string; phonetics: PhoneticInfo[]; origin?: string; meanings: Meaning[]; }