zuplo 7.14.5 → 7.14.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/ai-gateway/cookbooks/data-residency.mdx +259 -0
- package/docs/ai-gateway/fallback.mdx +6 -4
- package/docs/ai-gateway/guardrails.mdx +283 -0
- package/docs/ai-gateway/observability/analytics.mdx +121 -0
- package/docs/ai-gateway/observability/data-handling.mdx +120 -0
- package/docs/ai-gateway/observability/tracing.mdx +203 -0
- package/docs/ai-gateway/overview.mdx +6 -4
- package/docs/ai-gateway/policies/overview.mdx +4 -0
- package/docs/ai-gateway/policy-chains.mdx +10 -3
- package/docs/ai-gateway/routing.mdx +277 -0
- package/docs/analytics/overview.md +3 -0
- package/docs/analytics/tabs/cache.md +89 -0
- package/docs/articles/connect-to-aws-alb-with-mtls.mdx +23 -0
- package/docs/articles/deployments-and-rollbacks.mdx +93 -0
- package/docs/articles/proxy-openai-realtime-api.mdx +247 -0
- package/docs/caching/gateway-caching.mdx +2 -1
- package/docs/caching/semantic-caching.mdx +133 -0
- package/docs/cli/lint.mdx +13 -0
- package/docs/cli/variable-delete.mdx +66 -0
- package/docs/concepts/source-control-and-deployment.mdx +6 -0
- package/docs/concepts/upstream-credentials.mdx +2 -1
- package/docs/guides/backend-failover.mdx +117 -0
- package/docs/guides/blue-green-deployments.mdx +199 -0
- package/docs/handlers/websocket-pipeline-handler.mdx +94 -0
- package/docs/mcp-server/authorize-tool-calls.mdx +284 -0
- package/docs/mcp-server/overview.mdx +3 -0
- package/docs/policies/mcp-capability-filter-inbound/doc.md +101 -1
- package/docs/policies/semantic-cache-inbound/doc.md +6 -5
- package/docs/policies/semantic-cache-inbound/schema.json +4 -4
- package/package.json +5 -5
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Blue-Green Deployments for Your Backend
|
|
3
|
+
sidebar_label: Blue-Green Deployments
|
|
4
|
+
description:
|
|
5
|
+
Run blue and green backend stacks behind Zuplo, test the idle stack through a
|
|
6
|
+
staging gateway with the same policies, then switch traffic and roll back
|
|
7
|
+
safely.
|
|
8
|
+
tags:
|
|
9
|
+
- backends
|
|
10
|
+
- deployment
|
|
11
|
+
- testing
|
|
12
|
+
applies_to:
|
|
13
|
+
product: gateway
|
|
14
|
+
hosting: all
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
A blue-green deployment runs two copies of your backend, called blue and green.
|
|
18
|
+
One copy serves live traffic while you deploy and test the next release on the
|
|
19
|
+
other. When the new release passes its tests, you switch traffic to it, and the
|
|
20
|
+
old copy stays ready as a fallback.
|
|
21
|
+
|
|
22
|
+
This guide shows how to run a blue-green release behind a Zuplo gateway: a
|
|
23
|
+
staging gateway environment sends test traffic to the idle backend with the same
|
|
24
|
+
routes and policies as production, and production keeps serving the live backend
|
|
25
|
+
until you switch.
|
|
26
|
+
|
|
27
|
+
:::tip
|
|
28
|
+
|
|
29
|
+
Zuplo already deploys changes to the gateway itself without downtime, and you
|
|
30
|
+
can roll back to any earlier build. You don't need two gateways to change your
|
|
31
|
+
gateway safely. See
|
|
32
|
+
[Deployments and Rollbacks](../articles/deployments-and-rollbacks.mdx). This
|
|
33
|
+
guide is about releasing your backend.
|
|
34
|
+
|
|
35
|
+
:::
|
|
36
|
+
|
|
37
|
+
## How it works
|
|
38
|
+
|
|
39
|
+
Each backend copy has its own URL, for example its own load balancer or its own
|
|
40
|
+
hostname. Two Zuplo environments run the same project:
|
|
41
|
+
|
|
42
|
+
- **Production** is deployed from your default branch and forwards requests to
|
|
43
|
+
the live backend.
|
|
44
|
+
- **Staging** is a preview environment deployed from a separate branch. It
|
|
45
|
+
forwards requests to the idle backend, so you can test a release through the
|
|
46
|
+
same routes and policies that production uses.
|
|
47
|
+
|
|
48
|
+
<Diagram height="h-72">
|
|
49
|
+
<DiagramNode id="clients">API clients</DiagramNode>
|
|
50
|
+
<DiagramNode id="testers">Testers</DiagramNode>
|
|
51
|
+
<DiagramNode id="prod" variant="zuplo">
|
|
52
|
+
Production environment
|
|
53
|
+
</DiagramNode>
|
|
54
|
+
<DiagramNode id="staging" variant="zuplo">
|
|
55
|
+
Staging environment
|
|
56
|
+
</DiagramNode>
|
|
57
|
+
<DiagramNode id="blue" variant="blue">
|
|
58
|
+
Blue backend (live)
|
|
59
|
+
</DiagramNode>
|
|
60
|
+
<DiagramNode id="green" variant="green">
|
|
61
|
+
Green backend (idle)
|
|
62
|
+
</DiagramNode>
|
|
63
|
+
<DiagramEdge from="clients" to="prod" />
|
|
64
|
+
<DiagramEdge from="testers" to="staging" />
|
|
65
|
+
<DiagramEdge from="prod" to="blue" label="BACKEND_URL" />
|
|
66
|
+
<DiagramEdge from="staging" to="green" label="BACKEND_URL" />
|
|
67
|
+
</Diagram>
|
|
68
|
+
|
|
69
|
+
## Prerequisites
|
|
70
|
+
|
|
71
|
+
- A Zuplo project connected to [source control](../articles/source-control.mdx),
|
|
72
|
+
so that branches deploy as environments
|
|
73
|
+
- Two copies of your backend, each reachable at its own URL
|
|
74
|
+
|
|
75
|
+
## 1/ Read the backend URL from an environment variable
|
|
76
|
+
|
|
77
|
+
Set each route's backend URL from an
|
|
78
|
+
[environment variable](../articles/environment-variables.mdx), so the production
|
|
79
|
+
and staging environments can forward to different backends with the same code.
|
|
80
|
+
With the [URL Forward handler](../handlers/url-forward.mdx):
|
|
81
|
+
|
|
82
|
+
```json title="config/routes.oas.json"
|
|
83
|
+
{
|
|
84
|
+
"handler": {
|
|
85
|
+
"export": "urlForwardHandler",
|
|
86
|
+
"module": "$import(@zuplo/runtime)",
|
|
87
|
+
"options": {
|
|
88
|
+
"baseUrl": "${env.BACKEND_URL}"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
In the Zuplo Portal, open
|
|
95
|
+
[**Settings**](https://portal.zuplo.com/+/account/project/settings/general) and
|
|
96
|
+
select **Environment Variables**. Add `BACKEND_URL` for the **Production**
|
|
97
|
+
environment, set to the URL of the live backend, for example
|
|
98
|
+
`https://blue.api.example.com`.
|
|
99
|
+
|
|
100
|
+
## 2/ Create the staging environment
|
|
101
|
+
|
|
102
|
+
Create a branch, for example `staging`, and push it. Zuplo deploys it as a
|
|
103
|
+
preview environment with its own URL. See
|
|
104
|
+
[Branch-Based Deployments](../articles/branch-based-deployments.mdx).
|
|
105
|
+
|
|
106
|
+
Then set up the staging environment:
|
|
107
|
+
|
|
108
|
+
- **Backend URL:** add `BACKEND_URL` for the `staging` preview environment, set
|
|
109
|
+
to the idle backend, for example `https://green.api.example.com`. A variable
|
|
110
|
+
can apply to one named preview environment, so it doesn't affect other
|
|
111
|
+
branches.
|
|
112
|
+
- **Secrets:** give every secret your policies read, such as a shared secret
|
|
113
|
+
from your CDN, a value for the staging environment.
|
|
114
|
+
- **API keys:** preview environments use a separate
|
|
115
|
+
[API key bucket](../articles/api-key-buckets.mdx) from production, so
|
|
116
|
+
production keys don't work there. Create test consumers in the preview bucket,
|
|
117
|
+
or see
|
|
118
|
+
[Custom bucket configuration](../articles/api-key-buckets.mdx#custom-bucket-configuration).
|
|
119
|
+
- **mTLS client certificates:** if your backend requires a
|
|
120
|
+
[client certificate](../articles/securing-backend-mtls.mdx), enable the
|
|
121
|
+
certificate for the preview environment type.
|
|
122
|
+
- **Custom domain (optional):** give the staging environment its own hostname,
|
|
123
|
+
such as `staging-api.example.com`. See
|
|
124
|
+
[Custom Domains](../articles/custom-domains.mdx).
|
|
125
|
+
|
|
126
|
+
Redeploy the staging environment after you change its variables, because
|
|
127
|
+
variable changes take effect on the next deployment.
|
|
128
|
+
|
|
129
|
+
## 3/ Release a new backend version
|
|
130
|
+
|
|
131
|
+
1. Deploy the new version of your backend to the idle copy (green).
|
|
132
|
+
2. Test it through the staging environment's URL. Requests pass through the same
|
|
133
|
+
routes and policies as production, so the test covers authentication, rate
|
|
134
|
+
limits, and request validation, not only the backend.
|
|
135
|
+
3. When the release passes, switch production traffic to green (see the next
|
|
136
|
+
section).
|
|
137
|
+
4. Point the staging environment at the copy that's now idle (blue), ready for
|
|
138
|
+
the next release.
|
|
139
|
+
|
|
140
|
+
## 4/ Switch production traffic
|
|
141
|
+
|
|
142
|
+
Choose where the switch happens:
|
|
143
|
+
|
|
144
|
+
| Switch at | How | Zuplo deployment needed |
|
|
145
|
+
| ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ----------------------- |
|
|
146
|
+
| Your load balancer or DNS | Point the hostname that production's `BACKEND_URL` uses at green, for example by changing a load balancer rule or DNS record | No |
|
|
147
|
+
| The production `BACKEND_URL` | Change the variable to green's URL, then redeploy the production environment | Yes |
|
|
148
|
+
|
|
149
|
+
Switching at the load balancer is the fastest option, in both directions,
|
|
150
|
+
because it doesn't wait for a gateway deployment. Changing `BACKEND_URL` keeps
|
|
151
|
+
the switch in Zuplo, and the redeploy rolls out without downtime.
|
|
152
|
+
|
|
153
|
+
## Roll back
|
|
154
|
+
|
|
155
|
+
Switch back the same way you switched forward. Blue is still running the
|
|
156
|
+
previous release, so rolling back only changes where traffic goes:
|
|
157
|
+
|
|
158
|
+
- If you switched at the load balancer or DNS, point it back at blue.
|
|
159
|
+
- If you switched with `BACKEND_URL`, set it back to blue's URL and redeploy
|
|
160
|
+
production.
|
|
161
|
+
|
|
162
|
+
:::caution
|
|
163
|
+
|
|
164
|
+
Redeploying an earlier build of the production environment doesn't restore an
|
|
165
|
+
earlier value of `BACKEND_URL`, because a redeploy uses current settings. Change
|
|
166
|
+
the variable itself to roll back a `BACKEND_URL` switch.
|
|
167
|
+
|
|
168
|
+
:::
|
|
169
|
+
|
|
170
|
+
## Test through production instead of a staging environment
|
|
171
|
+
|
|
172
|
+
If the gateway configuration doesn't change between releases, testers can reach
|
|
173
|
+
the idle backend through the production environment itself. A routing policy
|
|
174
|
+
sends requests with a header such as `x-stage: canary`, or from users on an
|
|
175
|
+
allow list, to the idle backend, and everyone else to the live one. You don't
|
|
176
|
+
need a staging environment or a second hostname. The trade-off is that both
|
|
177
|
+
backends share one gateway deployment. See
|
|
178
|
+
[Route Employees to Canary or Staging Backends](./canary-routing-for-employees.mdx).
|
|
179
|
+
|
|
180
|
+
## When to run blue and green gateway environments
|
|
181
|
+
|
|
182
|
+
Most releases only need the pattern above. If a release also changes the gateway
|
|
183
|
+
configuration in a risky way, such as new authentication policies, you can run
|
|
184
|
+
two long-lived environments from `blue` and `green` branches and switch traffic
|
|
185
|
+
in front of Zuplo, for example by changing your CDN's origin.
|
|
186
|
+
|
|
187
|
+
Both environments are preview environments, which serve traffic the same way as
|
|
188
|
+
production. Production-scoped settings don't apply to them, so set their
|
|
189
|
+
[environment variables](../articles/environment-variables.mdx) for each named
|
|
190
|
+
environment and choose which [API key bucket](../articles/api-key-buckets.mdx)
|
|
191
|
+
they use. See
|
|
192
|
+
[Preview vs Production Environments](../articles/environments.mdx#preview-vs-production-environments-or-multiple-production-environments).
|
|
193
|
+
|
|
194
|
+
## Next steps
|
|
195
|
+
|
|
196
|
+
- [Deployments and Rollbacks](../articles/deployments-and-rollbacks.mdx)
|
|
197
|
+
- [Testing Preview Environments](../articles/testing-preview-environments.mdx)
|
|
198
|
+
- [Fail Over Between Backends](./backend-failover.mdx)
|
|
199
|
+
- [Connect to an AWS ALB with mTLS](../articles/connect-to-aws-alb-with-mtls.mdx)
|
|
@@ -4,6 +4,15 @@ sidebar_label: WebSocket Pipeline Handler
|
|
|
4
4
|
applies_to:
|
|
5
5
|
product: gateway
|
|
6
6
|
hosting: all
|
|
7
|
+
differs:
|
|
8
|
+
managed-dedicated: >-
|
|
9
|
+
The gateway opens the backend connection with the `rewritePattern` URL
|
|
10
|
+
only. Headers from the client request and from route policies aren't sent
|
|
11
|
+
to your backend, so pass any value the backend needs in the URL.
|
|
12
|
+
self-hosted: >-
|
|
13
|
+
The gateway opens the backend connection with the `rewritePattern` URL
|
|
14
|
+
only. Headers from the client request and from route policies aren't sent
|
|
15
|
+
to your backend, so pass any value the backend needs in the URL.
|
|
7
16
|
---
|
|
8
17
|
|
|
9
18
|
:::note
|
|
@@ -205,6 +214,91 @@ example, one policy to validate a message against a schema and a second to
|
|
|
205
214
|
redact fields. Because each policy receives the previous policy's output, order
|
|
206
215
|
matters.
|
|
207
216
|
|
|
217
|
+
## Close the connection from a message policy
|
|
218
|
+
|
|
219
|
+
A message policy can end the session instead of forwarding a message. Call
|
|
220
|
+
`close()` on the client socket and return `undefined` so the message isn't
|
|
221
|
+
forwarded. When the client connection closes, the handler closes the backend
|
|
222
|
+
connection too.
|
|
223
|
+
|
|
224
|
+
The client socket is `source` in an `inbound` policy and `target` in an
|
|
225
|
+
`outbound` policy. Use a close code from `4000` to `4999`, the range reserved
|
|
226
|
+
for applications, or `1000` for a normal closure. Keep the reason to 123 bytes
|
|
227
|
+
or fewer.
|
|
228
|
+
|
|
229
|
+
This inbound policy accepts only the message types it knows about and closes the
|
|
230
|
+
connection on anything else:
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
import { ZuploContext, ZuploRequest } from "@zuplo/runtime";
|
|
234
|
+
|
|
235
|
+
const ALLOWED_TYPES = new Set(["subscribe", "unsubscribe", "ping"]);
|
|
236
|
+
|
|
237
|
+
export async function allowKnownMessages(
|
|
238
|
+
data: string,
|
|
239
|
+
target: WebSocket,
|
|
240
|
+
source: WebSocket,
|
|
241
|
+
request: ZuploRequest,
|
|
242
|
+
context: ZuploContext,
|
|
243
|
+
) {
|
|
244
|
+
let type: unknown;
|
|
245
|
+
try {
|
|
246
|
+
type = JSON.parse(data).type;
|
|
247
|
+
} catch {
|
|
248
|
+
type = undefined;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (typeof type !== "string" || !ALLOWED_TYPES.has(type)) {
|
|
252
|
+
context.log.warn("Closing WebSocket session", { type });
|
|
253
|
+
// `source` is the client in an inbound policy
|
|
254
|
+
source.close(4003, "Message type not allowed");
|
|
255
|
+
return undefined; // don't forward the message
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return data;
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
To tell the client why before you close, call `source.send(...)` with an error
|
|
263
|
+
message first.
|
|
264
|
+
|
|
265
|
+
## Add headers to the backend connection
|
|
266
|
+
|
|
267
|
+
<AppliesTo hosting="managed-edge" />
|
|
268
|
+
|
|
269
|
+
The gateway opens the backend connection with the headers of the upgrade request
|
|
270
|
+
after the route's inbound policies run. A policy that sets a header, such as the
|
|
271
|
+
[Set Headers policy](../policies/set-headers-inbound.mdx), sets it on the
|
|
272
|
+
backend connection. Use this to replace the client's credential with a backend
|
|
273
|
+
credential that the client never sees.
|
|
274
|
+
|
|
275
|
+
For example, after an [API Key](../policies/api-key-inbound.mdx) policy
|
|
276
|
+
authenticates the client, this Set Headers policy overwrites the client's
|
|
277
|
+
`Authorization` header with a key stored in an
|
|
278
|
+
[environment variable](../articles/environment-variables.mdx):
|
|
279
|
+
|
|
280
|
+
```json title="config/policies.json"
|
|
281
|
+
{
|
|
282
|
+
"name": "set-backend-credential",
|
|
283
|
+
"policyType": "set-headers-inbound",
|
|
284
|
+
"handler": {
|
|
285
|
+
"export": "SetHeadersInboundPolicy",
|
|
286
|
+
"module": "$import(@zuplo/runtime)",
|
|
287
|
+
"options": {
|
|
288
|
+
"headers": [
|
|
289
|
+
{
|
|
290
|
+
"name": "Authorization",
|
|
291
|
+
"value": "Bearer $env(BACKEND_API_KEY)"
|
|
292
|
+
}
|
|
293
|
+
]
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
For a complete example, see
|
|
300
|
+
[Proxy the OpenAI Realtime API](../articles/proxy-openai-realtime-api.mdx).
|
|
301
|
+
|
|
208
302
|
## The `onOpen` connection hook
|
|
209
303
|
|
|
210
304
|
The `onOpen` option configures a function that runs **once per connection**,
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Cookbook: Authorize MCP tool calls with OpenFGA"
|
|
3
|
+
sidebar_label: Authorize tool calls
|
|
4
|
+
description:
|
|
5
|
+
Authorize each MCP Server tool call by caller and tool arguments with OpenFGA,
|
|
6
|
+
so an AI agent can only act on the accounts and amounts its user is allowed.
|
|
7
|
+
applies_to:
|
|
8
|
+
product: gateway
|
|
9
|
+
hosting: all
|
|
10
|
+
plan: enterprise
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
Authenticating the MCP route tells you who the caller is. It doesn't tell you
|
|
14
|
+
whether that caller may run a particular tool with particular arguments, such as
|
|
15
|
+
transferring $5,000 out of account `acc_123`. This cookbook adds that decision
|
|
16
|
+
with the [OpenFGA Authorization policy](../policies/openfga-authz-inbound.mdx):
|
|
17
|
+
a small custom policy reads the caller and the tool arguments, and OpenFGA
|
|
18
|
+
decides whether the call goes ahead.
|
|
19
|
+
|
|
20
|
+
## Where the decision runs
|
|
21
|
+
|
|
22
|
+
When a client calls a tool, the MCP Server handler invokes the tool's API route
|
|
23
|
+
inside the gateway. That invocation runs the route's full inbound policy
|
|
24
|
+
pipeline, then its handler and outbound policies, the same as a direct HTTP
|
|
25
|
+
request. Put the authorization policies on the tool's route, not only on the MCP
|
|
26
|
+
route.
|
|
27
|
+
|
|
28
|
+
<Diagram height="h-56">
|
|
29
|
+
<DiagramNode id="client">MCP client</DiagramNode>
|
|
30
|
+
<DiagramNode id="mcp">MCP route: authentication</DiagramNode>
|
|
31
|
+
<DiagramNode id="tool">
|
|
32
|
+
Tool route: authentication, checks, OpenFGA
|
|
33
|
+
</DiagramNode>
|
|
34
|
+
<DiagramNode id="api">Your API</DiagramNode>
|
|
35
|
+
<DiagramEdge from="client" to="mcp" label="tools/call" />
|
|
36
|
+
<DiagramEdge from="mcp" to="tool" label="POST /accounts/acc_123/transfers" />
|
|
37
|
+
<DiagramEdge from="tool" to="api" />
|
|
38
|
+
</Diagram>
|
|
39
|
+
|
|
40
|
+
On the tool's route, a policy sees:
|
|
41
|
+
|
|
42
|
+
| What | Where to read it |
|
|
43
|
+
| ------------------ | ------------------------------------------------------------------------------------------- |
|
|
44
|
+
| The caller | `request.user`, which the authentication policy on the tool's route sets |
|
|
45
|
+
| Path arguments | `request.params` |
|
|
46
|
+
| Query arguments | `request.query` |
|
|
47
|
+
| Body arguments | `await request.clone().json()` |
|
|
48
|
+
| The tool name | `new ZuploMcpSdk(context).getRawCallToolRequest()?.params.name` |
|
|
49
|
+
| The raw tool input | `new ZuploMcpSdk(context).getRawCallToolRequest()?.params.arguments`, as the client sent it |
|
|
50
|
+
|
|
51
|
+
The MCP Server handler maps tool arguments onto the route's path, query, header,
|
|
52
|
+
and body parameters before it invokes the route, for both flat and nested
|
|
53
|
+
[tool input styles](./tools.mdx). Read the arguments from the request, and your
|
|
54
|
+
policy works the same for every input style.
|
|
55
|
+
|
|
56
|
+
The handler passes the MCP request's headers, including `Authorization`, to the
|
|
57
|
+
tool's route. Add the same authentication policy you use on the MCP route to the
|
|
58
|
+
tool's route, and `request.user` holds the same caller.
|
|
59
|
+
|
|
60
|
+
## Example: limit transfers by account and amount
|
|
61
|
+
|
|
62
|
+
The example API has a `transferFunds` operation,
|
|
63
|
+
`POST /accounts/{accountId}/transfers`, with a body of
|
|
64
|
+
`{ "amount": 250, "toAccountId": "acc_456" }`. The operation declares the
|
|
65
|
+
`accountId` path parameter and the request body schema, so the tool takes them
|
|
66
|
+
as arguments. The tool's name is the `operationId`, `transferFunds`, unless you
|
|
67
|
+
set `mcp.name` on the route. The rules are:
|
|
68
|
+
|
|
69
|
+
- Only callers you grant the `transferFunds` tool can call it.
|
|
70
|
+
- A caller can transfer from an account only up to the limit you grant them on
|
|
71
|
+
that account.
|
|
72
|
+
|
|
73
|
+
### 1. Model the rules in OpenFGA
|
|
74
|
+
|
|
75
|
+
Write the authorization model in your OpenFGA store. The `within_transfer_limit`
|
|
76
|
+
condition compares the requested amount, which the gateway sends with each
|
|
77
|
+
check, to the maximum stored on the relationship:
|
|
78
|
+
|
|
79
|
+
```txt
|
|
80
|
+
model
|
|
81
|
+
schema 1.1
|
|
82
|
+
|
|
83
|
+
type user
|
|
84
|
+
|
|
85
|
+
type mcp_tool
|
|
86
|
+
relations
|
|
87
|
+
define can_call: [user]
|
|
88
|
+
|
|
89
|
+
type account
|
|
90
|
+
relations
|
|
91
|
+
define can_transfer: [user with within_transfer_limit]
|
|
92
|
+
|
|
93
|
+
condition within_transfer_limit(amount: double, max_amount: double) {
|
|
94
|
+
amount <= max_amount
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Then write tuples for each user. For example, to let `alice` call the tool and
|
|
99
|
+
transfer up to 500 from `acc_123`:
|
|
100
|
+
|
|
101
|
+
| User | Relation | Object | Condition |
|
|
102
|
+
| ------------ | -------------- | ------------------------ | ------------------------------------------------ |
|
|
103
|
+
| `user:alice` | `can_call` | `mcp_tool:transferFunds` | None |
|
|
104
|
+
| `user:alice` | `can_transfer` | `account:acc_123` | `within_transfer_limit`, `{ "max_amount": 500 }` |
|
|
105
|
+
|
|
106
|
+
### 2. Write the checks policy
|
|
107
|
+
|
|
108
|
+
Create `modules/transfer-authz.ts`. The policy builds two OpenFGA checks from
|
|
109
|
+
the caller and the arguments, and stores them on the context with
|
|
110
|
+
`OpenFGAAuthZInboundPolicy.setContextChecks`. The OpenFGA policy that runs next
|
|
111
|
+
sends them to your store.
|
|
112
|
+
|
|
113
|
+
```ts title="modules/transfer-authz.ts"
|
|
114
|
+
import {
|
|
115
|
+
HttpProblems,
|
|
116
|
+
OpenFGAAuthZInboundPolicy,
|
|
117
|
+
ZuploContext,
|
|
118
|
+
ZuploMcpSdk,
|
|
119
|
+
ZuploRequest,
|
|
120
|
+
} from "@zuplo/runtime";
|
|
121
|
+
|
|
122
|
+
interface TransferBody {
|
|
123
|
+
amount: number;
|
|
124
|
+
toAccountId: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
interface Check {
|
|
128
|
+
user: string;
|
|
129
|
+
relation: string;
|
|
130
|
+
object: string;
|
|
131
|
+
context?: object;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function setTransferChecks(
|
|
135
|
+
request: ZuploRequest,
|
|
136
|
+
context: ZuploContext,
|
|
137
|
+
) {
|
|
138
|
+
const userId = request.user?.sub;
|
|
139
|
+
if (!userId) {
|
|
140
|
+
return HttpProblems.unauthorized(request, context);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const { amount } = (await request.clone().json()) as TransferBody;
|
|
144
|
+
if (typeof amount !== "number") {
|
|
145
|
+
return HttpProblems.badRequest(request, context, {
|
|
146
|
+
detail: "amount must be a number",
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const checks: Check[] = [
|
|
151
|
+
{
|
|
152
|
+
user: `user:${userId}`,
|
|
153
|
+
relation: "can_transfer",
|
|
154
|
+
object: `account:${request.params.accountId}`,
|
|
155
|
+
// Values for the within_transfer_limit condition
|
|
156
|
+
context: { amount },
|
|
157
|
+
},
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
// Present only when the route runs for an MCP tool call
|
|
161
|
+
const toolCall = new ZuploMcpSdk(context).getRawCallToolRequest();
|
|
162
|
+
if (toolCall) {
|
|
163
|
+
checks.push({
|
|
164
|
+
user: `user:${userId}`,
|
|
165
|
+
relation: "can_call",
|
|
166
|
+
object: `mcp_tool:${toolCall.params.name}`,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
OpenFGAAuthZInboundPolicy.setContextChecks(context, checks);
|
|
171
|
+
return request;
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The request goes ahead only when OpenFGA allows every check. If any check fails,
|
|
176
|
+
the OpenFGA policy returns `403 Forbidden`, and the MCP client receives a tool
|
|
177
|
+
result with `isError` set.
|
|
178
|
+
|
|
179
|
+
### 3. Configure the policies
|
|
180
|
+
|
|
181
|
+
Add the checks policy and the OpenFGA policy to `config/policies.json`. Store
|
|
182
|
+
the OpenFGA token in an
|
|
183
|
+
[environment variable](../articles/environment-variables.mdx):
|
|
184
|
+
|
|
185
|
+
```json title="config/policies.json"
|
|
186
|
+
{
|
|
187
|
+
"policies": [
|
|
188
|
+
{
|
|
189
|
+
"name": "set-transfer-checks",
|
|
190
|
+
"policyType": "custom-code-inbound",
|
|
191
|
+
"handler": {
|
|
192
|
+
"export": "setTransferChecks",
|
|
193
|
+
"module": "$import(./modules/transfer-authz)"
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
"name": "openfga-authz",
|
|
198
|
+
"policyType": "openfga-authz-inbound",
|
|
199
|
+
"handler": {
|
|
200
|
+
"export": "OpenFGAAuthZInboundPolicy",
|
|
201
|
+
"module": "$import(@zuplo/runtime)",
|
|
202
|
+
"options": {
|
|
203
|
+
"apiUrl": "https://api.us1.fga.dev",
|
|
204
|
+
"storeId": "$env(OPENFGA_STORE_ID)",
|
|
205
|
+
"authorizationModelId": "$env(OPENFGA_MODEL_ID)",
|
|
206
|
+
"credentials": {
|
|
207
|
+
"method": "api-token",
|
|
208
|
+
"token": "$env(OPENFGA_API_TOKEN)"
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
]
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### 4. Add the policies to the tool's route
|
|
218
|
+
|
|
219
|
+
On the `transferFunds` operation, list the authentication policy first, then the
|
|
220
|
+
checks policy, then the OpenFGA policy. This example uses `mcp-oauth-inbound`,
|
|
221
|
+
the OAuth policy on the MCP route from the
|
|
222
|
+
[MCP Server handler](../handlers/mcp-server.mdx#oauth-authentication) setup:
|
|
223
|
+
|
|
224
|
+
```json title="config/routes.oas.json"
|
|
225
|
+
"/accounts/{accountId}/transfers": {
|
|
226
|
+
"post": {
|
|
227
|
+
"operationId": "transferFunds",
|
|
228
|
+
"x-zuplo-route": {
|
|
229
|
+
"handler": {
|
|
230
|
+
"export": "urlForwardHandler",
|
|
231
|
+
"module": "$import(@zuplo/runtime)",
|
|
232
|
+
"options": {
|
|
233
|
+
"baseUrl": "https://api.example.com"
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
"policies": {
|
|
237
|
+
"inbound": [
|
|
238
|
+
"mcp-oauth-inbound",
|
|
239
|
+
"set-transfer-checks",
|
|
240
|
+
"openfga-authz"
|
|
241
|
+
]
|
|
242
|
+
},
|
|
243
|
+
"mcp": {
|
|
244
|
+
"type": "tool"
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Because the checks run on the route, they also apply when a client calls
|
|
252
|
+
`POST /accounts/{accountId}/transfers` directly over HTTP. In that case there's
|
|
253
|
+
no tool call, so only the account check runs. To accept the operation only
|
|
254
|
+
through the MCP server, also add the
|
|
255
|
+
[Internal Only policy](../policies/internal-only-inbound.mdx), as described in
|
|
256
|
+
[Keep tool routes private](./tools.mdx#keep-tool-routes-private).
|
|
257
|
+
|
|
258
|
+
## Use AuthZEN instead
|
|
259
|
+
|
|
260
|
+
If your policy decision point speaks AuthZEN, use the
|
|
261
|
+
[AuthZEN Authorization policy](../policies/authzen-inbound.mdx) in place of
|
|
262
|
+
OpenFGA. Build the subject, resource, and action from `request.user` and the
|
|
263
|
+
arguments in the same kind of custom policy, and store them with
|
|
264
|
+
`AuthZenInboundPolicy.setAuthorizationPayload`. This example uses the `userId`
|
|
265
|
+
and `toolCall` variables from the checks policy above:
|
|
266
|
+
|
|
267
|
+
```ts
|
|
268
|
+
AuthZenInboundPolicy.setAuthorizationPayload(context, {
|
|
269
|
+
subject: { type: "user", id: userId },
|
|
270
|
+
resource: { type: "account", id: request.params.accountId },
|
|
271
|
+
action: { name: toolCall?.params.name ?? request.method },
|
|
272
|
+
});
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
An AuthZEN request carries one subject, resource, and action. To decide on an
|
|
276
|
+
argument value such as the amount, use OpenFGA conditions as this cookbook does.
|
|
277
|
+
|
|
278
|
+
## Next steps
|
|
279
|
+
|
|
280
|
+
- Choose which operations become tools in [Tools](./tools.mdx).
|
|
281
|
+
- Read every [ZuploMcpSdk](../programmable-api/mcp-sdk.mdx) method available to
|
|
282
|
+
policies and handlers.
|
|
283
|
+
- Test the denied and allowed paths with the tools in
|
|
284
|
+
[Test your MCP server](./testing.mdx).
|
|
@@ -72,6 +72,9 @@ Expose only the operations your clients need. Apply authentication and access
|
|
|
72
72
|
controls to those operations, use rate limits to control usage, and configure
|
|
73
73
|
[audit logging](../articles/audit-logging.mdx) to record calls.
|
|
74
74
|
|
|
75
|
+
To decide per call whether a caller can run a tool with the arguments it sent,
|
|
76
|
+
see [Authorize tool calls with OpenFGA](./authorize-tool-calls.mdx).
|
|
77
|
+
|
|
75
78
|
Each operation the server exposes is also a REST route that clients can reach
|
|
76
79
|
directly over HTTP. To serve those operations only through the MCP server, add
|
|
77
80
|
the [Internal Only policy](../policies/internal-only-inbound.mdx) to each route,
|