Skip to content

Commit 3e15fee

Browse files
committed
refactor: cleanup registry class
1 parent 0f9a812 commit 3e15fee

File tree

3 files changed

+9
-76
lines changed

3 files changed

+9
-76
lines changed

src/ui/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { UIRegistry, getDefaultUIRegistry, setDefaultUIRegistry } from "./registry/index.js";
1+
export { UIRegistry } from "./registry/index.js";

src/ui/registry/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { UIRegistry, getDefaultUIRegistry, setDefaultUIRegistry } from "./registry.js";
1+
export { UIRegistry } from "./registry.js";

src/ui/registry/registry.ts

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ import { uiMap } from "./uiMap.js";
44

55
/**
66
* Get the default UI dist path by finding the package root.
7-
* Uses require.resolve to find the package location, which works in both ESM and CJS.
87
*/
98
function getDefaultUIDistPath(): string {
109
try {
11-
// Use require.resolve to find the package.json
12-
// This works because require.resolve is available in both ESM (via createRequire) and CJS
1310
// eslint-disable-next-line @typescript-eslint/no-require-imports
1411
const packageJsonPath = require.resolve("mongodb-mcp-server/package.json");
1512
const packageRoot = dirname(packageJsonPath);
1613
return join(packageRoot, "dist", "ui");
1714
} catch {
18-
// Fallback: try to find dist/ui relative to current working directory
1915
return join(process.cwd(), "dist", "ui");
2016
}
2117
}
@@ -25,54 +21,27 @@ function getDefaultUIDistPath(): string {
2521
*
2622
* The registry:
2723
* - Loads default bundled HTML from dist/ui/ at runtime
28-
* - Allows adding or replacing UIs via register()
24+
* - Allows custom UIs via constructor options
2925
* - Provides get() to retrieve UI HTML for a tool
3026
*/
3127
export class UIRegistry {
3228
private customUIs: Map<string, string> = new Map();
3329
private cache: Map<string, string> = new Map();
3430
private uiDistPath: string;
3531

36-
constructor(options?: { uiDistPath?: string; customUIs?: Record<string, string> }) {
37-
// Use provided path or auto-detect the dist/ui location
38-
this.uiDistPath = options?.uiDistPath ?? getDefaultUIDistPath();
32+
constructor(options?: { customUIs?: Record<string, string> }) {
33+
this.uiDistPath = getDefaultUIDistPath();
3934

40-
// Apply initial custom UIs if provided
4135
if (options?.customUIs) {
4236
for (const [toolName, html] of Object.entries(options.customUIs)) {
43-
this.register(toolName, html);
37+
this.customUIs.set(toolName, html);
4438
}
4539
}
4640
}
4741

4842
/**
49-
* Register a custom UI HTML string for a tool
50-
* @param toolName The name of the tool (e.g., 'list-databases')
51-
* @param html The HTML string to use for this tool's UI
52-
*/
53-
register(toolName: string, html: string): void {
54-
this.customUIs.set(toolName, html);
55-
}
56-
57-
/**
58-
* Unregister a custom UI for a tool, reverting to the default (if one exists)
59-
* @param toolName The name of the tool
60-
*/
61-
unregister(toolName: string): void {
62-
this.customUIs.delete(toolName);
63-
}
64-
65-
/**
66-
* Check if a tool has a UI registered (either custom or default)
67-
* @param toolName The name of the tool
68-
*/
69-
has(toolName: string): boolean {
70-
return this.customUIs.has(toolName) || toolName in uiMap;
71-
}
72-
73-
/**
74-
* Get the UI HTML string for a tool
75-
* Returns the custom UI if registered, otherwise loads the default from disk
43+
* Get the UI HTML string for a tool.
44+
* Returns the custom UI if provided, otherwise loads the default from disk.
7645
* @param toolName The name of the tool
7746
* @returns The HTML string, or undefined if no UI exists for this tool
7847
*/
@@ -93,7 +62,7 @@ export class UIRegistry {
9362
return this.cache.get(toolName);
9463
}
9564

96-
// Try to load from disk (component name -> HTML file)
65+
// Try to load from disk
9766
const filePath = join(this.uiDistPath, `${componentName}.html`);
9867
if (!existsSync(filePath)) {
9968
return undefined;
@@ -107,40 +76,4 @@ export class UIRegistry {
10776
return undefined;
10877
}
10978
}
110-
111-
/**
112-
* Clear the cache of loaded UI HTML strings
113-
* Useful if the underlying files have changed
114-
*/
115-
clearCache(): void {
116-
this.cache.clear();
117-
}
118-
119-
/**
120-
* Get all registered tool names (both custom and default)
121-
*/
122-
getRegisteredTools(): string[] {
123-
const tools = new Set<string>([...this.customUIs.keys(), ...Object.keys(uiMap)]);
124-
return Array.from(tools);
125-
}
126-
}
127-
128-
// Default singleton instance
129-
let defaultRegistry: UIRegistry | undefined;
130-
131-
/**
132-
* Get or create the default UIRegistry instance
133-
*/
134-
export function getDefaultUIRegistry(): UIRegistry {
135-
if (!defaultRegistry) {
136-
defaultRegistry = new UIRegistry();
137-
}
138-
return defaultRegistry;
139-
}
140-
141-
/**
142-
* Set the default UIRegistry instance
143-
*/
144-
export function setDefaultUIRegistry(registry: UIRegistry): void {
145-
defaultRegistry = registry;
14679
}

0 commit comments

Comments
 (0)