@xano/developer-mcp 2.2.2-beta.2 → 2.2.2

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.
@@ -4,91 +4,52 @@ applyTo: "middleware/*.xs"
4
4
 
5
5
  # Middleware
6
6
 
7
- > **TL;DR:** Declare a middleware once (`middleware <name> { … }`), then attach it
8
- > with a `middleware = { pre: [...], post: [...] }` block on a **query/API**, a
9
- > **function**, or an **API group**. One middleware object serves both phases;
10
- > branch on `$input.type` (`"pre"` | `"post"`). Its `input` block is fixed to
11
- > `vars` + `type` — custom parameters are stripped.
12
-
13
- Middleware intercepts requests before (`pre`) and after (`post`) your queries,
14
- functions, tasks, and tools run. You define the middleware in one place and attach
15
- it to the constructs that should use it.
7
+ Middleware intercepts and processes requests before and after your functions, queries, tasks, and tools execute.
16
8
 
17
9
  ## Quick Reference
18
10
 
19
11
  ```xs
20
- // 1. Declare the middleware (the input block is fixed — see Input Contract)
21
- middleware add_metadata {
22
- input {
23
- json vars
24
- enum type {
25
- values = ["pre", "post"]
26
- }
27
- }
28
-
29
- stack {
30
- conditional {
31
- if ($input.type == "post") {
32
- // post-phase work
33
- }
34
- else {
35
- // pre-phase work
36
- }
37
- }
38
- }
39
-
40
- response = { server_time: now }
41
- response_strategy = "merge"
42
- exception_policy = "silent"
43
- }
44
- ```
45
-
46
- ```xs
47
- // 2. Attach it — reference the middleware by name
48
- query list_items verb=GET {
49
- api_group = "catalog"
50
-
51
- stack {
52
- db.query "items" { } as $items
53
- }
54
-
55
- response = $items
56
- middleware = {
57
- pre : [{name: "rate_limit"}]
58
- post: [{name: "add_metadata"}]
59
- }
12
+ middleware "<name>" {
13
+ description = "What this middleware does"
14
+ exception_policy = "silent" | "rethrow" | "critical"
15
+ response_strategy = "merge" | "replace"
16
+ input { ... }
17
+ stack { ... }
18
+ response = $result
60
19
  }
61
20
  ```
62
21
 
63
- | Piece | Purpose |
22
+ ### Required Blocks
23
+ | Block | Purpose |
64
24
  |-------|---------|
65
- | `middleware <name> { … }` | The reusable definition |
66
- | `input { json vars; enum type { … } }` | Fixed system inputs (see Input Contract) |
67
- | `response_strategy` | `merge` (default) or `replace` — how the middleware response combines |
68
- | `exception_policy` | `silent` (default), `rethrow`, or `critical` |
69
- | `middleware = { pre: [...], post: [...] }` | Attaches middleware to a construct or group by name |
25
+ | `input` | Define parameters passed to middleware |
26
+ | `stack` | Processing logic |
27
+ | `response` | Output returned to caller |
28
+
29
+ ### Optional Attributes
30
+ | Attribute | Values | Default | Description |
31
+ |-----------|--------|---------|-------------|
32
+ | `description` | text | - | Documents middleware purpose |
33
+ | `exception_policy` | `silent`, `rethrow`, `critical` | `rethrow` | How to handle errors |
34
+ | `response_strategy` | `merge`, `replace` | `merge` | How response combines with original |
70
35
 
71
36
  ---
72
37
 
73
- ## Declaring a Middleware
38
+ ## Basic Structure
74
39
 
75
40
  ```xs
76
- middleware log_request {
41
+ middleware "log_request" {
77
42
  description = "Logs all incoming requests"
78
43
 
79
44
  input {
80
- json vars
81
- enum type {
82
- values = ["pre", "post"]
83
- }
45
+ json request_data
84
46
  }
85
47
 
86
48
  stack {
87
- var $timestamp { value = now }
88
49
  debug.log {
89
50
  value = {
90
- timestamp: $timestamp,
91
- data: $input.vars
51
+ timestamp: now,
52
+ data: $input.request_data
92
53
  }
93
54
  }
94
55
  }
@@ -97,237 +58,70 @@ middleware log_request {
97
58
  }
98
59
  ```
99
60
 
