@agents-txt/express 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 agents-txt 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/dist/index.cjs ADDED
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ RateLimiter: () => RateLimiter,
24
+ agentsTxt: () => agentsTxt
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/middleware.ts
29
+ var import_core = require("@agents-txt/core");
30
+
31
+ // src/rate-limiter.ts
32
+ var RateLimiter = class {
33
+ windows = /* @__PURE__ */ new Map();
34
+ cleanupTimer;
35
+ windowMs;
36
+ defaultLimit;
37
+ maxEntries;
38
+ constructor(options = {}) {
39
+ this.windowMs = options.windowMs ?? 6e4;
40
+ this.defaultLimit = options.defaultLimit ?? 60;
41
+ this.maxEntries = options.maxEntries ?? 1e4;
42
+ this.cleanupTimer = setInterval(() => {
43
+ const now = Date.now();
44
+ for (const [key, entry] of this.windows) {
45
+ entry.timestamps = entry.timestamps.filter((t) => now - t < this.windowMs);
46
+ if (entry.timestamps.length === 0) this.windows.delete(key);
47
+ }
48
+ }, options.cleanupIntervalMs ?? 6e4);
49
+ }
50
+ check(key, limit) {
51
+ const effectiveLimit = limit ?? this.defaultLimit;
52
+ const now = Date.now();
53
+ if (!this.windows.has(key) && this.windows.size >= this.maxEntries) {
54
+ return { allowed: false, remaining: 0, retryAfterMs: this.windowMs };
55
+ }
56
+ let entry = this.windows.get(key);
57
+ if (!entry) {
58
+ entry = { timestamps: [] };
59
+ this.windows.set(key, entry);
60
+ }
61
+ entry.timestamps = entry.timestamps.filter((t) => now - t < this.windowMs);
62
+ if (entry.timestamps.length >= effectiveLimit) {
63
+ const retryAfterMs = this.windowMs - (now - entry.timestamps[0]);
64
+ return { allowed: false, remaining: 0, retryAfterMs };
65
+ }
66
+ entry.timestamps.push(now);
67
+ return { allowed: true, remaining: effectiveLimit - entry.timestamps.length };
68
+ }
69
+ destroy() {
70
+ clearInterval(this.cleanupTimer);
71
+ this.windows.clear();
72
+ }
73
+ };
74
+
75
+ // src/middleware.ts
76
+ function agentsTxt(options) {
77
+ const doc = {
78
+ specVersion: "1.0",
79
+ generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
80
+ site: options.site,
81
+ capabilities: options.capabilities,
82
+ access: options.access ?? { allow: ["*"], disallow: [] },
83
+ agents: options.agents ?? { "*": {} }
84
+ };
85
+ const txtContent = (0, import_core.generate)(doc);
86
+ const jsonContent = (0, import_core.generateJSON)(doc);
87
+ const txtPath = options.paths?.txt ?? "/.well-known/agents.txt";
88
+ const jsonPath = options.paths?.json ?? "/.well-known/agents.json";
89
+ const rateLimiter = options.rateLimit !== false ? new RateLimiter({ defaultLimit: (options.rateLimit && options.rateLimit.defaultLimit) ?? 60 }) : null;
90
+ const corsOrigins = options.corsOrigins ?? ["*"];
91
+ return function agentsTxtMiddleware(req, res, next) {
92
+ if (req.path !== txtPath && req.path !== jsonPath) {
93
+ next();
94
+ return;
95
+ }
96
+ const origin = req.headers.origin;
97
+ if (corsOrigins.includes("*")) {
98
+ res.setHeader("Access-Control-Allow-Origin", "*");
99
+ } else if (origin && corsOrigins.includes(origin)) {
100
+ res.setHeader("Access-Control-Allow-Origin", origin);
101
+ }
102
+ res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
103
+ res.setHeader("Vary", "Origin");
104
+ res.setHeader("X-Content-Type-Options", "nosniff");
105
+ res.setHeader("X-Frame-Options", "DENY");
106
+ if (req.method === "OPTIONS") {
107
+ res.status(204).end();
108
+ return;
109
+ }
110
+ if (rateLimiter) {
111
+ const ip = req.ip ?? req.socket?.remoteAddress ?? "unknown";
112
+ const result = rateLimiter.check(ip);
113
+ res.setHeader("X-RateLimit-Remaining", String(result.remaining));
114
+ if (!result.allowed) {
115
+ res.setHeader("Retry-After", String(Math.ceil((result.retryAfterMs || 6e4) / 1e3)));
116
+ res.status(429).json({ error: "Rate limit exceeded" });
117
+ return;
118
+ }
119
+ }
120
+ if (req.path === txtPath) {
121
+ res.setHeader("Content-Type", "text/plain; charset=utf-8");
122
+ res.setHeader("Cache-Control", "public, max-age=300");
123
+ res.send(txtContent);
124
+ } else {
125
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
126
+ res.setHeader("Cache-Control", "public, max-age=300");
127
+ res.send(jsonContent);
128
+ }
129
+ };
130
+ }
131
+ // Annotate the CommonJS export names for ESM import in node:
132
+ 0 && (module.exports = {
133
+ RateLimiter,
134
+ agentsTxt
135
+ });
@@ -0,0 +1,53 @@
1
+ import { SiteInfo, Capability, AccessControl, AgentPolicy } from '@agents-txt/core';
2
+ import { Request, Response, NextFunction } from 'express';
3
+
4
+ interface AgentsTxtOptions {
5
+ /** Site identity. */
6
+ site: SiteInfo;
7
+ /** Capabilities to declare. */
8
+ capabilities: Capability[];
9
+ /** Access control rules. Default: allow all. */
10
+ access?: AccessControl;
11
+ /** Per-agent policies. Default: wildcard. */
12
+ agents?: Record<string, AgentPolicy>;
13
+ /** Rate limiting options. Set to false to disable. */
14
+ rateLimit?: {
15
+ enabled?: boolean;
16
+ defaultLimit?: number;
17
+ } | false;
18
+ /** Allowed CORS origins. Default: ["*"]. */
19
+ corsOrigins?: string[];
20
+ /** Serve paths. Default: /.well-known/agents.txt and /.well-known/agents.json */
21
+ paths?: {
22
+ txt?: string;
23
+ json?: string;
24
+ };
25
+ }
26
+ declare function agentsTxt(options: AgentsTxtOptions): (req: Request, res: Response, next: NextFunction) => void;
27
+
28
+ interface RateLimiterOptions {
29
+ /** Default requests per window. Default: 60. */
30
+ defaultLimit?: number;
31
+ /** Window size in ms. Default: 60000 (1 minute). */
32
+ windowMs?: number;
33
+ /** Cleanup interval in ms. Default: 60000. */
34
+ cleanupIntervalMs?: number;
35
+ /** Max tracked keys (OOM protection). Default: 10000. */
36
+ maxEntries?: number;
37
+ }
38
+ declare class RateLimiter {
39
+ private windows;
40
+ private cleanupTimer;
41
+ private windowMs;
42
+ private defaultLimit;
43
+ private maxEntries;
44
+ constructor(options?: RateLimiterOptions);
45
+ check(key: string, limit?: number): {
46
+ allowed: boolean;
47
+ remaining: number;
48
+ retryAfterMs?: number;
49
+ };
50
+ destroy(): void;
51
+ }
52
+
53
+ export { type AgentsTxtOptions, RateLimiter, type RateLimiterOptions, agentsTxt };
@@ -0,0 +1,53 @@
1
+ import { SiteInfo, Capability, AccessControl, AgentPolicy } from '@agents-txt/core';
2
+ import { Request, Response, NextFunction } from 'express';
3
+
4
+ interface AgentsTxtOptions {
5
+ /** Site identity. */
6
+ site: SiteInfo;
7
+ /** Capabilities to declare. */
8
+ capabilities: Capability[];
9
+ /** Access control rules. Default: allow all. */
10
+ access?: AccessControl;
11
+ /** Per-agent policies. Default: wildcard. */
12
+ agents?: Record<string, AgentPolicy>;
13
+ /** Rate limiting options. Set to false to disable. */
14
+ rateLimit?: {
15
+ enabled?: boolean;
16
+ defaultLimit?: number;
17
+ } | false;
18
+ /** Allowed CORS origins. Default: ["*"]. */
19
+ corsOrigins?: string[];
20
+ /** Serve paths. Default: /.well-known/agents.txt and /.well-known/agents.json */
21
+ paths?: {
22
+ txt?: string;
23
+ json?: string;
24
+ };
25
+ }
26
+ declare function agentsTxt(options: AgentsTxtOptions): (req: Request, res: Response, next: NextFunction) => void;
27
+
28
+ interface RateLimiterOptions {
29
+ /** Default requests per window. Default: 60. */
30
+ defaultLimit?: number;
31
+ /** Window size in ms. Default: 60000 (1 minute). */
32
+ windowMs?: number;
33
+ /** Cleanup interval in ms. Default: 60000. */
34
+ cleanupIntervalMs?: number;
35
+ /** Max tracked keys (OOM protection). Default: 10000. */
36
+ maxEntries?: number;
37
+ }
38
+ declare class RateLimiter {
39
+ private windows;
40
+ private cleanupTimer;
41
+ private windowMs;
42
+ private defaultLimit;
43
+ private maxEntries;
44
+ constructor(options?: RateLimiterOptions);
45
+ check(key: string, limit?: number): {
46
+ allowed: boolean;
47
+ remaining: number;
48
+ retryAfterMs?: number;
49
+ };
50
+ destroy(): void;
51
+ }
52
+
53
+ export { type AgentsTxtOptions, RateLimiter, type RateLimiterOptions, agentsTxt };
package/dist/index.js ADDED
@@ -0,0 +1,107 @@
1
+ // src/middleware.ts
2
+ import { generate, generateJSON } from "@agents-txt/core";
3
+
4
+ // src/rate-limiter.ts
5
+ var RateLimiter = class {
6
+ windows = /* @__PURE__ */ new Map();
7
+ cleanupTimer;
8
+ windowMs;
9
+ defaultLimit;
10
+ maxEntries;
11
+ constructor(options = {}) {
12
+ this.windowMs = options.windowMs ?? 6e4;
13
+ this.defaultLimit = options.defaultLimit ?? 60;
14
+ this.maxEntries = options.maxEntries ?? 1e4;
15
+ this.cleanupTimer = setInterval(() => {
16
+ const now = Date.now();
17
+ for (const [key, entry] of this.windows) {
18
+ entry.timestamps = entry.timestamps.filter((t) => now - t < this.windowMs);
19
+ if (entry.timestamps.length === 0) this.windows.delete(key);
20
+ }
21
+ }, options.cleanupIntervalMs ?? 6e4);
22
+ }
23
+ check(key, limit) {
24
+ const effectiveLimit = limit ?? this.defaultLimit;
25
+ const now = Date.now();
26
+ if (!this.windows.has(key) && this.windows.size >= this.maxEntries) {
27
+ return { allowed: false, remaining: 0, retryAfterMs: this.windowMs };
28
+ }
29
+ let entry = this.windows.get(key);
30
+ if (!entry) {
31
+ entry = { timestamps: [] };
32
+ this.windows.set(key, entry);
33
+ }
34
+ entry.timestamps = entry.timestamps.filter((t) => now - t < this.windowMs);
35
+ if (entry.timestamps.length >= effectiveLimit) {
36
+ const retryAfterMs = this.windowMs - (now - entry.timestamps[0]);
37
+ return { allowed: false, remaining: 0, retryAfterMs };
38
+ }
39
+ entry.timestamps.push(now);
40
+ return { allowed: true, remaining: effectiveLimit - entry.timestamps.length };
41
+ }
42
+ destroy() {
43
+ clearInterval(this.cleanupTimer);
44
+ this.windows.clear();
45
+ }
46
+ };
47
+
48
+ // src/middleware.ts
49
+ function agentsTxt(options) {
50
+ const doc = {
51
+ specVersion: "1.0",
52
+ generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
53
+ site: options.site,
54
+ capabilities: options.capabilities,
55
+ access: options.access ?? { allow: ["*"], disallow: [] },
56
+ agents: options.agents ?? { "*": {} }
57
+ };
58
+ const txtContent = generate(doc);
59
+ const jsonContent = generateJSON(doc);
60
+ const txtPath = options.paths?.txt ?? "/.well-known/agents.txt";
61
+ const jsonPath = options.paths?.json ?? "/.well-known/agents.json";
62
+ const rateLimiter = options.rateLimit !== false ? new RateLimiter({ defaultLimit: (options.rateLimit && options.rateLimit.defaultLimit) ?? 60 }) : null;
63
+ const corsOrigins = options.corsOrigins ?? ["*"];
64
+ return function agentsTxtMiddleware(req, res, next) {
65
+ if (req.path !== txtPath && req.path !== jsonPath) {
66
+ next();
67
+ return;
68
+ }
69
+ const origin = req.headers.origin;
70
+ if (corsOrigins.includes("*")) {
71
+ res.setHeader("Access-Control-Allow-Origin", "*");
72
+ } else if (origin && corsOrigins.includes(origin)) {
73
+ res.setHeader("Access-Control-Allow-Origin", origin);
74
+ }
75
+ res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
76
+ res.setHeader("Vary", "Origin");
77
+ res.setHeader("X-Content-Type-Options", "nosniff");
78
+ res.setHeader("X-Frame-Options", "DENY");
79
+ if (req.method === "OPTIONS") {
80
+ res.status(204).end();
81
+ return;
82
+ }
83
+ if (rateLimiter) {
84
+ const ip = req.ip ?? req.socket?.remoteAddress ?? "unknown";
85
+ const result = rateLimiter.check(ip);
86
+ res.setHeader("X-RateLimit-Remaining", String(result.remaining));
87
+ if (!result.allowed) {
88
+ res.setHeader("Retry-After", String(Math.ceil((result.retryAfterMs || 6e4) / 1e3)));
89
+ res.status(429).json({ error: "Rate limit exceeded" });
90
+ return;
91
+ }
92
+ }
93
+ if (req.path === txtPath) {
94
+ res.setHeader("Content-Type", "text/plain; charset=utf-8");
95
+ res.setHeader("Cache-Control", "public, max-age=300");
96
+ res.send(txtContent);
97
+ } else {
98
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
99
+ res.setHeader("Cache-Control", "public, max-age=300");
100
+ res.send(jsonContent);
101
+ }
102
+ };
103
+ }
104
+ export {
105
+ RateLimiter,
106
+ agentsTxt
107
+ };
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@agents-txt/express",
3
+ "version": "0.1.0",
4
+ "description": "Express middleware for serving agents.txt — one line to make your site AI-agent ready",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "keywords": [
20
+ "agents.txt",
21
+ "agents-txt",
22
+ "ai-agents",
23
+ "express",
24
+ "middleware",
25
+ "llm",
26
+ "mcp",
27
+ "well-known",
28
+ "robots-txt",
29
+ "capability-declaration",
30
+ "web-standard",
31
+ "anthropic",
32
+ "claude",
33
+ "openai"
34
+ ],
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/kaylacar/agents-txt.git",
38
+ "directory": "packages/express"
39
+ },
40
+ "homepage": "https://github.com/kaylacar/agents-txt",
41
+ "bugs": {
42
+ "url": "https://github.com/kaylacar/agents-txt/issues"
43
+ },
44
+ "dependencies": {
45
+ "@agents-txt/core": "^0.1.0"
46
+ },
47
+ "peerDependencies": {
48
+ "express": "^4.0.0 || ^5.0.0"
49
+ },
50
+ "peerDependenciesMeta": {
51
+ "express": {
52
+ "optional": true
53
+ }
54
+ },
55
+ "devDependencies": {
56
+ "@types/express": "^5.0.0",
57
+ "express": "^5.1.0",
58
+ "tsup": "^8.0.0",
59
+ "typescript": "^5.0.0",
60
+ "vitest": "^4.0.0"
61
+ },
62
+ "license": "MIT",
63
+ "scripts": {
64
+ "build": "tsup src/index.ts --format esm,cjs --dts",
65
+ "test": "vitest run",
66
+ "typecheck": "tsc --noEmit",
67
+ "clean": "rm -rf dist"
68
+ }
69
+ }