@zuplo/runtime 6.62.5 → 6.62.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/out/esm/index.js CHANGED
@@ -197,7 +197,7 @@ data: ${o}
197
197
  `;break;case"id":c=h.includes("\0")?void 0:h;break;case"retry":/^\d+$/.test(h)?n(parseInt(h,10)):r(new ta(`Invalid \`retry\` value: "${h}"`,{type:"invalid-retry",value:h,line:x}));break;default:r(new ta(`Unknown field "${b.length>20?`${b.slice(0,20)}\u2026`:b}"`,{type:"unknown-field",field:b,value:h,line:x}));break}}s(m,"processField");function f(){u.length>0&&e({id:c,event:l||void 0,data:u.endsWith(`
198
198
  `)?u.slice(0,-1):u}),c=void 0,u="",l=""}s(f,"dispatchEvent");function g(b={}){i&&b.consume&&p(i),a=!0,c=void 0,u="",l="",i=""}return s(g,"reset"),{feed:d,reset:g}}s(iv,"createParser");function pE(t){let e=[],r="",n=0;for(;n<t.length;){let o=t.indexOf("\r",n),i=t.indexOf(`
199
199
  `,n),a=-1;if(o!==-1&&i!==-1?a=Math.min(o,i):o!==-1?o===t.length-1?a=-1:a=o:i!==-1&&(a=i),a===-1){r=t.slice(n);break}else{let c=t.slice(n,a);e.push(c),n=a+1,t[n-1]==="\r"&&t[n]===`
200
- `&&n++}}return[e,r]}s(pE,"splitLines");var ra=class extends TransformStream{static{s(this,"EventSourceParserStream")}constructor({onError:e,onRetry:r,onComment:n}={}){let o;super({start(i){o=iv({onEvent:s(a=>{i.enqueue(a)},"onEvent"),onError(a){e==="terminate"?i.error(a):typeof e=="function"&&e(a)},onRetry:r,onComment:n})},transform(i){o.feed(i)}})}};var na=class{static{s(this,"HTTPClientTransport")}url;timeout;headers;fetch;logger;sessionId;messageHandler;errorCallback;closeCallback;isConnected=!1;constructor(e){this.url=e.url,this.timeout=e.timeout||3e4,this.headers={"Content-Type":"application/json",Accept:"application/json, text/event-stream",...e.headers},this.fetch=e.fetch||globalThis.fetch,this.logger=e.logger||rr(),e.enableSessions&&this.logger.debug("Session support not yet implemented for HTTP client transport")}setHeaders(e){this.headers={...this.headers,...e}}async connect(){try{new URL(this.url),this.isConnected=!0,this.logger.info("HTTP Client Transport connected to:",this.url)}catch{let r=new Error(`Invalid URL: ${this.url}`);throw this.errorCallback&&this.errorCallback(r),r}}async send(e){if(!this.isConnected)throw new Error("Transport not connected. Call connect() first.");let r;try{let n={...this.headers};this.sessionId&&(n["Mcp-Session-Id"]=this.sessionId);let o=new AbortController;r=setTimeout(()=>o.abort(),this.timeout);let i=await this.fetch(this.url,{method:"POST",headers:{...this.headers,...this.sessionId&&{"Mcp-Session-Id":this.sessionId}},body:JSON.stringify(e),signal:o.signal});if(!i.ok)throw new Error(`HTTP ${i.status}: ${i.statusText}`);(i.headers.get("Content-Type")||"").includes("text/event-stream")?await this.handleSSEResponse(i):await this.handleJSONResponse(i)}catch(n){if(n instanceof Error&&n.name==="AbortError")throw this.errorCallback&&this.errorCallback(n),new Error(`Request timeout after ${this.timeout}ms`);let o=new Error(`A client error occurred: ${n}`);throw this.errorCallback&&this.errorCallback(o),o}finally{r&&clearTimeout(r)}}onMessage(e){this.messageHandler=e}async close(){this.logger.debug("Closing HTTP Client Transport"),this.isConnected=!1,this.messageHandler=void 0,this.closeCallback&&this.closeCallback(),this.logger.info("HTTP Client Transport closed")}onClose(e){this.closeCallback=e}onError(e){this.errorCallback=e}getSessionId(){return this.sessionId}setSessionId(e){this.sessionId=e,this.logger.debug("Session ID set:",e?"***":"undefined")}async handleJSONResponse(e){let r=await e.text();if(!r.trim()){this.logger.debug("Received empty response");return}let n;try{n=JSON.parse(r)}catch{throw new Error(`Invalid JSON response: ${r}`)}this.logger.debug("Received JSON-RPC response:",n),this.messageHandler&&await this.messageHandler(n)}async handleSSEResponse(e){if(!e.body)throw new Error("SSE response has no body");this.logger.debug("Handling SSE response");let r=s(o=>{if(!o.event||o.event==="message")try{let i=JSON.parse(o.data);this.logger.debug("Received SSE message:",i),this.messageHandler&&this.messageHandler(i)}catch(i){this.logger.warn("Failed to parse SSE message data:",o.data,i)}},"onEvent"),n=e.body.pipeThrough(new TextDecoderStream).pipeThrough(new ra).getReader();try{for(;;){let{done:o,value:i}=await n.read();if(o)return;r(i)}}catch(o){throw this.logger.error("Error processing SSE stream:",o),new Error(`SSE stream error: ${o}`)}}};var mE="MCP Client",fE="0.0.0",oa=class{static{s(this,"MCPClient")}name;version;capabilities;transport;isInitialized=!1;protocolVersion;logger;requestId=1;transportOptions;constructor(e={}){this.name=e.name||mE,this.version=e.version||fE,this.logger=e.logger||rr(),this.transportOptions=e.transportOptions||{},this.capabilities={experimental:{},sampling:{},...e.capabilities}}async connect(e){this.transportOptions.headers&&e.setHeaders(this.transportOptions.headers),this.transport=e,await e.connect()}async initialize(e=Do){if(!this.transport)throw new Error("No transport connected. Call connect() first.");let r=Ir({id:this.requestId,method:"initialize",params:{protocolVersion:e,capabilities:this.capabilities,clientInfo:{name:this.name,version:this.version}}}),n=await this.sendRequest(r);if(tr(n))throw new Error(`Initialization failed: ${n.error.message}`);let o=n.result;return this.isInitialized=!0,this.protocolVersion=o.protocolVersion,this.logger.info("Successfully initialized MCP client",{serverInfo:o,protocolVersion:this.protocolVersion}),o}async ping(){if(!this.isInitialized)throw new Error("Client not initialized. Call initialize() first.");let e=Ir({id:this.requestId,method:"ping"}),r=await this.sendRequest(e);if(tr(r))throw new Error(`Ping failed: ${r.error.message}`)}async listTools(){if(!this.isInitialized)throw new Error("Client not initialized. Call initialize() first.");let e=Ir({id:this.requestId,method:"tools/list",params:{}}),r=await this.sendRequest(e);if(tr(r))throw new Error(`Failed to list tools: ${r.error.message}`);return r.result.tools}async callTool(e,r={}){if(!this.isInitialized)throw new Error("Client not initialized. Call initialize() first.");let n=Ir({id:this.requestId,method:"tools/call",params:{name:e,arguments:r}}),o=await this.sendRequest(n);if(tr(o))throw new Error(`Failed to call tool '${e}': ${o.error.message}`);return o.result}async listPrompts(){if(!this.isInitialized)throw new Error("Client not initialized. Call initialize() first.");let e=Ir({id:this.requestId,method:"prompts/list",params:{}}),r=await this.sendRequest(e);if(tr(r))throw new Error(`Failed to list prompts: ${r.error.message}`);return r.result.prompts}async getPrompt(e,r={}){if(!this.isInitialized)throw new Error("Client not initialized. Call initialize() first.");let n=Ir({id:this.requestId,method:"prompts/get",params:{name:e,arguments:r}}),o=await this.sendRequest(n);if(tr(o))throw new Error(`Failed to get prompt '${e}': ${o.error.message}`);return o.result}async disconnect(){this.transport&&(await this.transport.close(),this.transport=void 0),this.isInitialized=!1,this.protocolVersion=void 0}async sendRequest(e){return new Promise((r,n)=>{let o=e.id;this.transport?.onMessage(async i=>((nn(i)||tr(i))&&i.id===o&&r(i),null)),this.transport?.send(e).catch(n)})}};var ia=class{constructor(e,r,n=!1){this.baseUrl=e;this.originToolName=r;this.debugMode=n}static{s(this,"ProxyMcpToolExecutor")}async execute(e){let r=ur(),n=Date.now(),o=new oa({name:"Zuplo MCP Gateway Proxy Client",version:"0.0.1"});try{this.debugMode&&r.log.debug("Proxying MCP tool call",{originToolName:this.originToolName,baseUrl:this.baseUrl,args:e});let i=new na({url:this.baseUrl});i.setHeaders({"Content-Type":"application/json",...ea()});let a,c=Date.now();try{await o.connect(i),await o.initialize(),a=await o.callTool(this.originToolName,e)}catch(l){throw new Error(`could not call tool: ${this.originToolName}`,l)}let u=Date.now()-c;return this.debugMode&&r.log.debug("Proxy MCP response received",{originToolName:this.originToolName,upstreamElapsedMs:u,proxyResult:a}),a}catch(i){let a=Date.now()-n,c=i instanceof Error?`${i.name}: ${i.message}`:String(i);return r.log.error("Proxy MCP tool execution failed",{originToolName:this.originToolName,baseUrl:this.baseUrl,totalElapsedMs:a,err:i}),{content:[{type:"text",text:`Tool execution failed for '${this.originToolName}': ${c}`}],isError:!0}}finally{await o.disconnect()}}};var cn=class t{static{s(this,"ZuploMcpServer")}static instances=new Map;static async getInstance({opts:e,context:r,key:n,origins:o}){let i=e.debugMode??!1,a=t.instances.get(n);if(a)i&&r.log.debug("MCP Server warm reuse",{routeKey:n});else{let c=Date.now();if(a=new t(e,r,o??[]),a.registerToolsFromOptions(a.opts.openApiTools??[]),a.registerToolsFromFiles(a.opts.openApiFilePaths??[]),a.registerToolsFromFileSources(a.opts.files??[]),a.registerPromptsFromFileSources(a.opts.prompts??[]),a.registerResourcesFromFileSources(a.opts.resources??[]),await a.registerProxyTools(a.origins??[]),await a.transport.connect(),a.server.withTransport(a.transport),t.instances.set(n,a),i){let u=a.server.getTools(),l=u?u.size:0,d=a.server.getPromptDefinitions(),p=d?d.length:0,m=a.server.getResourceDefinitions(),f=m?m.length:0,g=Date.now()-c;r.log.debug("MCP Server cold start",{routeKey:n,toolCount:l,promptCount:p,resourceCount:f,totalElapsedMs:g,debugMode:i,includeOutputSchema:a.includeOutputSchema,includeStructuredContent:a.includeStructuredContent})}}return a}transport;server;opts;origins;context;includeOutputSchema;includeStructuredContent;debugMode;constructor(e,r,n){this.debugMode=e.debugMode??!1;let o;this.debugMode?o=r.log:o=new Mo,this.context=r,this.opts=e,this.origins=n??[],this.server=new Fs({name:e.name??"Zuplo MCP Server",version:e.version??"0.0.0",logger:o}),this.transport=new Hs({logger:o}),this.includeOutputSchema=e.includeOutputSchema??!1,this.includeStructuredContent=e.includeStructuredContent??!1}async handleRequest(e,r){let n=Date.now();try{let o=await e.clone().json().catch(()=>({}));if(this.debugMode&&r.log.debug("MCP Server request start",{method:o.method,requestId:o.id,routePath:r.route?.path}),this.debugMode&&o.method==="tools/list"){let c=this.server.getTools(),u=c?Array.from(c.keys()):[];r.log.debug("MCP Server list tools request",{toolCount:u.length,toolNames:u})}if(this.debugMode&&o.method==="prompts/list"){let c=this.server.getPromptDefinitions(),u=c?c.map(l=>l.name):[];r.log.debug("MCP Server list prompts request",{promptCount:u.length,promptNames:u})}if(this.debugMode&&o.method==="resources/list"){let c=this.server.getResourceDefinitions(),u=c?c.map(l=>l.name):[];r.log.debug("MCP Server list resources request",{resourceCount:u.length,resourceNames:u})}let i=await this.transport.handleRequest(e),a=Date.now()-n;if(this.debugMode){let c=await i.clone().json().catch(()=>({}));r.log.debug("MCP Server response complete",{status:i.status,requestMethod:o.method,requestId:o.id,totalElapsedMs:a,respData:c})}return i}catch(o){let i=Date.now()-n;return r.log.error("MCP server internal error",{elapsedMs:i,err:o}),new Response("Internal error",{status:500})}}generatePromptDescriptionFromSpec(e){let r=e.raw();return r?.description?r.description:r?.summary?r.summary:`Generate content for operation for ${e.path}`}generateToolNameFromSpec(e,r){let n=e.raw();if(n?.operationId)return`${n.operationId}`;let o=`${r}_${e.path}`.replace(/[^\w]/g,"_");return this.context.log.warn(`No operationId found for route ${r} ${e.path}. Using auto-generated name "${o}". To improve AI tool selection, add a descriptive operationId to your OpenAPI spec (e.g., "get_user_by_id" instead of "GET_/users/{id}")`),o}generateToolDescriptionFromSpec(e,r){let n=e.raw();return n?.description?n.description:n?.summary?n.summary:`Call ${r.toUpperCase()} ${e.path}`}registerToolsFromOptions(e){let r=new Set;for(let n of e){if(this.context.log.warn(`DEPRECATED: Registering MCP tool with name: "${n.name}", operationId: "${n.operationId}", method: "${n.method}" from "options.openApiTools" will soon be deprecated: migrate to using "options.files" configurations`),n.name){if(typeof n.name!="string"||n.name.trim()==="")throw new Error("MCP Tool configuration error: Tool name must be a non-empty string if provided.");if(r.has(n.name))throw new Error(`MCP Tool configuration error: Duplicate tool name "${n.name}". Tool names must be unique across all configured tools.`)}let o=this.getRouteDataForOptions(n);if(!o){let l=n.operationId?`operationId: "${n.operationId}"`:`path: "${n.path}"`;throw new Error(`MCP Tool configuration error: Could not find gateway route data for ${n.method.toUpperCase()} ${l}. Verify that the route exists in your OpenAPI specification and that the provided metadata matches OpenAPI specification data.`)}if(!o.handler){let l=n.operationId?`operationId: "${n.operationId}"`:`path: "${n.path}"`;throw new Error(`MCP Tool configuration error: Route ${n.method.toUpperCase()} ${l} has no handler configured. Ensure the route has a proper handler defined in your OpenAPI specification.`)}let i=n.name??this.generateToolNameFromSpec(o,n.method);if(r.has(i))throw new Error(`MCP Tool configuration error: Tool name conflict detected. The name "${i}" is already in use. Consider providing a unique 'name' in your tool configuration or ensure your operationIds are unique.`);r.add(i);let a=n.description??this.generateToolDescriptionFromSpec(o,n.method),c=n.includeOutputSchema??this.includeOutputSchema??!0,u=n.includeStructuredContent??this.includeStructuredContent??!0;this.registerToolsForMethod(o,n.method,i,a,c,u)}}static getOperationsMetadataForFile(e){let{routes:r}=we.instance.routeData,o=r.find(u=>u.metadata?.filepath)?.metadata?.filepath?.includes("\\")?"\\":"/",i=e.split("/").join(o),a=r.filter(u=>u.metadata?.filepath===i);if(a.length===0)throw new Error(`MCP Tool configuration error: No routes found for file path ${e}. Verify that the OpenAPI file exists and is properly loaded in your Gateway configuration with routes.`);let c=new Map;for(let u of a){let l=u?.raw().operationId;if(l){c.set(l,{routeConfig:u});let d=u?.raw()["x-zuplo-mcp-tool"],p=u?.raw()["x-zuplo-mcp-prompt"],m=u?.raw()["x-zuplo-mcp-graphql"],f=u?.raw()["x-zuplo-mcp-resource"];(d||p||m||f)&&c.set(l,{routeConfig:u,toolExtension:d||void 0,promptExtension:p||void 0,graphqlExtension:m||void 0,resourceExtension:f||void 0})}}return c}registerPromptsFromFileSources(e){for(let r of e){let n=t.getOperationsMetadataForFile(r.path),o=new Set;for(let i of r.operationIds){let a=n.get(i);if(!a)throw new Error(`MCP Prompt configuration error: Could not find operation with ID "${i}" in file ${r.path}. Verify that the operation ID exists in your OpenAPI specification.`);let{routeConfig:c,promptExtension:u}=a,l=u?.name??i;if(o.has(l))throw new Error(`MCP Prompt configuration error: Duplicate prompt name "${l}". Prompt names must be unique across all configured prompts.`);o.add(l);let d=u?.description??this.generatePromptDescriptionFromSpec(c);if(!(u?.enabled??!0)){this.debugMode&&this.context.log.debug("MCP prompt disabled by extension",{promptName:l,operationId:i});continue}this.registerPromptForOperation(l,d,c,i)}}}registerPromptForOperation(e,r,n,o){try{if(n.handler?.export==="mcpServerHandler")return;let a=Cr.createPromptValidatorBuilder(n).build();this.server.addPrompt({name:e,description:r,validator:a,generator:s(async c=>{let u=ur(),l=Date.now();try{this.debugMode&&u.log.debug("MCP prompt invoked",{promptName:e,operationId:n.raw()?.operationId,path:n.path,args:c});let d=this.buildToolUrl(n,{body:c}),p=this.buildToolRequest("POST","application/json",{body:c});this.debugMode&&u.log.debug("MCP prompt downstream call",{promptName:e,url:d,method:"POST",bodyPreview:c});let m=Date.now(),f=await u.invokeRoute(d,p),g=Date.now()-m,b=await f.json();if(!f.ok)throw new Error(`Route returned ${f.status}: ${b}`);let h;try{if(h=b.messages||b,!Array.isArray(h))throw new Error("Response must contain a 'messages' array or be an array of messages")}catch(v){throw new Error(`Invalid JSON response or missing messages array: ${v instanceof Error?v.message:String(v)}`)}let x=Date.now()-l;return this.debugMode&&u.log.debug("MCP prompt response complete",{promptName:e,operationId:n.raw()?.operationId,status:f.status,elapsedMs:x,downstreamElapsedMs:g,messageCount:h.length}),h}catch(d){let p=Date.now()-l,m=d instanceof Error?`${d.name}: ${d.message}`:String(d);throw u.log.error("MCP prompt invocation failed",{promptName:e,operationId:n.raw()?.operationId,path:n.path,elapsedMs:p,err:d}),new Error(`MCP tool call failed for tool '${e}': ${m}`)}},"generator")}),this.debugMode&&this.context.log.debug("MCP prompt registered from file source",{promptName:e,operationId:o,path:n.path})}catch(i){throw this.context.log.error("Failed to register MCP prompt",{promptName:e,operationId:o,error:i instanceof Error?i.message:String(i)}),new Error(`Failed to register prompt "${e}" for operation "${o}": ${i instanceof Error?i.message:String(i)}`)}}registerResourcesFromFileSources(e){let r=new Set;for(let n of e){let o=t.getOperationsMetadataForFile(n.path);for(let i of n.operationIds){let a=o.get(i);if(!a)throw new Error(`MCP Resource configuration error: Could not find operation with ID "${i}" in file ${n.path}. Verify that the operation ID exists in your OpenAPI specification.`);let{routeConfig:c,resourceExtension:u}=a;if(!(u?.enabled??!0)){this.debugMode&&this.context.log.debug("MCP resource disabled by extension",{operationId:i});continue}if(!c.methods||c.methods.length===0)throw new Error(`MCP Resource configuration error: Operation "${i}" has no HTTP methods defined. Resources must use GET method.`);if(c.methods.length>1)throw new Error(`MCP Resource configuration error: Operation "${i}" has multiple HTTP methods (${c.methods.join(", ")}). Resources must use exactly one method and it must be GET.`);let d=c.methods[0].toUpperCase();if(d!=="GET")throw new Error(`MCP Resource configuration error: Operation "${i}" uses ${d} method. Resources are read-only and must use GET method only.`);let p=u?.name?.trim()||i.trim()||"";if(!p)throw new Error(`MCP Resource configuration error: Could not determine resource name for operation "${i}". Ensure the operation has an operationId or provide a name in x-zuplo-mcp-resource extension.`);if(r.has(p))throw new Error(`MCP Resource configuration error: Duplicate resource name "${p}". Resource names must be unique across all configured resources.`);r.add(p);let m=u?.description?.trim()||c.raw()?.description?.trim()||c.raw()?.summary?.trim()||`Resource for ${p}`,f=u?.uri?.trim()||`mcp://resources/${encodeURIComponent(p)}`,g=u?.mimeType?.trim()||"text/plain";this.registerResourceForOperation(p,f,m,c,i,g)}}}registerResourceForOperation(e,r,n,o,i,a){try{if(o.handler?.export==="mcpServerHandler")return;this.server.addResource(e,r,{title:e,description:n,mimeType:a},async()=>{let c=ur(),u=Date.now();try{this.debugMode&&c.log.debug("MCP resource invoked",{resourceName:e,operationId:o.raw()?.operationId,path:o.path,uri:r});let l=this.buildToolUrl(o,{}),d=this.buildToolRequest("GET",null,{});this.debugMode&&c.log.debug("MCP resource downstream call",{resourceName:e,url:l,method:"GET"});let p=Date.now(),m=await c.invokeRoute(l,d),f=Date.now()-p,g=await m.text(),b=Date.now()-u;return this.debugMode&&c.log.debug("MCP resource response complete",{resourceName:e,operationId:o.raw()?.operationId,status:m.status,elapsedMs:b,downstreamElapsedMs:f,declaredMimeType:a,contentLength:g.length}),{contents:[{uri:r,declaredMimeType:a,text:g}]}}catch(l){let d=Date.now()-u,p=l instanceof Error?`${l.name}: ${l.message}`:String(l);throw c.log.error("MCP resource invocation failed",{resourceName:e,operationId:o.raw()?.operationId,path:o.path,elapsedMs:d,err:l}),new Error(`MCP resource call failed for resource '${e}': ${p}`)}}),this.debugMode&&this.context.log.debug("MCP resource registered from file source",{resourceName:e,operationId:i,path:o.path,uri:r,mimeType:a||"auto-detected"})}catch(c){throw this.context.log.error("Failed to register MCP resource",{resourceName:e,operationId:i,error:c instanceof Error?c.message:String(c)}),new Error(`Failed to register resource "${e}" for operation "${i}": ${c instanceof Error?c.message:String(c)}`)}}registerToolsFromFileSources(e){let r=new Set;for(let n of e){let o=t.getOperationsMetadataForFile(n.path);this.debugMode&&this.context.log.debug("Processing file source",{path:n.path,operationIds:n.operationIds,availableOperationIds:Array.from(o.keys())});for(let i of n.operationIds){let a=o.get(i);if(!a)throw new Error(`MCP Tool configuration error: Operation ID "${i}" not found in OpenAPI spec at ${n.path}. Available operation IDs: ${Array.from(o.keys()).join(", ")}`);if(a.toolExtension?.enabled===!1){this.debugMode&&this.context.log.debug("Skipping disabled tool",{operationId:i,path:n.path});continue}let c=a.toolExtension?.name?.trim()??i,u=(a.toolExtension?.description?.trim()||null)??(a.routeConfig.raw().description?.trim()||null)??(a.routeConfig.raw().summary?.trim()||null)??`Executes tool: ${c}`;if(!u)throw new Error(`MCP Tool configuration error: no tool description found for operationId: "${i}" in file "${n.path}"`);if(r.has(c))throw new Error(`MCP Tool configuration error: Tool name conflict detected. The name "${c}" is already in use. Consider providing a unique 'name' in the x-zuplo-mcp-tool extension for operation "${i}".`);r.add(c);let l=a.toolExtension?.includeOutputSchema??this.includeOutputSchema??!1,d=a.toolExtension?.includeStructuredContent??this.includeStructuredContent??!1;if(a.routeConfig.methods.length!=1)throw new Error(`MCP tool configuration error: multiple methods "${a.routeConfig.methods.join(", ")}" on operation "${i}" not permitted. Only tools with one singular method is permitted.`);a.graphqlExtension?(a.graphqlExtension.enabled??!0)&&this.registerGraphQLTools(i,a.routeConfig,a.graphqlExtension,r):this.registerToolsForMethod(a.routeConfig,a.routeConfig.methods[0],c,u,l,d)}}}registerGraphQLTools(e,r,n,o){let i=n.introspectionToolName??`${e}_introspect`,a=n.executeToolName??`${e}_execute_query`;if(o.has(i))throw new Error(`MCP GraphQL tool configuration error: Tool name conflict detected. The introspection tool name "${i}" is already in use. Provide a unique 'introspectionToolName' in the x-zuplo-graphql extension.`);if(o.has(a))throw new Error(`MCP GraphQL tool configuration error: Tool name conflict detected. The execute tool name "${a}" is already in use. Provide a unique 'executeToolName' in the x-zuplo-graphql extension.`);o.add(i),o.add(a);let c=n.introspectionToolDescription??`Get the GraphQL schema for ${e}`,u=n.executeToolDescription??`Execute a GraphQL query on ${e}`,l=r.path,p=Cr.createObjectValidatorBuilder({}).build();this.server.addTool({name:i,description:c,validator:p,handler:s(async()=>{let f=ur(),g=Date.now();try{this.debugMode&&f.log.debug("MCP GraphQL introspection tool invoked",{toolName:i,operationId:e,path:l});let b=Gs(),h=this.buildToolUrl(r,{}),x=this.buildToolRequest("POST","application/json",{body:{query:b}}),v=Date.now(),C=await f.invokeRoute(h,x),A=Date.now()-v,O=await C.text(),S=Date.now()-g;this.debugMode&&(C.ok?f.log.debug("MCP GraphQL introspection tool ok response",{toolName:i,operationId:e,status:C.status,elapsedMs:S,downstreamElapsedMs:A}):f.log.debug("MCP GraphQL introspection tool 'isError' response",{toolName:i,operationId:e,status:C.status,elapsedMs:S,downstreamElapsedMs:A,response:O}));let M;try{M=JSON.parse(O)}catch(z){this.debugMode&&f.log.debug("MCP GraphQL introspection structuredContent JSON parse failed",{toolName:i,body:O,parseErr:z})}return{content:[{type:"text",text:O}],...M&&{structuredContent:M},isError:!C.ok}}catch(b){let h=Date.now()-g,x=b instanceof Error?`${b.name}: ${b.message}`:String(b);throw f.log.error("MCP GraphQL introspection tool invocation failed",{toolName:i,operationId:e,path:l,elapsedMs:h,err:b}),new Error(`MCP GraphQL introspection tool call failed for '${i}': ${x}`)}},"handler")});let m={type:"object",properties:{query:{type:"string",description:"The GraphQL query to execute"},variables:{type:"object",description:"Optional variables for the GraphQL query"}},required:["query"],additionalProperties:!1};this.server.addTool({name:a,description:u,validator:new sr(m,f=>typeof f=="object"&&f!==null&&"query"in f&&typeof f?.query=="string"?{success:!0,data:f,errorData:null}:{success:!1,data:null,errorMessage:"Invalid input: query field is required and must be a string",errorData:null}),handler:s(async f=>{let g=ur(),b=Date.now();try{this.debugMode&&g.log.debug("MCP GraphQL execute tool invoked",{toolName:a,operationId:e,path:l,queryPreview:f.query.substring(0,100)});let h=this.buildToolUrl(r,{}),x={query:f.query};f.variables&&(x.variables=f.variables);let v=this.buildToolRequest("POST","application/json",{body:x}),C=Date.now(),A=await g.invokeRoute(h,v),O=Date.now()-C,S=await A.text(),M=Date.now()-b;this.debugMode&&(A.ok?g.log.debug("MCP GraphQL execute tool ok response",{toolName:a,operationId:e,status:A.status,elapsedMs:M,downstreamElapsedMs:O}):g.log.debug("MCP GraphQL execute tool 'isError' response",{toolName:a,operationId:e,status:A.status,elapsedMs:M,downstreamElapsedMs:O,response:S}));let z;try{z=JSON.parse(S)}catch(Z){this.debugMode&&g.log.debug("MCP GraphQL execute structuredContent JSON parse failed",{toolName:a,body:S,parseErr:Z})}return{content:[{type:"text",text:S}],...z&&{structuredContent:z},isError:!A.ok}}catch(h){let x=Date.now()-b,v=h instanceof Error?`${h.name}: ${h.message}`:String(h);throw g.log.error("MCP GraphQL execute tool invocation failed",{toolName:a,operationId:e,path:l,elapsedMs:x,err:h}),new Error(`MCP GraphQL execute tool call failed for '${a}': ${v}`)}},"handler")}),this.debugMode&&this.context.log.debug("MCP GraphQL tools registered",{operationId:e,introspectionToolName:i,executeToolName:a,path:l})}registerToolsFromFiles(e){let r=new Set(e.map(i=>i.filePath)),n=new Set;for(let i of we.instance.routeData.routes){let a=i.metadata?.filepath;a&&r.has(a)&&(this.context.log.warn(`DEPRECATED: Registering MCP tool with operationId: "${i.raw()?.operationId}"from file: "${a}" with "options.openApiFilePaths" will soon be removed: migrate to using "options.files" configurations`),n.add(a),this.registerToolsForRawRoutedata(i,this.includeOutputSchema,this.includeStructuredContent))}let o=e.map(i=>i.filePath).filter(i=>!n.has(i));if(o.length>0)throw new Error(`MCP Tool configuration error: Could not find routes for the following file paths: ${o.join(", ")}. Verify that these OpenAPI files exist and are properly loaded in your Gateway configuration.`)}registerToolsForRawRoutedata(e,r,n){if(e.mcp?.enabled!==!1){if(!e)throw new Error("MCP Tool configuration error: Route data cannot be null or undefined.");if(!e.methods||e.methods.length===0)throw new Error(`MCP Tool configuration error: Route "${e.path}" has no HTTP methods defined. Ensure the route has at least one HTTP method (GET, POST, etc.) in your OpenAPI specification.`);if(!e.handler)throw new Error(`MCP Tool configuration error: Route "${e.path}" has no handler configured. Ensure the route has a proper handler defined in your OpenAPI specification.`);for(let o of e.methods){let i=this.generateToolNameFromSpec(e,o),a=this.generateToolDescriptionFromSpec(e,o);this.registerToolsForMethod(e,o,i,a,r,n)}}}registerToolsForMethod(e,r,n,o,i,a){if(e.handler?.export!=="mcpServerHandler")if(e.raw().requestBody?.content)for(let c of Object.keys(e.raw().requestBody?.content)){let l=Object.keys(e.raw().requestBody?.content).length>1?`${n}_${c.replace(/[^\w]/g,"_")}`:n;this.registerGenericToolSpec(l,o,e,r,c,i,a)}else this.registerGenericToolSpec(n,o,e,r,null,i,a)}registerGenericToolSpec(e,r,n,o,i,a,c){let u=this.buildUniversalValidator(n,i);if(!u)throw new Error(`MCP Tool registration error: Could not build parameter validator for ${o.toUpperCase()} ${n.path}. This may indicate an issue with the OpenAPI parameter definitions for this route.`);let l=a?this.extractOutputSchema(n):void 0;try{this.server.addTool({name:e,description:r,validator:u,...l&&{outputSchema:l},handler:s(async d=>{let p=ur(),m=Date.now();try{this.debugMode&&p.log.debug("MCP tool invoked",{toolName:e,operationId:n.raw()?.operationId,method:o,path:n.path,args:d});let f=this.buildToolUrl(n,d),g=this.buildToolRequest(o,i,d);this.debugMode&&p.log.debug("MCP tool downstream call",{toolName:e,url:f,method:o,headers:g.headers?Object.keys(g.headers).join(", "):"",bodyPreview:d.body}),p.analyticsContext.addAnalyticsEvent(1,Oe.MCP_TOOL_USAGE,{toolName:e,toolPath:n.path,toolMethod:o,toolOperationId:n.raw()?.operationId??"unknown"});let b=Date.now(),h=await p.invokeRoute(f,g),x=Date.now()-b,v=await h.text(),C;if(c)try{C=JSON.parse(v)}catch(O){this.debugMode&&p.log.debug("MCP tool structuredContent JSON parse failed",{toolName:e,body:v,parseErr:O})}let A=Date.now()-m;return this.debugMode&&(h.ok?p.log.debug("MCP tool ok response",{toolName:e,operationId:n.raw()?.operationId,status:h.status,elapsedMs:A,downstreamElapsedMs:x}):p.log.debug("MCP tool 'isError' response",{toolName:e,operationId:n.raw()?.operationId,status:h.status,elapsedMs:A,downstreamElapsedMs:x,response:v})),{content:[{type:"text",text:v}],...C&&{structuredContent:C},isError:!h.ok}}catch(f){let g=Date.now()-m,b=f instanceof Error?`${f.name}: ${f.message}`:String(f);return p.log.error("MCP server tool invocation failed",{toolName:e,operationId:n.raw()?.operationId,method:o,path:n.path,elapsedMs:g,err:f}),{content:[{type:"text",text:`MCP tool call failed for tool '${e}': ${b}`}],isError:!0}}},"handler")}),this.debugMode&&this.context.log.debug("MCP tool registered",{toolName:e,method:o.toUpperCase(),path:n.path,operationId:n.raw()?.operationId,contentType:i||"none",includeOutputSchema:a,includeStructuredContent:c,hasValidator:!!u,hasOutputSchema:!!l})}catch(d){throw new Error(`Failed to add tool ${o} ${n.path}`,d)}}async registerProxyTools(e){let r=new Set;for(let n of e){if(n.enabled===!1){this.debugMode&&this.context.log.debug("Skipping disabled origin",{origin:n.name});continue}let o=n.toolNamePrefix??"";this.debugMode&&this.context.log.debug("Registering origin tools",{origin:n.name,baseUrl:n.baseUrl,toolCount:n.tools.length,toolNamePrefix:o||"none",tools:n.tools});for(let i of n.tools){let a=`${o}${i.name}`;if(r.has(a))throw new Error(`MCP origin configuration error: Tool name conflict detected. The name "${a}" is already in use. Consider using different tool names or toolNamePrefix for origin "${n.name}".`);r.add(a);let u=Cr.createObjectValidatorBuilder(i.inputSchema).build();try{let l=new ia(n.baseUrl,i.name,this.debugMode);this.server.addTool({name:a,description:i.description,validator:u,handler:s(async d=>await l.execute(d),"handler")}),this.debugMode&&this.context.log.debug("origin tool registered",{localToolName:a,originToolName:i.name,origin:n.name,baseUrl:n.baseUrl})}catch(l){throw new Error(`Failed to register origin tool ${a} from ${n.name}`,{cause:l})}}}this.debugMode&&e.length>0&&this.context.log.debug("origin tool registration complete",{totaloOigins:e.length,totalOriginTools:r.size,originToolNames:Array.from(r)})}buildToolUrl(e,r){return new Js(e).build(r.pathParams,r.queryParams)}buildToolRequest(e,r,n){let o=this.buildToolHeaders(e,r,n),i={method:e,headers:o};return this.shouldIncludeBody(e,r,n)&&(i.body=this.serializeBody(n.body,r)),i}buildToolHeaders(e,r,n){let o={...ea()};return n.headers&&Object.entries(n.headers).forEach(([i,a])=>{o[i.toLowerCase()]=a}),r&&n.body!==void 0&&(o["content-type"]=r),this.isBodyMethod(e||"")&&n.body!==void 0&&!o["content-type"]&&(o["content-type"]="application/json"),o}shouldIncludeBody(e,r,n){return n.body===void 0?!1:this.isBodyMethod(e)?!0:r!==null}isBodyMethod(e){return["POST","PUT","PATCH"].includes(e.toUpperCase())}serializeBody(e,r){return JSON.stringify(e)}buildUniversalValidator(e,r){try{let o=Cr.createValidatorBuilder(e,r).build();return this.debugMode&&this.context.log.debug("MCP validator built successfully",{method:e.methods?.[0],path:e.path,contentType:r||"none",operationId:e.raw()?.operationId}),o}catch(n){let o=n instanceof Error?n.message:String(n);throw this.context.log.warn("MCP validator build failed",{method:e.methods?.[0],path:e.path,contentType:r||"none",operationId:e.raw()?.operationId,error:o}),n}}getRouteDataForOptions(e){let{routes:r}=we.instance.routeData,n=!!e.operationId,o=!!e.path;if(!n&&!o)throw new Error("MCP Tool configuration error: Either 'operationId' or 'path' must be provided in your tool specification.");return n?r.find(i=>i?.raw().operationId===e.operationId&&i?.methods?.some(a=>a.toUpperCase()===e.method.toUpperCase())):r.find(i=>i?.path===e.path&&i?.methods?.some(a=>a.toUpperCase()===e.method.toUpperCase()))}extractOutputSchema(e){try{let r=e.raw(),n=r?.responses;if(!n){this.context.log.warn("No responses found in OpenAPI spec for outputSchema",{path:e.path,operationId:r?.operationId});return}for(let o of Object.keys(n))if(o.startsWith("2")){let a=n[o]?.content;if(a){for(let c of["application/json","application/json; charset=utf-8"])if(a[c]?.schema){let u=a[c].schema;return this.debugMode&&this.context.log.debug("Output schema extracted successfully",{path:e.path,operationId:r?.operationId,statusCode:o,contentType:c,schemaKeys:u&&typeof u=="object"?Object.keys(u).join(", "):"none"}),u}}}this.debugMode&&this.context.log.debug("No OpenAPI spec 2xx responses fond for outputSchema",{path:e.path,operationId:r?.operationId,statusCodes:Object.keys(n).join(", ")});return}catch(r){this.context.log.warn("Failed to extract outputSchema",{path:e.path,operationId:e.raw()?.operationId,error:r instanceof Error?r.message:String(r),stack:r instanceof Error?r.stack:void 0});return}}};async function gE(t,e){if(R("handler.mcp-server"),e.route.methods.some(a=>a.toUpperCase()!=="POST"))throw new w(`Invalid route config: mcpServerHandler may only use POST. Route '${e.route.path}' declares methods: [${e.route.methods.join(", ")}]`);let r={};t.headers.forEach((a,c)=>{r[c]=a});let n=e.route?.handler?.options??{},o=e.route?.path??"unknown-route",i=await cn.getInstance({opts:n,context:e,key:o});return Ys(r,e,()=>i.handleRequest(t,e))}s(gE,"mcpServerHandler");var sv=10,av=3e4,Er=class{static{s(this,"BackgroundLoader")}#e=new Map;#t;#n;#r;#o;#i={};constructor(e,r){if(typeof r=="number"){let n=r;this.#n=n*1e3,this.#o=av,this.#r=sv}else{let n=r;this.#n=n.ttlSeconds*1e3,this.#o=n.loaderTimeoutSeconds?n.loaderTimeoutSeconds*1e3:av,this.#r=sv}this.#t=e}#s(e){return e.expiry<=new Date}#c(e){let r=this.#i[e];return!(r===void 0||r===0)}#u(e){let r=this.#e.get(e);if(r&&!this.#s(r))return r.data}async get(e){let r=this.#u(e);if(r)return this.#l(e),r;if(this.#c(e))try{await hE(()=>this.#u(e)!==void 0||!this.#c(e),this.#o+this.#r+1,this.#r);let n=this.#u(e);if(n)return n}catch{}return this.#a(e)}#l(e){if(!this.#c(e)){let r=this.#a(e);Jt().waitUntil(r)}}async#a(e){try{this.#i[e]===void 0&&(this.#i[e]=0),this.#i[e]++;let r=await Promise.race([this.#t(e),scheduler.wait(this.#o)]);if(r===void 0)throw new w(`BackgroundLoader: Loader timed out after ${this.#o} ms.`);return this.#e.set(e,{data:r,expiry:new Date(Date.now()+this.#n)}),r}finally{this.#i[e]--}}};async function hE(t,e,r){let n=Date.now();for(;!t();){let o=Date.now()-n;if(o>e)throw new w(`BackgroundLoader: Timeout waiting for an on-going loader after ${o} ms.`);await scheduler.wait(r)}}s(hE,"waitUntilTrue");var yE={ttlSeconds:600,loaderTimeoutSeconds:30},cv=new Er(async()=>{let t=P.instance.zuploEdgeApiUrl;if(!t)throw new w("Zuplo edge API URL not configured");let e=P.instance.authApiJWT;if(!e)throw new w("Zuplo auth API JWT not configured");let r=P.instance.deploymentName;if(!r)throw new w("Deployment name not configured");let n=ke.ZUPLO_SERVICE_BUCKET_ID;if(!n)throw new w("ZUPLO_SERVICE_BUCKET_ID env not configured");let o=`${t}/zups/v1/buckets/${n}/mcp/resolve`,i=await j.fetch(o,{method:"POST",headers:{Authorization:`Bearer ${e}`,"User-Agent":P.instance.systemUserAgent,"Content-Type":"application/json"},body:JSON.stringify({deployment:r})});if(!i.ok){let u=await i.text().catch(()=>"");throw new w(`Failed to resolve MCP gateway configuration from ${o}: ${i.status} ${i.statusText}${u?` - ${u}`:""}`)}let c=(await i.json()).mcpGatewayConfig;if(!c||typeof c!="object")throw new w("Invalid MCP gateway configuration: not an object");if(!Array.isArray(c.servers))throw new w("Invalid MCP gateway configuration: servers must be an array");return c},yE);async function bE(t,e){if(R("handler.mcp-gateway"),e.route.methods.some(f=>f.toUpperCase()!=="POST"))throw new w(`Invalid route config: mcpGatewayHandler may only use POST. Route '${e.route.path}' declares methods: [${e.route.methods.join(", ")}]`);let r=e.route?.handler?.options??{},n=await cv.get("NO_KEY");e.log.debug(`Loaded configuration with ${n.servers?.length??0} servers`);let o=t.params.slug;if(!o)throw new Error("No MCP gateway slug provided in request");let i=n.servers.find(f=>f.slug===o);if(!i)throw new Error(`No MCP server configuration found for slug: ${o}`);let a={name:i.name,debugMode:r.debugMode},u=new TextEncoder().encode(JSON.stringify(i)),l=await crypto.subtle.digest("SHA-256",u),d=new Uint8Array(l),p=Array.from(d).map(f=>f.toString(16).padStart(2,"0")).join(""),m=`mcp-origin-config:${o}:${p.substring(0,32)}`;return e.log.debug(`Using MCP server key: ${m}`),wE({request:t,context:e,opts:a,key:m,origins:i.origins})}s(bE,"mcpGatewayHandler");async function wE({request:t,context:e,opts:r,key:n,origins:o}){let i={};t.headers.forEach((c,u)=>{i[u]=c});let a=await cn.getInstance({opts:r,context:e,key:n,origins:o});return Ys(i,e,()=>a.handleRequest(t,e))}s(wE,"mcpServerProxyHandler");function Be(t,e){let r={};for(let n in t){let o=t[n];Array.isArray(o)||(o=[o]);for(let i of o)if(i.param){if(n in e){let a=vE(n,e,i);a!==void 0&&uv(r,i.param,a)}else if(i?.required&&i.default!==void 0){let a=typeof i.default=="function"?i.default(e):i.default;uv(r,i.param,a)}}}return r}s(Be,"validateAndTransformRequest");function vE(t,e,r){let n=e[t];return r.transform&&(n=r.transform(e)),n===void 0&&r.default!==void 0&&(n=typeof r.default=="function"?r.default(e):r.default),typeof n=="number"&&(r.min!==void 0&&n<r.min&&(n=r.min),r.max!==void 0&&n>r.max&&(n=r.max)),n}s(vE,"getValue");function uv(t,e,r){if(r===void 0)return;let n=e.split("."),o=t;for(let i=0;i<n.length-1;i++){let a=n[i];o[a]||(o[a]={}),o=o[a]}o[n[n.length-1]]=r}s(uv,"setNestedProperty");async function _r(t){let e=new TextEncoder().encode(t),r=await crypto.subtle.digest({name:"SHA-256"},e);return[...new Uint8Array(r)].map(o=>o.toString(16).padStart(2,"0")).join("")}s(_r,"sha256");var lv=new Map;async function xe(t,e,r){let n,o=`${t}-${e}`,i=lv.get(o);return i!==void 0?n=i:(n=`zuplo-policy-${await _r(JSON.stringify({policyName:t,options:r}))}`,lv.set(t,n)),n}s(xe,"getPolicyCacheName");var xE=60,RE={openai:{"gpt-5":{model:"gpt-5",kind:"completions",status:"active",inputCostPerToken:125e-8,outputCostPerToken:1e-5},"gpt-5-nano":{model:"gpt-5-nano",kind:"completions",status:"active",inputCostPerToken:5e-8,outputCostPerToken:4e-7},"gpt-5-mini":{model:"gpt-5-mini",kind:"completions",status:"active",inputCostPerToken:25e-8,outputCostPerToken:2e-6},"gpt-5-nano-2025-08-07":{model:"gpt-5-nano-2025-08-07",kind:"completions",status:"active",inputCostPerToken:5e-8,outputCostPerToken:4e-7},"gpt-5-mini-2025-08-07":{model:"gpt-5-mini-2025-08-07",kind:"completions",status:"active",inputCostPerToken:25e-8,outputCostPerToken:2e-6},"gpt-5-2025-08-07":{model:"gpt-5-2025-08-07",kind:"completions",status:"active",inputCostPerToken:125e-8,outputCostPerToken:1e-5},"gpt-5-chat-latest":{model:"gpt-5-chat-latest",kind:"completions",status:"active",inputCostPerToken:125e-8,outputCostPerToken:1e-5},"gpt-4o":{model:"gpt-4o",kind:"completions",status:"active",inputCostPerToken:25e-7,outputCostPerToken:1e-5},"gpt-4o-2024-11-20":{model:"gpt-4o-2024-11-20",kind:"completions",status:"active",inputCostPerToken:25e-7,outputCostPerToken:1e-5},"gpt-4o-2024-08-06":{model:"gpt-4o-2024-08-06",kind:"completions",status:"active",inputCostPerToken:25e-7,outputCostPerToken:1e-5},"gpt-4o-2024-05-13":{model:"gpt-4o-2024-05-13",kind:"completions",status:"active",inputCostPerToken:5e-6,outputCostPerToken:15e-6},"gpt-4o-mini":{model:"gpt-4o-mini",kind:"completions",status:"active",inputCostPerToken:15e-8,outputCostPerToken:6e-7},"gpt-4o-mini-2024-07-18":{model:"gpt-4o-mini-2024-07-18",kind:"completions",status:"active",inputCostPerToken:15e-8,outputCostPerToken:6e-7},"gpt-4.1":{model:"gpt-4.1",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6},"gpt-4.1-mini":{model:"gpt-4.1-mini",kind:"completions",status:"active",inputCostPerToken:4e-7,outputCostPerToken:16e-7},"gpt-4.1-nano":{model:"gpt-4.1-nano",kind:"completions",status:"active",inputCostPerToken:1e-7,outputCostPerToken:4e-7},"gpt-4-turbo":{model:"gpt-4-turbo",kind:"completions",status:"active",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4-turbo-2024-04-09":{model:"gpt-4-turbo-2024-04-09",kind:"completions",status:"active",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4-turbo-preview":{model:"gpt-4-turbo-preview",kind:"completions",status:"active",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4-0125-preview":{model:"gpt-4-0125-preview",kind:"completions",status:"active",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4-1106-preview":{model:"gpt-4-1106-preview",kind:"completions",status:"active",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4-vision-preview":{model:"gpt-4-vision-preview",kind:"completions",status:"deprecated",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4":{model:"gpt-4",kind:"completions",status:"active",inputCostPerToken:3e-5,outputCostPerToken:6e-5},"gpt-4-0613":{model:"gpt-4-0613",kind:"completions",status:"active",inputCostPerToken:3e-5,outputCostPerToken:6e-5},"gpt-4-32k":{model:"gpt-4-32k",kind:"completions",status:"deprecated",inputCostPerToken:6e-5,outputCostPerToken:12e-5},"gpt-4-32k-0613":{model:"gpt-4-32k-0613",kind:"completions",status:"deprecated",inputCostPerToken:6e-5,outputCostPerToken:12e-5},"gpt-3.5-turbo":{model:"gpt-3.5-turbo",kind:"completions",status:"active",inputCostPerToken:5e-7,outputCostPerToken:15e-7},"gpt-3.5-turbo-0125":{model:"gpt-3.5-turbo-0125",kind:"completions",status:"active",inputCostPerToken:5e-7,outputCostPerToken:15e-7},"gpt-3.5-turbo-1106":{model:"gpt-3.5-turbo-1106",kind:"completions",status:"active",inputCostPerToken:1e-6,outputCostPerToken:2e-6},"gpt-3.5-turbo-16k":{model:"gpt-3.5-turbo-16k",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:4e-6},"text-embedding-3-large":{model:"text-embedding-3-large",kind:"embeddings",status:"active",inputCostPerToken:13e-8,outputCostPerToken:0},"text-embedding-3-small":{model:"text-embedding-3-small",kind:"embeddings",status:"active",inputCostPerToken:2e-8,outputCostPerToken:0},"text-embedding-ada-002":{model:"text-embedding-ada-002",kind:"embeddings",status:"active",inputCostPerToken:1e-7,outputCostPerToken:0}},anthropic:{"claude-opus-4-20250514":{model:"claude-opus-4-20250514",kind:"completions",status:"active",inputCostPerToken:15e-6,outputCostPerToken:75e-6},"claude-sonnet-4-20250514":{model:"claude-sonnet-4-20250514",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:15e-6},"claude-3-7-sonnet-20250219":{model:"claude-3-7-sonnet-20250219",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:15e-6},"claude-3-5-sonnet-20241022":{model:"claude-3-5-sonnet-20241022",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:15e-6},"claude-3-5-sonnet-20240620":{model:"claude-3-5-sonnet-20240620",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:15e-6},"claude-3-5-haiku-20241022":{model:"claude-3-5-haiku-20241022",kind:"completions",status:"active",inputCostPerToken:8e-7,outputCostPerToken:4e-6},"claude-3-opus-20240229":{model:"claude-3-opus-20240229",kind:"completions",status:"active",inputCostPerToken:15e-6,outputCostPerToken:75e-6},"claude-3-haiku-20240307":{model:"claude-3-haiku-20240307",kind:"completions",status:"active",inputCostPerToken:25e-8,outputCostPerToken:125e-8},"claude-2.1":{model:"claude-2.1",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6},"claude-2.0":{model:"claude-2.0",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6},"claude-instant-1.2":{model:"claude-instant-1.2",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6}},google:{"gemini-2.0-flash-exp":{model:"gemini-2.0-flash-exp",kind:"completions",status:"active",inputCostPerToken:15e-8,outputCostPerToken:6e-7},"gemini-2.0-flash":{model:"gemini-2.0-flash",kind:"completions",status:"active",inputCostPerToken:1e-7,outputCostPerToken:4e-7},"gemini-2.0-flash-lite":{model:"gemini-2.0-flash-lite",kind:"completions",status:"active",inputCostPerToken:75e-9,outputCostPerToken:3e-7},"gemini-2.5-pro":{model:"gemini-2.5-pro",kind:"completions",status:"active",inputCostPerToken:125e-8,outputCostPerToken:1e-5},"gemini-pro":{model:"gemini-pro",kind:"completions",status:"active",inputCostPerToken:5e-7,outputCostPerToken:15e-7},"gemini-embedding-001":{model:"gemini-embedding-001",kind:"embeddings",status:"active",inputCostPerToken:15e-8,outputCostPerToken:0}},mistral:{"mistral-large-latest":{model:"mistral-large-latest",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:6e-6},"mistral-large-2411":{model:"mistral-large-2411",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:6e-6},"mistral-large-2407":{model:"mistral-large-2407",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:9e-6},"mistral-medium-latest":{model:"mistral-medium-latest",kind:"completions",status:"active",inputCostPerToken:4e-7,outputCostPerToken:2e-6},"mistral-small-latest":{model:"mistral-small-latest",kind:"completions",status:"active",inputCostPerToken:1e-7,outputCostPerToken:3e-7},"mistral-small-2409":{model:"mistral-small-2409",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6},"open-mistral-7b":{model:"open-mistral-7b",kind:"completions",status:"active",inputCostPerToken:25e-8,outputCostPerToken:25e-8},"open-mixtral-8x7b":{model:"open-mixtral-8x7b",kind:"completions",status:"active",inputCostPerToken:7e-7,outputCostPerToken:7e-7},"open-mixtral-8x22b":{model:"open-mixtral-8x22b",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:6e-6},"open-mistral-nemo":{model:"open-mistral-nemo",kind:"completions",status:"active",inputCostPerToken:3e-7,outputCostPerToken:3e-7},"codestral-latest":{model:"codestral-latest",kind:"completions",status:"active",inputCostPerToken:1e-6,outputCostPerToken:3e-6},"ministral-3b-latest":{model:"ministral-3b-latest",kind:"completions",status:"active",inputCostPerToken:4e-8,outputCostPerToken:4e-8},"ministral-8b-latest":{model:"ministral-8b-latest",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6},"mistral-embed":{model:"mistral-embed",kind:"embeddings",status:"active",inputCostPerToken:1e-7,outputCostPerToken:0}}};async function ft(t){let e=Q.getLogger(t),r=await xe("supported-models","models",{}),n=new be(r,t),o=await n.get("models");if(o)return e.info("Using cached supported models data",{providersCount:Object.keys(o.modelsByProvider).length,providers:Object.keys(o.modelsByProvider)}),o.modelsByProvider;let i=new Headers({"content-type":"application/json"});je(i,t.requestId);let a=`${P.instance.zuploEdgeApiUrl}/v1/buckets/${ke.ZUPLO_SERVICE_BUCKET_ID}/providers`,c=await Te({retryDelayMs:100,retries:5},a,{method:"GET",headers:i}),u;if(c.status!==200){try{let d=await c.text(),p=JSON.parse(d);e.error("Gateway service unavailable, using fallback models",p)}catch{e.error("Gateway service unavailable, using fallback models")}t.log.info("AI Gateway: Using fallback model data due to service unavailability"),u=RE}else{let d=await c.json();e.info("Gateway service response received",{providersCount:Object.keys(d.data).length,providers:Object.keys(d.data),totalModels:Object.values(d.data).reduce((p,m)=>p+m.length,0)}),u={};for(let[p,m]of Object.entries(d.data)){let f=p.toLowerCase();u[f]={};for(let g of m)u[f][g.model]=g}}let l={modelsByProvider:u};return n.put("models",l,xE),u}s(ft,"getSupportedModels");function _t(t,e,r,n,o,i){let a=e.toLowerCase(),c=o[a];if(!c)return i.warn("Provider not found in supported models list",{provider:e,model:t}),0;let u=c[t];if(!u)return i.warn("Model not found in supported models list for provider",{provider:e,model:t}),0;let l=r*u.inputCostPerToken,d=n*u.outputCostPerToken;return l+d}s(_t,"calculateModelCost");function dv(t,e,r,n){let o=e.toLowerCase(),i=n[o];if(!i)return!1;let a=i[t];return a?a.kind===r&&a.status==="active":!1}s(dv,"isModelSupported");function pv(t,e,r){let n=t.toLowerCase(),o=r[n];return o?Object.values(o).filter(i=>i.kind===e&&i.status==="active").map(i=>i.model):[]}s(pv,"getModelsByProviderAndKind");var xm=class{static{s(this,"GatewayServiceClient")}baseUrl;constructor(e){this.baseUrl=e??P.instance.zuploEdgeApiUrl}async fetchCurrentMeters(e,r){let n=`${this.baseUrl}/v1/hierarchical-quota/${e}`,o=new Headers({"Content-Type":"application/json"});je(o,r.requestId);let i=await fetch(n,{method:"GET",headers:o});if(!i.ok)throw new le(`Failed to fetch meters: ${i.status} ${i.statusText}`);return await i.json()}async checkHierarchicalQuotaLimits(e,r){let n=`${this.baseUrl}/v1/hierarchical-quota/${e}/limits`,o=new Headers({"Content-Type":"application/json"});je(o,r.requestId);let i=await fetch(n,{method:"GET",headers:o});if(!i.ok)throw new le(`Failed to check quota limits: ${i.status} ${i.statusText}`);return await i.json()}async incrementMeters(e,r,n){let o=`${this.baseUrl}/v1/hierarchical-quota/${e}`,i=new Headers({"Content-Type":"application/json"});je(i,n.requestId);let a=await fetch(o,{method:"POST",headers:i,body:JSON.stringify({increments:r})});if(!a.ok)throw new le(`Failed to increment meters: ${a.status} ${a.statusText}`)}},vm=null,sa={get instance(){return vm===null&&(vm=new xm),vm}};var mv=Ne("zuplo:policies:AIGatewayMeteringInboundPolicy"),fv=Symbol("ai-gateway-meter-increments"),st=class t extends Re{static{s(this,"AIGatewayMeteringInboundPolicy")}static setIncrements(e,r){pe.set(e,fv,r)}static getIncrements(e){return pe.get(e,fv)??{}}constructor(e,r){super(e,r),R("policy.inbound.ai-gateway-metering-inbound")}async handler(e,r){let n=Date.now(),o=Q.getLogger(r),i=s((a,c)=>{if(this.options.throwOnFailure)throw new le(a,{cause:c});o.error(c,a)},"throwOrLog");try{let a=e.user?.configuration;if(!a)throw new w(`AIGatewayMeteringInboundPolicy '${this.policyName}' - No configuration found in request.user. Ensure ai-gateway-inbound policy runs first.`);let c=a;if(!c.id)throw new w(`AIGatewayMeteringInboundPolicy '${this.policyName}' - Configuration ID not found.`);let u=await this.fetchCurrentMeters(c.id,r,o),l=this.checkWarnings(c,u);for(let p of l)r.analyticsContext.addAnalyticsEvent(1,Oe.AI_GATEWAY_WARNING_COUNT,{type:`quota-${p.type}-${p.period}`,configId:c.id});let d=await this.checkHierarchicalQuotaLimits(c.id,r,o);return d.violation?(r.analyticsContext.addAnalyticsEvent(1,Oe.AI_GATEWAY_BLOCKED_COUNT,{type:`quota-${d.violation.meter}-${d.violation.period}`,configId:c.id}),this.createHierarchicalQuotaExceededResponse(e,r,d.violation)):(r.addResponseSendingFinalHook(async()=>{try{let p=t.getIncrements(r);mv(`AIGatewayMeteringInboundPolicy '${this.policyName}' - increments ${JSON.stringify(p)}`),Object.keys(p).length>0&&await t.incrementMetersInternal(c.id,p,r,o)}catch(p){i(`AIGatewayMeteringInboundPolicy '${this.policyName}' - Failed to increment meters`,p)}}),e)}catch(a){if(a instanceof w)throw a;return i(`AIGatewayMeteringInboundPolicy '${this.policyName}' - Error`,a),e}finally{let a=Date.now()-n;mv(`AIGatewayMeteringInboundPolicy '${this.policyName}' - latency ${a}ms`)}}async fetchCurrentMeters(e,r,n){try{return await sa.instance.fetchCurrentMeters(e,r)}catch(o){throw n.error(o,`AIGatewayMeteringInboundPolicy '${this.policyName}' - Failed to fetch meters`),o}}async checkHierarchicalQuotaLimits(e,r,n){try{return await sa.instance.checkHierarchicalQuotaLimits(e,r)}catch(o){throw n.error(o,`AIGatewayMeteringInboundPolicy '${this.policyName}' - Failed to check hierarchical quota limits`),o}}static async incrementMeters(e,r,n){let o=Q.getLogger(n);return t.incrementMetersInternal(e,r,n,o)}static async incrementMetersInternal(e,r,n,o){try{await sa.instance.incrementMeters(e,r,n)}catch(i){throw o.error(i,"AIGatewayMeteringInboundPolicy - Failed to increment meters"),i}}checkWarnings(e,r){let n=[];for(let[o,i]of Object.entries(e.limits)){if(!i)continue;let a=r.meters[o];if(!a)continue;let c=this.checkQuotaWarning(i.daily,a.daily,o,"daily");c&&n.push(c);let u=this.checkQuotaWarning(i.monthly,a.monthly,o,"monthly");u&&n.push(u)}return n}checkQuotaWarning(e,r,n,o){if(!e?.warning?.enabled||!e?.warning?.threshold||!e?.limit)return null;let i=e.warning.threshold/100*e.limit;return r>=i?{type:n,period:o}:null}createHierarchicalQuotaExceededResponse(e,r,n){let o=`${n.period} ${n.meter} quota exceeded in configuration '${n.configLabel}'. Limit: ${n.limit}, Current: ${n.currentUsage}`;return D.tooManyRequests(e,r,{detail:o})}};async function lr(t,e){if(!P.instance.remoteLogURL){Q.getLogger(t).debug("Remote log URL is not configured, skipping analytics");return}t.analyticsContext.addAnalyticsEvent(parseFloat(e.cost.toFixed(10)),Oe.AI_GATEWAY_COST_SUM,{model:e.model,provider:e.provider,configId:e.configId}),t.analyticsContext.addAnalyticsEvent(1,Oe.AI_GATEWAY_REQUEST_COUNT,{model:e.model,provider:e.provider,configId:e.configId}),t.analyticsContext.addAnalyticsEvent(e.promptTokens,Oe.AI_GATEWAY_TOKEN_SUM,{model:e.model,provider:e.provider,configId:e.configId,tokenType:"prompt"}),t.analyticsContext.addAnalyticsEvent(e.completionTokens,Oe.AI_GATEWAY_TOKEN_SUM,{model:e.model,provider:e.provider,configId:e.configId,tokenType:"completion"});let r=t.analyticsContext.flushAnalyticsEvents();new Fr(t,{endpoint:`${P.instance.remoteLogURL}/v2/analytics`}).pushEvents(r)}s(lr,"sendStreamAnalytics");var gv={model:{param:"model",required:!0,default:"gpt-4o-mini"},messages:{param:"messages",required:!0},temperature:{param:"temperature",default:1,min:0,max:2},top_p:{param:"top_p",default:1,min:0,max:1},n:{param:"n",default:1,min:1,max:128},stream:{param:"stream",default:!1},stop:{param:"stop"},max_tokens:{param:"max_tokens",min:0},presence_penalty:{param:"presence_penalty",default:0,min:-2,max:2},frequency_penalty:{param:"frequency_penalty",default:0,min:-2,max:2},logit_bias:{param:"logit_bias"},user:{param:"user"},seed:{param:"seed"},functions:{param:"functions"},function_call:{param:"function_call"},tools:{param:"tools"},tool_choice:{param:"tool_choice"},parallel_tool_calls:{param:"parallel_tool_calls"},response_format:{param:"response_format"},logprobs:{param:"logprobs",default:!1},top_logprobs:{param:"top_logprobs",min:0,max:20},max_completion_tokens:{param:"max_completion_tokens"},service_tier:{param:"service_tier"},stream_options:{param:"stream_options"}},PE={model:{param:"model",required:!0,default:"text-embedding-3-small"},input:{param:"input",required:!0},user:{param:"user"},dimensions:{param:"dimensions"},encoding_format:{param:"encoding_format",default:"float"}},aa=class{static{s(this,"OpenAIProvider")}name="openai";async chatComplete(e,r){let n=Be(gv,e),o=await fetch("https://api.openai.com/v1/chat/completions",{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify(n)});if(!o.ok){let a=await o.json();throw new Error(`OpenAI API error: ${a.error?.message||"Unknown error"}`)}return{...await o.json(),provider:"openai"}}async chatCompleteStream(e,r,n){let o=Be(gv,{...e,stream:!0,stream_options:{include_usage:!0}}),i=await fetch("https://api.openai.com/v1/chat/completions",{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify(o)});if(!i.ok){let m=await i.json();throw new Error(`OpenAI API error: ${m.error?.message||"Unknown error"}`)}if(!i.body)throw new Error("No response body received from OpenAI API");let a=n.custom.userContext,c="",u=new TextDecoder,l=new TransformStream({transform(m,f){f.enqueue(m);let g=u.decode(m);c+=g,!c.includes('"usage"')&&c.length>4096&&(c=c.slice(-32));let b;for(;(b=c.indexOf(`
200
+ `&&n++}}return[e,r]}s(pE,"splitLines");var ra=class extends TransformStream{static{s(this,"EventSourceParserStream")}constructor({onError:e,onRetry:r,onComment:n}={}){let o;super({start(i){o=iv({onEvent:s(a=>{i.enqueue(a)},"onEvent"),onError(a){e==="terminate"?i.error(a):typeof e=="function"&&e(a)},onRetry:r,onComment:n})},transform(i){o.feed(i)}})}};var na=class{static{s(this,"HTTPClientTransport")}url;timeout;headers;fetch;logger;sessionId;messageHandler;errorCallback;closeCallback;isConnected=!1;constructor(e){this.url=e.url,this.timeout=e.timeout||3e4,this.headers={"Content-Type":"application/json",Accept:"application/json, text/event-stream",...e.headers},this.fetch=e.fetch||globalThis.fetch,this.logger=e.logger||rr(),e.enableSessions&&this.logger.debug("Session support not yet implemented for HTTP client transport")}setHeaders(e){this.headers={...this.headers,...e}}async connect(){try{new URL(this.url),this.isConnected=!0,this.logger.info("HTTP Client Transport connected to:",this.url)}catch{let r=new Error(`Invalid URL: ${this.url}`);throw this.errorCallback&&this.errorCallback(r),r}}async send(e){if(!this.isConnected)throw new Error("Transport not connected. Call connect() first.");let r;try{let n={...this.headers};this.sessionId&&(n["Mcp-Session-Id"]=this.sessionId);let o=new AbortController;r=setTimeout(()=>o.abort(),this.timeout);let i=await this.fetch(this.url,{method:"POST",headers:{...this.headers,...this.sessionId&&{"Mcp-Session-Id":this.sessionId}},body:JSON.stringify(e),signal:o.signal});if(!i.ok)throw new Error(`HTTP ${i.status}: ${i.statusText}`);(i.headers.get("Content-Type")||"").includes("text/event-stream")?await this.handleSSEResponse(i):await this.handleJSONResponse(i)}catch(n){if(n instanceof Error&&n.name==="AbortError")throw this.errorCallback&&this.errorCallback(n),new Error(`Request timeout after ${this.timeout}ms`);let o=new Error(`A client error occurred: ${n}`);throw this.errorCallback&&this.errorCallback(o),o}finally{r&&clearTimeout(r)}}onMessage(e){this.messageHandler=e}async close(){this.logger.debug("Closing HTTP Client Transport"),this.isConnected=!1,this.messageHandler=void 0,this.closeCallback&&this.closeCallback(),this.logger.info("HTTP Client Transport closed")}onClose(e){this.closeCallback=e}onError(e){this.errorCallback=e}getSessionId(){return this.sessionId}setSessionId(e){this.sessionId=e,this.logger.debug("Session ID set:",e?"***":"undefined")}async handleJSONResponse(e){let r=await e.text();if(!r.trim()){this.logger.debug("Received empty response");return}let n;try{n=JSON.parse(r)}catch{throw new Error(`Invalid JSON response: ${r}`)}this.logger.debug("Received JSON-RPC response:",n),this.messageHandler&&await this.messageHandler(n)}async handleSSEResponse(e){if(!e.body)throw new Error("SSE response has no body");this.logger.debug("Handling SSE response");let r=s(o=>{if(!o.event||o.event==="message")try{let i=JSON.parse(o.data);this.logger.debug("Received SSE message:",i),this.messageHandler&&this.messageHandler(i)}catch(i){this.logger.warn("Failed to parse SSE message data:",o.data,i)}},"onEvent"),n=e.body.pipeThrough(new TextDecoderStream).pipeThrough(new ra).getReader();try{for(;;){let{done:o,value:i}=await n.read();if(o)return;r(i)}}catch(o){throw this.logger.error("Error processing SSE stream:",o),new Error(`SSE stream error: ${o}`)}}};var mE="MCP Client",fE="0.0.0",oa=class{static{s(this,"MCPClient")}name;version;capabilities;transport;isInitialized=!1;protocolVersion;logger;requestId=1;transportOptions;constructor(e={}){this.name=e.name||mE,this.version=e.version||fE,this.logger=e.logger||rr(),this.transportOptions=e.transportOptions||{},this.capabilities={experimental:{},sampling:{},...e.capabilities}}async connect(e){this.transportOptions.headers&&e.setHeaders(this.transportOptions.headers),this.transport=e,await e.connect()}async initialize(e=Do){if(!this.transport)throw new Error("No transport connected. Call connect() first.");let r=Ir({id:this.requestId,method:"initialize",params:{protocolVersion:e,capabilities:this.capabilities,clientInfo:{name:this.name,version:this.version}}}),n=await this.sendRequest(r);if(tr(n))throw new Error(`Initialization failed: ${n.error.message}`);let o=n.result;return this.isInitialized=!0,this.protocolVersion=o.protocolVersion,this.logger.info("Successfully initialized MCP client",{serverInfo:o,protocolVersion:this.protocolVersion}),o}async ping(){if(!this.isInitialized)throw new Error("Client not initialized. Call initialize() first.");let e=Ir({id:this.requestId,method:"ping"}),r=await this.sendRequest(e);if(tr(r))throw new Error(`Ping failed: ${r.error.message}`)}async listTools(){if(!this.isInitialized)throw new Error("Client not initialized. Call initialize() first.");let e=Ir({id:this.requestId,method:"tools/list",params:{}}),r=await this.sendRequest(e);if(tr(r))throw new Error(`Failed to list tools: ${r.error.message}`);return r.result.tools}async callTool(e,r={}){if(!this.isInitialized)throw new Error("Client not initialized. Call initialize() first.");let n=Ir({id:this.requestId,method:"tools/call",params:{name:e,arguments:r}}),o=await this.sendRequest(n);if(tr(o))throw new Error(`Failed to call tool '${e}': ${o.error.message}`);return o.result}async listPrompts(){if(!this.isInitialized)throw new Error("Client not initialized. Call initialize() first.");let e=Ir({id:this.requestId,method:"prompts/list",params:{}}),r=await this.sendRequest(e);if(tr(r))throw new Error(`Failed to list prompts: ${r.error.message}`);return r.result.prompts}async getPrompt(e,r={}){if(!this.isInitialized)throw new Error("Client not initialized. Call initialize() first.");let n=Ir({id:this.requestId,method:"prompts/get",params:{name:e,arguments:r}}),o=await this.sendRequest(n);if(tr(o))throw new Error(`Failed to get prompt '${e}': ${o.error.message}`);return o.result}async disconnect(){this.transport&&(await this.transport.close(),this.transport=void 0),this.isInitialized=!1,this.protocolVersion=void 0}async sendRequest(e){return new Promise((r,n)=>{let o=e.id;this.transport?.onMessage(async i=>((nn(i)||tr(i))&&i.id===o&&r(i),null)),this.transport?.send(e).catch(n)})}};var ia=class{constructor(e,r,n=!1,o){this.baseUrl=e;this.originToolName=r;this.debugMode=n;this.originHeaders=o}static{s(this,"ProxyMcpToolExecutor")}async execute(e){let r=ur(),n=Date.now(),o=new oa({name:"Zuplo MCP Gateway Proxy Client",version:"0.0.1"});try{this.debugMode&&r.log.debug("Proxying MCP tool call",{originToolName:this.originToolName,baseUrl:this.baseUrl,args:e});let i=new na({url:this.baseUrl});i.setHeaders({"Content-Type":"application/json",...ea(),...this.originHeaders??{}});let a,c=Date.now();try{await o.connect(i),await o.initialize(),a=await o.callTool(this.originToolName,e)}catch(l){throw new Error(`could not call tool: ${this.originToolName}`,l)}let u=Date.now()-c;return this.debugMode&&r.log.debug("Proxy MCP response received",{originToolName:this.originToolName,upstreamElapsedMs:u,proxyResult:a}),a}catch(i){let a=Date.now()-n,c=i instanceof Error?`${i.name}: ${i.message}`:String(i);return r.log.error("Proxy MCP tool execution failed",{originToolName:this.originToolName,baseUrl:this.baseUrl,totalElapsedMs:a,err:i}),{content:[{type:"text",text:`Tool execution failed for '${this.originToolName}': ${c}`}],isError:!0}}finally{await o.disconnect()}}};var cn=class t{static{s(this,"ZuploMcpServer")}static instances=new Map;static async getInstance({opts:e,context:r,key:n,origins:o}){let i=e.debugMode??!1,a=t.instances.get(n);if(a)i&&r.log.debug("MCP Server warm reuse",{routeKey:n});else{let c=Date.now();if(a=new t(e,r,o??[]),a.registerToolsFromOptions(a.opts.openApiTools??[]),a.registerToolsFromFiles(a.opts.openApiFilePaths??[]),a.registerToolsFromFileSources(a.opts.files??[]),a.registerPromptsFromFileSources(a.opts.prompts??[]),a.registerResourcesFromFileSources(a.opts.resources??[]),await a.registerProxyTools(a.origins??[]),await a.transport.connect(),a.server.withTransport(a.transport),t.instances.set(n,a),i){let u=a.server.getTools(),l=u?u.size:0,d=a.server.getPromptDefinitions(),p=d?d.length:0,m=a.server.getResourceDefinitions(),f=m?m.length:0,g=Date.now()-c;r.log.debug("MCP Server cold start",{routeKey:n,toolCount:l,promptCount:p,resourceCount:f,totalElapsedMs:g,debugMode:i,includeOutputSchema:a.includeOutputSchema,includeStructuredContent:a.includeStructuredContent})}}return a}transport;server;opts;origins;context;includeOutputSchema;includeStructuredContent;debugMode;constructor(e,r,n){this.debugMode=e.debugMode??!1;let o;this.debugMode?o=r.log:o=new Mo,this.context=r,this.opts=e,this.origins=n??[],this.server=new Fs({name:e.name??"Zuplo MCP Server",version:e.version??"0.0.0",logger:o}),this.transport=new Hs({logger:o}),this.includeOutputSchema=e.includeOutputSchema??!1,this.includeStructuredContent=e.includeStructuredContent??!1}async handleRequest(e,r){let n=Date.now();try{let o=await e.clone().json().catch(()=>({}));if(this.debugMode&&r.log.debug("MCP Server request start",{method:o.method,requestId:o.id,routePath:r.route?.path}),this.debugMode&&o.method==="tools/list"){let c=this.server.getTools(),u=c?Array.from(c.keys()):[];r.log.debug("MCP Server list tools request",{toolCount:u.length,toolNames:u})}if(this.debugMode&&o.method==="prompts/list"){let c=this.server.getPromptDefinitions(),u=c?c.map(l=>l.name):[];r.log.debug("MCP Server list prompts request",{promptCount:u.length,promptNames:u})}if(this.debugMode&&o.method==="resources/list"){let c=this.server.getResourceDefinitions(),u=c?c.map(l=>l.name):[];r.log.debug("MCP Server list resources request",{resourceCount:u.length,resourceNames:u})}let i=await this.transport.handleRequest(e),a=Date.now()-n;if(this.debugMode){let c=await i.clone().json().catch(()=>({}));r.log.debug("MCP Server response complete",{status:i.status,requestMethod:o.method,requestId:o.id,totalElapsedMs:a,respData:c})}return i}catch(o){let i=Date.now()-n;return r.log.error("MCP server internal error",{elapsedMs:i,err:o}),new Response("Internal error",{status:500})}}generatePromptDescriptionFromSpec(e){let r=e.raw();return r?.description?r.description:r?.summary?r.summary:`Generate content for operation for ${e.path}`}generateToolNameFromSpec(e,r){let n=e.raw();if(n?.operationId)return`${n.operationId}`;let o=`${r}_${e.path}`.replace(/[^\w]/g,"_");return this.context.log.warn(`No operationId found for route ${r} ${e.path}. Using auto-generated name "${o}". To improve AI tool selection, add a descriptive operationId to your OpenAPI spec (e.g., "get_user_by_id" instead of "GET_/users/{id}")`),o}generateToolDescriptionFromSpec(e,r){let n=e.raw();return n?.description?n.description:n?.summary?n.summary:`Call ${r.toUpperCase()} ${e.path}`}registerToolsFromOptions(e){let r=new Set;for(let n of e){if(this.context.log.warn(`DEPRECATED: Registering MCP tool with name: "${n.name}", operationId: "${n.operationId}", method: "${n.method}" from "options.openApiTools" will soon be deprecated: migrate to using "options.files" configurations`),n.name){if(typeof n.name!="string"||n.name.trim()==="")throw new Error("MCP Tool configuration error: Tool name must be a non-empty string if provided.");if(r.has(n.name))throw new Error(`MCP Tool configuration error: Duplicate tool name "${n.name}". Tool names must be unique across all configured tools.`)}let o=this.getRouteDataForOptions(n);if(!o){let l=n.operationId?`operationId: "${n.operationId}"`:`path: "${n.path}"`;throw new Error(`MCP Tool configuration error: Could not find gateway route data for ${n.method.toUpperCase()} ${l}. Verify that the route exists in your OpenAPI specification and that the provided metadata matches OpenAPI specification data.`)}if(!o.handler){let l=n.operationId?`operationId: "${n.operationId}"`:`path: "${n.path}"`;throw new Error(`MCP Tool configuration error: Route ${n.method.toUpperCase()} ${l} has no handler configured. Ensure the route has a proper handler defined in your OpenAPI specification.`)}let i=n.name??this.generateToolNameFromSpec(o,n.method);if(r.has(i))throw new Error(`MCP Tool configuration error: Tool name conflict detected. The name "${i}" is already in use. Consider providing a unique 'name' in your tool configuration or ensure your operationIds are unique.`);r.add(i);let a=n.description??this.generateToolDescriptionFromSpec(o,n.method),c=n.includeOutputSchema??this.includeOutputSchema??!0,u=n.includeStructuredContent??this.includeStructuredContent??!0;this.registerToolsForMethod(o,n.method,i,a,c,u)}}static getOperationsMetadataForFile(e){let{routes:r}=we.instance.routeData,n=r.filter(i=>i.metadata?.filepath===e);if(n.length===0)throw new Error(`MCP Tool configuration error: No routes found for file path ${e}. Verify that the OpenAPI file exists and is properly loaded in your Gateway configuration with routes.`);let o=new Map;for(let i of n){let a=i?.raw().operationId;if(a){o.set(a,{routeConfig:i});let c=i?.raw()["x-zuplo-mcp-tool"],u=i?.raw()["x-zuplo-mcp-prompt"],l=i?.raw()["x-zuplo-mcp-graphql"],d=i?.raw()["x-zuplo-mcp-resource"];(c||u||l||d)&&o.set(a,{routeConfig:i,toolExtension:c||void 0,promptExtension:u||void 0,graphqlExtension:l||void 0,resourceExtension:d||void 0})}}return o}registerPromptsFromFileSources(e){for(let r of e){let n=t.getOperationsMetadataForFile(r.path),o=new Set;for(let i of r.operationIds){let a=n.get(i);if(!a)throw new Error(`MCP Prompt configuration error: Could not find operation with ID "${i}" in file ${r.path}. Verify that the operation ID exists in your OpenAPI specification.`);let{routeConfig:c,promptExtension:u}=a,l=u?.name??i;if(o.has(l))throw new Error(`MCP Prompt configuration error: Duplicate prompt name "${l}". Prompt names must be unique across all configured prompts.`);o.add(l);let d=u?.description??this.generatePromptDescriptionFromSpec(c);if(!(u?.enabled??!0)){this.debugMode&&this.context.log.debug("MCP prompt disabled by extension",{promptName:l,operationId:i});continue}this.registerPromptForOperation(l,d,c,i)}}}registerPromptForOperation(e,r,n,o){try{if(n.handler?.export==="mcpServerHandler")return;let a=Cr.createPromptValidatorBuilder(n).build();this.server.addPrompt({name:e,description:r,validator:a,generator:s(async c=>{let u=ur(),l=Date.now();try{this.debugMode&&u.log.debug("MCP prompt invoked",{promptName:e,operationId:n.raw()?.operationId,path:n.path,args:c});let d=this.buildToolUrl(n,{body:c}),p=this.buildToolRequest("POST","application/json",{body:c});this.debugMode&&u.log.debug("MCP prompt downstream call",{promptName:e,url:d,method:"POST",bodyPreview:c});let m=Date.now(),f=await u.invokeRoute(d,p),g=Date.now()-m,b=await f.json();if(!f.ok)throw new Error(`Route returned ${f.status}: ${b}`);let h;try{if(h=b.messages||b,!Array.isArray(h))throw new Error("Response must contain a 'messages' array or be an array of messages")}catch(v){throw new Error(`Invalid JSON response or missing messages array: ${v instanceof Error?v.message:String(v)}`)}let x=Date.now()-l;return this.debugMode&&u.log.debug("MCP prompt response complete",{promptName:e,operationId:n.raw()?.operationId,status:f.status,elapsedMs:x,downstreamElapsedMs:g,messageCount:h.length}),h}catch(d){let p=Date.now()-l,m=d instanceof Error?`${d.name}: ${d.message}`:String(d);throw u.log.error("MCP prompt invocation failed",{promptName:e,operationId:n.raw()?.operationId,path:n.path,elapsedMs:p,err:d}),new Error(`MCP tool call failed for tool '${e}': ${m}`)}},"generator")}),this.debugMode&&this.context.log.debug("MCP prompt registered from file source",{promptName:e,operationId:o,path:n.path})}catch(i){throw this.context.log.error("Failed to register MCP prompt",{promptName:e,operationId:o,error:i instanceof Error?i.message:String(i)}),new Error(`Failed to register prompt "${e}" for operation "${o}": ${i instanceof Error?i.message:String(i)}`)}}registerResourcesFromFileSources(e){let r=new Set;for(let n of e){let o=t.getOperationsMetadataForFile(n.path);for(let i of n.operationIds){let a=o.get(i);if(!a)throw new Error(`MCP Resource configuration error: Could not find operation with ID "${i}" in file ${n.path}. Verify that the operation ID exists in your OpenAPI specification.`);let{routeConfig:c,resourceExtension:u}=a;if(!(u?.enabled??!0)){this.debugMode&&this.context.log.debug("MCP resource disabled by extension",{operationId:i});continue}if(!c.methods||c.methods.length===0)throw new Error(`MCP Resource configuration error: Operation "${i}" has no HTTP methods defined. Resources must use GET method.`);if(c.methods.length>1)throw new Error(`MCP Resource configuration error: Operation "${i}" has multiple HTTP methods (${c.methods.join(", ")}). Resources must use exactly one method and it must be GET.`);let d=c.methods[0].toUpperCase();if(d!=="GET")throw new Error(`MCP Resource configuration error: Operation "${i}" uses ${d} method. Resources are read-only and must use GET method only.`);let p=u?.name?.trim()||i.trim()||"";if(!p)throw new Error(`MCP Resource configuration error: Could not determine resource name for operation "${i}". Ensure the operation has an operationId or provide a name in x-zuplo-mcp-resource extension.`);if(r.has(p))throw new Error(`MCP Resource configuration error: Duplicate resource name "${p}". Resource names must be unique across all configured resources.`);r.add(p);let m=u?.description?.trim()||c.raw()?.description?.trim()||c.raw()?.summary?.trim()||`Resource for ${p}`,f=u?.uri?.trim()||`mcp://resources/${encodeURIComponent(p)}`,g=u?.mimeType?.trim()||"text/plain";this.registerResourceForOperation(p,f,m,c,i,g)}}}registerResourceForOperation(e,r,n,o,i,a){try{if(o.handler?.export==="mcpServerHandler")return;this.server.addResource(e,r,{title:e,description:n,mimeType:a},async()=>{let c=ur(),u=Date.now();try{this.debugMode&&c.log.debug("MCP resource invoked",{resourceName:e,operationId:o.raw()?.operationId,path:o.path,uri:r});let l=this.buildToolUrl(o,{}),d=this.buildToolRequest("GET",null,{});this.debugMode&&c.log.debug("MCP resource downstream call",{resourceName:e,url:l,method:"GET"});let p=Date.now(),m=await c.invokeRoute(l,d),f=Date.now()-p,g=await m.text(),b=Date.now()-u;return this.debugMode&&c.log.debug("MCP resource response complete",{resourceName:e,operationId:o.raw()?.operationId,status:m.status,elapsedMs:b,downstreamElapsedMs:f,declaredMimeType:a,contentLength:g.length}),{contents:[{uri:r,declaredMimeType:a,text:g}]}}catch(l){let d=Date.now()-u,p=l instanceof Error?`${l.name}: ${l.message}`:String(l);throw c.log.error("MCP resource invocation failed",{resourceName:e,operationId:o.raw()?.operationId,path:o.path,elapsedMs:d,err:l}),new Error(`MCP resource call failed for resource '${e}': ${p}`)}}),this.debugMode&&this.context.log.debug("MCP resource registered from file source",{resourceName:e,operationId:i,path:o.path,uri:r,mimeType:a||"auto-detected"})}catch(c){throw this.context.log.error("Failed to register MCP resource",{resourceName:e,operationId:i,error:c instanceof Error?c.message:String(c)}),new Error(`Failed to register resource "${e}" for operation "${i}": ${c instanceof Error?c.message:String(c)}`)}}registerToolsFromFileSources(e){let r=new Set;for(let n of e){let o=t.getOperationsMetadataForFile(n.path);this.debugMode&&this.context.log.debug("Processing file source",{path:n.path,operationIds:n.operationIds,availableOperationIds:Array.from(o.keys())});for(let i of n.operationIds){let a=o.get(i);if(!a)throw new Error(`MCP Tool configuration error: Operation ID "${i}" not found in OpenAPI spec at ${n.path}. Available operation IDs: ${Array.from(o.keys()).join(", ")}`);if(a.toolExtension?.enabled===!1){this.debugMode&&this.context.log.debug("Skipping disabled tool",{operationId:i,path:n.path});continue}let c=a.toolExtension?.name?.trim()??i,u=(a.toolExtension?.description?.trim()||null)??(a.routeConfig.raw().description?.trim()||null)??(a.routeConfig.raw().summary?.trim()||null)??`Executes tool: ${c}`;if(!u)throw new Error(`MCP Tool configuration error: no tool description found for operationId: "${i}" in file "${n.path}"`);if(r.has(c))throw new Error(`MCP Tool configuration error: Tool name conflict detected. The name "${c}" is already in use. Consider providing a unique 'name' in the x-zuplo-mcp-tool extension for operation "${i}".`);r.add(c);let l=a.toolExtension?.includeOutputSchema??this.includeOutputSchema??!1,d=a.toolExtension?.includeStructuredContent??this.includeStructuredContent??!1;if(a.routeConfig.methods.length!=1)throw new Error(`MCP tool configuration error: multiple methods "${a.routeConfig.methods.join(", ")}" on operation "${i}" not permitted. Only tools with one singular method is permitted.`);a.graphqlExtension?(a.graphqlExtension.enabled??!0)&&this.registerGraphQLTools(i,a.routeConfig,a.graphqlExtension,r):this.registerToolsForMethod(a.routeConfig,a.routeConfig.methods[0],c,u,l,d)}}}registerGraphQLTools(e,r,n,o){let i=n.introspectionToolName??`${e}_introspect`,a=n.executeToolName??`${e}_execute_query`;if(o.has(i))throw new Error(`MCP GraphQL tool configuration error: Tool name conflict detected. The introspection tool name "${i}" is already in use. Provide a unique 'introspectionToolName' in the x-zuplo-graphql extension.`);if(o.has(a))throw new Error(`MCP GraphQL tool configuration error: Tool name conflict detected. The execute tool name "${a}" is already in use. Provide a unique 'executeToolName' in the x-zuplo-graphql extension.`);o.add(i),o.add(a);let c=n.introspectionToolDescription??`Get the GraphQL schema for ${e}`,u=n.executeToolDescription??`Execute a GraphQL query on ${e}`,l=r.path,p=Cr.createObjectValidatorBuilder({}).build();this.server.addTool({name:i,description:c,validator:p,handler:s(async()=>{let f=ur(),g=Date.now();try{this.debugMode&&f.log.debug("MCP GraphQL introspection tool invoked",{toolName:i,operationId:e,path:l});let b=Gs(),h=this.buildToolUrl(r,{}),x=this.buildToolRequest("POST","application/json",{body:{query:b}}),v=Date.now(),C=await f.invokeRoute(h,x),A=Date.now()-v,O=await C.text(),S=Date.now()-g;this.debugMode&&(C.ok?f.log.debug("MCP GraphQL introspection tool ok response",{toolName:i,operationId:e,status:C.status,elapsedMs:S,downstreamElapsedMs:A}):f.log.debug("MCP GraphQL introspection tool 'isError' response",{toolName:i,operationId:e,status:C.status,elapsedMs:S,downstreamElapsedMs:A,response:O}));let M;try{M=JSON.parse(O)}catch(z){this.debugMode&&f.log.debug("MCP GraphQL introspection structuredContent JSON parse failed",{toolName:i,body:O,parseErr:z})}return{content:[{type:"text",text:O}],...M&&{structuredContent:M},isError:!C.ok}}catch(b){let h=Date.now()-g,x=b instanceof Error?`${b.name}: ${b.message}`:String(b);throw f.log.error("MCP GraphQL introspection tool invocation failed",{toolName:i,operationId:e,path:l,elapsedMs:h,err:b}),new Error(`MCP GraphQL introspection tool call failed for '${i}': ${x}`)}},"handler")});let m={type:"object",properties:{query:{type:"string",description:"The GraphQL query to execute"},variables:{type:"object",description:"Optional variables for the GraphQL query"}},required:["query"],additionalProperties:!1};this.server.addTool({name:a,description:u,validator:new sr(m,f=>typeof f=="object"&&f!==null&&"query"in f&&typeof f?.query=="string"?{success:!0,data:f,errorData:null}:{success:!1,data:null,errorMessage:"Invalid input: query field is required and must be a string",errorData:null}),handler:s(async f=>{let g=ur(),b=Date.now();try{this.debugMode&&g.log.debug("MCP GraphQL execute tool invoked",{toolName:a,operationId:e,path:l,queryPreview:f.query.substring(0,100)});let h=this.buildToolUrl(r,{}),x={query:f.query};f.variables&&(x.variables=f.variables);let v=this.buildToolRequest("POST","application/json",{body:x}),C=Date.now(),A=await g.invokeRoute(h,v),O=Date.now()-C,S=await A.text(),M=Date.now()-b;this.debugMode&&(A.ok?g.log.debug("MCP GraphQL execute tool ok response",{toolName:a,operationId:e,status:A.status,elapsedMs:M,downstreamElapsedMs:O}):g.log.debug("MCP GraphQL execute tool 'isError' response",{toolName:a,operationId:e,status:A.status,elapsedMs:M,downstreamElapsedMs:O,response:S}));let z;try{z=JSON.parse(S)}catch(Z){this.debugMode&&g.log.debug("MCP GraphQL execute structuredContent JSON parse failed",{toolName:a,body:S,parseErr:Z})}return{content:[{type:"text",text:S}],...z&&{structuredContent:z},isError:!A.ok}}catch(h){let x=Date.now()-b,v=h instanceof Error?`${h.name}: ${h.message}`:String(h);throw g.log.error("MCP GraphQL execute tool invocation failed",{toolName:a,operationId:e,path:l,elapsedMs:x,err:h}),new Error(`MCP GraphQL execute tool call failed for '${a}': ${v}`)}},"handler")}),this.debugMode&&this.context.log.debug("MCP GraphQL tools registered",{operationId:e,introspectionToolName:i,executeToolName:a,path:l})}registerToolsFromFiles(e){let r=new Set(e.map(i=>i.filePath)),n=new Set;for(let i of we.instance.routeData.routes){let a=i.metadata?.filepath;a&&r.has(a)&&(this.context.log.warn(`DEPRECATED: Registering MCP tool with operationId: "${i.raw()?.operationId}"from file: "${a}" with "options.openApiFilePaths" will soon be removed: migrate to using "options.files" configurations`),n.add(a),this.registerToolsForRawRoutedata(i,this.includeOutputSchema,this.includeStructuredContent))}let o=e.map(i=>i.filePath).filter(i=>!n.has(i));if(o.length>0)throw new Error(`MCP Tool configuration error: Could not find routes for the following file paths: ${o.join(", ")}. Verify that these OpenAPI files exist and are properly loaded in your Gateway configuration.`)}registerToolsForRawRoutedata(e,r,n){if(e.mcp?.enabled!==!1){if(!e)throw new Error("MCP Tool configuration error: Route data cannot be null or undefined.");if(!e.methods||e.methods.length===0)throw new Error(`MCP Tool configuration error: Route "${e.path}" has no HTTP methods defined. Ensure the route has at least one HTTP method (GET, POST, etc.) in your OpenAPI specification.`);if(!e.handler)throw new Error(`MCP Tool configuration error: Route "${e.path}" has no handler configured. Ensure the route has a proper handler defined in your OpenAPI specification.`);for(let o of e.methods){let i=this.generateToolNameFromSpec(e,o),a=this.generateToolDescriptionFromSpec(e,o);this.registerToolsForMethod(e,o,i,a,r,n)}}}registerToolsForMethod(e,r,n,o,i,a){if(e.handler?.export!=="mcpServerHandler")if(e.raw().requestBody?.content)for(let c of Object.keys(e.raw().requestBody?.content)){let l=Object.keys(e.raw().requestBody?.content).length>1?`${n}_${c.replace(/[^\w]/g,"_")}`:n;this.registerGenericToolSpec(l,o,e,r,c,i,a)}else this.registerGenericToolSpec(n,o,e,r,null,i,a)}registerGenericToolSpec(e,r,n,o,i,a,c){let u=this.buildUniversalValidator(n,i);if(!u)throw new Error(`MCP Tool registration error: Could not build parameter validator for ${o.toUpperCase()} ${n.path}. This may indicate an issue with the OpenAPI parameter definitions for this route.`);let l=a?this.extractOutputSchema(n):void 0;try{this.server.addTool({name:e,description:r,validator:u,...l&&{outputSchema:l},handler:s(async d=>{let p=ur(),m=Date.now();try{this.debugMode&&p.log.debug("MCP tool invoked",{toolName:e,operationId:n.raw()?.operationId,method:o,path:n.path,args:d});let f=this.buildToolUrl(n,d),g=this.buildToolRequest(o,i,d);this.debugMode&&p.log.debug("MCP tool downstream call",{toolName:e,url:f,method:o,headers:g.headers?Object.keys(g.headers).join(", "):"",bodyPreview:d.body}),p.analyticsContext.addAnalyticsEvent(1,Oe.MCP_TOOL_USAGE,{toolName:e,toolPath:n.path,toolMethod:o,toolOperationId:n.raw()?.operationId??"unknown"});let b=Date.now(),h=await p.invokeRoute(f,g),x=Date.now()-b,v=await h.text(),C;if(c)try{C=JSON.parse(v)}catch(O){this.debugMode&&p.log.debug("MCP tool structuredContent JSON parse failed",{toolName:e,body:v,parseErr:O})}let A=Date.now()-m;return this.debugMode&&(h.ok?p.log.debug("MCP tool ok response",{toolName:e,operationId:n.raw()?.operationId,status:h.status,elapsedMs:A,downstreamElapsedMs:x}):p.log.debug("MCP tool 'isError' response",{toolName:e,operationId:n.raw()?.operationId,status:h.status,elapsedMs:A,downstreamElapsedMs:x,response:v})),{content:[{type:"text",text:v}],...C&&{structuredContent:C},isError:!h.ok}}catch(f){let g=Date.now()-m,b=f instanceof Error?`${f.name}: ${f.message}`:String(f);return p.log.error("MCP server tool invocation failed",{toolName:e,operationId:n.raw()?.operationId,method:o,path:n.path,elapsedMs:g,err:f}),{content:[{type:"text",text:`MCP tool call failed for tool '${e}': ${b}`}],isError:!0}}},"handler")}),this.debugMode&&this.context.log.debug("MCP tool registered",{toolName:e,method:o.toUpperCase(),path:n.path,operationId:n.raw()?.operationId,contentType:i||"none",includeOutputSchema:a,includeStructuredContent:c,hasValidator:!!u,hasOutputSchema:!!l})}catch(d){throw new Error(`Failed to add tool ${o} ${n.path}`,d)}}async registerProxyTools(e){let r=new Set;for(let n of e){if(n.enabled===!1){this.debugMode&&this.context.log.debug("Skipping disabled origin",{origin:n.name});continue}let o=n.toolNamePrefix??"";this.debugMode&&this.context.log.debug("Registering origin tools",{origin:n.name,baseUrl:n.baseUrl,toolCount:n.tools.length,toolNamePrefix:o||"none",tools:n.tools});for(let i of n.tools){let a=`${o}${i.name}`;if(r.has(a))throw new Error(`MCP origin configuration error: Tool name conflict detected. The name "${a}" is already in use. Consider using different tool names or toolNamePrefix for origin "${n.name}".`);r.add(a);let u=Cr.createObjectValidatorBuilder(i.inputSchema).build();try{let l=new ia(n.baseUrl,i.name,this.debugMode,n.headers);this.server.addTool({name:a,description:i.description,validator:u,handler:s(async d=>await l.execute(d),"handler")}),this.debugMode&&this.context.log.debug("origin tool registered",{localToolName:a,originToolName:i.name,origin:n.name,baseUrl:n.baseUrl})}catch(l){throw new Error(`Failed to register origin tool ${a} from ${n.name}`,{cause:l})}}}this.debugMode&&e.length>0&&this.context.log.debug("origin tool registration complete",{totaloOigins:e.length,totalOriginTools:r.size,originToolNames:Array.from(r)})}buildToolUrl(e,r){return new Js(e).build(r.pathParams,r.queryParams)}buildToolRequest(e,r,n){let o=this.buildToolHeaders(e,r,n),i={method:e,headers:o};return this.shouldIncludeBody(e,r,n)&&(i.body=this.serializeBody(n.body,r)),i}buildToolHeaders(e,r,n){let o={...ea()};return n.headers&&Object.entries(n.headers).forEach(([i,a])=>{o[i.toLowerCase()]=a}),r&&n.body!==void 0&&(o["content-type"]=r),this.isBodyMethod(e||"")&&n.body!==void 0&&!o["content-type"]&&(o["content-type"]="application/json"),o}shouldIncludeBody(e,r,n){return n.body===void 0?!1:this.isBodyMethod(e)?!0:r!==null}isBodyMethod(e){return["POST","PUT","PATCH"].includes(e.toUpperCase())}serializeBody(e,r){return JSON.stringify(e)}buildUniversalValidator(e,r){try{let o=Cr.createValidatorBuilder(e,r).build();return this.debugMode&&this.context.log.debug("MCP validator built successfully",{method:e.methods?.[0],path:e.path,contentType:r||"none",operationId:e.raw()?.operationId}),o}catch(n){let o=n instanceof Error?n.message:String(n);throw this.context.log.warn("MCP validator build failed",{method:e.methods?.[0],path:e.path,contentType:r||"none",operationId:e.raw()?.operationId,error:o}),n}}getRouteDataForOptions(e){let{routes:r}=we.instance.routeData,n=!!e.operationId,o=!!e.path;if(!n&&!o)throw new Error("MCP Tool configuration error: Either 'operationId' or 'path' must be provided in your tool specification.");return n?r.find(i=>i?.raw().operationId===e.operationId&&i?.methods?.some(a=>a.toUpperCase()===e.method.toUpperCase())):r.find(i=>i?.path===e.path&&i?.methods?.some(a=>a.toUpperCase()===e.method.toUpperCase()))}extractOutputSchema(e){try{let r=e.raw(),n=r?.responses;if(!n){this.context.log.warn("No responses found in OpenAPI spec for outputSchema",{path:e.path,operationId:r?.operationId});return}for(let o of Object.keys(n))if(o.startsWith("2")){let a=n[o]?.content;if(a){for(let c of["application/json","application/json; charset=utf-8"])if(a[c]?.schema){let u=a[c].schema;return this.debugMode&&this.context.log.debug("Output schema extracted successfully",{path:e.path,operationId:r?.operationId,statusCode:o,contentType:c,schemaKeys:u&&typeof u=="object"?Object.keys(u).join(", "):"none"}),u}}}this.debugMode&&this.context.log.debug("No OpenAPI spec 2xx responses fond for outputSchema",{path:e.path,operationId:r?.operationId,statusCodes:Object.keys(n).join(", ")});return}catch(r){this.context.log.warn("Failed to extract outputSchema",{path:e.path,operationId:e.raw()?.operationId,error:r instanceof Error?r.message:String(r),stack:r instanceof Error?r.stack:void 0});return}}};async function gE(t,e){if(R("handler.mcp-server"),e.route.methods.some(a=>a.toUpperCase()!=="POST"))throw new w(`Invalid route config: mcpServerHandler may only use POST. Route '${e.route.path}' declares methods: [${e.route.methods.join(", ")}]`);let r={};t.headers.forEach((a,c)=>{r[c]=a});let n=e.route?.handler?.options??{},o=e.route?.path??"unknown-route",i=await cn.getInstance({opts:n,context:e,key:o});return Ys(r,e,()=>i.handleRequest(t,e))}s(gE,"mcpServerHandler");var sv=10,av=3e4,Er=class{static{s(this,"BackgroundLoader")}#e=new Map;#t;#n;#r;#o;#i={};constructor(e,r){if(typeof r=="number"){let n=r;this.#n=n*1e3,this.#o=av,this.#r=sv}else{let n=r;this.#n=n.ttlSeconds*1e3,this.#o=n.loaderTimeoutSeconds?n.loaderTimeoutSeconds*1e3:av,this.#r=sv}this.#t=e}#s(e){return e.expiry<=new Date}#c(e){let r=this.#i[e];return!(r===void 0||r===0)}#u(e){let r=this.#e.get(e);if(r&&!this.#s(r))return r.data}async get(e){let r=this.#u(e);if(r)return this.#l(e),r;if(this.#c(e))try{await hE(()=>this.#u(e)!==void 0||!this.#c(e),this.#o+this.#r+1,this.#r);let n=this.#u(e);if(n)return n}catch{}return this.#a(e)}#l(e){if(!this.#c(e)){let r=this.#a(e);Jt().waitUntil(r)}}async#a(e){try{this.#i[e]===void 0&&(this.#i[e]=0),this.#i[e]++;let r=await Promise.race([this.#t(e),scheduler.wait(this.#o)]);if(r===void 0)throw new w(`BackgroundLoader: Loader timed out after ${this.#o} ms.`);return this.#e.set(e,{data:r,expiry:new Date(Date.now()+this.#n)}),r}finally{this.#i[e]--}}};async function hE(t,e,r){let n=Date.now();for(;!t();){let o=Date.now()-n;if(o>e)throw new w(`BackgroundLoader: Timeout waiting for an on-going loader after ${o} ms.`);await scheduler.wait(r)}}s(hE,"waitUntilTrue");var yE={ttlSeconds:600,loaderTimeoutSeconds:30},cv=new Er(async()=>{let t=P.instance.zuploEdgeApiUrl;if(!t)throw new w("Zuplo edge API URL not configured");let e=P.instance.authApiJWT;if(!e)throw new w("Zuplo auth API JWT not configured");let r=P.instance.deploymentName;if(!r)throw new w("Deployment name not configured");let n=ke.ZUPLO_SERVICE_BUCKET_ID;if(!n)throw new w("ZUPLO_SERVICE_BUCKET_ID env not configured");let o=`${t}/zups/v1/buckets/${n}/mcp/resolve`,i=await j.fetch(o,{method:"POST",headers:{Authorization:`Bearer ${e}`,"User-Agent":P.instance.systemUserAgent,"Content-Type":"application/json"},body:JSON.stringify({deployment:r})});if(!i.ok){let u=await i.text().catch(()=>"");throw new w(`Failed to resolve MCP gateway configuration from ${o}: ${i.status} ${i.statusText}${u?` - ${u}`:""}`)}let c=(await i.json()).mcpGatewayConfig;if(!c||typeof c!="object")throw new w("Invalid MCP gateway configuration: not an object");if(!Array.isArray(c.servers))throw new w("Invalid MCP gateway configuration: servers must be an array");return c},yE);async function bE(t,e){if(R("handler.mcp-gateway"),e.route.methods.some(f=>f.toUpperCase()!=="POST"))throw new w(`Invalid route config: mcpGatewayHandler may only use POST. Route '${e.route.path}' declares methods: [${e.route.methods.join(", ")}]`);let r=e.route?.handler?.options??{},n=await cv.get("NO_KEY");e.log.debug(`Loaded configuration with ${n.servers?.length??0} servers`);let o=t.params.slug;if(!o)throw new Error("No MCP gateway slug provided in request");let i=n.servers.find(f=>f.slug===o);if(!i)throw new Error(`No MCP server configuration found for slug: ${o}`);let a={name:i.name,debugMode:r.debugMode},u=new TextEncoder().encode(JSON.stringify(i)),l=await crypto.subtle.digest("SHA-256",u),d=new Uint8Array(l),p=Array.from(d).map(f=>f.toString(16).padStart(2,"0")).join(""),m=`mcp-origin-config:${o}:${p.substring(0,32)}`;return e.log.debug(`Using MCP server key: ${m}`),wE({request:t,context:e,opts:a,key:m,origins:i.origins})}s(bE,"mcpGatewayHandler");async function wE({request:t,context:e,opts:r,key:n,origins:o}){let i={};t.headers.forEach((c,u)=>{i[u]=c});let a=await cn.getInstance({opts:r,context:e,key:n,origins:o});return Ys(i,e,()=>a.handleRequest(t,e))}s(wE,"mcpServerProxyHandler");function Be(t,e){let r={};for(let n in t){let o=t[n];Array.isArray(o)||(o=[o]);for(let i of o)if(i.param){if(n in e){let a=vE(n,e,i);a!==void 0&&uv(r,i.param,a)}else if(i?.required&&i.default!==void 0){let a=typeof i.default=="function"?i.default(e):i.default;uv(r,i.param,a)}}}return r}s(Be,"validateAndTransformRequest");function vE(t,e,r){let n=e[t];return r.transform&&(n=r.transform(e)),n===void 0&&r.default!==void 0&&(n=typeof r.default=="function"?r.default(e):r.default),typeof n=="number"&&(r.min!==void 0&&n<r.min&&(n=r.min),r.max!==void 0&&n>r.max&&(n=r.max)),n}s(vE,"getValue");function uv(t,e,r){if(r===void 0)return;let n=e.split("."),o=t;for(let i=0;i<n.length-1;i++){let a=n[i];o[a]||(o[a]={}),o=o[a]}o[n[n.length-1]]=r}s(uv,"setNestedProperty");async function _r(t){let e=new TextEncoder().encode(t),r=await crypto.subtle.digest({name:"SHA-256"},e);return[...new Uint8Array(r)].map(o=>o.toString(16).padStart(2,"0")).join("")}s(_r,"sha256");var lv=new Map;async function xe(t,e,r){let n,o=`${t}-${e}`,i=lv.get(o);return i!==void 0?n=i:(n=`zuplo-policy-${await _r(JSON.stringify({policyName:t,options:r}))}`,lv.set(t,n)),n}s(xe,"getPolicyCacheName");var xE=60,RE={openai:{"gpt-5":{model:"gpt-5",kind:"completions",status:"active",inputCostPerToken:125e-8,outputCostPerToken:1e-5},"gpt-5-nano":{model:"gpt-5-nano",kind:"completions",status:"active",inputCostPerToken:5e-8,outputCostPerToken:4e-7},"gpt-5-mini":{model:"gpt-5-mini",kind:"completions",status:"active",inputCostPerToken:25e-8,outputCostPerToken:2e-6},"gpt-5-nano-2025-08-07":{model:"gpt-5-nano-2025-08-07",kind:"completions",status:"active",inputCostPerToken:5e-8,outputCostPerToken:4e-7},"gpt-5-mini-2025-08-07":{model:"gpt-5-mini-2025-08-07",kind:"completions",status:"active",inputCostPerToken:25e-8,outputCostPerToken:2e-6},"gpt-5-2025-08-07":{model:"gpt-5-2025-08-07",kind:"completions",status:"active",inputCostPerToken:125e-8,outputCostPerToken:1e-5},"gpt-5-chat-latest":{model:"gpt-5-chat-latest",kind:"completions",status:"active",inputCostPerToken:125e-8,outputCostPerToken:1e-5},"gpt-4o":{model:"gpt-4o",kind:"completions",status:"active",inputCostPerToken:25e-7,outputCostPerToken:1e-5},"gpt-4o-2024-11-20":{model:"gpt-4o-2024-11-20",kind:"completions",status:"active",inputCostPerToken:25e-7,outputCostPerToken:1e-5},"gpt-4o-2024-08-06":{model:"gpt-4o-2024-08-06",kind:"completions",status:"active",inputCostPerToken:25e-7,outputCostPerToken:1e-5},"gpt-4o-2024-05-13":{model:"gpt-4o-2024-05-13",kind:"completions",status:"active",inputCostPerToken:5e-6,outputCostPerToken:15e-6},"gpt-4o-mini":{model:"gpt-4o-mini",kind:"completions",status:"active",inputCostPerToken:15e-8,outputCostPerToken:6e-7},"gpt-4o-mini-2024-07-18":{model:"gpt-4o-mini-2024-07-18",kind:"completions",status:"active",inputCostPerToken:15e-8,outputCostPerToken:6e-7},"gpt-4.1":{model:"gpt-4.1",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6},"gpt-4.1-mini":{model:"gpt-4.1-mini",kind:"completions",status:"active",inputCostPerToken:4e-7,outputCostPerToken:16e-7},"gpt-4.1-nano":{model:"gpt-4.1-nano",kind:"completions",status:"active",inputCostPerToken:1e-7,outputCostPerToken:4e-7},"gpt-4-turbo":{model:"gpt-4-turbo",kind:"completions",status:"active",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4-turbo-2024-04-09":{model:"gpt-4-turbo-2024-04-09",kind:"completions",status:"active",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4-turbo-preview":{model:"gpt-4-turbo-preview",kind:"completions",status:"active",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4-0125-preview":{model:"gpt-4-0125-preview",kind:"completions",status:"active",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4-1106-preview":{model:"gpt-4-1106-preview",kind:"completions",status:"active",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4-vision-preview":{model:"gpt-4-vision-preview",kind:"completions",status:"deprecated",inputCostPerToken:1e-5,outputCostPerToken:3e-5},"gpt-4":{model:"gpt-4",kind:"completions",status:"active",inputCostPerToken:3e-5,outputCostPerToken:6e-5},"gpt-4-0613":{model:"gpt-4-0613",kind:"completions",status:"active",inputCostPerToken:3e-5,outputCostPerToken:6e-5},"gpt-4-32k":{model:"gpt-4-32k",kind:"completions",status:"deprecated",inputCostPerToken:6e-5,outputCostPerToken:12e-5},"gpt-4-32k-0613":{model:"gpt-4-32k-0613",kind:"completions",status:"deprecated",inputCostPerToken:6e-5,outputCostPerToken:12e-5},"gpt-3.5-turbo":{model:"gpt-3.5-turbo",kind:"completions",status:"active",inputCostPerToken:5e-7,outputCostPerToken:15e-7},"gpt-3.5-turbo-0125":{model:"gpt-3.5-turbo-0125",kind:"completions",status:"active",inputCostPerToken:5e-7,outputCostPerToken:15e-7},"gpt-3.5-turbo-1106":{model:"gpt-3.5-turbo-1106",kind:"completions",status:"active",inputCostPerToken:1e-6,outputCostPerToken:2e-6},"gpt-3.5-turbo-16k":{model:"gpt-3.5-turbo-16k",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:4e-6},"text-embedding-3-large":{model:"text-embedding-3-large",kind:"embeddings",status:"active",inputCostPerToken:13e-8,outputCostPerToken:0},"text-embedding-3-small":{model:"text-embedding-3-small",kind:"embeddings",status:"active",inputCostPerToken:2e-8,outputCostPerToken:0},"text-embedding-ada-002":{model:"text-embedding-ada-002",kind:"embeddings",status:"active",inputCostPerToken:1e-7,outputCostPerToken:0}},anthropic:{"claude-opus-4-20250514":{model:"claude-opus-4-20250514",kind:"completions",status:"active",inputCostPerToken:15e-6,outputCostPerToken:75e-6},"claude-sonnet-4-20250514":{model:"claude-sonnet-4-20250514",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:15e-6},"claude-3-7-sonnet-20250219":{model:"claude-3-7-sonnet-20250219",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:15e-6},"claude-3-5-sonnet-20241022":{model:"claude-3-5-sonnet-20241022",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:15e-6},"claude-3-5-sonnet-20240620":{model:"claude-3-5-sonnet-20240620",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:15e-6},"claude-3-5-haiku-20241022":{model:"claude-3-5-haiku-20241022",kind:"completions",status:"active",inputCostPerToken:8e-7,outputCostPerToken:4e-6},"claude-3-opus-20240229":{model:"claude-3-opus-20240229",kind:"completions",status:"active",inputCostPerToken:15e-6,outputCostPerToken:75e-6},"claude-3-haiku-20240307":{model:"claude-3-haiku-20240307",kind:"completions",status:"active",inputCostPerToken:25e-8,outputCostPerToken:125e-8},"claude-2.1":{model:"claude-2.1",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6},"claude-2.0":{model:"claude-2.0",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6},"claude-instant-1.2":{model:"claude-instant-1.2",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6}},google:{"gemini-2.0-flash-exp":{model:"gemini-2.0-flash-exp",kind:"completions",status:"active",inputCostPerToken:15e-8,outputCostPerToken:6e-7},"gemini-2.0-flash":{model:"gemini-2.0-flash",kind:"completions",status:"active",inputCostPerToken:1e-7,outputCostPerToken:4e-7},"gemini-2.0-flash-lite":{model:"gemini-2.0-flash-lite",kind:"completions",status:"active",inputCostPerToken:75e-9,outputCostPerToken:3e-7},"gemini-2.5-pro":{model:"gemini-2.5-pro",kind:"completions",status:"active",inputCostPerToken:125e-8,outputCostPerToken:1e-5},"gemini-pro":{model:"gemini-pro",kind:"completions",status:"active",inputCostPerToken:5e-7,outputCostPerToken:15e-7},"gemini-embedding-001":{model:"gemini-embedding-001",kind:"embeddings",status:"active",inputCostPerToken:15e-8,outputCostPerToken:0}},mistral:{"mistral-large-latest":{model:"mistral-large-latest",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:6e-6},"mistral-large-2411":{model:"mistral-large-2411",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:6e-6},"mistral-large-2407":{model:"mistral-large-2407",kind:"completions",status:"active",inputCostPerToken:3e-6,outputCostPerToken:9e-6},"mistral-medium-latest":{model:"mistral-medium-latest",kind:"completions",status:"active",inputCostPerToken:4e-7,outputCostPerToken:2e-6},"mistral-small-latest":{model:"mistral-small-latest",kind:"completions",status:"active",inputCostPerToken:1e-7,outputCostPerToken:3e-7},"mistral-small-2409":{model:"mistral-small-2409",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6},"open-mistral-7b":{model:"open-mistral-7b",kind:"completions",status:"active",inputCostPerToken:25e-8,outputCostPerToken:25e-8},"open-mixtral-8x7b":{model:"open-mixtral-8x7b",kind:"completions",status:"active",inputCostPerToken:7e-7,outputCostPerToken:7e-7},"open-mixtral-8x22b":{model:"open-mixtral-8x22b",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:6e-6},"open-mistral-nemo":{model:"open-mistral-nemo",kind:"completions",status:"active",inputCostPerToken:3e-7,outputCostPerToken:3e-7},"codestral-latest":{model:"codestral-latest",kind:"completions",status:"active",inputCostPerToken:1e-6,outputCostPerToken:3e-6},"ministral-3b-latest":{model:"ministral-3b-latest",kind:"completions",status:"active",inputCostPerToken:4e-8,outputCostPerToken:4e-8},"ministral-8b-latest":{model:"ministral-8b-latest",kind:"completions",status:"active",inputCostPerToken:2e-6,outputCostPerToken:8e-6},"mistral-embed":{model:"mistral-embed",kind:"embeddings",status:"active",inputCostPerToken:1e-7,outputCostPerToken:0}}};async function ft(t){let e=Q.getLogger(t),r=await xe("supported-models","models",{}),n=new be(r,t),o=await n.get("models");if(o)return e.info("Using cached supported models data",{providersCount:Object.keys(o.modelsByProvider).length,providers:Object.keys(o.modelsByProvider)}),o.modelsByProvider;let i=new Headers({"content-type":"application/json"});je(i,t.requestId);let a=`${P.instance.zuploEdgeApiUrl}/v1/buckets/${ke.ZUPLO_SERVICE_BUCKET_ID}/providers`,c=await Te({retryDelayMs:100,retries:5},a,{method:"GET",headers:i}),u;if(c.status!==200){try{let d=await c.text(),p=JSON.parse(d);e.error("Gateway service unavailable, using fallback models",p)}catch{e.error("Gateway service unavailable, using fallback models")}t.log.info("AI Gateway: Using fallback model data due to service unavailability"),u=RE}else{let d=await c.json();e.info("Gateway service response received",{providersCount:Object.keys(d.data).length,providers:Object.keys(d.data),totalModels:Object.values(d.data).reduce((p,m)=>p+m.length,0)}),u={};for(let[p,m]of Object.entries(d.data)){let f=p.toLowerCase();u[f]={};for(let g of m)u[f][g.model]=g}}let l={modelsByProvider:u};return n.put("models",l,xE),u}s(ft,"getSupportedModels");function _t(t,e,r,n,o,i){let a=e.toLowerCase(),c=o[a];if(!c)return i.warn("Provider not found in supported models list",{provider:e,model:t}),0;let u=c[t];if(!u)return i.warn("Model not found in supported models list for provider",{provider:e,model:t}),0;let l=r*u.inputCostPerToken,d=n*u.outputCostPerToken;return l+d}s(_t,"calculateModelCost");function dv(t,e,r,n){let o=e.toLowerCase(),i=n[o];if(!i)return!1;let a=i[t];return a?a.kind===r&&a.status==="active":!1}s(dv,"isModelSupported");function pv(t,e,r){let n=t.toLowerCase(),o=r[n];return o?Object.values(o).filter(i=>i.kind===e&&i.status==="active").map(i=>i.model):[]}s(pv,"getModelsByProviderAndKind");var xm=class{static{s(this,"GatewayServiceClient")}baseUrl;constructor(e){this.baseUrl=e??P.instance.zuploEdgeApiUrl}async fetchCurrentMeters(e,r){let n=`${this.baseUrl}/v1/hierarchical-quota/${e}`,o=new Headers({"Content-Type":"application/json"});je(o,r.requestId);let i=await fetch(n,{method:"GET",headers:o});if(!i.ok)throw new le(`Failed to fetch meters: ${i.status} ${i.statusText}`);return await i.json()}async checkHierarchicalQuotaLimits(e,r){let n=`${this.baseUrl}/v1/hierarchical-quota/${e}/limits`,o=new Headers({"Content-Type":"application/json"});je(o,r.requestId);let i=await fetch(n,{method:"GET",headers:o});if(!i.ok)throw new le(`Failed to check quota limits: ${i.status} ${i.statusText}`);return await i.json()}async incrementMeters(e,r,n){let o=`${this.baseUrl}/v1/hierarchical-quota/${e}`,i=new Headers({"Content-Type":"application/json"});je(i,n.requestId);let a=await fetch(o,{method:"POST",headers:i,body:JSON.stringify({increments:r})});if(!a.ok)throw new le(`Failed to increment meters: ${a.status} ${a.statusText}`)}},vm=null,sa={get instance(){return vm===null&&(vm=new xm),vm}};var mv=Ne("zuplo:policies:AIGatewayMeteringInboundPolicy"),fv=Symbol("ai-gateway-meter-increments"),st=class t extends Re{static{s(this,"AIGatewayMeteringInboundPolicy")}static setIncrements(e,r){pe.set(e,fv,r)}static getIncrements(e){return pe.get(e,fv)??{}}constructor(e,r){super(e,r),R("policy.inbound.ai-gateway-metering-inbound")}async handler(e,r){let n=Date.now(),o=Q.getLogger(r),i=s((a,c)=>{if(this.options.throwOnFailure)throw new le(a,{cause:c});o.error(c,a)},"throwOrLog");try{let a=e.user?.configuration;if(!a)throw new w(`AIGatewayMeteringInboundPolicy '${this.policyName}' - No configuration found in request.user. Ensure ai-gateway-inbound policy runs first.`);let c=a;if(!c.id)throw new w(`AIGatewayMeteringInboundPolicy '${this.policyName}' - Configuration ID not found.`);let u=await this.fetchCurrentMeters(c.id,r,o),l=this.checkWarnings(c,u);for(let p of l)r.analyticsContext.addAnalyticsEvent(1,Oe.AI_GATEWAY_WARNING_COUNT,{type:`quota-${p.type}-${p.period}`,configId:c.id});let d=await this.checkHierarchicalQuotaLimits(c.id,r,o);return d.violation?(r.analyticsContext.addAnalyticsEvent(1,Oe.AI_GATEWAY_BLOCKED_COUNT,{type:`quota-${d.violation.meter}-${d.violation.period}`,configId:c.id}),this.createHierarchicalQuotaExceededResponse(e,r,d.violation)):(r.addResponseSendingFinalHook(async()=>{try{let p=t.getIncrements(r);mv(`AIGatewayMeteringInboundPolicy '${this.policyName}' - increments ${JSON.stringify(p)}`),Object.keys(p).length>0&&await t.incrementMetersInternal(c.id,p,r,o)}catch(p){i(`AIGatewayMeteringInboundPolicy '${this.policyName}' - Failed to increment meters`,p)}}),e)}catch(a){if(a instanceof w)throw a;return i(`AIGatewayMeteringInboundPolicy '${this.policyName}' - Error`,a),e}finally{let a=Date.now()-n;mv(`AIGatewayMeteringInboundPolicy '${this.policyName}' - latency ${a}ms`)}}async fetchCurrentMeters(e,r,n){try{return await sa.instance.fetchCurrentMeters(e,r)}catch(o){throw n.error(o,`AIGatewayMeteringInboundPolicy '${this.policyName}' - Failed to fetch meters`),o}}async checkHierarchicalQuotaLimits(e,r,n){try{return await sa.instance.checkHierarchicalQuotaLimits(e,r)}catch(o){throw n.error(o,`AIGatewayMeteringInboundPolicy '${this.policyName}' - Failed to check hierarchical quota limits`),o}}static async incrementMeters(e,r,n){let o=Q.getLogger(n);return t.incrementMetersInternal(e,r,n,o)}static async incrementMetersInternal(e,r,n,o){try{await sa.instance.incrementMeters(e,r,n)}catch(i){throw o.error(i,"AIGatewayMeteringInboundPolicy - Failed to increment meters"),i}}checkWarnings(e,r){let n=[];for(let[o,i]of Object.entries(e.limits)){if(!i)continue;let a=r.meters[o];if(!a)continue;let c=this.checkQuotaWarning(i.daily,a.daily,o,"daily");c&&n.push(c);let u=this.checkQuotaWarning(i.monthly,a.monthly,o,"monthly");u&&n.push(u)}return n}checkQuotaWarning(e,r,n,o){if(!e?.warning?.enabled||!e?.warning?.threshold||!e?.limit)return null;let i=e.warning.threshold/100*e.limit;return r>=i?{type:n,period:o}:null}createHierarchicalQuotaExceededResponse(e,r,n){let o=`${n.period} ${n.meter} quota exceeded in configuration '${n.configLabel}'. Limit: ${n.limit}, Current: ${n.currentUsage}`;return D.tooManyRequests(e,r,{detail:o})}};async function lr(t,e){if(!P.instance.remoteLogURL){Q.getLogger(t).debug("Remote log URL is not configured, skipping analytics");return}t.analyticsContext.addAnalyticsEvent(parseFloat(e.cost.toFixed(10)),Oe.AI_GATEWAY_COST_SUM,{model:e.model,provider:e.provider,configId:e.configId}),t.analyticsContext.addAnalyticsEvent(1,Oe.AI_GATEWAY_REQUEST_COUNT,{model:e.model,provider:e.provider,configId:e.configId}),t.analyticsContext.addAnalyticsEvent(e.promptTokens,Oe.AI_GATEWAY_TOKEN_SUM,{model:e.model,provider:e.provider,configId:e.configId,tokenType:"prompt"}),t.analyticsContext.addAnalyticsEvent(e.completionTokens,Oe.AI_GATEWAY_TOKEN_SUM,{model:e.model,provider:e.provider,configId:e.configId,tokenType:"completion"});let r=t.analyticsContext.flushAnalyticsEvents();new Fr(t,{endpoint:`${P.instance.remoteLogURL}/v2/analytics`}).pushEvents(r)}s(lr,"sendStreamAnalytics");var gv={model:{param:"model",required:!0,default:"gpt-4o-mini"},messages:{param:"messages",required:!0},temperature:{param:"temperature",default:1,min:0,max:2},top_p:{param:"top_p",default:1,min:0,max:1},n:{param:"n",default:1,min:1,max:128},stream:{param:"stream",default:!1},stop:{param:"stop"},max_tokens:{param:"max_tokens",min:0},presence_penalty:{param:"presence_penalty",default:0,min:-2,max:2},frequency_penalty:{param:"frequency_penalty",default:0,min:-2,max:2},logit_bias:{param:"logit_bias"},user:{param:"user"},seed:{param:"seed"},functions:{param:"functions"},function_call:{param:"function_call"},tools:{param:"tools"},tool_choice:{param:"tool_choice"},parallel_tool_calls:{param:"parallel_tool_calls"},response_format:{param:"response_format"},logprobs:{param:"logprobs",default:!1},top_logprobs:{param:"top_logprobs",min:0,max:20},max_completion_tokens:{param:"max_completion_tokens"},service_tier:{param:"service_tier"},stream_options:{param:"stream_options"}},PE={model:{param:"model",required:!0,default:"text-embedding-3-small"},input:{param:"input",required:!0},user:{param:"user"},dimensions:{param:"dimensions"},encoding_format:{param:"encoding_format",default:"float"}},aa=class{static{s(this,"OpenAIProvider")}name="openai";async chatComplete(e,r){let n=Be(gv,e),o=await fetch("https://api.openai.com/v1/chat/completions",{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify(n)});if(!o.ok){let a=await o.json();throw new Error(`OpenAI API error: ${a.error?.message||"Unknown error"}`)}return{...await o.json(),provider:"openai"}}async chatCompleteStream(e,r,n){let o=Be(gv,{...e,stream:!0,stream_options:{include_usage:!0}}),i=await fetch("https://api.openai.com/v1/chat/completions",{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify(o)});if(!i.ok){let m=await i.json();throw new Error(`OpenAI API error: ${m.error?.message||"Unknown error"}`)}if(!i.body)throw new Error("No response body received from OpenAI API");let a=n.custom.userContext,c="",u=new TextDecoder,l=new TransformStream({transform(m,f){f.enqueue(m);let g=u.decode(m);c+=g,!c.includes('"usage"')&&c.length>4096&&(c=c.slice(-32));let b;for(;(b=c.indexOf(`
201
201
 
202
202
  `))!==-1;){let h=c.slice(0,b);if(c=c.slice(b+2),!h.includes('"usage"'))continue;let x="";for(let v of h.split(`
203
203
  `))if(v.startsWith("data: ")){x=v.slice(6);break}if(x&&x.trim()!=="[DONE]")try{let v=JSON.parse(x);if(v.usage&&v.usage.total_tokens>0){if(a){let C=v.usage;n.waitUntil((async()=>{try{let A=C.prompt_tokens||0,O=C.completion_tokens||0,S=C.total_tokens||0,M=Q.getLogger(n),z=await ft(n),Z=_t(e.model,"openai",A,O,z,M);M.info("OpenAI streaming usage tracked",{userId:a.sub,promptTokens:A,completionTokens:O,totalTokens:S,model:e.model,provider:"openai",cost:Z});let $=a.configuration?.id;if($){let E={requests:1,tokens:S,costs:Z};await st.incrementMeters($,E,n)}else n.log.warn("No configuration ID found for streaming usage metering");await lr(n,{promptTokens:A,completionTokens:O,model:e.model,provider:"openai",configId:$||"unknown",cost:Z})}catch(A){n.log.error("Error processing OpenAI streaming token usage",{error:A})}})())}c="";break}}catch{}}if(c.length>8192){let h=c.lastIndexOf(`