nic-6443 commented on code in PR #13942: URL: https://github.com/apache/apisix/pull/13942#discussion_r4022007069
########## apisix/plugins/openapi-to-mcp.lua: ########## @@ -0,0 +1,187 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +local core = require("apisix.core") +local upstream = require("apisix.upstream") +local streamable_http = require("apisix.plugins.openapi-to-mcp.transport.streamable_http") +local mcp_sse = require("apisix.plugins.openapi-to-mcp.transport.sse") +local ngx = ngx +local pairs = pairs + +local schema = { + type = "object", + properties = { + transport = { + description = "The transport mechanisms for client-server communication", + type = "string", + default = "sse", + enum = {"sse", "streamable_http"}, + }, + openapi_url = { + description = "URL of the OpenAPI specification document", + type = "string", + minLength = 1, + }, + base_url = { + description = "Base URL of the external service", + type = "string", + minLength = 1, + }, + headers = { + description = "Headers to include in requests to the external service", + type = "object", + minProperties = 0, + patternProperties = { + ["^[^:]+$"] = { + oneOf = { + { type = "string" } + } + } + }, + }, + flatten_parameters = { + description = "Whether to flatten query and path parameters " .. + "in the tool inputSchema. When false (default), " .. + "parameters are nested under queryParameters or pathParameters. " .. + "When true, parameters are placed directly in properties.", + type = "boolean", + default = false, + }, + }, + required = { "openapi_url", "base_url" }, +} + +local plugin_name = "openapi-to-mcp" + +local _M = { + version = 0.1, + priority = 540, + name = plugin_name, + schema = schema, +} + + +function _M.check_schema(conf) + return core.schema.check(schema, conf) +end + + +-- Resolve base_url and headers against request variables. +local function resolve_conf(conf, ctx) + local base_url, err = core.utils.resolve_var(conf.base_url, ctx.var) + if err then + core.log.error("failed to resolve variable for base_url: ", + conf.base_url, ", error: ", err) + base_url = conf.base_url + end + + local headers = {} + for key, value in pairs(conf.headers or {}) do + local resolved_value, herr = core.utils.resolve_var(value, ctx.var) + if herr then + core.log.error("failed to resolve variable for header, key: ", key, + ", error: ", herr) + resolved_value = value + end + headers[key] = resolved_value + end + + return base_url, headers +end + + +local function set_placeholder_upstream(ctx) + -- before_proxy runs after set_upstream()/pick_server(), so the request still + -- needs a resolvable upstream even though the response is produced in-process + -- and this address is never dialled. Port 1 is used so that a bug which let + -- the request through would fail fast instead of hanging. + local up_conf = { + name = "openapi-to-mcp-inprocess", + type = "roundrobin", + nodes = { { host = "127.0.0.1", port = 1, weight = 1, priority = 0 } }, + scheme = "http", + } + + local matched_route = ctx.matched_route + up_conf.parent = matched_route + local upstream_key = up_conf.type .. "#route_" .. matched_route.value.id .. "_openapi_to_mcp" + + ctx.var.upstream_scheme = "http" + ctx.var.upstream_host = "127.0.0.1" + ctx.var.upstream_port = 1 + upstream.set(ctx, upstream_key, ctx.conf_version, up_conf) +end Review Comment: Can we refer to the approach of ai-proxy and use the `bypass_nginx_upstream` variable? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
