Compare commits
7 Commits
f347c8ca6f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 322e3bc8db | |||
| b7dcd618ba | |||
| bdc4b0b8f7 | |||
| 98223c8312 | |||
| 8e353dce44 | |||
| 8e19ae07bb | |||
| 57039a50b5 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -33,5 +33,6 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
|
||||
# Data
|
||||
# Outputs & Inputs
|
||||
data
|
||||
bin
|
||||
@@ -1,8 +1,7 @@
|
||||
import husky from "husky"
|
||||
|
||||
// Skip Husky install in production and CI environments
|
||||
if (process.env.NODE_ENV === "production" || process.env.CI === "true") {
|
||||
if (process.env.NODE_ENV === "production" || process.env.CI === "true" || process.env.SKIP_HUSKY === "true") {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const { default: husky } = await import("husky")
|
||||
console.log(husky())
|
||||
64
helpers/markov.ts
Normal file
64
helpers/markov.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
export interface MarkovOptions {
|
||||
minLength?: number
|
||||
maxLength?: number
|
||||
order?: number
|
||||
startWith?: string
|
||||
maxAttempts?: number
|
||||
}
|
||||
|
||||
export const buildMarkovChain = (words: string[], order: number, startWith?: string): Map<string, string[]> => {
|
||||
const chain: Map<string, string[]> = new Map()
|
||||
|
||||
for (const word of words) {
|
||||
const paddedWord = startWith ? startWith + word : word
|
||||
for (let i = 0; i <= paddedWord.length - order; i++) {
|
||||
const state = paddedWord.slice(i, i + order)
|
||||
const nextChar = i + order < paddedWord.length ? paddedWord.charAt(i + order) : ""
|
||||
if (!chain.has(state)) {
|
||||
chain.set(state, [])
|
||||
}
|
||||
;(chain.get(state) as string[]).push(nextChar)
|
||||
}
|
||||
}
|
||||
|
||||
return chain
|
||||
}
|
||||
|
||||
export const generateWordFromChain = (
|
||||
chain: Map<string, string[]>,
|
||||
minLength: number,
|
||||
maxLength: number,
|
||||
maxAttempts: number
|
||||
): string | null => {
|
||||
let attempts = 0
|
||||
while (attempts < maxAttempts) {
|
||||
attempts++
|
||||
|
||||
// Choose random starting state
|
||||
const states = Array.from(chain.keys())
|
||||
if (states.length === 0) return null
|
||||
|
||||
const currentIndex = Math.floor(Math.random() * states.length)
|
||||
let current = states[currentIndex] as string
|
||||
let word = current
|
||||
|
||||
// Generate until we reach desired length
|
||||
while (word.length < maxLength) {
|
||||
const possibilities = chain.get(current) || []
|
||||
if (possibilities.length === 0) break
|
||||
|
||||
const nextIndex = Math.floor(Math.random() * possibilities.length)
|
||||
const next = possibilities[nextIndex] as string
|
||||
if (next === "") break // End of word
|
||||
|
||||
word += next
|
||||
current = current.slice(1) + next
|
||||
}
|
||||
|
||||
// Check if word meets length requirements
|
||||
if (word.length >= minLength && word.length <= maxLength) {
|
||||
return word
|
||||
}
|
||||
}
|
||||
return null // Could not generate valid word
|
||||
}
|
||||
@@ -15,9 +15,9 @@
|
||||
"typescript": "^5"
|
||||
},
|
||||
"scripts": {
|
||||
"build:stdio": "bun build ./stdio.ts --compile --outfile stdio",
|
||||
"build:stdio": "bun build ./stdio.ts --compile --outfile bin/stdio",
|
||||
"build": "bun build:stdio",
|
||||
"prepare": "bun .husky/install.ts"
|
||||
"prepare": "bun .husky/install.mts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "^1.26.0",
|
||||
|
||||
@@ -27,7 +27,7 @@ export const readDictionary = async (name: string): Promise<string> => {
|
||||
if (!(await file.exists())) {
|
||||
throw new Error(`Dictionary "${name === "" || name === undefined ? "undefined" : name}" does not exist`)
|
||||
}
|
||||
return Bun.file(path).text()
|
||||
return await file.text()
|
||||
}
|
||||
|
||||
export const listDictionaries = async (): Promise<string[]> => {
|
||||
|
||||
29
services/makeUpWords.ts
Normal file
29
services/makeUpWords.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { buildMarkovChain, generateWordFromChain, type MarkovOptions } from "@/helpers/markov"
|
||||
|
||||
export const generateMadeUpWords = (words: string[], count: number, options: MarkovOptions = {}): string[] => {
|
||||
const { minLength, maxLength, order = 2, startWith, maxAttempts = 1000 } = options
|
||||
|
||||
// If no words provided, return empty array
|
||||
if (words.length === 0) {
|
||||
return []
|
||||
}
|
||||
|
||||
// Calculate min/max lengths from dictionary if not provided
|
||||
const lengths = words.map((w) => w.length)
|
||||
const actualMinLength = minLength ?? Math.min(...lengths)
|
||||
const actualMaxLength = maxLength ?? Math.max(...lengths)
|
||||
|
||||
// Build Markov chain
|
||||
const chain = buildMarkovChain(words, order, startWith)
|
||||
|
||||
// Generate the requested number of words
|
||||
const result: string[] = []
|
||||
for (let i = 0; i < count; i++) {
|
||||
const word = generateWordFromChain(chain, actualMinLength, actualMaxLength, maxAttempts)
|
||||
if (word) {
|
||||
result.push(word)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
4
stdio.ts
4
stdio.ts
@@ -1,7 +1,8 @@
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
||||
import { registerDictionariesFunctionality } from "@/resources/dictionaries"
|
||||
import { addRandomWordsTool } from "./tools/getRandomWords"
|
||||
import { addRandomWordsTool } from "@/tools/getRandomWords"
|
||||
import { addMakeUpWordsTool } from "@/tools/makeUpWords"
|
||||
|
||||
const server = new McpServer({
|
||||
name: "writer-helpers",
|
||||
@@ -10,6 +11,7 @@ const server = new McpServer({
|
||||
|
||||
registerDictionariesFunctionality(server)
|
||||
addRandomWordsTool(server)
|
||||
addMakeUpWordsTool(server)
|
||||
|
||||
const transport = new StdioServerTransport()
|
||||
await server.connect(transport)
|
||||
|
||||
@@ -5,32 +5,47 @@ import { getRandomWords } from "@/services/randomWord"
|
||||
|
||||
export const addRandomWordsTool = (server: McpServer) => {
|
||||
server.registerTool(
|
||||
"get_random_words",
|
||||
"get_random_words_from_dict",
|
||||
{
|
||||
title: "Get Random Words",
|
||||
title: "Get Random Word From Dictionary",
|
||||
description: "Get a list of random words from a dictionary.",
|
||||
inputSchema: z.object({
|
||||
dictionary: z.string(),
|
||||
dictionary_name: z.string(),
|
||||
count: z.number().optional().default(1)
|
||||
})
|
||||
},
|
||||
async (input) => {
|
||||
try {
|
||||
const words: string[] = (await readDictionary(input.dictionary)).split("\n")
|
||||
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: []
|
||||
content: [{ type: "text", text: `Error reading dictionary: ${error.message}` }]
|
||||
}
|
||||
}
|
||||
return {
|
||||
isError: true,
|
||||
content: []
|
||||
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") }] }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
103
tools/makeUpWords.ts
Normal file
103
tools/makeUpWords.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
|
||||
import z from "zod"
|
||||
import { readDictionary } from "@/services/dictionaries"
|
||||
import { generateMadeUpWords } from "@/services/makeUpWords"
|
||||
|
||||
export const addMakeUpWordsTool = (server: McpServer) => {
|
||||
server.registerTool(
|
||||
"generate_made_up_words_from_dict",
|
||||
{
|
||||
title: "Generate Made-up Words from Dictionary",
|
||||
description: "Generate new words using Markov chains based on a dictionary.",
|
||||
inputSchema: z.object({
|
||||
dictionary_name: z.string(),
|
||||
count: z.number().optional().default(1),
|
||||
minLength: z.number().optional(),
|
||||
maxLength: z.number().optional(),
|
||||
order: z.number().optional().default(2),
|
||||
startWith: z.string().optional(),
|
||||
maxAttempts: z.number().optional().default(1000)
|
||||
})
|
||||
},
|
||||
async (input) => {
|
||||
try {
|
||||
if (input.count <= 0) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "Count must be a positive number" }]
|
||||
}
|
||||
}
|
||||
if (input.minLength !== undefined && input.maxLength !== undefined && input.minLength > input.maxLength) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "minLength cannot be greater than maxLength" }]
|
||||
}
|
||||
}
|
||||
|
||||
const words: string[] = (await readDictionary(input.dictionary_name)).split("\n").filter((w) => w.trim())
|
||||
|
||||
const generatedWords = generateMadeUpWords(words, input.count, {
|
||||
minLength: input.minLength,
|
||||
maxLength: input.maxLength,
|
||||
order: input.order,
|
||||
startWith: input.startWith,
|
||||
maxAttempts: input.maxAttempts
|
||||
})
|
||||
|
||||
return { content: [{ type: "text", text: generatedWords.join("\n") }] }
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text", text: `Error generating words: ${error.message}` }]
|
||||
}
|
||||
}
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "Unknown error generating words" }]
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
server.registerTool(
|
||||
"generate_made_up_words_from_list",
|
||||
{
|
||||
title: "Generate Made-up Words from List",
|
||||
description: "Generate new words using Markov chains based on a list of words.",
|
||||
inputSchema: z.object({
|
||||
words: z.array(z.string()),
|
||||
count: z.number().optional().default(1),
|
||||
minLength: z.number().optional(),
|
||||
maxLength: z.number().optional(),
|
||||
order: z.number().optional().default(2),
|
||||
startWith: z.string().optional(),
|
||||
maxAttempts: z.number().optional().default(1000)
|
||||
})
|
||||
},
|
||||
async (input) => {
|
||||
if (input.count <= 0) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "Count must be a positive number" }]
|
||||
}
|
||||
}
|
||||
if (input.minLength !== undefined && input.maxLength !== undefined && input.minLength > input.maxLength) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "minLength cannot be greater than maxLength" }]
|
||||
}
|
||||
}
|
||||
|
||||
const generatedWords = generateMadeUpWords(input.words, input.count, {
|
||||
minLength: input.minLength,
|
||||
maxLength: input.maxLength,
|
||||
order: input.order,
|
||||
startWith: input.startWith,
|
||||
maxAttempts: input.maxAttempts
|
||||
})
|
||||
|
||||
return { content: [{ type: "text", text: generatedWords.join("\n") }] }
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user