@scoheart/x-search-mcp 0.3.0 → 0.6.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.
Files changed (105) hide show
  1. package/.env.example +7 -10
  2. package/README.md +29 -17
  3. package/THIRD_PARTY_NOTICES.md +4 -0
  4. package/TOOL.md +58 -77
  5. package/dist/evidence-licenses.txt +791 -0
  6. package/dist/evidence.html +170 -0
  7. package/dist/package.json +11 -3
  8. package/dist/src/auth.d.ts.map +1 -1
  9. package/dist/src/auth.js +67 -57
  10. package/dist/src/auth.js.map +1 -1
  11. package/dist/src/call-log.d.ts +5 -4
  12. package/dist/src/call-log.d.ts.map +1 -1
  13. package/dist/src/call-log.js +82 -54
  14. package/dist/src/call-log.js.map +1 -1
  15. package/dist/src/cli.d.ts.map +1 -1
  16. package/dist/src/cli.js +26 -19
  17. package/dist/src/cli.js.map +1 -1
  18. package/dist/src/config.d.ts.map +1 -1
  19. package/dist/src/config.js +12 -16
  20. package/dist/src/config.js.map +1 -1
  21. package/dist/src/device-login.d.ts +20 -0
  22. package/dist/src/device-login.d.ts.map +1 -0
  23. package/dist/src/device-login.js +110 -0
  24. package/dist/src/device-login.js.map +1 -0
  25. package/dist/src/events.d.ts +16 -0
  26. package/dist/src/events.d.ts.map +1 -0
  27. package/dist/src/events.js +128 -0
  28. package/dist/src/events.js.map +1 -0
  29. package/dist/src/evidence-app.d.ts +4 -0
  30. package/dist/src/evidence-app.d.ts.map +1 -0
  31. package/dist/src/evidence-app.js +17 -0
  32. package/dist/src/evidence-app.js.map +1 -0
  33. package/dist/src/http-server.d.ts +1 -0
  34. package/dist/src/http-server.d.ts.map +1 -1
  35. package/dist/src/http-server.js +6 -4
  36. package/dist/src/http-server.js.map +1 -1
  37. package/dist/src/http.d.ts +12 -15
  38. package/dist/src/http.d.ts.map +1 -1
  39. package/dist/src/http.js +78 -118
  40. package/dist/src/http.js.map +1 -1
  41. package/dist/src/index.d.ts +1 -0
  42. package/dist/src/index.d.ts.map +1 -1
  43. package/dist/src/index.js.map +1 -1
  44. package/dist/src/multi-user/service.d.ts.map +1 -1
  45. package/dist/src/multi-user/service.js +3 -2
  46. package/dist/src/multi-user/service.js.map +1 -1
  47. package/dist/src/search-input.d.ts +31 -0
  48. package/dist/src/search-input.d.ts.map +1 -0
  49. package/dist/src/search-input.js +102 -0
  50. package/dist/src/search-input.js.map +1 -0
  51. package/dist/src/search-response.d.ts +26 -0
  52. package/dist/src/search-response.d.ts.map +1 -0
  53. package/dist/src/search-response.js +128 -0
  54. package/dist/src/search-response.js.map +1 -0
  55. package/dist/src/search.d.ts +7 -20
  56. package/dist/src/search.d.ts.map +1 -1
  57. package/dist/src/search.js +129 -440
  58. package/dist/src/search.js.map +1 -1
  59. package/dist/src/server.d.ts +8 -9
  60. package/dist/src/server.d.ts.map +1 -1
  61. package/dist/src/server.js +110 -193
  62. package/dist/src/server.js.map +1 -1
  63. package/dist/src/tool-contract.d.ts +183 -0
  64. package/dist/src/tool-contract.d.ts.map +1 -0
  65. package/dist/src/tool-contract.js +88 -0
  66. package/dist/src/tool-contract.js.map +1 -0
  67. package/dist/src/types.d.ts +7 -37
  68. package/dist/src/types.d.ts.map +1 -1
  69. package/dist/src/watch-cli.d.ts +2 -0
  70. package/dist/src/watch-cli.d.ts.map +1 -0
  71. package/dist/src/watch-cli.js +41 -0
  72. package/dist/src/watch-cli.js.map +1 -0
  73. package/dist/src/watch-store.d.ts +41 -0
  74. package/dist/src/watch-store.d.ts.map +1 -0
  75. package/dist/src/watch-store.js +171 -0
  76. package/dist/src/watch-store.js.map +1 -0
  77. package/dist/ui/evidence.d.ts +2 -0
  78. package/dist/ui/evidence.d.ts.map +1 -0
  79. package/dist/ui/evidence.js +150 -0
  80. package/dist/ui/evidence.js.map +1 -0
  81. package/docs/events-and-apps.md +40 -0
  82. package/examples/codex.toml +6 -7
  83. package/examples/mcp.json +2 -6
  84. package/package.json +11 -3
  85. package/server.json +34 -0
  86. package/src/auth.ts +65 -57
  87. package/src/call-log.ts +83 -53
  88. package/src/cli.ts +26 -19
  89. package/src/config.ts +11 -13
  90. package/src/device-login.ts +120 -0
  91. package/src/events.ts +124 -0
  92. package/src/evidence-app.ts +19 -0
  93. package/src/http-server.ts +7 -4
  94. package/src/http.ts +83 -162
  95. package/src/index.ts +1 -0
  96. package/src/multi-user/service.ts +3 -2
  97. package/src/search-input.ts +109 -0
  98. package/src/search-response.ts +122 -0
  99. package/src/search.ts +131 -476
  100. package/src/server.ts +114 -207
  101. package/src/tool-contract.ts +96 -0
  102. package/src/types.ts +8 -40
  103. package/src/watch-cli.ts +35 -0
  104. package/src/watch-store.ts +182 -0
  105. package/tsconfig.json +2 -1
