@amtp/protocol 1.0.1

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.
Files changed (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +386 -0
  3. package/USAGE_GUIDE.md +722 -0
  4. package/bin/amtp.ts +387 -0
  5. package/dist/client/amtp-client.d.ts +164 -0
  6. package/dist/client/amtp-client.js +460 -0
  7. package/dist/client/amtp-client.js.map +1 -0
  8. package/dist/client/examples/basic-client.d.ts +6 -0
  9. package/dist/client/examples/basic-client.js +35 -0
  10. package/dist/client/examples/basic-client.js.map +1 -0
  11. package/dist/crawler/amtp-crawler.d.ts +125 -0
  12. package/dist/crawler/amtp-crawler.js +359 -0
  13. package/dist/crawler/amtp-crawler.js.map +1 -0
  14. package/dist/crawler/examples/basic-crawler.d.ts +6 -0
  15. package/dist/crawler/examples/basic-crawler.js +28 -0
  16. package/dist/crawler/examples/basic-crawler.js.map +1 -0
  17. package/dist/index.d.ts +15 -0
  18. package/dist/index.js +70 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/server/adapters/fastify-adapter.d.ts +86 -0
  21. package/dist/server/adapters/fastify-adapter.js +169 -0
  22. package/dist/server/adapters/fastify-adapter.js.map +1 -0
  23. package/dist/server/amtp-ql-executor.d.ts +24 -0
  24. package/dist/server/amtp-ql-executor.js +198 -0
  25. package/dist/server/amtp-ql-executor.js.map +1 -0
  26. package/dist/server/amtp-ql-parser.d.ts +30 -0
  27. package/dist/server/amtp-ql-parser.js +212 -0
  28. package/dist/server/amtp-ql-parser.js.map +1 -0
  29. package/dist/server/amtp-server.d.ts +183 -0
  30. package/dist/server/amtp-server.js +650 -0
  31. package/dist/server/amtp-server.js.map +1 -0
  32. package/dist/server/examples/basic-server.d.ts +6 -0
  33. package/dist/server/examples/basic-server.js +215 -0
  34. package/dist/server/examples/basic-server.js.map +1 -0
  35. package/dist/server/examples/saas-dashboard-server.d.ts +44 -0
  36. package/dist/server/examples/saas-dashboard-server.js +387 -0
  37. package/dist/server/examples/saas-dashboard-server.js.map +1 -0
  38. package/dist/server/markdown-parser.d.ts +31 -0
  39. package/dist/server/markdown-parser.js +463 -0
  40. package/dist/server/markdown-parser.js.map +1 -0
  41. package/dist/server/notifications.d.ts +40 -0
  42. package/dist/server/notifications.js +134 -0
  43. package/dist/server/notifications.js.map +1 -0
  44. package/dist/server/permissions.d.ts +40 -0
  45. package/dist/server/permissions.js +156 -0
  46. package/dist/server/permissions.js.map +1 -0
  47. package/dist/server/security.d.ts +127 -0
  48. package/dist/server/security.js +368 -0
  49. package/dist/server/security.js.map +1 -0
  50. package/dist/types/amtp.types.d.ts +720 -0
  51. package/dist/types/amtp.types.js +224 -0
  52. package/dist/types/amtp.types.js.map +1 -0
  53. package/package.json +89 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 AMTP Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,386 @@
1
+ # AMTP: Agent Markdown Transfer Protocol
2
+
3
+ **HTTP for the Agentic Web**
4
+
5
+ A production-grade, markdown-first protocol for AI agents and headless browsers to interact with web services without requiring DOM rendering, CSS engines, or JavaScript execution.
6
+
7
+ ## 🎯 Vision
8
+
9
+ Enable AI agents to browse the web efficiently by providing:
10
+
11
+ - **Semantic Markdown** instead of HTML
12
+ - **Deterministic Structure** for machine parsing
13
+ - **Token Efficiency** for LLM consumption
14
+ - **Hypermedia Navigation** built-in
15
+ - **Streaming Updates** in real-time
16
+ - **Session Awareness** for stateful workflows
17
+ - **Action-First Design** with zero DOM clicks
18
+
19
+ ## ⚡ Quick Start
20
+
21
+ ### Installation
22
+
23
+ ```bash
24
+ npm install @amtp/sdk
25
+ ```
26
+
27
+ ### Using the Agent Client
28
+
29
+ ```typescript
30
+ import { AMTPClient } from "@amtp/sdk";
31
+
32
+ const client = new AMTPClient({
33
+ baseUrl: "https://example.com",
34
+ capabilities: ["actions", "streaming", "forms"],
35
+ });
36
+
37
+ // Fetch a page
38
+ const page = await client.getPage("/products/mbp-14");
39
+ console.log(page.title); // "MacBook Pro 14""
40
+
41
+ // Execute an action
42
+ const result = await client.executeAction("buy", {
43
+ productId: "mbp-14",
44
+ quantity: 1,
45
+ paymentMethod: "credit_card",
46
+ });
47
+
48
+ // Submit a form
49
+ await client.submitForm(checkoutForm, {
50
+ email: "user@example.com",
51
+ address: "123 Main St",
52
+ });
53
+
54
+ // Stream real-time updates
55
+ client.streamUpdates("/api/stream/order", (update) => {
56
+ console.log("Order status:", update);
57
+ });
58
+ ```
59
+
60
+ ### Building an AMTP Server
61
+
62
+ ```typescript
63
+ import { AMTPServer } from "@amtp/sdk";
64
+
65
+ const server = new AMTPServer({
66
+ port: 3000,
67
+ enableCORS: true,
68
+ enableCompression: true,
69
+ });
70
+
71
+ server.register("GET", "/products/:id", async (req, res) => {
72
+ const productId = req.params.id;
73
+ const product = await fetchProduct(productId);
74
+
75
+ const amtpDoc = {
76
+ type: "document",
77
+ title: product.name,
78
+ actions: [
79
+ { id: "buy", label: "BUY", method: "POST", endpoint: "/api/buy" },
80
+ ],
81
+ links: [
82
+ { text: "Reviews", url: `/products/${productId}/reviews` },
83
+ ],
84
+ };
85
+
86
+ res.json(amtpDoc);
87
+ });
88
+
89
+ server.start();
90
+ ```
91
+
92
+ ### Crawling AMTP Sites
93
+
94
+ ```typescript
95
+ import { AMTPCrawler } from "@amtp/sdk";
96
+
97
+ const crawler = new AMTPCrawler({
98
+ baseUrl: "https://example.com",
99
+ maxPages: 10000,
100
+ maxDepth: 5,
101
+ });
102
+
103
+ const pages = await crawler.crawl();
104
+ console.log(`Crawled ${pages.length} pages`);
105
+
106
+ // Search the index
107
+ const results = crawler.search("macbook pro");
108
+ ```
109
+
110
+ ## 📚 Documentation
111
+
112
+ ### Core Specifications
113
+ - [**AMTP RFC** - Full Protocol Specification](./spec/AMTP-RFC.md)
114
+ - [**Markdown Grammar** - Formal EBNF Grammar](./spec/MARKDOWN-GRAMMAR.md)
115
+ - [**Advanced Features** - Sessions, Streaming, Security](./spec/ADVANCED-FEATURES.md)
116
+
117
+ ### Implementations
118
+ - [**Server Implementation** - Express middleware & server](./src/server/)
119
+ - [**Client SDK** - Agent client library](./src/client/)
120
+ - [**Crawler** - Website crawler & indexer](./src/crawler/)
121
+
122
+ ### Examples & Reference
123
+ - [**Real-World Examples** - E-commerce, SaaS, search](./reference-implementations/EXAMPLES.md)
124
+ - [**TypeScript Types** - Protocol type definitions](./src/types/amtp.types.ts)
125
+
126
+ ## 🏗️ Project Structure
127
+
128
+ ```
129
+ AMTP/
130
+ ├── spec/
131
+ │ ├── AMTP-RFC.md # Full RFC specification
132
+ │ ├── MARKDOWN-GRAMMAR.md # Markdown grammar & parsing
133
+ │ ├── ADVANCED-FEATURES.md # Sessions, streaming, security
134
+ │ └── protocols/ # Protocol versions
135
+ ├── src/
136
+ │ ├── types/
137
+ │ │ └── amtp.types.ts # TypeScript type definitions
138
+ │ ├── server/
139
+ │ │ ├── amtp-server.ts # Express server & middleware
140
+ │ │ └── markdown-parser.ts # AMTP markdown parser
141
+ │ ├── client/
142
+ │ │ └── amtp-client.ts # Agent client SDK
143
+ │ └── crawler/
144
+ │ └── amtp-crawler.ts # Website crawler & indexer
145
+ ├── reference-implementations/
146
+ │ └── EXAMPLES.md # Real-world examples
147
+ ├── docs/ # Additional documentation
148
+ └── README.md # This file
149
+ ```
150
+
151
+ ## 🎨 Key Concepts
152
+
153
+ ### 1. Markdown-First
154
+
155
+ Pages are semantic markdown, not HTML:
156
+
157
+ ```markdown
158
+ # MacBook Pro 14"
159
+
160
+ Price: $1,999
161
+ In Stock: Yes
162
+
163
+ ## Actions
164
+
165
+ [BUY] [ADD_TO_CART] [SAVE_FOR_LATER]
166
+
167
+ ## Specifications
168
+
169
+ | Component | Spec |
170
+ |-----------|------|
171
+ | CPU | M3 Pro |
172
+ | RAM | 18GB |
173
+ ```
174
+
175
+ ### 2. Action-Native
176
+
177
+ Semantic actions instead of DOM clicks:
178
+
179
+ ```json
180
+ {
181
+ "action": "buy",
182
+ "parameters": {
183
+ "productId": "mbp-14",
184
+ "quantity": 1,
185
+ "paymentMethod": "credit_card"
186
+ }
187
+ }
188
+ ```
189
+
190
+ ### 3. Content Negotiation
191
+
192
+ Same URL serves multiple formats:
193
+
194
+ ```http
195
+ GET /products/mbp-14
196
+ Accept: text/amtp+markdown → Markdown response (for agents)
197
+ Accept: text/html → HTML response (for browsers)
198
+ Accept: application/json → JSON API response (for clients)
199
+ ```
200
+
201
+ ### 4. Session-Aware
202
+
203
+ Sessions persist state across requests:
204
+
205
+ ```http
206
+ X-Session-ID: sess_abc123
207
+ → Maintains cart, auth, preferences, workflow state
208
+ ```
209
+
210
+ ### 5. Streaming Support
211
+
212
+ Real-time updates via SSE, WebSocket, or HTTP streams:
213
+
214
+ ```
215
+ GET /api/stream/order/ord_123
216
+ → Streams: order_shipped, out_for_delivery, delivered, etc.
217
+ ```
218
+
219
+ ## 🔐 Security
220
+
221
+ AMTP includes built-in security features:
222
+
223
+ - **Input Validation** - Type, format, range, injection prevention
224
+ - **Authentication** - Session tokens, JWT, Bearer tokens, API keys
225
+ - **Authorization** - Permission-based action execution
226
+ - **CSRF Protection** - Token-based protection
227
+ - **Rate Limiting** - Per-agent, per-action rate limits
228
+ - **Encryption** - HTTPS only, encrypted sensitive data
229
+
230
+ ## 📊 Performance
231
+
232
+ AMTP optimizes for:
233
+
234
+ - **Low Latency** - No DOM rendering, direct semantic response
235
+ - **Token Efficiency** - ~90% less tokens than HTML for LLMs
236
+ - **Network Efficiency** - Gzip compression, chunked transfer
237
+ - **Cacheability** - Standard HTTP caching headers
238
+ - **Scalability** - Stateless servers, horizontal scaling
239
+
240
+ Typical Response Times:
241
+ - Product page: 50-100ms
242
+ - Form submission: 200-500ms
243
+ - Checkout flow: 1-3 seconds
244
+
245
+ ## 🤖 Agent Capabilities
246
+
247
+ Agents can declare capabilities:
248
+
249
+ ```http
250
+ X-AMTP-Capabilities: actions,streaming,forms,tools,pagination,multimodal
251
+ ```
252
+
253
+ Server adapts response:
254
+ - ✅ Actions: Always available
255
+ - ✅ Streaming: SSE, WebSocket, HTTP streams
256
+ - ✅ Forms: Complex field types and validation
257
+ - ✅ Tools: External API integration
258
+ - ✅ Pagination: Cursor, offset, infinite scroll
259
+ - ✅ Multimodal: Images, videos, documents
260
+
261
+ ## 📈 Roadmap
262
+
263
+ ### v1.0 (Current)
264
+ - ✅ Core protocol
265
+ - ✅ Markdown grammar
266
+ - ✅ Action system
267
+ - ✅ Forms & navigation
268
+ - ✅ Session management
269
+ - ✅ Basic streaming
270
+ - ✅ Type definitions
271
+ - ✅ Reference implementations
272
+
273
+ ### v1.1 (Q3 2024)
274
+ - [ ] Multimedia support
275
+ - [ ] Natural language descriptions
276
+ - [ ] Advanced filtering
277
+ - [ ] Batch operations
278
+
279
+ ### v2.0 (Q4 2024)
280
+ - [ ] Multi-agent coordination
281
+ - [ ] Distributed sessions
282
+ - [ ] Advanced caching
283
+ - [ ] Webhook support
284
+
285
+ ### v3.0 (Q1 2025)
286
+ - [ ] Voice interface
287
+ - [ ] Mobile optimization
288
+ - [ ] Blockchain integration
289
+ - [ ] Federated identity
290
+
291
+ ## 🔗 Comparisons
292
+
293
+ | Aspect | HTML | JSON API | GraphQL | AMTP |
294
+ |--------|------|----------|---------|------|
295
+ | **Machine-Readable** | ❌ | ✅ | ✅ | ✅ |
296
+ | **Navigation Built-in** | ✅ | ❌ | ❌ | ✅ |
297
+ | **Hypermedia** | ✅ | ❌ | ❌ | ✅ |
298
+ | **LLM-Friendly** | ❌ | ⚠️ | ⚠️ | ✅ |
299
+ | **Token Efficient** | ❌ | ⚠️ | ⚠️ | ✅ |
300
+ | **Low Latency** | ❌ | ✅ | ⚠️ | ✅ |
301
+ | **Cacheable** | ✅ | ✅ | ❌ | ✅ |
302
+ | **Browser Compatible** | ✅ | ❌ | ❌ | ✅* |
303
+
304
+ *Via content negotiation
305
+
306
+ ## 🚀 Getting Started
307
+
308
+ ### For Server Developers
309
+
310
+ ```bash
311
+ # 1. Install SDK
312
+ npm install @amtp/sdk
313
+
314
+ # 2. Follow server implementation guide
315
+ # See: src/server/amtp-server.ts
316
+
317
+ # 3. Register routes
318
+ # See: reference-implementations/EXAMPLES.md
319
+
320
+ # 4. Deploy with Express
321
+ npm start
322
+ ```
323
+
324
+ ### For Agent Developers
325
+
326
+ ```bash
327
+ # 1. Install SDK
328
+ npm install @amtp/sdk
329
+
330
+ # 2. Create client
331
+ const client = new AMTPClient({ baseUrl: "..." });
332
+
333
+ # 3. Build workflows
334
+ # See: src/client/amtp-client.ts
335
+
336
+ # 4. Run agent
337
+ npm run agent
338
+ ```
339
+
340
+ ### For Search/Crawlers
341
+
342
+ ```bash
343
+ # 1. Build crawler
344
+ const crawler = new AMTPCrawler({ baseUrl: "..." });
345
+
346
+ # 2. Crawl website
347
+ await crawler.crawl();
348
+
349
+ # 3. Index results
350
+ # See: src/crawler/amtp-crawler.ts
351
+ ```
352
+
353
+ ## 📝 License
354
+
355
+ MIT License - See LICENSE file
356
+
357
+ ## 🤝 Contributing
358
+
359
+ Contributions welcome! Please:
360
+
361
+ 1. Review the RFC specification
362
+ 2. Check existing implementations
363
+ 3. Follow TypeScript conventions
364
+ 4. Add tests for new features
365
+ 5. Submit pull request with description
366
+
367
+ ## 📧 Contact
368
+
369
+ Questions or ideas? Open an issue or discussion.
370
+
371
+ ---
372
+
373
+ **AMTP is the protocol for the agentic web.**
374
+
375
+ *Designed for AI agents, built on web standards, compatible with everything.*
376
+
377
+ ### Stay Updated
378
+
379
+ - ⭐ Star this repository
380
+ - 🔔 Watch for releases
381
+ - 💬 Join discussions
382
+ - 📮 Subscribe to updates
383
+
384
+ ---
385
+
386
+ **Made with ❤️ for the future of agent-web interactions**