Skip to main content

Quickstart: Zero-Trust Security via Access Policies

Enable authorization for Mastra with access policies.

This quickstart demonstrates how to secure agent-to-agent communication using Catalyst access policies. You'll run two apps — a caller that invokes a server running a Mastra workflow — and then apply a deny policy to block unauthorized access.

note

This tutorial uses Catalyst Cloud to get started quickly with zero infrastructure setup. Catalyst Cloud uses a tunnel to route service invocation requests from the cloud to your local dev machine. For production or on-premises requirements, Diagrid also offers a self-hosted enterprise option where traffic stays within your private network.

You will learn how to:

  • Use Catalyst service invocation for agent-to-agent communication
  • Apply an access control configuration to enforce zero-trust authorization
  • Observe a 403 PermissionDenied response when a caller is blocked by policy
Caller to server through Catalyst service invocationCatalystCaller appcallerApp IDAccesspolicyserverApp IDMastraserver
Caller to server through Catalyst service invocationCatalyst checks the access policy before it forwards the call. A denied call gets a 403 and never reaches the server.

Your apps run entirely on your local machine — you are not deploying code to Catalyst. diagrid dev run opens a secure tunnel between Catalyst Cloud and your local processes and registers each app with an ID. When the caller sends a service invocation request, it travels up to Catalyst Cloud (not directly to the server). Catalyst evaluates the access policy there, in the cloud: if the policy allows it, the request is forwarded back down through the tunnel to the local server; if the policy denies it, Catalyst returns a 403 PermissionDenied immediately and the request never reaches your local server process.

1. Prerequisites​

2. Log in to Catalyst​

diagrid login

Confirm your identity:

diagrid whoami

3. Create project files​

Create a new directory for the quickstart:

mkdir mastra-access-policies
cd mastra-access-policies

Create the app folders:

mkdir server
mkdir caller

server/package.json​

Create server/package.json with the following content:

{
"name": "server",
"private": true,
"type": "module",
"dependencies": {
"@mastra/core": "1.76.0",
"tsx": "4.23.15",
"zod": "4.6.7"
}
}

server/server.ts​

Create server/server.ts with the following content. It runs a two-step Mastra workflow behind a /research HTTP endpoint.

note

The server runs the two steps as a Mastra workflow with no agent or model call, so the quickstart needs no LLM API key.

// Server app: exposes a Mastra workflow via HTTP.
import { createServer } from "node:http";
import { createStep, createWorkflow } from "@mastra/core/workflows";
import { z } from "zod";

// ── Workflow state ───────────────────────────────────────────
const researchState = z.object({
topic: z.string(),
messages: z.array(z.string()),
});

// ── Workflow steps ───────────────────────────────────────────
const gatherSources = createStep({
id: "gather_sources",
inputSchema: researchState,
outputSchema: researchState,
execute: async ({ inputData }) => {
const result = `Found 42 papers on '${inputData.topic}'`;
console.log(`>>> gather_sources: ${result}`);
return { ...inputData, messages: [...inputData.messages, result] };
},
});

const summarize = createStep({
id: "summarize",
inputSchema: researchState,
outputSchema: researchState,
execute: async ({ inputData }) => {
const result = `Summary: AI diagnostics up 40% for '${inputData.topic}'`;
console.log(`>>> summarize: ${result}`);
return { ...inputData, messages: [...inputData.messages, result] };
},
});

// ── Build workflow ───────────────────────────────────────────
const research = createWorkflow({
id: "research",
inputSchema: researchState,
outputSchema: researchState,
})
.then(gatherSources)
.then(summarize)
.commit();

// ── HTTP server ──────────────────────────────────────────────
const server = createServer(async (req, res) => {
const send = (status: number, body: unknown) => {
res.writeHead(status, { "Content-Type": "application/json" });
res.end(JSON.stringify(body));
};

if (req.method === "GET" && req.url === "/health") {
return send(200, { status: "ok" });
}

if (req.method === "POST" && req.url === "/research") {
let raw = "";
for await (const chunk of req) raw += chunk;
const { topic } = JSON.parse(raw);

const run = await research.createRun();
const result = await run.start({ inputData: { topic, messages: [] } });
if (result.status !== "success") {
return send(500, { error: `workflow ended as ${result.status}` });
}
return send(200, { messages: result.result.messages });
}

send(404, { error: "not found" });
});

server.listen(8002, () => console.log("Server listening on http://0.0.0.0:8002"));

caller/package.json​

Create caller/package.json with the following content:

{
"name": "caller",
"private": true,
"type": "module",
"dependencies": {
"tsx": "4.23.15"
}
}

caller/caller.ts​

Create caller/caller.ts with the following content. This app invokes the server's /research endpoint through Catalyst service invocation:

// Caller app: invokes the server app through Catalyst service invocation.
import { createServer } from "node:http";

// Catalyst injects these environment variables via `diagrid dev run`
const DAPR_HTTP_ENDPOINT = process.env.DAPR_HTTP_ENDPOINT ?? "http://localhost:3500";
const DAPR_API_TOKEN = process.env.DAPR_API_TOKEN ?? "";