@@ -0,0 +1,182 @@
1
+ import { randomBytes, randomUUID, createHmac, timingSafeEqual } from 'node:crypto';
2
+ import { closeSync, constants, fstatSync, mkdirSync, openSync, statSync } from 'node:fs';
3
+ import { homedir } from 'node:os';
4
+ import { dirname, resolve } from 'node:path';
5
+ import { DatabaseSync } from 'node:sqlite';
6
+ import { validateSearchParams } from './search-input.js';
7
+ import type { SearchSource } from './tool-contract.js';
8
+
9
+ const day = 86_400_000;
10
+ const retention = 7 * day;
11
+ const columns = 'id, query, interval_ms, runs_left, next_run_at, expires_at, last_status';
12
+ type Watch = {
13
+ id: string; query: string; interval_ms: number; runs_left: number;
14
+ next_run_at: number; expires_at: number; last_status: string | null;
15
+ };
16
+ type Post = Extract<SearchSource, { kind: 'x_post' }>;
17
+ type Evidence = { seq: number; observed_at: number; post_id: string; url: string };
18
+ export class InvalidWatchRequest extends Error {}
19
+
20
+ export function watchDatabasePath() {
21
+ return resolve(process.env.X_SEARCH_EVENTS_DB ?? resolve(homedir(), '.config/x-search-mcp/events/watches.sqlite'));
22
+ }
23
+
24
+ function privateFile(file: string) {
25
+ const directory = dirname(file);
26
+ mkdirSync(directory, { recursive: true, mode: 0o700 });
27
+ const parent = statSync(directory);
28
+ const uid = process.getuid?.();
29
+ if (!parent.isDirectory() || (uid !== undefined && (parent.mode & 0o077 || parent.uid !== uid))) {
30
+ throw new Error('The watch database requires a private, user-owned directory.');
31
+ }
32
+ const fd = openSync(file, constants.O_RDWR | constants.O_CREAT | constants.O_NOFOLLOW | constants.O_NONBLOCK, 0o600);
33
+ try {
34
+ const info = fstatSync(fd);
35
+ if (!info.isFile() || (uid !== undefined && (info.mode & 0o077 || info.uid !== uid))) {
36
+ throw new Error('The watch database must be a private, user-owned file.');
37
+ }
38
+ } finally {
39
+ closeSync(fd);
40
+ }
41
+ }
42
+
43
+ function cursorFor(key: string, seq: number) {
44
+ const position = String(seq);
45
+ const signature = createHmac('sha256', key).update(position).digest('base64url');
46
+ return position + '.' + signature;
47
+ }
48
+
49
+ function parseCursor(key: string, cursor: string) {
50
+ const seq = Number(cursor.split('.')[0]);
51
+ if (!Number.isSafeInteger(seq) || seq < 0) throw new InvalidWatchRequest('Invalid event cursor for this watch.');
52
+ const expected = Buffer.from(cursorFor(key, seq));
53
+ const received = Buffer.from(cursor);
54
+ if (received.length !== expected.length || !timingSafeEqual(received, expected)) {
55
+ throw new InvalidWatchRequest('Invalid event cursor for this watch.');
56
+ }
57
+ return seq;
58
+ }
59
+
60
+ export class WatchStore {
61
+ private readonly db: DatabaseSync;
62
+
63
+ constructor(file: string) {
64
+ privateFile(file);
65
+ this.db = new DatabaseSync(file);
66
+ try {
67
+ this.db.exec(`
68
+ PRAGMA busy_timeout = 1000;
69
+ PRAGMA foreign_keys = ON;
70
+ PRAGMA secure_delete = ON;
71
+ CREATE TABLE IF NOT EXISTS watches (
72
+ id TEXT PRIMARY KEY, query TEXT NOT NULL, interval_ms INTEGER NOT NULL,
73
+ runs_left INTEGER NOT NULL, next_run_at INTEGER NOT NULL, expires_at INTEGER NOT NULL,
74
+ last_status TEXT, cursor_key TEXT NOT NULL
75
+ ) STRICT;
76
+ CREATE TABLE IF NOT EXISTS evidence (
77
+ seq INTEGER PRIMARY KEY AUTOINCREMENT, watch_id TEXT NOT NULL REFERENCES watches(id) ON DELETE CASCADE,
78
+ observed_at INTEGER NOT NULL, post_id TEXT NOT NULL, url TEXT NOT NULL,
79
+ UNIQUE(watch_id, post_id)
80
+ ) STRICT;
81
+ CREATE INDEX IF NOT EXISTS evidence_by_watch ON evidence(watch_id, seq);
82
+ `);
83
+ } catch (error) {
84
+ this.db.close();
85
+ throw error;
86
+ }
87
+ }
88
+
89
+ add(query: string, intervalMinutes: number, runs: number, now = Date.now()): Watch {
90
+ const input = validateSearchParams({ query });
91
+ const interval = intervalMinutes * 60_000;
92
+ if (!Number.isSafeInteger(intervalMinutes) || intervalMinutes < 5 || intervalMinutes > 1440
93
+ || !Number.isSafeInteger(runs) || runs < 1 || runs > 48 || runs * interval > retention) {
94
+ throw new Error('Choose an interval of 5–1440 minutes and 1–48 checks within seven days.');
95
+ }
96
+ this.prune(now);
97
+ const id = randomUUID();
98
+ const watch: Watch = {
99
+ id, query: input.query, interval_ms: interval, runs_left: runs,
100
+ next_run_at: now + interval, expires_at: now + retention, last_status: null,
101
+ };
102
+ const inserted = this.db.prepare(`
103
+ INSERT INTO watches (id, query, interval_ms, runs_left, next_run_at, expires_at, cursor_key)
104
+ SELECT ?, ?, ?, ?, ?, ?, ?
105
+ WHERE (SELECT count(*) FROM watches) < 64
106
+ AND (SELECT count(*) FROM watches WHERE runs_left > 0 AND expires_at > ?) < 8
107
+ `).run(id, watch.query, interval, runs, watch.next_run_at, watch.expires_at, randomBytes(32).toString('hex'), now);
108
+ if (!inserted.changes) throw new Error('Remove an existing watch before adding another.');
109
+ return watch;
110
+ }
111
+
112
+ list(now = Date.now()) {
113
+ this.prune(now);
114
+ return this.db.prepare(`SELECT ${columns} FROM watches ORDER BY next_run_at, id`).all() as Watch[];
115
+ }
116
+
117
+ remove(id: string) {
118
+ return this.db.prepare('DELETE FROM watches WHERE id = ?').run(id).changes > 0;
119
+ }
120
+
121
+ claim(now = Date.now()): Pick<Watch, 'id' | 'query'> | undefined {
122
+ this.prune(now);
123
+ return this.db.prepare(`
124
+ UPDATE watches SET runs_left = runs_left - 1, next_run_at = ? + interval_ms, last_status = 'running'
125
+ WHERE id = (SELECT id FROM watches WHERE runs_left > 0 AND next_run_at <= ? AND expires_at > ?
126
+ ORDER BY next_run_at, id LIMIT 1)
127
+ RETURNING id, query
128
+ `).get(now, now, now) as Pick<Watch, 'id' | 'query'> | undefined;
129
+ }
130
+
131
+ record(id: string, status: string, posts: readonly Post[] = [], now = Date.now()) {
132
+ this.db.exec('BEGIN IMMEDIATE');
133
+ try {
134
+ const watch = this.db.prepare('UPDATE watches SET last_status = ? WHERE id = ?').run(status, id);
135
+ if (watch.changes) {
136
+ const insert = this.db.prepare(`
137
+ INSERT INTO evidence (watch_id, observed_at, post_id, url) VALUES (?, ?, ?, ?)
138
+ ON CONFLICT(watch_id, post_id) DO NOTHING
139
+ `);
140
+ for (const post of posts.slice(0, 8)) insert.run(id, now, post.post_id, post.url);
141
+ }
142
+ this.db.exec('COMMIT');
143
+ } catch (error) {
144
+ this.db.exec('ROLLBACK');
145
+ throw error;
146
+ }
147
+ }
148
+
149
+ poll(id: string, cursor: string | null, maxEvents: number, maxAgeMs?: number, now = Date.now()) {
150
+ const watch = this.db.prepare('SELECT cursor_key FROM watches WHERE id = ? AND expires_at > ?')
151
+ .get(id, now - retention) as { cursor_key: string } | undefined;
152
+ if (!watch) throw new InvalidWatchRequest('Unknown or removed watch.');
153
+
154
+ const latest = this.db.prepare('SELECT coalesce(max(seq), 0) AS seq FROM evidence WHERE watch_id = ?')
155
+ .get(id) as { seq: number };
156
+ if (cursor === null) return { rows: [], cursor: cursorFor(watch.cursor_key, latest.seq), truncated: false, hasMore: false };
157
+ const position = parseCursor(watch.cursor_key, cursor);
158
+ if (position > latest.seq) throw new InvalidWatchRequest('Invalid event cursor for this watch.');
159
+ const ageFloor = now - Math.min(maxAgeMs ?? retention, retention);
160
+ const skipped = this.db.prepare(`
161
+ SELECT coalesce(max(seq), 0) AS seq FROM evidence
162
+ WHERE watch_id = ? AND seq > ? AND seq <= ? AND observed_at < ?
163
+ `).get(id, position, latest.seq, ageFloor) as { seq: number };
164
+ const start = Math.max(position, skipped.seq);
165
+ const rows = this.db.prepare(`
166
+ SELECT seq, observed_at, post_id, url FROM evidence WHERE watch_id = ? AND seq > ? AND seq <= ?
167
+ ORDER BY seq LIMIT ?
168
+ `).all(id, start, latest.seq, maxEvents + 1) as Evidence[];
169
+ const hasMore = rows.length > maxEvents;
170
+ if (hasMore) rows.pop();
171
+ return {
172
+ rows, hasMore, truncated: start > position,
173
+ cursor: cursorFor(watch.cursor_key, hasMore ? rows.at(-1)!.seq : latest.seq),
174
+ };
175
+ }
176
+
177
+ private prune(now: number) {
178
+ this.db.prepare('DELETE FROM watches WHERE expires_at <= ?').run(now - retention);
179
+ }
180
+
181
+ close() { this.db.close(); }
182
+ }
package/tsconfig.json CHANGED
@@ -17,7 +17,8 @@
17
17
  },
18
18
  "include": [
19
19
  "src/**/*",
20
- "bin/**/*"
20
+ "bin/**/*",
21
+ "ui/**/*.ts"
21
22
  ],
22
23
  "exclude": [
23
24
  "node_modules",