~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
65 lines · 3.1 KB Raw History
// What record.mjs and data.mjs share: where things are, the trusted views
// (the bundled ones and the operator's, harness/apps.mjs), the lexicons,
// and how an entry's arguments are filled in.

import { createRequire } from 'node:module';
import { readdirSync, readFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { appViews } from '../apps.mjs';

export const here = dirname(fileURLToPath(import.meta.url));
export const root = resolve(here, '../..');
export const answersFile = join(here, 'answers.json');
export const iconsDir = join(here, 'icons');

const json = (file) => JSON.parse(readFileSync(file, 'utf8'));
const docsIn = (dir) =>
	readdirSync(join(root, dir))
		.filter((f) => f.endsWith('.json'))
		.map((f) => json(join(root, dir, f)));

/** The trusted views as this checkout has them, by their file's name, each with the method or collection it draws: the bundled ones (views/) and the operator's HUD is served with here (harness/apps.mjs). */
export const views = new Map([
	...readdirSync(join(root, 'views'))
		.filter((f) => f.endsWith('.json'))
		.map((f) => [f.slice(0, -5), json(join(root, 'views', f))]),
	...appViews()
]);

/** The bundled view that draws a call, as the server picks it: a query's by its method, records' by their collection; a record a method view names in its `records`, by that view, as its query; none for the rest. */
export function viewOf(name, args) {
	const nsid = name === 'query' ? args.method : name === 'list_records' ? args.collection : name === 'get_record' ? args.uri?.split('/')[3] : null;
	if (!nsid) return null;
	for (const doc of views.values()) if ((doc.method ?? doc.collection) === nsid) return doc;
	if (name === 'get_record') for (const doc of views.values()) if (doc.records?.some((r) => r.collection === nsid)) return doc;
	return null;
}

/** `_meta.view` as the server once recorded it: the view's document, how it draws this call (a method view's, its query), trusted, its origins. */
export function shown(name, doc) {
	const mode = doc.method ? 'query' : name === 'list_records' ? 'list' : 'record';
	return { doc, mode, trusted: true, origins: doc.origins ?? [] };
}

/** The bundled lexicons (lexicons/) and `more`, for @atproto/lexicon to hold answers to. */
export function lexiconsOf(more = []) {
	const { Lexicons } = createRequire(join(root, 'harness/package.json'))('@atproto/lexicon');
	return new Lexicons([...docsIn('lexicons'), ...more]);
}

/** HUD's own lexicons (schemas/), as look.mjs loads them, for the trees (`place.hud.ui`). */
export function schemas() {
	const { Lexicons } = createRequire(join(root, 'harness/package.json'))('@atproto/lexicon');
	return new Lexicons(docsIn('schemas').filter((d) => d.defs?.main?.type !== 'space'));
}

/** `args` with what record.mjs picked put in for each `$picked.<name>`. */
export function filled(args, picked) {
	return JSON.parse(JSON.stringify(args), (_, v) => {
		if (typeof v !== 'string' || !v.startsWith('$picked.')) return v;
		const name = v.slice(8);
		if (!(name in picked)) throw new Error(`nothing picked for ${v}`);
		return picked[name];
	});
}