import { env } from "../../../env.js";
import { Hono } from "hono";
import { StreamableHTTPTransport } from "@hono/mcp";
import { HeaderForwardingHook, InkeepAgentsCore, SDKHooks, createConsoleLogger, createMCPServer } from "@inkeep/agents-mcp";

//#region src/domains/mcp/routes/mcp.ts
const app = new Hono();
/**
* Headers to forward from incoming requests to downstream API calls.
* x-forwarded-cookie is mapped to cookie for browser compatibility
* (browsers don't allow setting Cookie header directly).
*/
const FORWARDED_HEADERS = [
	"x-forwarded-cookie",
	"authorization",
	"cookie"
];
app.all("/", async (c) => {
	const transport = new StreamableHTTPTransport();
	const noOpLogger = createConsoleLogger("error");
	const headersToForward = {};
	for (const headerName of FORWARDED_HEADERS) {
		const value = c.req.header(headerName);
		if (value) headersToForward[headerName] = value;
	}
	if (headersToForward["x-forwarded-cookie"] && !headersToForward.cookie) headersToForward.cookie = headersToForward["x-forwarded-cookie"];
	const createSDKWithHeaders = () => {
		const hooks = new SDKHooks();
		hooks.registerBeforeRequestHook(new HeaderForwardingHook(headersToForward));
		return new InkeepAgentsCore({
			serverURL: env.INKEEP_AGENTS_API_URL,
			hooks
		});
	};
	await createMCPServer({
		logger: noOpLogger,
		serverURL: env.INKEEP_AGENTS_API_URL,
		getSDK: createSDKWithHeaders
	}).server.connect(transport);
	return transport.handleRequest(c);
});
var mcp_default = app;

//#endregion
export { mcp_default as default };