UNPKG

trellis

Version:

Agentic State Engine — event-sourced causal graph with branching, decision traces, and realtime sync for AI-native applications

122 lines (120 loc) 3.34 kB
import { corsPreflightResponse, discoveryMcpGateway, mcpServiceDocument, oauthAuthorizationServerMetadata, oauthProtectedResourceMetadata, withCors } from "../chunk-WGKXHUAD.js"; import "../chunk-2NJRCGWJ.js"; import "../chunk-GA6RZXIK.js"; import "../chunk-YC5I32PS.js"; import "../chunk-UZRUP7QW.js"; import "../chunk-JA7AIHRK.js"; import "../chunk-SZ3VAB5P.js"; import "../chunk-VRFPXKFZ.js"; import "../chunk-PVOECISX.js"; import "../chunk-BYTAOXGW.js"; import "../chunk-G3XIHPSQ.js"; import "../chunk-LEGH72HW.js"; import "../chunk-RUMOVKR4.js"; import "../chunk-2ESYSVXG.js"; // src/mcp/gateway-serve.ts async function createGatewayFetchHandler(opts = {}) { const configDir = opts.configDir ?? "."; const discoveryCtx = { configDir }; return async (req) => { if (req.method === "OPTIONS") { return corsPreflightResponse(req); } const url = new URL(req.url); const path = url.pathname.replace(/\/$/, "") || "/"; const origin = opts.publicUrl?.replace(/\/$/, "") || `${url.protocol}//${url.host}`; const respond = (res) => withCors(req, res); if (path === "/health") { return respond( new Response( JSON.stringify({ status: "ok", service: "trellis-mcp-gateway", mcp: "/mcp", gateway: "/gateway/mcp" }), { headers: { "Content-Type": "application/json" } } ) ); } if (path === "/.well-known/oauth-protected-resource") { return respond( new Response(JSON.stringify(oauthProtectedResourceMetadata(origin)), { headers: { "Content-Type": "application/json" } }) ); } if (path === "/.well-known/oauth-authorization-server") { return respond( new Response(JSON.stringify(oauthAuthorizationServerMetadata(origin)), { headers: { "Content-Type": "application/json" } }) ); } if (path === "/mcp" || path === "/gateway/mcp") { const res = await discoveryMcpGateway.handle(req, { ...discoveryCtx, origin, gatewayPublicUrl: origin }); return respond(res); } if (path === "/") { return respond( new Response(JSON.stringify(mcpServiceDocument(origin, {})), { headers: { "Content-Type": "application/json" } }) ); } return respond( new Response(JSON.stringify({ error: "Not Found" }), { status: 404, headers: { "Content-Type": "application/json" } }) ); }; } async function startGatewayServer(opts = {}) { const port = opts.port ?? 3940; const fetch = await createGatewayFetchHandler(opts); if (typeof globalThis.Bun !== "undefined") { const server2 = globalThis.Bun.serve({ port, hostname: opts.hostname ?? "0.0.0.0", fetch }); return { port: server2.port ?? port, stop: () => server2.stop(true) }; } const { startNodeServer } = await import("../server/node-adapter.js"); const server = await startNodeServer({ port, hostname: opts.hostname, fetch, websocket: { open() { }, message() { }, close() { } } }); return { port: server.port, stop: () => server.stop(true) }; } export { createGatewayFetchHandler, startGatewayServer };