refactor(mcp): use shared tool extraction helper and clean imports

Replace deep relative package.json import with existing
getPackageVersion() helper. Extract extractToolsFromProcedures into
a shared _extract-tools.ts that silences expected trpc-to-mcp warnings
for z.void() procedures. Remove dead eslint-disable comments.
This commit is contained in:
Thomas Camlong committed 2026-06-08 23:38:06 +02:00
1 parent 4ed264c318
commit de68b64321
3 files changed
+36 -16

No files matched your search

@@ -8,9 +8,9 @@ import { api } from "@homarr/api/server";
import { auth } from "@homarr/auth/next";
import { extractBaseUrlFromHeaders } from "@homarr/common";
import { getScopedI18n } from "@homarr/translation/server";
import { extractToolsFromProcedures } from "trpc-to-mcp";
import { createMetaTitle } from "~/metadata";
import { extractMcpTools } from "~/app/api/mcp/_extract-tools";
import { ApiKeysManagement } from "./components/api-keys";
import { ApiPageTabs } from "./components/api-page-tabs";
import { ScalarApiReference } from "./components/scalar-api-reference";
@@ -22,9 +22,8 @@ let cachedToolGroups: McpToolGroup[] | null = null;
function getMcpToolGroups(): McpToolGroup[] {
if (cachedToolGroups) return cachedToolGroups;
const tools = extractToolsFromProcedures(mcpRouter);
const tools = extractMcpTools();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const procedures = (mcpRouter as any)._def.procedures as Record<string, { _def?: { type?: string } }>;
const procedureTypeMap = new Map<string, "query" | "mutation">();
for (const [key, proc] of Object.entries(procedures)) {
@@ -3,7 +3,6 @@ import type { NextRequest } from "next/server";
import { userAgent } from "next/server";
import { createMcpHandler } from "mcp-handler";
import type { McpTool } from "trpc-to-mcp";
import { extractToolsFromProcedures } from "trpc-to-mcp";
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
import { createTRPCContext, mcpRouter } from "@homarr/api/mcp";
@@ -13,7 +12,8 @@ import { ipAddressFromHeaders } from "@homarr/common/server";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { db } from "@homarr/db";
import packageJson from "../../../../../../../package.json";
import { getPackageVersion } from "~/versions/package-reader";
import { extractMcpTools } from "../_extract-tools";
const logger = createLogger({ module: "mcpRoute" });
@@ -25,15 +25,14 @@ interface RateLimitStore {
cleanupTimer: ReturnType<typeof setInterval> | null;
}
const globalRateLimit = globalThis as unknown as { mcpRateLimit?: RateLimitStore };
if (!globalRateLimit.mcpRateLimit) {
globalRateLimit.mcpRateLimit = {
authFailures: new Map(),
cleanupTimer: null,
};
declare global {
var mcpRateLimit: RateLimitStore | undefined;
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const rateLimitStore = globalRateLimit.mcpRateLimit!;
const rateLimitStore = globalThis.mcpRateLimit ?? (globalThis.mcpRateLimit = {
authFailures: new Map(),
cleanupTimer: null,
});
const authFailures = rateLimitStore.authFailures;
if (!rateLimitStore.cleanupTimer) {
@@ -88,7 +87,7 @@ function normalizeSchema(schema: McpTool["inputSchema"]) {
function getTools() {
if (!toolsCache) {
toolsCache = extractToolsFromProcedures(mcpRouter).map((tool) => ({
toolsCache = extractMcpTools().map((tool) => ({
...tool,
inputSchema: normalizeSchema(tool.inputSchema),
}));
@@ -190,7 +189,6 @@ const mcpHandler = createMcpHandler(
}
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const procedure = tool.pathInRouter.reduce<any>((acc, part) => acc?.[part], caller);
if (typeof procedure !== "function") {
return {
@@ -218,7 +216,7 @@ const mcpHandler = createMcpHandler(
instructions: SERVER_INSTRUCTIONS,
serverInfo: {
name: "homarr",
version: packageJson.version,
version: getPackageVersion(),
},
},
{
@@ -0,0 +1,23 @@
import { extractToolsFromProcedures } from "trpc-to-mcp";
import { mcpRouter } from "@homarr/api/mcp";
let cache: ReturnType<typeof extractToolsFromProcedures> | null = null;
export function extractMcpTools() {
if (cache) return cache;
const originalWarn = console.warn;
console.warn = (...args: unknown[]) => {
if (typeof args[0] === "string" && args[0].includes("[TRPC-TO-MCP]")) return;
originalWarn.apply(console, args);
};
try {
cache = extractToolsFromProcedures(mcpRouter);
} finally {
console.warn = originalWarn;
}
return cache;
}