@xano/developer-mcp 2.2.4 → 2.2.6-beta.0

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,314 +4,151 @@ applyTo: "middleware/*.xs"
4
4
 
5
5
  # Middleware
6
6
 
7
- Middleware intercepts and processes requests before and after your functions, queries, tasks, and tools execute.
7
+ > **TL;DR:** Define a middleware, then attach it inside the API's `query` declaration with `middleware = {post: [{name: "docs_post"}]}`. Post middleware receives the response envelope in `$input.vars`; the successful endpoint result is `$input.vars.result`.
8
8
 
9
9
  ## Quick Reference
10
10
 
11
- ```xs
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
19
- }
20
- ```
11
+ | Need | Syntax |
12
+ |------|--------|
13
+ | Attach to one API | `middleware = {post: [{name: "docs_post"}]}` alongside `response` |
14
+ | Middleware inputs | `json vars` and `enum type { values = ["pre", "post"] }` |
15
+ | Current phase | `$input.type` |
16
+ | Successful post result | `$input.vars.result` |
17
+ | Add response fields | `response_strategy = "merge"` |
18
+ | Replace response | `response_strategy = "replace"` |
19
+ | Surface middleware errors | `exception_policy = "rethrow"` |
21
20
 
22
- ### Required Blocks
23
- | Block | Purpose |
24
- |-------|---------|
25
- | `input` | Define parameters passed to middleware |
26
- | `stack` | Processing logic |
27
- | `response` | Output returned to caller |
21
+ Use an array of `{name: "..."}` objects, not an array of strings. Declare the referenced middleware before attaching it. The name must match exactly.
28
22
 
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 |
23
+ ## Attach Post Middleware to an API
35
24
 
36
- ---
25
+ Create each declaration in its own `.xs` file. This example needs no database table.
37
26
 
38
- ## Basic Structure
27
+ ### 1. Define the middleware
39
28
 
40
29
  ```xs
41
- middleware "log_request" {
42
- description = "Logs all incoming requests"
43
-
30
+ middleware docs_post {
31
+ exception_policy = "rethrow"
32
+ response_strategy = "merge"
44
33
  input {
45
- json request_data
46
- }
47
-
48
- stack {
49
- debug.log {
50
- value = {
51
- timestamp: now,
52
- data: $input.request_data
53
- }
34
+ json vars
35
+ enum type {
36
+ values = ["pre", "post"]
54
37
  }
55
38
  }
56
-
57
- response = null
58
- }
59
- ```
60
-
61
- ---
62
-
63
- ## Exception Policies
64
-
65
- Control how middleware handles errors:
66
-
67
- ### silent
68
- Errors are caught and ignored. Execution continues normally.
69
-
70
- ```xs
71
- middleware "optional_enrichment" {
72
- exception_policy = "silent"
73
-
74
- input {
75
- int user_id
76
- }
77
-
78
39
  stack {
79
- // If this fails, request continues without enrichment
80
- api.call "external_service" {
81
- url = "https://api.example.com/enrich/" ~ $input.user_id
82
- } as $enriched
40
+ var $observed { value = $input.vars }
83
41
  }
84
-
85
- response = $enriched
42
+ response = {middleware_phase: $input.type, observed: $observed}
86
43
  }
87
44
  ```
88
45
 
89
- ### rethrow (default)
90
- Errors are passed through to the caller. Standard error handling applies.
91
-
92
- ```xs
93
- middleware "validate_token" {
94
- exception_policy = "rethrow"
95
-
96
- input {
97
- text token
98
- }
46
+ `input`, `stack`, and `response` are required by the MCP validator. The server supplies the fixed `vars` and `type` inputs. Custom middleware inputs are not a way to pass parameters from an API.
99
47
 
100
- stack {
101
- precondition ($input.token|strlen > 0) {
102
- error_type = "accessdenied"
103
- error = "Token required"
104
- }
105
- }
106
-
107
- response = { valid: true }
108
- }
109
- ```
110
-
111
- ### critical
112
- Errors are treated as critical failures. Request is immediately terminated.
48
+ ### 2. Create an API group
113
49
 
