om

package module
v0.0.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

om

CI Go Go Reference MIT License Discord

A Go gopher biting into an OpenAPI document

om serves an OpenAPI document or a GraphQL API as an MCP server, with one tool per API operation.

Install

go install github.com/holon-sh/om/cmd/om@latest

Usage

om serve https://example.com/openapi.yaml

om serve https://api.example.com works too when the API publishes an API catalog or a service-desc link; failing those, it tries the recommended openapi.json and openapi.yaml under the URL.

The server listens at http://127.0.0.1:3000/mcp; set HOST and PORT to change that. om serve lists any operations it skipped and the environment variables that hold your API credentials. Server variables default to the document's values; override one with OM_ and its name in upper case.

The server has no authentication of its own. Put it behind one before exposing it beyond localhost.

GraphQL
om serve -bearer https://api.github.com/graphql operations/*.graphql

An endpoint followed by operations files is a GraphQL source. Each named query or mutation in the files becomes one tool, named after the operation, and a fragment defined in one file may be spread in another:

"""
The signed-in user's login and their most recently pushed repositories.
"""
query Viewer("How many repositories, at most 100." $count: Int = 10) {
  viewer {
    login
    repositories(first: $count, orderBy: {field: PUSHED_AT, direction: DESC}) {
      nodes { name url isPrivate }
    }
  }
}

The tool's input is the operation's variables, and its result is the response's data, with errors beside it when the result is partial. A query is marked read-only. om serve lists any operation it skipped, such as a subscription or one that does not validate against the schema.

om reads the schema by introspection at startup, sending the same credential its tools will send, unless -schema names a file of SDL or an introspection result. -bearer sends OM_TOKEN as a bearer token. -header X-Api-Key sends OM_X_API_KEY in that header. Without either, om sends no credential.

Embed

Compile a document once, store the model, and serve it wherever you need it:

target, err := discover.ParseTarget("https://api.example.com")
offers := discover.Offers(ctx, http.DefaultTransport, target) // every OpenAPI description it publishes
data, at, err := discover.Get(ctx, http.DefaultTransport, offers[0].URL)
compiled, err := om.Compile(ctx, om.OpenAPI{Data: data, Location: at.URL()})
model, err := json.Marshal(compiled.Document)

var served om.Document
err = json.Unmarshal(model, &served)
handler, err := served.Handler(func(_ context.Context, name string) (string, bool) { return os.LookupEnv(name) }, http.DefaultTransport)
http.Handle("/mcp", handler)

A GraphQL API compiles the same way, from its schema and operations:

lookup := func(_ context.Context, name string) (string, bool) { return os.LookupEnv(name) } // OM_TOKEN
schema, err := om.Introspect(ctx, "https://api.github.com/graphql", om.BearerToken{}, lookup, http.DefaultTransport) // or SDL
compiled, err := om.Compile(ctx, om.GraphQL{
	Endpoint:   "https://api.github.com/graphql",
	Schema:     schema,
	Operations: map[string][]byte{"viewer.graphql": viewer},
	Credential: om.BearerToken{},
})

License

MIT. The om gopher is based on the Go gopher by Renee French, licensed under CC BY 4.0.

Documentation

Overview

Package om serves an API as an MCP server, one tool per operation. Compile turns an OpenAPI document, or a GraphQL schema and trusted documents, into a Document: a model a host stores as JSON and serves with Document.Handler.

This package is the whole public surface for compiling and serving. A host never sees an operation's protocol.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Introspect added in v0.0.15

func Introspect(ctx context.Context, endpoint string, credential Credential, lookup func(ctx context.Context, name string) (string, bool), transport http.RoundTripper) ([]byte, error)

Introspect fetches the schema of the GraphQL API at endpoint, as the introspection result GraphQL.Schema takes. It sends credential, whose input lookup supplies, through transport, within 30 seconds in all, and reads up to 32 MiB, the limits discover.Get reads an OpenAPI description within: a schema is a document, and the published schemas of large APIs exceed the 1 MiB a tool's result may hold. A host calls it while compiling, never while serving: a Document's tools never depend on a live schema.

