37 lines
1014 B
TypeScript
37 lines
1014 B
TypeScript
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
|
|
import z from "zod"
|
|
import { readDictionary } from "@/services/dictionaries"
|
|
import { getRandomWords } from "@/services/randomWord"
|
|
|
|
export const addRandomWordsTool = (server: McpServer) => {
|
|
server.registerTool(
|
|
"get_random_words",
|
|
{
|
|
title: "Get Random Words",
|
|
description: "Get a list of random words from a dictionary.",
|
|
inputSchema: z.object({
|
|
dictionary: z.string(),
|
|
count: z.number().optional().default(1)
|
|
})
|
|
},
|
|
async (input) => {
|
|
try {
|
|
const words: string[] = (await readDictionary(input.dictionary)).split("\n")
|
|
|
|
return { content: [{ type: "text", text: getRandomWords(words, input.count).join("\n") }] }
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
isError: true,
|
|
content: []
|
|
}
|
|
}
|
|
return {
|
|
isError: true,
|
|
content: []
|
|
}
|
|
}
|
|
}
|
|
)
|
|
}
|