114
50
  ```xs
115
- middleware "security_check" {
116
- exception_policy = "critical"
117
-
118
- input {
119
- text ip_address
120
- }
121
-
122
- stack {
123
- db.query "blocked_ips" {
124
- where = $db.blocked_ips.ip == $input.ip_address
125
- return = { type: "exists" }
126
- } as $is_blocked
127
-
128
- precondition (!$is_blocked) {
129
- error_type = "accessdenied"
130
- error = "Access denied"
131
- }
132
- }
133
-
134
- response = { allowed: true }
51
+ api_group docs_audit {
52
+ canonical = "docs-demo"
135
53
  }
136
54
  ```
137
55
 
138
- ---
139
-
140
- ## Response Strategies
141
-
142
- Control how middleware response combines with the original response:
56
+ Use a unique canonical for a new group. Preserve the existing canonical when editing a group.
143
57
 
144
- ### merge (default)
145
- Middleware response is merged with original response.
58
+ ### 3. Attach the middleware inside the query
146
59
 
147
60
  ```xs
148
- middleware "add_metadata" {
149
- response_strategy = "merge"
150
-
61
+ query check verb=GET {
62
+ api_group = "docs_audit"
151
63
  input {
152
64
  }
153
-
154
65
  stack {
155
- var $meta {
156
- value = {
157
- server_time: now,
158
- version: "1.0.0"
159
- }
160
- }
66
+ var $result { value = {ok: true} }
161
67
  }
162
-
163
- response = { _meta: $meta }
68
+ response = $result
69
+ middleware = {post: [{name: "docs_post"}]}
164
70
  }
165
- // Original: { data: [...] }
166
- // Result: { data: [...], _meta: { server_time: ..., version: "1.0.0" } }
167
71
  ```
168
72
 
169
- ### replace
170
- Middleware response completely replaces original response.
73
+ The middleware block is a sibling of `input`, `stack`, and `response`. Do not put it inside `stack`, and do not add `type = "post"` to the middleware declaration.
171
74
 
172
- ```xs
173
- middleware "transform_response" {
174
- response_strategy = "replace"
175
-
176
- input {
177
- json original_response
178
- }
75
+ Request `GET /api:docs-demo/check` on the instance. The live response is:
179
76
 
180
- stack {
181
- var $transformed {
182
- value = {
183
- success: true,
184
- payload: $input.original_response
185
- }
186
- }
187
- }
188
-
189
- response = $transformed
77
+ ```json
78
+ {
79
+ "ok": true,
80
+ "middleware_phase": "post",
81
+ "observed": {"status": "ok", "result": {"ok": true}}
190
82
  }
191
83
  ```
192
84
 
193
- ---
85
+ The `ok` field comes from the API. The other fields prove that the middleware ran in the post phase and saw the endpoint's response. Returning an object with `merge` adds its fields to the successful response object. This example deliberately echoes the envelope for inspection; a production middleware should return only the intended public fields.
194
86
 
195
- ## Common Patterns
87
+ ## Response Envelope and Replacement
196
88
 
197
- ### Request Logging
89
+ For the successful API above, `$input.vars` is `{status: "ok", result: {ok: true}}`. To wrap the endpoint's payload, use `$input.vars.result`, not the whole envelope. Do not assume this successful-response shape describes an error path or the pre phase.
198
90
 
199
91
  ```xs
