Skip to content

HTTP & MCP

Fetch the module first: go get github.com/yshengliao/gortexa@v0.27.0. Every snippet below is compile-verified against v0.27.0.

Builds a ServeMux whose error handler funnels through apperr, with a protojson marshaler and Authorization-header forwarding.

import (
"github.com/yshengliao/gortexa/apperr"
"github.com/yshengliao/gortexa/httpcompat"
)
gateway := httpcompat.NewServeMux(apperr.Default)
// register grpc-gateway handlers against gateway + the loopback conn
handler := httpcompat.MaxBodyBytes(gateway) // caps the body at 1 MiB
handler = httpcompat.CORS(handler, cfg.Server)

OpenAPIRoute(next, cfg, specPath) mounts an OpenAPI spec.

Exposes RPCs annotated with gortexa.ai.v1 as MCP tools, dispatched over the loopback through the same interceptor chain.

import (
"github.com/yshengliao/gortexa/apperr"
"github.com/yshengliao/gortexa/mcp"
)
descs, err := mcp.ServiceDescriptors("resource.v1.ResourceService")
if err != nil {
return err
}
bridge, err := mcp.NewBridge(conn, descs, apperr.Default, cfg.Observ)
if err != nil {
return err
}
bridge.SetAllowedOrigins(cfg.Server.CORSOrigins)
app.SetMCPHandler(bridge.Handler())

Export tool schemas offline with mcp.ExportSchemas(format, descs), where format is mcp / openai / gemini.