Skip to content

Offer a go/mcp registry's operations as tools

If your program already registers operations with go/mcp, call registry.Tools(ctx, reg) from gitlab.com/phpboyscout/go/chat-mcptools/registry (chat-mcptools v0.3.0 or later) and pass the result to SetTools. The model can then find, inspect and call those operations through any API provider, with no MCP server running between them and no MCP SDK linked into your program.

import (
    "gitlab.com/phpboyscout/go/chat"
    "gitlab.com/phpboyscout/go/chat-mcptools/registry"
    "gitlab.com/phpboyscout/go/mcp"
)

reg, err := mcp.New(operations, mcp.WithPolicy(policy))
if err != nil {
    return err
}

tools, err := registry.Tools(ctx, reg)
if err != nil {
    return err
}

if err := client.SetTools(tools); err != nil {
    return err
}

answer, err := client.Chat(ctx, "Which projects are behind schedule?")

Compact or direct

By default the model gets three tools, search_tools, get_tool_details and call_tool. They are the same three, with the same schemas, that go/mcp's own server publishes over MCP. A registry of any size therefore costs three tool definitions, and the model searches for what it needs.

To give the model one tool per operation instead, pass registry.Direct():

tools, err := registry.Tools(ctx, reg, registry.Direct())

Direct mode lists the operations ctx is allowed to discover at that moment. It returns an error, naming the operation, in two cases:

  • The name is one a provider would refuse. go/mcp allows dots and names up to 128 characters, but no provider accepts a dot, and OpenAI and Bedrock stop at 64 characters. Every name must match ^[A-Za-z_][A-Za-z0-9_-]{0,63}$. go/mcp's Cobra binding joins command paths with underscores, so a go-tool-base registry passes as it is.
  • The input schema uses a keyword chat.Tool cannot carry, such as unevaluatedProperties. Passing it on without that keyword would offer the model a looser schema than the operation declares.

Compact mode has neither limit, because the operation name travels as an argument.

Authorization is asked again on every call

The tool list is a snapshot of names, never of permission. Every call goes through Registry.Invoke under the context the tool loop hands the handler, which is derived from the one you passed to Chat. Your policy sees the caller for that request, exactly as it would over MCP.

A go/mcp policy refuses by returning an *mcp.Failure coded mcp.FailureUnavailable. Any other error from Authorize is reported as an internal failure of the callback, not as a refusal:

policy := mcp.Policy{
    Authorize: func(ctx context.Context, req mcp.AccessRequest) error {
        if !allowed(ctx, req.Operation) {
            return mcp.NewFailure(mcp.FailureUnavailable, "Unavailable", nil)
        }

        return nil
    },
    ScopeKey: func(ctx context.Context) (string, error) { return userID(ctx) },
}

What the model reads

  • A success is the result's text, followed by {"value": ...} when the operation returned structured data.
  • A failure is go/mcp's envelope, {"failure": {"code": "...", "message": "...", "retryable": false}}. It is returned as a *chat.ToolFailure, so a provider with an error marker for tool results sets it (see Call tools). The failure's private cause is never in it.
  • Images, audio and embedded resources are described as JSON ({"kind": "image", "mimeType": "image/png"}) rather than sent, because a chat tool result carries text only.
  • An operation that asks for input (a form or a URL) fails as confirmation_required, since the model cannot answer either.