131 lines
4.1 KiB
TypeScript
131 lines
4.1 KiB
TypeScript
import { type McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"
|
|
import { UriTemplate } from "@modelcontextprotocol/sdk/shared/uriTemplate.js"
|
|
import type { ListResourcesResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js"
|
|
import z from "zod"
|
|
import {
|
|
createDictionary,
|
|
deleteDictionary,
|
|
listDictionaries,
|
|
readDictionary,
|
|
updateDictionary
|
|
} from "@/services/dictionaries"
|
|
|
|
export const registerDictionariesFunctionality = (server: McpServer) => {
|
|
server.registerResource(
|
|
"dictionaries",
|
|
new ResourceTemplate(new UriTemplate(`dictionary://{name}`), {
|
|
list: async () => {
|
|
try {
|
|
const dictionaries = await listDictionaries()
|
|
return {
|
|
resources: dictionaries.map((dictionary) => ({
|
|
name: dictionary,
|
|
uri: `dictionary://${dictionary}`,
|
|
mimeType: "text/plain"
|
|
}))
|
|
} satisfies ListResourcesResult
|
|
} catch (_) {
|
|
return {
|
|
resources: []
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
{ mimeType: "text/plain" },
|
|
async (uri: URL) => {
|
|
try {
|
|
const dictionary = await readDictionary(uri.href.replace("dictionary://", ""))
|
|
return { contents: [{ uri: uri.toString(), text: dictionary }] } satisfies ReadResourceResult
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
isError: true,
|
|
contents: [{ uri: uri.toString(), text: error.message }]
|
|
} satisfies ReadResourceResult
|
|
}
|
|
console.error(error)
|
|
return {
|
|
isError: true,
|
|
contents: [{ uri: uri.toString(), text: "An unknown error occurred." }]
|
|
} satisfies ReadResourceResult
|
|
}
|
|
}
|
|
)
|
|
|
|
server.registerTool(
|
|
"add_dictionary",
|
|
{
|
|
title: "Add Dictionary",
|
|
description: "Add a dictionary. Dictionaries are text files containing words or phrases separated by newlines.",
|
|
inputSchema: z.object({
|
|
name: z.string(),
|
|
content: z.string()
|
|
})
|
|
},
|
|
async ({ name, content }) => {
|
|
try {
|
|
const dictionaryName = await createDictionary(name, content)
|
|
return {
|
|
content: [
|
|
{ type: "text", text: `Dictionary ${name} created` },
|
|
{ type: "resource_link", uri: `dictionary://${dictionaryName}`, name: name }
|
|
]
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
return { isError: true, content: [{ type: "text", text: `An error occurred: ${error.message}` }] }
|
|
}
|
|
console.error(error)
|
|
return { isError: true, content: [{ type: "text", text: "An unknown error occurred." }] }
|
|
}
|
|
}
|
|
)
|
|
|
|
server.registerTool(
|
|
"delete_dictionary",
|
|
{
|
|
title: "Delete Dictionary",
|
|
description: "Delete a dictionary.",
|
|
inputSchema: z.object({
|
|
name: z.string()
|
|
})
|
|
},
|
|
async ({ name }) => {
|
|
try {
|
|
await deleteDictionary(name)
|
|
return { content: [{ type: "text", text: "Dictionary deleted" }] }
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
return { isError: true, content: [{ type: "text", text: `An error occurred: ${error.message}` }] }
|
|
}
|
|
console.error(error)
|
|
return { isError: true, content: [{ type: "text", text: "An unknown error occurred." }] }
|
|
}
|
|
}
|
|
)
|
|
|
|
server.registerTool(
|
|
"update_dictionary",
|
|
{
|
|
title: "Update Dictionary",
|
|
description: "Update a dictionary, overwriting the existing content.",
|
|
inputSchema: z.object({
|
|
name: z.string(),
|
|
content: z.string()
|
|
})
|
|
},
|
|
async ({ name, content }) => {
|
|
try {
|
|
await updateDictionary(name, content)
|
|
return { content: [{ type: "text", text: "Dictionary updated" }] }
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
return { isError: true, content: [{ type: "text", text: `An error occurred: ${error.message}` }] }
|
|
}
|
|
console.error(error)
|
|
return { isError: true, content: [{ type: "text", text: "An unknown error occurred." }] }
|
|
}
|
|
}
|
|
)
|
|
}
|