52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
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_from_dict",
|
|
{
|
|
title: "Get Random Word From Dictionary",
|
|
description: "Get a list of random words from a dictionary.",
|
|
inputSchema: z.object({
|
|
dictionary_name: z.string(),
|
|
count: z.number().optional().default(1)
|
|
})
|
|
},
|
|
async (input) => {
|
|
try {
|
|
const words: string[] = (await readDictionary(input.dictionary_name)).split("\n")
|
|
|
|
return { content: [{ type: "text", text: getRandomWords(words, input.count).join("\n") }] }
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
isError: true,
|
|
content: [{ type: "text", text: `Error reading dictionary: ${error.message}` }]
|
|
}
|
|
}
|
|
return {
|
|
isError: true,
|
|
content: [{ type: "text", text: "Unknown error reading dictionary" }]
|
|
}
|
|
}
|
|
}
|
|
)
|
|
|
|
server.registerTool(
|
|
"get_random_words_from_list",
|
|
{
|
|
title: "Get Random Word From List",
|
|
description: "Get a list of random words from a list of words.",
|
|
inputSchema: z.object({
|
|
words: z.array(z.string()),
|
|
count: z.number().optional().default(1)
|
|
})
|
|
},
|
|
async (input) => {
|
|
return { content: [{ type: "text", text: getRandomWords(input.words, input.count).join("\n") }] }
|
|
}
|
|
)
|
|
}
|