Say which agent may call which tool
Give one agent access to one tool of one MCP server, with a relationship, and check it at the server.
What it shows
- A grant says that the agent "Refund desk agent" may call the tool
refund_invoiceof the MCP server "Billing tools". - LumoAuth stores the grant as a relationship:
mcp_tool:<serverId>/refund_invoice#invoke@agent:<agentId>. - Before the MCP server runs a tool, it asks LumoAuth about the caller and the tool.
- The answer is yes for
refund_invoiceand no fordelete_invoice, which has no grant.
The MCP server asks with a token of its own application. It never sends the token that it received from its caller.
Before you start
- Check that your plan includes fine-grained authorization in the plans matrix. Without it, every tool is refused.
- A registered MCP server, an application for that server, an agent and a grant. The setup script creates all four and prints their IDs.
- The application of the MCP server uses the client credentials grant with the scope
mcp:authorize. The MCP server names it under Per-tool authorization (tool_authorizer_client_idsin the Admin API). - The setup script needs an API key with the scopes
admin:agents:read,admin:agents:write,admin:clients:readandadmin:clients: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, admin:agents:write, admin:clients:read and admin:clients:write. |
MCP_SERVER_ID | The ID of the registered MCP server (mcp_…), not its Resource URI. The setup script prints it. |
LUMOAUTH_MCP_CLIENT_ID | The client ID of the MCP server's own application. The setup script prints it. |
LUMOAUTH_MCP_CLIENT_SECRET | The client secret of that application. The setup script prints it once. |
AGENT_ID | The ID of the agent that calls the tool (agt_…). The setup script prints it. |
The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.
node agents/fine-grained-tool-access/setup.mjs
setup.mjs
// Creates what the recipe needs: an MCP server, the server's own application,
// an agent, and a grant for one tool. Run it again at any time.
//
// node agents/fine-grained-tool-access/setup.mjs
//
// The API key needs the scopes admin:agents:read, admin:agents:write,
// admin:clients:read and admin:clients:write.
import { call, ensure, list, printEnv } from '../../_lib/admin.mjs';
// 1. The application of the MCP server itself. The server asks LumoAuth with
// a token of this application, never with the token of its caller.
const appName = 'Billing MCP server';
let app = (await list('/admin/clients')).find((c) => c.name === appName);
let secret;
if (app) {
// LumoAuth shows a secret once. A second run therefore makes a new secret,
// and the old one stops working.
secret = (await call('POST', `/admin/clients/${app.clientId}/rotate-secret`)).data.secret;
} else {
app = (
await call('POST', '/admin/clients', {
name: appName,
grantTypes: ['client_credentials'],
scopes: ['mcp:authorize'],
isConfidential: true,
})
).data;
secret = app.secret;
}
// 2. The MCP server. Only the application above may ask about its tools.
const resourceUri = 'https://billing-mcp.example.com/mcp';
const server = await ensure('/admin/mcp/servers', (s) => s.resource_uri === resourceUri, {
name: 'Billing tools',
resource_uri: resourceUri,
scopes_supported: ['mcp:read', 'mcp:write'],
tool_authorizer_client_ids: [app.clientId],
});
// 3. The agent that calls the MCP server.
const agent = await ensure('/admin/agents', (a) => a.name === 'Refund desk agent', {
name: 'Refund desk agent',
description: 'Refunds invoices through the billing MCP server.',
identityType: 'client_secret',
});
// 4. The grant: this agent may call one tool of this server. LumoAuth stores
// it as the relationship mcp_tool:<server>/refund_invoice#invoke@agent:<agent>.
// To allow every tool of the server, send { all_tools: true } instead.
await call('PUT', `/admin/mcp/servers/${server.server_id}/grants/${agent.agentId}`, { tools: ['refund_invoice'] });
console.log(`Agent ${agent.agentId} may call refund_invoice on MCP server ${server.server_id}.`);
printEnv({
MCP_SERVER_ID: server.server_id,
LUMOAUTH_MCP_CLIENT_ID: app.clientId,
LUMOAUTH_MCP_CLIENT_SECRET: secret,
AGENT_ID: agent.agentId,
});
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. The curl version prints the two answers as JSON.
- Node.js
- Python
- curl
agents/fine-grained-tool-access/js/authorize.mjs
// The MCP server asks LumoAuth whether one agent may run one tool.
//
// node agents/fine-grained-tool-access/js/authorize.mjs
import { LumoAgent } from '@lumoauth/agent';
// The application of the MCP server itself. LumoAgent gets a client
// credentials token for it and renews it. Reads LUMOAUTH_URL and LUMOAUTH_ORG_ID.
const server = new LumoAgent({
clientId: process.env.LUMOAUTH_MCP_CLIENT_ID,
clientSecret: process.env.LUMOAUTH_MCP_CLIENT_SECRET,
scopes: ['mcp:authorize'],
});
// In a real MCP server, the caller is the `sub` claim of the token that the
// server verified: agent_<agent ID> for an agent.
const caller = { type: 'agent', id: process.env.AGENT_ID };
for (const tool of ['refund_invoice', 'delete_invoice']) {
// A refusal is an answer, not an error.
const decision = await server.mcp.authorizeTool({ serverId: process.env.MCP_SERVER_ID, subject: caller, tool });
console.log(`${tool}: ${decision.allowed ? 'allowed' : 'denied'}`);
}
node agents/fine-grained-tool-access/js/authorize.mjs
agents/fine-grained-tool-access/python/authorize.py
# The MCP server asks LumoAuth whether one agent may run one tool.
#
# python agents/fine-grained-tool-access/python/authorize.py
import os
from lumoauth import LumoAuth
# The application of the MCP server itself gets a client credentials token.
# A server that runs for hours gets a new token before this one expires.
# Reads LUMOAUTH_URL and LUMOAUTH_ORG_ID.
own = LumoAuth().auth.client_credentials(
os.environ["LUMOAUTH_MCP_CLIENT_ID"],
os.environ["LUMOAUTH_MCP_CLIENT_SECRET"],
scopes=["mcp:authorize"],
)
lumo = LumoAuth(token_provider=lambda: own["access_token"])
# In a real MCP server, the caller is the `sub` claim of the token that the
# server verified: agent_<agent ID> for an agent.
caller = os.environ["AGENT_ID"]
for tool in ["refund_invoice", "delete_invoice"]:
# A refusal is an answer, not an error.
decision = lumo.mcp.authorize_tool(os.environ["MCP_SERVER_ID"], "agent", caller, tool)
print(f"{tool}: {'allowed' if decision.allowed else 'denied'}")
python agents/fine-grained-tool-access/python/authorize.py
agents/fine-grained-tool-access/http/authorize.sh
#!/bin/sh
# The MCP server asks LumoAuth whether one agent may run one tool, with curl.
#
# sh agents/fine-grained-tool-access/http/authorize.sh
set -eu
API="$LUMOAUTH_URL/orgs/$LUMOAUTH_ORG_ID/api/v1"
# The application of the MCP server itself gets a client credentials token.
# The request has no `resource` parameter.
TOKEN=$(curl --silent --show-error "$API/oauth/token" \
--user "$LUMOAUTH_MCP_CLIENT_ID:$LUMOAUTH_MCP_CLIENT_SECRET" \
--data "grant_type=client_credentials" \
--data "scope=mcp:authorize" |
sed -n 's/.*"access_token" *: *"\([^"]*\)".*/\1/p')
for TOOL in refund_invoice delete_invoice; do
# The answer is 200 for an allowed call and 403 for a refused one.
curl --silent --show-error "$API/mcp/servers/$MCP_SERVER_ID/tools/authorize" \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"subject\": {\"type\": \"agent\", \"id\": \"$AGENT_ID\"}, \"tool\": \"$TOOL\"}"
echo
done
sh agents/fine-grained-tool-access/http/authorize.sh
Check the result
Output
refund_invoice: allowed
delete_invoice: denied
- Allow every tool of the server: send
{"all_tools": true}withPUT /admin/mcp/servers/{serverId}/grants/{agentId}. Run the program again: both answers are "allowed". - Remove the grants with
DELETEon the same path. The next call of each tool is "denied". - The portal lists the same relationships under Relationships, on the Tuples tab.
- Each decision is in the audit log with the event type
mcp.tool.authorization.
Limits
- Every tool is refused until a grant exists.
- The answer of this endpoint comes from the grants alone. It does not read your attribute rules.
- A tool name may not contain
/,:,#or*. - An agent can ask about itself before it calls a tool, with
agents.authorizeMcpTool(serverId, tool). An MCP server does not use that call, because it would have to send the token of its caller.