Check capabilities without a network call
Decide inside the agent, from the list of its capabilities, with the same matching rules as the server.
What it shows
- An agent, "Invoice clerk", holds two capabilities:
invoices:*andcustomers:read. - The agent reads its capabilities from LumoAuth once and keeps the list.
- A guard checks the list before each tool runs. The guard makes no request.
invoices:*coversinvoices:refund, so the refund tool runs. Nothing coverscustomers:delete, so the guard refuses that tool.
The guard uses the matching rules of the server. A capability that ends in :* covers every action under that prefix, and * alone covers every action. Do not compare capability names with ==: that test misses the wildcards.
Before you start
- An agent in the registry with the capabilities
invoices:*andcustomers:read, and its client ID and secret. The setup script registers the agent and prints both values. - The setup script needs an API key with the scopes
admin:agents:readandadmin:agents:write. Create it in the portal under Applications & developer tools, API keys.
| Variable | Value |
|---|---|
LUMOAUTH_URL | The address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region. |
LUMOAUTH_ORG_ID | Your organization, for example acme-corp. |
LUMOAUTH_API_KEY | For the setup script only: an API key with the scopes admin:agents:read and admin:agents:write. |
AGENT_CLIENT_ID | The client ID of the agent. The setup script prints it. |
AGENT_CLIENT_SECRET | The client secret of the agent. The setup script prints it once. |
The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.
node agents/local-capability-checks/setup.mjs
setup.mjs
// Registers the agent of the recipe, "Invoice clerk", with a wildcard
// capability and a plain one, and prints its client ID and secret. Run it
// again at any time.
//
// node agents/local-capability-checks/setup.mjs
//
// The API key needs the scopes admin:agents:read and admin:agents:write.
import { call, list, printEnv } from '../../_lib/admin.mjs';
const name = 'Invoice clerk';
const capabilities = ['invoices:*', 'customers:read'];
let agent = (await list('/admin/agents')).find((a) => a.name === name);
let secret;
if (agent) {
// LumoAuth shows a secret once. A second run therefore makes a new secret,
// and the old one stops working.
await call('PUT', `/admin/agents/${agent.agentId}/scopes`, { scopes: capabilities });
secret = (await call('POST', `/admin/agents/${agent.agentId}/rotate-credentials`)).data.clientSecret;
} else {
agent = (
await call('POST', '/admin/agents', {
name,
description: 'Works on invoices. It may read customers.',
identityType: 'client_secret',
capabilities,
})
).data;
secret = agent.clientSecret;
}
console.log(`Agent "${name}" (${agent.agentId}) holds: ${capabilities.join(', ')}`);
printEnv({ AGENT_CLIENT_ID: agent.agentId, AGENT_CLIENT_SECRET: secret });
Code
Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization.
- Python
- Go
- Java
- C#
- Node.js
# The agent reads its capabilities once, then checks each tool without a
# network call.
#
# python agents/local-capability-checks/python/guard.py
from lumoauth import LumoAuthAgent, require_capability
class InvoiceClerk(LumoAuthAgent):
# The decorator matches as the server does: invoices:* covers invoices:refund.
@require_capability("invoices:refund")
def refund_invoice(self):
return "refunded"
@require_capability("customers:read")
def read_customer(self):
return "read"
@require_capability("customers:delete")
def delete_customer(self):
return "deleted"
# Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
agent = InvoiceClerk()
# The only request of the program. The agent keeps the answer.
capabilities = agent.get_agent_info()["capabilities"]
print(f"capabilities: {', '.join(capabilities)}")
for tool in (agent.refund_invoice, agent.read_customer, agent.delete_customer):
try:
tool()
print(f"{tool.__name__}: ran")
except PermissionError:
print(f"{tool.__name__}: refused")
python agents/local-capability-checks/python/guard.py
// The agent reads its capabilities once, then checks each tool without a
// network call.
//
// go run ./agents/local-capability-checks/go
package main
import (
"context"
"errors"
"fmt"
"log"
"strings"
lumoauth "github.com/lumoauth/lumo-auth-go"
)
func main() {
ctx := context.Background()
// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
agent, err := lumoauth.NewAgent(lumoauth.AgentConfig{})
if err != nil {
log.Fatal(err)
}
// The only request of the program. The agent keeps the answer.
capabilities, err := agent.Capabilities(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("capabilities: %s\n", strings.Join(capabilities, ", "))
tools := []struct{ name, capability string }{
{"refund_invoice", "invoices:refund"},
{"read_customer", "customers:read"},
{"delete_customer", "customers:delete"},
}
for _, tool := range tools {
// RequireCapability matches as the server does: invoices:* covers invoices:refund.
err := agent.RequireCapability(ctx, tool.capability)
switch {
case err == nil:
fmt.Printf("%s: ran\n", tool.name)
case errors.Is(err, lumoauth.ErrPermissionDenied):
fmt.Printf("%s: refused\n", tool.name)
default:
log.Fatal(err)
}
}
}
go run ./agents/local-capability-checks/go
// The agent reads its capabilities once, then checks each tool without a
// network call.
//
// java -cp "java/lib/*" agents/local-capability-checks/java/GuardTools.java
import io.lumoauth.LumoAgent;
import io.lumoauth.errors.LumoAuthPermissionDeniedException;
import java.util.LinkedHashMap;
import java.util.Map;
public class GuardTools {
public static void main(String[] args) {
// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
LumoAgent agent = LumoAgent.fromEnvironment();
// The only request of the program. The agent keeps the answer.
System.out.println("capabilities: " + String.join(", ", agent.capabilities()));
Map<String, String> tools = new LinkedHashMap<>();
tools.put("refund_invoice", "invoices:refund");
tools.put("read_customer", "customers:read");
tools.put("delete_customer", "customers:delete");
tools.forEach((name, capability) -> {
try {
// requireCapability matches as the server does: invoices:* covers invoices:refund.
agent.requireCapability(capability);
System.out.println(name + ": ran");
} catch (LumoAuthPermissionDeniedException e) {
System.out.println(name + ": refused");
}
});
}
}
java -cp "java/lib/*" agents/local-capability-checks/java/GuardTools.java
#:package LumoAuth@1.7.0
#:property PublishAot=false
// The agent reads its capabilities once, then checks each tool without a
// network call.
//
// dotnet run agents/local-capability-checks/csharp/GuardTools.cs
using LumoAuth;
using LumoAuth.Errors;
// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
var agent = LumoAgent.FromEnvironment();
// The only request of the program. The agent keeps the answer.
var capabilities = await agent.CapabilitiesAsync();
Console.WriteLine($"capabilities: {string.Join(", ", capabilities)}");
var tools = new (string Name, string Capability)[]
{
("refund_invoice", "invoices:refund"),
("read_customer", "customers:read"),
("delete_customer", "customers:delete"),
};
foreach (var tool in tools)
{
try
{
// RequireCapabilityAsync matches as the server does: invoices:* covers invoices:refund.
await agent.RequireCapabilityAsync(tool.Capability);
Console.WriteLine($"{tool.Name}: ran");
}
catch (LumoAuthPermissionDeniedException)
{
Console.WriteLine($"{tool.Name}: refused");
}
}
dotnet run agents/local-capability-checks/csharp/GuardTools.cs
// The agent reads its capabilities once, then checks each tool without a
// network call.
//
// node agents/local-capability-checks/js/guard.mjs
import { LumoAgent } from '@lumoauth/agent';
import { capabilityCovered } from '@lumoauth/mcp';
// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
const agent = new LumoAgent();
// The only request of the program.
const capabilities = await agent.capabilities();
console.log(`capabilities: ${capabilities.join(', ')}`);
// The guard of a tool. capabilityCovered() matches as the server does:
// invoices:* covers invoices:refund.
function guarded(capability, tool) {
return () => {
if (!capabilityCovered(capability, capabilities)) throw new Error(`The agent lacks ${capability}.`);
return tool();
};
}
const tools = {
refund_invoice: guarded('invoices:refund', () => 'refunded'),
read_customer: guarded('customers:read', () => 'read'),
delete_customer: guarded('customers:delete', () => 'deleted'),
};
for (const [name, tool] of Object.entries(tools)) {
try {
tool();
console.log(`${name}: ran`);
} catch {
console.log(`${name}: refused`);
}
}
node agents/local-capability-checks/js/guard.mjs
Each SDK also has the matching function alone, for a list that you hold yourself: capability_covers() and any_capability_covers() in Python, CapabilityCovers() and AnyCapabilityCovers() in Go, Capabilities.covers() and Capabilities.anyCovers() in Java, AgentCapability.Covers() and AgentCapability.AnyCovers() in .NET, and capabilityCovered() in @lumoauth/mcp.
Check the result
capabilities: invoices:*, customers:read
refund_invoice: ran
read_customer: ran
delete_customer: refused
- Remove
invoices:*from the capabilities of the agent, on its edit page in the portal. Run the program again:refund_invoiceis now "refused".
Limits
- The guard looks only at the capabilities of the agent. It does not apply your attribute rules, your relationships or the scope of the token. The Ask API applies all of them.
- A running agent keeps the list that it read. It sees a change after it reads the list again, for example at its next start.
- A local check sends nothing to LumoAuth, so the audit log has no entry for it.