100
- ### Required Blocks
101
- | Block | Required | Purpose |
102
- |-------|----------|---------|
103
- | `input` | Yes | Fixed system inputs (`vars`, `type`) — see Input Contract |
104
- | `stack` | Yes | Processing logic |
105
- | `response` | No | Output returned to the caller (omit it freely) |
106
-
107
- Omitting `input` or `stack` is a syntax error. Omitting `response` is fine.
108
-
109
- ### Optional Attributes
110
- | Attribute | Values | Default | Description |
111
- |-----------|--------|---------|-------------|
112
- | `description` | text | - | Documents the middleware (stored as a leading comment) |
113
- | `exception_policy` | `silent`, `rethrow`, `critical` | `silent` | How errors are handled |
114
- | `response_strategy` | `merge`, `replace` | `merge` | How the middleware response combines with the original |
115
-
116
- Values outside the listed enums are rejected (`Input "…" is not one of the allowable values.`).
117
-
118
- > The quoted name (`middleware "<name>"`) parses, but the canonical form is
119
- > unquoted: `middleware <name> { … }`. There is **no** `type` attribute on the
120
- > declaration — `type = "post"` fails with `Invalid block: type`. Which phase a
121
- > middleware runs in is decided where you attach it, not here.
122
-
123
- ---
124
-
125
- ## Input Contract (footgun)
126
-
127
- A middleware's `input` block is **fixed**. It is always normalised to exactly two
128
- system inputs, and **any custom parameters you add are silently discarded on push**:
129
-
130
- ```xs
131
- // ❌ These custom params are stripped — $input.token / $input.user_id become null
132
- input {
133
- text token
134
- int user_id
135
- }
136
-
137
- // ✅ The only inputs that survive — this is what every middleware receives
138
- input {
139
- json vars // request (pre) or response (post) payload context
140
- enum type { // which phase this invocation is running in
141
- values = ["pre", "post"]
142
- }
143
- }
144
- ```
145
-
146
- Read middleware data through `$input.vars` and branch on `$input.type` — never
147
- through named custom parameters.
148
-
149
- ### Pre vs Post phases
150
-
151
- There is **one middleware object per name, and it serves both phases**. The same
152
- `stack` runs for the pre and the post invocation; `$input.type` (`"pre"` |
153
- `"post"`) tells you which one you are in. Branch on it to do pre-work vs post-work
154
- in a single middleware:
155
-
156
- ```xs
157
- stack {
158
- conditional {
159
- if ($input.type == "pre") {
160
- // runs BEFORE the endpoint — inspect $input.vars (the request);
161
- // short-circuit with a precondition to block the request.
162
- }
163
- else {
164
- // runs AFTER the endpoint — $input.vars carries the response;
165
- // transform or augment it (combined per response_strategy).
166
- }
167
- }
168
- }
169
- ```
170
-
171
- ---
172
-
173
- ## Attaching Middleware
174
-
175
- A middleware does nothing until it is **attached**. Attach it with a `middleware`
176
- block that lists middleware by name for the `pre` and/or `post` phase:
177
-
178
- ```xs
179
- middleware = {
180
- pre : [{name: "rate_limit"}]
181
- post: [{name: "add_metadata"}]
182
- }
183
- ```
184
-
185
- Both lists are optional and independent — supply only the phase you need. A list
186
- can hold more than one middleware, and they run in the order written:
187
-
188
- ```xs
189
- middleware = {
190
- pre: [{name: "rate_limit"}, {name: "auth_check"}]
191
- }
192
- ```
193
-
194
- You can attach middleware at three scopes:
195
-
196
- ### On a query / API endpoint (per-endpoint)
197
-
198
- ```xs
199
- query create_order verb=POST {
200
- api_group = "orders"
201
-
202
- input {
203
- json order
204
- }
205
-
206
- stack {
207
- db.add "orders" { data = $input.order } as $new
208
- }
209
-
210
- response = $new
211
- middleware = {
212
- pre : [{name: "rate_limit"}]
213
- post: [{name: "add_metadata"}]
214
- }
215
- }
216
- ```
217
-
218
- ### On a function (function-stack)
219
-
220
- ```xs
221
- function process_payment {
222
- input {
223
- decimal amount
224
- }
225
-
226
- stack {
227
- var $result { value = { charged: $input.amount } }
228
- }
229
-
230
- response = $result
231
- middleware = {
232
- post: [{name: "audit_log"}]
233
- }
234
- }
235
- ```
236
-
237
- ### On an API group (applies to the whole group)
238
-
239
- Attach a `middleware` block to the `api_group` declaration to run it for every
240
- endpoint in that group:
241
-
242
- ```xs
243
- api_group orders {
244
- canonical = "orders_grp"
245
- middleware = {
246
- pre : [{name: "rate_limit"}]
247
- post: [{name: "add_metadata"}]
248
- }
249
- }
250
- ```
251
-
252
- > **Workspace-wide (global) middleware is configured in the Xano dashboard.** The
253
- > `middleware` block is only valid on queries, functions, and API groups — placing
254
- > it on the `workspace` declaration fails with `Invalid block: middleware`. To run
255
- > a middleware across the whole workspace, attach it from the dashboard's
256
- > middleware settings.
257
-
258
- > ⚠️ **Middleware names must match exactly.** A name that doesn't resolve to an
259
- > existing middleware is silently blanked (`{name: ""}`) on push, and the endpoint
260
- > then runs with no middleware — no error at push or request time. Double-check the
261
- > spelling against the middleware's declared name.
262
-
263
61
  ---
