import { env, waitUntil } from "cloudflare:workers";
import { z } from "zod";
import { HttpError } from "../../errors";
import { assertVerifiedForWrites } from "../../verified-writes";
import { randomToken, sha256Hex } from "../../crypto";
import { MergeError } from "../../pulls";
import { auditToolCall, limitToolCall } from "../audit";
import { recordToolUsage } from "../usage";
import { RPC, RpcError } from "../protocol";
import type { JsonObject, McpCaller } from "../protocol";
import { accessTools } from "./access";
import type { Tool, ToolContext, ToolResult } from "./base";
import { ToolError } from "./base";
import { ciTools } from "./ci";
import { coordinationTools } from "./coordination";
import { editorTools } from "./editor";
import { gitTools } from "./git";
import { issueTools } from "./issues";
import { pullTools } from "./pulls";
import { repoTools } from "./repos";
import { siteTools } from "./sites";
import { stackTools } from "./stacks";
const MAX_STRUCTURED_BYTES = 256 * 1024;
const REQUEST_STATE_TTL_SECONDS = 600;
const STATE_PREFIX = "mcp-mrtr:";
const DENIED_STATUSES = new Set([401, 403, 404]);
/** Limit errors thrown inside the coordination hub arrive as plain Errors over Durable Object RPC. */
const COORDINATION_LIMIT = /^Too many (active claims|claims held|subscribers)/;
const TOOLS: Tool[] = [
...repoTools,
...gitTools,
...pullTools,
...stackTools,
...issueTools,
...ciTools,
...accessTools,
...siteTools,
...coordinationTools,