import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
import { registerAllTools } from "./tools/registry.js";
import { Env } from "./types.js";

export async function handleMcpRequest(
  request: Request,
  env: Env,
  orgNamespace: string,
  partnerToken: string,
): Promise<Response> {
  const transport = new WebStandardStreamableHTTPServerTransport({ sessionIdGenerator: undefined });
  const server = new McpServer({ name: "arcrun-mcp-server", version: "1.0.0" });

  registerAllTools(server, env, orgNamespace, partnerToken);
  await server.connect(transport);

  return transport.handleRequest(request);
}