264
62
 
265
63
  ## Exception Policies
266
64
 
267
- Control how errors thrown inside the middleware are handled.
65
+ Control how middleware handles errors:
268
66
 
269
- ### silent (default)
270
- Errors are caught and ignored; execution continues.
67
+ ### silent
68
+ Errors are caught and ignored. Execution continues normally.
271
69
 
272
70
  ```xs
273
- middleware optional_enrichment {
71
+ middleware "optional_enrichment" {
274
72
  exception_policy = "silent"
275
73
 
276
74
  input {
277
- json vars
278
- enum type { values = ["pre", "post"] }
75
+ int user_id
279
76
  }
280
77
 
281
78
  stack {
282
- // If this fails, the request continues without enrichment
283
- api.request {
284
- url = "https://api.example.com/enrich"
285
- method = "GET"
79
+ // If this fails, request continues without enrichment
80
+ api.call "external_service" {
81
+ url = "https://api.example.com/enrich/" ~ $input.user_id
286
82
  } as $enriched
287
83
  }
288
84
 
289
- response = { enrichment: $enriched }
85
+ response = $enriched
290
86
  }
291
87
  ```
292
88
 
293
- ### rethrow
294
- Errors are passed through to the caller; standard error handling applies.
89
+ ### rethrow (default)
90
+ Errors are passed through to the caller. Standard error handling applies.
295
91
 
296
92
  ```xs
297
- middleware validate_token {
93
+ middleware "validate_token" {
298
94
  exception_policy = "rethrow"
299
95
 
300
96
  input {
301
- json vars
302
- enum type { values = ["pre", "post"] }
97
+ text token
303
98
  }
304
99
 
305
100
  stack {
306
- precondition (($input.vars|get:"token"|strlen) > 0) {
101
+ precondition ($input.token|strlen > 0) {
307
102
  error_type = "accessdenied"
308
103
  error = "Token required"
309
104
  }
310
105
  }
311
106
 
312
- response = null
107
+ response = { valid: true }
313
108
  }
314
109
  ```
315
110
 
316
111
  ### critical
317
- Errors are treated as critical failures and terminate the request immediately.
112
+ Errors are treated as critical failures. Request is immediately terminated.
318
113
 
319
114
  ```xs
320
- middleware security_check {
115
+ middleware "security_check" {
321
116
  exception_policy = "critical"
322
117
 
323
118
  input {
324
- json vars
325
- enum type { values = ["pre", "post"] }
119
+ text ip_address
326
120
  }
327
121
 
328
122
  stack {
329
123
  db.query "blocked_ips" {
330
- where = $db.blocked_ips.ip == $env.$remote_ip
124
+ where = $db.blocked_ips.ip == $input.ip_address
331
125
  return = { type: "exists" }
332
126
  } as $is_blocked
333
127
 
@@ -337,7 +131,7 @@ middleware security_check {
337
131
  }
338
132
  }
339
133
 
340
- response = null
134
+ response = { allowed: true }
341
135
  }
342
136
  ```
343
137
 
