Documentation
¶
Overview ¶
Package restapi serves GraphLume's tools over plain HTTP and JSON.
Why this exists beside MCP ¶
MCP is the best way for an agent to reach the index, and many callers cannot use it: a model wired up with HTTP function calling, an internal bot, a CI step with curl, a language with no MCP client. For them the tools are the same and only the envelope differs, so this package is an envelope and nothing more.
It holds no handlers of its own. Every call is forwarded to the MCP server through an in-process client (mcp.Invoker), which means a tool's validation, output bounds, untrusted-content framing and audit record apply here because they apply there. The alternative — a second set of handlers that drifts from the first — is the failure this design removes.
Routes ¶
GET /api/v1 what this server is, and where to go next
GET /api/v1/tools every tool the active profile exposes
GET /api/v1/tools/{name} one tool: description and input schema
POST /api/v1/tools/{name} call it; the body is the JSON arguments object
GET /api/v1/openapi.json the same surface as an OpenAPI 3.1 document
Authentication, the Host allowlist and the Origin check are the caller's (internal/cli wraps this handler in the same guard as the MCP endpoint), so there is one definition of who may talk to the server.
What a response means ¶
A tool that answered returns 200, including when the answer is "that symbol is not in the index" — the tool worked and said so. The tool's own failure flag is carried in the body as is_error, exactly as MCP carries isError, rather than mapped onto a status code: a client that retries on 5xx must not retry a question whose answer is "you asked for a negative limit". Status codes describe the HTTP exchange: 400 for a body that is not a JSON object, 404 for a tool that does not exist, 405 for the wrong method, 413 for a body over the limit.
Index ¶
Constants ¶
const BasePath = "/api/v1"
BasePath is where the API is mounted.
const MaxBodyBytes = 1 << 20
MaxBodyBytes bounds a call's arguments. The largest legitimate argument is a diff for diff; a megabyte is far beyond one a model can use.
Variables ¶
This section is empty.
Functions ¶
func OpenAPI ¶
OpenAPI builds an OpenAPI 3.1 document for the given tools.
It is GENERATED from the tools the server actually registered, on every request, and never written down. A hand-maintained specification is a second description of the tool surface, and the first time a parameter is renamed it becomes a wrong one that a client generator then turns into wrong code.
3.1 rather than 3.0 because 3.1's schema object IS JSON Schema, so each tool's input schema is embedded unchanged; 3.0 would need it rewritten into a dialect with different nullability and no `$schema`.
Each tool becomes one POST operation whose operationId is the tool name, so a function-calling client that maps operations to functions ends up with the same names an MCP client sees.
Types ¶
type CallResponse ¶
type CallResponse struct {
Tool string `json:"tool"`
// Text is the rendered answer, the same text an MCP client receives.
Text string `json:"text"`
// Result is the same answer as structured data.
Result json.RawMessage `json:"result,omitempty"`
// IsError is the tool's own failure flag. See the package comment.
IsError bool `json:"is_error"`
}
CallResponse is the body of a successful tool call.
type ErrorResponse ¶
type ErrorResponse struct {
Error ErrorBody `json:"error"`
}
ErrorResponse is the body of a non-2xx response.
type Info ¶
type Info struct {
Project string // project name, as configured
Version string // binary version
Profile string // active tool profile
}
Info identifies the server in the index document and the OpenAPI document.
type Invoker ¶
type Invoker interface {
Tools(ctx context.Context) ([]mcp.ToolInfo, error)
Call(ctx context.Context, name string, args json.RawMessage) (mcp.CallResult, error)
}
Invoker is the part of mcp.Invoker this package uses. Declared here, at the consumer, so the HTTP layer can be tested against a fake without a database.