import { env } from "cloudflare:workers";
import { z } from "zod";
import { HttpError } from "../../errors";
import { randomToken, sha256Hex } from "../../crypto";
import { MergeError } from "../../pulls";
import { auditToolCall, limitToolCall } from "../audit";
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 { gitTools } from "./git";
import { issueTools } from "./issues";
import { pullTools } from "./pulls";
import { repoTools } from "./repos";
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,
...issueTools,
...ciTools,
...accessTools,
...coordinationTools,
];
const BY_NAME = new Map(TOOLS.map((tool) => [tool.name, tool]));
function jsonSchema(schema: z.ZodType): JsonObject {
const generated = z.toJSONSchema(schema, { target: "draft-2020-12", io: "input" });
const { $schema: _ignored, ...rest } = generated;
return rest;