Types

type APIKeyHeader added in v0.0.15

type APIKeyHeader struct {
	Name string
}

APIKeyHeader sends an input as the header Name: OM_X_API_KEY for X-Api-Key. Name may be Authorization, for APIs that take a key there without the Bearer scheme.

type Authorization added in v0.0.6

type Authorization = core.Authorization

Authorization is the RFC 8414 §2 issuer of the authorization server that issues an access token and the scopes the operations that send it require.

type BearerToken added in v0.0.15

type BearerToken struct{}

BearerToken sends the input OM_TOKEN as an RFC 6750 §2.1 bearer token. The token is one the host stores, such as an API key or a personal access token. A GraphQL schema names no OAuth issuer, and a GraphQL endpoint rarely answers with an RFC 9728 challenge that would, so a GraphQL source declares no OAuth authorization.

type Compiled added in v0.0.13

type Compiled struct {
	// Document is the model a host stores and serves.
	Document Document
	// Omitted are the operations Document leaves out, each with its reason: first those the protocol cannot compile,
	// then those the rules every protocol shares leave out, each in source order.
	Omitted []Omission
}

Compiled is what one source compiles to.

func Compile added in v0.0.13

func Compile(ctx context.Context, source Source) (Compiled, error)

Compile turns source into a Document and the operations it left out. Every protocol ends in the same rules: an operation is left out when its name is not an MCP tool name or is taken, when its schemas do not compile, or when an input it needs takes the name of an input from elsewhere in the source; operations and inputs are sorted by name; and only the definitions kept tools reach are stored. It fails when no operation is left.

type Credential added in v0.0.3

type Credential interface {
	// contains filtered or unexported methods
}

Credential is how a GraphQL API authenticates every request: a BearerToken or an APIKeyHeader. om names the input that holds the secret, as it does for an OpenAPI security scheme, and the host supplies its value.

type Document added in v0.0.3

type Document struct {
	Title       string
	Version     string
	Description string
	// Server is the URL every operation is sent to. Its {name} variables are filled from the Inputs that have a
	// Default. A host may replace it with another URL template of the same API; holon replaces it with the app's URL.
	Server string
	Inputs []Input
	// contains filtered or unexported fields
}

Document is a compiled API: its identity, its server, the inputs a host supplies, and its tools, each with the way its protocol reaches the API. Its JSON encoding is the model a host stores between compiling and serving.

A Document is servable when Compile made it, or when it decoded from a model of a protocol and format this version of om serves. Any other Document, including the zero one, decodes without error and is refused by every method, so a host reports one refusal, from Document.Implementation, wherever it asks.

func (Document) Handler added in v0.0.3

func (d Document) Handler(lookup func(ctx context.Context, name string) (string, bool), transport http.RoundTripper) (http.Handler, error)

Handler serves the document's operations as MCP 2026-07-28 tools over Streamable HTTP, statelessly. lookup supplies an input's value by name; transport carries the upstream requests. It refuses a model it cannot serve with an error, never a panic.

func (Document) Implementation added in v0.0.7

func (d Document) Implementation() (*mcp.Implementation, *mcp.ServerOptions, error)

Implementation is the MCP server om serves the document as and the options it serves it with. It refuses a model of a protocol or format this version of om does not serve.

func (Document) MarshalJSON added in v0.0.13

func (d Document) MarshalJSON() ([]byte, error)

MarshalJSON writes the model: the protocol's key with its format first, then the shared fields, then the operations in the protocol's own shape. The same Document always encodes to the same bytes, so a host may store it by digest.

func (Document) Tool added in v0.0.10

func (d Document) Tool(name string) (*mcp.Tool, error)

Tool is the tool named name, standalone, as tools/list shows it, with its schemas as json.RawMessage. It is nil when the document has no such tool.

