Files
MikrotikManager/backend/src/modules/mcp/control-lane.ts
T

27 lines
1.8 KiB
TypeScript

/** Caller passes the ACTUAL authorized registration descriptors, never caller
* output/state/id or an external list. Lane selection grants no authority:
* existing schema/auth/policy/runtime checks still run in the SDK callback.
*/
interface RegisteredOperation { id: string; toolName: string; effect: string }
const tools: Readonly<Record<string, { id: string; effect: string }>> = Object.freeze({
mm_capabilities_get: { id: "capabilities.read", effect: "read" },
mm_change_get: { id: "changes.read", effect: "read" },
mm_operation_get: { id: "operations.read", effect: "read" },
mm_server_probe_receipt_get: { id: "servers.probes.receipt.read", effect: "read" },
mm_change_cancel: { id: "changes.cancel", effect: "read" },
mm_server_probe_cancel: { id: "servers.probes.cancel", effect: "write" },
})
const protocols = new Set(["initialize", "ping", "tools/list", "notifications/initialized", "notifications/cancelled"])
export function classifyMcpLane(body: unknown, authorizedRegistrations: readonly RegisteredOperation[]): "control" | "normal" {
const item: unknown = Array.isArray(body) && body.length === 1 ? body[0] : body
if (!item || typeof item !== "object" || Array.isArray(item)) return "normal"
const request = item as { method?: unknown; params?: unknown }
if (typeof request.method !== "string") return "normal"
if (protocols.has(request.method)) return "control"
if (request.method !== "tools/call" || !request.params || typeof request.params !== "object") return "normal"
const name = (request.params as { name?: unknown }).name
if (typeof name !== "string" || !Object.hasOwn(tools, name)) return "normal"
const expected = tools[name]
return authorizedRegistrations.some(row => row.toolName === name && row.id === expected.id && row.effect === expected.effect) ? "control" : "normal"
}