const server = createServer(async (req, res) => {
const send = (status: number, body: unknown) => {
res.writeHead(status, { "Content-Type": "application/json" });
res.end(JSON.stringify(body));
};

if (req.method === "GET" && req.url === "/health") {
return send(200, { status: "ok" });
}

if (req.method === "POST" && req.url === "/call-server") {
let raw = "";
for await (const chunk of req) raw += chunk;
const { topic } = JSON.parse(raw);

// Call the server app's /research endpoint via Catalyst service invocation.
const url = `${DAPR_HTTP_ENDPOINT}/v1.0/invoke/server/method/research`;
console.log(`>>> Calling server via Catalyst: ${url}`);
const resp = await fetch(url, {
method: "POST",
headers: {
"dapr-api-token": DAPR_API_TOKEN,
"Content-Type": "application/json",
},
body: JSON.stringify({ topic }),
signal: AbortSignal.timeout(30_000),
});
const text = await resp.text();
console.log(`>>> Response status: ${resp.status}`);
console.log(`>>> Response body: ${text}`);
return send(200, {
status_code: resp.status,
body: resp.status === 200 ? JSON.parse(text) : text,
});
}

send(404, { error: "not found" });
});

server.listen(8001, () => console.log("Caller listening on http://0.0.0.0:8001"));

The caller uses DAPR_HTTP_ENDPOINT and DAPR_API_TOKEN, which are automatically injected by diagrid dev run. The service invocation URL follows the pattern:

{DAPR_HTTP_ENDPOINT}/v1.0/invoke/{target-app-id}/method/{endpoint}

dev.yaml​

Create dev.yaml in the project root:

version: 1
common:
appLogDestination: console
apps:
- appID: caller
appDirPath: ./caller
command: ["npx", "tsx", "caller.ts"]
- appID: server
appDirPath: ./server
appPort: 8002
command: ["npx", "tsx", "server.ts"]
tip

The appPort field on the server tells Catalyst which local port the app listens on. This is required for apps that receive service invocation requests. The caller does not need appPort because it only sends requests.

4. Install dependencies​

Install the dependencies of both apps:

cd server && npm install && cd ..
cd caller && npm install && cd ..

5. Run with default allow policy​

Start both apps with Catalyst Cloud:

diagrid dev run -f dev.yaml --project mastra-access-qs --approve
tip

Wait for the log to show Server listening on ... and Caller listening on ... before proceeding.

Open a new terminal and invoke the caller:

curl -X POST http://localhost:8001/call-server \
-H "Content-Type: application/json" \
-d '{"topic": "AI in healthcare"}'

You should see a successful response:

{
"status_code": 200,
"body": {
"messages": [
"Found 42 papers on 'AI in healthcare'",
"Summary: AI diagnostics up 40% for 'AI in healthcare'"
]
}
}

By default, Catalyst allows all agent-to-agent communication. The request traveled from your local caller → Catalyst Cloud → through the tunnel → your local server. The caller successfully invoked the server's Mastra workflow.

6. Apply a deny policy​

Stop the running application by pressing Ctrl+C, then disconnect the apps from Catalyst:

diagrid dev stop --id caller --project mastra-access-qs
diagrid dev stop --id server --project mastra-access-qs

Deny every caller on the server app:

diagrid app access grant server --project mastra-access-qs --caller '*' --action deny --wait
tip

If the command reports local app connection already exists, wait a few seconds and run it again.

Passing '*' as the caller sets the policy's default action instead of naming one app, so this denies all incoming service invocation requests to the server, regardless of which app is calling it. Because authorization is enforced in Catalyst Cloud — not by the server process — a denied request never reaches your local server. The 403 is returned by Catalyst before any traffic enters the tunnel.

7. Verify access is denied​

Restart both apps:

diagrid dev run -f dev.yaml --project mastra-access-qs --approve

Open a new terminal and invoke the caller again:

curl -X POST http://localhost:8001/call-server \
-H "Content-Type: application/json" \
-d '{"topic": "AI in healthcare"}'

This time, the caller receives a 403 PermissionDenied error:

{
"status_code": 403,
"body": "{\"errorCode\":\"ERR_DIRECT_INVOKE\",\"message\":\"failed to invoke, id: server, err: rpc error: code = PermissionDenied desc = access control policy has denied access to id: spiffe://<trust-domain>/ns/<project-id>/caller operation: research verb: POST\"}"
}

The access policy blocked the caller from invoking the server. The server's Mastra workflow never executed.

8. Clean up​

Stop the running application by pressing Ctrl+C.

Delete the Catalyst Cloud project:

diagrid project delete mastra-access-qs

Summary​

In this quickstart, you:

  • Set up two apps communicating through Catalyst service invocation
  • Verified that agent-to-agent calls succeed with the default allow policy
  • Applied a deny-all access policy that blocked the caller with a 403 PermissionDenied error

Next steps​