@@ -345,68 +139,54 @@ middleware security_check {
345
139
 
346
140
  ## Response Strategies
347
141
 
348
- Control how the middleware's `response` combines with the endpoint's response.
349
- This applies to the **post** phase, where `$input.vars` carries the response.
142
+ Control how middleware response combines with the original response:
350
143
 
351
144
  ### merge (default)
352
- The middleware response is merged into the original response.
145
+ Middleware response is merged with original response.
353
146
 
354
147
  ```xs
355
- middleware add_metadata {
148
+ middleware "add_metadata" {
356
149
  response_strategy = "merge"
357
150
 
358
151
  input {
359
- json vars
360
- enum type { values = ["pre", "post"] }
361
152
  }
362
153
 
363
154
  stack {
364
- var $server_time { value = now }
365
- }
366
-
367
- response = {
368
- _meta: {
369
- server_time: $server_time,
370
- version: "1.0.0"
155
+ var $meta {
156
+ value = {
157
+ server_time: now,
158
+ version: "1.0.0"
159
+ }
371
160
  }
372
161
  }
162
+
163
+ response = { _meta: $meta }
373
164
  }
374
165
  // Original: { data: [...] }
375
- // Result: { data: [...], _meta: { server_time: ..., version: "1.0.0" } }
166
+ // Result: { data: [...], _meta: { server_time: ..., version: "1.0.0" } }
376
167
  ```
377
168
 
378
- > ⚠️ **`now` only evaluates as a direct value.** Bare `now` returns the current
379
- > timestamp when it is the whole value of a `var` or a top-level entry of the
380
- > `response` object. Nested **inside** a `response` object literal (e.g. under
381
- > `_meta`) it is emitted as the literal string `"now"`. Compute it into a `var`
382
- > first and reference that var:
383
- > ```xs
384
- > stack { var $server_time { value = now } }
385
- > response = { _meta: { server_time: $server_time } } // ✅ real timestamp
386
- > ```
387
-
388
169
  ### replace
389
- The middleware response completely replaces the original response.
170
+ Middleware response completely replaces original response.
390
171
 
391
172
  ```xs
392
- middleware wrap_response {
173
+ middleware "transform_response" {
393
174
  response_strategy = "replace"
394
175
 
395
176
  input {
396
- json vars
397
- enum type { values = ["pre", "post"] }
177
+ json original_response
398
178
  }
399
179
 
400
180
  stack {
401
- var $wrapped {
181
+ var $transformed {
402
182
  value = {
403
183
  success: true,
404
- payload: $input.vars
184
+ payload: $input.original_response
405
185
  }
406
186
  }
407
187
  }
408
188
 
409
- response = $wrapped
189
+ response = $transformed
410
190
  }
411
191
  ```
412
192
 
@@ -414,153 +194,116 @@ middleware wrap_response {
414
194
 
415
195
  ## Common Patterns
416
196
 
417
- Every middleware receives the same two inputs (`vars`, `type`). Read request or
418
- response data from `$input.vars` and use `$input.type` to act only in the phase you
419
- want.
420
-
421
- ### Audit logging (post)
197
+ ### Request Logging
422
198
 
423
199
  ```xs
424
- middleware audit_log {
425
- description = "Logs requests for an audit trail"
200
+ middleware "audit_log" {
201
+ description = "Logs all API requests for audit trail"
426
202
  exception_policy = "silent"
427
203
 
428
204
  input {
429
- json vars
430
- enum type { values = ["pre", "post"] }
205
+ text endpoint
206
+ text method
207
+ json request_body?
208
+ int user_id?
431
209
  }
432
210
 
433
211
  stack {
434
- conditional {
435
- if ($input.type == "post") {
436
- db.add "audit_log" {
437
- data = {
438
- context: $input.vars,
439
- ip_address: $env.$remote_ip,
440
- created_at: now
441
- }
442
- }
212
+ db.add "audit_log" {
213
+ data = {
214
+ endpoint: $input.endpoint,
215
+ method: $input.method,
216
+ request_body: $input.request_body,
217
+ user_id: $input.user_id,
218
+ ip_address: $env.$remote_ip,
219
+ created_at: now
443
220
  }
444
221
  }
445
222
  }
223
+
224
+ response = null
446
225
  }
447
226
  ```
448
227
 
449
- ### Rate limiting (pre)
228
+ ### Rate Limiting
450
229
 
451
230
  ```xs
452
- middleware rate_limit {
453
- description = "Limits requests per client IP"
231
+ middleware "rate_limit" {
232
+ description = "Enforces rate limits per user"
233
+ exception_policy = "rethrow"
454
234
 
455
235
  input {
456
- json vars
457
- enum type { values = ["pre", "post"] }
236
+ int user_id
237
+ int max_requests?=100
238
+ int window_seconds?=60
458
239
  }
459
240
 
460
241
  stack {
461
- conditional {
462
- if ($input.type == "pre") {
463
- redis.ratelimit {
464
- key = "ratelimit:" ~ $env.$remote_ip
465
- max = 100
466
- ttl = 60
467
- error = "Rate limit exceeded"
468
- }
469
- }
242
+ redis.ratelimit {
243
+ key = "ratelimit:" ~ $input.user_id
244
+ max = $input.max_requests
245
+ ttl = $input.window_seconds
246
+ error = "Rate limit exceeded"
470
247
  }
471
248
  }
249
+
250
+ response = null
472
251
  }
473
252
  ```
474
253
 
475
- ### Response caching (post)
254
+ ### Response Caching
476
255
 
477
256
  ```xs
478
- middleware cache_response {
257
+ middleware "cache_response" {
479
258
  description = "Caches responses in Redis"
480
259
  exception_policy = "silent"
481
260
 
482
261
  input {
483
- json vars
484
- enum type { values = ["pre", "post"] }
262
+ text cache_key
263
+ int ttl_seconds?=300
264
+ json response_data
485
265
  }
486
266
 
487
267
  stack {
488
- conditional {
489
- if ($input.type == "post") {
490
- redis.set {
491
- key = "cache:" ~ ($input.vars|json_encode)
492
- data = $input.vars
493
- ttl = 300
494
- }
495
- }
268
+ redis.set {
269
+ key = $input.cache_key
270
+ value = $input.response_data|json_encode
271
+ expire = $input.ttl_seconds
496
272
  }
497
273
  }
274
+
275
+ response = null
498
276
  }
499
277
  ```
500
278
 
501
279
  ---
502
280
 
503
- ## Common Mistakes
281
+ ## Applying Middleware
504
282
 
505
- ❌ **Adding custom inputs to a middleware:**
506
- ```xs
507
- input {
508
- text token // stripped on push — $input.token is null at runtime
509
- }
510
- ```
511
- ✅ **Read from the fixed `vars` input:**
512
- ```xs
513
- input {
514
- json vars
515
- enum type { values = ["pre", "post"] }
516
- }
517
- // ... reference $input.vars, e.g. ($input.vars|get:"token")
518
- ```
283
+ Middleware is configured at the branch level. See `xano_xanoscript_docs({ topic: "branch" })` for configuration details.
519
284
 
520
- ❌ **Expecting a `type` attribute to pick the phase:**
521
- ```xs
522
- middleware log_request {
523
- type = "post" // Invalid block: type
524
- ...
525
- }
526
- ```
527
- ✅ **One object serves both phases — branch on `$input.type`:**
528
285
  ```xs
529
- stack {
530
- conditional {
531
- if ($input.type == "post") { /* ... */ }
286
+ branch "production" {
287
+ middleware = {
288
+ query: {
289
+ pre: ["validate_token", "rate_limit"],
290
+ post: ["audit_log", "cache_response"]
291
+ },
292
+ function: {
293
+ pre: ["validate_token"],
294
+ post: ["audit_log"]
295
+ }
532
296
  }
533
297
  }
534
298
  ```
535
299
 
536
- ❌ **Misspelling the attached name:**
537
- ```xs
538
- middleware = { pre: [{name: "rate_limitr"}] } // silently becomes {name: ""} — no middleware runs
539
- ```
540
- ✅ **Match the declared middleware name exactly:**
541
- ```xs
542
- middleware = { pre: [{name: "rate_limit"}] }
543
- ```
544
-
545
- ❌ **Putting a middleware block on the workspace declaration:**
546
- ```xs
547
- workspace "my-app" {
548
- middleware = { pre: [...] } // Invalid block: middleware
549
- }
550
- ```
551
- ✅ **Attach on a query, function, or API group — or configure workspace-wide
552
- middleware in the dashboard.**
553
-
554
300
  ---
555
301
 
556
302
  ## Best Practices
557
303
 
558
- 1. **Keep middleware focused** — each one should do a single thing well.
559
- 2. **Choose the exception policy deliberately** — `critical` for security gates,
560
- `silent` for optional enrichment.
561
- 3. **Attach at the right scope** — per-endpoint for targeted logic, on the API
562
- group for cross-cutting concerns shared by every endpoint in the group.
563
- 4. **Mind performance** — middleware runs on every matching request.
304
+ 1. **Keep middleware focused** - Each middleware should do one thing well
305
+ 2. **Use appropriate exception policies** - Critical for security, silent for optional enrichment
306
+ 3. **Consider performance** - Middleware runs on every request; log even silent failures
564
307
 
565
308
  ---
566
309
 
@@ -568,7 +311,7 @@ middleware in the dashboard.**
568
311
 
569
312
  | Topic | Description |
570
313
  |-------|-------------|
571
- | `apis` | API endpoint request/response lifecycle |
572
- | `functions` | Reusable function stacks |
314
+ | `branch` | Branch-level middleware configuration |
573
315
  | `security` | Authentication and authorization patterns |
574
- | `integrations/redis` | Rate limiting and caching primitives |
316
+ | `apis` | API endpoint request/response lifecycle |
317
+ | `performance` | Performance optimization strategies |
@@ -1,4 +1,4 @@
1
1
  {
2
- "version": "2.5.0",
3
- "updated": "2026-07-02"
2
+ "version": "2.4.0",
3
+ "updated": "2026-06-10"
4
4
  }