func (Document) Tools added in v0.0.7

func (d Document) Tools(cursor string) (*mcp.ListToolsResult, error)

Tools is the page of tools a tools/list cursor names: as many standalone tools as fit in 256 KiB of names, descriptions and schemas, and at least one. Its cacheScope is public, since a model's tools do not depend on who asks. A cursor Tools did not issue is invalid params.

func (*Document) UnmarshalJSON added in v0.0.13

func (d *Document) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a model. A model of a protocol or format this version of om does not serve decodes without its operations, whose shape it cannot know, and every method then refuses it. A model of a format it serves decodes strictly: an operation it cannot send, such as one with an unknown credential, fails the decode.

type GraphQL added in v0.0.15

type GraphQL struct {
	// Endpoint is the URL om sends every operation to, as a GraphQL over HTTP POST: https, with a host, and without
	// credentials, a query or a fragment. It is the Document's server.
	Endpoint string
	// Schema is the API's schema: SDL, or the JSON of an introspection result, which [Introspect] fetches.
	Schema []byte
	// Operations are the trusted documents by file name: executable GraphQL documents whose named operations become
	// tools. A fragment defined in one may be spread in another.
	Operations map[string][]byte
	// Credential is what every operation sends to authenticate. It is nil for an API that needs none.
	Credential Credential
}

GraphQL is a GraphQL API and the trusted documents that choose its tools: each named query or mutation becomes one tool, named after the operation, whose input is its variables and whose result is its response's data and errors. A GraphQL schema names neither the API's endpoint nor how it authenticates, so GraphQL carries both.

type Input

type Input = core.Input

Input is a value the host supplies by name: a server variable when it has a default, a credential when it has none. A credential with Authorization is an OAuth 2.0 access token, which operations send as an RFC 6750 §2.1 bearer token.

type Omission

type Omission = core.Omission

Omission is an operation a Document leaves out, and why. Its String is "<operation>: <reason>", where the operation is "GET /widgets" for OpenAPI and "query Viewer" for GraphQL.

type OpenAPI added in v0.0.13

type OpenAPI struct {
	// Data is the document, as JSON or YAML.
	Data []byte
	// Location is where Data came from, which resolves a relative server URL. It is nil when unknown.
	Location *url.URL
}

OpenAPI is an OpenAPI 3.0, 3.1 or 3.2 document. Its first server is the Document's server, and its security schemes are the credentials each operation sends.

type Source added in v0.0.13

type Source interface {
	// contains filtered or unexported methods
}

Source is what om compiles: an OpenAPI document, or a GraphQL API and its trusted documents.

Directories

Path Synopsis
cmd
om command
Package discover finds the OpenAPI descriptions an API publishes, from an HTTPS URL.
Package discover finds the OpenAPI descriptions an API publishes, from an HTTPS URL.
internal
core
Package core is what every protocol om serves shares: the MCP tool catalog, the JSON Schema definitions tools share, the inputs a host supplies, credentials, the limits on each upstream exchange, and the MCP server itself.
Package core is what every protocol om serves shares: the MCP tool catalog, the JSON Schema definitions tools share, the inputs a host supplies, credentials, the limits on each upstream exchange, and the MCP server itself.
graphql
Package graphql is om's GraphQL protocol: trusted documents, the named operations of executable GraphQL documents validated against the API's schema, served as one tool per named query or mutation.
Package graphql is om's GraphQL protocol: trusted documents, the named operations of executable GraphQL documents validated against the API's schema, served as one tool per named query or mutation.
openapi
Package openapi is om's OpenAPI protocol: it compiles an OpenAPI 3.0–3.2 document into a core.Draft of operations, and each operation sends itself as an HTTP request with parameters serialized in the styles OpenAPI defines.
Package openapi is om's OpenAPI protocol: it compiles an OpenAPI 3.0–3.2 document into a core.Draft of operations, and each operation sends itself as an HTTP request with parameters serialized in the styles OpenAPI defines.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL