Golem tools middleware go

From golemcloud/golem · View interactive version

Golem tools middleware go is an AI agent skill from the GitHub repository golemcloud/golem. Defines Golem tool middleware in Go. Use for validation, policy, auditing, caching, adapting one tool to another, or rewriting tool calls and results, either typed for one tool or universal for any tool, in a Go Golem project.

How to use this skill

Skill file

Tool Middleware in Go

A middleware wraps the invocations of a tool: it sees a call on its way in, hands it to the layer beneath, and sees the outcome on its way back. Prefer the typed form: it is declared on the tool it presents, and handles that tool's commands with their own argument and result types. Use the universal form only for middleware that must work with any tool, such as an audit log.

Typed Middleware

package policy

import (
	"myapp/tools/inventory"

	"github.com/golemcloud/golem/sdks/go/golem/tool"
)

// Static configuration of one installation; its schema is published.
type Params struct{ MaxAdjust int32 }

var Policy = inventory.Tool.Middleware[Params]("inventory-policy", tool.MiddlewareSpec{
	Version: "1.0.0",
	Summary: "Caps stock adjustments",
})

var _ = Policy.Handle(inventory.Adjust, func(ctx *tool.MiddlewareContext[Params], a inventory.AdjustArgs) (int32, error) {
	if a.By > ctx.Parameters().MaxAdjust {
		return 0, tool.ConstraintViolation("adjustment too large")
	}
	return Policy.Underlying(ctx, inventory.Adjust).Forward(a)
})

Blank-import the package from main.go. Use golem.Unit as the parameter type for a middleware that takes none.

Commands With Outputs

var _ = Policy.HandleOutput(inventory.Import, func(ctx *tool.MiddlewareOutputContext[Params], a inventory.ImportArgs) (int32, error) {
	inv, err := Policy.UnderlyingOutput(ctx, inventory.Import).Start(a)
	if err != nil {
		return 0, err
	}
	if _, err := io.Copy(ctx.Stdout(), redact(inv.Stdout())); err != nil {
		return 0, err
	}
	return inv.Wait()
})

Adapters

An adapter presents one tool over another — for example a new version of a tool's interface over the old implementation:

var Compat = invv2.Tool.Adapter[golem.Unit]("inventory-v2-on-v1", invv1.Tool, tool.MiddlewareSpec{Version: "1.0.0"})

var _ = Compat.Handle(invv2.Show, func(ctx *tool.MiddlewareContext[golem.Unit], a invv2.ShowArgs) (string, error) {
	return Compat.Underlying(ctx, invv1.Get).Call(func(b *invv1.GetArgs) { b.SKU = a.SKU })
})

An adapter must handle every command of the tool it presents; a missing one is a definition error. The wrapped tool can be one this component defines, or a tool client (tool.DefineToolClient, see golem-call-tool-go).

Universal Middleware

A universal middleware has no Go types for the tools it wraps: the arguments and the result travel as golem.TypedValue, which carries its own schema.

type AuditParams struct {
	Channel string
	Deny    bool
}

var Audit = tool.DefineUniversalToolMiddleware[AuditParams]("audit", tool.MiddlewareSpec{
	Version: "1.0.0",
	Summary: "Records every tool invocation",
})

var _ = Audit.Handle(func(ctx *tool.UniversalMiddlewareContext[AuditParams]) (golem.Option[golem.TypedValue], error) {
	p := ctx.Parameters()
	slog.Info("tool call", "channel", p.Channel, "tool", ctx.ToolName(), "command", ctx.CommandPath())
	if p.Deny {
		return golem.None[golem.TypedValue](), tool.ConstraintViolation("denied by audit policy")
	}
	return ctx.Next(ctx.Input()) // forwards stdin, passes stdout and stderr through
})

Install

Declare the middleware, then install it on a tool binding or for a whole environment:

tools:
  inventory: {}
  middleware:
    inventory-policy:
      component: my-app:policy
    audit:
      component: my-app:policy

agents:
  AssistantAgent:
    tools:
      inventory:
        middleware:
          - name: inventory-policy
            parameters: { maxAdjust: 100 }

environments:
  local:
    tools:
      middleware: [audit]

Parameter keys are the canonical field names (MaxAdjust → maxAdjust).

Related Skills

| Skill | When to Load |

|-------|--------------|

| golem-define-tool-go | Defining the tools a middleware wraps |

| golem-call-tool-go | Calling a tool through its middleware chain |

| golem-edit-manifest | Tool bindings and middleware installation |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.