Skip to content
← Public packages

@kentcdodds/audible

Personal Audible library app — browser re-auth, library, wishlist, NAS archive stub.

src/home-mcp.ts

58 lines · 1.7 KB · TypeScript
import { kody } from 'kody:runtime'

export type HomeAudiobookMcp = {
	audiobook_library_path: (args?: { filename?: string }) => Promise<{
		ok?: boolean
		libraryPath?: string
		exists?: boolean
		writable?: boolean
		ffmpegAvailable?: boolean
		file?: unknown
		[key: string]: unknown
	}>
	audiobook_library_filename: (args: { title: string }) => Promise<{
		ok?: boolean
		title?: string
		filename?: string
		[key: string]: unknown
	}>
	audiobook_exists: (args: { filename: string }) => Promise<{
		ok?: boolean
		filename?: string
		path?: string
		exists?: boolean
		[key: string]: unknown
	}>
	audiobook_import_aaxc: (
		args: Record<string, unknown>,
	) => Promise<Record<string, unknown>>
}

const REQUIRED_TOOLS = [
	'audiobook_library_path',
	'audiobook_library_filename',
	'audiobook_exists',
	'audiobook_import_aaxc',
] as const

/**
 * Resolve connected home MCP audiobook convert tools.
 * Conversion and NAS writes live on home — this package only calls them.
 */
export function getHomeAudiobookMcp(): HomeAudiobookMcp {
	const mcp = (kody as { mcp?: Record<string, unknown> }).mcp
	const home = mcp?.home as HomeAudiobookMcp | undefined
	if (!home) {
		throw new Error(
			'Home MCP is not connected. archiveToNas needs kody.mcp["home"] audiobook convert tools (audiobook_library_path, audiobook_library_filename, audiobook_exists, audiobook_import_aaxc). Connect the home connector and retry.',
		)
	}
	for (const name of REQUIRED_TOOLS) {
		if (typeof home[name] !== 'function') {
			throw new Error(
				`Home MCP is missing ${name}. Restart or update the home connector so audiobook convert tools are available, then retry archiveToNas.`,
			)
		}
	}
	return home
}