Documentation
¶
Overview ¶
Package api2mcp turns an existing HTTP API into an MCP server.
It is OpenAPI-first and layered: a Source (an OpenAPI spec, a live router, a manual list) is normalized into a framework-agnostic intermediate representation, the engine maps that to MCP tools backed by real HTTP calls, and a curation layer decides which tools are safe to expose and how their responses are shaped. The MCP protocol itself is handled by mark3labs/mcp-go.
Minimal use:
src, _ := openapi.FromFile("openapi.yaml")
srv := api2mcp.New(src, api2mcp.WithBaseURL("https://api.internal"))
srv.ServeStdio(context.Background())
Index ¶
- func StdAuditLogger(e engine.AuditEvent)
- type Option
- func ExcludeOperations(ids ...string) Option
- func ExcludePaths(globs ...string) Option
- func ExcludeTags(tags ...string) Option
- func IncludeOperations(ids ...string) Option
- func IncludePaths(globs ...string) Option
- func IncludeTags(tags ...string) Option
- func ReadOnly() Option
- func WithAnnotator(fn engine.AnnotateFunc) Option
- func WithAuditLogger(fn engine.AuditFunc) Option
- func WithBaseURL(url string) Option
- func WithEndpointPath(p string) Option
- func WithFilter(keep func(ir.Operation) bool) Option
- func WithForwardHeaders(names ...string) Option
- func WithHTTPClient(c *http.Client) Option
- func WithMarkDestructive(operationIDs ...string) Option
- func WithMaxResponseBytes(n int) Option
- func WithName(name string) Option
- func WithRateLimit(perSecond float64, burst int) Option
- func WithResponseShaper(s engine.ShapeFunc) Option
- func WithStaticHeader(key, value string) Option
- func WithVersion(v string) Option
- type Server
- func (s *Server) HTTPHandler(ctx context.Context) (http.Handler, error)
- func (s *Server) MCPServer(ctx context.Context) (*server.MCPServer, error)
- func (s *Server) ServeHTTP(ctx context.Context, addr string) error
- func (s *Server) ServeStdio(ctx context.Context) error
- func (s *Server) Tools(ctx context.Context) ([]engine.Tool, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StdAuditLogger ¶
func StdAuditLogger(e engine.AuditEvent)
StdAuditLogger logs each tool call to the standard logger in a single line:
api2mcp: getUser GET /users/{id} -> 200 (12ms)
Pass it to WithAuditLogger for out-of-the-box observability.
Types ¶
type Option ¶
type Option func(*options)
Option configures a Server.
func ExcludeOperations ¶
ExcludeOperations drops the operations with the given ids.
func ExcludePaths ¶
ExcludePaths drops operations whose path matches one of the globs (e.g. "/admin/*", "/internal/*").
func ExcludeTags ¶
ExcludeTags drops operations carrying any of the given tags.
func IncludeOperations ¶
IncludeOperations keeps only the operations with the given ids.
func IncludePaths ¶
IncludePaths keeps only operations whose path matches one of the globs (e.g. "/public/*").
func IncludeTags ¶
IncludeTags keeps only operations carrying at least one of the given tags (a whitelist). Combine with other Include* options — they are OR-ed.
func ReadOnly ¶
func ReadOnly() Option
ReadOnly exposes only side-effect free operations (GET/HEAD). This is the recommended default when pointing an LLM at a real API.
func WithAnnotator ¶
func WithAnnotator(fn engine.AnnotateFunc) Option
WithAnnotator adjusts each tool's safety annotations after they are derived from the HTTP method. For the common case, prefer WithMarkDestructive.
func WithAuditLogger ¶
WithAuditLogger registers a callback invoked after every tool call with the operation, upstream status, duration and any error — so LLM-driven traffic against your API is observable. Pass api2mcp.StdAuditLogger for a sane default.
func WithBaseURL ¶
WithBaseURL sets the upstream API root that tool calls are sent to. Required unless the Source already resolves absolute URLs.
func WithEndpointPath ¶
WithEndpointPath sets the HTTP path the streamable-HTTP transport listens on (default "/mcp").
func WithFilter ¶
WithFilter adds a custom predicate; operations for which it returns false are dropped. (Implemented as an exclude of the negation.)
func WithForwardHeaders ¶
WithForwardHeaders forwards the named headers from the incoming MCP HTTP request to every upstream call — typically "Authorization" so a client's Bearer/JWT reaches your protected API. Only meaningful with ServeHTTP.
func WithHTTPClient ¶
WithHTTPClient overrides the HTTP client used for upstream calls.
func WithMarkDestructive ¶
WithMarkDestructive forces the destructiveHint (and clears readOnlyHint) on the named operations — for endpoints whose method understates their risk, e.g. a POST /charge or POST /accounts/{id}/close. MCP clients use this to warn or require confirmation before the LLM invokes them.
func WithMaxResponseBytes ¶
WithMaxResponseBytes caps the size of each tool's rendered response so a large upstream payload cannot blow up the model's context. 0 means no limit.
func WithRateLimit ¶
WithRateLimit caps how often each tool may be invoked, using a per-tool token bucket. perSecond is the sustained rate; burst is the most back-to-back calls allowed. A denied call fails fast with a clear message rather than blocking. This protects the upstream API from a runaway LLM.
func WithResponseShaper ¶
WithResponseShaper overrides how upstream responses are rendered for the LLM.
func WithStaticHeader ¶
WithStaticHeader adds a header sent on every upstream call (e.g. an API key).
func WithVersion ¶
WithVersion sets the MCP server version advertised to clients.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server wires a Source to the engine and an MCP transport.
func (*Server) HTTPHandler ¶
HTTPHandler returns the streamable-HTTP handler so callers can mount the MCP endpoint inside their own http.Server / router instead of calling ServeHTTP.
func (*Server) MCPServer ¶
MCPServer builds a ready-to-serve mcp-go server with all tools registered.
func (*Server) ServeHTTP ¶
ServeHTTP builds the server and serves it over the MCP streamable-HTTP transport at addr (e.g. ":8080"). This is the load-balancer-friendly transport suitable for remote/hosted deployments. Forwarded auth headers (see WithForwardHeaders) are passed through to upstream calls per request.
func (*Server) ServeStdio ¶
ServeStdio builds the server and serves it over stdio (for desktop clients like Claude Desktop, Cursor, Zed).
Directories
¶
| Path | Synopsis |
|---|---|
|
adapter
|
|
|
chiadapter
Package chiadapter exposes a live chi router as an api2mcp Source.
|
Package chiadapter exposes a live chi router as an api2mcp Source. |
|
echoadapter
Package echoadapter exposes a live *echo.Echo as an api2mcp Source.
|
Package echoadapter exposes a live *echo.Echo as an api2mcp Source. |
|
fiberadapter
Package fiberadapter exposes a live *fiber.App as an api2mcp Source.
|
Package fiberadapter exposes a live *fiber.App as an api2mcp Source. |
|
ginadapter
Package ginadapter exposes a live *gin.Engine as an api2mcp Source, for embedded mode: mount an MCP endpoint inside an existing Gin app without a separate process or an OpenAPI spec.
|
Package ginadapter exposes a live *gin.Engine as an api2mcp Source, for embedded mode: mount an MCP endpoint inside an existing Gin app without a separate process or an OpenAPI spec. |
|
cmd
|
|
|
api2mcp
command
Command api2mcp is a standalone binary that serves an existing HTTP API as an MCP server, configured entirely from a YAML file — no Go code required.
|
Command api2mcp is a standalone binary that serves an existing HTTP API as an MCP server, configured entirely from a YAML file — no Go code required. |
|
Package engine is the framework-agnostic core: it turns ir.Operations into MCP tools (with raw JSON Schema inputs) and wires each tool's handler to the HTTP Executor.
|
Package engine is the framework-agnostic core: it turns ir.Operations into MCP tools (with raw JSON Schema inputs) and wires each tool's handler to the HTTP Executor. |
|
examples
|
|
|
embedded-chi
command
Command embedded-chi mounts an MCP endpoint at /mcp inside an existing chi router.
|
Command embedded-chi mounts an MCP endpoint at /mcp inside an existing chi router. |
|
embedded-echo
command
Command embedded-echo mounts an MCP endpoint at /mcp inside an existing Echo app.
|
Command embedded-echo mounts an MCP endpoint at /mcp inside an existing Echo app. |
|
embedded-fiber
command
Command embedded-fiber mounts an MCP endpoint at /mcp inside an existing Fiber app.
|
Command embedded-fiber mounts an MCP endpoint at /mcp inside an existing Fiber app. |
|
embedded-gin
command
Command embedded-gin shows embedded mode: an existing Gin API mounts its own MCP endpoint at /mcp in the same process, with no OpenAPI spec and no separate server.
|
Command embedded-gin shows embedded mode: an existing Gin API mounts its own MCP endpoint at /mcp in the same process, with no OpenAPI spec and no separate server. |
|
openapi-http
command
Command openapi-http serves an OpenAPI spec as an MCP server over the streamable-HTTP transport, forwarding the caller's Authorization header to the upstream API and capping response size.
|
Command openapi-http serves an OpenAPI spec as an MCP server over the streamable-HTTP transport, forwarding the caller's Authorization header to the upstream API and capping response size. |
|
openapi-stdio
command
Command openapi-stdio is the M0 spike: it loads an OpenAPI spec and serves every operation as an MCP tool over stdio.
|
Command openapi-stdio is the M0 spike: it loads an OpenAPI spec and serves every operation as an MCP tool over stdio. |
|
Package ir defines the framework-agnostic intermediate representation that every Source normalizes into.
|
Package ir defines the framework-agnostic intermediate representation that every Source normalizes into. |
|
Package policy is the curation layer (L4): it decides which operations are safe and appropriate to expose as MCP tools.
|
Package policy is the curation layer (L4): it decides which operations are safe and appropriate to expose as MCP tools. |
|
schema
|
|
|
reflectschema
Package reflectschema enriches a Source's operations with request-body JSON Schemas reflected from Go structs.
|
Package reflectschema enriches a Source's operations with request-body JSON Schemas reflected from Go structs. |
|
Package source defines how operations are discovered.
|
Package source defines how operations are discovered. |
|
openapi
Package openapi turns an OpenAPI 3 document into ir.Operations.
|
Package openapi turns an OpenAPI 3 document into ir.Operations. |
|
route
Package route holds the shared logic every framework adapter uses to turn a (method, path) route into an ir.Operation.
|
Package route holds the shared logic every framework adapter uses to turn a (method, path) route into an ir.Operation. |
|
swaggo
Package swaggo turns a swaggo-generated Swagger (OpenAPI 2.0) document into an api2mcp Source.
|
Package swaggo turns a swaggo-generated Swagger (OpenAPI 2.0) document into an api2mcp Source. |