200
- middleware "audit_log" {
201
- description = "Logs all API requests for audit trail"
202
- exception_policy = "silent"
203
-
204
- input {
205
- text endpoint
206
- text method
207
- json request_body?
208
- int user_id?
209
- }
210
-
211
- stack {
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
220
- }
221
- }
222
- }
223
-
224
- response = null
225
- }
226
- ```
227
-
228
- ### Rate Limiting
229
-
230
- ```xs
231
- middleware "rate_limit" {
232
- description = "Enforces rate limits per user"
92
+ middleware docs_wrap {
233
93
  exception_policy = "rethrow"
234
-
94
+ response_strategy = "replace"
235
95
  input {
236
- int user_id
237
- int max_requests?=100
238
- int window_seconds?=60
96
+ json vars
97
+ enum type {
98
+ values = ["pre", "post"]
99
+ }
239
100
  }
240
-
241
101
  stack {
242
- redis.ratelimit {
243
- key = "ratelimit:" ~ $input.user_id
244
- max = $input.max_requests
245
- ttl = $input.window_seconds
246
- error = "Rate limit exceeded"
247
- }
102
+ var $payload { value = $input.vars.result }
248
103
  }
249
-
250
- response = null
104
+ response = {success: true, payload: $payload}
251
105
  }
252
106
  ```
253
107
 
254
- ### Response Caching
108
+ Attach `docs_wrap` as the query's post middleware:
255
109
 
256
110
  ```xs
257
- middleware "cache_response" {
258
- description = "Caches responses in Redis"
259
- exception_policy = "silent"
260
-
111
+ query wrap verb=GET {
112
+ api_group = "docs_audit"
261
113
  input {
262
- text cache_key
263
- int ttl_seconds?=300
264
- json response_data
265
114
  }
266
-
267
115
  stack {
268
- redis.set {
269
- key = $input.cache_key
270
- value = $input.response_data|json_encode
271
- expire = $input.ttl_seconds
272
- }
116
+ var $result { value = {ok: true} }
273
117
  }
274
-
275
- response = null
118
+ response = $result
119
+ middleware = {post: [{name: "docs_wrap"}]}
276
120
  }
277
121
  ```
278
122
 
279
- ---
123
+ Request `GET /api:docs-demo/wrap`. The expected response is:
280
124
 
281
- ## Applying Middleware
125
+ ```json
126
+ {"success": true, "payload": {"ok": true}}
127
+ ```
282
128
 
283
- Middleware is configured at the branch level. See `xano_xanoscript_docs({ topic: "branch" })` for configuration details.
129
+ With `replace`, the middleware's response becomes the endpoint response. With `merge`, the original successful response fields remain alongside middleware fields.
284
130
 
285
- ```xs
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
- }
296
- }
297
- }
298
- ```
131
+ ## Validate and Verify
299
132
 
300
- ---
133
+ 1. Validate every definition with `xano_validate_xanoscript`.
134
+ 2. Preview the scoped workspace push with `--dry-run`, then push the middleware, group, and API together.
135
+ 3. Pull the workspace again and inspect the query's `middleware` attachment.
136
+ 4. Call the real endpoint and assert the final HTTP response. Parsing or a successful push alone does not prove that middleware executed.
301
137
 
302
- ## Best Practices
138
+ The repository's `examples/realtime-v2/verify.mjs` exercises both merge and replace against a live workspace. Its target is configured in `examples/realtime-v2/frontend/config.json`.
303
139
 
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
140
+ ## Common Mistakes
307
141
 
308
- ---
142
+ - **Defining without attaching:** a middleware declaration alone does not attach it to an API. Put the `middleware` block inside the query.
143
+ - **Using string entries:** use `post: [{name: "docs_post"}]`, not `post: ["docs_post"]`.
144
+ - **Inventing middleware inputs:** read the server-provided `$input.vars` and `$input.type`.
145
+ - **Wrapping the wrong value:** for a successful post response, wrap `$input.vars.result`; `$input.vars` also includes execution status.
146
+ - **Trusting validation alone:** check the pulled attachment and execute the endpoint after pushing.
309
147
 
310
148
  ## Related Topics
311
149
 
312
150
  | Topic | Description |
313
151
  |-------|-------------|
314
- | `branch` | Branch-level middleware configuration |
315
- | `security` | Authentication and authorization patterns |
316
- | `apis` | API endpoint request/response lifecycle |
317
- | `performance` | Performance optimization strategies |
152
+ | `apis` | API declarations and responses |
153
+ | `realtime-v2` | Message handlers and live server publishing |
154
+ | `security` | Authentication and authorization |